:root {
  --bg: #f3f1ec;
  --surface: #ffffff;
  --surface-2: #ebe8e1;
  --ink: #1d1f22;
  --ink-2: #565a60;
  --line: #d6d2c8;
  --brand: #1f3a4d;
  --brand-ink: #ffffff;
  --accent: #e8a317;
  --accent-ink: #1d1f22;
  --go: #1f7a4d;
  --go-ink: #ffffff;
  --info: #2c5f8a;
  --warn-bg: #fdecc8;
  --warn-ink: #6b4500;
  --bad: #b3261e;
  --bad-bg: #fbe3e1;
  --ok-bg: #dff1e6;
  --radius: 14px;
  --tap: 56px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 2px 8px rgba(0, 0, 0, 0.05);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #15181b;
    --surface: #1f2327;
    --surface-2: #2a2f34;
    --ink: #eceae5;
    --ink-2: #a5a9ae;
    --line: #3a4046;
    --brand: #16293a;
    --accent: #f0b232;
    --go: #2a9a63;
    --info: #6aa5d8;
    --warn-bg: #4a3610;
    --warn-ink: #ffd68a;
    --bad: #ff8a80;
    --bad-bg: #4a1c1a;
    --ok-bg: #173a27;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 17px/1.45 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  min-height: 100dvh;
}
button, input, select, textarea { font: inherit; color: inherit; }

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  background: var(--brand); color: var(--brand-ink);
  padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top));
  min-height: 56px;
}
.topbar h1 { font-size: 19px; margin: 0; flex: 1; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .back {
  background: transparent; border: 0; color: inherit; font-size: 17px;
  min-height: 44px; min-width: 44px; padding: 0 10px 0 4px; cursor: pointer; border-radius: 10px;
}
.topbar .back:active { background: rgba(255,255,255,.12); }

main { max-width: 760px; margin: 0 auto; padding: 16px 16px 120px; }

.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; margin: 0 0 12px; box-shadow: var(--shadow);
}
.card h2 { margin: 0 0 8px; font-size: 18px; }
.muted { color: var(--ink-2); }
.small { font-size: 14px; }
.mono { font-family: ui-monospace, 'SF Mono', Menlo, Consolas, monospace; letter-spacing: .02em; }
.row { display: flex; gap: 10px; align-items: center; }
.row > .grow { flex: 1; min-width: 0; }
.stack > * + * { margin-top: 10px; }
.hidden { display: none !important; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--tap); padding: 0 18px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  font-weight: 600; cursor: pointer; text-decoration: none; user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.btn:active { transform: translateY(1px); filter: brightness(.95); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.block { display: flex; width: 100%; }
.btn.primary { background: var(--go); border-color: var(--go); color: var(--go-ink); }
.btn.accent { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { background: transparent; color: var(--bad); border-color: var(--bad); }
.btn.ghost { background: transparent; }
.btn.small { min-height: 44px; padding: 0 14px; font-size: 15px; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; }
.btn-row > .btn { flex: 1 1 140px; }

/* Home */
.home-grid { display: grid; gap: 12px; }
.big-btn {
  display: flex; align-items: center; gap: 16px; text-align: left;
  min-height: 96px; padding: 16px 20px; border-radius: 18px; border: 0;
  font-size: 23px; font-weight: 700; cursor: pointer; text-decoration: none;
  box-shadow: var(--shadow); -webkit-tap-highlight-color: transparent;
}
.big-btn:active { transform: translateY(1px); filter: brightness(.95); }
.big-btn svg { width: 40px; height: 40px; flex: none; }
.big-btn .sub { display: block; font-size: 15px; font-weight: 500; opacity: .85; }
.big-btn.out { background: var(--accent); color: var(--accent-ink); }
.big-btn.in { background: var(--go); color: var(--go-ink); }
.big-btn.now { background: var(--brand); color: var(--brand-ink); }
.big-btn .badge {
  margin-left: auto; background: var(--bad); color: #fff; border-radius: 999px;
  font-size: 15px; padding: 2px 10px; font-weight: 700;
}
.minor-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 16px; }
.minor-grid .btn { min-height: 64px; }
.minor-grid .btn:last-child:nth-child(odd) { grid-column: 1 / -1; }

/* Forms */
label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 14px; font-weight: 600; color: var(--ink-2); margin-bottom: 4px; }
input[type='text'], input[type='search'], input[type='number'], input[type='date'], input[type='password'], select, textarea {
  width: 100%; min-height: 52px; padding: 10px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); font-size: 18px;
}
textarea { min-height: 80px; }
input:focus, select:focus, textarea:focus, .btn:focus-visible, .big-btn:focus-visible {
  outline: 3px solid var(--accent); outline-offset: 1px;
}
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* Lists */
.list { list-style: none; margin: 0; padding: 0; }
.list > li { border-top: 1px solid var(--line); padding: 12px 0; }
.list > li:first-child { border-top: 0; }
.pick {
  display: flex; width: 100%; align-items: center; gap: 12px; text-align: left;
  min-height: var(--tap); padding: 10px 12px; border: 1px solid var(--line);
  border-radius: 12px; background: var(--surface); cursor: pointer; margin-bottom: 8px;
}
.pick:active { background: var(--surface-2); }
.pick .title { font-weight: 650; }
.pick > .grow { flex: 1; min-width: 0; }

