/* Cardigan — design tokens and layout.
   Every colour is a token in the blocks below; no colour literal appears in JS
   or anywhere under the token blocks (a test checks, as in VitalitySync).
   Light and dark follow the phone. The barcode itself is always black on white
   (--code-*), whatever the theme: that's what till scanners read best. */

:root {
  --bg: #faf6f1; --surface: #ffffff; --surface-2: #f1ebe4; --line: #e2d8cd;
  --text: #2a1f2d; --muted: #6e6273; --accent: #5b2f63; --accent-text: #ffffff; --focus: #c4862a;
  --good: #2f7a4a; --good-bg: #e3f3e8; --bad: #b23a2e; --bad-bg: #fbe6e3; --warn: #8a5a00; --warn-bg: #fdf1d8;
  --code-bg: #ffffff; --code-ink: #000000; --scrim: #000000b3; --on-scrim: #ffffff; --aim: #f2c14e;
  --c1: #c8463a; --c2: #d9772b; --c3: #b88a12; --c4: #3f8a4f; --c5: #1f8a8a; --c6: #3a6fb8; --c7: #7a4a9e; --c8: #6b6b75;
  --on-tile: #ffffff;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --radius: 14px; --tap: 48px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17121a; --surface: #221b26; --surface-2: #2c2431; --line: #3b3241;
    --text: #f2ecf3; --muted: #b0a5b5; --accent: #d9a6e3; --accent-text: #1c1320; --focus: #f2c14e;
    --good: #7fd19b; --good-bg: #1d3325; --bad: #ff9d90; --bad-bg: #3d1f1b; --warn: #f2c14e; --warn-bg: #3a2e12;
    --c1: #a83a30; --c2: #b8611f; --c3: #94700c; --c4: #2f6e3d; --c5: #17706f; --c6: #2f5b98; --c7: #643c82; --c8: #55555e;
  }
}

