:root {
    color-scheme: light;
    --bg: #f5f7fb;
    --panel: #ffffff;
    --panel-soft: #eef2f7;
    --text: #111827;
    --muted: #667085;
    --line: #d9e0ea;
    --accent: #2563eb;
    --accent-strong: #1d4ed8;
    --success: #168a5b;
    --warning: #d97706;
    --danger: #dc2626;
    --shadow: 0 10px 32px rgba(15, 23, 42, 0.08);
    --radius: 8px;
}

[data-theme="dark"] {
    color-scheme: dark;
    --bg: #0b0f17;
    --panel: #141a24;
    --panel-soft: #1d2532;
    --text: #f3f6fb;
    --muted: #a6b0c0;
    --line: #30394a;
    --accent: #60a5fa;
    --accent-strong: #93c5fd;
    --success: #34d399;
    --warning: #fbbf24;
    --danger: #f87171;
    --shadow: 0 14px 36px rgba(0, 0, 0, 0.32);
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    min-height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    font-size: 16px;
}

body { padding-bottom: env(safe-area-inset-bottom); }

a { color: inherit; text-decoration: none; }

button, input, select {
    font: inherit;
}

input, select {
    width: 100%;
    min-height: 44px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--panel);
    color: var(--text);
    padding: 0 0.75rem;
}

label span {
    display: block;
    color: var(--muted);
    font-size: 0.78rem;
    margin-bottom: 0.3rem;
}

.app-shell {
    min-height: 100vh;
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    min-height: 64px;
    padding: 0.6rem clamp(1rem, 4vw, 2rem);
    border-bottom: 1px solid var(--line);
    background: color-mix(in srgb, var(--panel) 92%, transparent);
    backdrop-filter: blur(14px);
}

.brand, .login-brand {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    font-weight: 800;
}

.brand-mark {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    border-radius: 8px;
    background: var(--accent);
    color: #fff;
    font-weight: 900;
}

.desktop-nav {
    display: none;
    align-items: center;
    gap: 0.25rem;
    padding: 0.25rem;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--panel-soft);
}

.desktop-nav a {
    min-height: 36px;
    padding: 0.45rem 0.8rem;
    border-radius: 6px;
    color: var(--muted);
}

.desktop-nav a.active {
    background: var(--panel);
    color: var(--text);
    box-shadow: var(--shadow);
}

.topbar-actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.logout-link {
    color: var(--muted);
    font-size: 0.92rem;
}

.icon-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    height: 44px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--panel);
    color: var(--text);
    cursor: pointer;
}

.content {
    width: min(1280px, 100%);
    margin: 0 auto;
    padding: 1rem 1rem 5.6rem;
}

.page-head {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    margin-bottom: 1rem;
}

.page-head h1 {
    margin: 0 0 0.25rem;
    font-size: 1.6rem;
    line-height: 1.15;
}

.page-head p {
    margin: 0;
    color: var(--muted);
}

.toolbar {
    display: flex;
    gap: 0.6rem;
    align-items: end;
    flex-wrap: wrap;
}

.compact-toolbar > label {
    flex: 1 1 140px;
}

.primary-button, .secondary-button {
    min-height: 44px;
    border-radius: var(--radius);
    padding: 0 0.9rem;
    border: 1px solid transparent;
    cursor: pointer;
    font-weight: 700;
}

.primary-button {
    background: var(--accent);
    color: #fff;
}

.primary-button:hover { background: var(--accent-strong); }

.secondary-button {
    background: var(--panel);
    color: var(--text);
    border-color: var(--line);
}

.full-width { width: 100%; }

.alert {
    border: 1px solid color-mix(in srgb, var(--danger) 45%, var(--line));
    background: color-mix(in srgb, var(--danger) 10%, var(--panel));
    color: var(--text);
    padding: 0.8rem;
    border-radius: var(--radius);
    margin-bottom: 1rem;
}

.hidden { display: none !important; }

.kpi-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.7rem;
}

.kpi-card, .panel, .sample-card, .detail-panel, .sample-list-wrap {
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--panel);
    box-shadow: var(--shadow);
}

.kpi-card {
    min-height: 96px;
    padding: 0.85rem;
}

.kpi-card.primary {
    background: color-mix(in srgb, var(--accent) 9%, var(--panel));
}

.kpi-card strong {
    display: block;
    font-size: clamp(1.45rem, 8vw, 2rem);
    line-height: 1.1;
}

