:root {
  --bg: oklch(0.30 0.055 215);
  --bg-deep: oklch(0.22 0.05 218);
  --ink: #ffffff;
  --ink-dim: oklch(0.78 0.02 215);
  --ink-faint: oklch(0.62 0.03 215);
  --accent: oklch(0.85 0.15 195);
  --accent-soft: oklch(0.85 0.15 195 / 0.18);
  --hit: rgba(255,255,255,0.06);
  --line: rgba(255,255,255,0.10);
  --reach: env(safe-area-inset-top);
  --reabottom: env(safe-area-inset-bottom);
  --reaside: env(safe-area-inset-left);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro Text", system-ui, sans-serif;
  font-feature-settings: "tnum" 1, "ss01" 1;
  overscroll-behavior: none;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}
body {
  /* Subtle pool gradient — calm, not slop */
  background:
    radial-gradient(120% 80% at 50% -10%, oklch(0.36 0.06 210) 0%, transparent 60%),
    var(--bg);
}

/* ======= Layout ======= */
#app {
  position: fixed; inset: 0;
  display: grid;
  grid-template-rows: auto auto auto auto auto 1fr auto;
  padding-top: max(env(safe-area-inset-top), 14px);
  padding-bottom: max(env(safe-area-inset-bottom), 16px);
  padding-left: calc(env(safe-area-inset-left) + 24px);
  padding-right: calc(env(safe-area-inset-right) + 24px);
}

/* ======= Top bar ======= */
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 44px;
  flex-wrap: nowrap;
  min-width: 0;
}
.timer {
  font-variant-numeric: tabular-nums;
  font-size: 17px;
  font-weight: 500;
  color: var(--ink-dim);
  letter-spacing: 0.02em;
  min-width: 88px;
}
.timer.hidden { visibility: hidden; }
.spacer { flex: 1; }
.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 40px;
  padding: 0 14px;
  border-radius: 12px;
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--line);
  color: var(--ink-dim);
  font-size: 13px;
  font-weight: 500;
  letter-spacing: 0.02em;
  flex-shrink: 0;
}
/* Narrow viewports: tighten gaps but keep labels */
@media (max-width: 480px) {
  .topbar { gap: 6px; }
  .pill { padding: 0 8px; font-size: 11px; }
  .corner-btn.reset { min-width: 60px; padding: 0 10px; }
}
.pill .dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 8px var(--accent);
}
.pill.off .dot { background: var(--ink-faint); box-shadow: none; }
.pill.off { color: var(--ink-faint); }

/* ======= Progress bar ======= */
.progress-wrap {
  position: relative;
  margin-top: 40px;
  height: 10px;
  border-radius: 999px;
  background: rgba(255,255,255,0.10);
}
.progress-wrap.hidden { display: none; }
.progress-bar {
  height: 100%;
  max-width: 100%;
  width: 0%;
  background: var(--accent);
  border-radius: 999px;
  transition: width 280ms cubic-bezier(.2,.7,.2,1);
}
.progress-meta {
  margin-top: 6px;
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--ink-faint);
  font-variant-numeric: tabular-nums;
}
.progress-meta.hidden { display: none; }

/* ======= Count display (passive) ======= */
.count-display {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding: 8px 0 0;
  /* Big numeral with line-height: 0.85 overflows its box; without
     this, the overflowing glyph swallows taps on the +Lap button. */
  pointer-events: none;
}

/* ======= Big increment button ======= */
.add-btn {
  appearance: none;
  border: 0;
  width: clamp(220px, 32vw, 360px);
  margin: 16px auto 0;
  padding: 0;
  height: clamp(80px, 12vh, 120px);
  border-radius: 22px;
  background: var(--accent);
  color: oklch(0.20 0.05 218);
  font-family: inherit;
  font-weight: 700;
  font-size: 28px;
  letter-spacing: 0.04em;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  box-shadow:
    0 1px 0 rgba(255,255,255,0.25) inset,
    0 -2px 0 rgba(0,0,0,0.10) inset,
    0 14px 40px rgba(0,0,0,0.30);
  transition: transform 90ms cubic-bezier(.2,.7,.2,1), filter 120ms ease, box-shadow 120ms ease;
  touch-action: manipulation;
  -webkit-touch-callout: none;
}
.add-btn:active {
  transform: scale(0.985);
  filter: brightness(0.94);
  box-shadow:
    0 1px 0 rgba(255,255,255,0.20) inset,
    0 -2px 0 rgba(0,0,0,0.10) inset,
    0 4px 14px rgba(0,0,0,0.30);
}
.add-btn .plus {
  font-size: 36px;
  font-weight: 300;
  line-height: 0.6;
  margin-top: -4px;
}
.add-btn .label-add {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: 0.18em;
  text-transform: uppercase;
}
.label {
  font-size: 14px;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin-bottom: 8px;
  font-weight: 600;
}
.count {
  font-size: clamp(140px, 32vh, 280px);
  line-height: 0.85;
  font-weight: 700;
  letter-spacing: -0.04em;
  font-variant-numeric: tabular-nums;
  transition: transform 120ms cubic-bezier(.2,.7,.2,1);
  will-change: transform;
}
.count.bump { transform: scale(1.04); }
.goal-line {
  margin-top: 14px;
  font-size: 15px;
  color: var(--ink-dim);
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
}
.goal-line.hidden { display: none; }
.hint { display: none; }

