:root {
  --bg: #1d1a2b;
  --bg-2: #262238;
  --panel: #2e2944;
  --card: #fff8e7;
  --card-ink: #2a2433;
  --ink: #f4efe2;
  --muted: #b7b0c9;
  --line: #120f1c;
  --accent: #ff9a1f;
  --accent-ink: #2a1600;
  --good: #5fd068;
  --water: #4fb8f0;
  --danger: #ff5e6c;
  --chip: #3a3454;
  --px: 3px;
  --radius: 0;
  --font: 'Pixelify Sans', ui-monospace, 'SF Mono', Menlo, monospace;
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #efe6d2;
    --bg-2: #e4d8bf;
    --panel: #f7f0df;
    --card: #ffffff;
    --card-ink: #2a2433;
    --ink: #2a2433;
    --muted: #6d6580;
    --line: #2a2433;
    --chip: #ffffff;
    color-scheme: light;
  }
}

/* Manual override from Settings → Theme (forced dark just skips the block above). */
:root[data-theme="light"] {
  --bg: #efe6d2;
  --bg-2: #e4d8bf;
  --panel: #f7f0df;
  --card: #ffffff;
  --card-ink: #2a2433;
  --ink: #2a2433;
  --muted: #6d6580;
  --line: #2a2433;
  --chip: #ffffff;
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 17px;
  line-height: 1.35;
  -webkit-font-smoothing: none;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

button { font: inherit; color: inherit; cursor: pointer; }
h1, h2, h3 { margin: 0 0 .4em; font-weight: 700; line-height: 1.15; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.3rem; }
h3 { font-size: .95rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
p { margin: 0 0 .8em; }
.muted { color: var(--muted); }
.center { text-align: center; }
[hidden] { display: none !important; }

canvas.px-icon, #stage { image-rendering: pixelated; image-rendering: crisp-edges; }
.px-icon { display: inline-block; vertical-align: middle; flex: none; }

/* Pixel border: a stepped outline made of four shadows. */
.pixel, .btn, .chip, .card, .dlg, .say, .act, .icon-btn, .last-summary, .stat, .now-card, .wx {
  border: none;
  border-radius: var(--radius);
  box-shadow:
    0 calc(-1 * var(--px)) 0 0 var(--line),
    0 var(--px) 0 0 var(--line),
    calc(-1 * var(--px)) 0 0 0 var(--line),
    var(--px) 0 0 0 var(--line);
}

/* ---------- layout ---------- */

.app {
  height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  grid-template-columns: minmax(0, 1fr);
  padding-top: env(safe-area-inset-top);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--bg-2);
  border-bottom: var(--px) solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 1.05rem; white-space: nowrap; }
.top-info { flex: 1; min-width: 0; display: flex; gap: 12px; justify-content: center; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; }
.clock { color: var(--ink); font-weight: 600; }
.top-actions { display: flex; gap: 10px; }
.icon-btn {
  width: 38px; height: 38px;
  display: grid; place-items: center;
  background: var(--chip);
  margin: var(--px);
}
.icon-btn.text { font-weight: 700; font-size: .9rem; }
.icon-btn:active { transform: translateY(2px); }

.layout {
  min-height: 0;
  display: grid;
  grid-template-rows: minmax(210px, 42%) auto minmax(0, 1fr);
  grid-template-columns: minmax(0, 1fr);
}
/* During the workday the stage shrinks to make room for the pinned Now block. */
.app[data-screen="day"] .layout { grid-template-rows: clamp(170px, 29%, 240px) auto minmax(0, 1fr); }

.stage-wrap {
  grid-row: 1;
  position: relative;
  min-height: 0;
  overflow: hidden;
  display: grid;
  place-items: center;
  background: #5cbcf5;
  border-bottom: var(--px) solid var(--line);
}
#stage { display: block; }

.panel {
  grid-row: 3;
  min-height: 0;
  overflow-y: auto;
  padding: 18px 16px 24px;
  background: var(--panel);
  -webkit-overflow-scrolling: touch;
}

.view { display: none; max-width: 640px; margin: 0 auto; }
.app[data-screen="welcome"] .view[data-view="welcome"],
.app[data-screen="day"] .view[data-view="day"],
.app[data-screen="lunch"] .view[data-view="lunch"],
.app[data-screen="summary"] .view[data-view="summary"] { display: block; animation: rise .35s steps(5) both; }

@keyframes rise { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: none; } }

