/* ---------------------------------------------------------------
   Apnea — breath-hold training
   Dark-only by design: the app is used in dim rooms and poolside.
   --------------------------------------------------------------- */

:root {
  color-scheme: dark;

  --bg: #0e1216;
  --surface-1: #161b21;   /* cards, chart surface */
  --surface-2: #1e242c;
  --surface-3: #283039;
  --line: #2c343d;

  --text-primary: #f1f4f7;
  --text-secondary: #a8b3bf;
  --text-muted: #717d89;

  /* categorical slots, dark steps (see dataviz reference palette) */
  --series-1: #3987e5;  /* blue   */
  --series-2: #d95926;  /* orange */
  --series-3: #199e70;  /* aqua   */
  --series-4: #c98500;  /* yellow */

  --accent: var(--series-1);
  --accent-soft: rgba(57, 135, 229, .16);
  --hold: var(--series-3);
  --hold-soft: rgba(25, 158, 112, .16);
  --relax: #9b7ce8;                       /* breathing work, off the series ramp */
  --relax-soft: rgba(155, 124, 232, .16);
  --warn: #c98500;
  --danger: #e66767;

  --radius: 16px;
  --radius-sm: 10px;
  --gap: 14px;

  --shadow: 0 1px 0 rgba(255, 255, 255, .03) inset, 0 8px 24px rgba(0, 0, 0, .35);

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
}

body {
  background: var(--bg);
  color: var(--text-primary);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}

.app-shell {
  max-width: 560px;
  margin: 0 auto;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.view {
  flex: 1;
  padding: max(18px, env(safe-area-inset-top)) 16px 96px;
  outline: none;
  min-width: 0;
}

/* the runner lays out its own full-height column, insets included */
.view.is-running { padding: 0; }

/* ---------------------------------------------------------- typography */

h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.0625rem; }
h3 { font-size: 0.9375rem; }
p { margin: 0; }

.page-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 18px;
}

.eyebrow {
  font-size: 0.6875rem;
  text-transform: uppercase;
  letter-spacing: 0.09em;
  color: var(--text-muted);
  font-weight: 600;
}

.muted { color: var(--text-secondary); font-size: 0.875rem; }
.tiny { color: var(--text-muted); font-size: 0.75rem; }
.num { font-variant-numeric: tabular-nums; font-feature-settings: "tnum"; }

/* -------------------------------------------------------------- cards */

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

.card + .card { margin-top: var(--gap); }

.stack { display: flex; flex-direction: column; gap: var(--gap); }
.row { display: flex; align-items: center; gap: 10px; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }

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

button { font: inherit; color: inherit; }

a { color: inherit; }

.btn {
  appearance: none;
  text-decoration: none;
  border: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--text-primary);
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  font-size: 0.9375rem;
  font-weight: 550;
  cursor: pointer;
  transition: background .15s ease, border-color .15s ease, transform .06s ease;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.btn:hover { background: var(--surface-3); }
