:root {
  --bg: #09090b;
  --surface: #111113;
  --surface-2: #18181b;
  --border: #232327;
  --border-2: #3a3a41;
  --text: #fafafa;
  --text-2: #a1a1aa;
  --text-3: #8e8e97;
  --accent: #34d399;
  --danger: #e07a6a;
  --bronze: #c4895a;
  --silver: #b9bec7;
  --gold: #d9b44a;
  --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: 48px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

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

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

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

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

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

.play {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  touch-action: none;
}

/* The touch area reaches the screen edges so drags that start near the edge still land. */
.map-hit {
  flex: none;
  display: flex;
  justify-content: center;
  margin: 0 calc(-1 * var(--gutter));
  padding: 0 var(--map-gutter, 8px);
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

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

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

.hint {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 28px;
  text-align: center;
  background: rgba(17, 17, 19, 0.9);
  pointer-events: none;
  transition: opacity 0.2s;
}

.hint.out { opacity: 0; }

.banner {
  position: absolute;
  left: 50%;
  top: 12px;
  transform: translateX(-50%);
  padding: 6px 12px;
  border: 1px solid var(--border-2);
  border-radius: 999px;
  background: rgba(17, 17, 19, 0.92);
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  pointer-events: none;
}

.hint p {
  margin: 0;
  font-size: 16px;
  font-weight: 550;
  line-height: 1.35;
  color: var(--text);
  text-wrap: balance;
}

.hint .hint-sub {
  font-size: 14px;
  font-weight: 400;
  color: var(--text-2);
}

/* one line under the map: last result, or what the held piece does */
.line {
  flex: none;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-2);
  white-space: nowrap;
  overflow: hidden;
}

.line b { color: var(--text); font-weight: 600; }
.line .up { color: var(--accent); font-weight: 600; }
.line .down { color: var(--text-3); }

/* tray and battle HUD share one slot so nothing below moves */
.slot {
  flex: none;
  height: var(--slot-h, 68px);
}

.tray {
  height: 100%;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
  touch-action: none;
}

.chip {
  appearance: none;
  -webkit-appearance: none;
  position: relative;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 0;
  border: 1px solid var(--border-2);
  border-radius: 12px;
  background: var(--surface-2);
  color: var(--text);
  font: 500 11px/1 var(--font);
  cursor: grab;
  touch-action: none;
  transition: background-color 0.15s, border-color 0.15s, opacity 0.15s, transform 0.1s;
}

.chip canvas { display: block; pointer-events: none; }
.chip span { pointer-events: none; letter-spacing: 0.01em; }

.chip.placed {
  background: transparent;
  border-color: var(--border);
  color: var(--text-3);
}

.chip.placed canvas { opacity: 0.45; }

.chip.held {
  border-color: var(--accent);
  background: #13241d;
  color: var(--text);
}

.chip.held canvas { opacity: 1; }

.chip .tag {
  font-size: 10px;
  font-weight: 500;
  color: var(--text-3);
  letter-spacing: 0.02em;
}

.chip.later:not(.held):not(.placed) { border-style: dashed; }

/* between waves only the arriving piece can move */
.chip.locked { opacity: 0.35; }
.chip.next:not(.held) { border-color: var(--accent); }

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

.hud {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 4px;
}

.hud-score {
  display: flex;
  align-items: baseline;
  gap: 10px;
  font-size: 40px;
  line-height: 1;
  font-weight: 650;
  letter-spacing: -0.03em;
}

.hud-score .of {
  margin-left: 2px;
  font-size: 18px;
  font-weight: 500;
  letter-spacing: 0;
  color: var(--text-3);
}

.hud-keep {
  flex: none;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
}

.hearts {
  display: flex;
  gap: 4px;
}

.hearts svg { display: block; width: 18px; height: 16px; }
.hearts path { fill: #e4e4e7; transition: fill 0.15s; }
.hearts .lost path { fill: #2a2a2f; }

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

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

.status {
  flex: none;
  margin-top: auto;
  display: grid;
  grid-template-columns: minmax(0, 0.8fr) minmax(0, 1.3fr) auto;
  align-items: center;
  gap: 12px;
  min-height: 52px;
  padding: 6px 0 4px;
}

.stat { min-width: 0; }

.stat-btn {
  appearance: none;
  -webkit-appearance: none;
  display: block;
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: default;
}

.stat-btn.loadable { cursor: pointer; }
.stat-btn.loadable .stat-label { color: var(--accent); }

.stat-goal {
  font-size: 13px;
  line-height: 1.3;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.stat-label {
  display: block;
  font-size: 12px;
  color: var(--text-3);
  line-height: 1.2;
}

.stat-value {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 18px;
  font-weight: 650;
  letter-spacing: -0.01em;
  line-height: 1.3;
  white-space: nowrap;
}

.dot {
  display: inline-block;
  flex: none;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: transparent;
  box-shadow: inset 0 0 0 1.5px var(--border-2);
}

.dot.bronze { background: var(--bronze); box-shadow: none; }
.dot.silver { background: var(--silver); box-shadow: none; }
.dot.gold { background: var(--gold); box-shadow: none; }
.dot.goldplus { background: var(--gold); box-shadow: 0 0 0 2px var(--bg), 0 0 0 3px var(--gold); }

.c-bronze { color: var(--bronze); }
.c-silver { color: var(--silver); }
.c-gold { color: var(--gold); }
.c-goldplus { color: #f2d779; }

/* ---------- actions ---------- */

.actions {
  flex: none;
  display: flex;
  gap: 10px;
  padding-top: 6px;
}

.btn {
  appearance: none;
  -webkit-appearance: none;
  border: 1px solid transparent;
  border-radius: 12px;
  height: 52px;
  padding: 0 20px;
  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.98); }
.btn:focus-visible { outline: 2px solid var(--text-2); outline-offset: 2px; }

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

.btn-primary:disabled {
  background: var(--surface-2);
  border-color: var(--border);
  color: var(--text-3);
  cursor: default;
}

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

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

.btn-ghost {
  height: 40px;
  padding: 0 14px;
  border-color: var(--border-2);
  font-size: 14px;
  color: #e4e4e7;
}

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

/* ---------- drag ghost ---------- */

.ghost {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 20;
  pointer-events: none;
  visibility: hidden;
  will-change: transform;
}

.ghost.show { visibility: visible; }
.ghost canvas { display: block; }

/* ---------- short screens ---------- */

@media (max-height: 700px) {
  .top { height: 42px; }
  .line { height: 26px; }
  .slot { height: 60px; }
  .status { min-height: 46px; padding: 4px 0 2px; }
  .stat-value { font-size: 17px; }
  .btn { height: 48px; }
  .btn-ghost { height: 38px; }
  .hud-score { font-size: 34px; }
  .chip .tag { display: none; }
}

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