/* ---------------------------------------------------------------- base ---- */

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg); color: var(--text); font-family: var(--font);
  font-size: 17px; line-height: 1.45; -webkit-text-size-adjust: 100%;
  min-height: 100dvh; padding-bottom: env(safe-area-inset-bottom);
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
[hidden] { display: none !important; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
h1, h2 { line-height: 1.2; }
.wrap { max-width: 720px; margin: 0 auto; padding: 16px; }
.muted { color: var(--muted); }
.help { color: var(--muted); font-size: 14px; margin: 4px 0 12px; }
.error { color: var(--bad); }

/* -------------------------------------------------------------- header ---- */

header.top {
  position: sticky; top: 0; z-index: 10; background: var(--bg);
  border-bottom: 1px solid var(--line);
  padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
  display: flex; align-items: center; gap: 8px;
}
header.top h1 { font-size: 22px; margin: 0 0 0 4px; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.icon-btn {
  min-width: var(--tap); min-height: var(--tap); border: 0; border-radius: 999px;
  background: transparent; font-size: 26px; line-height: 1; display: inline-grid; place-items: center;
}
.icon-btn.small { min-width: 36px; min-height: 36px; font-size: 20px; }
.icon-btn:active { background: var(--surface-2); }

/* ------------------------------------------------------------- buttons ---- */

.btn {
  min-height: var(--tap); padding: 10px 18px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface); font-weight: 600;
}
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.danger { color: var(--bad); border-color: var(--bad); background: transparent; width: 100%; margin-top: 24px; }
.btn.small { min-height: 38px; padding: 6px 14px; font-size: 15px; }
.btn:disabled { opacity: .45; cursor: default; }
.link-btn { border: 0; background: none; padding: 4px; color: var(--accent); font-weight: 600; text-decoration: underline; }

.add-row { display: flex; gap: 10px; flex-wrap: wrap; margin: 20px 0 8px; }
.add-row .btn { flex: 1 1 160px; }
.add-row.big .btn { min-height: 56px; font-size: 18px; }

/* -------------------------------------------------------------- notice ---- */

.notice {
  display: flex; align-items: center; gap: 8px; margin: 8px 16px 0; padding: 6px 6px 6px 14px;
  border-radius: var(--radius); background: var(--surface-2); max-width: 688px;
}
.notice span { flex: 1; }
.notice.good { background: var(--good-bg); color: var(--good); }
.notice.bad { background: var(--bad-bg); color: var(--bad); }
@media (min-width: 720px) { .notice { margin: 8px auto 0; } }

/* ---------------------------------------------------------------- list ---- */

.toolbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }
.search {
  flex: 1 1 200px; min-height: var(--tap); padding: 8px 14px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface);
}
.segs { display: inline-flex; border: 1px solid var(--line); border-radius: 999px; overflow: hidden; background: var(--surface); }
.seg { border: 0; background: transparent; min-height: 40px; padding: 6px 14px; font-size: 15px; }
.seg.on { background: var(--accent); color: var(--accent-text); font-weight: 600; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
@media (max-width: 359px) { .grid { grid-template-columns: 1fr 1fr; gap: 10px; } }
.tile {
  aspect-ratio: 1.586; border: 0; border-radius: var(--radius); padding: 12px; text-align: left;
  color: var(--on-tile); display: flex; flex-direction: column; justify-content: space-between; min-width: 0;
}
.tile:active { transform: scale(.97); }
.tile-initials { font-size: 30px; font-weight: 800; letter-spacing: .02em; opacity: .9; }
.tile-name { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.c1 { background: var(--c1); } .c2 { background: var(--c2); } .c3 { background: var(--c3); } .c4 { background: var(--c4); }
.c5 { background: var(--c5); } .c6 { background: var(--c6); } .c7 { background: var(--c7); } .c8 { background: var(--c8); }

.empty-state { text-align: center; padding-top: 48px; }
.empty-state p { color: var(--muted); max-width: 420px; margin: 8px auto; }
.hello-icon { font-size: 56px; }

/* -------------------------------------------------------- at the till ---- */

body[data-screen="card"], body[data-screen="scan"] { background: var(--scrim); }
.till {
  min-height: 100dvh; display: flex; flex-direction: column; color: var(--on-tile);
  padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom);
}
.till-top { display: flex; align-items: center; gap: 8px; padding: 8px 12px; }
.till-top h1 { flex: 1; min-width: 0; margin: 0; font-size: 22px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.till-top .icon-btn { color: var(--on-tile); }
.till-top .btn { background: transparent; color: var(--on-tile); border-color: var(--on-tile); }
.sheet {
  background: var(--code-bg); color: var(--code-ink); margin: 8px 12px; border-radius: 18px;
  padding: 20px 16px; display: flex; flex-direction: column; align-items: center; gap: 12px;
}
.code-box { width: 100%; display: grid; place-items: center; }
.code-box.is-linear { height: min(34vh, 220px); }
.code-box.is-matrix { height: min(48vh, 320px); }
.code-box.is-stacked { height: min(32vh, 220px); }
svg.code { display: block; width: 100%; height: 100%; }
svg.code.matrix { width: auto; max-width: 100%; aspect-ratio: auto; }
.code-bg { fill: var(--code-bg); }
.code-ink { fill: var(--code-ink); }
.number { font-family: var(--mono); font-size: clamp(22px, 7vw, 34px); font-weight: 700; letter-spacing: .04em; margin: 0; text-align: center; word-break: break-all; }
.note { margin: 0; color: var(--code-ink); opacity: .7; text-align: center; }
.no-code { font-size: 18px; text-align: center; margin: 24px 0; }
.hint { text-align: center; padding: 0 24px; margin: 8px 0 24px; opacity: .9; font-size: 15px; }

/* -------------------------------------------------------------- form ---- */

.form label, .form .label { display: block; font-weight: 600; margin: 16px 0 6px; }
.form input:not([type="radio"]), .form select {
  width: 100%; min-height: var(--tap); padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--surface);
}
.form #f-number { font-family: var(--mono); font-size: 19px; letter-spacing: .03em; }
.field-error { color: var(--bad); font-size: 15px; margin: 4px 0 0; min-height: 0; }
.field-error:empty { display: none; }
.warn { background: var(--warn-bg); color: var(--warn); border-radius: 10px; padding: 8px 12px; margin: 8px 0 0; }
.scanned { display: flex; justify-content: space-between; align-items: center; gap: 8px; background: var(--good-bg); color: var(--good); border-radius: 10px; padding: 6px 6px 6px 12px; }
.preview { background: var(--code-bg); border-radius: 10px; margin-top: 4px; height: 96px; display: grid; place-items: center; padding: 8px; border: 1px solid var(--line); }
.preview:empty { display: none; }
.preview .no-code { color: var(--code-ink); font-family: var(--mono); margin: 0; }
.swatches { display: grid; grid-template-columns: repeat(8, minmax(0, 44px)); gap: 10px; padding: 5px; align-items: start; }
.swatch { display: block; width: 100%; height: auto; aspect-ratio: 1; border-radius: 999px; cursor: pointer; margin: 0; position: relative; }
.swatch input { appearance: none; -webkit-appearance: none; position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; border-radius: 999px; cursor: pointer; }
.swatch:has(input:checked) { outline: 3px solid var(--text); outline-offset: 3px; }
.swatch input:focus-visible { outline: 3px solid var(--focus); outline-offset: 6px; }
.form-actions { display: flex; gap: 10px; margin-top: 28px; }
.form-actions .btn { flex: 1; }

/* ------------------------------------------------------------- camera ---- */

.scan { position: fixed; inset: 0; background: var(--code-ink); color: var(--on-scrim); }
.cam { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.aim {
  position: absolute; left: 8%; right: 8%; top: 32%; height: 26%;
  border: 3px solid var(--aim); border-radius: 16px; box-shadow: 0 0 0 100vmax var(--scrim);
}
.scan-top, .scan-bottom { position: absolute; left: 0; right: 0; display: flex; align-items: center; gap: 8px; padding: 12px; }
.scan-top { top: 0; padding-top: calc(12px + env(safe-area-inset-top)); }
.scan-top h1 { flex: 1; margin: 0; font-size: 20px; }
.scan-top .icon-btn { color: var(--on-scrim); }
.scan-top .btn { background: transparent; color: var(--on-scrim); border-color: var(--on-scrim); }
.scan-bottom { bottom: 0; flex-direction: column; padding-bottom: calc(20px + env(safe-area-inset-bottom)); text-align: center; }
.cam-status { margin: 0 0 8px; max-width: 460px; }
.cam-status.error { color: var(--on-scrim); background: var(--bad); border-radius: 10px; padding: 8px 12px; }

/* ----------------------------------------------------------- settings ---- */

.settings section { border-bottom: 1px solid var(--line); padding-bottom: 12px; }
.settings section:last-child { border-bottom: 0; }
.settings h2 { font-size: 19px; margin: 18px 0 6px; }
.facts { display: grid; grid-template-columns: max-content 1fr; gap: 6px 14px; margin: 8px 0; }
.facts dt { font-weight: 600; }
.facts dd { margin: 0; color: var(--muted); }
@media (max-width: 420px) { .facts { grid-template-columns: 1fr; } .facts dd { margin-bottom: 8px; } }