.tag {
  display: inline-block; font-size: 13px; font-weight: 700; padding: 2px 8px;
  border-radius: 999px; background: var(--surface-2); color: var(--ink-2); vertical-align: middle;
}
.tag.out { background: var(--warn-bg); color: var(--warn-ink); }
.tag.in { background: var(--ok-bg); color: var(--go); }
.tag.retired { background: var(--surface-2); color: var(--ink-2); text-decoration: line-through; }
.tag.overdue { background: var(--bad); color: #fff; }

.alert { border-radius: 12px; padding: 12px 14px; margin-bottom: 12px; font-weight: 500; }
.alert.bad { background: var(--bad-bg); color: var(--bad); }
.alert.warn { background: var(--warn-bg); color: var(--warn-ink); }
.alert.ok { background: var(--ok-bg); color: var(--go); }

.days {
  font-size: 34px; font-weight: 800; line-height: 1; font-variant-numeric: tabular-nums;
}
.days small { font-size: 14px; font-weight: 600; color: var(--ink-2); display: block; margin-top: 2px; }

.item-card { border-left: 6px solid var(--line); }
.item-card.overdue { border-left-color: var(--bad); background: var(--bad-bg); }
.group-head { display: flex; align-items: baseline; gap: 8px; margin: 18px 4px 8px; }
.group-head h2 { margin: 0; font-size: 19px; }

/* Scanner */
.scanner { position: relative; background: #000; border-radius: var(--radius); overflow: hidden; aspect-ratio: 4 / 3; max-height: 52vh; width: 100%; }
.scanner video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scanner .frame {
  position: absolute; inset: 18% 24%; border: 3px solid rgba(255,255,255,.85); border-radius: 16px;
  box-shadow: 0 0 0 999px rgba(0,0,0,.25);
}
.scanner .msg { position: absolute; left: 0; right: 0; bottom: 8px; text-align: center; color: #fff; font-size: 15px; text-shadow: 0 1px 2px #000; }
.picker { position: relative; margin-top: 12px; }
.picker-label { display: block; font-size: 14px; font-weight: 600; color: var(--ink-2); margin-bottom: 4px; }
.picker-input { min-height: 56px !important; scroll-margin-top: 72px; }
.picker-list {
  list-style: none; margin: 6px 0 0; padding: 4px; max-height: min(50vh, 420px); overflow-y: auto;
  overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
}
.picker-group {
  position: sticky; top: -4px; z-index: 1; background: var(--surface-2); color: var(--ink-2);
  font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em;
  padding: 6px 10px; border-radius: 8px; margin: 4px 0 2px;
}
.picker-opt {
  display: flex; flex-direction: column; justify-content: center; gap: 2px;
  min-height: 60px; padding: 8px 12px; border-radius: 10px; cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.picker-opt + .picker-opt { border-top: 1px solid var(--line); }
.picker-opt:hover, .picker-opt.active { background: var(--ok-bg); }
.picker-opt:active { filter: brightness(.95); }
.picker-main { font-size: 17px; }
.picker-sub { font-size: 14px; color: var(--ink-2); }
.picker-empty { padding: 14px 12px; color: var(--ink-2); line-height: 1.6; }

/* Bottom action bar for the primary step action */
.actionbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 9;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(6px); border-top: 1px solid var(--line);
  padding: 10px 16px; padding-bottom: max(10px, env(safe-area-inset-bottom));
}
.actionbar .inner { max-width: 760px; margin: 0 auto; display: flex; gap: 10px; }
.actionbar .btn { flex: 1; }

/* Steps */
.steps { display: flex; gap: 6px; margin-bottom: 14px; }
.steps span { flex: 1; height: 6px; border-radius: 3px; background: var(--line); }
.steps span.on { background: var(--accent); }

/* PIN pad */
.pin-wrap { max-width: 360px; margin: 6vh auto 0; text-align: center; }
.pin-wrap h1 { font-size: 24px; margin: 12px 0 4px; }
.pin-dots { display: flex; justify-content: center; gap: 14px; margin: 22px 0; min-height: 22px; }
.pin-dots i { width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--ink-2); }
.pin-dots i.on { background: var(--ink); border-color: var(--ink); }
.pinpad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.pinpad button {
  min-height: 72px; font-size: 28px; font-weight: 600; border-radius: 16px;
  border: 1px solid var(--line); background: var(--surface); cursor: pointer;
}
.pinpad button:active { background: var(--surface-2); }
.pinpad .go { background: var(--go); color: var(--go-ink); border-color: var(--go); font-size: 20px; }

/* Weekday toggles */
.weekdays { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; }
.weekdays button {
  min-height: 56px; border-radius: 12px; border: 2px solid var(--line); background: var(--surface);
  font-weight: 700; cursor: pointer;
}
.weekdays button[aria-pressed='true'] { background: var(--go); border-color: var(--go); color: var(--go-ink); }

table.summary { width: 100%; border-collapse: collapse; font-size: 16px; }
table.summary th, table.summary td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.summary td.num, table.summary th.num { text-align: right; font-variant-numeric: tabular-nums; }

.toast {
  position: fixed; left: 50%; bottom: 96px; transform: translateX(-50%); z-index: 50;
  background: var(--ink); color: var(--bg); padding: 12px 18px; border-radius: 12px;
  font-weight: 600; box-shadow: var(--shadow); max-width: calc(100% - 32px);
}

details > summary { cursor: pointer; min-height: 44px; display: flex; align-items: center; font-weight: 600; }

@media (min-width: 700px) {
  .home-grid { grid-template-columns: repeat(3, 1fr); }
  .big-btn { flex-direction: column; justify-content: center; text-align: center; min-height: 160px; }
  .big-btn .badge { margin: 0; }
  .minor-grid { grid-template-columns: repeat(5, 1fr); }
  .minor-grid .btn:last-child:nth-child(odd) { grid-column: auto; }
}

@media print {
  .topbar, .actionbar, .no-print, .toast { display: none !important; }
  body { background: #fff; color: #000; }
  main { padding: 0; max-width: none; }
  .card { box-shadow: none; border: 0; padding: 0; }
}