/* ======= Bottom bar ======= */
.bottombar {
  display: grid;
  grid-template-columns: 1fr;
  align-items: end;
  gap: 8px;
  min-height: 56px;
}
.corner-btn {
  appearance: none;
  border: 1px solid var(--line);
  background: rgba(255,255,255,0.04);
  color: var(--ink-dim);
  height: 48px;
  border-radius: 14px;
  font-size: 14px;
  font-weight: 500;
  letter-spacing: 0.02em;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 0 12px;
  cursor: pointer;
  transition: background 120ms ease, transform 80ms ease, color 120ms ease;
}
.corner-btn:active { transform: scale(0.97); background: rgba(255,255,255,0.10); }
.corner-btn .glyph {
  font-size: 20px;
  font-weight: 400;
  line-height: 1;
  display: inline-block;
}
.corner-btn.minus {
  width: 40px;
  min-width: 40px;
  height: 40px;
  padding: 0;
  border-radius: 12px;
  margin-right: 4px;
}
.corner-btn.minus .glyph { font-size: 22px; line-height: 1; }
.corner-btn.center { justify-self: start; min-width: 96px; }
.corner-btn.reset {
  position: relative;
  overflow: hidden;
  height: 40px;
  min-width: 72px;
  border-radius: 12px;
  padding: 0 14px;
}
.corner-btn.reset .fill {
  position: absolute; inset: 0;
  background: oklch(0.62 0.18 25);
  width: 0%;
  transition: width 80ms linear;
}
.corner-btn.reset span { position: relative; z-index: 1; }

/* ======= Splits panel ======= */
.panel {
  position: absolute;
  left: max(env(safe-area-inset-left), 12px);
  right: max(env(safe-area-inset-right), 12px);
  bottom: calc(56px + max(env(safe-area-inset-bottom), 16px) + 12px);
  background: var(--bg-deep);
  border: 1px solid var(--line);
  border-radius: 18px;
  max-height: 50vh;
  display: flex;
  flex-direction: column;
  transform: translateY(120vh);
  transition: transform 280ms cubic-bezier(.2,.7,.2,1);
  box-shadow: 0 12px 40px rgba(0,0,0,0.35);
  z-index: 10;
  visibility: hidden;
}
.panel.open { transform: translateY(0); visibility: visible; }
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px 10px;
  border-bottom: 1px solid var(--line);
}
.panel-head h3 { margin: 0; font-size: 15px; font-weight: 600; letter-spacing: 0.02em; }
.panel-head button {
  appearance: none; border: 0; background: transparent; color: var(--ink-dim);
  font-size: 14px; cursor: pointer; padding: 4px 8px;
}
.splits-list {
  overflow-y: auto;
  padding: 8px 0;
  -webkit-overflow-scrolling: touch;
}
.split-row {
  display: grid;
  grid-template-columns: 48px 1fr auto;
  align-items: baseline;
  gap: 12px;
  padding: 8px 16px;
  font-variant-numeric: tabular-nums;
}
.split-row + .split-row { border-top: 1px solid rgba(255,255,255,0.05); }
.split-row .n { color: var(--ink-faint); font-size: 13px; }
.split-row .lap { color: var(--ink); font-size: 16px; font-weight: 500; }
.split-row .total { color: var(--ink-faint); font-size: 13px; }
.split-row.fastest .lap { color: var(--accent); }
.splits-empty {
  padding: 24px 16px;
  color: var(--ink-faint);
  font-size: 14px;
  text-align: center;
}
.flex-spacer { /* pushes button to bottom of main area */ }

