:root {
  --bg: #f4f8f5;
  --surface: #ffffff;
  --surface-2: #e9f1ec;
  --line: #d9e5dd;
  --ink: #18241d;
  --ink-2: #4a5a50;
  --ink-3: #75857b;
  --accent: #2e8b57;
  --accent-ink: #ffffff;
  --accent-soft: #dcefe3;
  --warn: #b86e00;
  --danger: #c62828;
  --focus: #2f6fd9;
  --shadow: 0 1px 2px rgb(20 50 30 / 6%), 0 4px 16px rgb(20 50 30 / 6%);
  --radius: 14px;
  /* Calendar heat steps: one hue, light to dark (0 h, <1 h, 1–2, 2–4, 4–6, 6 h+). */
  --heat-0: var(--surface);
  --heat-1: #e3f3e8;
  --heat-2: #bfe3cb;
  --heat-3: #8fcca5;
  --heat-4: #4fa673;
  --heat-5: #2a7449;
  --heat-ink-strong: #ffffff;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121715;
    --surface: #1a211d;
    --surface-2: #232c27;
    --line: #2f3a33;
    --ink: #e8f0eb;
    --ink-2: #b3c2b8;
    --ink-3: #7f8f85;
    --accent: #4cc38a;
    --accent-ink: #0b1a12;
    --accent-soft: #1d3328;
    --warn: #f0a83a;
    --danger: #ff6b6b;
    --focus: #7aa7ff;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 4px 16px rgb(0 0 0 / 25%);
    --heat-1: #1d3328;
    --heat-2: #22503a;
    --heat-3: #2a6e4b;
    --heat-4: #38935f;
    --heat-5: #5cc98d;
    --heat-ink-strong: #0b1a12;
    color-scheme: dark;
  }
}
/* The last five minutes and the alarm: the whole page turns red. */
body.final {
  --bg: #c62828;
  --surface: #b3201f;
  --surface-2: rgb(255 255 255 / 20%);
  --line: rgb(255 255 255 / 28%);
  --ink: #ffffff;
  --ink-2: #ffe3e3;
  --ink-3: #ffc9c9;
  --accent: #ffffff;
  --accent-ink: #b3201f;
  --accent-soft: rgb(255 255 255 / 20%);
  --warn: #ffffff;
  --danger: #ffffff;
  --heat-0: transparent;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
  transition: background-color .4s;
}
[hidden] { display: none !important; }
a { color: var(--accent); }
h1, h2 { margin: 0; line-height: 1.2; }
h2 { font-size: 1.05rem; font-weight: 650; }
.muted { color: var(--ink-3); }
button { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4em;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  border-radius: 999px; padding: .6em 1.2em; cursor: pointer; font-weight: 550;
  transition: background .15s, transform .05s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: scale(.98); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: var(--accent); filter: brightness(1.06); }
.btn-secondary { background: var(--surface-2); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--ink-2); }
.btn-small { padding: .35em .9em; font-size: .9rem; }
.btn-big { padding: .8em 2.2em; font-size: 1.1rem; min-width: 9rem; }
.icon-btn {
  width: 2.4rem; height: 2.4rem; border-radius: 50%; border: 1px solid var(--line);
  background: var(--surface); cursor: pointer; font-size: 1.4rem; line-height: 1;
}
.icon-btn:hover { background: var(--surface-2); }

/* Landing */
.landing { min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px; }
.landing-card {
  max-width: 440px; width: 100%; background: var(--surface); border: 1px solid var(--line);
  border-radius: 20px; padding: 32px 28px; box-shadow: var(--shadow); text-align: center;
}
.landing-logo { display: block; margin: 0 auto 12px; }
.landing h1 { font-size: 2rem; letter-spacing: -.02em; }
.lede { color: var(--ink-2); margin: 10px 0 18px; }
.features { text-align: left; margin: 0 0 24px; padding-left: 1.2em; color: var(--ink-2); font-size: .95rem; }
.features li { margin: .35em 0; }
.signin-area { display: flex; flex-direction: column; align-items: center; gap: 12px; min-height: 44px; }
.signin-msg { margin: 0; color: var(--ink-2); font-size: .92rem; }
.signin-msg.error { color: var(--danger); }
.fineprint { font-size: .8rem; color: var(--ink-3); margin: 22px 0 0; }

