:root {
    --bg: #090a0c;
    --surface: #101216;
    --surface-raised: #171a20;
    --surface-hover: #1e222a;
    --border: #292e37;
    --border-strong: #3a414d;
    --text: #f4f7fb;
    --text-soft: #aeb6c2;
    --muted: #727c89;
    --blue: #1787ff;
    --blue-hover: #47a0ff;
    --green: #2bd594;
    --amber: #f4b942;
    --danger: #ff626d;
    --sidebar: 264px;
    --radius: 4px;
    --font-heading: "Chivo", sans-serif;
    --font-body: "IBM Plex Sans", sans-serif;
    --font-mono: "JetBrains Mono", monospace;
}

* { box-sizing: border-box; }
html { background: var(--bg); color-scheme: dark; }
body { margin: 0; color: var(--text); font-family: var(--font-body); font-size: 15px; }
button, input, select { font: inherit; }
button, a, select, input { -webkit-tap-highlight-color: transparent; }
button, a { transition: background-color 120ms ease, color 120ms ease, border-color 120ms ease, opacity 120ms ease, transform 120ms ease; }
button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible, summary:focus-visible {
    outline: 2px solid var(--blue);
    outline-offset: 2px;
}
::selection { background: var(--blue); color: #fff; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.eyebrow {
    margin: 0 0 8px;
    color: #7f8998;
    font: 500 11px/1.4 var(--font-mono);
    letter-spacing: 0;
}
.brand-mark {
    display: grid;
    place-items: center;
    width: 34px;
    height: 34px;
    border: 1px solid var(--blue);
    color: #fff;
    background: #071b31;
    font: 700 18px/1 var(--font-heading);
}
.icon-button {
    display: inline-grid;
    place-items: center;
    width: 38px;
    height: 38px;
    padding: 0;
    color: var(--text-soft);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    cursor: pointer;
    text-decoration: none;
}
.icon-button:hover { color: #fff; background: var(--surface-hover); border-color: var(--border-strong); }
.primary-button, .secondary-button, .ghost-button, .details-button, .pagination-button {
    min-height: 40px;
    border-radius: var(--radius);
    cursor: pointer;
    text-decoration: none;
}
.primary-button {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    padding: 0 16px;
    color: #fff;
    background: var(--blue);
    border: 1px solid var(--blue);
    font-weight: 600;
}
.primary-button:hover { background: var(--blue-hover); border-color: var(--blue-hover); transform: translateY(-1px); }
.secondary-button {
    padding: 0 18px;
    color: var(--text);
    background: var(--surface-raised);
    border: 1px solid var(--border-strong);
    font-weight: 600;
}
.secondary-button:hover { background: var(--surface-hover); border-color: var(--blue); }
.ghost-button {
    display: inline-flex;
    align-items: center;
    gap: 9px;
    padding: 0 12px;
    color: var(--text-soft);
    background: transparent;
    border: 1px solid var(--border);
}
.ghost-button:hover { color: #fff; background: var(--surface-hover); border-color: var(--border-strong); }
.alert { padding: 13px 14px; border: 1px solid; border-radius: var(--radius); line-height: 1.5; }
.alert-error { color: #ffd8db; background: #241216; border-color: #6f2930; }

/* Login */
.login-body {
    min-height: 100vh;
    background-color: var(--bg);
}
.login-shell {
    display: flex;
    align-items: center;
    justify-content: center;
    width: min(440px, calc(100% - 32px));
    min-height: 100vh;
    margin: 0 auto;
    padding: 32px 0;
}
.signal-dot { width: 8px; height: 8px; flex: none; background: var(--green); box-shadow: 0 0 0 4px rgba(43, 213, 148, .1); }
.login-panel {
    width: 100%;
    padding: 34px;
    background: var(--surface);
    border: 1px solid var(--border-strong);
    animation: reveal 300ms ease both;
}
.login-panel h2 { margin: 0; font: 600 28px/1.2 var(--font-heading); }
.login-panel .alert { margin-top: 24px; }
.login-form { display: grid; gap: 10px; margin-top: 30px; }
.login-form label, .toolbar-field label { color: var(--text-soft); font: 500 11px/1.4 var(--font-mono); text-transform: uppercase; }
.login-form input, .toolbar select, .search-control input {
    width: 100%;
    height: 46px;
    padding: 0 13px;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}
.login-form input:hover, .toolbar select:hover, .search-control:hover { border-color: var(--border-strong); }
.login-form input:focus, .toolbar select:focus, .search-control:focus-within { border-color: var(--blue); }
.password-field { position: relative; margin-bottom: 16px; }
.password-field input { padding-right: 50px; }
.password-toggle { position: absolute; top: 4px; right: 4px; width: 38px; height: 38px; border: 0; background: transparent; }

/* Dashboard */
.app-body { height: 100vh; overflow: hidden; background: var(--bg); }
.app-layout { display: grid; grid-template-columns: var(--sidebar) minmax(0, 1fr); height: 100vh; }
.sidebar {
    z-index: 30;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
    min-width: 0;
    background: #0d0f12;
    border-right: 1px solid var(--border);
}
.sidebar-summary { padding: 22px 18px; border-bottom: 1px solid var(--border); }
.sidebar-summary span, .sidebar-summary small { display: block; color: var(--muted); font: 10px/1.4 var(--font-mono); }
.sidebar-summary strong { display: block; margin: 8px 0 2px; font: 600 25px/1 var(--font-heading); }
.dataset-nav { overflow-y: auto; padding: 8px; scrollbar-width: thin; scrollbar-color: var(--border-strong) transparent; }
.dataset-link {
    display: flex;
    align-items: center;
    gap: 11px;
    min-height: 52px;
    padding: 8px 10px;
    color: var(--text-soft);
    border-left: 2px solid transparent;
    text-decoration: none;
}
.dataset-link:hover { color: #fff; background: var(--surface-raised); }
.dataset-link.is-active { color: #fff; background: #111e2d; border-left-color: var(--blue); }
.dataset-initial { display: grid; place-items: center; width: 29px; height: 29px; flex: none; color: var(--text-soft); background: var(--surface-raised); border: 1px solid var(--border); font: 600 12px/1 var(--font-heading); }
.dataset-link.is-active .dataset-initial { color: #fff; background: #0b315a; border-color: #1f67a9; }
.dataset-link-copy { min-width: 0; }
.dataset-link-copy strong, .dataset-link-copy small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dataset-link-copy strong { font-size: 13px; font-weight: 500; }
.dataset-link-copy small { margin-top: 3px; color: var(--muted); font: 10px/1.2 var(--font-mono); }
.sidebar-footer { padding: 14px; border-top: 1px solid var(--border); }
.connection-state { display: flex; align-items: center; gap: 9px; margin: 0 4px 12px; color: var(--text-soft); font: 10px/1 var(--font-mono); }
.connection-state .signal-dot { width: 6px; height: 6px; box-shadow: none; }
.sidebar-footer form, .logout-button { width: 100%; }
.sidebar-backdrop { display: none; }
.workspace { min-width: 0; height: 100vh; overflow: hidden; }
.topbar {
    display: flex;
    align-items: center;
    min-height: 78px;
    padding: 0 28px;
    background: rgba(9, 10, 12, .97);
    border-bottom: 1px solid var(--border);
}
.menu-button { display: none; margin-right: 13px; }
.title-block { min-width: 0; }
.title-block .eyebrow { margin-bottom: 3px; }
.title-block h1 { margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font: 600 24px/1.2 var(--font-heading); }
.metrics-strip { display: grid; grid-template-columns: .8fr .8fr 1.7fr .8fr; min-height: 88px; background: var(--surface); border-bottom: 1px solid var(--border); }
.metric-cell { display: flex; flex-direction: column; justify-content: center; min-width: 0; padding: 15px 24px; border-right: 1px solid var(--border); }
.metric-cell:last-child { border-right: 0; }
.metric-cell span { color: var(--muted); font: 10px/1.2 var(--font-mono); }
.metric-cell strong { display: block; margin-top: 8px; overflow: hidden; color: var(--text); font: 500 20px/1.15 var(--font-mono); text-overflow: ellipsis; white-space: nowrap; }
.metric-cell .metric-file { color: #b9dafe; font-size: 13px; }
.content-area { height: calc(100vh - 166px); overflow-y: auto; padding: 30px 28px 48px; scrollbar-width: thin; scrollbar-color: var(--border-strong) transparent; }
.content-area > * { animation: reveal 280ms ease both; }
.dataset-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 24px; margin-bottom: 20px; }
.dataset-heading h2 { margin: 0; font: 600 21px/1.3 var(--font-heading); }
.dataset-heading p { margin: 7px 0 0; color: var(--muted); font: 11px/1.4 var(--font-mono); overflow-wrap: anywhere; }
.refresh-button svg { transition: transform 180ms ease; }
.refresh-button:hover svg { transform: rotate(35deg); }
.content-area > .alert { margin-bottom: 20px; }
.toolbar { display: grid; grid-template-columns: minmax(260px, 1.35fr) 120px minmax(230px, 1fr) auto; align-items: end; gap: 10px; margin-bottom: 14px; padding: 14px; background: var(--surface); border: 1px solid var(--border); }
.toolbar-field { min-width: 0; }
.toolbar-field label { display: block; margin: 0 0 7px; }
.toolbar select { height: 42px; appearance: auto; }
.search-control { position: relative; border-radius: var(--radius); }
.search-control svg { position: absolute; top: 12px; left: 12px; width: 17px; height: 17px; color: var(--muted); }
.search-control input { height: 42px; padding-left: 40px; }
.apply-button { height: 42px; }
.file-inventory { margin-bottom: 14px; background: var(--surface); border: 1px solid var(--border); }
.file-inventory summary { display: flex; align-items: center; justify-content: space-between; gap: 20px; padding: 13px 14px; cursor: pointer; color: var(--text-soft); font-weight: 500; }
.file-inventory summary:hover { color: #fff; background: var(--surface-raised); }
.file-inventory summary span { color: var(--muted); font: 10px/1 var(--font-mono); }
.file-list { max-height: 280px; overflow: auto; border-top: 1px solid var(--border); }
.file-row { display: grid; grid-template-columns: 78px minmax(0, 1fr) 100px; gap: 12px; align-items: center; min-height: 39px; padding: 7px 14px; border-bottom: 1px solid #20242b; color: var(--text-soft); }
.file-row:last-child { border-bottom: 0; }
.file-row code { overflow: hidden; font: 11px/1.4 var(--font-mono); text-overflow: ellipsis; white-space: nowrap; }
.file-row > span:last-child { text-align: right; color: var(--muted); font: 10px/1 var(--font-mono); }
.file-kind { color: #9bcaff; font: 500 9px/1 var(--font-mono); }
.records-grid { min-width: 0; border: 1px solid var(--border); background: var(--surface); }
.records-header, .record-row { display: grid; grid-template-columns: minmax(280px, 2.2fr) minmax(140px, 1fr) minmax(150px, 1.1fr) 80px 105px; align-items: center; }
.records-header { position: sticky; top: -30px; z-index: 3; min-height: 38px; color: var(--muted); background: #0d0f12; border-bottom: 1px solid var(--border); font: 10px/1 var(--font-mono); }
.records-header > span { padding: 0 13px; }
.record-row { min-height: 76px; border-bottom: 1px solid #22262d; }
.record-row:last-child { border-bottom: 0; }
.record-row:hover { background: var(--surface-hover); }
.record-row > * { min-width: 0; padding: 10px 13px; }
.profile-cell { display: flex; align-items: center; gap: 12px; }
.avatar-frame { display: grid; place-items: center; width: 44px; height: 44px; flex: none; overflow: hidden; color: #dbe9f8; background: #17304a; border: 1px solid #2c4d6d; font: 600 14px/1 var(--font-heading); }
.avatar-frame img { width: 100%; height: 100%; object-fit: cover; }
.profile-cell > div:last-child { min-width: 0; }
.profile-cell strong, .profile-cell small { display: block; overflow: hidden; text-overflow: ellipsis; }
.profile-cell strong { color: var(--text); font-size: 13px; white-space: nowrap; }
.profile-cell small { display: -webkit-box; margin-top: 5px; color: var(--muted); font-size: 11px; line-height: 1.35; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.record-row > code { overflow: hidden; color: #9bcaff; font: 10px/1.4 var(--font-mono); text-overflow: ellipsis; white-space: nowrap; }
.record-row > span { color: var(--text-soft); font-size: 12px; line-height: 1.4; }
.signal-value { color: var(--amber) !important; font-family: var(--font-mono); }
.record-actions { display: flex; align-items: center; justify-content: flex-end; gap: 6px; }
.record-actions .icon-button { width: 32px; height: 32px; }
.details-button { min-height: 32px; padding: 0 10px; color: #cfe7ff; background: #11263c; border: 1px solid #28547e; font-size: 11px; }
.details-button:hover { color: #fff; background: #183b5e; border-color: var(--blue); }
.record-row[hidden], .gallery-item[hidden] { display: none !important; }
.cursor-pagination { display: grid; grid-template-columns: 140px minmax(0, 1fr) 140px; align-items: center; gap: 16px; margin-top: 14px; padding: 14px; background: var(--surface); border: 1px solid var(--border); }
.pagination-button { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 38px; padding: 0 13px; color: var(--text-soft); background: var(--surface-raised); border: 1px solid var(--border-strong); font-weight: 500; }
.pagination-button:last-child { justify-self: end; }
.pagination-button:hover { color: #fff; border-color: var(--blue); background: #13253a; }
.pagination-button.is-disabled { opacity: .35; cursor: not-allowed; }
.cursor-status { min-width: 0; text-align: center; }
.cursor-status strong, .cursor-status code { display: block; }
.cursor-status strong { font-size: 12px; }
.cursor-status code { margin-top: 5px; overflow: hidden; color: var(--muted); font: 9px/1.3 var(--font-mono); text-overflow: ellipsis; white-space: nowrap; }
.stream-diagnostics { margin-top: 10px; color: var(--muted); text-align: right; font: 9px/1.4 var(--font-mono); }
.empty-state { display: grid; justify-items: start; padding: 54px 28px; border: 1px solid var(--border); background: var(--surface); }
.empty-state > span { color: var(--blue); font: 400 32px/1 var(--font-mono); }
.empty-state h3 { margin: 20px 0 6px; font: 600 18px/1.3 var(--font-heading); }
.empty-state p { max-width: 560px; margin: 0; color: var(--text-soft); line-height: 1.6; }
.image-gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 1px; overflow: hidden; border: 1px solid var(--border); background: var(--border); }
.gallery-item { display: grid; gap: 10px; min-width: 0; padding: 10px; color: var(--text-soft); background: var(--surface); text-decoration: none; }
.gallery-item:hover { color: #fff; background: var(--surface-hover); }
.gallery-item img { width: 100%; aspect-ratio: 1; object-fit: contain; background: #07080a; }
.gallery-item span { overflow: hidden; font: 10px/1.3 var(--font-mono); text-overflow: ellipsis; white-space: nowrap; }

/* Details drawer */
.record-drawer {
    width: min(660px, 100%);
    max-width: none;
    height: 100vh;
    max-height: none;
    margin: 0 0 0 auto;
    padding: 0;
    color: var(--text);
    background: #0c0e11;
    border: 0;
    border-left: 1px solid var(--border-strong);
    animation: slide-in 180ms ease both;
}
.record-drawer::backdrop { background: rgba(0, 0, 0, .68); }
.drawer-header { display: flex; align-items: center; justify-content: space-between; gap: 20px; min-height: 84px; padding: 18px 22px; border-bottom: 1px solid var(--border); }
.drawer-header .eyebrow { margin-bottom: 4px; }
.drawer-header h2 { margin: 0; font: 600 22px/1.2 var(--font-heading); }
.drawer-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 14px; min-height: 58px; padding: 10px 22px; border-bottom: 1px solid var(--border); }
.drawer-toolbar > span { color: #9bcaff; font: 11px/1 var(--font-mono); }
.copy-json { min-height: 34px; font-size: 11px; }
.structured-json { height: calc(100vh - 142px); overflow: auto; padding: 20px 22px 60px; }
.json-tree { display: grid; gap: 1px; background: var(--border); border: 1px solid var(--border); }
.json-entry { display: grid; grid-template-columns: minmax(120px, .45fr) minmax(0, 1fr); gap: 16px; padding: 10px 12px; background: var(--surface); }
.json-key { color: #83bdff; font: 10px/1.5 var(--font-mono); overflow-wrap: anywhere; }
.json-value { min-width: 0; color: #d8dde6; font: 11px/1.55 var(--font-mono); overflow-wrap: anywhere; white-space: pre-wrap; }
.json-value.is-null { color: var(--muted); }
.json-value.is-boolean { color: var(--amber); }
.json-value.is-number { color: #64dfb0; }
.json-nested { display: grid; gap: 6px; }
.json-nested details { border-left: 1px solid #31577e; padding-left: 10px; }
.json-nested summary { cursor: pointer; color: #9bcaff; font: 10px/1.5 var(--font-mono); }

@keyframes reveal { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }
@keyframes slide-in { from { opacity: 0; transform: translateX(24px); } to { opacity: 1; transform: translateX(0); } }

@media (max-width: 1120px) {
    :root { --sidebar: 230px; }
    .records-header, .record-row { grid-template-columns: minmax(250px, 2fr) minmax(130px, 1fr) minmax(140px, 1fr) 70px 100px; }
    .toolbar { grid-template-columns: minmax(230px, 1fr) 110px minmax(200px, 1fr); }
    .apply-button { grid-column: 3; justify-self: end; width: 110px; }
}

@media (max-width: 860px) {
    .app-layout { display: block; }
    .sidebar { position: fixed; inset: 0 auto 0 0; width: min(310px, 88vw); transform: translateX(-102%); transition: transform 180ms ease; box-shadow: 12px 0 28px rgba(0,0,0,.35); }
    .app-body.sidebar-open .sidebar { transform: translateX(0); }
    .menu-button { display: inline-grid; }
    .sidebar-backdrop { position: fixed; z-index: 20; inset: 0; width: 100%; height: 100%; padding: 0; background: rgba(0,0,0,.65); border: 0; }
    .app-body.sidebar-open .sidebar-backdrop { display: block; }
    .topbar { padding: 0 18px; }
    .metrics-strip { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .metric-cell:nth-child(2) { border-right: 0; }
    .metric-cell:nth-child(-n+2) { border-bottom: 1px solid var(--border); }
    .content-area { height: calc(100vh - 254px); padding: 24px 18px 42px; }
    .toolbar { grid-template-columns: minmax(0, 1fr) 110px; }
    .toolbar-search { grid-column: 1 / -1; }
    .apply-button { grid-column: 1 / -1; justify-self: stretch; width: 100%; }
    .records-header { display: none; }
    .record-row { grid-template-columns: minmax(0, 1fr) auto; gap: 2px 12px; padding: 13px; }
    .record-row > * { padding: 4px 0; }
    .profile-cell { grid-column: 1 / -1; padding-bottom: 10px; }
    .record-row > code { grid-column: 1; }
    .record-row > span[role="cell"] { grid-column: 1; }
    .record-row .signal-value { grid-column: 1; }
    .record-actions { grid-column: 2; grid-row: 2 / span 3; align-self: center; flex-direction: column; }
}

@media (max-width: 720px) {
    .login-shell { width: min(100% - 30px, 440px); padding: 30px 0; }
    .login-panel { padding: 26px 22px; }
    .metric-cell { padding: 13px 16px; }
    .metric-cell strong { font-size: 17px; }
    .metric-cell .metric-file { font-size: 11px; }
    .file-row { grid-template-columns: 65px minmax(0, 1fr); }
    .file-row > span:last-child { grid-column: 2; text-align: left; }
    .cursor-pagination { grid-template-columns: 1fr 1fr; }
    .cursor-status { grid-column: 1 / -1; grid-row: 1; }
    .pagination-button { grid-row: 2; }
    .record-drawer { width: 100%; }
    .json-entry { grid-template-columns: 1fr; gap: 5px; }
}

@media (max-width: 480px) {
    .topbar { min-height: 70px; padding: 0 12px; }
    .title-block h1 { font-size: 19px; }
    .metrics-strip { min-height: 152px; }
    .metric-cell { padding: 10px 12px; }
    .content-area { height: calc(100vh - 222px); padding: 20px 12px 36px; }
    .dataset-heading { align-items: center; }
    .toolbar { grid-template-columns: 1fr; padding: 12px; }
    .toolbar-search, .apply-button { grid-column: 1; }
    .record-row { grid-template-columns: minmax(0, 1fr); }
    .record-actions { grid-column: 1; grid-row: auto; align-items: stretch; flex-direction: row; justify-content: flex-start; padding-top: 10px; }
    .details-button { flex: 1; }
    .record-row > code, .record-row > span[role="cell"] { white-space: normal; overflow-wrap: anywhere; }
    .image-gallery { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

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