:root{
  --linen:#EFE3CC;
  --parch:#E8D9BB;
  --clay:#B4552F;
  --clay-dark:#8A3E22;
  --olive:#6B7333;
  --deep:#3B2A20;
  --ink:#2A1B12;
  --gold:#F2B33D;
  --crimson:#C7312C;
  --emerald:#2E8C5A;
  --azure:#4C8FD1;
  --stone:#4a3527;
}
*{box-sizing:border-box}
html,body{height:100%}
body{
  margin:0;
  background:#241811;
  color:var(--linen);
  font-family:'Cairo',system-ui,-apple-system,'Segoe UI',sans-serif;
  overflow:hidden;
  -webkit-font-smoothing:antialiased;
  -webkit-tap-highlight-color:transparent;
}
button{font-family:inherit;cursor:pointer;border:0;background:none;color:inherit;user-select:none;-webkit-user-select:none;touch-action:manipulation}

/* paper grain over everything */
body::after{
  content:'';
  position:fixed;
  inset:0;
  pointer-events:none;
  z-index:60;
  opacity:.11;
  mix-blend-mode:overlay;
  background-image:url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='160' height='160'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='3' stitchTiles='stitch'/></filter><rect width='160' height='160' filter='url(%23n)' opacity='0.6'/></svg>");
}

#game{
  position:relative;
  height:100vh;
  height:100dvh;
  display:flex;
  flex-direction:column;
  overflow:hidden;
  background:
    radial-gradient(120% 80% at 50% 0%, #3b2a20 0%, #241811 60%, #170f0a 100%);
}

