:root {
  --paper: #eef1f4;
  --card: #ffffff;
  --ink: #12202e;
  --muted: #5b6b7a;
  --line: #d7dee5;
  --accent: #1f5fbf;
  --accent-ink: #ffffff;
  --over: #c23b2e;
  --soon: #b87a00;
  --ok: #2f7d5c;
  --over-bg: #fbe9e6;
  --soon-bg: #fbf0d5;
  --ok-bg: #e2f1ea;
  --none-bg: #e6ebf0;
  --shadow: 0 1px 2px rgba(18, 32, 46, 0.06);
  --radius: 14px;
  --tabbar: 64px;
  --font: 'Bricolage Grotesque', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #0e1620;
    --card: #16212d;
    --ink: #e8eef4;
    --muted: #93a3b3;
    --line: #26343f;
    --accent: #5b93e8;
    --accent-ink: #08121d;
    --over: #f0705f;
    --soon: #e3b341;
    --ok: #5fbf94;
    --over-bg: #3a1e1c;
    --soon-bg: #352b10;
    --ok-bg: #16302a;
    --none-bg: #1e2b37;
    --shadow: none;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 400 16px/1.45 var(--font);
  min-height: 100dvh;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; border-radius: 6px; }

#app { max-width: 600px; margin: 0 auto; min-height: 100dvh; padding-bottom: calc(var(--tabbar) + 96px + env(safe-area-inset-bottom)); }

/* ---------- header ---------- */
/* ---------- section bar: Admin | Routines ---------- */
.secbar {
  position: sticky; top: 0; z-index: 7; padding: calc(8px + env(safe-area-inset-top)) 16px 6px; background: var(--paper);
}
.secbar nav { display: flex; gap: 4px; padding: 4px; background: var(--none-bg); border-radius: 14px; }
.secbar a {
  flex: 1; display: flex; align-items: center; justify-content: center; gap: 8px; min-height: 40px; border-radius: 10px;
  color: var(--muted); text-decoration: none; font-weight: 600; font-size: 15px;
}
.secbar a svg { width: 20px; height: 20px; }
.secbar a[aria-current=page] { background: var(--card); color: var(--accent); box-shadow: var(--shadow); }

.top {
  position: sticky; top: calc(58px + env(safe-area-inset-top)); z-index: 5;
  display: flex; align-items: center; gap: 8px;
  padding: 8px 16px 12px;
  background: var(--paper);
}
.top h1 { flex: 1; margin: 0; font-size: 30px; line-height: 1.1; font-weight: 700; font-variation-settings: 'opsz' 48; letter-spacing: -0.01em; min-width: 0; overflow-wrap: anywhere; }
.top .sub { display: block; margin-top: 2px; font-size: 15px; font-weight: 400; color: var(--muted); letter-spacing: 0; }
.icon-btn {
  display: grid; place-items: center; width: 44px; height: 44px; flex: none;
  border: 0; border-radius: 12px; background: transparent; color: var(--ink);
}
.icon-btn:active { background: var(--none-bg); }
.icon-btn svg { width: 24px; height: 24px; }

/* ---------- lists ---------- */
.group { margin: 4px 16px 20px; }
.group > h2 { margin: 0 0 8px 2px; font-size: 15px; font-weight: 600; color: var(--muted); }
.group > h2 .n { font-weight: 400; }
.group.over > h2 { color: var(--over); }
.stack { display: grid; gap: 8px; }

.row {
  display: flex; align-items: stretch; width: 100%; text-align: left;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: hidden; padding: 0; color: inherit; text-decoration: none;
}
.row:active { border-color: var(--accent); }
.row .body { flex: 1; min-width: 0; padding: 12px 14px; display: grid; gap: 1px; align-content: center; }
.row .title { font-weight: 600; overflow-wrap: anywhere; }
.row .meta { font-size: 14px; color: var(--muted); overflow-wrap: anywhere; }
.row .notes { font-size: 14px; color: var(--muted); margin-top: 4px; white-space: pre-wrap; overflow-wrap: anywhere; }

