:root {
  color-scheme: light;
  --bg: #f4f4f2;
  --surface: #fcfcfb;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #898781;
  --line: #e1e0d9;
  --axis: #c3c2b7;
  --series: #2a78d6;
  --series-soft: #2a78d61f;
  --good: #0ca30c;
  --good-text: #006300;
  --warning: #fab219;
  --critical: #d03b3b;
  --focus: #2a78d6;
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --bg: #121211;
    --surface: #1a1a19;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --muted: #898781;
    --line: #2c2c2a;
    --axis: #383835;
    --series: #3987e5;
    --series-soft: #3987e526;
    --good-text: #0ca30c;
    --focus: #3987e5;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211;
  --surface: #1a1a19;
  --text: #f0efec;
  --text-2: #c3c2b7;
  --muted: #898781;
  --line: #2c2c2a;
  --axis: #383835;
  --series: #3987e5;
  --series-soft: #3987e526;
  --good-text: #0ca30c;
  --focus: #3987e5;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.muted { color: var(--muted); }
.sub { color: var(--text-2); }
[hidden] { display: none !important; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 16px;
  padding: 12px 24px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; font-size: 15px; white-space: nowrap; }
.tabs { display: flex; gap: 4px; overflow-x: auto; flex: 1; scrollbar-width: none; }
.tabs button {
  border: 0; background: none; padding: 6px 12px; border-radius: 8px; cursor: pointer;
  color: var(--text-2); white-space: nowrap;
}
.tabs button[aria-selected="true"] { background: var(--series-soft); color: var(--text); font-weight: 600; }
.who { display: flex; align-items: center; gap: 10px; white-space: nowrap; font-size: 13px; }
.role-chip { border: 1px solid var(--line); border-radius: 99px; padding: 1px 8px; color: var(--text-2); font-size: 12px; }

/* ---------- layout ---------- */
main { max-width: 1200px; margin: 0 auto; padding: 24px; }
.page-head { display: flex; flex-wrap: wrap; align-items: end; justify-content: space-between; gap: 12px; margin-bottom: 20px; }
.page-head h1 { margin: 0; font-size: 22px; line-height: 1.2; }
.controls { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.card h2 { margin: 0 0 12px; font-size: 14px; font-weight: 600; }
.card h2 .sub { font-weight: 400; }
.grid { display: grid; gap: 16px; }
.kpis { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); margin-bottom: 16px; }
.two { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); margin-bottom: 16px; }
.stack > * + * { margin-top: 16px; }

.kpi .label { color: var(--text-2); font-size: 12px; }
.kpi .value { font-size: 24px; font-weight: 650; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi .delta { font-size: 12px; margin-top: 2px; color: var(--muted); }
.delta.up { color: var(--good-text); }
.delta.down { color: var(--critical); }

/* ---------- inputs ---------- */
.input, select.input {
  height: 34px; padding: 0 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface);
}
.btn {
  height: 34px; padding: 0 14px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface);
  cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--axis); }
