:root {
  --bg: #f6f7f9; --panel: #fff; --text: #1a1d23; --muted: #6b7280; --border: #e5e7eb;
  --accent: #2563eb; --accent-soft: #eff4ff; --good: #059669; --bad: #dc2626; --warn: #d97706;
}
/* Theme: follows the system unless the user picks one (html[data-theme], set in index.html / app.js) */
:root { color-scheme: light; }
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f1115; --panel: #171a21; --text: #e6e8eb; --muted: #9aa1ad; --border: #2a2f3a;
  --accent: #6b9bff; --accent-soft: #1c2640; --good: #34d399; --bad: #f87171; --warn: #fbbf24;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0f1115; --panel: #171a21; --text: #e6e8eb; --muted: #9aa1ad; --border: #2a2f3a;
    --accent: #6b9bff; --accent-soft: #1c2640; --good: #34d399; --bad: #f87171; --warn: #fbbf24;
  }
}
* { box-sizing: border-box; }
body { margin: 0; font-family: Inter, system-ui, sans-serif; background: var(--bg); color: var(--text); font-size: 14px; }
.hidden { display: none !important; }
.muted { color: var(--muted); } .small { font-size: 12px; }
.good { color: var(--good); } .bad { color: var(--bad); } .warn { color: var(--warn); }
h1 { font-size: 22px; margin: 0 0 4px; } h2 { font-size: 18px; margin: 0 0 16px; } h3 { font-size: 14px; margin: 0 0 12px; color: var(--muted); font-weight: 600; }

.card { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 18px; }
.btn { border: 1px solid var(--border); background: var(--panel); color: var(--text); padding: 8px 14px; border-radius: 8px; cursor: pointer; font: inherit; font-weight: 500; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--bad); border-color: var(--bad); background: transparent; }
.btn.small { padding: 4px 10px; font-size: 12px; }
input, select, textarea { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; width: 100%; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); font-weight: 500; }
label.check { flex-direction: row; align-items: center; gap: 8px; }
label.check input { width: auto; }
.msg { min-height: 1em; font-size: 13px; margin: 8px 0 0; }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 12px; }

/* Layout */
.app { display: flex; min-height: 100vh; }
.sidebar { width: 220px; background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; padding: 18px 12px; position: sticky; top: 0; height: 100vh; }
.brand { font-weight: 700; font-size: 18px; padding: 0 10px 18px; }
nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
nav a { padding: 9px 10px; border-radius: 8px; cursor: pointer; color: var(--muted); font-weight: 500; display: flex; align-items: center; gap: 10px; -webkit-tap-highlight-color: transparent; user-select: none; }
nav svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
nav .short { display: none; }
nav a:hover { color: var(--text); }
nav a.active { background: var(--accent-soft); color: var(--accent); }
.sidebar-foot { display: flex; flex-direction: column; gap: 8px; padding: 0 10px; word-break: break-all; }
.theme-toggle { display: inline-flex; align-items: center; gap: 6px; align-self: flex-start; }
.theme-toggle svg { width: 14px; height: 14px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
main { flex: 1; padding: 24px; min-width: 0; }
.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head h1 { margin: 0; }
.spacer { flex: 1; }

.grid { display: grid; gap: 16px; }
.kpis { grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); margin-bottom: 16px; }
.kpi .label { color: var(--muted); font-size: 12px; font-weight: 500; }
.kpi .value { font-size: 24px; font-weight: 700; margin-top: 6px; font-variant-numeric: tabular-nums; }
.kpi .sub { font-size: 12px; margin-top: 4px; }
.two { grid-template-columns: 2fr 1fr; margin-bottom: 16px; }
.chart-box { position: relative; height: 260px; }