.btn:active { transform: scale(.985); }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.btn-primary {
  background: var(--accent);
  border-color: transparent;
  color: #fff;
  font-weight: 600;
}
.btn-primary:hover { background: #4d93e8; }

.btn-ghost { background: transparent; }
.btn-ghost:hover { background: var(--surface-2); }

.btn-danger { color: var(--danger); }
.btn-danger:hover { background: rgba(230, 103, 103, .12); }

.btn-danger-solid {
  background: #b23b3b;
  border-color: transparent;
  color: #fff;
  font-weight: 600;
}
.btn-danger-solid:hover { background: #c44747; }

.btn-block { width: 100%; }
.btn-lg { padding: 16px 20px; font-size: 1.0625rem; min-height: 54px; }
.btn-sm { padding: 7px 11px; min-height: 34px; font-size: 0.8125rem; }

/* ---------------------------------------------------------- tab bar */

.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 20;
  display: flex;
  justify-content: center;
  gap: 2px;
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
  background: rgba(14, 18, 22, .88);
  backdrop-filter: blur(14px);
  border-top: 1px solid var(--line);
}

.tabbar[hidden] { display: none; }

.tabbar a {
  flex: 1;
  max-width: 110px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 6px 2px;
  border-radius: 10px;
  text-decoration: none;
  color: var(--text-muted);
  font-size: 0.6875rem;
  font-weight: 550;
}

.tabbar a[aria-current="page"] { color: var(--accent); background: var(--accent-soft); }
.tabbar svg { width: 21px; height: 21px; stroke: currentColor; fill: none; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

/* ------------------------------------------------------------- lists */

.tile {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  text-align: left;
  padding: 15px 16px;
  background: var(--surface-1);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  cursor: pointer;
  text-decoration: none;
  color: inherit;
  transition: border-color .15s ease, background .15s ease;
}
.tile:hover { background: var(--surface-2); border-color: var(--surface-3); }

.tile-icon {
  width: 42px;
  height: 42px;
  flex: 0 0 42px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  background: var(--accent-soft);
  color: var(--accent);
}
.tile-icon svg { width: 22px; height: 22px; stroke: currentColor; fill: none; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.tile-icon.green { background: var(--hold-soft); color: var(--hold); }
.tile-icon.orange { background: rgba(217, 89, 38, .16); color: var(--series-2); }
.tile-icon.yellow { background: rgba(201, 133, 0, .16); color: var(--series-4); }
.tile-icon.purple { background: var(--relax-soft); color: var(--relax); }

.tile-title { font-weight: 600; font-size: 0.9375rem; }
.tile-sub { color: var(--text-secondary); font-size: 0.8125rem; }
.tile-chev { color: var(--text-muted); }

/* -------------------------------------------------------- form bits */

.field { display: flex; flex-direction: column; gap: 8px; }
.field > label, .field-label { font-size: 0.8125rem; color: var(--text-secondary); font-weight: 550; }

.stepper {
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 4px;
}

.stepper button {
  width: 40px;
  height: 40px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 1.25rem;
  border-radius: 8px;
  cursor: pointer;
  flex: 0 0 auto;
}
.stepper button:hover { background: var(--surface-3); color: var(--text-primary); }
.stepper button:disabled { opacity: .3; cursor: not-allowed; }

.stepper .stepper-value {
  flex: 1;
  text-align: center;
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  font-size: 1.0625rem;
  font-weight: 600;
}

.seg {
  display: flex;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 3px;
  gap: 3px;
  /* six filters do not fit a 320px phone: scroll the row rather than let it
     push the page sideways, and keep the scrollbar itself out of the way */
  overflow-x: auto;
  scrollbar-width: none;
}
.seg::-webkit-scrollbar { display: none; }
.seg button {
  flex: 1 0 auto;
  white-space: nowrap;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  padding: 9px 10px;
  border-radius: 8px;
  font-size: 0.8125rem;
  font-weight: 550;
  cursor: pointer;
  white-space: nowrap;
}
.seg button[aria-pressed="true"] { background: var(--surface-3); color: var(--text-primary); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 5px 11px;
  font-size: 0.75rem;
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  color: var(--text-secondary);
}
.chip.accent { border-color: rgba(217, 89, 38, .5); color: #f0a683; background: rgba(217, 89, 38, .12); }

.switch { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 4px 0; }
.switch input { width: 44px; height: 26px; accent-color: var(--accent); }

textarea,
input[type="text"],
input[type="number"],
input[type="datetime-local"] {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  padding: 10px 12px;
  font: inherit;
  font-size: 0.9375rem;
  resize: vertical;
}
textarea:focus, input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* ------------------------------------------------------- run screen */

.runner {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: max(14px, env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom));
  gap: 14px;
}

.runner-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex: 0 0 auto;
}

.runner > .stack { flex: 0 0 auto; }

.phase-badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  font-weight: 650;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-secondary);
}
.phase-badge::before {
  content: "";
  width: 8px; height: 8px;
  border-radius: 50%;
  background: currentColor;
}
.phase-prep    { background: var(--accent-soft); color: #8dbcf1; }
.phase-hold    { background: var(--hold-soft); color: #4fc79b; }
.phase-recover { background: rgba(201, 133, 0, .16); color: #e0ac4a; }

.dial-wrap {
  flex: 1;
  display: grid;
  place-items: center;
  position: relative;
  min-height: 180px;
  padding: 8px 0;
}

/* the vh term keeps the ring from crowding out the buttons on short screens */
.dial { width: min(76vw, 310px, 42vh); aspect-ratio: 1; display: block; overflow: visible; }
.dial-track { fill: none; stroke: var(--surface-2); stroke-width: 6; }
.dial-arc {
  fill: none;
  stroke: var(--accent);
  stroke-width: 6;
  stroke-linecap: round;
  transition: stroke-dashoffset .2s linear, stroke .3s ease;
}
.dial-arc.hold { stroke: var(--hold); }
.dial-arc.recover { stroke: var(--series-4); }
.dial-tick { stroke: var(--series-2); stroke-width: 2.5; stroke-linecap: round; }

.dial-center {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  pointer-events: none;
  text-align: center;
}

.time-big {
  font-family: var(--font);
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
  font-size: clamp(2rem, 8.5vw, 2.5rem);
  font-weight: 300;
  letter-spacing: 0.01em;
  line-height: 1;
}
.time-sub { color: var(--text-secondary); font-size: 0.8125rem; }

.contraction-btn {
  width: 100%;
  border: 1px solid rgba(217, 89, 38, .45);
  background: rgba(217, 89, 38, .14);
  color: #f5b191;
  border-radius: var(--radius);
  padding: 20px;
  font-size: 1.0625rem;
  font-weight: 650;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  cursor: pointer;
  transition: transform .08s ease, background .15s ease;
}
.contraction-btn:active { transform: scale(.98); background: rgba(217, 89, 38, .3); }
.contraction-btn .cb-count { display: block; font-size: 0.75rem; letter-spacing: 0.02em; text-transform: none; font-weight: 500; color: rgba(245, 177, 145, .75); margin-top: 4px; }
.contraction-btn.flash { background: rgba(217, 89, 38, .42); }

/* The stage holds a static reference ring, the animated ring, and a readout
   that stays outside the transform so the digits never change size. */
.breath-stage {
  position: relative;
  width: min(70vw, 280px, 44vh);
  aspect-ratio: 1;
  display: grid;
  place-items: center;
}

.breath-stage::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, .07);
}

.breath-orb {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 1.5px solid rgba(110, 176, 240, .55);
  background: rgba(57, 135, 229, .07);
  transform: scale(var(--orb-scale, .55));
  will-change: transform;
}

.breath-readout {
  position: relative;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.orb-label {
  font-size: 0.75rem;
  font-weight: 550;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--text-secondary);
}
.orb-count {
  font-family: var(--font);
  font-size: 2.25rem;
  font-weight: 300;
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
  line-height: 1;
}

/* ------------------------------------------------------------ stats */

.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(96px, 1fr)); gap: 10px; }