/* ======= Setup sheet (goal mode) ======= */
.sheet-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.45);
  opacity: 0;
  pointer-events: none;
  transition: opacity 200ms ease;
  z-index: 50;
}
.sheet-backdrop.open { opacity: 1; pointer-events: auto; }
.sheet {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  background: var(--bg-deep);
  border-radius: 22px 22px 0 0;
  padding: 18px max(env(safe-area-inset-right), 18px) max(env(safe-area-inset-bottom), 22px) max(env(safe-area-inset-left), 18px);
  transform: translateY(105%);
  transition: transform 280ms cubic-bezier(.2,.7,.2,1);
  z-index: 60;
  border-top: 1px solid var(--line);
}
.sheet.open { transform: translateY(0); }
.sheet h2 {
  margin: 4px 0 4px;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.01em;
}
.sheet p { margin: 0 0 16px; color: var(--ink-dim); font-size: 14px; }
.quick-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}
.chip {
  appearance: none;
  border: 1px solid var(--line);
  background: rgba(255,255,255,0.04);
  color: var(--ink);
  height: 44px;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
}
.chip.active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}
.custom-row {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 18px;
}
.custom-row label { font-size: 14px; color: var(--ink-dim); }
.custom-row input {
  width: 96px;
  height: 44px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: rgba(255,255,255,0.04);
  color: var(--ink);
  font-size: 18px;
  font-weight: 600;
  text-align: center;
  font-variant-numeric: tabular-nums;
  -moz-appearance: textfield;
}
.custom-row input::-webkit-outer-spin-button,
.custom-row input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 0;
  border-top: 1px solid var(--line);
}
.toggle-row .lbl { font-size: 15px; }
.toggle-row .sub { font-size: 12px; color: var(--ink-faint); margin-top: 2px; }
.switch {
  position: relative;
  width: 50px; height: 30px;
  border-radius: 999px;
  background: rgba(255,255,255,0.15);
  cursor: pointer;
  transition: background 160ms ease;
  flex-shrink: 0;
}
.switch::after {
  content: ""; position: absolute;
  top: 3px; left: 3px;
  width: 24px; height: 24px;
  border-radius: 50%;
  background: #fff;
  transition: transform 160ms cubic-bezier(.2,.7,.2,1);
}
.switch.on { background: var(--accent); }
.switch.on::after { transform: translateX(20px); }
.sheet-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 18px;
}
.btn {
  appearance: none;
  border: 0;
  height: 52px;
  border-radius: 14px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}
.btn.primary { background: var(--accent); color: oklch(0.20 0.05 218); }
.btn.secondary { background: rgba(255,255,255,0.08); color: var(--ink); }

/* ======= Confirm dialog ======= */
.confirm {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.55);
  display: flex; align-items: center; justify-content: center;
  z-index: 70;
  opacity: 0; pointer-events: none;
  transition: opacity 180ms ease;
  padding: 24px;
}
.confirm.open { opacity: 1; pointer-events: auto; }
.confirm-card {
  background: var(--bg-deep);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 22px;
  max-width: 340px; width: 100%;
  text-align: center;
  transform: scale(0.96);
  transition: transform 180ms cubic-bezier(.2,.7,.2,1);
}
.confirm.open .confirm-card { transform: scale(1); }
.confirm-card h3 { margin: 0 0 6px; font-size: 18px; }
.confirm-card p { margin: 0 0 16px; color: var(--ink-dim); font-size: 14px; }
.confirm-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.btn.danger { background: oklch(0.62 0.18 25); color: #fff; }

/* ======= Goal-met flash ======= */
.flash {
  position: fixed; inset: 0;
  background: oklch(0.78 0.18 150);
  opacity: 0;
  pointer-events: none;
  z-index: 5;
  transition: opacity 220ms ease;
}
.flash.on { opacity: 0.85; }

/* ======= Goal-met sticky banner ======= */
.goal-banner {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%) scale(0.92);
  background: oklch(0.78 0.18 150);
  color: oklch(0.20 0.08 150);
  font-weight: 700;
  font-size: 13px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  padding: 8px 14px;
  border-radius: 999px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 220ms ease, transform 220ms ease;
  z-index: 6;
  white-space: nowrap;
}
.goal-banner.on { opacity: 1; transform: translate(-50%, -50%) scale(1); }

/* Desktop niceties */
@media (min-width: 720px) {
  .count { font-size: clamp(220px, 50vh, 480px); }
  .hint { font-size: 13px; }
}
