/* Blocs del Temple
   Concepte: una sala de temple vista des de dalt. Terra de gres càlid,
   murs de basalt verdós i blocs gravats amb glifs; la interfície és pedra fosca amb or. */
:root {
  color-scheme: dark;
  /* Color */
  --night: #121b1a;        /* fons: pedra de jungla a l'ombra */
  --basalt: #24302e;       /* murs */
  --basalt-hi: #3a4a44;    /* aresta il·luminada dels murs */
  --sand: #cfa96e;         /* terra del temple */
  --sand-lo: #b08a52;      /* junts del terra */
  --gold: #e3b84f;         /* accent: or */
  --ink: #f1e8d4;          /* text */
  --muted: #a5b1a3;        /* text secundari */
  --panel: #1b2624;        /* superfícies d'interfície */
  --line: #34433f;
  /* Colors dels blocs */
  --k-R: #c8472e;
  --k-B: #2f76b8;
  --k-G: #3d9a50;
  --k-Y: #e2b432;
  --k-S: #8b867a;
  /* Tipografia */
  --display: "Marcellus SC", "Trajan Pro", Georgia, serif;
  --body: "Alegreya Sans", "Segoe UI", system-ui, sans-serif;
  /* Mesures */
  --c: 56px;
  --gutter: 16px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background:
    radial-gradient(120% 70% at 50% 0%, rgba(227, 184, 79, .08), transparent 60%),
    var(--night);
  color: var(--ink);
  font: 16px/1.45 var(--body);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }

.app {
  height: 100%;
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: 900px;
  margin: 0 auto;
  padding-inline: var(--gutter);
  padding-block: 12px;
}

/* Capçalera */
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px 20px;
}
.brand {
  margin: 0;
  font: 400 clamp(1.35rem, 4.2vw, 2rem)/1 var(--display);
  letter-spacing: .06em;
  color: var(--gold);
  display: flex;
  align-items: center;
  gap: 10px;
  text-wrap: balance;
}
.brand-mark {
  width: 1.05em; height: 1.05em;
  background: var(--gold);
  clip-path: polygon(50% 0, 100% 100%, 0 100%);
  box-shadow: inset 0 -4px 0 rgba(0,0,0,.25);
  flex: none;
}
.stats {
  display: flex;
  gap: 18px;
  margin: 0;
}
.stats div { display: flex; flex-direction: column; align-items: flex-end; }
.stats dt {
  font-size: .68rem;
  text-transform: uppercase;
  letter-spacing: .14em;
  color: var(--muted);
}
.stats dd {
  margin: 0;
  font: 400 1.35rem/1.1 var(--display);
  font-variant-numeric: tabular-nums;
}
.stats small { font-size: .75em; color: var(--muted); }
.stats .good { color: var(--gold); }

