:root {
  --felt-1: #13573f;
  --felt-2: #0b3a2a;
  --ink: #f4f1e8;
  --ink-dim: rgba(244, 241, 232, 0.62);
  --card: #fbfaf6;
  --card-edge: rgba(0, 0, 0, 0.18);
  --red: #c8283a;
  --black: #1c2230;
  --back-1: #2b4c9b;
  --back-2: #1d3576;
  --accent: #f2c14e;
  --slot: rgba(255, 255, 255, 0.09);
  --slot-edge: rgba(255, 255, 255, 0.22);
  --sheet: #16241e;
  --cw: 48px;
  --ch: 68px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; overscroll-behavior: none; }
body {
  background: radial-gradient(120% 80% at 50% 20%, var(--felt-1), var(--felt-2) 75%);
  background-color: var(--felt-2);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-user-select: none; user-select: none;
  touch-action: none;
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
#app { position: fixed; inset: 0; display: flex; flex-direction: column; }

/* HUD */
#hud {
  display: flex; align-items: center; gap: 8px;
  padding: calc(var(--safe-t) + 6px) 10px 4px;
  min-height: calc(var(--safe-t) + 52px);
}
.hud-btn { width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; }
.hud-btn:active { background: rgba(255,255,255,0.1); }
.hud-btn svg, .tool svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.hud-stats { flex: 1; display: flex; justify-content: center; gap: 22px; }
.stat { display: flex; flex-direction: column; align-items: center; line-height: 1.1; }
.stat-label { font-size: 10px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--ink-dim); }
.stat-val { font-size: 17px; font-weight: 650; font-variant-numeric: tabular-nums; }
.hud-spacer { width: 40px; }

/* Board */
#board { position: relative; flex: 1; overflow: hidden; }
.slot {
  position: absolute; width: var(--cw); height: var(--ch);
  border-radius: calc(var(--cw) * 0.12);
  background: var(--slot); box-shadow: inset 0 0 0 1.5px var(--slot-edge);
  display: grid; place-items: center; color: rgba(255,255,255,0.28);
  font-size: calc(var(--cw) * 0.5); font-weight: 700;
}
.slot.stock-slot svg { width: 46%; height: 46%; fill: none; stroke: rgba(255,255,255,0.45); stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.slot.found-slot { font-size: calc(var(--cw) * 0.62); }
.slot.hint, .card.hint .face { animation: hintPulse 0.9s ease-in-out 2; }

.card {
  position: absolute; left: 0; top: 0;
  width: var(--cw); height: var(--ch);
  transition: transform 190ms cubic-bezier(.2,.8,.2,1);
  will-change: transform;
  perspective: 600px;
}
.card.dragging, .card.instant { transition: none; }
.card .inner { position: absolute; inset: 0; transition: transform 260ms ease; transform-style: preserve-3d; }
.card.down .inner { transform: rotateY(180deg); }
.card .face, .card .back {
  position: absolute; inset: 0; border-radius: calc(var(--cw) * 0.12);
  backface-visibility: hidden; -webkit-backface-visibility: hidden;
  box-shadow: 0 1px 2px rgba(0,0,0,0.35), 0 0 0 0.5px var(--card-edge);
}
.card .face { background: var(--card); overflow: hidden; }
.card .back {
  transform: rotateY(180deg);
  background:
    repeating-linear-gradient(45deg, rgba(255,255,255,0.08) 0 3px, transparent 3px 7px),
    repeating-linear-gradient(-45deg, rgba(255,255,255,0.08) 0 3px, transparent 3px 7px),
    linear-gradient(160deg, var(--back-1), var(--back-2));
  border: calc(var(--cw) * 0.06) solid #f6f3ea;
}
.face .r {
  position: absolute; left: calc(var(--cw) * 0.07); top: calc(var(--cw) * 0.02);
  font-size: calc(var(--cw) * 0.38); font-weight: 750; letter-spacing: -0.04em; line-height: 1.05;
}
.face .s {
  position: absolute; right: calc(var(--cw) * 0.07); top: calc(var(--cw) * 0.03);
  font-size: calc(var(--cw) * 0.33); line-height: 1.05;
}
.face .c {
  position: absolute; left: 0; right: 0; bottom: calc(var(--ch) * 0.08);
  text-align: center; font-size: calc(var(--cw) * 0.72); line-height: 1;
}
.face.red { color: var(--red); }
.face.black { color: var(--black); }
.card.sel .face { box-shadow: 0 0 0 2.5px var(--accent), 0 4px 12px rgba(0,0,0,0.4); }
.card.dragging .face { box-shadow: 0 10px 22px rgba(0,0,0,0.45), 0 0 0 0.5px var(--card-edge); }
.card.shake { animation: shake 280ms ease; }

@keyframes hintPulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(242,193,78,0), 0 1px 2px rgba(0,0,0,.35); }
  50% { box-shadow: 0 0 0 3px var(--accent), 0 0 18px 4px rgba(242,193,78,0.6); }
}
@keyframes shake {
  0%, 100% { translate: 0 0; } 25% { translate: -4px 0; } 50% { translate: 4px 0; } 75% { translate: -2px 0; }
}