/* zoned by a lift off the card surface rather than a border */
.stat {
  background: var(--surface-2);
  border: none;
  border-radius: var(--radius-sm);
  padding: 13px;
}
.stat-value {
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  font-size: 1.375rem;
  font-weight: 600;
  line-height: 1.15;
}
.stat-label { font-size: 0.6875rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.06em; margin-top: 3px; }

.hero-number {
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  font-size: clamp(2.75rem, 13vw, 3.5rem);
  font-weight: 650;
  line-height: 1;
  letter-spacing: -0.02em;
}

/* ------------------------------------------------------------ table */

.rounds { width: 100%; border-collapse: collapse; font-size: 0.8125rem; }
.rounds th {
  text-align: left;
  font-size: 0.6875rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  font-weight: 600;
  padding: 6px 8px;
  border-bottom: 1px solid var(--line);
}
.rounds td { padding: 7px 8px; border-bottom: 1px solid rgba(44, 52, 61, .5); font-variant-numeric: tabular-nums; }
.rounds tr:last-child td { border-bottom: none; }
.rounds td.n { color: var(--text-muted); width: 1%; }
.rounds tr.done td { color: var(--text-muted); }
.rounds tr.current td { background: var(--accent-soft); color: var(--text-primary); }

/* ------------------------------------------------------------ chart */

.chart-card { position: relative; }
.chart-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; margin-bottom: 4px; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 12px; margin: 6px 0 10px; }
.chart-legend span { display: inline-flex; align-items: center; gap: 6px; font-size: 0.75rem; color: var(--text-secondary); }
.chart-legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }

.chart-svg { display: block; width: 100%; overflow: visible; touch-action: pan-y; }
.chart-grid line { stroke: var(--line); stroke-width: 1; }
.chart-axis text { fill: var(--text-muted); font-size: 10px; font-family: var(--font); }
.chart-line { fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.chart-dot { stroke: var(--surface-1); stroke-width: 2; }
.chart-bar { rx: 4; }
.chart-crosshair { stroke: var(--text-muted); stroke-width: 1; stroke-dasharray: 3 3; }
.chart-hit { fill: transparent; cursor: crosshair; }

.chart-tip {
  position: absolute;
  z-index: 5;
  pointer-events: none;
  background: var(--surface-3);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 0.75rem;
  white-space: nowrap;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .45);
  transform: translate(-50%, -100%);
  opacity: 0;
  transition: opacity .12s ease;
}
.chart-tip.on { opacity: 1; }
.chart-tip b { font-family: var(--mono); font-variant-numeric: tabular-nums; font-weight: 600; }
.chart-tip .tip-row { display: flex; align-items: center; gap: 6px; margin-top: 3px; color: var(--text-secondary); }
.chart-tip .tip-row i { width: 8px; height: 8px; border-radius: 2px; }

details.data-table { margin-top: 10px; }
details.data-table summary { font-size: 0.75rem; color: var(--text-muted); cursor: pointer; }
details.data-table table { margin-top: 8px; }

.empty {
  text-align: center;
  padding: 28px 18px;
  color: var(--text-muted);
  font-size: 0.875rem;
  border: 1px dashed var(--line);
  border-radius: var(--radius);
}

/* ------------------------------------------------------- history */

.day-head {
  font-size: 0.6875rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  font-weight: 650;
  margin: 18px 0 8px;
}

