:root {
    color-scheme: light;
    --bg: #f5f6f8;
    --surface: #ffffff;
    --border: #e2e5ea;
    --text: #1c2230;
    --muted: #6b7280;
    --accent: #2f6bff;
    --accent-ink: #ffffff;
    --th-bg: #fafbfc;
    --secondary-bg: #eef1f6;
    --ok-bg: #e7f6ec; --ok-fg: #1b7a3d;
    --error-bg: #fdecec; --error-fg: #c0392b;
    --radius: 10px;
    --shadow: 0 1px 3px rgba(20, 30, 55, .08), 0 1px 2px rgba(20, 30, 55, .04);
}

/* Dark theme: explicit choice wins; otherwise follow the OS preference. */
[data-theme="dark"] {
    color-scheme: dark;
    --bg: #0f1420;
    --surface: #171d2b;
    --border: #2a3346;
    --text: #e6e9ef;
    --muted: #9aa4b2;
    --accent: #4b83ff;
    --accent-ink: #ffffff;
    --th-bg: #1e2636;
    --secondary-bg: #253048;
    --ok-bg: #12301f; --ok-fg: #4ade80;
    --error-bg: #3a1d1d; --error-fg: #f87171;
    --shadow: 0 1px 3px rgba(0, 0, 0, .4), 0 1px 2px rgba(0, 0, 0, .3);
}
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        color-scheme: dark;
        --bg: #0f1420;
        --surface: #171d2b;
        --border: #2a3346;
        --text: #e6e9ef;
        --muted: #9aa4b2;
        --accent: #4b83ff;
        --accent-ink: #ffffff;
        --th-bg: #1e2636;
        --secondary-bg: #253048;
        --ok-bg: #12301f; --ok-fg: #4ade80;
        --error-bg: #3a1d1d; --error-fg: #f87171;
        --shadow: 0 1px 3px rgba(0, 0, 0, .4), 0 1px 2px rgba(0, 0, 0, .3);
    }
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    color: var(--text);
    background: var(--bg);
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Top bar / nav */
.topbar {
    display: flex; align-items: center; gap: 24px;
    padding: 0 24px; height: 56px;
    background: var(--surface); border-bottom: 1px solid var(--border);
}
.topbar .brand { font-weight: 700; }
.topbar nav { display: flex; gap: 4px; flex: 1; }
.topbar nav a {
    padding: 8px 12px; border-radius: 8px; color: var(--muted); font-weight: 500;
}
.topbar nav a:hover { background: var(--bg); text-decoration: none; }
.topbar nav a.active { color: var(--text); background: var(--bg); }
.topbar .session { display: flex; align-items: center; gap: 12px; color: var(--muted); }
.theme-toggle { background: transparent; border: 1px solid var(--border); color: var(--text);
                padding: 5px 9px; border-radius: 8px; cursor: pointer; font-size: 15px; line-height: 1; }
.theme-toggle:hover { background: var(--bg); }

.container { max-width: 1080px; margin: 0 auto; padding: 28px 24px 64px; }

h1 { font-size: 22px; margin: 0 0 20px; }
h2 { font-size: 17px; margin: 28px 0 12px; }
.muted { color: var(--muted); }

/* Cards & KPI tiles */
.card {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px;
}
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 16px; margin-bottom: 24px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
       box-shadow: var(--shadow); padding: 16px 18px; }
.kpi .label { color: var(--muted); font-size: 13px; }
.kpi .value { font-size: 26px; font-weight: 700; margin-top: 4px; }

/* Charts */
.charts { display: grid; grid-template-columns: 1fr; gap: 20px; }
.chart-card { background: var(--surface); border: 1px solid var(--border);
              border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.chart-card canvas { width: 100% !important; height: 260px !important; }

/* Period toggle */
.toggle { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.toggle button { border: 0; background: var(--surface); padding: 8px 16px; cursor: pointer; font: inherit; color: var(--muted); }
.toggle button.active { background: var(--accent); color: var(--accent-ink); }

/* Tables */
table { width: 100%; border-collapse: collapse; background: var(--surface);
        border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); }
th { background: var(--th-bg); font-size: 13px; color: var(--muted); font-weight: 600; }
tbody tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

/* Forms */
form label { display: block; margin-bottom: 14px; font-weight: 500; }
form input, form select, form textarea {
    display: block; width: 100%; margin-top: 6px; padding: 9px 11px;
    border: 1px solid var(--border); border-radius: 8px; font: inherit;
    background: var(--surface); color: var(--text);
}
form input::placeholder, form textarea::placeholder { color: var(--muted); }
form select option { background: var(--surface); color: var(--text); }
form input:disabled, form select:disabled { color: var(--muted); opacity: 1; }
button, .btn {
    display: inline-block; border: 0; background: var(--accent); color: var(--accent-ink);
    padding: 10px 18px; border-radius: 8px; font: inherit; font-weight: 600; cursor: pointer;
}
button.secondary, .btn.secondary { background: var(--secondary-bg); color: var(--text); }
button.danger, .btn.danger { background: var(--error-fg); }
.btn:hover { text-decoration: none; opacity: .95; }

.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

/* Auth */
.auth-card { max-width: 380px; margin: 8vh auto 0; }
.auth-card { background: var(--surface); border: 1px solid var(--border);
             border-radius: var(--radius); box-shadow: var(--shadow); padding: 28px; }

/* Flash */
.flash { padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; font-weight: 500; }
.flash.ok { background: var(--ok-bg); color: var(--ok-fg); }
.flash.error { background: var(--error-bg); color: var(--error-fg); }

.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 16px; }
.inline { display: inline; }
