/* Stack Fable — warm pancake-breakfast world.
   Deliberately the opposite pole from Stack's cosmic jewel-box: morning
   light, wooden table, plates. The value→color hues are IDENTICAL to
   Stack's blocks so the number-color association transfers across games;
   only the rendering (glossy gem → fluffy pancake) changes. */

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  font-family: 'Fredoka', system-ui, sans-serif;
  background:
    radial-gradient(120% 80% at 80% -10%, rgba(255, 214, 140, 0.55), transparent 60%),
    linear-gradient(180deg, #fdf0dd 0%, #fbe3c4 55%, #f6d4a8 100%);
  color: #5b3a1e;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

body {
  display: flex;
  flex-direction: column;
}

/* ── Shared value colors (same hues as Stack's blocks) ─────────────────── */
[data-v="1"] { --vcol: #ef4444; --vcol-deep: #b91c1c; }
[data-v="2"] { --vcol: #f97316; --vcol-deep: #c2410c; }
[data-v="3"] { --vcol: #facc15; --vcol-deep: #ca8a04; }
[data-v="4"] { --vcol: #84cc16; --vcol-deep: #4d7c0f; }
[data-v="5"] { --vcol: #22c55e; --vcol-deep: #15803d; }
[data-v="6"] { --vcol: #06b6d4; --vcol-deep: #0e7490; }
[data-v="7"] { --vcol: #3b82f6; --vcol-deep: #1d4ed8; }
[data-v="8"] { --vcol: #a855f7; --vcol-deep: #7e22ce; }
[data-v="9"] { --vcol: #ec4899; --vcol-deep: #be185d; }

/* ── HUD ───────────────────────────────────────────────────────────────── */
.hud {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
  flex: 0 0 auto;
  z-index: 20;
}

.hud-back {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 999px;
  background: #fff;
  color: #5b3a1e;
  font-size: 22px;
  text-decoration: none;
  box-shadow: 0 2px 0 rgba(91, 58, 30, 0.18);
}

.target-pill {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin: 0 auto;
  padding: 6px 22px;
  border-radius: 999px;
  background: #fff;
  box-shadow: 0 3px 0 rgba(91, 58, 30, 0.18);
}
.target-label {
  font-size: 18px;
  font-weight: 600;
  color: #a3764a;
}
.target-num {
  font-size: 34px;
  font-weight: 700;
  color: #d97706;
  line-height: 1;
}
.target-pill--bump { animation: pill-bump 600ms cubic-bezier(0.3, 1.6, 0.5, 1); }
@keyframes pill-bump {
  0% { transform: scale(1); }
  35% { transform: scale(1.22); }
  100% { transform: scale(1); }
}

.score-pill {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 8px 16px;
  border-radius: 999px;
  background: #fff;
  box-shadow: 0 2px 0 rgba(91, 58, 30, 0.18);
}
.score-icon { color: #f59e0b; font-size: 18px; }
.pill-num { font-size: 22px; font-weight: 700; }
.best-mini {
  display: flex;
  align-items: baseline;
  gap: 4px;
  font-size: 12px;
  color: #a3764a;
  border-left: 2px solid #f3e0c8;
  padding-left: 8px;
}
.best-num { font-weight: 700; font-size: 15px; }

/* ── Equation banner (running totals) ──────────────────────────────────── */
.eq-banner[hidden], .overlay[hidden] { display: none; }

.eq-banner {
  position: fixed;
  top: calc(70px + env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 60;
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 10px 26px;
  border-radius: 22px;
  background: #fff;
  box-shadow: 0 8px 24px rgba(91, 58, 30, 0.28);
  font-size: 44px;
  font-weight: 700;
  white-space: nowrap;
}
.eq-sub { color: #a3764a; }
.eq-op { color: #cbb091; font-size: 34px; }
.eq-n { color: var(--vcol-deep, #5b3a1e); }
.eq-sum { color: #5b3a1e; }
.eq-sum--final {
  color: #d97706;
  font-size: 54px;
  animation: sum-pop 500ms cubic-bezier(0.3, 1.7, 0.5, 1);
}
@keyframes sum-pop {
  0% { transform: scale(0.4); }
  60% { transform: scale(1.25); }
  100% { transform: scale(1); }
}

/* ── Stage layout ──────────────────────────────────────────────────────── */
.stage {
  flex: 1 1 auto;
  display: flex;
  align-items: stretch;
  gap: 10px;
  padding: 6px 14px calc(10px + env(safe-area-inset-bottom));
  min-height: 0;
}

/* ── Dino ──────────────────────────────────────────────────────────────── */
.dino {
  flex: 0 0 150px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  padding-bottom: 24px;
  position: relative;
}
.dino-face {
  font-size: 112px;
  line-height: 1;
  transform: scaleX(-1);          /* face the plates */
  filter: drop-shadow(0 6px 6px rgba(91, 58, 30, 0.25));
  transition: transform 200ms ease;
}
.dino--chomp .dino-face {
  animation: chomp 520ms cubic-bezier(0.3, 1.4, 0.5, 1);
}
@keyframes chomp {
  0% { transform: scaleX(-1) rotate(0deg); }
  30% { transform: scaleX(-1) rotate(-14deg) translateX(-14px) scale(1.18); }
  60% { transform: scaleX(-1) rotate(6deg) scale(1.05); }
  100% { transform: scaleX(-1) rotate(0deg); }
}
.dino-bubble {
  opacity: 0;
  transform: translateY(8px) scale(0.85);
  transition: opacity 180ms ease, transform 220ms cubic-bezier(0.3, 1.5, 0.5, 1);
  background: #fff;
  color: #5b3a1e;
  font-size: 17px;
  font-weight: 600;
  padding: 8px 14px;
  border-radius: 16px;
  box-shadow: 0 3px 10px rgba(91, 58, 30, 0.2);
  margin-bottom: 10px;
  position: relative;
  max-width: 150px;
  text-align: center;
  pointer-events: none;
}
.dino-bubble::after {
  content: '';
  position: absolute;
  bottom: -7px;
  left: 50%;
  transform: translateX(-50%);
  border: 8px solid transparent;
  border-top-color: #fff;
  border-bottom: 0;
}
.dino-bubble--show { opacity: 1; transform: translateY(0) scale(1); }

/* ── Table + plates ────────────────────────────────────────────────────── */
.table-area {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-width: 0;
  max-width: 820px;
  margin: 0 auto;
  width: 100%;
  position: relative;
}
/* Morning sun, fixed in the sky behind the plates. */
.table-area::before {
  content: '☀️';
  position: absolute;
  top: 4%;
  right: 6%;
  font-size: 54px;
  opacity: 0.85;
  pointer-events: none;
  filter: drop-shadow(0 0 24px rgba(251, 191, 36, 0.7));
}
.table-area::after {
  content: '☁️';
  position: absolute;
  top: 16%;
  left: -4%;
  font-size: 44px;
  opacity: 0.7;
  pointer-events: none;
  animation: cloud-drift 70s linear infinite;
}
@keyframes cloud-drift {
  from { transform: translateX(0); }
  to { transform: translateX(80vw); }
}
.plates {
  flex: 1 1 auto;
  display: flex;
  gap: 12px;
  align-items: stretch;
  min-height: 0;
  padding: 4px 4px 0;
}
.tabletop {
  flex: 0 0 26px;
  border-radius: 10px 10px 6px 6px;
  background:
    repeating-linear-gradient(90deg,
      rgba(120, 72, 32, 0.12) 0 3px, transparent 3px 64px),
    linear-gradient(180deg, #c98a4e 0%, #b5723a 60%, #9c5f2e 100%);
  box-shadow: 0 4px 0 rgba(91, 58, 30, 0.25);
}

.plate-col {
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  min-width: 0;
  cursor: pointer;
  border-radius: 14px;
  position: relative;
}
.plate-col:active .plate-dish { transform: scaleX(0.96); }

.plate-stack {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column-reverse;  /* first child = bottom of the stack */
  align-items: center;
  gap: 2px;
  min-height: 0;
}

.plate-dish {
  flex: 0 0 16px;
  width: 92%;
  margin: 0 auto 2px;
  border-radius: 50% / 90%;
  background: linear-gradient(180deg, #ffffff 0%, #ece2d2 70%, #d9cab2 100%);
  box-shadow: 0 3px 0 rgba(91, 58, 30, 0.22);
  transition: transform 120ms ease, box-shadow 200ms ease;
}
.plate-col--danger .plate-dish { box-shadow: 0 3px 0 rgba(220, 38, 38, 0.55), 0 0 14px rgba(239, 68, 68, 0.35); }
.plate-col--full { cursor: default; }
.plate-col--full .plate-dish { background: linear-gradient(180deg, #e8dccb 0%, #d4c3a8 100%); }

.plate-col--wobble { animation: wobble 360ms ease; }
@keyframes wobble {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  50% { transform: translateX(6px); }
  75% { transform: translateX(-3px); }
}

/* ── Pancake tiles ─────────────────────────────────────────────────────── */
.tile {
  height: calc(100% / var(--max-h, 7));
  width: 88%;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  border-radius: 44% / 58%;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.5), rgba(255, 255, 255, 0.08) 50%),
    var(--vcol, #999);
  box-shadow:
    0 3px 0 var(--vcol-deep, #555),
    inset 0 -2px 4px rgba(0, 0, 0, 0.12);
  color: #fff;
  font-weight: 700;
  font-size: clamp(18px, 3.4vh, 30px);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
  position: relative;
  z-index: 1;
}

.tile--landed { animation: land-squash 240ms cubic-bezier(0.3, 1.5, 0.5, 1); }
@keyframes land-squash {
  0% { transform: scale(1.12, 0.78); }
  60% { transform: scale(0.94, 1.08); }
  100% { transform: scale(1, 1); }
}

/* Didactic states */
.stage--dimmed .tile:not(.tile--lit):not(.tile--lit-pending),
.stage--dimmed .plate-dish,
.stage--dimmed .tabletop,
.stage--dimmed .deal-panel {
  filter: brightness(0.55) saturate(0.6);
  transition: filter 220ms ease;
}
.tile--lit-pending {
  box-shadow:
    0 0 0 3px #fbbf24,
    0 3px 0 var(--vcol-deep, #555);
}
.tile--lit {
  filter: brightness(1.25) saturate(1.3);
  transform: scale(1.1);
  z-index: 5;
  box-shadow:
    0 0 0 4px #fbbf24,
    0 0 22px rgba(251, 191, 36, 0.8),
    0 3px 0 var(--vcol-deep, #555);
  transition: transform 160ms ease;
}

/* ── Deal panel ────────────────────────────────────────────────────────── */
.deal-panel {
  flex: 0 0 170px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  background: rgba(255, 255, 255, 0.55);
  border-radius: 22px;
  padding: 14px;
  box-shadow: 0 3px 0 rgba(91, 58, 30, 0.12);
}

.deal-current {
  width: 120px;
  height: 84px;
  display: grid;
  place-items: center;
}
.deal-current .tile {
  width: 116px;
  height: 76px;
  font-size: 40px;
  animation: hand-bob 2.4s ease-in-out infinite;
}
@keyframes hand-bob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}

.deal-row {
  display: flex;
  gap: 14px;
  align-items: flex-end;
}
.deal-label {
  font-size: 12px;
  font-weight: 600;
  color: #a3764a;
  text-align: center;
  margin-bottom: 4px;
}
.deal-next, .hold-slot {
  width: 72px;
  height: 52px;
  display: grid;
  place-items: center;
}
.deal-next .tile, .hold-slot .tile {
  width: 66px;
  height: 46px;
  font-size: 23px;
}
.hold-slot {
  border: 2.5px dashed #d3b890;
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.5);
  padding: 0;
  cursor: pointer;
  font: inherit;
}
.hold-slot--filled { border-style: solid; border-color: #c9a36d; }
.hold-slot:active { transform: scale(0.93); }

/* ── Particles ─────────────────────────────────────────────────────────── */
.particle {
  position: fixed;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--vcol, #fbbf24);
  pointer-events: none;
  z-index: 70;
  animation: particle-fly 650ms ease-out forwards;
}
@keyframes particle-fly {
  0% { transform: translate(0, 0) scale(1); opacity: 1; }
  100% { transform: translate(var(--dx, 0), var(--dy, -40px)) scale(0.3); opacity: 0; }
}

/* ── Game over overlay ─────────────────────────────────────────────────── */
.overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(91, 58, 30, 0.45);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  z-index: 100;
}
.overlay-card {
  background: #fffaf2;
  border-radius: 26px;
  padding: 28px 36px;
  text-align: center;
  box-shadow: 0 18px 50px rgba(60, 35, 12, 0.4);
  animation: card-in 360ms cubic-bezier(0.3, 1.4, 0.5, 1);
}
@keyframes card-in {
  0% { transform: scale(0.7) translateY(30px); opacity: 0; }
  100% { transform: scale(1) translateY(0); opacity: 1; }
}
.overlay-title { margin: 0 0 4px; font-size: 32px; color: #5b3a1e; }
.overlay-sub { font-size: 17px; color: #a3764a; margin-bottom: 18px; }
.overlay-sub--new-best { color: #d97706; font-weight: 600; }
.overlay-stats {
  display: flex;
  gap: 26px;
  justify-content: center;
  margin-bottom: 22px;
}
.stat-label { font-size: 13px; font-weight: 600; color: #a3764a; }
.stat-num { font-size: 30px; font-weight: 700; color: #5b3a1e; }

.big-btn {
  font: inherit;
  font-size: 21px;
  font-weight: 700;
  color: #fff;
  background: linear-gradient(180deg, #fbbf24, #d97706);
  border: none;
  border-radius: 999px;
  padding: 13px 38px;
  cursor: pointer;
  box-shadow: 0 4px 0 #b45309;
}
.big-btn:active { transform: translateY(2px); box-shadow: 0 2px 0 #b45309; }

/* ── Portrait: deal panel drops to the bottom, dino shrinks ────────────── */
@media (orientation: portrait) {
  .stage { flex-direction: column; }
  .dino {
    position: absolute;
    top: calc(58px + env(safe-area-inset-top));
    left: 8px;
    flex: none;
    padding-bottom: 0;
    z-index: 15;
  }
  .dino-face { font-size: 56px; }
  .deal-panel {
    flex: 0 0 auto;
    flex-direction: row;
    justify-content: center;
    gap: 22px;
    padding: 10px 14px;
  }
  .deal-current { width: 100px; height: 72px; }
  .deal-current .tile { width: 96px; height: 64px; font-size: 34px; }
  .deal-row { align-items: flex-end; }
}

@media (max-width: 600px) {
  .dino-bubble { display: none; }
}
