:root {
  --bg: #fff4e6;
  --bg2: #ffe9d2;
  --ink: #2b1d14;
  --muted: #8a6f5c;
  --card: #fffaf3;
  --accent: #ff6b3d;
  --accent-ink: #fff;
  --good: #1fa37a;
  --bad: #e2433b;
  --gap: clamp(4px, 1.2vw, 7px);
  --cell: 44px;
  --radius: 14px;
  --font: ui-rounded, "SF Pro Rounded", "Nunito", "Segoe UI Variable Display", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  /* Parelles complementàries comparteixen família de color: 1·9, 2·8, 3·7, 4·6, 5 */
  --d1: #ffd8cc; --d9: #e8553a;
  --d2: #ffe7a8; --d8: #e59a12;
  --d3: #c9f0df; --d7: #179b78;
  --d4: #dcd6ff; --d6: #6c5ce7;
  --d5: #ffc2da;
  --t1: #8c2f1b; --t9: #fff; --t2: #7a5200; --t8: #fff; --t3: #0f5c47; --t7: #fff; --t4: #3d2f9e; --t6: #fff; --t5: #9b1f52;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(120% 60% at 50% -10%, #fff 0%, transparent 60%),
    radial-gradient(80% 50% at 100% 100%, #ffd9bf 0%, transparent 60%),
    var(--bg);
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  overflow: hidden;
}
button { font: inherit; color: inherit; }

.app {
  position: fixed; inset: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-columns: minmax(0, 1fr);
  padding: max(10px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  max-width: 760px; margin: 0 auto;
}

/* HUD */
.hud { padding: 2px 2px 8px; }
.hud-row { display: grid; grid-template-columns: 1fr auto 1fr; align-items: end; gap: 8px; }
.stat { display: flex; flex-direction: column; line-height: 1; }
.stat-right { align-items: flex-end; }
.stat-label { font-size: 12px; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); font-weight: 700; }
.stat-value { font-size: clamp(28px, 8vw, 40px); font-weight: 800; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.stat-value.bump { animation: bump .35s ease; }
.stat-value.low { color: var(--bad); }
.combo {
  min-width: 72px; text-align: center; font-weight: 800; font-size: 15px;
  padding: 6px 12px; border-radius: 999px; background: transparent; color: var(--accent);
  opacity: 0; transform: scale(.8); transition: opacity .2s, transform .2s;
}
.combo.on { opacity: 1; transform: scale(1); background: #fff; box-shadow: 0 4px 14px rgba(255,107,61,.25); }
.timebar { margin-top: 10px; height: 8px; border-radius: 99px; background: rgba(43,29,20,.08); overflow: hidden; }
.timebar-fill { height: 100%; width: 100%; border-radius: inherit; background: linear-gradient(90deg, #ffb347, var(--accent)); transform-origin: left; transition: background .3s; }
.timebar-fill.low { background: linear-gradient(90deg, #ff7a6b, var(--bad)); }
.timebar-fill.plus { animation: flashbar .5s; }

/* Tauler */
.stage { position: relative; display: grid; place-items: center; min-height: 0; min-width: 0; overflow: hidden; }
.board-wrap { position: relative; touch-action: none; }
.board {
  display: grid;
  grid-template-columns: repeat(var(--cols, 8), var(--cell));
  grid-auto-rows: var(--cell);
  gap: var(--gap);
  padding: var(--gap);
  border-radius: calc(var(--radius) + 6px);
  background: rgba(255,255,255,.55);
  box-shadow: inset 0 1px 0 #fff, 0 10px 30px rgba(120,60,20,.10);
  touch-action: none;
  cursor: pointer;
}
.tile {
  position: relative;
  display: grid; place-items: center;
  border-radius: var(--radius);
  font-weight: 800;
  font-size: calc(var(--cell) * .5);
  font-variant-numeric: tabular-nums;
  background: var(--tile-bg);
  color: var(--tile-ink);
  box-shadow: inset 0 -3px 0 rgba(0,0,0,.10), 0 2px 4px rgba(90,40,10,.10);
  transition: transform .12s ease, box-shadow .12s ease, filter .12s;
  will-change: transform;
  z-index: 2;
}
.tile[data-v="1"] { --tile-bg: var(--d1); --tile-ink: var(--t1); }
.tile[data-v="2"] { --tile-bg: var(--d2); --tile-ink: var(--t2); }
.tile[data-v="3"] { --tile-bg: var(--d3); --tile-ink: var(--t3); }
.tile[data-v="4"] { --tile-bg: var(--d4); --tile-ink: var(--t4); }
.tile[data-v="5"] { --tile-bg: var(--d5); --tile-ink: var(--t5); }
.tile[data-v="6"] { --tile-bg: var(--d6); --tile-ink: var(--t6); }
.tile[data-v="7"] { --tile-bg: var(--d7); --tile-ink: var(--t7); }
.tile[data-v="8"] { --tile-bg: var(--d8); --tile-ink: var(--t8); }
.tile[data-v="9"] { --tile-bg: var(--d9); --tile-ink: var(--t9); }

.tile.sel { transform: scale(.9); box-shadow: 0 0 0 3px #fff, 0 0 0 6px var(--ink); z-index: 3; }
.tile.sel.ok { box-shadow: 0 0 0 3px #fff, 0 0 0 6px var(--good); }
.tile.sel.over { box-shadow: 0 0 0 3px #fff, 0 0 0 6px var(--bad); filter: saturate(.6); }
.tile.sel .ord { position: absolute; top: 3px; right: 6px; font-size: 10px; opacity: .7; }
.tile.hint { animation: hint 1.1s ease-in-out infinite; }
.tile.fall { animation: fall var(--dur, .32s) cubic-bezier(.3,1.35,.5,1) both; }
.tile.clearing { animation: pop .24s ease-in forwards; }
.board.shake { animation: shake .36s; }

.path-svg { position: absolute; inset: 0; pointer-events: none; z-index: 1; overflow: visible; }
.path-svg polyline { fill: none; stroke: rgba(43,29,20,.55); stroke-width: 10; stroke-linecap: round; stroke-linejoin: round; }
.path-svg.ok polyline { stroke: var(--good); }
.path-svg.over polyline { stroke: var(--bad); }

/* Efectes */
.fx { position: absolute; inset: 0; pointer-events: none; z-index: 5; }
.float {
  position: absolute; transform: translate(-50%, -50%);
  font-weight: 900; font-size: 26px; color: var(--ink);
  text-shadow: 0 2px 0 #fff, 0 0 12px #fff;
  animation: floatUp 1s ease-out forwards; white-space: nowrap;
}
.float .lbl { display: block; font-size: 15px; color: var(--accent); text-align: center; }
.float.big { font-size: 34px; }
.burst { position: absolute; width: 10px; height: 10px; border-radius: 50%; animation: burst .55s ease-out forwards; }

/* Dock */
.dock { display: grid; grid-template-columns: 48px 1fr 48px; align-items: center; gap: 10px; padding-top: 10px; }
.icon-btn {
  width: 48px; height: 48px; border-radius: 16px; border: 0; background: #fff;
  box-shadow: 0 3px 10px rgba(90,40,10,.12); display: grid; place-items: center; cursor: pointer;
}
.icon-btn svg { width: 22px; height: 22px; fill: var(--ink); stroke: var(--ink); }
.icon-btn:active { transform: scale(.94); }
#soundBtn .wave { display: none; } #soundBtn.on .wave { display: inline; } #soundBtn.on .mute { display: none; }
.sum {
  height: 48px; border-radius: 16px; background: #fff; display: grid; place-items: center;
  font-weight: 800; font-size: 18px; box-shadow: 0 3px 10px rgba(90,40,10,.12); padding: 0 12px;
  font-variant-numeric: tabular-nums; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
  transition: background .15s, color .15s;
}
.sum .sum-expr { overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.sum.idle { color: var(--muted); font-weight: 600; font-size: 15px; }
.sum.ok { background: var(--good); color: #fff; }
.sum.over { background: var(--bad); color: #fff; }

/* Pantalles */
.screen {
  position: fixed; inset: 0; z-index: 20; display: grid; place-items: center; padding: 20px;
  background: rgba(255,244,230,.82); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  animation: fadeIn .25s ease;
}
.screen.hidden { display: none; }
.card {
  width: min(420px, 100%); background: var(--card); border-radius: 28px; padding: 28px 24px 22px;
  box-shadow: 0 24px 60px rgba(120,60,20,.18), inset 0 1px 0 #fff; text-align: center;
  animation: rise .35s cubic-bezier(.2,1.2,.4,1);
}
.logo { display: inline-flex; gap: 8px; margin-bottom: 6px; }
.logo span {
  width: 64px; height: 64px; border-radius: 18px; display: grid; place-items: center;
  font-size: 38px; font-weight: 900; box-shadow: inset 0 -4px 0 rgba(0,0,0,.12);
}
.logo span:first-child { background: var(--d9); color: #fff; transform: rotate(-6deg); }
.logo span:last-child { background: var(--d3); color: var(--t3); transform: rotate(5deg); }
h1 { font-size: 44px; margin: 8px 0 6px; letter-spacing: -.03em; }
h2 { margin: 4px 0 16px; font-size: 28px; }
.lede { margin: 0 0 14px; font-size: 17px; line-height: 1.4; color: #5b4535; }
.rules { list-style: none; padding: 0; margin: 0 0 20px; text-align: left; display: grid; gap: 8px; font-size: 15px; color: #5b4535; }
.rules li { background: #fff; border-radius: 14px; padding: 10px 12px; }
.primary {
  display: block; width: 100%; border: 0; border-radius: 18px; padding: 16px; cursor: pointer;
  background: var(--accent); color: var(--accent-ink); font-size: 20px; font-weight: 800;
  box-shadow: 0 6px 0 #d24c22, 0 12px 24px rgba(255,107,61,.35);
  transition: transform .1s, box-shadow .1s;
}
.primary:active { transform: translateY(4px); box-shadow: 0 2px 0 #d24c22, 0 6px 14px rgba(255,107,61,.3); }
.primary:focus-visible, .ghost:focus-visible, .icon-btn:focus-visible { outline: 3px solid rgba(43,29,20,.35); outline-offset: 3px; }
.ghost { display: block; width: 100%; margin-top: 12px; border: 0; background: transparent; padding: 12px; font-weight: 700; color: var(--muted); cursor: pointer; font-size: 16px; }
.best-line { margin: 14px 0 0; color: var(--muted); font-weight: 700; min-height: 1.2em; }
.toggle { display: inline-flex; align-items: center; gap: 8px; margin-top: 10px; color: var(--muted); font-weight: 700; }
.kicker { margin: 0; color: var(--accent); font-weight: 800; text-transform: uppercase; letter-spacing: .1em; font-size: 14px; }
.final-score { font-size: 64px; margin: 4px 0 2px; letter-spacing: -.04em; line-height: 1; font-variant-numeric: tabular-nums; }
.final-score small { display: block; font-size: 15px; color: var(--muted); letter-spacing: .08em; text-transform: uppercase; margin-top: 6px; }
.record { display: inline-block; margin: 8px 0 0; padding: 6px 14px; border-radius: 999px; background: #ffe066; font-weight: 800; animation: bump .6s ease 2; }
.record.hidden { display: none; }
.summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 18px 0 20px; }
.summary div { background: #fff; border-radius: 14px; padding: 10px 4px; }
.summary dt { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; font-weight: 700; }
.summary dd { margin: 4px 0 0; font-size: 20px; font-weight: 800; font-variant-numeric: tabular-nums; }

.confetti { position: fixed; inset: 0; pointer-events: none; z-index: 30; width: 100%; height: 100%; }
.toast {
  position: fixed; left: 50%; bottom: calc(80px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 12px; font-weight: 700; opacity: 0;
  transition: opacity .2s, transform .2s; z-index: 40; pointer-events: none;
}
.toast.on { opacity: 1; transform: translateX(-50%) translateY(0); }

@keyframes bump { 0%{transform:scale(1)} 40%{transform:scale(1.18)} 100%{transform:scale(1)} }
@keyframes pop { 0%{transform:scale(.9)} 50%{transform:scale(1.15)} 100%{transform:scale(0) rotate(12deg); opacity:0} }
@keyframes fall { from { transform: translateY(calc(var(--fall) * -1 * (var(--cell) + var(--gap)))); } to { transform: none; } }
@keyframes shake { 0%,100%{transform:none} 20%{transform:translateX(-7px)} 40%{transform:translateX(6px)} 60%{transform:translateX(-4px)} 80%{transform:translateX(3px)} }
@keyframes hint { 0%,100%{ transform: scale(1); } 50%{ transform: scale(.88); box-shadow: 0 0 0 3px #fff, 0 0 0 6px var(--accent); } }
@keyframes floatUp { 0%{ opacity:0; transform: translate(-50%,-30%) scale(.6);} 15%{opacity:1; transform: translate(-50%,-60%) scale(1.1);} 100%{ opacity:0; transform: translate(-50%,-180%) scale(1);} }
@keyframes burst { from { transform: translate(-50%,-50%) scale(1); opacity: 1; } to { transform: translate(calc(-50% + var(--dx)), calc(-50% + var(--dy))) scale(.2); opacity: 0; } }
@keyframes fadeIn { from{opacity:0} to{opacity:1} }
@keyframes rise { from{ transform: translateY(20px) scale(.97); opacity:0 } to { transform:none; opacity:1 } }
@keyframes flashbar { 0%{filter:brightness(1)} 30%{filter:brightness(1.6) saturate(1.4)} 100%{filter:brightness(1)} }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; }
  .tile.hint { box-shadow: 0 0 0 3px #fff, 0 0 0 6px var(--accent); }
}
@media (hover: hover) {
  .tile:hover { filter: brightness(1.04); }
  .primary:hover { filter: brightness(1.05); }
}
