:root {
  --bg: #09090b;
  --surface: #0f0f11;
  --surface-2: #18181b;
  --border: #232327;
  --border-2: #3a3a41;
  --text: #fafafa;
  --text-2: #a1a1aa;
  --text-3: #8e8e97;
  --accent: #c3e95a;
  --hot: #ef4444;
  --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: 50px;
  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 .controls { visibility: hidden; }

/* The build area takes all the height the rest leaves; its touch target reaches the screen edges. */
.stage {
  flex: 1 1 auto;
  min-height: 0;
  margin: 0 calc(-1 * var(--gutter));
  padding: 0 var(--gutter) 10px;
  display: flex;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

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

.card canvas {
  position: absolute;
  left: 0;
  top: 0;
  display: block;
  touch-action: none;
}

.note {
  position: absolute;
  left: 12px;
  top: 10px;
  max-width: calc(100% - 110px);
  font-size: 12px;
  line-height: 1.3;
  color: var(--text-3);
  pointer-events: none;
}

.legend {
  position: absolute;
  right: 12px;
  top: 11px;
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 12px;
  color: var(--text-3);
  pointer-events: none;
}

.legend i {
  display: block;
  width: 52px;
  height: 5px;
  border-radius: 3px;
  background: linear-gradient(90deg, #d4d4d8 0%, #d6d3c4 40%, #fdba74 62%, #f97316 82%, #ef4444 100%);
}

.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;
  width: max-content;
  text-align: center;
  pointer-events: none;
  transition: opacity 0.25s;
}

.toast.out { opacity: 0; }

/* ---------- status ---------- */

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

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

.now { min-width: 0; padding-top: 2px; }

.now-main {
  font-size: 21px;
  line-height: 1.2;
  font-weight: 650;
  letter-spacing: -0.015em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.now-main .hl { color: var(--accent); }
.now-main .medal-gold { color: var(--gold); }
.now-main .medal-silver { color: var(--silver); }
.now-main .medal-bronze { color: var(--bronze); }

.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;
}

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

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

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

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

.count-val {
  font-size: 30px;
  line-height: 1.05;
  font-weight: 650;
  letter-spacing: -0.03em;
}

.count-val.bump { animation: bump 0.18s ease-out; }

@keyframes bump {
  0% { transform: scale(1.12); }
  100% { transform: scale(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 i {
  flex: none;
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--border-2);
}

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

.rung[data-medal="gold"].got i { background: var(--gold); }
.rung[data-medal="silver"].got i { background: var(--silver); }
.rung[data-medal="bronze"].got i { background: var(--bronze); }
.rung.got b { color: var(--text); }
.rung.target { border-color: var(--border-2); }
.rung.target b { color: var(--text); }
.rung.pop i { animation: pop 0.5s cubic-bezier(0.2, 1.6, 0.4, 1); }

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

/* ---------- controls ---------- */

/* Undo, Redo, [Brace all | Clear Best], 2x, Go: fits a 343 px column (375 px phone). */
.controls {
  flex: none;
  display: flex;
  gap: 6px;
  padding-top: 10px;
  min-width: 0;
}

.slot {
  flex: none;
  width: 114px;
  display: flex;
  gap: 6px;
}

.slot .btn { flex: 1 1 0; min-width: 0; padding: 0; }

.btn-brace {
  border-color: var(--accent) !important;
  color: var(--accent) !important;
}

.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, .icon-btn:focus-visible { outline: 2px solid var(--text-2); outline-offset: 2px; }

.btn-primary {
  flex: 1 1 auto;
  min-width: 0;
  padding: 0 8px;
  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: 44px;
  padding: 0 8px;
  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;
}

.btn-small::after {
  content: "";
  position: absolute;
  inset: -6px -4px;
}

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

.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); }

/* ---------- 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: 700px) {
  .top { height: 44px; }
  .info { gap: 8px; }
  .status { height: 52px; }
  .now-main { font-size: 19px; }
  .count-val { font-size: 27px; }
  .btn, .icon-btn { height: 48px; }
  .controls { padding-top: 8px; }
  .stage { padding-bottom: 8px; }
}

@media (max-width: 380px) {
  .controls, .slot { gap: 5px; }
  .slot { width: 108px; }
  .ladder { gap: 6px; }
  .rung { padding: 0 7px; gap: 4px; }
  .rung b { font-size: 13px; }
  .btn { font-size: 15px; }
}

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