/* The day-count tab: the one loud element. */
.days {
  flex: none; width: 68px; display: grid; place-content: center; text-align: center; padding: 8px 4px;
  background: var(--none-bg); color: var(--muted);
}
.days b { display: block; font-size: 28px; line-height: 1; font-weight: 800; font-variant-numeric: tabular-nums; font-variation-settings: 'opsz' 72; }
.days small { display: block; margin-top: 3px; font-size: 12px; font-weight: 500; }
.days.ok { background: var(--ok-bg); color: var(--ok); }
.days.soon { background: var(--soon-bg); color: var(--soon); }
.days.over { background: var(--over); color: #fff; }
.days.today { background: var(--soon); color: #fff; }
.days.none b { font-size: 22px; }
.days.word b { font-size: 17px; font-weight: 700; }

.row-actions { display: flex; align-items: center; gap: 6px; padding-right: 8px; }
.chip-btn {
  border: 1px solid var(--line); background: transparent; border-radius: 999px; padding: 8px 14px; min-height: 40px; font-weight: 600; font-size: 14px;
}
.chip-btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

/* ---------- detail ---------- */
.card { margin: 0 16px 20px; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.card h2, .section-h { margin: 0; font-size: 15px; font-weight: 600; color: var(--muted); }
.section-h { margin: 4px 18px 8px; display: flex; align-items: center; justify-content: space-between; min-height: 40px; }
.kv { display: grid; grid-template-columns: minmax(96px, 36%) 1fr; gap: 4px 12px; padding: 12px 14px; border-top: 1px solid var(--line); width: 100%; text-align: left; background: none; border-left: 0; border-right: 0; border-bottom: 0; }
.kv:first-child { border-top: 0; }
.kv dt, .kv .k { color: var(--muted); font-size: 14px; }
.kv dd, .kv .v { margin: 0; overflow-wrap: anywhere; font-weight: 500; }
.note-card { padding: 12px 14px; white-space: pre-wrap; overflow-wrap: anywhere; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 0 16px 20px; }

/* ---------- empty / login ---------- */
.empty { margin: 32px 24px; text-align: center; color: var(--muted); }
.empty h2 { margin: 0 0 6px; color: var(--ink); font-size: 20px; }
.empty p { margin: 0 0 16px; }
.login { padding: calc(64px + env(safe-area-inset-top)) 24px 24px; max-width: 420px; margin: 0 auto; }
.login h1 { font-size: 36px; margin: 0 0 6px; font-weight: 800; letter-spacing: -0.02em; }
.login p { color: var(--muted); margin: 0 0 24px; }
.banner { margin: 0 16px 12px; padding: 10px 14px; border-radius: 12px; background: var(--soon-bg); color: var(--soon); font-size: 14px; font-weight: 500; }
.error { color: var(--over); font-size: 14px; min-height: 1.4em; margin: 8px 0 0; }

/* ---------- forms ---------- */
label.f { display: grid; gap: 6px; margin: 0 0 14px; font-size: 14px; font-weight: 600; color: var(--muted); }
input[type=text], input[type=password], input[type=date], select, textarea {
  width: 100%; min-height: 48px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--paper); color: var(--ink); font-weight: 400; font-size: 16px;
}
textarea { min-height: 96px; resize: vertical; }
.pair { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 8px; align-items: start; }
.pair .x { grid-column: 1 / -1; justify-self: end; margin-top: -2px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 48px; padding: 0 20px; border-radius: 12px; border: 1px solid var(--line); background: var(--card); font-weight: 600;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.danger { color: var(--over); border-color: var(--over); background: transparent; }
.btn.wide { width: 100%; }
.btn[disabled] { opacity: 0.6; }
.btn-row { display: flex; gap: 8px; margin-top: 8px; }
.btn-row .btn { flex: 1; }

/* ---------- sheet ---------- */
dialog#sheet {
  margin: auto auto 0; width: 100%; max-width: 600px; max-height: 92dvh; padding: 0; border: 0; border-radius: 22px 22px 0 0;
  background: var(--card); color: var(--ink); overflow: auto; overscroll-behavior: contain;
}
dialog#sheet::backdrop { background: rgba(8, 14, 22, 0.55); }
dialog#sheet[open] { animation: rise 0.18s ease-out; }
@keyframes rise { from { transform: translateY(24px); opacity: 0.6; } to { transform: none; opacity: 1; } }
.sheet-in { padding: 18px 18px calc(18px + env(safe-area-inset-bottom)); }
.sheet-in h2 { margin: 0 0 14px; font-size: 22px; font-weight: 700; }
pre.sheet-text { white-space: pre-wrap; overflow-wrap: anywhere; font: 400 15px/1.5 var(--font); background: var(--paper); padding: 14px; border-radius: 12px; margin: 0 0 14px; }
.copybox { display: grid; gap: 8px; }
.copybox code { display: block; padding: 12px; border-radius: 12px; background: var(--paper); font-size: 13px; overflow-wrap: anywhere; user-select: all; }
.sheet-in p.help { color: var(--muted); font-size: 14px; margin: 0 0 14px; }

/* ---------- nav & fab ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6; display: flex; justify-content: center;
  background: var(--card); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
}
.tabbar nav { display: flex; width: 100%; max-width: 600px; }
.tabbar a {
  flex: 1; height: var(--tabbar); display: grid; place-content: center; justify-items: center; gap: 2px; color: var(--muted); text-decoration: none; font-size: 12px; font-weight: 600;
}
.tabbar a svg { width: 24px; height: 24px; }
.tabbar a[aria-current=page] { color: var(--accent); }
.fab {
  position: fixed; right: max(16px, calc((100vw - 600px) / 2 + 16px)); bottom: calc(var(--tabbar) + 16px + env(safe-area-inset-bottom)); z-index: 6;
  display: inline-flex; align-items: center; gap: 8px; min-height: 52px; padding: 0 20px; border: 0; border-radius: 999px;
  background: var(--accent); color: var(--accent-ink); font-weight: 700; box-shadow: 0 6px 16px rgba(18, 32, 46, 0.25);
}
.fab svg { width: 22px; height: 22px; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar) + 84px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(12px);
  background: var(--ink); color: var(--paper); padding: 10px 16px; border-radius: 999px; font-size: 14px; font-weight: 600; opacity: 0; pointer-events: none;
  transition: opacity 0.15s, transform 0.15s; z-index: 20; max-width: 90vw; text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%); }

/* ---------- routines ---------- */
.fab.nobar { bottom: calc(20px + env(safe-area-inset-bottom)); }
.banner.live { background: var(--ok-bg); color: var(--ok); }
.rcard { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 14px 14px 16px; }
.rhead { display: flex; align-items: flex-start; gap: 8px; }
.rtitle { flex: 1; min-width: 0; display: grid; gap: 1px; }
.rtitle b { font-size: 19px; font-weight: 700; overflow-wrap: anywhere; }
.rtitle .meta { font-size: 14px; color: var(--muted); }
.rhead .icon-btn { margin: -6px -6px 0 0; }
.rsteps { margin: 4px 0 14px; color: var(--muted); font-size: 14px; overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

.step { display: grid; grid-template-columns: 1fr 76px auto; gap: 8px; align-items: center; padding: 10px 0 12px; border-top: 1px solid var(--line); }
.step:first-child { border-top: 0; padding-top: 0; }
.step .full { grid-column: 1 / -1; }
.step .unit { color: var(--muted); font-size: 14px; }
.step input[type=number] { width: 100%; min-height: 48px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--paper); color: var(--ink); font-size: 16px; }
.step-act { grid-column: 1 / -1; display: flex; gap: 6px; justify-content: flex-end; }

/* The runner: one thing on screen at a time. */
#app.bare { padding-bottom: 0; }
#app.bare ~ #toast { bottom: auto; top: calc(76px + env(safe-area-inset-top)); }
.run { min-height: 100dvh; display: flex; flex-direction: column; padding: calc(10px + env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom)); }
.run-top { display: flex; align-items: center; gap: 8px; }
.run-title { flex: 1; min-width: 0; display: grid; text-align: center; }
.run-title b { font-weight: 700; overflow-wrap: anywhere; }
.run-title span { font-size: 14px; color: var(--muted); }
.icon-btn.armed { background: var(--over); color: #fff; }
.segs { display: flex; gap: 4px; margin: 12px 0 0; }
.segs i { flex: 1; height: 6px; border-radius: 3px; background: var(--line); }
.segs i.d { background: var(--ok); }
.segs i.c { background: var(--accent); }
.run-mid { flex: 1; display: flex; flex-direction: column; justify-content: center; text-align: center; padding: 16px 0; }
.run-mid h1 { margin: 0; font-size: clamp(30px, 9vw, 46px); line-height: 1.1; font-weight: 800; letter-spacing: -0.02em; overflow-wrap: anywhere; }
.rnote { margin: 10px 0 0; color: var(--muted); font-size: 18px; overflow-wrap: anywhere; white-space: pre-wrap; }
.clock { margin: 22px 0 14px; font-size: clamp(76px, 26vw, 140px); line-height: 1; font-weight: 800; font-variant-numeric: tabular-nums; font-variation-settings: 'opsz' 96; letter-spacing: -0.03em; }
.clock.low { color: var(--soon); }
.clock.over { color: var(--over); }
.bar { height: 10px; border-radius: 5px; background: var(--line); overflow: hidden; margin: 0 auto; width: 100%; max-width: 420px; }
.bar i { display: block; height: 100%; width: 0; background: var(--accent); border-radius: 5px; }
.status { min-height: 1.5em; margin: 12px 0 0; font-weight: 600; color: var(--over); }
.finish { margin: 4px 0 0; color: var(--muted); font-size: 14px; }
.run-actions { display: grid; gap: 8px; }
.btn.big { min-height: 64px; font-size: 20px; border-radius: 16px; }
.next { margin: 6px 0 0; text-align: center; color: var(--muted); font-size: 15px; overflow-wrap: anywhere; }
.run.done .run-mid h1 { font-size: 48px; }
.took { margin: 14px 0 0; font-size: 20px; color: var(--muted); }

@media (prefers-reduced-motion: reduce) {
  dialog#sheet[open] { animation: none; }
  #toast { transition: none; }
}

@media print {
  .tabbar, .secbar, .fab, .top .icon-btn, .actions, #toast { display: none !important; }
  body { background: #fff; color: #000; }
}
