:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #f0eee8;
  --border: #e2dfd6;
  --text: #1d1c19;
  --muted: #6d6a62;
  --accent: #d9631e;
  --accent-ink: #ffffff;
  --good: #1f8a4c;
  --bad: #c2362b;
  --warn: #b7791f;
  --bar-rev: #2f6fb0;
  --bar-cost: #c9c4b6;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 14px rgba(0,0,0,.04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15140f;
    --surface: #1f1e19;
    --surface-2: #29271f;
    --border: #36342b;
    --text: #efece4;
    --muted: #a19d92;
    --accent: #ee7a35;
    --good: #4cc27c;
    --bad: #ef6b5e;
    --warn: #e0a84a;
    --bar-rev: #5b9ad8;
    --bar-cost: #4c493f;
    --shadow: none;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #15140f; --surface: #1f1e19; --surface-2: #29271f; --border: #36342b; --text: #efece4;
  --muted: #a19d92; --accent: #ee7a35; --good: #4cc27c; --bad: #ef6b5e; --warn: #e0a84a;
  --bar-rev: #5b9ad8; --bar-cost: #4c493f; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 Inter, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--bad); min-height: 1.2em; margin: 0; }
.num { font-family: "JetBrains Mono", ui-monospace, monospace; font-variant-numeric: tabular-nums; font-size: 13px; }
.good { color: var(--good); }
.bad { color: var(--bad); }
.warn { color: var(--warn); }
a { color: inherit; }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow);
  padding: 32px; width: min(360px, 100%); display: grid; gap: 12px;
}
.brand { font-weight: 700; font-size: 16px; white-space: nowrap; }
.brand.big { font-size: 22px; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 20px;
  padding: 10px 24px; background: var(--surface); border-bottom: 1px solid var(--border);
}
#nav { display: flex; gap: 2px; flex: 1; overflow-x: auto; scrollbar-width: none; }
#nav a {
  text-decoration: none; color: var(--muted); padding: 7px 11px; border-radius: 8px; white-space: nowrap; font-weight: 500;
}
#nav a:hover { color: var(--text); background: var(--surface-2); }
#nav a.active { color: var(--text); background: var(--surface-2); }
main { padding: 24px; max-width: 1400px; margin: 0 auto; }

/* Controls */
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
.btn {
  font: inherit; font-weight: 600; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 8px 14px; border-radius: 8px; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.05); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--bad); }
.btn.small { padding: 5px 10px; font-size: 13px; }

.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head h1 { margin: 0; font-size: 22px; letter-spacing: -0.01em; }
.page-head .sub { color: var(--muted); margin-top: 2px; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.filters select, .filters input { width: auto; min-width: 130px; }

/* Cards */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 18px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; box-shadow: var(--shadow); }
.kpi .label { color: var(--muted); font-size: 12px; font-weight: 500; }
.kpi .value { font-size: 22px; font-weight: 700; margin-top: 4px; letter-spacing: -0.01em; }
.kpi .hint { color: var(--muted); font-size: 12px; margin-top: 2px; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow); margin-bottom: 18px; overflow: hidden; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 14px 16px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.card-head h2 { margin: 0; font-size: 15px; }
.card-body { padding: 16px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
.grid-2 > .card { margin-bottom: 0; }
.stack { display: grid; gap: 18px; margin-bottom: 18px; }

/* Tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { font-size: 12px; color: var(--muted); font-weight: 600; background: var(--surface-2); position: sticky; top: 0; }
th.r, td.r { text-align: right; }
tbody tr:last-child td { border-bottom: 0; }
tr.click { cursor: pointer; }
tr.click:hover td { background: var(--surface-2); }
tfoot td { font-weight: 600; background: var(--surface-2); border-top: 1px solid var(--border); }
.tag { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 500; background: var(--surface-2); color: var(--muted); }
.tag.good { color: var(--good); }
.tag.bad { color: var(--bad); }
.util { display: inline-flex; align-items: center; gap: 8px; }
.util .track { width: 64px; height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.util .fill { height: 100%; background: var(--accent); }
.pager { display: flex; justify-content: space-between; align-items: center; padding: 10px 16px; border-top: 1px solid var(--border); color: var(--muted); }

/* Chart */
.chart { width: 100%; height: 240px; display: block; }
.chart text { fill: var(--muted); font-size: 11px; font-family: Inter, sans-serif; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 5px; vertical-align: -1px; }

/* Timeline */
.timeline { position: relative; height: 34px; background: var(--surface-2); border-radius: 6px; overflow: hidden; }
.timeline .seg { position: absolute; top: 0; bottom: 0; display: flex; align-items: center; padding: 0 6px; font-size: 11px; font-weight: 600; color: #fff; overflow: hidden; white-space: nowrap; border-right: 2px solid var(--surface); }
.timeline-axis { display: flex; justify-content: space-between; font-size: 11px; color: var(--muted); margin-top: 4px; }

/* Dialog */
dialog { border: 1px solid var(--border); border-radius: 14px; padding: 0; background: var(--surface); color: var(--text); width: min(640px, calc(100vw - 32px)); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(0,0,0,.4); }
dialog form { padding: 20px; }
dialog h3 { margin: 0 0 14px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-grid label { display: grid; gap: 4px; font-size: 12px; color: var(--muted); font-weight: 500; }
.form-grid label.wide { grid-column: 1 / -1; }
.dialog-actions { display: flex; gap: 8px; margin-top: 18px; }
.spacer { flex: 1; }
.empty { padding: 40px; text-align: center; color: var(--muted); }
.note { font-size: 12px; color: var(--muted); }

@media (max-width: 900px) {
  .grid-2 { grid-template-columns: 1fr; }
}
@media (max-width: 640px) {
  .topbar { flex-wrap: wrap; padding: 10px 16px; gap: 8px; }
  #nav { order: 3; flex-basis: 100%; }
  #logout { margin-left: auto; }
  main { padding: 16px; }
  .form-grid { grid-template-columns: 1fr; }
  .kpi .value { font-size: 19px; }
  .filters select, .filters input { min-width: 0; flex: 1 1 140px; }
}