/* App shell */
.app { max-width: 760px; margin: 0 auto; padding: 0 16px calc(32px + env(safe-area-inset-bottom)); }
.topbar {
  display: flex; align-items: center; gap: 12px; padding: calc(10px + env(safe-area-inset-top)) 0 10px;
  position: sticky; top: 0; background: var(--bg); z-index: 5;
}
.brand { display: flex; align-items: center; gap: 8px; text-decoration: none; color: var(--ink); font-weight: 700; font-size: 1.05rem; }
.tabs { display: flex; gap: 4px; margin-left: auto; background: var(--surface-2); padding: 3px; border-radius: 999px; }
.tabs a {
  text-decoration: none; color: var(--ink-2); padding: .35em 1em; border-radius: 999px; font-weight: 550; font-size: .95rem;
}
.tabs a[aria-selected="true"] { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.menu-wrap { position: relative; }
.avatar-btn {
  width: 36px; height: 36px; border-radius: 50%; border: 1px solid var(--line); background: var(--accent-soft);
  padding: 0; overflow: hidden; cursor: pointer; display: grid; place-items: center; font-weight: 700; color: var(--accent);
}
.avatar-btn img { width: 100%; height: 100%; object-fit: cover; }
.menu {
  position: absolute; right: 0; top: calc(100% + 8px); min-width: 250px; background: var(--surface);
  border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); padding: 6px; z-index: 20;
}
.menu-who { display: flex; flex-direction: column; padding: 8px 10px 10px; font-size: .9rem; }
.menu-who span { color: var(--ink-3); overflow: hidden; text-overflow: ellipsis; }
.menu button, .menu a {
  display: block; width: 100%; text-align: left; background: none; border: 0; padding: 9px 10px;
  border-radius: 8px; cursor: pointer; color: var(--ink); text-decoration: none; font-size: .95rem;
}
.menu button:hover, .menu a:hover { background: var(--surface-2); }
.menu hr { border: 0; border-top: 1px solid var(--line); margin: 6px 0; }
.menu .danger { color: var(--danger); }

.view { padding-top: 8px; }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow);
}