/* Escenari i tauler */
.stage {
  flex: 1;
  min-height: 0;
  position: relative;
  display: grid;
  place-items: center;
}
.board {
  position: relative;
  border-radius: 10px;
  box-shadow:
    0 0 0 3px #0c1312,
    0 0 0 5px rgba(227, 184, 79, .35),
    0 24px 60px rgba(0,0,0,.55);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  overflow: hidden;
}
.tiles { display: grid; }
.t { width: var(--c); height: var(--c); position: relative; }
.t.floor {
  background:
    linear-gradient(135deg, rgba(255,255,255,.10), transparent 45%, rgba(0,0,0,.06)),
    var(--sand);
  box-shadow: inset 0 0 0 1px var(--sand-lo), inset 1px 1px 0 1px rgba(255,240,210,.25);
}
.t.floor.alt { background-color: #c9a267; }
.t.wall {
  background:
    linear-gradient(180deg, var(--basalt-hi), var(--basalt) 40%, #1b2523);
  box-shadow: inset 0 2px 0 rgba(255,255,255,.07), inset 0 -3px 0 rgba(0,0,0,.35), inset 0 0 0 1px rgba(0,0,0,.4);
}
.t.wall.inner {
  background: linear-gradient(180deg, #4a5a52, var(--basalt) 55%, #19221f);
  box-shadow: inset 0 3px 0 rgba(255,255,255,.12), inset 0 -5px 0 rgba(0,0,0,.45), 0 0 0 1px rgba(0,0,0,.4);
}
.t.exit::before {
  content: "";
  position: absolute;
  inset: 10%;
  border-radius: 12%;
  background: color-mix(in srgb, var(--k) 30%, var(--sand-lo));
  box-shadow: inset 0 3px 5px rgba(0,0,0,.45), 0 0 0 2px color-mix(in srgb, var(--k) 70%, #000);
}
.t.exit svg {
  position: absolute;
  inset: 25%;
  width: 50%;
  height: 50%;
  stroke: color-mix(in srgb, var(--k) 75%, #000);
  opacity: .9;
}

/* Capítol II: les cambres profundes, de pedra grisa i freda */
.board.ch2 { --sand: #b7b3a0; --sand-lo: #948f78; }
.board.ch2 .t.floor.alt { background-color: #b1ad99; }
.board.ch2 .t.wall { background: linear-gradient(180deg, #353a46, #22262f 40%, #171a21); }
.board.ch2 .t.wall.inner { background: linear-gradient(180deg, #4a5060, #262b35 55%, #161920); }

/* Blocs */
.pieces { position: absolute; inset: 0; pointer-events: none; }
.blk {
  position: absolute;
  left: 0; top: 0;
  width: var(--c); height: var(--c);
  padding: calc(var(--c) * .06);
  transition: transform var(--dur, 0ms) cubic-bezier(.45, .05, .55, 1);
  pointer-events: auto;
  cursor: grab;
  z-index: 2;
}
.blk:active { cursor: grabbing; }
.blk .face {
  width: 100%; height: 100%;
  border-radius: 16%;
  display: grid;
  place-items: center;
  background:
    linear-gradient(160deg, rgba(255,255,255,.28), transparent 40%, rgba(0,0,0,.22)),
    var(--k);
  box-shadow:
    inset 0 3px 0 rgba(255,255,255,.28),
    inset 0 -5px 0 rgba(0,0,0,.30),
    0 4px 8px rgba(0,0,0,.45);
  transition: box-shadow .25s, outline-color .15s;
  outline: 3px solid transparent;
  outline-offset: 2px;
}
.blk .face svg { width: 58%; height: 58%; stroke: rgba(255,250,235,.92); }
.blk.stone .face svg { stroke: rgba(40,36,30,.45); }
.blk.sel { z-index: 3; }
.blk.sel .face { outline-color: var(--ink); }
.blk.home .face {
  box-shadow:
    inset 0 3px 0 rgba(255,255,255,.35),
    inset 0 -5px 0 rgba(0,0,0,.25),
    0 0 0 2px var(--gold),
    0 0 calc(var(--c) * .4) color-mix(in srgb, var(--k) 70%, var(--gold));
}
.blk.hint .face { animation: hintPulse 1s ease-in-out infinite; }
@keyframes hintPulse {
  50% { box-shadow: 0 0 0 4px var(--gold), 0 0 calc(var(--c) * .5) var(--gold); }
}
.blk.bump .face { animation: bump .22s ease-out; }
@keyframes bump {
  40% { transform: translate(calc(var(--bx) * 6px), calc(var(--by) * 6px)); }
}
.board.won .blk .face { animation: glow 1.2s ease-in-out infinite alternate; }
@keyframes glow { to { filter: brightness(1.25); } }

/* Fletxes al voltant del bloc seleccionat */
.arrows {
  position: absolute;
  left: 0; top: 0;
  width: var(--c); height: var(--c);
  pointer-events: none;
  z-index: 4;
  transition: transform var(--dur, 0ms) cubic-bezier(.45, .05, .55, 1);
}
.arrow {
  position: absolute;
  width: calc(var(--c) * .5);
  height: calc(var(--c) * .5);
  min-width: 26px; min-height: 26px;
  border: 0;
  border-radius: 50%;
  padding: 0;
  display: grid;
  place-items: center;
  background: rgba(18, 27, 26, .78);
  color: var(--gold);
  cursor: pointer;
  pointer-events: auto;
  box-shadow: 0 0 0 1.5px rgba(227,184,79,.55);
}
.arrow:hover { background: rgba(18,27,26,.95); box-shadow: 0 0 0 2px var(--gold); }
.arrow svg { width: 60%; height: 60%; fill: currentColor; }
/* Cada fletxa queda centrada a la casella veïna */
.arrow { transform: translate(-50%, -50%); }
.arrow[data-dir="0"] { left: 50%; top: -50%; }
.arrow[data-dir="2"] { left: 50%; top: 150%; }
.arrow[data-dir="1"] { left: 150%; top: 50%; }
.arrow[data-dir="3"] { left: -50%; top: 50%; }
.arrow[disabled] { display: none; }

/* Missatges */
.toast {
  position: absolute;
  left: 50%;
  bottom: 4px;
  transform: translate(-50%, 8px);
  width: max-content;
  max-width: min(92%, 460px);
  padding: 8px 14px;
  border-radius: 8px;
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--ink);
  font-size: .95rem;
  text-align: center;
  opacity: 0;
  transition: opacity .2s, transform .2s;
  pointer-events: none;
  z-index: 10;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Controls */
.controls {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 8px;
}
.controls button {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 9px 14px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--panel);
  cursor: pointer;
  font-weight: 500;
  transition: border-color .15s, background .15s;
}
.controls button:hover { border-color: var(--gold); }
.controls button:disabled { opacity: .45; cursor: default; border-color: var(--line); }
.controls svg {
  width: 18px; height: 18px;
  fill: none; stroke: var(--gold);
  stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}
#btnSound .mute { display: none; }
#btnSound[aria-pressed="false"] .mute { display: inline; }
#btnSound[aria-pressed="false"] .wave { display: none; }
#btnMusic .mute { display: none; }
#btnMusic[aria-pressed="false"] .mute { display: inline; }
#btnMusic[aria-pressed="false"] svg { opacity: .6; }
#btnDemo .stop { display: none; }
#btnDemo[aria-pressed="true"] .stop { display: inline; }
#btnDemo[aria-pressed="true"] .play { display: none; }
#btnDemo[aria-pressed="true"] { border-color: var(--gold); background: #2a2a1c; }
#btnDemo svg .play, #btnDemo svg .stop { fill: var(--gold); stroke: none; }

/* Mode demo */
.board.demo::after {
  content: "Demo";
  position: absolute;
  top: 6px;
  left: 50%;
  transform: translateX(-50%);
  padding: 5px 12px 4px;
  border-radius: 999px;
  background: var(--gold);
  color: #1d1606;
  font: 400 .8rem/1 var(--display);
  letter-spacing: .2em;
  text-transform: uppercase;
  z-index: 6;
  pointer-events: none;
}
.board.demo .blk { cursor: default; }
.board.demo .arrow { pointer-events: none; opacity: .55; }
.board.demo .arrow.demo-dir {
  opacity: 1;
  background: var(--gold);
  color: #1d1606;
  box-shadow: 0 0 0 2px var(--gold), 0 0 14px var(--gold);
}

/* Glifs */
.glyph { fill: none; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }

/* Diàlegs */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(6, 10, 9, .66);
  backdrop-filter: blur(3px);
  animation: fade .2s ease-out;
}
@keyframes fade { from { opacity: 0; } }
.dialog {
  width: min(100%, 440px);
  max-height: 100%;
  overflow: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-top: 3px solid var(--gold);
  border-radius: 12px;
  padding: 22px 22px 20px;
  box-shadow: 0 30px 80px rgba(0,0,0,.6);
}
.dialog.wide { width: min(100%, 620px); }
.dialog h2 {
  margin: 0;
  font: 400 1.6rem/1.15 var(--display);
  letter-spacing: .03em;
  color: var(--gold);
  text-wrap: balance;
}
.dialog h3 {
  margin: 18px 0 6px;
  font: 400 1rem var(--display);
  letter-spacing: .08em;
  color: var(--gold);
}
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.close {
  border: 0; background: none; cursor: pointer;
  font-size: 1.8rem; line-height: 1; color: var(--muted);
  padding: 0 4px;
}
.close:hover { color: var(--ink); }
.eyebrow {
  margin: 0 0 4px;
  font-size: .72rem;
  text-transform: uppercase;
  letter-spacing: .18em;
  color: var(--muted);
}
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; flex-wrap: wrap; }
.dialog-actions button {
  padding: 10px 18px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 700;
}
.primary { background: var(--gold); color: #1d1606 !important; border: 1px solid var(--gold); }
.primary:hover { filter: brightness(1.08); }
.ghost { background: transparent; border: 1px solid var(--line); }
.ghost:hover { border-color: var(--gold); }

.stars { display: flex; gap: 8px; margin: 16px 0 8px; }
.stars svg { width: 40px; height: 40px; }
.stars .on { fill: var(--gold); }
.stars .off { fill: var(--line); }
.stars svg { animation: pop .35s backwards; }
.stars svg:nth-child(2) { animation-delay: .12s; }
.stars svg:nth-child(3) { animation-delay: .24s; }
@keyframes pop { from { transform: scale(.3); opacity: 0; } }
.win-text { margin: 0; color: var(--muted); max-width: 60ch; }
.win-text b { color: var(--ink); font-variant-numeric: tabular-nums; }

.rules, .keys { margin: 14px 0 0; padding: 0; list-style: none; display: grid; gap: 9px; }
.rules li { color: var(--muted); max-width: 62ch; }
.rules b { color: var(--ink); }
.keys li { display: flex; align-items: center; flex-wrap: wrap; gap: 5px; color: var(--muted); }
.keys span { margin-right: 8px; }
kbd {
  font: 600 .78rem/1 var(--body);
  min-width: 24px;
  padding: 5px 6px;
  text-align: center;
  border-radius: 5px;
  background: var(--night);
  border: 1px solid var(--line);
  border-bottom-width: 3px;
  color: var(--ink);
}

.menu-sub { margin: 6px 0 4px; color: var(--muted); }
.chapters { display: grid; gap: 6px; }
.chapter h3 {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 4px 10px;
  margin: 14px 0 10px;
  font: 400 1.15rem/1.2 var(--display);
  letter-spacing: .03em;
  color: var(--ink);
}
.chapter .ch-num {
  font: 500 .72rem/1 var(--body);
  text-transform: uppercase;
  letter-spacing: .16em;
  color: var(--gold);
}
.chapter h3 small {
  margin-left: auto;
  font: 500 .85rem var(--body);
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.level-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(76px, 1fr));
  gap: 10px;
}
.lvl-btn {
  aspect-ratio: 1;
  max-width: 100%;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: linear-gradient(180deg, #2a3835, #1f2a28);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  font-variant-numeric: tabular-nums;
}
.lvl-btn .n { font: 400 1.45rem/1 var(--display); }
.lvl-btn .s { display: flex; gap: 2px; }
.lvl-btn .s svg { width: 13px; height: 13px; }
.lvl-btn .s .on { fill: var(--gold); }
.lvl-btn .s .off { fill: var(--line); }
.lvl-btn.done { border-color: rgba(227,184,79,.45); }
.lvl-btn.current { box-shadow: 0 0 0 2px var(--gold); }
.lvl-btn:hover:not(:disabled) { border-color: var(--gold); }
.lvl-btn:disabled { cursor: not-allowed; opacity: .4; }
.lvl-btn .lock { width: 16px; height: 16px; fill: none; stroke: var(--muted); stroke-width: 2; }

@media (max-width: 560px) {
  .controls button span { display: none; }
  .controls { gap: 6px; }
  .controls button { padding: 10px 9px; }
  .stats { gap: 12px; }
  .stats div { align-items: flex-start; }
  .stats dd { font-size: 1.15rem; }
}
@media (prefers-reduced-motion: reduce) {
  .blk, .arrows { transition: none !important; }
  .blk .face, .stars svg, .overlay { animation: none !important; }
}
