:root {
  --bg: #0d0a0c;
  --bg-2: #171215;
  --panel: #1d171a;
  --panel-2: #262024;
  --line: #3a2f2c;
  --text: #efe3cc;
  --muted: #a7998a;
  --gold: #e9b654;
  --gold-2: #f7d58c;
  --danger: #e0685a;
  --radius: 14px;
  --font-title: 'Cinzel', 'Trajan Pro', Georgia, serif;
  --font-ui: 'Alegreya Sans', 'Segoe UI', system-ui, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background:
    radial-gradient(ellipse 80% 55% at 50% 42%, #2a1d17 0%, rgba(26, 18, 17, 0.6) 45%, transparent 75%),
    radial-gradient(ellipse at 50% 120%, #1a1310 0%, transparent 60%),
    var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
}
button { font: inherit; color: inherit; cursor: pointer; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
svg .dot { fill: currentColor; }

#app {
  height: 100dvh;
  max-width: 540px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  padding: calc(env(safe-area-inset-top) + 8px) 16px calc(env(safe-area-inset-bottom) + 12px);
}

/* ---------- header ---------- */
.top { display: grid; grid-template-columns: 84px minmax(0, 1fr) 84px; align-items: center; }
.top-right { display: flex; justify-content: flex-end; gap: 2px; }
.brand { text-align: center; }
.brand h1 {
  font-family: var(--font-title);
  font-weight: 700;
  font-size: clamp(20px, 6vw, 30px);
  letter-spacing: 0.08em;
  white-space: nowrap;
  margin: 0;
  background: linear-gradient(180deg, #fff1cf 0%, #e9b654 55%, #a8742a 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 0 14px rgba(233, 182, 84, 0.28));
}
.brand .sub { font-size: 13px; color: var(--muted); letter-spacing: 0.08em; text-transform: uppercase; margin-top: 2px; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%;
  border: 1px solid transparent; background: transparent;
  display: grid; place-items: center; color: var(--muted);
  transition: color .2s, background .2s;
}
.icon-btn:hover, .icon-btn:focus-visible { color: var(--gold-2); background: rgba(255, 255, 255, 0.05); outline: none; }

/* ---------- mode switch ---------- */
.modes { display: flex; flex-direction: column; align-items: center; gap: 8px; margin-top: 10px; }
.seg {
  display: inline-flex; padding: 3px; border-radius: 999px;
  background: var(--panel); border: 1px solid var(--line);
}
.seg button {
  border: 0; background: transparent; padding: 6px 18px; border-radius: 999px;
  color: var(--muted); font-weight: 500; letter-spacing: 0.04em; transition: background .2s, color .2s;
}
.seg button.is-on { background: linear-gradient(180deg, #3b2f25, #2a211b); color: var(--gold-2); box-shadow: inset 0 0 0 1px rgba(233, 182, 84, 0.35); }
.seg-small button { padding: 4px 14px; font-size: 14px; }
.bstrip { display: flex; align-items: center; gap: 8px; border: 0; background: transparent; color: var(--muted); font-size: 13px; padding: 2px 6px; margin-top: -2px; border-radius: 8px; }
.bstrip:hover, .bstrip:focus-visible { color: var(--gold-2); outline: none; }
.bstrip svg { width: 14px; height: 14px; }
.bprog { width: 110px; height: 3px; border-radius: 3px; background: var(--line); overflow: hidden; }
.bprog i { display: block; height: 100%; width: 0; background: linear-gradient(90deg, #a8742a, var(--gold-2)); transition: width .6s ease; }

/* ---------- HUD ---------- */
.hud { display: grid; grid-template-columns: 70px 1fr 70px; align-items: center; margin-top: 12px; }
.stat { display: flex; flex-direction: column; align-items: flex-start; }
.stat.right { align-items: flex-end; }
.stat .label { font-size: 11px; letter-spacing: 0.14em; text-transform: uppercase; color: var(--muted); }
.stat .val { font-family: var(--font-title); font-size: 24px; line-height: 1.1; }
.candles { display: flex; justify-content: center; gap: 5px; flex-wrap: wrap; min-height: 26px; align-items: flex-end; }
.candle { width: 7px; height: 16px; border-radius: 2px 2px 1px 1px; background: linear-gradient(180deg, #efe0c0, #b9a27d); position: relative; transition: opacity .4s, filter .4s; }
.candle::before {
  content: ''; position: absolute; left: 50%; top: -9px; width: 6px; height: 9px; transform: translateX(-50%);
  border-radius: 50% 50% 45% 45% / 65% 65% 35% 35%;
  background: radial-gradient(circle at 50% 70%, #fff6d8 0%, #ffc34d 45%, #ff7b1c 80%, transparent 82%);
  filter: drop-shadow(0 0 4px #ffb347);
  animation: flicker 1.6s ease-in-out infinite alternate;
}
.candle:nth-child(3n)::before { animation-delay: -.5s; }
.candle:nth-child(3n+1)::before { animation-delay: -1.1s; }
.candle.out { opacity: 0.35; filter: grayscale(1) brightness(.6); }
.candle.out::before { display: none; }
.candle.bonus { background: linear-gradient(180deg, #ffe7a8, #d89a36); }
@keyframes flicker { 0% { transform: translateX(-50%) scale(1, 1); } 50% { transform: translateX(-52%) scale(.92, 1.08); } 100% { transform: translateX(-48%) scale(1.04, .95); } }

/* ---------- stage ---------- */
.stage { position: relative; flex: 1; min-height: 0; margin: 4px -16px 0; }
#board { display: block; touch-action: manipulation; -webkit-mask-image: linear-gradient(180deg, #000 88%, transparent); mask-image: linear-gradient(180deg, #000 88%, transparent); }
.loading { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-family: var(--font-title); letter-spacing: .1em; pointer-events: none; transition: opacity .3s; }
.loading.gone { opacity: 0; }
.toast {
  position: absolute; left: 50%; bottom: 10px; transform: translate(-50%, 10px);
  background: rgba(20, 15, 16, 0.92); border: 1px solid var(--line); color: var(--text);
  padding: 8px 14px; border-radius: 999px; font-size: 15px; white-space: nowrap;
  opacity: 0; pointer-events: none; transition: opacity .25s, transform .25s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- palette: cut-glass gems ---------- */
.palette { display: flex; justify-content: center; gap: 10px; margin-top: 8px; }
.gem {
  --c: #888;
  position: relative; width: 50px; height: 50px; border: 0; padding: 0; background: transparent;
  transition: transform .15s ease, opacity .3s, filter .3s;
}
.gem .face {
  position: absolute; inset: 3px;
  clip-path: polygon(50% 0, 100% 30%, 100% 70%, 50% 100%, 0 70%, 0 30%);
  background:
    linear-gradient(135deg, rgba(255, 255, 255, .55) 0%, rgba(255, 255, 255, 0) 38%),
    linear-gradient(315deg, rgba(0, 0, 0, .35) 0%, rgba(0, 0, 0, 0) 45%),
    var(--c);
  box-shadow: inset 0 0 12px rgba(255, 255, 255, .25);
}
.gem .rim {
  position: absolute; inset: 0;
  clip-path: polygon(50% 0, 100% 30%, 100% 70%, 50% 100%, 0 70%, 0 30%);
  background: #120d0e;
}
.gem svg { position: absolute; left: 50%; top: 50%; width: 18px; height: 18px; transform: translate(-50%, -50%); fill: rgba(255, 255, 255, .6); stroke: none; }
.gem:not(.off):hover { transform: translateY(-3px); filter: drop-shadow(0 4px 12px var(--c)); }
.gem:active { transform: scale(.94); }
.gem.off { opacity: .28; filter: grayscale(.4); }
.gem.hinted { animation: gem-hint 0.9s ease-in-out infinite; filter: drop-shadow(0 0 14px #ffd27a); }
@keyframes gem-hint { 50% { transform: translateY(-6px) scale(1.06); } }
.gem.nope { animation: nope .3s; }
@keyframes nope { 25% { transform: translateX(-5px); } 75% { transform: translateX(5px); } }

/* ---------- buttons ---------- */
.actions { display: flex; justify-content: center; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.btn {
  display: inline-flex; align-items: center; gap: 7px; border-radius: 999px; padding: 10px 20px;
  font-weight: 700; letter-spacing: .04em; border: 1px solid var(--line); background: var(--panel);
  transition: transform .12s, background .2s, opacity .2s;
}
.btn svg { width: 18px; height: 18px; }
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .4; cursor: default; }
.btn.gold { background: linear-gradient(180deg, #f3cc78, #c68f35); border-color: #f5d690; color: #2a1a09; }
.btn.gold svg { stroke: #2a1a09; }
.btn.ghost:hover { background: var(--panel-2); }
.ad-tag { font-size: 11px; font-weight: 700; padding: 2px 6px; border-radius: 6px; background: rgba(42, 26, 9, .18); }
.btn[hidden] { display: none; }

/* ---------- modal ---------- */
.modal { position: fixed; inset: 0; display: grid; place-items: center; z-index: 20; padding: 16px; }
.modal[hidden] { display: none; }
.scrim { position: absolute; inset: 0; background: rgba(5, 3, 4, .72); backdrop-filter: blur(3px); animation: fade .25s; }
.card {
  position: relative; width: min(420px, 100%); max-height: 88dvh; overflow: auto;
  background: linear-gradient(180deg, #221b1d, #171214);
  border: 1px solid #4a3b30; border-radius: 20px; padding: 26px 22px 22px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .6), inset 0 1px 0 rgba(255, 230, 190, .08);
  animation: rise .35s cubic-bezier(.2, .9, .3, 1.2);
}
.card h2 { font-family: var(--font-title); font-weight: 700; letter-spacing: .08em; text-align: center; margin: 0 0 6px; font-size: 24px; color: var(--gold-2); }
.card p { color: #d7c9b1; line-height: 1.5; margin: 8px 0; font-size: 16px; }
.card .center { text-align: center; }
.close { position: absolute; top: 8px; right: 10px; width: 36px; height: 36px; border: 0; background: transparent; font-size: 26px; color: var(--muted); }
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { opacity: 0; transform: translateY(20px) scale(.97); } }

.stars { display: flex; justify-content: center; gap: 8px; margin: 14px 0 6px; }
.star { width: 44px; height: 44px; fill: #3a302b; stroke: none; transition: fill .3s; }
.star.on { fill: var(--gold); filter: drop-shadow(0 0 10px rgba(233, 182, 84, .6)); animation: star-pop .5s cubic-bezier(.2, 1.6, .4, 1) backwards; }
.star.on:nth-child(2) { animation-delay: .18s; }
.star.on:nth-child(3) { animation-delay: .36s; }
@keyframes star-pop { from { transform: scale(0) rotate(-40deg); } }
.result-line { text-align: center; font-size: 18px; margin: 4px 0; }
.result-line b { font-family: var(--font-title); color: var(--gold-2); }
.verdict { text-align: center; font-style: italic; color: var(--muted); margin: 2px 0 12px; }
.swatches { display: flex; flex-wrap: wrap; justify-content: center; gap: 4px; margin: 10px 0 16px; }
.swatches i { width: 16px; height: 16px; border-radius: 3px; display: block; box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .15); }
.row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin-top: 10px; }
.next { text-align: center; color: var(--muted); font-size: 14px; margin-top: 14px; }

.points { text-align: center; font-family: var(--font-title); color: var(--gold); font-size: 20px; margin: 2px 0; }
.points span { font-family: var(--font-ui); font-size: 14px; color: var(--muted); }
.points.dim { color: var(--muted); font-size: 17px; }

.grid-tabs { margin: 6px 0 10px; }
.grid-head { display: flex; justify-content: space-between; font-size: 14px; color: var(--muted); margin-bottom: 8px; }
.grid-head b { color: var(--gold-2); font-family: var(--font-title); }
.tiles { display: grid; grid-template-columns: repeat(10, 1fr); gap: 4px; max-height: 46dvh; overflow-y: auto; padding: 2px; }
.tile { aspect-ratio: 1; border-radius: 6px; border: 1px solid transparent; font-size: 11px; font-weight: 700; padding: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; }
.tile.lit { background: linear-gradient(160deg, #4a3a22, #2a2018); color: var(--gold-2); border-color: #5a4630; }
.tile.open { background: #231c1e; color: var(--text); border-color: var(--gold); box-shadow: 0 0 8px rgba(233, 182, 84, .35); }
.tile.locked { background: #171214; color: #4a3f3c; cursor: default; }
.tile.current { outline: 2px solid var(--gold-2); outline-offset: 1px; }
.pips { display: flex; gap: 1px; }
.pips i { width: 4px; height: 4px; border-radius: 50%; background: #5a4a3a; }
.pips i.on { background: var(--gold); }

.lb { display: flex; flex-direction: column; gap: 2px; max-height: 48dvh; overflow-y: auto; margin: 10px 0; }
.lb-row { display: grid; grid-template-columns: 36px 1fr auto; gap: 8px; padding: 7px 10px; border-radius: 8px; background: rgba(255, 255, 255, .03); }
.lb-row.you { background: rgba(233, 182, 84, .14); box-shadow: inset 0 0 0 1px rgba(233, 182, 84, .4); }
.lb-row .rk { color: var(--muted); font-weight: 700; }
.lb-row .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-row .sc { font-family: var(--font-title); color: var(--gold-2); }
.name-row { display: flex; gap: 8px; }
.name-row input { flex: 1; min-width: 0; background: #120e10; border: 1px solid var(--line); border-radius: 999px; padding: 9px 14px; color: var(--text); font: inherit; }
.name-row input:focus { outline: none; border-color: var(--gold); }

.grid-stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 16px 0; text-align: center; }
.grid-stats .n { font-family: var(--font-title); font-size: 26px; color: var(--gold-2); }
.grid-stats .l { font-size: 12px; color: var(--muted); line-height: 1.2; }
.bars { display: flex; flex-direction: column; gap: 6px; }
.bars .bar { display: grid; grid-template-columns: 54px 1fr; align-items: center; gap: 8px; font-size: 14px; color: var(--muted); }
.bars .fill { height: 20px; border-radius: 4px; background: linear-gradient(90deg, #a8742a, #e9b654); min-width: 22px; color: #2a1a09; font-weight: 700; font-size: 13px; display: flex; align-items: center; justify-content: flex-end; padding-right: 6px; }

.toggle { display: flex; align-items: center; justify-content: space-between; padding: 12px 0; border-bottom: 1px solid #2e2527; gap: 12px; }
.toggle:last-child { border-bottom: 0; }
.toggle .t { font-weight: 500; }
.toggle .d { font-size: 13px; color: var(--muted); }
.switch { position: relative; width: 48px; height: 28px; border-radius: 999px; background: #3a302e; border: 0; flex: none; transition: background .2s; }
.switch::after { content: ''; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #d9cbb3; transition: transform .2s; }
.switch[aria-checked="true"] { background: var(--gold); }
.switch[aria-checked="true"]::after { transform: translateX(20px); background: #fff; }

.help-list { padding: 0; margin: 12px 0; list-style: none; display: flex; flex-direction: column; gap: 12px; }
.help-list li { display: grid; grid-template-columns: 34px 1fr; gap: 10px; align-items: start; color: #d7c9b1; line-height: 1.45; }
.help-list .ico { width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center; background: #2a2124; font-size: 18px; }

/* ---------- simulated ad ---------- */
.ad-overlay { position: fixed; inset: 0; z-index: 40; background: #050304; display: grid; place-items: center; }
.ad-overlay[hidden] { display: none; }
.ad-box { text-align: center; }
.ad-label { letter-spacing: .2em; text-transform: uppercase; color: var(--muted); font-size: 12px; }
.ad-count { font-family: var(--font-title); font-size: 72px; color: var(--gold-2); margin: 10px 0; }
.ad-note { color: var(--muted); font-size: 13px; }

@media (max-height: 700px) {
  .brand h1 { font-size: 24px; }
  .modes { margin-top: 6px; }
  .hud { margin-top: 6px; }
  .gem { width: 44px; height: 44px; }
  .actions { margin-top: 8px; }
}
@media (max-width: 420px) {
  .btn { padding: 10px 14px; gap: 6px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
}