/* ==================== HUD ==================== */
.hud{
  flex:0 0 auto;
  display:flex;
  align-items:center;
  gap:8px;
  padding:8px 10px 7px;
  background:linear-gradient(#3a2a1e, #251811);
  border-bottom:2px solid #140d08;
  box-shadow:0 2px 10px rgba(0,0,0,.45);
  position:relative;
  z-index:6;
}
.hud__side{flex:1 1 0;min-width:0}
.hud__side--foe{text-align:end}
.hud__name{
  display:flex;align-items:center;gap:5px;
  font-size:12px;font-weight:900;color:var(--linen);
  letter-spacing:0;
  white-space:nowrap;overflow:hidden;text-overflow:ellipsis;
}
.hud__side--foe .hud__name{justify-content:flex-start;flex-direction:row-reverse}
.pip{width:7px;height:7px;border-radius:50%;flex:0 0 auto;box-shadow:0 0 6px currentColor}
.pip--crimson{background:var(--crimson);color:var(--crimson)}
.pip--emerald{background:var(--emerald);color:var(--emerald)}

.bar{
  position:relative;
  height:9px;
  margin-top:4px;
  border-radius:3px;
  background:#150e09;
  overflow:hidden;
  box-shadow:inset 0 1px 2px rgba(0,0,0,.9), 0 0 0 1px rgba(239,227,204,.16);
}
.bar__fill{
  display:block;height:100%;width:100%;
  border-radius:2px;
  transition:width .16s linear;
  box-shadow:0 0 8px rgba(255,190,90,.45);
}
.bar--hp .bar__fill{background:linear-gradient(90deg,#9d231f,#C7312C 55%,#F2B33D)}
.bar--hp.bar--foe .bar__fill{background:linear-gradient(90deg,#1f6b45,#2E8C5A 55%,#9fb6a6)}
.bar--en{height:6px}
.bar--en .bar__fill{background:linear-gradient(90deg,#2e5c86,#4C8FD1 60%,#a9d6ff);box-shadow:0 0 6px rgba(120,190,255,.5)}
.bar--en.bar--foe .bar__fill{background:linear-gradient(90deg,#7a5a1c,#F2B33D 60%,#ffe0a3)}

.hud__mid{
  flex:0 0 auto;
  min-width:58px;
  text-align:center;
  line-height:1.1;
}
.hud__round{font-size:11px;font-weight:900;color:var(--gold);white-space:nowrap}
.hud__wins{font-size:11px;font-weight:700;color:#d9c9ad;margin-top:1px;white-space:nowrap}
.hud__score{font-size:10px;font-weight:700;color:#9d8a72;margin-top:1px;white-space:nowrap}

/* ==================== STAGE ==================== */
.stage{
  position:relative;
  flex:1 1 auto;
  min-height:0;
  overflow:hidden;
  background:#1b120c;
}
.stage canvas{
  position:absolute;
  inset:0;
  display:block;
  width:100%;
  height:100%;
  touch-action:none;
}

/* ==================== CONTROLS ==================== */
.controls{
  flex:0 0 auto;
  display:flex;
  align-items:center;
  justify-content:center;
  gap:10px;
  padding:10px 10px 12px;
  background:linear-gradient(#2b1e16, #170f0a);
  border-top:2px solid #100a06;
  box-shadow:0 -3px 14px rgba(0,0,0,.5);
  position:relative;
  z-index:6;
}
.ctrl-group{display:flex;align-items:center;gap:9px}
.ctrl-group--util{padding-inline-start:2px}

.spell{
  position:relative;
  display:flex;
  flex-direction:column;
  align-items:center;
  justify-content:center;
  gap:1px;
  width:66px;
  height:66px;
  border-radius:50%;
  border:2px solid rgba(242,179,61,.3);
  background:radial-gradient(circle at 50% 26%, #4d3524 0%, #2a1c13 62%, #1b110c 100%);
  color:var(--linen);
  font-size:9.5px;
  font-weight:900;
  line-height:1;
  box-shadow:0 4px 0 #120c08, 0 8px 18px rgba(0,0,0,.45), inset 0 1px 0 rgba(255,255,255,.08);
  transition:transform .1s ease, box-shadow .1s ease, opacity .16s ease, border-color .16s ease;
}
.spell svg{width:23px;height:23px;display:block}
.spell__name{
  font-size:9px;
  letter-spacing:0;
  max-width:60px;
  white-space:nowrap;
  overflow:hidden;
  text-overflow:ellipsis;
}
.spell__cost{
  position:absolute;
  top:-3px;
  inset-inline-end:-3px;
  font-size:9px;
  font-weight:900;
  color:#2a1b12;
  background:var(--gold);
  border-radius:999px;
  padding:1px 5px;
  box-shadow:0 1px 3px rgba(0,0,0,.5);
}
.spell--crimson{border-color:rgba(199,49,44,.75);color:#ffb9ad}
.spell--crimson svg{color:#ff8f7d}
.spell--emerald{border-color:rgba(46,140,90,.8);color:#a6f0c6}
.spell--emerald svg{color:#77e2a8}
.spell--azure{border-color:rgba(76,143,209,.85);color:#b6dcff}
.spell--azure svg{color:#8ec6f8}

.spell:active,.spell.is-press{transform:translateY(3px) scale(.965);box-shadow:0 1px 0 #120c08, 0 4px 10px rgba(0,0,0,.4)}
.spell.is-poor{opacity:.42;filter:saturate(.5)}
.spell.is-ready{box-shadow:0 4px 0 #120c08, 0 0 18px rgba(242,179,61,.45), inset 0 1px 0 rgba(255,255,255,.1)}

.icon-btn{
  width:48px;height:48px;
  border-radius:50%;
  display:grid;place-items:center;
  background:radial-gradient(circle at 50% 30%, #402d20, #1e1410);
  border:2px solid rgba(239,227,204,.2);
  color:#c9b79b;
  box-shadow:0 3px 0 #120c08;
  transition:transform .1s ease;
}
.icon-btn svg{width:20px;height:20px}
.icon-btn:active{transform:translateY(2px);box-shadow:0 1px 0 #120c08}

/* ==================== OVERLAYS ==================== */
.overlay{
  position:absolute;
  inset:0;
  z-index:20;
  display:flex;
  align-items:center;
  justify-content:center;
  padding:16px;
  padding-block-start:clamp(14px, 10vh, 92px);
  padding-block-end:16px;
  background:rgba(24,14,9,.74);
  -webkit-backdrop-filter:blur(7px);
  backdrop-filter:blur(7px);
  overflow:auto;
  animation:fadeIn .22s ease-out;
}
.overlay.hidden{display:none}
@keyframes fadeIn{from{opacity:0}to{opacity:1}}

.panel{
  width:min(540px,100%);
  max-height:100%;
  overflow:auto;
  padding:24px 20px 20px;
  background:
    radial-gradient(120% 90% at 50% 0%, rgba(255,255,255,.55), rgba(255,255,255,0) 60%),
    linear-gradient(180deg,#F4E9D3 0%, #E4D0AC 100%);
  border:2px solid var(--clay);
  border-radius:26px 30px 24px 32px / 30px 24px 32px 26px;
  box-shadow:0 22px 60px rgba(12,6,3,.6), inset 0 0 0 6px rgba(242,179,61,.16);
  color:var(--ink);
  text-align:center;
  animation:pop .26s cubic-bezier(.2,1.3,.5,1);
}
@keyframes pop{from{transform:translateY(14px) scale(.97);opacity:0}to{transform:none;opacity:1}}

.kicker{
  margin:0 0 6px;
  font-size:11px;
  font-weight:900;
  letter-spacing:.14em;
  color:var(--clay-dark);
  text-transform:none;
}
.title{
  margin:0 0 10px;
  font-family:'Aref Ruqaa','Cairo',serif;
  font-weight:700;
  font-size:clamp(26px,7.4vw,40px);
  line-height:1.25;
  color:#4a2a18;
  text-shadow:0 1px 0 rgba(255,255,255,.55);
}
.title--sm{font-size:clamp(22px,6vw,32px)}
.lede{
  margin:0 auto 16px;
  max-width:42ch;
  font-size:14px;
  line-height:1.7;
  color:#5a4030;
}
.hint{
  margin:14px 0 18px;
  font-size:12.5px;
  line-height:1.65;
  color:#6b4a33;
  background:rgba(180,85,47,.1);
  border:1px dashed rgba(180,85,47,.45);
  border-radius:14px;
  padding:10px 12px;
}
.hint b{color:var(--clay-dark)}

.spell-list{list-style:none;margin:0 0 4px;padding:0;display:grid;gap:7px}
.spell-row{
  display:flex;align-items:center;gap:8px;
  background:rgba(255,255,255,.42);
  border:1px solid rgba(180,85,47,.22);
  border-radius:14px;
  padding:8px 10px;
  font-size:13px;
  color:#4a2a18;
}
.spell-row b{font-weight:900}
.spell-row .desc{margin-inline-start:auto;font-size:11px;color:#7a5b45;white-space:nowrap}
.chip{width:12px;height:12px;border-radius:50%;flex:0 0 auto;box-shadow:0 0 0 2px rgba(255,255,255,.6)}
.chip--crimson{background:var(--crimson)}
.chip--emerald{background:var(--emerald)}
.chip--azure{background:var(--azure)}
.kbd{
  font-size:10px;font-weight:900;color:#6b4a33;
  background:rgba(255,255,255,.7);
  border:1px solid rgba(107,74,51,.35);
  border-radius:6px;
  padding:1px 5px;
}

.btn{
  display:block;
  width:100%;
  min-height:54px;
  border-radius:999px;
  font-size:17px;
  font-weight:900;
  letter-spacing:0;
  transition:transform .12s ease, box-shadow .12s ease, filter .12s ease;
  margin-top:10px;
}
.btn--primary{
  background:linear-gradient(180deg,#d13c33,#8f2019);
  color:#FBEFD9;
  border:2px solid #6d1712;
  box-shadow:0 5px 0 #59120e, 0 12px 26px rgba(80,20,10,.35), inset 0 1px 0 rgba(255,255,255,.22);
}
.btn--primary:active{transform:translateY(3px);box-shadow:0 2px 0 #59120e, 0 6px 14px rgba(80,20,10,.3)}
.btn--ghost{
  background:transparent;
  color:var(--clay-dark);
  border:2px solid rgba(138,62,34,.42);
  min-height:46px;
  font-size:14px;
}
.btn--ghost:active{background:rgba(180,85,47,.1)}
.btn--tiny{
  min-height:40px;font-size:13px;
  background:rgba(107,115,51,.14);
  border:1px solid rgba(107,115,51,.5);
  color:#4a5220;
  border-radius:999px;
  margin-top:8px;
}
.btn:focus-visible,.spell:focus-visible,.icon-btn:focus-visible{
  outline:3px solid #4C8FD1;
  outline-offset:3px;
}

.best{margin:12px 0 0;font-size:12.5px;color:#7a5b45}
.best b{color:var(--clay-dark);font-size:15px}

.scoreline{
  display:flex;align-items:center;justify-content:space-between;
  background:rgba(180,85,47,.12);
  border-radius:14px;
  padding:10px 16px;
  margin:4px 0 8px;
  font-size:14px;
  font-weight:700;
  color:#5a4030;
}
.scoreline b{font-size:22px;color:var(--clay-dark);font-weight:900}
.pips-line{font-size:12.5px;color:#7a5b45;margin-bottom:6px;font-weight:700}

.statgrid{
  display:grid;
  grid-template-columns:1fr 1fr;
  gap:8px;
  margin:6px 0 4px;
}
.stat{
  background:rgba(255,255,255,.45);
  border:1px solid rgba(180,85,47,.22);
  border-radius:14px;
  padding:9px 6px;
  display:flex;flex-direction:column;gap:2px;
}
.stat span{font-size:10.5px;color:#7a5b45;font-weight:700}
.stat b{font-size:18px;color:#4a2a18;font-weight:900}

.board{
  margin-top:16px;
  border-top:1px dashed rgba(180,85,47,.4);
  padding-top:12px;
}
.board.hidden{display:none}
.board__head{display:flex;align-items:baseline;justify-content:space-between;gap:6px;font-size:12px;font-weight:900;color:#4a2a18}
.board__note{font-size:9.5px;font-weight:700;color:#94765c}
.board__list{list-style:none;margin:8px 0 0;padding:0;display:grid;gap:4px}
.board__list li{
  display:flex;justify-content:space-between;gap:8px;
  font-size:12px;color:#5a4030;
  background:rgba(255,255,255,.4);
  border-radius:10px;padding:6px 10px;
}
.board__list li.is-you{background:rgba(242,179,61,.3);font-weight:900}
.board__list li b{color:#4a2a18}
.board__empty{justify-content:center;color:#8a6a52;font-size:11.5px}

/* small phones */
@media (max-width:430px){
  .hud{padding:6px 8px 6px;gap:6px}
  .hud__name{font-size:11px}
  .hud__mid{min-width:52px}
  .hud__round{font-size:10px}
  .hud__wins{font-size:10px}
  .hud__score{font-size:9.5px}
  .bar{height:8px}
  .bar--en{height:5px}
  .spell{width:62px;height:62px}
  .spell svg{width:21px;height:21px}
  .spell__name{font-size:8.5px}
  .icon-btn{width:44px;height:44px}
  .controls{gap:8px;padding:8px 8px 10px}
  .panel{padding:20px 16px 16px}
}
@media (max-height:420px){
  .spell{width:54px;height:54px}
  .spell svg{width:18px;height:18px}
  .spell__name{display:none}
  .controls{padding:6px 8px}
  .icon-btn{width:44px;height:44px}
  .overlay{padding-block-start:12px}
  .title{font-size:24px}
  .lede{font-size:12.5px;margin-bottom:10px}
}
@media (prefers-reduced-motion:reduce){
  *{animation-duration:.001ms !important;transition-duration:.001ms !important}
}