/* Surfboard — iOS inset-grouped list styling, light + dark. */

:root {
  color-scheme: light;
  --accent: #1573ce;
  --bg: #f2f2f7;
  --group-bg: #ffffff;
  --label: #000000;
  --label-secondary: rgba(60, 60, 67, 0.6);
  --label-tertiary: rgba(60, 60, 67, 0.3);
  --separator: rgba(60, 60, 67, 0.18);
  --destructive: #d70015;
  --success: #34c759;
  --accent-soft: rgba(21, 115, 206, 0.1);
  --tide-top: #4aa8f0;
  --tide-bottom: #1252a0;
  --fill: rgba(120, 120, 128, 0.12);
  --sheet-bg: #f2f2f7;
  /* Rows inside the sheet sit one step above the sheet's own background. */
  --field-bg: #ffffff;
  --row-press: rgba(0, 0, 0, 0.05);
  --radius: 12px;

  /* Tide window height, and how far the water drops at low tide. The travel
     is deliberately well short of the full height so an untouched Today
     still shows a real band of water rather than a sliver of wave crest. */
  --tide-height: 164px;
  --tide-travel: 88px;
  /* Water carried below the screen edge. iOS can momentarily lift a fixed
     layer — rubber-band scrolling is the usual cause — and without this the
     page background shows through underneath the tide. Since `.tide` clips to
     its own bounds, this value *is* the tolerance: a lift larger than this
     still exposes the page. It costs nothing visually, being off-screen. */
  --tide-overshoot: 160px;

  /* Type scale — one notch above the iOS defaults. Every font-size below
     references these, so tuning happens here. */
  --text-body: 18px;
  --text-header: 25px;
  --text-secondary: 16px;
  --text-caption: 13px;
  --text-label: 14px;
  --text-segment: 15px;
}

/*
 * Dark palette, declared twice on purpose: once for the system preference and
 * once for an explicit override. CSS can't share one declaration block between
 * a media query and a plain selector, and keeping the media query means dark
 * mode is correct from the first paint, before any script runs.
 *
 * The :not() is what lets "Light" win over a device set to dark.
 */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --accent: #4a9eea;
    --bg: #000000;
    --group-bg: #1c1c1e;
    --label: #ffffff;
    --label-secondary: rgba(235, 235, 245, 0.6);
    --label-tertiary: rgba(235, 235, 245, 0.3);
    --separator: rgba(84, 84, 88, 0.65);
    --destructive: #ff453a;
    --success: #30d158;
    --accent-soft: rgba(74, 158, 234, 0.16);
    --tide-top: #2f7fc4;
    --tide-bottom: #0b3a72;
    --fill: rgba(120, 120, 128, 0.24);
    --sheet-bg: #1c1c1e;
    --field-bg: #2c2c2e;
    --row-press: rgba(255, 255, 255, 0.06);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --accent: #4a9eea;
  --bg: #000000;
  --group-bg: #1c1c1e;
  --label: #ffffff;
  --label-secondary: rgba(235, 235, 245, 0.6);
  --label-tertiary: rgba(235, 235, 245, 0.3);
  --separator: rgba(84, 84, 88, 0.65);
  --destructive: #ff453a;
  --success: #30d158;
  --accent-soft: rgba(74, 158, 234, 0.16);
  --tide-top: #2f7fc4;
  --tide-bottom: #0b3a72;
  --fill: rgba(120, 120, 128, 0.24);
  --sheet-bg: #1c1c1e;
  --field-bg: #2c2c2e;
  --row-press: rgba(255, 255, 255, 0.06);
}

* { box-sizing: border-box; }

/* The sheet sets `display: flex`, which would otherwise beat the UA's
   `[hidden] { display: none }` on specificity. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--label);
  font: var(--text-body)/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  /* Kill the rubber-band overscroll glow behind the standalone web app. */
  overscroll-behavior-y: none;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  padding: 0 0 calc(var(--tide-height) + env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
}

button, input, textarea {
  font: inherit;
  color: inherit;
}

/* ---------- Navigation bar ---------- */

.nav-bar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(env(safe-area-inset-top) + 10px) 20px 10px;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* Emoji ignores `color`; sizing it by font-size keeps it optically level with
   the weekday next to it. */
.nav-mark {
  flex: 0 0 auto;
  font-size: 27px;
  line-height: 1;
}

/* Weekday leads; the date trails in secondary so the pair reads as one line
   without needing punctuation between them. */
.nav-title {
  margin: 0;
  min-width: 0;
  display: flex;
  align-items: baseline;
  gap: 8px;
  font-weight: 400;
}

