:root {
    --bg: #eef1f5;
    --surface: #ffffff;
    --surface-2: #f7f8fa;
    --surface-3: #eef2f6;
    --text: #17202c;
    --muted: #627083;
    --line: #d9dee7;
    --line-strong: #b9c2d0;
    --brand: #26364f;
    --brand-dark: #17243a;
    --brand-soft: #edf2f8;
    --accent: #9b6a2f;
    --accent-soft: #f7efe5;
    --success: #1f6b52;
    --success-soft: #eaf6f1;
    --danger: #a13d3d;
    --danger-soft: #fbeeee;
    --warning: #8a640e;
    --warning-soft: #fff6df;
    --shadow: 0 18px 48px rgba(23, 32, 44, .10);
    --radius: 8px;
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    color: var(--text);
    background: var(--bg);
}

* { box-sizing: border-box; }
html { min-width: 320px; background: var(--bg); }
body { margin: 0; min-height: 100vh; background: var(--bg); }
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.login-view {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 28px;
    background:
        linear-gradient(90deg, rgba(23, 36, 58, .92), rgba(38, 54, 79, .82)),
        linear-gradient(180deg, #f7f8fa, var(--bg));
}

.login-card {
    width: min(440px, 100%);
    padding: 36px;
    border: 1px solid rgba(255, 255, 255, .18);
    border-radius: 8px;
    background: rgba(255, 255, 255, .97);
    box-shadow: var(--shadow);
}

.login-card h1 { margin: 18px 0 8px; font-size: clamp(1.65rem, 5vw, 2.15rem); line-height: 1.12; letter-spacing: 0; }
.muted { color: var(--muted); line-height: 1.6; }
.brand-mark {
    display: grid;
    place-items: center;
    width: 54px;
    height: 54px;
    border-radius: 8px;
    color: white;
    font-weight: 850;
    letter-spacing: .08em;
    background: var(--brand);
    box-shadow: inset 0 -3px 0 rgba(155, 106, 47, .9);
}
.brand-mark.small { width: 42px; height: 42px; border-radius: 7px; font-size: .82rem; }
.eyebrow { margin: 0; color: var(--accent); font-size: .72rem; font-weight: 850; letter-spacing: .14em; }

.stack { display: grid; gap: 18px; margin-top: 28px; }
label { display: grid; gap: 7px; color: #334155; font-size: .84rem; font-weight: 700; }
input, select, textarea {
    width: 100%;
    border: 1px solid var(--line-strong);
    border-radius: 7px;
    color: var(--text);
    background: var(--surface);
    outline: none;
    transition: border-color .15s ease, box-shadow .15s ease, background .15s ease;
}
input, select { min-height: 42px; padding: 0 12px; }
select {
    appearance: none;
    padding-right: 38px;
    background-image:
        linear-gradient(45deg, transparent 50%, var(--muted) 50%),
        linear-gradient(135deg, var(--muted) 50%, transparent 50%);
    background-position:
        calc(100% - 18px) 18px,
        calc(100% - 13px) 18px;
    background-size: 5px 5px, 5px 5px;
    background-repeat: no-repeat;
}
select:hover:not(:disabled) {
    border-color: var(--line-strong);
    background-color: var(--surface-2);
}
textarea { padding: 12px 13px; resize: vertical; line-height: 1.5; }
input:focus, select:focus, textarea:focus { border-color: var(--brand); box-shadow: 0 0 0 3px rgba(38, 54, 79, .13); }

.button {
    min-height: 40px;
    padding: 0 15px;
    border: 1px solid transparent;
    border-radius: 7px;
    font-weight: 760;
    color: var(--text);
    background: var(--surface);
    transition: transform .12s ease, box-shadow .12s ease, border-color .12s ease, background .12s ease;
}
.button:hover:not(:disabled) { transform: translateY(-1px); }
.button:disabled { cursor: not-allowed; opacity: .48; }
.button.primary { color: white; background: var(--brand); border-color: var(--brand); box-shadow: 0 8px 18px rgba(23, 36, 58, .14); }
.button.primary:hover:not(:disabled) { background: var(--brand-dark); }
.button.ghost { border-color: var(--line); background: var(--surface); }
.button.ghost:hover:not(:disabled) { border-color: var(--line-strong); background: var(--surface-2); }
.button.danger { color: var(--danger); border-color: #ffc9c9; background: var(--danger-soft); }
.button.full { width: 100%; }
.form-error { margin: 0; color: var(--danger); font-size: .86rem; font-weight: 650; line-height: 1.45; }

.app { min-height: 100vh; }
.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 24px;
    max-width: none;
    margin: 0;
    padding: 18px 32px;
    border-bottom: 1px solid var(--line);
    background: var(--surface);
}
.brand { display: flex; align-items: center; gap: 14px; }
.brand h1 { margin: 2px 0 0; font-size: 1.16rem; letter-spacing: 0; }
.topbar-meta { display: flex; align-items: center; gap: 12px; }
.metric { display: grid; min-width: 110px; padding-right: 16px; border-right: 1px solid var(--line); text-align: right; }
.metric strong { font-size: .95rem; }
.metric small { color: var(--muted); font-size: .68rem; }

.workspace { max-width: 1600px; margin: 0 auto; padding: 22px 32px 42px; }
.workspace-header {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    gap: 18px;
    margin-bottom: 14px;
}
.workspace-header h2 { margin: 0; font-size: 1.08rem; font-weight: 820; letter-spacing: 0; }
.workspace-header p { margin: 4px 0 0; color: var(--muted); font-size: .86rem; }
.toolbar {
    display: grid;
    grid-template-columns: minmax(320px, 1fr) repeat(3, minmax(130px, auto)) auto auto auto;
    gap: 10px;
    align-items: center;
    margin-bottom: 12px;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface);
}
.search-box { position: relative; display: block; }
.search-box svg { position: absolute; left: 14px; top: 50%; width: 19px; height: 19px; transform: translateY(-50%); fill: none; stroke: var(--muted); stroke-width: 2; stroke-linecap: round; }
.search-box input { height: 42px; padding-left: 42px; padding-right: 44px; background: var(--surface); }
.search-box kbd { position: absolute; right: 12px; top: 50%; transform: translateY(-50%); padding: 2px 7px; border: 1px solid var(--line); border-radius: 6px; color: var(--muted); background: var(--surface-2); font-size: .72rem; }
.toolbar select { min-width: 130px; background-color: var(--surface); }
.active-search { margin: 0 0 12px; padding: 9px 12px; border-radius: 7px; color: var(--brand); background: var(--brand-soft); font-size: .82rem; }

.table-card { position: relative; overflow: hidden; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); box-shadow: 0 10px 28px rgba(23, 32, 44, .05); }
.table-scroll { overflow: auto; }
table { width: 100%; min-width: 1120px; border-collapse: collapse; }
th, td { padding: 12px 14px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
th { position: sticky; top: 0; z-index: 1; color: var(--muted); background: #f6f7f9; font-size: .72rem; font-weight: 850; letter-spacing: .06em; text-transform: uppercase; }
th[aria-sort="ascending"], th[aria-sort="descending"] { color: var(--brand); background: #f1f4f8; }
.sort-button {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-height: 28px;
    padding: 0;
    border: 0;
    color: inherit;
    background: transparent;
    font: inherit;
    font-weight: inherit;
    letter-spacing: inherit;
    text-transform: inherit;
}
.sort-button::after {
    content: "";
    width: 0;
    height: 0;
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
    opacity: .35;
}
.sort-button.asc::after { border-bottom: 6px solid currentColor; }
.sort-button.desc::after { border-top: 6px solid currentColor; }
.sort-button:not(.active)::after { border-top: 6px solid currentColor; }
.sort-button:hover { color: var(--brand-dark); }
td { font-size: .86rem; }
tbody tr { transition: background .12s ease; }
tbody tr:hover { background: #f8fafc; }
tbody tr:last-child td { border-bottom: 0; }
.cell-id { color: var(--muted); font-variant-numeric: tabular-nums; }
.cell-primary { display: grid; gap: 4px; min-width: 180px; }
.cell-primary strong { font-size: .89rem; }
.cell-secondary { color: var(--muted); font-size: .75rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 220px; }
.badge { display: inline-flex; align-items: center; width: max-content; min-height: 24px; padding: 0 9px; border-radius: 6px; font-size: .72rem; font-weight: 800; }
.badge.success { color: var(--success); background: var(--success-soft); }
.badge.danger { color: var(--danger); background: var(--danger-soft); }
.row-action { min-width: 54px; min-height: 32px; padding: 0 10px; border: 1px solid var(--line); border-radius: 6px; color: var(--brand); background: white; font-weight: 800; }
.row-action:hover { color: var(--brand-dark); border-color: var(--line-strong); background: var(--brand-soft); }

.table-state { display: grid; place-items: center; gap: 8px; min-height: 250px; color: var(--muted); text-align: center; }
.spinner { width: 28px; height: 28px; border: 3px solid var(--line); border-top-color: var(--brand); border-radius: 50%; animation: spin .75s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.pager { display: flex; align-items: center; justify-content: space-between; gap: 16px; min-height: 62px; padding: 12px 16px; border-top: 1px solid var(--line); color: var(--muted); font-size: .82rem; background: #f8f9fb; }
.pager-actions { display: flex; align-items: center; gap: 10px; }
.page-number { min-width: 74px; text-align: center; }
.query-mode { margin-left: 8px; padding: 3px 7px; border-radius: 7px; color: var(--muted); background: #eef2f7; font-size: .68rem; }

.modal { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; padding: 20px; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(15, 23, 42, .56); backdrop-filter: blur(5px); }
.modal-panel { position: relative; width: min(900px, 100%); max-height: calc(100vh - 40px); overflow: auto; border-radius: 8px; background: var(--surface); box-shadow: 0 30px 90px rgba(2, 6, 23, .28); }
.modal-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 20px; padding: 24px 26px 18px; border-bottom: 1px solid var(--line); }
.modal-header h2 { margin: 5px 0 0; font-size: 1.35rem; }
.icon-button { display: grid; place-items: center; width: 38px; height: 38px; padding: 0; border: 1px solid var(--line); border-radius: 7px; color: var(--muted); background: white; font-size: 1.5rem; line-height: 1; }
#recordForm { padding: 24px 26px 26px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 17px; }
.span-2 { grid-column: span 2; }
.modal-footer { display: flex; justify-content: space-between; gap: 12px; margin-top: 24px; padding-top: 20px; border-top: 1px solid var(--line); }
.modal-footer-right { display: flex; gap: 10px; margin-left: auto; }
body.modal-open { overflow: hidden; }
.onboarding-panel { width: min(620px, 100%); }
.onboarding-body { display: grid; gap: 12px; padding: 22px 26px 4px; }
.onboarding-item {
    display: grid;
    gap: 5px;
    padding: 14px 0;
    border-bottom: 1px solid var(--line);
}
.onboarding-item:last-child { border-bottom: 0; }
.onboarding-item strong { color: var(--text); font-size: .95rem; }
.onboarding-item span { color: var(--muted); font-size: .86rem; line-height: 1.55; }
.onboarding-panel .modal-footer { justify-content: flex-end; margin: 0; padding: 20px 26px 24px; }

.toast-region { position: fixed; right: 18px; bottom: 18px; z-index: 80; display: grid; gap: 10px; width: min(380px, calc(100vw - 36px)); }
.toast { padding: 13px 15px; border: 1px solid var(--line); border-radius: 7px; color: var(--text); background: white; box-shadow: var(--shadow); font-size: .85rem; animation: toast-in .2s ease both; }
.toast.success { border-color: #b2f2bb; }
.toast.error { border-color: #ffc9c9; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

@media (max-width: 1050px) {
    .toolbar { grid-template-columns: 1fr 1fr 1fr; }
    .search-box { grid-column: 1 / -1; }
    .toolbar .button { width: 100%; }
}

@media (max-width: 720px) {
    .topbar { align-items: flex-start; padding: 16px; }
    .topbar-meta .metric { display: none; }
    .workspace { padding: 16px 14px 26px; }
    .workspace-header { align-items: flex-start; flex-direction: column; }
    .toolbar { grid-template-columns: 1fr 1fr; }
    .search-box { grid-column: 1 / -1; }
    .pager { align-items: flex-start; flex-direction: column; }
    .pager-actions { width: 100%; justify-content: space-between; }
    .form-grid { grid-template-columns: 1fr; }
    .span-2 { grid-column: span 1; }
    .modal { padding: 8px; }
    .modal-panel { max-height: calc(100vh - 16px); border-radius: 8px; }
    .modal-header, #recordForm { padding-left: 18px; padding-right: 18px; }
    .modal-footer { align-items: stretch; flex-direction: column-reverse; }
    .modal-footer-right { width: 100%; margin: 0; }
    .modal-footer-right .button, .modal-footer > .button { flex: 1; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