a.btn { display: inline-flex; align-items: center; text-decoration: none; }
.btn.primary { background: var(--series); border-color: var(--series); color: #fff; font-weight: 600; }
.btn.small { height: 28px; padding: 0 10px; font-size: 12px; }
.btn.link { border: 0; background: none; padding: 0; height: auto; color: var(--series); }

.seg { display: inline-flex; max-width: 100%; border: 1px solid var(--line); border-radius: 10px; padding: 3px; background: var(--surface); gap: 2px; }
.seg button { border: 0; background: none; padding: 7px 14px; border-radius: 7px; cursor: pointer; color: var(--text-2); white-space: nowrap; }
.seg button[aria-pressed="true"] { background: var(--series); color: #fff; font-weight: 600; }
@media (max-width: 720px) { .seg { display: flex; } .seg button { flex: 1; padding: 9px 6px; white-space: normal; font-size: 13px; } }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; margin: 0 -16px -16px; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 16px; border-top: 1px solid var(--line); text-align: left; }
th { font-size: 12px; font-weight: 600; color: var(--text-2); border-top: 0; white-space: nowrap; }
th[data-sort] { cursor: pointer; user-select: none; }
th[data-sort]::after { content: ""; margin-left: 4px; }
th[aria-sort="ascending"]::after { content: "▲"; font-size: 9px; }
th[aria-sort="descending"]::after { content: "▼"; font-size: 9px; }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover { background: var(--series-soft); }
tfoot td { font-weight: 650; border-top: 2px solid var(--axis); }
.outlet-name { font-weight: 550; }
.outlet-meta { font-size: 12px; color: var(--muted); }

/* status: icon + label, never colour alone */
.status { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; font-size: 13px; }
.status::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.status.full::before { background: var(--good); }
.status.appreciation::before { background: var(--warning); }
.status.none::before { background: transparent; border: 1.5px solid var(--muted); width: 5px; height: 5px; }

.streak { display: inline-flex; gap: 2px; align-items: center; }
.streak i { width: 6px; height: 12px; border-radius: 2px; background: var(--line); }
.streak i.on { background: var(--series); }
.streak span { margin-left: 6px; font-variant-numeric: tabular-nums; }
.bonus-next { font-size: 12px; color: var(--good-text); white-space: nowrap; }

/* ---------- chart ---------- */
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: 220px; overflow: visible; }
.chart .grid-line { stroke: var(--line); stroke-width: 1; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 11px; }
.chart .bar { fill: var(--series); }
.chart .bar.dim { opacity: .45; }
.chart .hit { fill: transparent; cursor: default; }
.chart .hit:hover + .bar, .chart .bar.hover { opacity: 1; filter: brightness(1.12); }
.tooltip {
  position: absolute; pointer-events: none; background: var(--text); color: var(--surface);
  padding: 6px 10px; border-radius: 6px; font-size: 12px; white-space: nowrap; transform: translate(-50%, calc(-100% - 8px));
}

/* ---------- drawer ---------- */
.drawer-backdrop { position: fixed; inset: 0; background: #0006; z-index: 20; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(560px, 100%); z-index: 21;
  background: var(--bg); border-left: 1px solid var(--line); overflow-y: auto; padding: 20px;
}
.drawer header { display: flex; justify-content: space-between; align-items: start; gap: 12px; margin-bottom: 16px; }
.drawer h2 { margin: 0; font-size: 18px; }

.modal {
  position: fixed; z-index: 21; top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: min(400px, calc(100% - 32px));
}
.modal .login form { margin-top: 8px; }