/* Dial */
.view-timer { display: flex; flex-direction: column; align-items: center; }
.dial { position: relative; width: min(78vw, 320px); aspect-ratio: 1; margin: 16px auto 8px; }
.ring { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring-track { fill: none; stroke: var(--surface-2); stroke-width: 14; }
.ring-fill {
  fill: none; stroke: var(--accent); stroke-width: 14; stroke-linecap: round;
  transition: stroke-dashoffset .3s linear, stroke .3s;
}
.dial.paused .ring-fill { opacity: .45; }
.dial-text { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.dial-state { font-size: .85rem; text-transform: uppercase; letter-spacing: .12em; color: var(--ink-3); font-weight: 650; }
.dial-time {
  font-size: clamp(3.2rem, 17vw, 4.6rem); font-weight: 300; letter-spacing: -.03em;
  font-variant-numeric: tabular-nums; line-height: 1.05; margin: 4px 0;
}
.dial-sub { font-size: .9rem; color: var(--ink-2); min-height: 1.3em; }

/* While a timer runs, the countdown takes over, readable at a glance from a desk. */
body.running .presets, body.running .custom-form, body.running .today-card { display: none !important; }
body.running .dial { width: min(92vw, 66dvh, 480px); margin-top: 8px; }
body.running .dial-time { font-size: min(22vw, 14dvh, 7.5rem); font-weight: 400; }
body.running .dial-state { font-size: min(4.2vw, 1.15rem); }
.presets { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin: 8px 0 4px; }
.presets button {
  min-width: 3.3rem; padding: .45em .9em; border-radius: 999px; border: 1px solid var(--line);
  background: var(--surface); cursor: pointer; font-variant-numeric: tabular-nums; font-weight: 550;
}
.presets button[aria-checked="true"] { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.presets button:disabled { opacity: .45; cursor: default; }
.custom-form { display: flex; gap: 8px; align-items: center; margin-top: 6px; }
.custom-form input {
  width: 5.5em; margin-left: 6px; padding: .35em .6em; border-radius: 8px; border: 1px solid var(--line);
  background: var(--surface); color: var(--ink); font: inherit;
}
.controls { display: flex; gap: 12px; margin: 22px 0 6px; flex-wrap: wrap; justify-content: center; }
.awake { font-size: .82rem; color: var(--ink-3); margin: 4px 0 0; }
.awake.off { color: var(--warn); }
.sound-hint { font-size: .85rem; color: var(--warn); margin: 4px 0 0; font-weight: 600; }

.today-card { width: 100%; margin-top: 24px; }
.today-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; margin-bottom: 10px; }
.today-link { display: inline-block; margin-top: 8px; font-size: .9rem; text-decoration: none; }

/* Timeline (24 h strip with session blocks) */
.timeline { position: relative; width: 100%; }
.timeline svg { display: block; width: 100%; overflow: visible; }
.tl-grid { stroke: var(--line); stroke-width: 1; }
.tl-base { fill: var(--surface-2); }
.tl-seg { fill: var(--accent); }
.tl-seg.partial { fill: var(--accent); opacity: .6; }
.tl-seg:hover, .tl-seg.hl { filter: brightness(1.12); }
.tl-label { fill: var(--ink-3); font-size: 11px; font-variant-numeric: tabular-nums; }
.tl-now { stroke: var(--ink); stroke-width: 1.5; }
.tl-hit { fill: transparent; cursor: default; }

/* Calendar */
.cal-head { display: flex; align-items: center; gap: 10px; }
.cal-head h2 { font-size: 1.3rem; min-width: 10ch; text-align: center; flex: 1; }
.cal-head #cal-today { margin-left: 4px; }
.cal-summary { margin: 6px 0 12px; text-align: center; font-size: .92rem; }
.cal-grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 4px; }
#cal-weekdays { margin-bottom: 4px; }
#cal-weekdays div { text-align: center; font-size: .75rem; color: var(--ink-3); font-weight: 600; text-transform: uppercase; letter-spacing: .05em; }
.cal-cell {
  position: relative; display: flex; flex-direction: column; justify-content: space-between;
  aspect-ratio: 4 / 3; min-height: 52px; padding: 5px 5px 4px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--heat-0); cursor: pointer; text-align: left; color: var(--ink);
  overflow: hidden;
}
.cal-cell.out { opacity: .38; }
.cal-cell.future { cursor: default; background: transparent; border-style: dashed; opacity: .55; }
.cal-cell[data-heat="1"] { background: var(--heat-1); }
.cal-cell[data-heat="2"] { background: var(--heat-2); }
.cal-cell[data-heat="3"] { background: var(--heat-3); }
.cal-cell[data-heat="4"] { background: var(--heat-4); color: var(--heat-ink-strong); }
.cal-cell[data-heat="5"] { background: var(--heat-5); color: var(--heat-ink-strong); }
.cal-cell.today { border: 2px solid var(--ink); }
.cal-cell.selected { outline: 3px solid var(--focus); outline-offset: 1px; }
.cal-day { font-size: .78rem; font-weight: 650; line-height: 1; }
.cal-hours { font-size: clamp(.72rem, 2.6vw, .95rem); font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.1; }
.cal-bottom { display: block; }
.cal-strip { display: block; width: 100%; height: 8px; border-radius: 2px; margin-top: 4px; }
.cal-strip .bg { fill: rgb(127 127 127 / 18%); }
.cal-strip .seg { fill: currentColor; }
.cal-cell[data-heat="0"] .cal-strip .seg,
.cal-cell[data-heat="1"] .cal-strip .seg,
.cal-cell[data-heat="2"] .cal-strip .seg,
.cal-cell[data-heat="3"] .cal-strip .seg { fill: var(--ink); }
.cal-legend { display: flex; justify-content: flex-end; align-items: center; gap: 6px; margin: 10px 2px 0; font-size: .75rem; color: var(--ink-3); flex-wrap: wrap; }
.cal-legend .sw { display: inline-flex; flex-direction: column; align-items: center; gap: 2px; }
.cal-legend .sw i { width: 22px; height: 12px; border-radius: 3px; border: 1px solid var(--line); display: block; }