.entry { border-left: 3px solid var(--line); padding-left: 12px; }
.entry.type-hold { border-left-color: var(--series-3); }
.entry.type-co2 { border-left-color: var(--series-2); }
.entry.type-o2 { border-left-color: var(--series-1); }
.entry.type-relax { border-left-color: var(--relax); }
.entry.type-walk { border-left-color: var(--series-3); }
.entry.type-dyn { border-left-color: var(--series-1); }
.entry.type-freedive { border-left-color: var(--series-4); }

/* the header row is the expand control, so it needs the affordances a button
   would have brought with it */
.entry-head { cursor: pointer; -webkit-tap-highlight-color: transparent; }
.entry-head:focus-visible { outline: 2px solid var(--accent); outline-offset: 6px; border-radius: var(--radius-sm); }

.entry-chevron {
  flex: none;
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  transition: transform .16s ease, color .16s ease, background .16s ease;
}

.entry-chevron.open { transform: rotate(90deg); }
.entry-head:hover .entry-chevron { color: var(--text-primary); background: var(--surface-2); }

/* --------------------------------------------------------- modal */

.modal {
  margin: auto;
  border: 1px solid var(--line);
  background: var(--surface-1);
  color: var(--text-primary);
  border-radius: var(--radius);
  padding: 22px;
  width: min(340px, calc(100vw - 48px));
  box-shadow: 0 20px 60px rgba(0, 0, 0, .6);
}

.modal::backdrop { background: rgba(6, 9, 12, .72); }
.modal h3 { font-size: 1.0625rem; margin-bottom: 6px; }
.modal p { color: var(--text-secondary); font-size: 0.875rem; }
.modal-actions { display: flex; gap: 8px; margin-top: 20px; }
.modal-actions .btn { flex: 1; }

/* --------------------------------------------------------- toast */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(88px + env(safe-area-inset-bottom));
  transform: translate(-50%, 12px);
  background: var(--surface-3);
  border: 1px solid var(--line);
  color: var(--text-primary);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 0.8125rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
  z-index: 50;
  box-shadow: var(--shadow);
}
.toast.on { opacity: 1; transform: translate(-50%, 0); }

.safety {
  font-size: 0.75rem;
  color: var(--text-muted);
  line-height: 1.5;
  border-left: 2px solid var(--warn);
  padding-left: 10px;
}

/* --------------------------------------------------- responsive tuning */

/* narrow phones (iPhone SE and down) */
@media (max-width: 380px) {
  .view { padding-left: 12px; padding-right: 12px; }
  .runner { padding-left: 12px; padding-right: 12px; }
  .card { padding: 14px; }
  h1 { font-size: 1.375rem; }
  .stat-grid { grid-template-columns: repeat(auto-fit, minmax(76px, 1fr)); gap: 8px; }
  .stat { padding: 11px; }
  .stat-value { font-size: 1.1875rem; }
  .stat-label { font-size: 0.625rem; letter-spacing: 0.04em; }
  .tabbar a { font-size: 0.625rem; }
  .seg button { padding: 9px 6px; font-size: 0.75rem; }
  .stepper button { width: 36px; height: 36px; }
  .tile { padding: 13px 13px; gap: 11px; }
  .tile-icon { width: 38px; height: 38px; flex-basis: 38px; }
  .chart-head { flex-direction: column; align-items: flex-start; gap: 2px; }
  .chart-head h2 { font-size: 1rem; }
}

/* short screens: give the buttons back the room the ring was taking */
@media (max-height: 700px) {
  .contraction-btn { padding: 15px; font-size: 1rem; }
  .btn-lg { min-height: 48px; padding: 13px 18px; font-size: 1rem; }
  .runner { gap: 10px; }
  .dial-wrap { min-height: 140px; }
}

/* landscape phones: stacking doesn't fit, so the ring and the controls
   sit side by side and everything stays above the fold */
@media (max-height: 560px) and (orientation: landscape) {
  .runner {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    grid-template-areas:
      "head  head"
      "dial  actions"
      "strip strip";
    grid-template-rows: auto 1fr auto;
    gap: 8px 20px;
    padding-top: max(10px, env(safe-area-inset-top));
  }
  .runner-head { grid-area: head; }
  .runner > .dial-wrap { grid-area: dial; min-height: 0; padding: 0; align-self: center; }
  .runner > .stack { grid-area: actions; align-self: center; }
  .runner > .chips { grid-area: strip; }

  .dial { width: min(34vw, 190px, 64vh); }
  .breath-stage { width: min(34vw, 190px, 64vh); }
  .time-big { font-size: 1.625rem; }
  .time-sub { font-size: 0.75rem; }
  .contraction-btn { padding: 13px; font-size: 0.9375rem; }
  .orb-count { font-size: 1.75rem; }
}

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