.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.filters > * { width: auto; min-width: 140px; }
.filters input[type=search] { min-width: 220px; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { font-size: 12px; color: var(--muted); font-weight: 600; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.wrap { white-space: normal; max-width: 320px; }
tbody tr { cursor: pointer; } tbody tr:hover { background: var(--accent-soft); }
.pill { display: inline-block; padding: 2px 8px; border-radius: 99px; font-size: 11px; background: var(--bg); border: 1px solid var(--border); color: var(--muted); }
.pill.good { color: var(--good); border-color: currentColor; } .pill.bad { color: var(--bad); border-color: currentColor; } .pill.warn { color: var(--warn); border-color: currentColor; }
.pager { display: flex; align-items: center; gap: 8px; margin-top: 12px; justify-content: flex-end; }
.list-row { display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--border); gap: 8px; }
.list-row:last-child { border-bottom: 0; }
.bar { height: 6px; background: var(--bg); border-radius: 99px; overflow: hidden; margin-top: 6px; }
.bar > div { height: 100%; background: var(--accent); }
.bar > div.over { background: var(--bad); }
.empty { padding: 30px; text-align: center; color: var(--muted); }

dialog { border: 1px solid var(--border); border-radius: 14px; padding: 22px; background: var(--panel); color: var(--text); width: min(640px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(0,0,0,.45); }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-grid .full { grid-column: 1 / -1; }
.modal-actions { display: flex; gap: 8px; margin-top: 18px; }

.toast { position: fixed; bottom: 20px; right: 20px; background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; font-weight: 500; z-index: 10; }

/* Phone / home-screen app layout */
@media (max-width: 800px) {
  :root { --nav-h: 58px; }
  body { -webkit-text-size-adjust: 100%; overscroll-behavior-y: none; }
  .login { padding: calc(16px + env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom)); }
  .app { flex-direction: column; }
  /* Sidebar becomes a slim top bar (brand + sign out) */
  .sidebar { width: 100%; height: auto; position: sticky; top: 0; z-index: 5; flex-direction: row; align-items: center; justify-content: space-between;
    padding: calc(10px + env(safe-area-inset-top)) 16px 10px; border-right: 0; border-bottom: 1px solid var(--border); }
  .brand { padding: 0; font-size: 17px; }
  .sidebar-foot { flex-direction: row; align-items: center; gap: 10px; padding: 0; }
  #user-email { display: none; }
  .theme-toggle .theme-label { display: none; }
  .theme-toggle { padding: 6px 8px; }
  /* Nav becomes a fixed bottom tab bar */
  nav { position: fixed; left: 0; right: 0; bottom: 0; z-index: 6; flex-direction: row; gap: 0; background: var(--panel); border-top: 1px solid var(--border);
    padding: 4px 4px env(safe-area-inset-bottom); overflow-x: auto; scrollbar-width: none; }
  nav::-webkit-scrollbar { display: none; }
  nav a { flex: 1 0 52px; flex-direction: column; gap: 3px; padding: 6px 2px; font-size: 10px; border-radius: 10px; height: var(--nav-h); justify-content: center; }
  nav a.active { background: transparent; }
  nav svg { width: 22px; height: 22px; }
  nav .full { display: none; } nav .short { display: inline; }
  main { padding: 16px 16px calc(var(--nav-h) + 24px + env(safe-area-inset-bottom)); }
  .page-head h1 { font-size: 20px; }
  .kpis { grid-template-columns: 1fr 1fr; gap: 10px; }
  .kpi { padding: 14px; } .kpi .value { font-size: 19px; }
  .two { grid-template-columns: 1fr; }
  .chart-box { height: 220px; }
  .filters > *, .filters input[type=search] { min-width: 0; flex: 1 1 45%; }
  .filters input[type=search] { flex-basis: 100%; }
  .hide-sm { display: none; }
  th, td { padding: 9px 6px; }
  td.wrap { max-width: 180px; }
  /* iOS zooms into inputs under 16px; keep them at 16px */
  input, select, textarea { font-size: 16px; }
  /* Modal as a bottom sheet */
  dialog { width: 100vw; max-width: 100vw; margin: auto 0 0; border-radius: 16px 16px 0 0; max-height: 92vh; overflow-y: auto;
    padding: 20px 16px calc(20px + env(safe-area-inset-bottom)); }
  .form-grid { grid-template-columns: 1fr; }
  .toast { left: 16px; right: 16px; bottom: calc(var(--nav-h) + 16px + env(safe-area-inset-bottom)); text-align: center; }
}