/* Toolbar */
#toolbar {
  display: flex; justify-content: space-around; align-items: center;
  padding: 6px 8px calc(var(--safe-b) + 8px);
  background: linear-gradient(to top, rgba(0,0,0,0.28), rgba(0,0,0,0));
}
.tool {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  min-width: 64px; padding: 6px 10px; border-radius: 14px;
  font-size: 11px; font-weight: 600; letter-spacing: 0.02em;
}
.tool:active { background: rgba(255,255,255,0.1); }
.tool:disabled { opacity: 0.35; }
.tool[hidden] { display: none; }
#btn-auto { color: var(--accent); }

#version {
  position: fixed; left: 8px; bottom: calc(var(--safe-b) + 2px);
  font-size: 9px; color: rgba(255,255,255,0.35); font-variant-numeric: tabular-nums; pointer-events: none;
}

/* Toast */
#toast {
  position: fixed; left: 50%; top: calc(var(--safe-t) + 62px); transform: translate(-50%, -10px);
  background: rgba(10, 20, 16, 0.92); color: var(--ink); padding: 9px 14px; border-radius: 12px;
  font-size: 13px; font-weight: 550; max-width: 86vw; text-align: center;
  opacity: 0; pointer-events: none; transition: opacity 180ms, transform 180ms; z-index: 5000;
  box-shadow: 0 6px 20px rgba(0,0,0,0.35);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Bottom sheet */
#sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.45); z-index: 6000; }
#sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6001;
  background: var(--sheet); border-radius: 20px 20px 0 0;
  padding: 8px 16px calc(var(--safe-b) + 16px);
  max-width: 560px; margin: 0 auto;
  box-shadow: 0 -10px 30px rgba(0,0,0,0.4);
  animation: sheetIn 220ms cubic-bezier(.2,.8,.2,1);
}
@keyframes sheetIn { from { transform: translateY(100%); } }
.sheet-grip { width: 38px; height: 4px; border-radius: 2px; background: rgba(255,255,255,0.25); margin: 4px auto 10px; }
#sheet h2 { margin: 0 0 10px; font-size: 17px; }
.sheet-btn {
  display: flex; width: 100%; align-items: center; justify-content: space-between;
  padding: 14px 14px; margin: 6px 0; border-radius: 14px; background: rgba(255,255,255,0.06);
  font-size: 15px; font-weight: 600; text-align: left;
}
.sheet-btn:active { background: rgba(255,255,255,0.12); }
.sheet-btn small { color: var(--ink-dim); font-weight: 500; }
.stats-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 8px 0; }
.stats-grid div { background: rgba(255,255,255,0.06); border-radius: 12px; padding: 10px; text-align: center; }
.stats-grid b { display: block; font-size: 20px; }
.stats-grid span { font-size: 11px; color: var(--ink-dim); }

/* Win */
#win { position: fixed; inset: 0; display: grid; place-items: center; z-index: 7000; background: rgba(0,0,0,0.35); }
#win[hidden], #updater[hidden], #sheet[hidden], #sheet-backdrop[hidden] { display: none; }
.win-card {
  background: var(--sheet); border-radius: 22px; padding: 24px 22px; width: min(86vw, 340px); text-align: center;
  box-shadow: 0 20px 50px rgba(0,0,0,0.5); animation: pop 320ms cubic-bezier(.2,1.4,.4,1);
}
@keyframes pop { from { transform: scale(0.8); opacity: 0; } }
.win-title { font-size: 28px; font-weight: 800; margin-bottom: 8px; color: var(--accent); }
.win-stats { color: var(--ink-dim); margin-bottom: 18px; line-height: 1.6; }
.primary {
  width: 100%; padding: 14px; border-radius: 14px; background: var(--accent); color: #1f1a0b;
  font-weight: 750; font-size: 16px;
}
.primary:active { filter: brightness(0.92); }

/* Updater */
#updater { position: fixed; inset: 0; z-index: 9000; display: grid; place-items: center; background: rgba(6, 22, 16, 0.86); backdrop-filter: blur(4px); }
.upd-card { text-align: center; width: min(80vw, 300px); }
.upd-spinner { width: 42px; height: 42px; margin: 0 auto 14px; border-radius: 50%; border: 3px solid rgba(255,255,255,0.15); border-top-color: var(--accent); animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.upd-title { font-size: 20px; font-weight: 750; }
.upd-ver { color: var(--accent); font-weight: 650; margin-top: 2px; }
.upd-notes { color: var(--ink-dim); font-size: 13px; margin: 8px 0 14px; min-height: 1em; }
.upd-bar { height: 4px; border-radius: 2px; background: rgba(255,255,255,0.12); overflow: hidden; }
.upd-fill { height: 100%; width: 0; background: var(--accent); animation: fill 1.3s ease forwards; }
@keyframes fill { to { width: 100%; } }
.upd-sub { font-size: 12px; color: var(--ink-dim); margin-top: 10px; }

@media (prefers-reduced-motion: reduce) {
  .card, .card .inner { transition-duration: 1ms; }
}