.nav-title .weekday {
  font-size: var(--text-header);
  font-weight: 700;
  letter-spacing: -0.3px;
}

.nav-title .daydate {
  font-size: var(--text-body);
  color: var(--label-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.nav-button {
  margin-left: auto;
  flex: 0 0 auto;
  width: 34px;
  height: 34px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: none;
  color: var(--accent);
  display: grid;
  place-items: center;
}

/* ---------- Sections ---------- */

main {
  position: relative;
  z-index: 1;
  padding: 0 16px;
}

.section { margin-top: 26px; }

.section-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 4px 8px;
}

.section-header .title {
  font-size: var(--text-body);
  font-weight: 600;
}

.section-header .count {
  margin-left: auto;
  font-size: var(--text-secondary);
  color: var(--label-secondary);
  font-variant-numeric: tabular-nums;
}

.section-header .ring { flex: 0 0 auto; }

.card {
  background: var(--group-bg);
  border-radius: var(--radius);
  overflow: hidden;
}

/* ---------- Progress ring ---------- */

.ring circle { transition: stroke-dashoffset 0.25s ease-in-out; }
.ring .track { stroke: var(--label-tertiary); }
.ring .fill { stroke: var(--accent); }
.ring .center {
  fill: var(--accent);
  transition: opacity 0.25s ease-in-out;
}

/* ---------- Rows ---------- */

/* Left transparent on purpose: .row-delete already fills exactly the strip
   the row uncovers, and a red background here bleeds through the card's
   rounded corners. */
.row-wrap {
  position: relative;
  overflow: hidden;
}

.row-complete {
  position: absolute;
  inset: 0 auto 0 0;
  width: 84px;
  display: grid;
  place-items: center;
  background: var(--success);
  color: #fff;
}

.row-complete svg { transition: transform 0.18s cubic-bezier(0.34, 1.4, 0.64, 1); }

/* Past the trigger point the checkmark pops, so you can feel the threshold
   without having to watch how far you've dragged. */
.row-wrap.arming .row-complete svg { transform: scale(1.3); }

.row-delete {
  position: absolute;
  inset: 0 0 0 auto;
  width: 84px;
  border: 0;
  background: var(--destructive);
  color: #fff;
  font-size: var(--text-secondary);
  font-weight: 500;
}

.row {
  position: relative;
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 11px 16px;
  background: var(--group-bg);
  transform: translateX(0);
  transition: transform 0.22s cubic-bezier(0.22, 0.61, 0.36, 1);
  touch-action: pan-y;
}

.row.dragging { transition: none; }
.row.open { transform: translateX(-84px); }

.row-wrap + .row-wrap .row::before,
.row-wrap + .add-row::before,
.empty + .add-row::before {
  content: "";
  position: absolute;
  left: 52px;
  right: 0;
  top: 0;
  border-top: 0.5px solid var(--separator);
}

.row .body {
  flex: 1;
  min-width: 0;
  /* Nudge the text down so its cap-height lines up with the toggle circle. */
  padding-top: 1px;
}

.row .task-title {
  overflow-wrap: anywhere;
}

.row.completed .task-title {
  text-decoration: line-through;
  color: var(--label-secondary);
}

.row .subtitle {
  margin-top: 2px;
  font-size: var(--text-caption);
  color: var(--label-secondary);
}

.row.overdue:not(.completed) .subtitle { color: var(--destructive); }

/* ---------- Toggle circle ---------- */

.toggle {
  flex: 0 0 auto;
  width: 24px;
  height: 24px;
  padding: 0;
  border: 0;
  background: none;
  color: var(--accent);
  display: grid;
  place-items: center;
}

.toggle .disc {
  width: 21px;
  height: 21px;
  border: 1.7px solid currentColor;
  border-radius: 50%;
  display: grid;
  place-items: center;
}

.toggle .disc::after {
  content: "";
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: currentColor;
  transform: scale(0);
  transition: transform 0.18s cubic-bezier(0.34, 1.4, 0.64, 1);
}

.row.completed .toggle { color: var(--label-secondary); }
.row.completed .toggle .disc::after { transform: scale(1); }

/* ---------- Add row ---------- */

.add-row {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 16px;
  background: var(--group-bg);
}

.add-row .plus {
  flex: 0 0 auto;
  width: 24px;
  height: 24px;
  display: grid;
  place-items: center;
  color: var(--accent);
}

.add-row input {
  flex: 1;
  min-width: 0;
  border: 0;
  background: none;
  outline: none;
  padding: 0;
}

.add-row input::placeholder { color: var(--label-tertiary); }

.empty {
  padding: 12px 16px;
  color: var(--label-tertiary);
  font-size: var(--text-secondary);
}

/* ---------- Edit sheet ---------- */

.sheet-scrim {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(0, 0, 0, 0.4);
  animation: fade-in 0.25s ease;
}

.sheet {
  position: fixed;
  z-index: 21;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: 92dvh;
  display: flex;
  flex-direction: column;
  background: var(--sheet-bg);
  border-radius: 14px 14px 0 0;
  padding-top: 8px;
  animation: slide-up 0.3s cubic-bezier(0.22, 0.61, 0.36, 1);
}

@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

@keyframes slide-up {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}

/* The centered variant has to carry the translateX itself, since `transform`
   is a single property and the animation would otherwise drop the centering. */
@keyframes slide-up-centered {
  from { transform: translate(-50%, 24px); opacity: 0; }
  to   { transform: translate(-50%, 0); opacity: 1; }
}

/* On a wide screen the sheet becomes a centered floating card. */
@media (min-width: 600px) {
  .sheet {
    left: 50%;
    bottom: 24px;
    transform: translateX(-50%);
    width: 520px;
    border-radius: 14px;
    animation-name: slide-up-centered;
  }
}

.sheet-bar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 6px 12px 12px;
}

