:root {
  --bg-start: #fff3a8;
  --bg-end: #ffb199;
  --tile-bg: linear-gradient(180deg, #ffffff, #fff0e0);
  --tile-color: #c2410c;
  --tile-shadow: 0 4px 0 #f59e0b, inset 0 2px 0 rgba(255,255,255,.8);
  --tile-sel-bg: linear-gradient(180deg, #fde047, #f97316);
  --tile-sel-shadow: 0 4px 0 #b45309, 0 0 0 3px #fff, 0 0 0 5px #f97316;
  --text: #3b2a12;
  --cols: 9;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--text);
  background: linear-gradient(160deg, var(--bg-start), var(--bg-end));
}

.app {
  max-width: 560px;
  margin: 0 auto;
  padding: 16px 16px 120px;
  min-height: 100vh;
}

.header {
  text-align: center;
  padding: calc(12px + env(safe-area-inset-top, 0px)) 0 16px;
  padding-left: 50px;
}

.title {
  margin: 0 0 8px;
  font-size: 28px;
  font-weight: 800;
  letter-spacing: 0.5px;
  color: #7c2d12;
  text-shadow: 0 2px 0 #fff, 0 3px 0 rgba(0,0,0,0.06);
}

.stats {
  display: flex;
  justify-content: center;
  gap: 18px;
  font-size: 14px;
  font-weight: 600;
  color: #7c2d12;
}

.board-wrap {
  display: flex;
  justify-content: center;
  padding: 8px 0 20px;
}

.board {
  display: grid;
  grid-template-columns: repeat(var(--cols), 1fr);
  gap: 6px;
  width: 100%;
  max-width: 500px;
  padding: 14px;
  background: rgba(255,255,255,0.5);
  border-radius: 18px;
  box-shadow: inset 0 2px 0 rgba(255,255,255,0.8), 0 8px 24px rgba(180,83,9,0.15);
}

.tile {
  appearance: none;
  border: none;
  aspect-ratio: 1;
  border-radius: 12px;
  background: var(--tile-bg);
  color: var(--tile-color);
  font: 700 clamp(18px, 4.5vw, 28px)/1 system-ui, sans-serif;
  cursor: pointer;
  box-shadow: var(--tile-shadow);
  transition: transform 200ms cubic-bezier(.34, 1.56, .64, 1),
              box-shadow 200ms ease,
              opacity 200ms ease;
  user-select: none;
  padding: 0;
}

.tile:hover:not(.struck):not(.selected) { transform: translateY(-1px); }

.tile.selected {
  background: var(--tile-sel-bg);
  color: #fff;
  box-shadow: var(--tile-sel-shadow);
  transform: scale(1.08);
}

.tile.struck {
  opacity: 0.25;
  box-shadow: none;
  cursor: default;
  text-decoration: line-through;
}

/* Action bar --------------------------------------------------------------- */
.actions {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom, 0));
  background: linear-gradient(180deg, rgba(255,243,168,0) 0%, rgba(255,177,153,0.85) 40%);
  backdrop-filter: blur(4px);
  z-index: 10;
}

.btn {
  appearance: none;
  border: none;
  border-radius: 14px;
  padding: 12px 10px;
  font: 700 14px/1 system-ui, sans-serif;
  color: #fff;
  cursor: pointer;
  box-shadow: 0 4px 0 rgba(0,0,0,0.2), inset 0 2px 0 rgba(255,255,255,0.3);
  transition: transform 120ms ease, box-shadow 120ms ease;
}
.btn:active { transform: translateY(2px); box-shadow: 0 2px 0 rgba(0,0,0,0.2); }

.btn-undo { background: linear-gradient(180deg, #60a5fa, #2563eb); }
.btn-hint { background: linear-gradient(180deg, #fde047, #eab308); color: #713f12; }
.btn-line { background: linear-gradient(180deg, #86efac, #16a34a); }
.btn-new  { background: linear-gradient(180deg, #fca5a5, #dc2626); }

/* Toast -------------------------------------------------------------------- */
.toast {
  position: fixed;
  bottom: 110px;
  left: 50%;
  transform: translateX(-50%) translateY(10px);
  background: rgba(60, 30, 0, 0.9);
  color: #fff;
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 200ms ease, transform 200ms ease;
  z-index: 40;
}
.toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.hidden { display: none !important; }

/* Animations --------------------------------------------------------------- */
@keyframes pop {
  0%   { transform: scale(1); }
  40%  { transform: scale(1.25); }
  70%  { transform: scale(0.9); }
  100% { transform: scale(0); opacity: 0; }
}
.tile.popping { animation: pop 280ms ease forwards; }

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%      { transform: translateX(-5px); }
  40%      { transform: translateX(5px); }
  60%      { transform: translateX(-4px); }
  80%      { transform: translateX(3px); }
}
.tile.shaking { animation: shake 300ms ease; }

@keyframes hintPulse {
  0%, 100% { box-shadow: 0 4px 0 #f59e0b, 0 0 0 0 rgba(250, 204, 21, 0); }
  50%      { box-shadow: 0 4px 0 #f59e0b, 0 0 0 8px rgba(250, 204, 21, 0.6); }
}
.tile.hinting { animation: hintPulse 500ms ease 2; }

@keyframes fallIn {
  0%   { transform: translateY(-24px); opacity: 0; }
  70%  { transform: translateY(4px); opacity: 1; }
  100% { transform: translateY(0); }
}
.tile.falling {
  animation: fallIn 380ms cubic-bezier(.34, 1.56, .64, 1) backwards;
}

/* Win overlay -------------------------------------------------------------- */
.win-overlay {
  position: fixed;
  inset: 0;
  background: rgba(60, 30, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  overflow: hidden;
  animation: fadeIn 400ms ease;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.win-card {
  background: #fff;
  padding: 28px 36px;
  border-radius: 20px;
  text-align: center;
  box-shadow: 0 16px 50px rgba(0,0,0,0.3);
  z-index: 2;
  position: relative;
}
.win-card h2 { margin: 0 0 8px; font-size: 32px; color: #f97316; }
.win-card p { margin: 4px 0; }
.win-card .subtitle { color: #78716c; font-size: 14px; }
.win-card .btn { margin-top: 16px; min-width: 140px; }

.confetti {
  position: absolute;
  top: -20px;
  width: 10px;
  height: 16px;
  border-radius: 2px;
  animation-name: confettiFall;
  animation-timing-function: linear;
  animation-iteration-count: infinite;
  z-index: 1;
}
@keyframes confettiFall {
  0%   { transform: translateY(0) rotate(0deg); opacity: 1; }
  100% { transform: translateY(110vh) rotate(720deg); opacity: 0.8; }
}

/* --- Back button --- */
.back-btn {
  position: fixed;
  top: calc(14px + env(safe-area-inset-top, 0px));
  left: 12px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: rgba(124,45,18,0.7);
  color: #fff;
  font-size: 22px;
  font-weight: bold;
  cursor: pointer;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(4px);
}
.back-btn:hover { background: rgba(124,45,18,0.9); }

/* Desktop tweaks ----------------------------------------------------------- */
@media (min-width: 768px) {
  .app { padding-bottom: 32px; }
  .actions {
    position: static;
    grid-template-columns: repeat(4, auto);
    justify-content: center;
    background: none;
    backdrop-filter: none;
    padding: 0 0 24px;
  }
  .btn { min-width: 130px; }
  .toast { bottom: 32px; }
}