/* Landscape tablets / desktop / phones: stage + Now block on the left, panel on the right */
@media (min-width: 820px) and (orientation: landscape), (min-width: 1100px), (orientation: landscape) and (max-height: 500px) {
  .layout, .app[data-screen="day"] .layout { grid-template-rows: minmax(120px, 1fr) minmax(0, auto); grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
  .stage-wrap { grid-column: 1; border-bottom: none; }
  .panel { grid-row: 1 / span 2; grid-column: 2; padding: 24px; border-left: var(--px) solid var(--line); }
  /* .now placement lives after the .now base rules below */
}

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

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px;
  padding: 8px 16px;
  margin: var(--px);
  background: var(--chip);
  color: var(--ink);
  font-weight: 600;
  transition: transform .05s;
}
.btn:active { transform: translateY(2px); }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-ghost { background: transparent; }
.btn-big { width: calc(100% - 2 * var(--px)); min-height: 56px; font-size: 1.2rem; margin-top: 12px; }
.btn.wide { width: calc(100% - 2 * var(--px)); margin-top: 14px; }
.card .btn { background: #efe6d2; color: var(--card-ink); }
.card .btn-primary { background: var(--accent); color: var(--accent-ink); }
.card .btn-ghost { background: transparent; }

.greeting { color: var(--accent); }

/* ---------- speech card ---------- */

.card {
  position: relative;
  margin: var(--px) var(--px) 22px;
  padding: 14px 16px 12px;
  background: var(--card);
  color: var(--card-ink);
  animation: pop .3s steps(4) both;
}
.card.quiet { background: #e9f4ff; }
@keyframes pop { 0% { transform: scale(.85); opacity: 0; } 100% { transform: none; opacity: 1; } }
.card.shake { animation: shake .4s steps(6) 2; }
@keyframes shake { 0%,100% { transform: none; } 25% { transform: translateX(-4px); } 75% { transform: translateX(4px); } }

.card-head { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; }
.card-title { font-size: 1.25rem; font-weight: 700; }
.card-cheek { font-style: italic; color: #a1462d; margin-bottom: 6px; }
.card-text { margin-bottom: 10px; }
.card-text ul { margin: 0; padding-left: 1.1em; }
.card-actions { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.card-actions .spacer { flex: 1; }
.snooze-row { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 8px; }
.snooze-row .btn { min-height: 38px; padding: 4px 12px; }
.quiet-tag { font-size: .8rem; color: #4a6b8a; }

.progress {
  height: 14px;
  margin: 10px 0;
  background: #e2d9c3;
  box-shadow: 0 -3px 0 0 var(--line), 0 3px 0 0 var(--line), -3px 0 0 0 var(--line), 3px 0 0 0 var(--line);
  margin-inline: 3px;
}
.progress > div { height: 100%; background: var(--good); transition: width .25s steps(3); }
.guide-step { display: flex; justify-content: space-between; color: #6d6580; font-size: .9rem; }
.guide-big { font-size: 1.5rem; font-weight: 700; margin: 4px 0; }
.guide-now { display: flex; justify-content: space-between; align-items: baseline; font-size: 1.25rem; font-weight: 700; color: #2e7d3a; }
.guide-now.prep, .guide-now.switch { color: #6d6580; }
.guide-count { font-variant-numeric: tabular-nums; }
.routine { margin: 6px 0 0; padding-left: 1.4em; }
.routine li { margin: 2px 0; }
.routine li::marker { font-weight: 700; }
.routine .dose { float: right; margin-left: 10px; color: #6d6580; font-size: .9rem; white-space: nowrap; }

.checklist { list-style: none; margin: 4px 0 10px; padding: 0; }
.checklist li { margin: 0; }
.checklist label { display: flex; align-items: center; gap: 10px; padding: 8px 2px; cursor: pointer; }
.checklist input { appearance: none; width: 22px; height: 22px; flex: none; margin: 0; background: #fff; box-shadow: 0 -3px 0 0 var(--line), 0 3px 0 0 var(--line), -3px 0 0 0 var(--line), 3px 0 0 0 var(--line); margin-left: 3px; }
.checklist input:checked { background: var(--good); }
.checklist input:checked::after { content: '✓'; display: grid; place-items: center; color: #fff; font-weight: 700; height: 100%; }
.checklist input:checked + span { text-decoration: line-through; color: #8a8399; }

.breath {
  display: grid; place-items: center;
  height: 150px;
}
.breath-box {
  width: 60px; height: 60px;
  background: var(--water);
  box-shadow: 0 -3px 0 0 var(--line), 0 3px 0 0 var(--line), -3px 0 0 0 var(--line), 3px 0 0 0 var(--line);
  transition: transform 4s steps(16);
}
.breath-box.in, .breath-box.hold { transform: scale(2); }

/* ---------- now block (timer hero + posture/water tiles) ---------- */

.now {
  grid-row: 2;
  display: none;
  grid-template-columns: minmax(0, 640px);
  justify-content: center;
  gap: 12px;
  padding: 12px 16px 14px;
  background: var(--bg-2);
  border-bottom: var(--px) solid var(--line);
  container-type: inline-size; /* the timer scales with the block's width */
  --timer: clamp(2.2rem, 11cqi, 4.2rem);
}
.app[data-screen="day"] .now { display: grid; }

.now-card {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 10px;
  margin: var(--px);
  padding: 12px 14px;
  background: var(--chip);
  color: var(--ink);
}
.now-card.focus { background: #6b2f3a; color: #fff; }
.now-card.break { background: #2f6b3d; color: #fff; }
.now-card.meeting { background: #3d3f6b; color: #fff; }
/* The stage takes the height this block leaves, so the block keeps a fixed height whatever
   the text says: label is one line, sub is always two lines tall, the row always fits the timer. */
.now-row { display: flex; align-items: center; gap: 12px; min-height: var(--timer); }
.now-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.now-label { font-weight: 700; font-size: 1.05rem; margin-bottom: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.now-sub, .rounds { font-size: .85rem; }
.now-sub { opacity: .8; height: 2.7em; overflow: hidden; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; }
.now-main:has(.rounds) .now-sub { height: 1.35em; -webkit-line-clamp: 1; }
.now-timer { font-size: var(--timer); font-weight: 700; line-height: 1; font-variant-numeric: tabular-nums; white-space: nowrap; }
.now-timer:empty { display: none; }
.now-bar { display: grid; grid-auto-flow: column; gap: 2px; height: 10px; }
.now-bar i { background: rgba(255,255,255,.18); }
.now-bar i.on { background: rgba(255,255,255,.85); }
.now-card.idle .now-bar i { background: color-mix(in srgb, var(--muted) 25%, transparent); }
.now-card.idle .now-bar i.on { background: var(--accent); }
.rounds { display: flex; align-items: center; gap: 4px; height: 1.35em; }
.rounds i { width: 10px; height: 10px; background: rgba(255,255,255,.25); }
.rounds i.on { background: var(--danger); }

.tiles { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.chip {
  display: flex; align-items: center; gap: 10px;
  min-width: 0;
  min-height: 56px;
  margin: var(--px);
  padding: 8px 12px;
  background: var(--chip);
  text-align: left;
}
.chip .chip-main { display: flex; flex-direction: column; min-width: 0; }
.chip .chip-label, .chip .chip-sub { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chip .chip-label { font-weight: 700; }
.chip .chip-sub { font-size: .85rem; color: var(--muted); }
.chip:active { transform: translateY(2px); }
.pips { display: flex; gap: 3px; margin-top: 3px; flex-wrap: wrap; }

@media (min-width: 820px) and (orientation: landscape), (min-width: 1100px), (orientation: landscape) and (max-height: 500px) {
  .now { grid-column: 1; overflow-y: auto; border-bottom: none; border-top: var(--px) solid var(--line); }

  @media (min-height: 700px) {
    .now { padding: 20px 24px; gap: 16px; }
    .now-card { padding: 18px 20px; gap: 14px; }
    .now-label { font-size: 1.3rem; }
    .now-sub, .rounds { font-size: 1rem; }
    .now-bar { height: 14px; }
    .tiles .chip { padding: 12px 16px; }
  }
}
.pip { width: 8px; height: 10px; background: #554d73; }
.pip.on { background: var(--water); }

/* ---------- up next ---------- */

.upnext ul { list-style: none; margin: 0; padding: 0; }
.upnext li {
  display: flex; align-items: center; gap: 12px;
  padding: 9px 4px;
  border-bottom: 2px dashed color-mix(in srgb, var(--muted) 35%, transparent);
}
.upnext li .name { flex: 1; }
.upnext li .when { color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.upnext li.soon .when { color: var(--accent); }
.upnext li.held { opacity: .55; }

/* ---------- action bar ---------- */

.actionbar {
  display: none;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  background: var(--bg-2);
  border-top: var(--px) solid var(--line);
}
.app[data-screen="day"] .actionbar { display: grid; }
.act {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 8px 4px;
  margin: var(--px);
  background: var(--chip);
  font-size: .85rem; font-weight: 600;
}
.act:active { transform: translateY(2px); }
.act.on { background: var(--accent); color: var(--accent-ink); }
.act-icon { height: 24px; }

/* ---------- say bubble (on stage) ---------- */

.say {
  position: absolute;
  z-index: 2;
  max-width: min(260px, 70%);
  padding: 8px 12px;
  background: var(--card);
  color: var(--card-ink);
  font-size: .95rem;
  transform: translate(-50%, -100%);
  animation: say-pop .25s steps(3) both;
  pointer-events: none;
}
@keyframes say-pop {
  from { transform: translate(-50%, -100%) scale(.85); opacity: 0; }
  to { transform: translate(-50%, -100%); opacity: 1; }
}
.say::after {
  content: '';
  position: absolute;
  bottom: -9px; left: 50%;
  width: 9px; height: 9px;
  background: var(--card);
  transform: translateX(-50%);
  box-shadow: -3px 0 0 var(--line), 3px 0 0 var(--line), 0 3px 0 var(--line);
}

/* ---------- weather badge (on stage) ---------- */

.wx {
  position: absolute;
  z-index: 1;
  top: 9px; left: 9px;
  max-width: calc(100% - 18px);
  display: flex; align-items: center; gap: 8px;
  padding: 4px 10px 4px 6px;
  background: var(--card);
  color: var(--card-ink);
  text-align: left;
  line-height: 1.15;
}
.wx:active { transform: translateY(2px); }
.wx-main { display: block; font-weight: 700; font-size: .95rem; }
.wx-sub { display: block; font-size: .78rem; color: #6d6580; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- welcome / lunch / summary ---------- */

.last-summary { margin: 22px var(--px) 8px; padding: 12px 14px; background: var(--chip); }
.last-summary h3 { margin-bottom: 6px; }
.hint { margin-top: 18px; font-size: .9rem; color: var(--muted); }
.big-timer { font-size: 3rem; text-align: center; margin: 12px 0; font-variant-numeric: tabular-nums; }

.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; margin: 14px var(--px); }
.stat { padding: 10px 12px; background: var(--chip); display: flex; gap: 10px; align-items: center; }
.stat .v { font-size: 1.3rem; font-weight: 700; }
.stat .k { font-size: .8rem; color: var(--muted); }
.ratio { margin: 18px var(--px) 6px; }
.ratio-bar { display: flex; height: 18px; box-shadow: 0 -3px 0 0 var(--line), 0 3px 0 0 var(--line), -3px 0 0 0 var(--line), 3px 0 0 0 var(--line); }
.ratio-bar .sit { background: #8a6a4a; }
.ratio-bar .stand { background: var(--good); }
.ratio-legend { display: flex; justify-content: space-between; font-size: .85rem; color: var(--muted); margin-top: 8px; }
.verdict { font-size: 1.15rem; color: var(--accent); }

/* ---------- dialogs ---------- */

.dlg {
  width: min(420px, calc(100vw - 40px));
  max-height: calc(100dvh - 40px);
  padding: 20px;
  background: var(--panel);
  color: var(--ink);
}
.dlg-wide { width: min(560px, calc(100vw - 32px)); }
.dlg::backdrop { background: rgba(10, 8, 20, .6); }
.dlg .opts { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin: 14px 0 4px; }
.dlg .opts .btn:last-child { grid-column: span 3; }
.dlg .row { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }

.set-group { margin: 18px 0 0; }
.set-row { display: flex; align-items: center; gap: 10px; padding: 8px 0; flex-wrap: wrap; }
.set-row .lbl { flex: 1 1 160px; display: flex; align-items: center; gap: 8px; }
.set-row input[type="number"] {
  width: 64px; padding: 6px; font: inherit; text-align: center;
  background: var(--chip); color: var(--ink); border: 2px solid var(--line);
}
.set-row input[type="range"] { flex: 1 1 140px; accent-color: var(--accent); }
.set-row .small { font-size: .85rem; color: var(--muted); }
.set-row.sub { padding: 0 0 8px 34px; }
.set-row.sub .lbl { flex: 0 1 auto; }
.toggle { appearance: none; width: 44px; height: 24px; background: #554d73; position: relative; cursor: pointer; border: 2px solid var(--line); flex: none; margin: 0; }
.toggle::after { content: ''; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; background: #fff; transition: left .1s steps(2); }
.toggle:checked { background: var(--good); }
.toggle:checked::after { left: 22px; }
.seg { display: inline-flex; gap: 6px; }
.seg .btn { min-height: 36px; padding: 4px 12px; }
.seg .btn.on { background: var(--accent); color: var(--accent-ink); }

@media (max-width: 480px) {
  .brand > span:last-child { display: none; }
  .topbar { gap: 8px; }
}

@media (max-width: 380px) {
  html, body { font-size: 15px; }
  .top-info .day-timer { display: none; }
}

/* Phones in landscape: the screen is short, so shrink the bars, icons and type until the
   whole workday screen fits without scrolling. Icons are drawn with inline sizes, hence !important. */
@media (orientation: landscape) and (max-height: 500px) {
  html, body { font-size: 14px; }

  .topbar { padding: 3px 12px; gap: 10px; }
  .brand { font-size: .95rem; gap: 6px; }
  .top-actions { gap: 6px; }
  .icon-btn { width: 30px; height: 30px; }
  .brand .px-icon, .icon-btn .px-icon, .act-icon .px-icon, .wx .px-icon { width: 16px !important; height: 16px !important; }

  .layout, .app[data-screen="day"] .layout { grid-template-rows: minmax(70px, 1fr) auto; }
  .panel { padding: 12px 16px; }

  .now { padding: 6px 10px 8px; gap: 4px; --timer: clamp(1.6rem, 9cqi, 2.4rem); }
  .now-card { padding: 6px 10px; gap: 6px; }
  .now-row { gap: 10px; }
  .now-row .px-icon, .chip .px-icon { width: 24px !important; height: 24px !important; }
  .now-label { font-size: .95rem; }
  .now-sub, .rounds { font-size: .75rem; }
  .now-bar { height: 6px; }
  .tiles { gap: 8px; }
  .chip { min-height: 0; padding: 4px 10px; gap: 8px; }
  .chip .chip-sub { font-size: .75rem; }
  .pips { margin-top: 2px; }

  .actionbar { gap: 8px; padding: 3px 12px calc(3px + env(safe-area-inset-bottom)); }
  .act { flex-direction: row; justify-content: center; gap: 8px; padding: 5px 4px; font-size: .8rem; }
  .act-icon { height: 16px; }

  .wx { top: 6px; left: 6px; gap: 6px; padding: 2px 8px 2px 4px; }
  .wx-main { font-size: .85rem; }
  .wx-sub { font-size: .7rem; }

  .upnext li { padding: 6px 4px; }
  .card { padding: 10px 12px 8px; margin-bottom: 14px; }
  .card-title { font-size: 1.05rem; }
  .card-text { margin-bottom: 6px; }
  .checklist { margin-bottom: 6px; }
  .checklist label { padding: 4px 2px; }
  .btn { min-height: 36px; padding: 4px 12px; }
  .btn-big { min-height: 44px; font-size: 1.05rem; margin-top: 8px; }
  .big-timer { font-size: 2.2rem; margin: 6px 0; }
  .breath { height: 100px; }
  .breath-box { width: 40px; height: 40px; }

  .dlg { padding: 14px; max-height: calc(100dvh - 16px); }
  .dlg .opts { margin: 8px 0 2px; }
  .btn.wide { margin-top: 8px; }
  .set-group { margin-top: 10px; }
  .set-row { padding: 4px 0; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