.sheet-heading { font-size: var(--text-body); font-weight: 600; }

.sheet-action {
  border: 0;
  background: none;
  color: var(--accent);
  font-size: var(--text-body);
  padding: 4px 6px;
  justify-self: start;
}

.sheet-action.strong { font-weight: 600; justify-self: end; }
.sheet-action:disabled { color: var(--label-tertiary); }

.sheet-body {
  overflow-y: auto;
  padding: 0 16px max(20px, env(safe-area-inset-bottom));
  -webkit-overflow-scrolling: touch;
}

.field-label {
  padding: 22px 16px 7px;
  font-size: var(--text-label);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--label-secondary);
}

/* No preceding group to separate from at the top of a sheet. */
.field-label.first { padding-top: 6px; }

.field-group {
  background: var(--field-bg);
  border-radius: var(--radius);
  padding: 11px 16px;
}

.field {
  display: block;
  width: 100%;
  border: 0;
  padding: 0;
  background: none;
  outline: none;
  resize: none;
  overflow: hidden;
  line-height: 1.35;
}

.field::placeholder { color: var(--label-tertiary); }

.segmented {
  display: flex;
  gap: 2px;
  background: var(--fill);
  border-radius: 9px;
  padding: 2px;
}

.segmented button {
  flex: 1;
  border: 0;
  border-radius: 7px;
  background: none;
  padding: 6px 4px;
  font-size: var(--text-segment);
  color: var(--label);
}

.segmented button[aria-checked="true"] {
  background: var(--field-bg);
  font-weight: 600;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
}

/* ---------- Tide ---------- */

/*
 * Level is driven by --tide-fill (0–1, set from JS to Today's completion).
 * The container is a fixed-height window and the waves slide up inside it,
 * so changing the level is a transform rather than a layout change.
 */
.tide {
  position: fixed;
  left: 0;
  right: 0;
  /* Dropped below the viewport and made taller by the same amount, so the
     top edge lands exactly where a plain `bottom: 0` would put it. */
  bottom: calc(-1 * var(--tide-overshoot));
  height: calc(var(--tide-height) + var(--tide-overshoot));
  z-index: 0;
  overflow: hidden;
  pointer-events: none;
  /* Its own compositing layer, so iOS is less likely to repaint it late. */
  will-change: transform;
}

.tide-body {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  /* Pinned to the wave height rather than the (now taller) window, so the
     overshoot doesn't stretch the wave shapes. */
  height: var(--tide-height);
  /* At fill 0 only a sliver of water shows; at 1 the window is full. */
  transform: translateY(calc((1 - var(--tide-fill, 0)) * var(--tide-travel)));
  transition: transform 0.8s cubic-bezier(0.33, 1, 0.68, 1);
}

.tide svg {
  position: absolute;
  bottom: 0;
  left: 0;
  /* Two periods across twice the viewport, so one period is exactly 50%. */
  width: 200%;
  height: 100%;
}

/*
 * Deep water continuing below the wave block. The three wave layers are
 * semi-transparent, so this matches their composited alpha rather than using
 * a solid fill — 1-(1-.20)(1-.28)(1-.42) normally, and again for the brighter
 * `full` state — otherwise the seam would show as a darker band.
 */
