:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eceef2;
  --border: #d8dce3;
  --text: #15171c;
  --muted: #5c6470;
  --accent: #2563eb;
  --accent-dim: #dbeafe;
  --danger: #dc2626;
  --mono: "IBM Plex Mono", monospace;
  --sans: "IBM Plex Sans", system-ui, sans-serif;
  --radius: 2px;
  --shadow: 0 1px 2px rgba(0,0,0,.06);
}

[data-theme="dark"] {
  --bg: #0d0f12;
  --surface: #14171c;
  --surface-2: #1b1f26;
  --border: #2a3038;
  --text: #e8eaed;
  --muted: #8b939e;
  --accent: #3b82f6;
  --accent-dim: #1e293b;
  --shadow: 0 1px 2px rgba(0,0,0,.35);
}

*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: var(--sans);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.ref-link {
  color: var(--accent);
  text-decoration: none;
  word-break: break-all;
}
.ref-link:hover { text-decoration: underline; }
code, .mono { font-family: var(--mono); font-size: .9em; }
.muted { color: var(--muted); font-size: .9rem; }
.hidden { display: none !important; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .75rem;
  padding: .45rem 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar-left, .topbar-right {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: .4rem;
}
.topbar-right { justify-content: flex-end; }
.brand { font-weight: 600; color: var(--text); text-decoration: none; font-size: .95rem; }
.sep { color: var(--muted); }
.crumb { color: var(--muted); font-size: .9rem; }

.topbar .btn-compact,
.topbar .btn-secondary.btn-compact,
.topbar .btn-ghost.btn-compact {
  padding: .25rem .5rem;
  font-size: .78rem;
  line-height: 1.3;
}
.topbar .btn-icon.btn-compact {
  padding: .22rem .42rem;
  font-size: .85rem;
  line-height: 1.2;
}
.topbar .select-compact,
.topbar .input-compact {
  padding: .25rem .45rem;
  font-size: .78rem;
}
.topbar .custom-range { gap: .35rem; }
.topbar .range-sep { color: var(--muted); font-size: .78rem; }

.main { max-width: 1280px; margin: 0 auto; padding: 1.25rem; }

.page-heading {
  margin: 0 0 1rem;
  font-size: 1.35rem;
  font-weight: 600;
}
.panel-head {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 1.25rem;
}
.panel-head-compact { margin-bottom: 1rem; }
.panel-head h1 { margin: 0; font-size: 1.5rem; font-weight: 600; }
.controls { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
.custom-range { display: flex; gap: .5rem; align-items: center; }

.select, .input {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: .45rem .65rem;
  border-radius: var(--radius);
  font: inherit;
}
.input.search { min-width: 220px; }
.select:focus, .input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.btn-primary, .btn-secondary, .btn-ghost, .btn-icon {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font: inherit;
  cursor: pointer;
  padding: .45rem .85rem;
  background: var(--surface);
  color: var(--text);
}
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.05); }
.btn-secondary:hover, .btn-ghost:hover, .btn-icon:hover { background: var(--surface-2); }
.btn-icon { padding: .35rem .55rem; }

.metric-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: .75rem;
  margin-bottom: 1.25rem;
}
.metric-grid-compact {
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: .5rem;
  margin-bottom: 1rem;
}
#totals.metric-grid-compact {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}
.metric, .metric-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .85rem 1rem;
  box-shadow: var(--shadow);
}
.metric-grid-compact .metric,
.metric-grid-compact .metric-card {
  padding: .5rem .7rem;
}
.metric-label { display: block; color: var(--muted); font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; }
.metric-grid-compact .metric-label,
.metric-grid-compact .metric-card .metric-label {
  font-size: .68rem;
  letter-spacing: .03em;
  line-height: 1.3;
}
.metric-value { display: block; font-size: 1.6rem; font-weight: 600; margin-top: .15rem; }
.metric-grid-compact .metric-value,
.metric-grid-compact .metric-card .metric-value {
  font-size: 1.2rem;
  margin-top: .1rem;
}
.metric-delta { color: var(--muted); font-size: .8rem; }
.metric-grid-compact .metric-delta,
.metric-grid-compact .metric-card .metric-delta {
  font-size: .72rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 1rem;
}
.card-head { padding: .85rem 1rem; border-bottom: 1px solid var(--border); }
.card-head h2, .card-head h3 { margin: 0; font-size: 1rem; font-weight: 600; }

.split { display: grid; grid-template-columns: 1fr 320px; gap: 1rem; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.mini-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; padding: 1rem; }
.stack { display: flex; flex-direction: column; gap: .75rem; padding: 1rem; }
.field { display: flex; flex-direction: column; gap: .3rem; font-size: .9rem; }
.field span { color: var(--muted); }

