/* =========================================================================
   PWA Starter Kit — design system
   Mobile-first. Two breakpoints only (640 / 1024). No framework, no build.

   Theme colours are injected by build.sh from config.json into the
   `--brand-*` tokens below; everything else derives from them.
   ========================================================================= */

/* ---------- 1. Tokens ---------------------------------------------------- */
:root {
  /* Brand — overwritten by build.sh */
  --brand-theme: #1a1a2e;
  --brand-accent: #4f7cff;
  --brand-bg: #0f0f1a;

  /* Derived accent ramp */
  --accent: var(--brand-accent);
  --accent-soft: color-mix(in oklab, var(--accent) 18%, transparent);
  --accent-line: color-mix(in oklab, var(--accent) 40%, transparent);
  --accent-ink: #ffffff;

  /* Surfaces (dark default) */
  --bg: var(--brand-bg);
  --surface: color-mix(in oklab, var(--brand-theme) 82%, #ffffff);
  --surface-2: color-mix(in oklab, var(--brand-theme) 92%, #ffffff);
  --surface-3: color-mix(in oklab, var(--brand-theme) 70%, #ffffff);
  --line: color-mix(in oklab, #ffffff 12%, transparent);
  --line-strong: color-mix(in oklab, #ffffff 22%, transparent);

  /* Ink */
  --ink: #f2f3f7;
  --ink-2: color-mix(in oklab, #f2f3f7 72%, transparent);
  --ink-3: color-mix(in oklab, #f2f3f7 48%, transparent);

  /* Status */
  --ok: #34d399;
  --warn: #fbbf24;
  --err: #f87171;
  --info: #60a5fa;
  --muted: #94a3b8;

  /* Geometry */
  --r-sm: 8px;
  --r: 14px;
  --r-lg: 20px;
  --r-full: 999px;

  /* Spacing scale */
  --s1: 4px;  --s2: 8px;  --s3: 12px; --s4: 16px;
  --s5: 24px; --s6: 32px; --s7: 48px; --s8: 64px;

  /* Type */
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;

  /* Elevation */
  --shadow-1: 0 1px 2px rgb(0 0 0 / .3);
  --shadow-2: 0 4px 16px rgb(0 0 0 / .35);
  --shadow-3: 0 12px 40px rgb(0 0 0 / .45);

  /* Chrome */
  --header-h: 56px;
  --tabbar-h: 60px;
  --maxw: 1100px;

  --ease: cubic-bezier(.2, .7, .3, 1);
  color-scheme: dark;
}

/* Light theme: opt in with <html data-scheme="light">, or follow the OS when
   config sets color_scheme:"auto" (build.sh adds data-scheme="auto"). */
:root[data-scheme="light"],
:root[data-scheme="auto"] {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #f0f2f8;
  --surface-3: #e6e9f2;
  --line: color-mix(in oklab, #0b1020 12%, transparent);
  --line-strong: color-mix(in oklab, #0b1020 24%, transparent);
  --ink: #10142a;
  --ink-2: color-mix(in oklab, #10142a 70%, transparent);
  --ink-3: color-mix(in oklab, #10142a 45%, transparent);
  --shadow-1: 0 1px 2px rgb(16 20 42 / .08);
  --shadow-2: 0 4px 16px rgb(16 20 42 / .10);
  --shadow-3: 0 12px 40px rgb(16 20 42 / .16);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root[data-scheme="auto"] {
    --bg: var(--brand-bg);
    --surface: color-mix(in oklab, var(--brand-theme) 82%, #ffffff);
    --surface-2: color-mix(in oklab, var(--brand-theme) 92%, #ffffff);
    --surface-3: color-mix(in oklab, var(--brand-theme) 70%, #ffffff);
    --line: color-mix(in oklab, #ffffff 12%, transparent);
    --line-strong: color-mix(in oklab, #ffffff 22%, transparent);
    --ink: #f2f3f7;
    --ink-2: color-mix(in oklab, #f2f3f7 72%, transparent);
    --ink-3: color-mix(in oklab, #f2f3f7 48%, transparent);
    color-scheme: dark;
  }
}

/* ---------- 2. Reset ----------------------------------------------------- */
*, *::before, *::after { box-sizing: border-box; }
* { margin: 0; }

html {
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
}
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
}

body {
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.55;
  color: var(--ink);
  background: var(--bg);
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
  /* Safe areas for notched devices in standalone mode */
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
           env(safe-area-inset-bottom) env(safe-area-inset-left);
  overscroll-behavior-y: none;
}

img, svg, video, canvas { display: block; max-width: 100%; height: auto; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); text-decoration-thickness: 1px; text-underline-offset: 2px; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--r-sm);
}

::selection { background: var(--accent-soft); }

/* ---------- 3. Layout ---------------------------------------------------- */
.app {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: var(--s3);
  height: var(--header-h);
  padding: 0 var(--s4);
  background: color-mix(in oklab, var(--bg) 82%, transparent);
  backdrop-filter: saturate(1.4) blur(12px);
  border-bottom: 1px solid var(--line);
}
.app-header .brand {
  display: flex; align-items: center; gap: var(--s2);
  font-weight: 650; letter-spacing: -.01em;
}
.app-header .brand img { width: 26px; height: 26px; border-radius: 7px; }
.app-header .spacer { flex: 1; }

.app-main {
  flex: 1;
  width: 100%;
  max-width: var(--maxw);
  margin-inline: auto;
  padding: var(--s5) var(--s4) calc(var(--s8) + env(safe-area-inset-bottom));
}

.view { display: none; }
.view.is-active { display: block; animation: fade .22s var(--ease); }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } }

/* Bottom tab bar — the primary nav on mobile, hidden on wide screens */
.tabbar {
  position: sticky;
  bottom: 0;
  z-index: 20;
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: var(--s1);
  padding: var(--s2) var(--s2) calc(var(--s2) + env(safe-area-inset-bottom));
  background: color-mix(in oklab, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
}
.tabbar button {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: var(--s2) var(--s1);
  border: 0; background: none; cursor: pointer;
  color: var(--ink-3);
  font-size: 11px; font-weight: 600; letter-spacing: .01em;
  border-radius: var(--r-sm);
  transition: color .16s var(--ease), background .16s var(--ease);
}
.tabbar button .ico { font-size: 20px; line-height: 1; }
.tabbar button[aria-selected="true"] { color: var(--accent); background: var(--accent-soft); }
.tabbar button:active { transform: scale(.96); }

@media (min-width: 1024px) {
  .tabbar {
    position: fixed; bottom: auto; top: calc(var(--header-h) + var(--s5));
    left: max(var(--s4), calc(50vw - var(--maxw) / 2 - 200px));
    width: 176px;
    grid-auto-flow: row; grid-auto-columns: auto;
    border: 1px solid var(--line); border-radius: var(--r);
    background: var(--surface);
  }
  .tabbar button { flex-direction: row; justify-content: flex-start; gap: var(--s3); font-size: 14px; padding: var(--s3); }
}

/* ---------- 4. Grid & cards ---------------------------------------------- */
.grid {
  display: grid;
  gap: var(--s4);
  grid-template-columns: 1fr;
}
@media (min-width: 640px) { .grid { grid-template-columns: repeat(2, 1fr); } }
@media (min-width: 1024px) { .grid { grid-template-columns: repeat(3, 1fr); } }
.grid.cols-2 { grid-template-columns: 1fr; }
@media (min-width: 640px) { .grid.cols-2 { grid-template-columns: repeat(2, 1fr); } }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r);
  padding: var(--s4);
  box-shadow: var(--shadow-1);
  display: flex;
  flex-direction: column;
  gap: var(--s3);
  min-width: 0;
}
.card.is-disabled { opacity: .55; }
.card-head { display: flex; align-items: flex-start; gap: var(--s3); }
.card-icon {
  width: 38px; height: 38px; flex: none;
  display: grid; place-items: center;
  font-size: 20px;
  border-radius: 11px;
  background: var(--surface-3);
  border: 1px solid var(--line);
}
.card-title { font-weight: 640; letter-spacing: -.01em; line-height: 1.3; }
.card-sub { font-size: 13px; color: var(--ink-3); }
.card-desc { font-size: 14px; color: var(--ink-2); }
.card-foot { margin-top: auto; display: flex; flex-wrap: wrap; gap: var(--s2); }

/* ---------- 5. Buttons --------------------------------------------------- */
.btn {
  --btn-bg: var(--surface-3);
  --btn-ink: var(--ink);
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s2);
  min-height: 40px;
  padding: 0 var(--s4);
  border: 1px solid var(--line-strong);
  border-radius: var(--r-full);
  background: var(--btn-bg);
  color: var(--btn-ink);
  font-size: 14px; font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: transform .12s var(--ease), filter .16s var(--ease), background .16s var(--ease);
  -webkit-tap-highlight-color: transparent;
}
.btn:hover:not(:disabled) { filter: brightness(1.12); }
.btn:active:not(:disabled) { transform: scale(.97); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { --btn-bg: var(--accent); --btn-ink: var(--accent-ink); border-color: transparent; }
.btn.ghost { --btn-bg: transparent; }
.btn.danger { --btn-bg: color-mix(in oklab, var(--err) 22%, transparent); border-color: var(--err); }
.btn.sm { min-height: 32px; padding: 0 var(--s3); font-size: 13px; }
.btn.block { width: 100%; }
.btn.icon { padding: 0; width: 40px; }

/* ---------- 6. Badges ---------------------------------------------------- */
.badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 2px 9px;
  font-size: 11px; font-weight: 700;
  letter-spacing: .03em; text-transform: uppercase;
  border-radius: var(--r-full);
  border: 1px solid currentColor;
  white-space: nowrap;
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.ok      { color: var(--ok); }
.badge.warn    { color: var(--warn); }
.badge.err     { color: var(--err); }
.badge.info    { color: var(--info); }
.badge.muted   { color: var(--muted); }
.badge.plain::before { display: none; }

/* ---------- 7. Forms ----------------------------------------------------- */
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field label { font-size: 13px; font-weight: 600; color: var(--ink-2); }
.field .hint { font-size: 12px; color: var(--ink-3); }

.input, select.input, textarea.input {
  width: 100%;
  min-height: 42px;
  padding: 9px var(--s3);
  background: var(--surface-2);
  color: var(--ink);
  border: 1px solid var(--line-strong);
  border-radius: var(--r-sm);
  transition: border-color .16s var(--ease), box-shadow .16s var(--ease);
}
.input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea.input { min-height: 88px; resize: vertical; font-family: inherit; }

.switch { display: flex; align-items: center; gap: var(--s3); cursor: pointer; user-select: none; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch .track {
  width: 44px; height: 26px; flex: none;
  border-radius: var(--r-full);
  background: var(--surface-3);
  border: 1px solid var(--line-strong);
  position: relative;
  transition: background .18s var(--ease), border-color .18s var(--ease);
}
.switch .track::after {
  content: ""; position: absolute; top: 2px; left: 2px;
  width: 20px; height: 20px; border-radius: 50%;
  background: var(--ink-2);
  transition: transform .18s var(--ease), background .18s var(--ease);
}
.switch input:checked + .track { background: var(--accent); border-color: transparent; }
.switch input:checked + .track::after { transform: translateX(18px); background: #fff; }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }
.switch input:disabled + .track { opacity: .4; }

/* ---------- 8. Output / log panes ---------------------------------------- */
.out {
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.6;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  padding: var(--s3);
  max-height: 220px;
  overflow: auto;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  color: var(--ink-2);
}
.out:empty { display: none; }
.out .l-ok   { color: var(--ok); }
.out .l-err  { color: var(--err); }
.out .l-warn { color: var(--warn); }
.out .l-info { color: var(--info); }
.out .t { color: var(--ink-3); }

/* Media preview shared by camera / screen-capture / PiP modules */
.preview {
  width: 100%;
  aspect-ratio: 4 / 3;
  background: #000;
  border-radius: var(--r-sm);
  border: 1px solid var(--line);
  object-fit: cover;
}

/* ---------- 9. Tables ---------------------------------------------------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--r); }
table.data { width: 100%; border-collapse: collapse; font-size: 14px; min-width: 520px; }
table.data th, table.data td { padding: 10px var(--s3); text-align: left; border-bottom: 1px solid var(--line); }
table.data th {
  font-size: 11px; text-transform: uppercase; letter-spacing: .05em;
  color: var(--ink-3); font-weight: 700;
  background: var(--surface-2);
  position: sticky; top: 0;
}
table.data tr:last-child td { border-bottom: 0; }
table.data tbody tr:hover { background: var(--surface-2); }
table.data td.num { text-align: right; font-variant-numeric: tabular-nums; font-family: var(--mono); }

/* ---------- 10. Stats ---------------------------------------------------- */
.stats { display: grid; gap: var(--s3); grid-template-columns: repeat(2, 1fr); }
@media (min-width: 640px) { .stats { grid-template-columns: repeat(4, 1fr); } }
.stat {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--r); padding: var(--s4);
}
.stat .k { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--ink-3); font-weight: 700; }
.stat .v { font-size: 28px; font-weight: 700; letter-spacing: -.02em; font-variant-numeric: tabular-nums; margin-top: 2px; }
.stat .d { font-size: 12px; color: var(--ink-3); }

/* Tiny CSS-only bar chart used on the admin dashboard */
.bars { display: flex; flex-direction: column; gap: var(--s2); }
.bar-row { display: grid; grid-template-columns: 1fr 3fr auto; gap: var(--s3); align-items: center; font-size: 13px; }
.bar-row .label { color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-row .track { height: 8px; background: var(--surface-3); border-radius: var(--r-full); overflow: hidden; }
.bar-row .fill { height: 100%; background: var(--accent); border-radius: var(--r-full); transition: width .5s var(--ease); }
.bar-row .n { font-family: var(--mono); font-size: 12px; color: var(--ink-3); }

/* ---------- 11. Toasts --------------------------------------------------- */
.toasts {
  position: fixed;
  left: 50%; transform: translateX(-50%);
  bottom: calc(var(--tabbar-h) + var(--s4) + env(safe-area-inset-bottom));
  z-index: 100;
  display: flex; flex-direction: column; gap: var(--s2);
  width: min(440px, calc(100vw - var(--s5)));
  pointer-events: none;
}
.toast {
  pointer-events: auto;
  display: flex; align-items: center; gap: var(--s3);
  padding: var(--s3) var(--s4);
  background: var(--surface-3);
  border: 1px solid var(--line-strong);
  border-radius: var(--r);
  box-shadow: var(--shadow-3);
  font-size: 14px;
  animation: toast-in .24s var(--ease);
}
.toast.ok  { border-color: var(--ok); }
.toast.err { border-color: var(--err); }
.toast.leaving { animation: toast-out .18s var(--ease) forwards; }
@keyframes toast-in  { from { opacity: 0; transform: translateY(14px) scale(.97); } }
@keyframes toast-out { to   { opacity: 0; transform: translateY(8px) scale(.98); } }

/* ---------- 12. Install banner ------------------------------------------- */
.install-banner {
  display: none;
  align-items: center; gap: var(--s3);
  padding: var(--s3) var(--s4);
  margin-bottom: var(--s4);
  background: var(--accent-soft);
  border: 1px solid var(--accent-line);
  border-radius: var(--r);
}
.install-banner.show { display: flex; }
.install-banner .txt { flex: 1; min-width: 0; font-size: 14px; }
.install-banner strong { display: block; }

/* ---------- 13. Helpers -------------------------------------------------- */
.stack   { display: flex; flex-direction: column; gap: var(--s4); }
.stack-2 { display: flex; flex-direction: column; gap: var(--s2); }
.row     { display: flex; flex-wrap: wrap; gap: var(--s2); align-items: center; }
.row.between { justify-content: space-between; }
.section-title {
  font-size: 12px; text-transform: uppercase; letter-spacing: .07em;
  color: var(--ink-3); font-weight: 700;
  margin: var(--s5) 0 var(--s3);
}
.section-title:first-child { margin-top: 0; }
h1 { font-size: 26px; font-weight: 700; letter-spacing: -.025em; line-height: 1.2; }
h2 { font-size: 20px; font-weight: 660; letter-spacing: -.02em; }
h3 { font-size: 16px; font-weight: 640; }
.lede { color: var(--ink-2); font-size: 15px; }
.mono { font-family: var(--mono); font-size: 13px; }
.muted-text { color: var(--ink-3); }
.small { font-size: 13px; }
.hidden { display: none !important; }
.spinner {
  width: 15px; height: 15px; flex: none;
  border: 2px solid var(--line-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.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;
}

/* ---------- 14. View Transitions ----------------------------------------- */
@view-transition { navigation: auto; }
::view-transition-old(root) { animation: vt-out .2s var(--ease) both; }
::view-transition-new(root) { animation: vt-in .24s var(--ease) both; }
@keyframes vt-out { to { opacity: 0; transform: translateY(-8px); } }
@keyframes vt-in { from { opacity: 0; transform: translateY(10px); } }

/* ---------- 15. Print ---------------------------------------------------- */
@media print {
  .app-header, .tabbar, .toasts, .btn { display: none !important; }
  body { background: #fff; color: #000; }
}