.kpi-card span {
    display: block;
    margin-top: 0.35rem;
    color: var(--muted);
    font-size: 0.84rem;
}

.threshold-strip {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.55rem;
    margin: 0.8rem 0 1rem;
}

.threshold-strip div {
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-height: 48px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 0 0.7rem;
    background: var(--panel);
}

.threshold-strip span { color: var(--muted); }

.chart-grid, .breakdown-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1rem;
}

.panel {
    min-width: 0;
    padding: 1rem;
    margin-bottom: 1rem;
}

.panel h2 {
    margin: 0 0 0.8rem;
    font-size: 1rem;
}

.chart-canvas {
    width: 100%;
    height: 260px;
    display: block;
}

.legend {
    display: flex;
    gap: 0.55rem;
    flex-wrap: wrap;
    margin-top: 0.7rem;
    color: var(--muted);
    font-size: 0.78rem;
}

.legend-item {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
}

.legend-swatch {
    width: 10px;
    height: 10px;
    border-radius: 2px;
}

.breakdown-row {
    display: flex;
    justify-content: space-between;
    gap: 1rem;
    padding: 0.55rem 0;
    border-bottom: 1px solid var(--line);
}

.breakdown-row:last-child { border-bottom: 0; }

.breakdown-row span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: var(--muted);
}

.dataset-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    width: 100%;
}

.dataset-actions select {
    grid-column: span 2;
}

.dataset-layout {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1rem;
}

.sample-list-wrap {
    min-width: 0;
    padding: 0.75rem;
}

.list-summary {
    display: flex;
    gap: 0.35rem;
    align-items: baseline;
    margin-bottom: 0.65rem;
    color: var(--muted);
}

.list-summary strong {
    color: var(--text);
    font-size: 1.35rem;
}

.sample-list {
    display: grid;
    gap: 0.65rem;
}

.sample-card {
    display: block;
    width: 100%;
    text-align: left;
    color: var(--text);
    padding: 0.8rem;
    cursor: pointer;
}

.sample-card.active {
    border-color: var(--accent);
}

.sample-top {
    display: flex;
    justify-content: space-between;
    gap: 0.75rem;
}

.sample-title {
    font-weight: 800;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sample-date {
    color: var(--muted);
    font-size: 0.78rem;
    white-space: nowrap;
}

.sample-meta {
    display: flex;
    gap: 0.4rem;
    flex-wrap: wrap;
    color: var(--muted);
    font-size: 0.78rem;
    margin-top: 0.45rem;
}

.badges {
    display: flex;
    gap: 0.25rem;
    flex-wrap: wrap;
    margin-top: 0.55rem;
}

.badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 2rem;
    height: 1.25rem;
    padding: 0 0.35rem;
    border-radius: 4px;
    color: #fff;
    font-size: 0.68rem;
    font-weight: 800;
}

