/* The Pain Game — whiteboard look from the reference sketches. */
:root {
  --bg: #FAFAF7;
  --dot: rgba(0, 0, 0, 0.09);
  --ink: #141414;
  --muted: #6d6d68;
  --line: #e4e4de;
  --card: #ffffff;
  --gray: #DCDCDC;
  --green: #B3E3BC;
  --green-ink: #22804a;
  --orange: #F7C6A8;
  --orange-ink: #c7612a;
  --red: #E4A0A2;
  --red-ink: #c4323c;
  --blend: #CBB27D;
  --blue: #536CF5;
  --pill: #55BE6E;
  --radius: 14px;
  --font: 'Lexend', ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background-color: var(--bg);
  background-image: radial-gradient(var(--dot) 1.2px, transparent 1.4px);
  background-size: 22px 22px;
  color: var(--ink);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ── Frame ─────────────────────────────────────────────────────────────── */
.app {
  position: relative;
  height: 100vh;
  height: 100dvh;
  max-width: 1120px;
  margin: 0 auto;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  gap: 10px;
  padding:
    max(12px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
    max(12px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

.top { display: flex; align-items: center; justify-content: center; position: relative; min-height: 44px; }
.pill {
  background: var(--pill);
  color: #0b0b0b;
  font-weight: 700;
  font-size: clamp(20px, 3.3vw, 34px);
  letter-spacing: 0.01em;
  line-height: 1;
  padding: 0.3em 0.62em;
  border-radius: 0.36em;
  white-space: nowrap;
}
.top-left, .top-right {
  position: absolute; top: 50%; transform: translateY(-50%);
  display: flex; gap: 8px; align-items: center;
}
.top-left { left: 0; }
.top-right { right: 0; }
.chip {
  font-weight: 600; font-size: 12px; letter-spacing: 0.08em;
  padding: 7px 11px; border-radius: 999px;
  background: #fff; border: 1px solid var(--line);
  white-space: nowrap;
}
.icon-btn {
  width: 38px; height: 38px; border-radius: 50%;
  display: inline-grid; place-items: center;
  background: #fff; border: 1px solid var(--line); cursor: pointer; padding: 0;
}
.icon-btn:hover { border-color: #cfcfc8; }
.icon-btn svg { width: 18px; height: 18px; }

/* ── Stage: bar + HUD ──────────────────────────────────────────────────── */
.stage {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 264px;
  gap: 16px;
  min-height: 0;
}
.bar-area { position: relative; min-height: 0; }
.bar-area canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }

.ready-banner {
  position: absolute; left: 50%; top: 30%;
  transform: translateX(-50%);
  background: var(--ink); color: #fff;
  padding: 12px 18px; border-radius: 12px;
  font-weight: 600; font-size: 15px; text-align: center;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.18);
  animation: bob 1.8s ease-in-out infinite;
  pointer-events: none;
  max-width: calc(100% - 24px);
  z-index: 2;
}
.ready-banner small { display: block; font-weight: 400; opacity: 0.75; font-size: 12px; margin-top: 3px; }
@keyframes bob { 50% { transform: translateX(-50%) translateY(-6px); } }

.hud { display: flex; flex-direction: column; gap: 10px; align-self: center; }
.hud-cell {
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 14px;
  min-width: 0;
}
.hud-label {
  display: block;
  font-size: 11px; font-weight: 600; letter-spacing: 0.12em;
  color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.lbl-short { display: none; }
.hud-big {
  display: block; font-size: 38px; font-weight: 700; line-height: 1.08;
  font-variant-numeric: tabular-nums;
}
.hud-mid {
  display: block; font-size: 22px; font-weight: 700; line-height: 1.2;
  font-variant-numeric: tabular-nums;
}
#score.neg { color: var(--red-ink); }
.timebar, .meter {
  height: 5px; border-radius: 3px; background: #efefea; overflow: hidden; margin-top: 6px;
}
.timebar i, .meter i {
  display: block; height: 100%; width: 100%;
  transform-origin: left; transform: scaleX(1);
  background: var(--ink);
}
.meter i { background: var(--orange-ink); transform: scaleX(0); }
#cellTime.urgent .hud-big { color: var(--red-ink); animation: pulse 1s ease-in-out infinite; }
#cellTime.urgent .timebar i { background: var(--red-ink); }
@keyframes pulse { 50% { opacity: 0.55; } }

.adapt-row { display: flex; align-items: center; gap: 10px; }
.ring { width: 34px; height: 34px; flex: none; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 4; }
.ring .ring-bg { stroke: #ececE6; }
.ring .ring-fg { stroke: #9a9a95; stroke-linecap: round; transition: stroke 0.2s; }
.hud-text { display: block; font-size: 14px; font-weight: 600; line-height: 1.25; }
.hud-cell.adapting { border-color: var(--green-ink); box-shadow: 0 0 0 2px rgba(34, 128, 74, 0.12); }
.hud-cell.hurting { border-color: var(--red-ink); box-shadow: 0 0 0 2px rgba(196, 50, 60, 0.14); }

.rhythm {
  position: relative; height: 10px; border-radius: 5px; margin-top: 8px;
  background: linear-gradient(90deg, #e9e9e3 0%, #e9e9e3 30%, var(--green) 34%, var(--green) 50%,
    var(--orange) 55%, var(--orange) 68%, var(--red) 75%, var(--red) 100%);
}
.rhythm .dot {
  position: absolute; top: 50%; left: 0; width: 14px; height: 14px; margin: -7px 0 0 -7px;
  border-radius: 50%; background: var(--ink); border: 2px solid #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  transition: left 0.12s linear;
}
#rhythmWord { color: var(--ink); letter-spacing: 0.06em; }

/* ── Tap pad (touch) and key hint (desktop) ───────────────────────────── */
.pad {
  position: relative; overflow: hidden;
  display: none; flex-direction: column; align-items: center; justify-content: center;
  height: clamp(96px, 19dvh, 170px);
  border-radius: 20px;
  background: #fff;
  border: 2px solid var(--blue);
  color: var(--blue);
  touch-action: none;
  cursor: pointer;
  -webkit-touch-callout: none;
}
.pad.pressed { background: #f0f3ff; }
.pad-label { font-weight: 800; font-size: 22px; letter-spacing: 0.06em; pointer-events: none; }
.pad-sub { font-size: 12px; font-weight: 500; opacity: 0.75; pointer-events: none; margin-top: 2px; }
.ripple {
  position: absolute; width: 120px; height: 120px; margin: -60px 0 0 -60px;
  border-radius: 50%; background: rgba(83, 108, 245, 0.22);
  transform: scale(0); animation: ripple 0.45s ease-out forwards; pointer-events: none;
}
@keyframes ripple { to { transform: scale(2.4); opacity: 0; } }

.hint { margin: 0; text-align: center; color: var(--muted); font-size: 14px; }
kbd {
  font-family: var(--font); font-weight: 600; font-size: 12px;
  padding: 2px 7px; border-radius: 6px; background: #fff; border: 1px solid var(--line);
  box-shadow: 0 1px 0 var(--line);
}
.touch-only { display: none; }

/* ── Overlays ──────────────────────────────────────────────────────────── */
.overlay {
  position: absolute; inset: 0; z-index: 20;
  display: none; align-items: center; justify-content: center;
  padding: 16px;
  background: rgba(250, 250, 247, 0.62);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}
.overlay.show { display: flex; }
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.14);
  width: 100%; max-width: 560px; max-height: 100%;
  overflow: auto; -webkit-overflow-scrolling: touch;
  padding: 24px;
  -webkit-user-select: text; user-select: text;
}
.card.small { max-width: 380px; text-align: center; }
.card h1 { font-size: 22px; line-height: 1.3; margin: 0 0 16px; font-weight: 700; }
.card h2 { margin: 0 0 8px; }
.eyebrow { font-size: 12px; font-weight: 600; letter-spacing: 0.14em; color: var(--muted); }

.legend { list-style: none; margin: 0 0 14px; padding: 0; display: grid; gap: 10px; }
.legend li { display: flex; gap: 12px; align-items: flex-start; }
.legend b { display: block; font-size: 15px; }
.legend span { display: block; font-size: 14px; color: var(--muted); line-height: 1.4; }
.sw { flex: none; width: 26px; height: 26px; border-radius: 6px; margin-top: 1px; }
.sw.gray { background: var(--gray); }
.sw.green { background: var(--green); }
.sw.orange { background: var(--orange); }
.sw.red { background: var(--red); }
.how { font-size: 15px; line-height: 1.5; margin: 0 0 8px; }
.how.small { font-size: 14px; color: var(--muted); }
.best { font-size: 13px; color: var(--muted); margin: 12px 0 0; text-align: center; min-height: 1em; }

.actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 18px; }
.card.small .actions { justify-content: center; }
.btn {
  flex: 1 1 auto;
  font-weight: 700; font-size: 17px;
  padding: 14px 22px; border-radius: 12px; border: 0;
  background: var(--ink); color: #fff; cursor: pointer;
  min-height: 50px;
}
.btn:hover { background: #2a2a2a; }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.secondary { background: #fff; color: var(--ink); border: 1px solid var(--line); }
.btn.secondary:hover { background: #f6f6f2; }
.text-btn { background: none; border: 0; padding: 0; color: var(--muted); text-decoration: underline; cursor: pointer; font-size: 13px; }

/* Results */
.card.results { max-width: 600px; }
.res-top { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.badge {
  font-size: 12px; font-weight: 700; letter-spacing: 0.08em;
  padding: 5px 10px; border-radius: 999px; background: var(--pill); color: #0b0b0b;
}
.headline { font-size: 30px; font-weight: 800; margin: 6px 0 2px; line-height: 1.1; }
.headline[data-cat="very"] { color: var(--green-ink); }
.headline[data-cat="effective"] { color: #3a9a5c; }
.headline[data-cat="not"] { color: #7a7a74; }
.headline[data-cat="over"] { color: var(--orange-ink); }
.headline[data-cat="more-pain"] { color: var(--red-ink); }
.score-row { display: flex; align-items: baseline; gap: 8px; margin: 2px 0 12px; }
.res-score { font-size: 46px; font-weight: 800; font-variant-numeric: tabular-nums; line-height: 1; }
.res-score.neg { color: var(--red-ink); }
.res-unit { color: var(--muted); font-size: 14px; }

.strip { position: relative; display: flex; height: 12px; border-radius: 6px; overflow: visible; }
.strip .seg { display: block; height: 100%; }
.strip .seg:first-child { border-radius: 6px 0 0 6px; }
.strip .seg:last-of-type { border-radius: 0 6px 6px 0; }
.seg.neg { width: 38.46%; background: var(--red); }
.seg.low { width: 19.23%; background: var(--gray); }
.seg.mid { width: 19.23%; background: #cfeed5; }
.seg.high { width: 23.08%; background: var(--green); }
.marker {
  position: absolute; top: -6px; width: 4px; height: 24px; margin-left: -2px;
  background: var(--ink); border-radius: 2px;
  transition: left 0.6s cubic-bezier(.2, .8, .2, 1);
}
.strip-labels { position: relative; height: 18px; font-size: 11px; color: var(--muted); margin-top: 4px; }
.strip-labels span { position: absolute; transform: translateX(-50%); white-space: nowrap; }
.strip-labels span:first-child { transform: none; }

.message { font-size: 15.5px; line-height: 1.55; margin: 12px 0 10px; }
.lines { list-style: none; padding: 0; margin: 0 0 12px; display: grid; gap: 4px; }
.lines li { font-size: 14px; color: #3c3c38; padding-left: 16px; position: relative; }
.lines li::before { content: ''; position: absolute; left: 2px; top: 0.55em; width: 7px; height: 7px; border-radius: 50%; background: var(--blue); }
.chart { display: block; width: 100%; height: 118px; border-radius: 10px; background: #f4f4f0; }
.chart-caption { font-size: 11px; color: var(--muted); margin: 4px 0 10px; display: flex; justify-content: space-between; }
.breakdown { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 6px 0 10px; }
.breakdown div { background: #f6f6f2; border-radius: 10px; padding: 8px 10px; }
.breakdown span { display: block; font-size: 11px; color: var(--muted); letter-spacing: 0.06em; font-weight: 600; }
.breakdown b { font-size: 17px; font-variant-numeric: tabular-nums; }
.breakdown .pain b { color: var(--red-ink); }
.zones { display: flex; height: 10px; border-radius: 5px; overflow: hidden; }
.zones i { display: block; height: 100%; }
.zones-legend { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 12px; color: var(--muted); margin-top: 6px; }
.zones-legend span::before {
  content: ''; display: inline-block; width: 9px; height: 9px; border-radius: 2px;
  margin-right: 5px; vertical-align: -1px; background: var(--c);
}

/* Session */
.sets { display: grid; gap: 6px; margin: 4px 0 14px; }
.sets div {
  display: flex; justify-content: space-between; gap: 10px; align-items: baseline;
  background: #f6f6f2; border-radius: 10px; padding: 9px 12px; font-size: 14px;
}
.sets b { font-variant-numeric: tabular-nums; }
.range-rows { display: grid; gap: 6px; }
.range-row { display: grid; grid-template-columns: 86px 1fr 52px; gap: 8px; align-items: center; font-size: 12px; color: var(--muted); }
.range-row .track { height: 12px; border-radius: 6px; background: #f1f1ec; overflow: hidden; }
.range-row .fill { height: 100%; background: var(--green); border-right: 3px solid var(--green-ink); }
.range-row b { color: var(--ink); text-align: right; font-variant-numeric: tabular-nums; }

/* Flare flash */
.vignette {
  position: fixed; inset: 0; pointer-events: none; z-index: 15;
  background: radial-gradient(ellipse at center, transparent 52%, rgba(212, 40, 52, 0.38) 100%);
  opacity: 0; transition: opacity 0.6s ease;
}
.vignette.hit { opacity: 1; transition: none; }
.vignette.severe { background: radial-gradient(ellipse at center, rgba(212, 40, 52, 0.08) 35%, rgba(180, 20, 35, 0.55) 100%); }

/* ── Touch devices: show the pad ──────────────────────────────────────── */
@media (pointer: coarse) {
  .pad { display: flex; }
  .hint { display: none; }
  .kbd-only { display: none; }
  .touch-only { display: inline; }
}

/* ── Narrow screens: HUD becomes a compact strip above the bar ────────── */
@media (max-width: 760px) {
  .app { gap: 8px; }
  .pad { display: flex; }
  .stage { grid-template-columns: 1fr; grid-template-rows: auto minmax(0, 1fr); gap: 8px; }
  .hud {
    display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px;
    align-self: stretch; order: -1;
  }
  .hud-cell { padding: 6px 8px; border-radius: 10px; }
  .hud-cell.wide { grid-column: span 2; }
  .hud-label { font-size: 9px; letter-spacing: 0.08em; }
  .hud-big { font-size: 22px; }
  .hud-mid { font-size: 16px; }
  .hud-text { font-size: 12px; }
  .ring { width: 26px; height: 26px; }
  .adapt-row { gap: 7px; }
  .rhythm { margin-top: 6px; height: 8px; }
  .rhythm .dot { width: 12px; height: 12px; margin: -6px 0 0 -6px; }
  .timebar, .meter { height: 4px; margin-top: 4px; }
  .top { justify-content: flex-start; gap: 8px; }
  .top-left { position: static; transform: none; order: 2; }
  .pill { font-size: 18px; }
  .chip { font-size: 11px; padding: 6px 9px; }
  .icon-btn { width: 34px; height: 34px; }
  .card { padding: 18px; border-radius: 18px; }
  .card h1 { font-size: 19px; }
  .headline { font-size: 25px; }
  .res-score { font-size: 38px; }
  .breakdown { grid-template-columns: repeat(2, 1fr); }
  .ready-banner { top: 22%; font-size: 14px; }
}
@media (max-width: 420px) {
  .lbl-long { display: none; }
  .lbl-short { display: inline; }
}
@media (max-width: 380px) {
  .hud-big { font-size: 19px; }
  .hud-label { font-size: 9px; letter-spacing: 0.06em; }
}
@media (max-width: 340px) {
  .top-left .chip { display: none; }
}
@media (max-height: 560px) and (max-width: 760px) {
  .pad { height: 84px; }
}

@media (prefers-reduced-motion: reduce) {
  .ready-banner, #cellTime.urgent .hud-big { animation: none; }
  .vignette { transition: none; }
}