.jump { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }
.jump a { padding: 6px 12px; border: 1px solid var(--line); border-radius: 99px; color: var(--text-2); text-decoration: none; background: var(--surface); font-size: 13px; }
.jump a:hover { border-color: var(--axis); color: var(--text); }
.badge { display: inline-block; min-width: 18px; padding: 0 6px; border-radius: 99px; background: var(--warning); color: #0b0b0b; font-size: 11px; font-weight: 700; text-align: center; }
fieldset.rules { border: 1px solid var(--line); border-radius: 8px; padding: 12px; margin: 0; }
fieldset.rules legend { padding: 0 6px; font-weight: 600; font-size: 13px; }
fieldset.rules .form-row { grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 12px 16px; align-items: start; }
fieldset.rules label { line-height: 1.35; }
.unit-input { display: flex; align-items: center; gap: 6px; }
.unit-input .input { flex: 1; min-width: 0; text-align: right; font-variant-numeric: tabular-nums; }
.unit-input span { color: var(--muted); font-size: 12px; min-width: 40px; }
td a { color: var(--series); text-decoration: none; } td a:hover { text-decoration: underline; }
td .btn.small.primary { color: #fff; }

/* ---------- misc ---------- */
.progress { height: 8px; border-radius: 99px; background: var(--line); overflow: hidden; margin-top: 8px; }
.progress > div { height: 100%; background: var(--series); border-radius: 99px; }
.notice { padding: 12px 14px; border-radius: 8px; background: var(--series-soft); color: var(--text); margin-bottom: 16px; }
.notice.warn { background: color-mix(in srgb, var(--warning) 18%, transparent); border: 1px solid color-mix(in srgb, var(--warning) 55%, transparent); }
.secret { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 15px; padding: 10px 12px; border: 1px dashed var(--axis); border-radius: 8px; user-select: all; }
.form-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 8px; align-items: end; }
.form-row label { display: grid; gap: 4px; font-size: 12px; color: var(--text-2); }
.empty { padding: 32px; text-align: center; color: var(--muted); }
.error { color: var(--critical); }

/* ---------- responsive ---------- */
.show-sm { display: none; }
.grid > *, .card { min-width: 0; }            /* let wide tables scroll inside the card, never the page */
form[data-account] { flex-wrap: nowrap; }
form[data-account] .input { width: 160px; min-width: 0; flex: 0 1 auto; }
.check { display: inline-flex; align-items: center; gap: 8px; }
td .btn.small { margin: 2px 0 2px 4px; }

@media (min-width: 721px) { .kpis.five { grid-template-columns: repeat(5, minmax(0, 1fr)); } }

/* tablet and small laptop */
@media (max-width: 1024px) {
  .hide-md { display: none !important; }
  .kpi .value { font-size: 20px; overflow-wrap: anywhere; }
  .topbar { padding: 10px 16px; gap: 10px; }
  main { padding: 20px 16px; }
  .two { grid-template-columns: 1fr; }
}

/* phone */
@media (max-width: 720px) {
  .topbar { flex-wrap: wrap; gap: 8px; padding-top: max(10px, env(safe-area-inset-top)); }
  .tabs { order: 3; flex-basis: 100%; }
  .who { margin-left: auto; gap: 6px; }
  #who { display: none; }
  .role-chip { max-width: 34vw; overflow: hidden; text-overflow: ellipsis; }
  main { padding: 16px 12px calc(24px + env(safe-area-inset-bottom)); }
  .page-head { align-items: stretch; flex-direction: column; }
  .page-head h1 { font-size: 20px; }
  .controls > .input, .controls > .btn { flex: 1 1 140px; justify-content: center; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .kpi .value { font-size: 20px; overflow-wrap: anywhere; }
  .kpis > :last-child:nth-child(odd) { grid-column: 1 / -1; }
  .outlet-meta { overflow-wrap: anywhere; }
  td.num { padding-left: 4px; }
  .card { padding: 14px; }
  .table-wrap { margin: 0 -14px -14px; }
  th, td { padding: 9px 14px; }
  .hide-sm { display: none !important; }
  .show-sm { display: block; margin-top: 4px; font-size: 12px; }
  span.show-sm { display: inline; margin: 0; font-size: 14px; }
  .show-sm .status { font-size: 12px; }
  .input, select.input { font-size: 16px; height: 40px; }   /* 16px stops iOS zooming on focus */
  .btn { height: 40px; }
  .btn.small { height: 34px; }
  .drawer { padding: 14px 12px; border-left: 0; }
  .chart svg { height: 200px; }

  /* admin tables become one card per row */
  table.stack-sm thead { display: none; }
  table.stack-sm, table.stack-sm tbody, table.stack-sm tr, table.stack-sm td { display: block; width: 100%; }
  table.stack-sm tr { border-top: 1px solid var(--line); padding: 10px 0; }
  table.stack-sm td { border: 0; padding: 4px 14px; text-align: left; white-space: normal; }
  table.stack-sm td .btn.small { margin: 4px 6px 0 0; }
  .bonus-next { white-space: normal; }
  form[data-account] .input { flex: 1 1 auto; width: auto; }
  form[data-account] .btn { flex: 0 0 auto; }
}

@media (max-width: 380px) {
  .role-chip { display: none; }
  .kpi .value { font-size: 18px; }
}

/* ---------- login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login { width: min(380px, 100%); }
.login h1 { font-size: 20px; margin: 0 0 4px; }
.login form { display: grid; gap: 12px; margin-top: 20px; }
.login label { display: grid; gap: 4px; font-size: 13px; color: var(--text-2); }
.login .input { height: 40px; }
.login .btn { height: 40px; }

/* Sheet import: outlet picker for names the system does not know */
.alias-row select { display: block; width: 100%; max-width: 100%; margin-top: 8px; }

.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