.threshold-250 { background: #168a5b; }
.threshold-500 { background: #d97706; }
.threshold-750 { background: #dc2626; }
.threshold-1000 { background: #0891b2; }
.threshold-1250 { background: #7c3aed; }
.threshold-1500 { background: #64748b; }

.detail-panel {
    min-height: 420px;
    overflow: hidden;
}

.empty-state {
    min-height: 320px;
    display: grid;
    place-items: center;
    color: var(--muted);
    padding: 1rem;
}

.detail-head {
    padding: 0.9rem;
    border-bottom: 1px solid var(--line);
}

.detail-head h2 {
    margin: 0 0 0.5rem;
    font-size: 1.1rem;
}

.detail-meta {
    display: flex;
    gap: 0.4rem;
    flex-wrap: wrap;
    color: var(--muted);
    font-size: 0.78rem;
}

.mobile-tabs {
    display: grid;
    grid-template-columns: 1fr 1fr;
    padding: 0.7rem;
    gap: 0.5rem;
}

.tab-button {
    min-height: 40px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--panel-soft);
    color: var(--muted);
    font-weight: 700;
}

.tab-button.active {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
}

.text-compare {
    display: block;
    padding: 0 0.9rem 0.9rem;
}

.text-column {
    display: none;
}

.text-column.active {
    display: block;
}

.text-column h3 {
    margin: 0.5rem 0;
    font-size: 0.9rem;
    color: var(--muted);
}

.text-box {
    max-height: 58vh;
    overflow: auto;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 0.8rem;
    background: var(--panel-soft);
    white-space: pre-wrap;
    line-height: 1.5;
}

.prompt-details {
    margin: 0 0.9rem 0.9rem;
    border-top: 1px solid var(--line);
    padding-top: 0.8rem;
}

.prompt-details summary {
    min-height: 44px;
    cursor: pointer;
    color: var(--muted);
}

.prompt-box {
    max-height: 220px;
    overflow: auto;
    white-space: pre-wrap;
    padding: 0.7rem;
    background: var(--panel-soft);
    border-radius: var(--radius);
}

.drawer-backdrop {
    position: fixed;
    inset: 0;
    z-index: 35;
    background: rgba(0, 0, 0, 0.38);
    display: none;
}

.drawer-backdrop.open { display: block; }

.filter-drawer {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 40;
    max-height: 88vh;
    overflow: auto;
    transform: translateY(105%);
    transition: transform 180ms ease;
    background: var(--panel);
    border-top: 1px solid var(--line);
    border-radius: 12px 12px 0 0;
    padding: 1rem 1rem calc(1rem + env(safe-area-inset-bottom));
}

.filter-drawer.open { transform: translateY(0); }

.drawer-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.8rem;
}

.drawer-head h2 {
    margin: 0;
    font-size: 1.2rem;
}

.filter-form {
    display: grid;
    gap: 0.8rem;
}

.field-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.7rem;
}

.threshold-filter-group {
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 0.7rem;
    margin: 0;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.5rem;
}

.threshold-filter-group legend {
    color: var(--muted);
    padding: 0 0.3rem;
}

.threshold-filter-group label {
    min-height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.35rem;
    border: 1px solid var(--line);
    border-radius: var(--radius);
}

.drawer-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.7rem;
    position: sticky;
    bottom: 0;
    background: var(--panel);
    padding-top: 0.5rem;
}

.bottom-nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 30;
    display: grid;
    grid-template-columns: 1fr 1fr;
    min-height: calc(64px + env(safe-area-inset-bottom));
    padding-bottom: env(safe-area-inset-bottom);
    border-top: 1px solid var(--line);
    background: var(--panel);
}

.bottom-nav a {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.15rem;
    color: var(--muted);
    font-size: 0.78rem;
}

.bottom-nav a.active {
    color: var(--accent);
}

.nav-icon {
    font-size: 1.35rem;
    line-height: 1;
}

.login-body {
    display: grid;
    place-items: center;
    min-height: 100vh;
    padding: 1rem;
}

.login-panel {
    width: min(420px, 100%);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--panel);
    box-shadow: var(--shadow);
    padding: 1.25rem;
}

.login-brand {
    margin-bottom: 1.25rem;
}

.login-brand h1 {
    margin: 0;
    font-size: 1.35rem;
}

.login-brand p {
    margin: 0.15rem 0 0;
    color: var(--muted);
}

.login-form {
    display: grid;
    gap: 0.9rem;
}

@media (min-width: 720px) {
    .desktop-nav { display: flex; }
    .bottom-nav { display: none; }
    .content { padding-bottom: 2rem; }
    .page-head {
        flex-direction: row;
        align-items: end;
        justify-content: space-between;
    }
    .kpi-grid {
        grid-template-columns: repeat(4, minmax(0, 1fr));
    }
    .threshold-strip {
        grid-template-columns: repeat(6, minmax(0, 1fr));
    }
    .chart-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
    .breakdown-grid {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }
    .dataset-actions {
        display: flex;
        width: auto;
    }
    .dataset-actions select {
        grid-column: auto;
        width: 160px;
    }
    .dataset-layout {
        grid-template-columns: minmax(340px, 420px) minmax(0, 1fr);
        align-items: start;
    }
    .sample-list-wrap {
        position: sticky;
        top: 82px;
        max-height: calc(100vh - 106px);
        overflow: auto;
    }
    .detail-panel {
        min-height: calc(100vh - 106px);
    }
    .mobile-tabs { display: none; }
    .text-compare {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 0.9rem;
        padding: 0.9rem;
    }
    .text-column,
    .text-column.active {
        display: block;
    }
    .text-box {
        max-height: calc(100vh - 285px);
    }
    .filter-drawer {
        left: auto;
        right: 1rem;
        bottom: 1rem;
        width: min(460px, calc(100vw - 2rem));
        border: 1px solid var(--line);
        border-radius: var(--radius);
        box-shadow: var(--shadow);
    }
}

@media (min-width: 1100px) {
    .kpi-grid {
        grid-template-columns: repeat(8, minmax(0, 1fr));
    }
}

