:root {
  --bg: #111418;
  --panel: #1b2128;
  --text: #e8edf2;
  --muted: #9aa7b4;
  --accent: #ffb020;
  --line: #2b333c;
  /* Game colours: 0 white, 1 blue, 2 green, 3 red, >=4 black. */
  --c0: #f4f6f8;
  --c1: #2f6fed;
  --c2: #23a559;
  --c3: #e23b3b;
  --c4: #0b0d10;
}
@media (prefers-color-scheme: light) {
  :root { --bg:#eef1f4; --panel:#fff; --text:#161a1e; --muted:#5a6672; --line:#d6dde3; }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
#app { max-width: 560px; margin: 0 auto; padding: 16px; }
.screen[hidden] { display: none; }
h1 { text-align: center; font-size: 1.7rem; margin: 4px 0 12px; letter-spacing: .5px; }

/* --- setup form --- */
form fieldset { border: 1px solid var(--line); border-radius: 12px; margin: 0 0 10px; padding: 8px 12px 10px; }
form legend { color: var(--muted); font-size: .85rem; padding: 0 6px; }
form label { display: block; padding: 8px 4px; font-size: 1.05rem; min-height: 44px; }
input[type=radio] { transform: scale(1.3); margin-right: 10px; }
input[type=range] { width: 100%; height: 36px; }
.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chips label { flex: 1; display: flex; align-items: center; justify-content: center; text-align: center; border: 1px solid var(--line); border-radius: 10px; padding: 8px 6px; font-size: .95rem; line-height: 1.2; cursor: pointer; }
.chips input { display: none; }
.chips input:checked + span { color: var(--bg); }
.chips label:has(input:checked) { background: var(--accent); border-color: var(--accent); color: var(--bg); font-weight: 700; }

button { font-size: 1.1rem; border-radius: 12px; border: 1px solid var(--line); padding: 14px 16px; min-height: 48px; background: var(--panel); color: var(--text); cursor: pointer; }
button.primary { background: var(--accent); color: #111; border-color: var(--accent); font-weight: 700; width: 100%; }
button.ghost { background: transparent; color: var(--muted); width: 100%; margin-top: 10px; }
.error { color: var(--c3); text-align: center; }

/* --- scoreboard --- */
#scoreboard { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
.score { background: var(--panel); border-radius: 12px; padding: 8px 12px; text-align: center; min-width: 76px; }
.score .who { display: block; font-size: .8rem; color: var(--muted); }
.score .pts { display: block; font-size: 1.6rem; font-weight: 800; }
.score.active { outline: 2px solid var(--accent); }
#status { flex: 1; text-align: center; color: var(--muted); font-size: 1rem; font-weight: 600; min-height: 1.2em; transition: color .2s; }

/* --- board --- */
#board, #tut-board {
  display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px;
  touch-action: manipulation; user-select: none; position: relative;
  border-radius: 12px; transition: box-shadow .2s;
}
.cell {
  aspect-ratio: 1 / 1; min-width: 48px; min-height: 48px;
  border-radius: 10px; border: none;
  display: flex; align-items: center; justify-content: center; position: relative;
  font-size: clamp(1.3rem, 7vw, 2.2rem); font-weight: 800;
  color: #10141a; background: var(--c0);
  transition: transform .09s ease, background-color .12s ease;
}
.cell.v1 { background: var(--c1); color: #fff; }
.cell.v2 { background: var(--c2); color: #fff; }
.cell.v3 { background: var(--c3); color: #fff; }
.cell.v4 { background: var(--c4); color: #fff; }
.cell.empty { color: transparent; }
.cell.exploding { transform: scale(1.18); box-shadow: 0 0 0 3px var(--accent); z-index: 2; }
.cell.splash { transform: scale(1.08); }
.cell .pending { position: absolute; top: 3px; right: 5px; font-size: .7rem; font-weight: 800; color: var(--accent); background: rgba(0,0,0,.45); border-radius: 6px; padding: 0 4px; }

/* Floating points: "+1" per explosion, "+N" for the whole chain. */
.gain {
  position: absolute; z-index: 3; pointer-events: none; white-space: nowrap;
  font-size: 1.2rem; font-weight: 800; color: var(--accent);
  text-shadow: 0 1px 2px rgba(0,0,0,.9), 0 0 8px rgba(0,0,0,.6);
  transform: translate(-50%, -50%); animation: gain .8s ease-out forwards;
}
.gain.big { font-size: clamp(2.6rem, 16vw, 4.4rem); animation: gain-big 1.5s ease-out forwards; }
@keyframes gain { to { opacity: 0; transform: translate(-50%, -160%); } }
@keyframes gain-big {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(.4); }
  15% { opacity: 1; transform: translate(-50%, -50%) scale(1.2); }
  30% { transform: translate(-50%, -50%) scale(1); }
  70% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -85%) scale(1); }
}
@keyframes gain-fade { 0%, 60% { opacity: 1; } 100% { opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .gain, .gain.big { animation-name: gain-fade; } }

/* --- controls --- */
#controls { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 14px; }
#controls button { flex: 1; }
#pending-count { color: var(--muted); min-width: 56px; text-align: center; }
.locked #board { pointer-events: none; }
/* Rival's turn: a red ring round the board instead of dimming it. */
.ai-turn #board, .rival-turn #board { box-shadow: 0 0 0 3px var(--bg), 0 0 0 5px var(--c3); }

/* --- overlay --- */
#overlay { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: flex; align-items: center; justify-content: center; padding: 24px; }
#overlay[hidden] { display: none; }
.dialog { background: var(--panel); border-radius: 16px; padding: 24px; width: 100%; max-width: 340px; text-align: center; }
.dialog h2 { margin: 0 0 8px; }
.dialog p { color: var(--muted); margin: 0 0 18px; }

/* --- account bar --- */
#account { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: 44px; margin-bottom: 4px; position: relative; }
#account > div { display: flex; align-items: center; gap: 8px; }
#account [hidden] { display: none !important; }
button.link { background: none; border: none; color: var(--muted); padding: 8px 4px; min-height: 44px; font-size: .95rem; }
button.itch { background: #fa5c5c; border-color: #fa5c5c; color: #fff; font-weight: 700; padding: 8px 14px; min-height: 44px; font-size: .95rem; }
button.user { display: flex; align-items: center; gap: 8px; padding: 6px 12px; min-height: 44px; font-size: .95rem; max-width: 60vw; }
button.user span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#avatar { width: 28px; height: 28px; border-radius: 50%; object-fit: cover; }
#account-menu { position: absolute; right: 0; top: 100%; z-index: 5; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 6px; display: flex; flex-direction: column; min-width: 200px; box-shadow: 0 8px 24px rgba(0,0,0,.35); }
#account-menu button { background: none; border: none; text-align: left; font-size: 1rem; padding: 12px; }
#account-menu button.danger { color: var(--c3); }
.muted { color: var(--muted); }
.ok { color: var(--c2); text-align: center; }

/* --- stats & ranking --- */
h2 { margin: 8px 0 16px; text-align: center; }
h3 { color: var(--muted); font-size: .9rem; font-weight: 600; margin: 20px 0 8px; }
.cards { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.card { background: var(--panel); border-radius: 12px; padding: 12px; text-align: center; }
.card strong { display: block; font-size: 1.4rem; }
.card span { display: block; color: var(--muted); font-size: .8rem; margin-top: 2px; }
.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.list li { background: var(--panel); border-radius: 10px; padding: 10px 12px; font-size: .92rem; border-left: 4px solid var(--line); }
.list li.win { border-left-color: var(--c2); }
.list li.loss { border-left-color: var(--c3); }
.ranking li { display: flex; align-items: center; gap: 10px; border-left: none; }
.ranking li.me { outline: 2px solid var(--accent); }
.ranking .pos { font-weight: 800; min-width: 2ch; text-align: right; color: var(--accent); }
.ranking .name { flex: 1; display: flex; align-items: center; gap: 8px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ranking .name img { width: 24px; height: 24px; border-radius: 50%; object-fit: cover; flex: none; }
.ranking .value { color: var(--muted); font-size: .85rem; text-align: right; }
#ranking-me { margin-top: 10px; }
#ranking-progress { margin-top: 10px; }
@media (max-width: 520px) { #ranking .tabs { display: grid; grid-template-columns: 1fr 1fr; } }
.tabs { display: flex; gap: 6px; margin-bottom: 10px; }
.tabs button { flex: 1; font-size: .95rem; padding: 10px; }
.tabs button[aria-selected=true] { background: var(--accent); border-color: var(--accent); color: #111; font-weight: 700; }
#result-saved { font-size: .9rem; }

/* --- login callback window --- */
.callback { text-align: center; padding-top: 40px; }
.callback p { font-size: 1.1rem; margin: 20px 0; }

/* "Jugar" always visible, even in itch.io's fixed-height frame. */
.sticky-actions { position: sticky; bottom: 0; padding: 10px 0 6px; background: linear-gradient(transparent, var(--bg) 30%); }
.sticky-actions .error { margin: 0 0 8px; }

/* --- whose turn --- */
button.link.icon { font-size: 1.3rem; padding: 6px 8px; }
.ai-turn #status { color: var(--c3); }
.ai-turn #status::before { content: ""; display: inline-block; width: 8px; height: 8px; margin-right: 6px; border-radius: 50%; background: var(--c3); vertical-align: middle; animation: pulse 1s ease-in-out infinite; }
.ai-turn #score1 { outline: 2px solid var(--c3); animation: ring 1s ease-in-out infinite; }
@keyframes ring { 50% { outline-color: transparent; } }
@keyframes pulse { 50% { opacity: .45; } }
@media (prefers-reduced-motion: reduce) { .ai-turn #status::before, .ai-turn #score1 { animation: none; } }

/* --- online --- */
.center { text-align: center; }
.small { font-size: .85rem; }
button.online-entry { width: 100%; margin-bottom: 10px; background: var(--panel); border-color: var(--accent); color: var(--text); font-weight: 700; }
button.link.inline { min-height: 0; padding: 0; color: var(--accent); text-decoration: underline; font-size: inherit; }
#online fieldset { display: flex; flex-direction: column; gap: 8px; border: 1px solid var(--line); border-radius: 12px; margin: 0 0 10px; padding: 8px 12px 10px; }
#online legend { color: var(--muted); font-size: .85rem; padding: 0 6px; }
#online .chips label { padding: 8px 4px; }
.field { display: flex; flex-direction: column; gap: 6px; color: var(--muted); font-size: .85rem; }
input[type=text], .field input, .join-row input { font-size: 1.1rem; padding: 10px 12px; min-height: 48px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel); color: var(--text); width: 100%; }
.join-row { display: flex; gap: 8px; }
.join-row input { flex: 1; text-transform: uppercase; letter-spacing: 2px; font-weight: 700; }
.room-code { font-size: 2.6rem; font-weight: 800; letter-spacing: 6px; text-align: center; margin: 8px 0; color: var(--accent); user-select: all; }
.row { display: flex; gap: 8px; }
.row button { flex: 1; }
.waiting { margin: 22px 0 6px; color: var(--muted); animation: pulse 1.6s ease-in-out infinite; }
.notice { margin: 0 0 8px; padding: 8px 12px; border-radius: 10px; background: var(--panel); border-left: 4px solid var(--accent); font-size: .9rem; }
#status.hurry { color: var(--c3); }
.rival-turn #status { color: var(--c3); }
.rival-turn #status::before { content: ""; display: inline-block; width: 8px; height: 8px; margin-right: 6px; border-radius: 50%; background: var(--c3); vertical-align: middle; animation: pulse 1s ease-in-out infinite; }
.rival-turn .score.active { outline: 2px solid var(--c3); animation: ring 1s ease-in-out infinite; }
@media (prefers-reduced-motion: reduce) { .rival-turn #status::before, .rival-turn .score.active, .waiting { animation: none; } }

button:disabled { opacity: .45; cursor: not-allowed; }

/* --- arena --- */
.arena-list li { display: flex; align-items: center; gap: 8px; border-left: none; }
.arena-list .name { flex: 1; display: flex; align-items: center; gap: 8px; min-width: 0; }
.arena-list .name img { width: 32px; height: 32px; border-radius: 50%; object-fit: cover; flex: none; }
.arena-list .who { display: flex; flex-direction: column; min-width: 0; }
.arena-list .who span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.arena-list small { color: var(--muted); font-size: .8rem; }
.arena-list small.available::before { content: "● "; color: var(--c2); }
.arena-list small.playing::before { content: "● "; color: var(--accent); }
.arena-list button.challenge { min-height: 40px; padding: 8px 14px; font-size: .95rem; background: var(--accent); border-color: var(--accent); color: #111; font-weight: 700; }
.arena-list button.challenge:disabled { background: var(--panel); border-color: var(--line); color: var(--muted); }
.arena-list button.block { min-height: 40px; font-size: .8rem; padding: 4px; }
.modal { position: fixed; inset: 0; z-index: 20; background: rgba(0,0,0,.65); display: flex; align-items: center; justify-content: center; padding: 24px; }
.modal .dialog button { width: 100%; margin-top: 8px; }
.challenger { font-size: 1.3rem; font-weight: 800; color: var(--text) !important; }
button.link.danger { color: var(--c3); }
.blocked { display: flex; align-items: center; justify-content: space-between; border-left: none; }

/* --- privacy page & link --- */
a { color: var(--accent); }
.legal-link { display: block; text-align: center; font-size: .8rem; color: var(--muted); margin-top: 6px; }
.legal { line-height: 1.55; padding-bottom: 40px; }
.legal h1 { font-size: 1.6rem; }
.legal h2 { text-align: left; font-size: 1.15rem; margin: 26px 0 8px; }
.legal h3 { color: var(--text); font-size: 1rem; margin: 16px 0 6px; }
.legal ul { padding-left: 20px; }
.legal li { margin: 4px 0; }
.legal .summary { background: var(--panel); border-radius: 12px; padding: 4px 16px 8px; border-left: 4px solid var(--accent); }
.legal table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.legal th, .legal td { text-align: left; vertical-align: top; padding: 8px; border-bottom: 1px solid var(--line); }
.legal code { font-size: .9em; }

/* --- itch.io account & in-game name --- */
a.button { display: block; text-align: center; text-decoration: none; border-radius: 12px; padding: 14px 16px; min-height: 48px; font-size: 1.1rem; }
a.button.primary { background: var(--accent); color: #111; font-weight: 700; }
#account-needed p { margin: 6px 0 10px; }
.name-input { width: 100%; font-size: 1.2rem; font-weight: 700; text-align: center; padding: 10px 12px; min-height: 48px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--text); margin: 6px 0; }
.name-warning { background: rgba(226, 59, 59, .12); border: 1px solid var(--c3); border-radius: 10px; padding: 10px 12px; color: var(--text) !important; font-size: .95rem; }
.name-warning strong { color: var(--c3); }
#online fieldset > p.small { margin: 0; text-align: center; }

/* --- how-to video --- */
.entry-row { margin-bottom: 10px; }
.entry-row button.online-entry { margin-bottom: 0; }
.video-dialog { max-width: 420px; padding: 14px; display: flex; flex-direction: column; gap: 10px; max-height: calc(100vh - 32px); }
.video-dialog h2 { margin: 0; }
.video-dialog video { width: 100%; min-height: 0; flex: 1 1 auto; max-height: calc(100vh - 170px); aspect-ratio: 9 / 16; object-fit: contain; background: #000; border-radius: 10px; }
#howto { padding: 12px; }

/* --- accounts --- */
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.modal .dialog { max-height: calc(100vh - 24px); overflow-y: auto; }
.auth-dialog { display: flex; flex-direction: column; gap: 8px; text-align: left; }
.auth-dialog h3 { margin: 4px 0; color: var(--text); font-size: 1.1rem; text-align: center; }
.auth-view { display: flex; flex-direction: column; gap: 8px; }
.auth-view[hidden] { display: none !important; }
.auth-dialog input:not([type=checkbox]), #security input:not([type=checkbox]) { width: 100%; font-size: 1.05rem; padding: 10px 12px; min-height: 46px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--text); }
.auth-dialog button { width: 100%; }
.auth-dialog button.link.inline { width: auto; }
.auth-dialog .tabs { margin-bottom: 4px; }
.check { display: flex; gap: 10px; align-items: flex-start; font-size: .9rem; color: var(--muted); min-height: 0; padding: 4px 0; }
.check input { transform: scale(1.3); margin-top: 3px; flex: none; }
.recovery-code { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 1.35rem; font-weight: 800; letter-spacing: 1px; text-align: center; padding: 12px; border-radius: 10px; background: var(--bg); border: 2px dashed var(--accent); color: var(--accent) !important; user-select: all; word-break: break-all; }
#security .panel { border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin: 0 0 10px; display: flex; flex-direction: column; gap: 8px; }
#security .panel h3 { margin: 0; color: var(--text); }
#security legend { color: var(--muted); font-size: .85rem; padding: 0 6px; }

/* --- language, login and arena entry --- */
button.lang { background: none; border: 1px solid var(--line); color: var(--muted); font-size: .8rem; font-weight: 700; letter-spacing: .5px; padding: 4px 8px; min-height: 32px; border-radius: 8px; }
button.login { background: transparent; border-color: var(--accent); color: var(--accent); font-weight: 700; padding: 5px 14px; min-height: 44px; font-size: .95rem; display: flex; flex-direction: column; align-items: center; line-height: 1.2; }
button.login small { font-size: .72rem; font-weight: 500; color: var(--muted); }
button.arena-entry { width: 100%; margin-bottom: 10px; padding: 10px 16px; min-height: 44px; font-size: .95rem; background: var(--panel); color: var(--muted); }
button.arena-entry.live { color: var(--text); border-color: var(--c2); }
button.arena-entry.live::before { content: "● "; color: var(--c2); }

/* --- English how-to videos: portrait, landscape (wide screens) and the chain loop --- */
.video-tabs { margin: 0; }
.video-tabs button { font-size: .85rem; padding: 8px; min-height: 40px; }
.video-dialog[data-video="how-to-play-wide"] { max-width: min(960px, 100%); }
.video-dialog[data-video="how-to-play-wide"] video { aspect-ratio: 16 / 9; }
.video-dialog[data-video="chain-en"] video { aspect-ratio: 640 / 720; }

/* --- welcome: create account / guest, then tutorial / play --- */
body.intro #open-ranking, body.intro #account-guest, body.intro #account-user { display: none !important; }
.welcome-hero { text-align: center; padding: 8px 0 4px; }
.hero-board { display: inline-grid; grid-template-columns: repeat(3, 44px); gap: 5px; margin: 6px auto 4px; }
.hero-board .cell { min-width: 0; min-height: 0; width: 44px; height: 44px; font-size: 1.15rem; border-radius: 9px; }
.hero-board .boom { box-shadow: 0 0 0 3px var(--accent); animation: boom 2.2s ease-in-out infinite; }
@keyframes boom { 0%, 72%, 100% { transform: scale(1); } 82% { transform: scale(1.2); } }
#welcome h1 { font-size: 2.3rem; margin: 8px 0 4px; }
.tagline { color: var(--muted); margin: 0 auto 22px; max-width: 32ch; line-height: 1.4; }
.welcome-step { display: flex; flex-direction: column; gap: 10px; }
.welcome-step p { margin: 4px 0 0; }
button.cta { width: 100%; display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 16px; background: var(--accent); border-color: var(--accent); color: #111; box-shadow: 0 6px 22px rgba(255, 176, 32, .28); }
button.cta strong { font-size: 1.25rem; }
button.cta small { font-size: .85rem; line-height: 1.3; opacity: .8; }
button.secondary { width: 100%; background: transparent; color: var(--text); }
.welcome-hello { text-align: center; font-size: 1.15rem; font-weight: 700; }
.choices { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
button.choice { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 18px 10px; border-color: var(--accent); }
.choice-icon { font-size: 1.8rem; line-height: 1; color: var(--accent); }
.choice strong { font-size: 1.15rem; }
.choice small { color: var(--muted); font-size: .82rem; line-height: 1.3; }

/* --- guided tutorial --- */
.tut-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.tut-head p { margin: 0; }
.tut-head h2 { text-align: left; margin: 0 0 8px; }
.coach { background: var(--panel); border-left: 4px solid var(--accent); border-radius: 10px; padding: 10px 12px; margin: 0 0 10px; line-height: 1.4; min-height: 4.4em; }
.tut-bar { display: flex; justify-content: space-between; align-items: center; margin: 0 2px 8px; }
.tut-score { font-weight: 800; color: var(--accent); }
.cell.target { z-index: 1; box-shadow: 0 0 0 3px var(--accent); animation: target 1s ease-in-out infinite; }
@keyframes target { 50% { box-shadow: 0 0 0 6px rgba(255, 176, 32, .4); } }
.tut-next { margin-top: 14px; }
.tut-end { text-align: center; }
.tut-trophy { font-size: 3rem; margin: 8px 0 0; }
.tut-end-score { font-weight: 700; }
.tut-rules { text-align: left; padding-left: 20px; line-height: 1.45; color: var(--muted); }
.tut-rules li { margin: 6px 0; }
.tut-rules strong { color: var(--text); }
.tut-account { text-align: left; margin-top: 14px; padding: 10px 12px; }
.tut-account p { margin: 0 0 8px; }
@media (prefers-reduced-motion: reduce) { .hero-board .boom, .cell.target { animation: none; } }
button.login strong, #open-ranking { white-space: nowrap; }
@media (max-width: 420px) { #open-ranking .label { display: none; } #open-ranking { font-size: 1.3rem; padding: 6px 8px; } }