.tide-body::after {
  content: "";
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  height: calc(var(--tide-overshoot) + var(--tide-travel));
  background: color-mix(in srgb, var(--tide-bottom) 66.6%, transparent);
  transition: background 0.8s ease;
}

.tide.full .tide-body::after {
  background: color-mix(in srgb, var(--tide-bottom) 83.8%, transparent);
}

.tide-stop-top { stop-color: var(--tide-top); }
.tide-stop-bottom { stop-color: var(--tide-bottom); }

.tide .wave {
  fill: url(#tide-fill);
  animation: drift linear infinite;
}

@keyframes drift {
  from { transform: translateX(0); }
  to   { transform: translateX(-120px); }
}

/* Different speeds keep the layers from ever locking into one shape. */
.tide .wave-back  { opacity: 0.20; animation-duration: 15s; }
.tide .wave-mid   { opacity: 0.28; animation-duration: 11s; animation-direction: reverse; }
.tide .wave-front { opacity: 0.42; animation-duration: 8s; }

/* Clearing Today lifts the whole thing a touch brighter. */
.tide.full .wave-back  { opacity: 0.30; }
.tide.full .wave-mid   { opacity: 0.42; }
.tide.full .wave-front { opacity: 0.60; }
.tide .wave { transition: opacity 0.8s ease; }

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

.notice {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 20px;
  padding: 12px 14px;
  border-radius: var(--radius);
  background: var(--accent-soft);
  color: var(--label);
  font-size: var(--text-caption);
  animation: notice-in 0.35s cubic-bezier(0.22, 0.61, 0.36, 1);
}

@keyframes notice-in {
  from { opacity: 0; transform: translateY(-6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.notice-glyph { flex: 0 0 auto; color: var(--accent); }
.notice-text { flex: 1; min-width: 0; }

.notice-dismiss {
  flex: 0 0 auto;
  border: 0;
  background: none;
  color: var(--label-secondary);
  font-size: var(--text-caption);
  padding: 4px 2px;
  line-height: 1;
}

/* ---------- Backup sheet ---------- */

.sheet-note {
  margin: 0;
  padding: 4px 16px;
  font-size: var(--text-caption);
  color: var(--label-secondary);
}

.sheet-note:empty { display: none; }
.sheet-note.warn { padding-top: 14px; }
.sheet-note.error { color: var(--destructive); }

.action-group {
  background: var(--field-bg);
  border-radius: var(--radius);
  overflow: hidden;
}

.action-row {
  display: flex;
  align-items: center;
  width: 100%;
  border: 0;
  background: none;
  color: var(--accent);
  font-size: var(--text-body);
  text-align: left;
  padding: 13px 16px;
}

.action-row + .action-row { border-top: 0.5px solid var(--separator); }

/* ---------- Switch row ---------- */

.switch-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 16px;
  background: var(--field-bg);
}

.switch-label { flex: 1; min-width: 0; }
.switch-row:has(.switch:disabled) .switch-label { color: var(--label-secondary); }

.switch {
  appearance: none;
  -webkit-appearance: none;
  flex: 0 0 auto;
  position: relative;
  width: 51px;
  height: 31px;
  margin: 0;
  border: 0;
  border-radius: 16px;
  background: var(--fill);
  transition: background 0.2s ease;
}

.switch::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 27px;
  height: 27px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.28);
  transition: transform 0.2s cubic-bezier(0.34, 1.3, 0.64, 1);
}

.switch:checked { background: var(--success); }
.switch:checked::after { transform: translateX(20px); }
.switch:disabled { opacity: 0.45; }
.action-row:disabled { color: var(--label-tertiary); }

.action-row .hint {
  margin-left: auto;
  padding-left: 12px;
  font-size: var(--text-caption);
  color: var(--label-secondary);
}

.destructive-row {
  width: 100%;
  margin-top: 22px;
  border: 0;
  border-radius: var(--radius);
  background: var(--field-bg);
  color: var(--destructive);
  padding: 12px 16px;
  font-size: var(--text-body);
}

/* ---------- Pointer affordances (skipped on touch) ---------- */

@media (hover: hover) {
  .row:active, .destructive-row:active { background: var(--row-press); }
  .toggle, .sheet-action, .row-delete, .segmented button { cursor: pointer; }
  .row .body { cursor: text; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::after {
    animation-duration: 0.01ms !important;
    /* Without this the tide's infinite drift keeps restarting rather than
       stopping — a near-zero duration alone doesn't end a looping animation. */
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