.table-wrap { overflow: auto; }
.table { width: 100%; border-collapse: collapse; font-size: .88rem; }
.table th, .table td { padding: .55rem .75rem; border-bottom: 1px solid var(--border); text-align: left; }
.table th { color: var(--muted); font-weight: 500; font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; background: var(--surface-2); }
.table th[data-sort] {
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}
.table th[data-sort]:hover { color: var(--text); }
.table th[data-sort]::after {
  content: "↕";
  display: inline-block;
  margin-left: .3em;
  font-size: .7em;
  opacity: .35;
}
.table th.sort-asc::after { content: "▲"; opacity: .85; }
.table th.sort-desc::after { content: "▼"; opacity: .85; }
.table.compact th, .table.compact td { padding: .4rem .6rem; }
.table tr:hover td { background: var(--surface-2); }
.empty { text-align: center; color: var(--muted); padding: 2rem !important; }
.truncate { max-width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.tabs { display: flex; gap: 0; border-bottom: 1px solid var(--border); margin-bottom: 1rem; flex-wrap: wrap; }
.tab {
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--muted);
  cursor: pointer;
  font: inherit;
  padding: .6rem 1rem;
  margin-bottom: -1px;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 500; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

.pager { padding: .75rem 1rem; border-top: 1px solid var(--border); display: flex; flex-wrap: wrap; gap: .75rem; justify-content: space-between; align-items: center; font-size: .85rem; color: var(--muted); }
.pag-btns { display: flex; gap: .25rem; }
.pag-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: .25rem .5rem;
  border-radius: var(--radius);
  cursor: pointer;
  font: inherit;
  min-width: 2rem;
}
.pag-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.pag-btn:disabled { opacity: .4; cursor: default; }

.chart-wrap { padding: 1rem; }
.chart-wrap-interactive { position: relative; }
.chart-wrap-interactive canvas { cursor: crosshair; }
.chart-tooltip {
  position: absolute;
  pointer-events: none;
  background: var(--surface);
  border: 1px solid var(--border);
  padding: .35rem .55rem;
  font-size: .78rem;
  line-height: 1.35;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  white-space: nowrap;
  transform: translate(-50%, calc(-100% - 8px));
  z-index: 2;
}
.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; justify-content: space-between; padding: .35rem 0; border-bottom: 1px solid var(--border); font-size: .88rem; }

.page-login .main { max-width: none; padding: 0; margin: 0; min-height: 100vh; }

.auth-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}
.auth-card {
  width: min(360px, 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) * 2);
  padding: 2.5rem 2rem;
  box-shadow: var(--shadow);
  gap: 1.25rem;
}
.auth-card h1 {
  margin: 0;
  font-size: 1.85rem;
  font-weight: 600;
  letter-spacing: -.02em;
}
.auth-card .field {
  width: 100%;
  text-align: left;
}
.auth-card .btn-primary {
  width: 100%;
  margin-top: .25rem;
  padding: .7rem 1.25rem;
  font-size: .95rem;
  font-weight: 500;
  letter-spacing: .02em;
  border: none;
  border-radius: calc(var(--radius) * 2);
  background: linear-gradient(180deg, color-mix(in srgb, var(--accent) 92%, #fff) 0%, var(--accent) 100%);
  box-shadow: 0 1px 2px rgba(0,0,0,.12), 0 4px 14px color-mix(in srgb, var(--accent) 35%, transparent);
  transition: transform .15s ease, box-shadow .15s ease, filter .15s ease;
}
.auth-card .btn-primary:hover {
  filter: brightness(1.06);
  transform: translateY(-1px);
  box-shadow: 0 2px 4px rgba(0,0,0,.14), 0 6px 20px color-mix(in srgb, var(--accent) 40%, transparent);
}
.auth-card .btn-primary:active {
  transform: translateY(0);
  filter: brightness(.98);
}
.auth-error { color: var(--danger); margin: 0; font-size: .9rem; width: 100%; }

.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.5);
  display: none; align-items: center; justify-content: center; padding: 1rem; z-index: 50;
}
.modal.open { display: flex; }
.modal-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  width: min(640px, 100%);
  max-height: 90vh;
  overflow: auto;
}
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: .75rem; }
.code-block {
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 1rem;
  overflow: auto;
  border-radius: var(--radius);
  font-family: var(--mono);
  font-size: .82rem;
  white-space: pre-wrap;
  word-break: break-all;
}

.cell-updated {
  animation: entry-flash 2s ease-out;
}
.metric.cell-updated,
.metric-card.cell-updated {
  border-radius: var(--radius);
}
.table th.cell-updated,
.table td.cell-updated {
  border-radius: 0;
}
@keyframes entry-flash {
  0% {
    background-color: color-mix(in srgb, var(--accent) 36%, var(--surface));
  }
  35% {
    background-color: color-mix(in srgb, var(--accent) 18%, var(--surface));
  }
  100% {
    background-color: transparent;
  }
}
.pill {
  display: inline-flex;
  align-items: center;
  padding: .12rem .45rem;
  border-radius: 999px;
  font-size: .72rem;
  font-weight: 600;
  letter-spacing: .02em;
  line-height: 1.4;
  vertical-align: middle;
}
.pill-curl {
  color: #0f5132;
  background: color-mix(in srgb, #20c997 22%, var(--surface));
  border: 1px solid color-mix(in srgb, #20c997 45%, var(--border));
}
[data-theme="dark"] .pill-curl {
  color: #9ef0cb;
  background: color-mix(in srgb, #20c997 18%, var(--surface-2));
}

@media (max-width: 900px) {
  .split, .grid-2, .mini-grid { grid-template-columns: 1fr; }
  .metric-grid-compact { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}