:root {
  --bg: #09090b;
  --surface: #0f0f11;
  --surface-2: #18181b;
  --border: #232327;
  --border-2: #3a3a41;
  --text: #fafafa;
  --text-2: #a1a1aa;
  --text-3: #8e8e97;
  --accent: #38bdf8;
  --gold: #c9a95c;
  --silver: #b9b9c2;
  --bronze: #a87a5b;
  --radius: 14px;
  --gutter: 16px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
}

html {
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  color: var(--text);
  font: 15px/1.4 var(--font);
  font-variant-numeric: tabular-nums;
  -webkit-font-smoothing: antialiased;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

[hidden] { display: none !important; }

#app {
  height: 100vh;
  height: 100dvh;
  max-width: 480px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  padding:
    env(safe-area-inset-top, 0px)
    calc(var(--gutter) + env(safe-area-inset-right, 0px))
    max(12px, env(safe-area-inset-bottom, 0px))
    calc(var(--gutter) + env(safe-area-inset-left, 0px));
}

@media (min-width: 560px) {
  #app { border-left: 1px solid var(--border); border-right: 1px solid var(--border); }
}

/* ---------- header ---------- */

.top {
  flex: none;
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand {
  font-size: 16px;
  font-weight: 650;
  letter-spacing: -0.01em;
}

.brand .num {
  margin-left: 4px;
  color: var(--text-3);
  font-weight: 500;
}

.top-right {
  display: flex;
  align-items: center;
  gap: 14px;
}

.streak {
  font-size: 13px;
  color: var(--text-3);
}

.streak b {
  color: var(--text);
  font-weight: 600;
}

/* ---------- play screen ---------- */

.screen {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.screen.loading .stage,
.screen.loading .info,
.screen.loading .tools,
.screen.loading .controls { visibility: hidden; }

.stage { flex: none; }

/* The touch target reaches the screen edges and 10px above/below the card. */
.course-hit {
  margin: 0 calc(-1 * var(--gutter));
  padding: 10px var(--gutter);
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  cursor: crosshair;
}

.screen.running .course-hit { cursor: pointer; }

.course-card {
  position: relative;
  margin: 0 auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: hidden;
}

.course-card canvas {
  display: block;
  touch-action: none;
}

/* ---------- info + controls ---------- */

.info {
  flex: none;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-top: 2px;
}

.controls {
  flex: none;
  padding-top: 10px;
}

.info { margin-bottom: auto; }

.status {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  height: 64px;
  overflow: hidden;
}

.toast {
  position: absolute;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  max-width: calc(100% - 24px);
  padding: 8px 12px;
  border: 1px solid var(--border-2);
  border-radius: 10px;
  background: rgba(24, 24, 27, 0.95);
  font-size: 13px;
  font-weight: 550;
  white-space: nowrap;
  pointer-events: none;
  transition: opacity 0.25s;
}

.toast.out { opacity: 0; }

.now { min-width: 0; }

.now-line {
  display: flex;
  align-items: baseline;
  gap: 10px;
  min-width: 0;
}

.now-main {
  font-size: 36px;
  line-height: 1.05;
  font-weight: 650;
  letter-spacing: -0.03em;
  white-space: nowrap;
}

.now-stars {
  font-size: 22px;
  letter-spacing: 1px;
  color: var(--accent);
  white-space: nowrap;
}

.now-stars .off { color: #3f3f46; }

.now-main.fail,
.now-main.idle {
  font-size: 22px;
  line-height: 1.2;
  letter-spacing: -0.015em;
  padding-top: 6px;
}

.now-sub span:first-child { overflow: hidden; text-overflow: ellipsis; }

.now-sub {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
  min-height: 20px;
  font-size: 14px;
  color: var(--text-2);
  white-space: nowrap;
}

.tag {
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
}

.best {
  flex: none;
  text-align: right;
}

.best-label {
  font-size: 12px;
  color: var(--text-3);
}

.best-val {
  font-size: 22px;
  line-height: 1.15;
  font-weight: 650;
  letter-spacing: -0.02em;
}

.best-medal {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 2px;
  font-size: 13px;
  color: var(--text-2);
}

.best-medal i, .rung i {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--border-2);
}

.best-medal[data-medal="gold"] i, .rung[data-medal="gold"].got i { background: var(--gold); }
.best-medal[data-medal="silver"] i, .rung[data-medal="silver"].got i { background: var(--silver); }
.best-medal[data-medal="bronze"] i, .rung[data-medal="bronze"].got i { background: var(--bronze); }

.best-medal.pop { animation: pop 0.5s cubic-bezier(0.2, 1.6, 0.4, 1); }

@keyframes pop {
  0% { transform: scale(0.6); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

.ladder {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.rung {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 34px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 12px;
  color: var(--text-3);
  white-space: nowrap;
}

.rung b {
  margin-left: auto;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-2);
}

.rung.got b { color: var(--text); }
.rung.target { border-color: var(--border-2); }
.rung.target b { color: var(--text); }

.controls {
  display: flex;
  gap: 8px;
}

@media (max-height: 600px) {
  .info { gap: 8px; }
  .status { height: 58px; }
}

.btn {
  appearance: none;
  -webkit-appearance: none;
  border: 1px solid transparent;
  border-radius: 12px;
  height: 52px;
  padding: 0 16px;
  font: 600 16px/1 var(--font);
  letter-spacing: -0.005em;
  color: inherit;
  background: none;
  cursor: pointer;
  touch-action: manipulation;
  transition: background-color 0.15s, color 0.15s, border-color 0.15s, transform 0.1s;
}

.btn:active:not(:disabled) { transform: scale(0.97); }
.btn:focus-visible { outline: 2px solid var(--text-2); outline-offset: 2px; }

.btn-primary {
  flex: 1;
  background: var(--text);
  color: #09090b;
}

.btn-primary.running {
  background: var(--surface-2);
  border-color: var(--border-2);
  color: var(--text);
}

.btn-secondary {
  flex: none;
  border-color: var(--border-2);
  color: #e4e4e7;
}

.btn-sm {
  min-width: 60px;
  padding: 0 12px;
  font-size: 15px;
}

.btn-secondary:disabled {
  border-color: var(--border);
  color: #5b5b63;
  cursor: default;
}

.btn-toggle[aria-pressed="true"] {
  background: var(--surface-2);
  border-color: var(--accent);
  color: var(--accent);
}

.btn-small {
  height: 34px;
  padding: 0 14px;
  border-color: var(--border-2);
  border-radius: 10px;
  font-size: 14px;
  color: #e4e4e7;
  position: relative;
}

/* Taller hit area than it looks. */
.btn-small::after {
  content: "";
  position: absolute;
  inset: -6px -4px;
}

/* ---------- tools ---------- */

.tools {
  flex: none;
  display: flex;
  align-items: center;
  gap: 6px;
  padding-top: 12px;
}

.seg {
  display: flex;
  flex: none;
  padding: 2px;
  border: 1px solid var(--border-2);
  border-radius: 12px;
}

.seg-btn {
  appearance: none;
  -webkit-appearance: none;
  height: 38px;
  min-width: 52px;
  padding: 0 10px;
  border: 0;
  border-radius: 9px;
  background: none;
  color: var(--text-3);
  font: 600 14px/1 var(--font);
  cursor: pointer;
  touch-action: manipulation;
  transition: background-color 0.15s, color 0.15s;
}

.seg-btn[aria-pressed="true"] {
  background: var(--surface-2);
  color: var(--text);
  box-shadow: inset 0 0 0 1px var(--border-2);
}

.seg-btn:focus-visible { outline: 2px solid var(--text-2); outline-offset: 1px; }

.brush .seg-btn {
  min-width: 34px;
  padding: 0;
  display: grid;
  place-items: center;
}

.brush i {
  display: block;
  border-radius: 50%;
  background: currentColor;
}

.brush [data-brush="s"] i { width: 6px; height: 6px; }
.brush [data-brush="m"] i { width: 11px; height: 11px; }
.brush [data-brush="l"] i { width: 17px; height: 17px; }

.brush.off { opacity: 0.35; }

.hist {
  display: flex;
  gap: 6px;
  margin-left: auto;
}

.icon-btn {
  appearance: none;
  -webkit-appearance: none;
  width: 42px;
  height: 44px;
  border: 1px solid var(--border-2);
  border-radius: 12px;
  background: none;
  color: #e4e4e7;
  display: grid;
  place-items: center;
  cursor: pointer;
  touch-action: manipulation;
}

.icon-btn svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-btn:disabled { border-color: var(--border); color: #4a4a52; cursor: default; }
.icon-btn:active:not(:disabled) { transform: scale(0.96); }

/* ---------- tweak readout ---------- */

.readout {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 20;
  padding: 6px 10px;
  border: 1px solid var(--border-2);
  border-radius: 10px;
  background: rgba(28, 28, 32, 0.97);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.5);
  font-size: 15px;
  font-weight: 650;
  white-space: pre;
  pointer-events: none;
  visibility: hidden;
  will-change: transform;
}

.readout.show { visibility: visible; }

/* ---------- error ---------- */

.error {
  justify-content: center;
  align-items: flex-start;
  gap: 6px;
  padding-bottom: 20vh;
}

.error-title { margin: 0; font-size: 17px; font-weight: 600; }
.error-detail { margin: 0; font-size: 13px; color: var(--text-3); }

@media (max-height: 600px) {
  .now-main { font-size: 30px; }
  .btn { height: 48px; }
  .tools { padding-top: 8px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition-duration: 0s !important; animation: none !important; }
}