.day-card { margin-top: 18px; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; margin: 12px 0 16px; }
.stat { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; }
.stat .k { font-size: .75rem; color: var(--ink-3); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.stat .v { font-size: 1.3rem; font-weight: 650; font-variant-numeric: tabular-nums; margin-top: 2px; }
.stat .s { font-size: .8rem; color: var(--ink-2); }

.session-list { list-style: none; padding: 0; margin: 14px 0 0; }
.session-list li { display: flex; align-items: center; gap: 10px; padding: 9px 2px; border-top: 1px solid var(--line); font-variant-numeric: tabular-nums; }
.session-list li.gap { border-top: 0; padding: 0 2px 2px 22px; font-size: .8rem; color: var(--ink-3); }
.session-list li.gap.tight { color: var(--warn); font-weight: 600; }
.session-list .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--accent); flex: none; }
.session-list .dot.partial { background: transparent; border: 2px solid var(--accent); }
.session-list .when { font-weight: 600; }
.session-list .meta { color: var(--ink-2); font-size: .9rem; }
.session-list .del { margin-left: auto; border: 0; background: none; color: var(--ink-3); cursor: pointer; padding: 4px 8px; border-radius: 6px; font-size: .85rem; }
.session-list .del:hover { background: var(--surface-2); color: var(--danger); }
.empty { color: var(--ink-3); font-size: .92rem; margin: 6px 0; }

/* Tooltip, toast, dialogs */
.tooltip {
  position: fixed; z-index: 50; pointer-events: none; background: var(--ink); color: var(--bg);
  font-size: .82rem; padding: 6px 9px; border-radius: 8px; white-space: nowrap; font-variant-numeric: tabular-nums;
  transform: translate(-50%, calc(-100% - 10px));
}
.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 999px; z-index: 40;
  font-size: .95rem; box-shadow: var(--shadow); max-width: calc(100vw - 32px); text-align: center;
}
.dialog {
  border: 1px solid var(--line); border-radius: 18px; background: var(--surface); color: var(--ink);
  padding: 24px; width: min(92vw, 380px); box-shadow: var(--shadow);
}
.dialog::backdrop { background: rgb(0 0 0 / 45%); }
.dialog p { color: var(--ink-2); }
.dialog-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; margin-top: 18px; }
.alarm { text-align: center; }
.alarm .dialog-actions { justify-content: center; }
.alarm h2 { font-size: 1.6rem; }
.alarm-icon {
  width: 64px; height: 64px; margin: 0 auto 12px; border-radius: 50%; background: var(--accent);
  animation: pulse 1s ease-in-out infinite;
}
@keyframes pulse { 50% { transform: scale(1.12); opacity: .75; } }
@media (prefers-reduced-motion: reduce) {
  .alarm-icon { animation: none; }
  .ring-fill { transition: none; }
}

@media (max-width: 480px) {
  .brand span { display: none; }
  .tabs { margin-left: 0; margin-right: auto; }
  .cal-cell { padding: 4px 3px 3px; border-radius: 8px; min-height: 0; }
  .cal-grid { gap: 3px; }
  .cal-cell { aspect-ratio: 1 / 1.1; }
  .cal-strip { height: 6px; margin-top: 2px; }
  .dialog-actions .btn { flex: 1; }
}
