/* 絶対に勝つな。 */
:root {
  --bg: #f6f6f4;
  --surface: #ffffff;
  --ink: #141414;
  --muted: #6b6b6b;
  --line: #d9d9d6;
  --o: #1f6feb;
  --x: #e5484d;
  --gold: #c99a06;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 6px 20px rgba(0,0,0,.06);
  --radius: 14px;
  --cell: min(28vw, 116px);
  font-family: "Hiragino Kaku Gothic ProN", "Hiragino Sans", "Noto Sans JP", "Yu Gothic", Meiryo, system-ui, sans-serif;
  color-scheme: light;
}
body[data-theme="dim"] {
  --bg: #e9e9e6;
  --surface: #f7f7f5;
  --line: #c9c9c5;
}
body[data-theme="dark"] {
  --bg: #0e0e10;
  --surface: #18181b;
  --ink: #ededed;
  --muted: #9a9aa0;
  --line: #2e2e33;
  --o: #4c8dff;
  --x: #ff5c63;
  --shadow: 0 0 0 1px rgba(255,255,255,.04), 0 8px 24px rgba(0,0,0,.5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  transition: background .6s, color .6s;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

#app { min-height: 100%; max-width: 480px; margin: 0 auto; position: relative; }

.screen { display: none; min-height: 100vh; min-height: 100dvh; padding: 16px 16px calc(20px + env(safe-area-inset-bottom)); }
.screen.active { display: flex; flex-direction: column; }

/* ---------- buttons ---------- */
.btn {
  appearance: none; border: 1.5px solid var(--ink); background: var(--surface);
  border-radius: 999px; padding: 12px 20px; font-weight: 700; letter-spacing: .04em;
  cursor: pointer; transition: transform .08s, background .2s, opacity .2s;
  touch-action: manipulation;
}
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--ink); color: var(--bg); }
.btn.small { padding: 6px 12px; font-size: 14px; border-width: 1px; }
.btn:disabled { opacity: .35; cursor: default; }
.btn.danger.locked { border-style: dashed; color: var(--muted); border-color: var(--muted); }
.btn.danger.unlocked { border-color: var(--x); color: var(--x); animation: flicker .18s steps(2) infinite; }
.btn .lock { margin-right: 4px; font-size: .9em; }
.link { background: none; border: none; color: var(--muted); text-decoration: underline; cursor: pointer; font-size: 13px; padding: 6px; }
.menu { display: flex; flex-direction: column; gap: 12px; width: 100%; max-width: 280px; margin: 0 auto; }

/* ---------- title ---------- */
.title-wrap { margin: auto 0; text-align: center; display: flex; flex-direction: column; align-items: center; gap: 14px; }
.title-en { margin: 0; letter-spacing: .5em; font-size: 13px; color: var(--muted); padding-left: .5em; }
.title-ja { margin: 0; font-size: clamp(48px, 16vw, 76px); line-height: 1.05; letter-spacing: .02em; font-weight: 900; }
.title-sub { margin: 0; font-size: 13px; color: var(--muted); }
.title-face { margin: 12px 0 18px; }
.title-foot { display: flex; gap: 16px; justify-content: center; margin-top: 8px; }

/* ---------- bars ---------- */
.bar { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 10px; margin-bottom: 12px; }
.bar h2 { margin: 0; font-size: 18px; text-align: center; }
.stage-label { display: flex; flex-direction: column; align-items: center; line-height: 1.25; min-width: 0; }
.stage-label span { font-size: 11px; letter-spacing: .2em; color: var(--muted); }
.stage-label strong { font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.group-tag { font-size: 12px; border: 1px solid var(--line); border-radius: 999px; padding: 3px 10px; color: var(--muted); white-space: nowrap; }

/* ---------- stage select ---------- */
.stage-list { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.stage-btn {
  aspect-ratio: 1; border-radius: 12px; border: 1.5px solid var(--ink); background: var(--surface);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-weight: 800; font-size: 20px; cursor: pointer;
}
.stage-btn small { font-size: 10px; font-weight: 600; color: var(--muted); }
.stage-btn.cleared { background: var(--ink); color: var(--bg); }
.stage-btn.cleared small { color: var(--bg); opacity: .7; }
.stage-btn:disabled { border-style: dashed; color: var(--line); border-color: var(--line); cursor: default; }
.group-head { grid-column: 1 / -1; font-size: 12px; color: var(--muted); margin: 8px 0 -2px; letter-spacing: .1em; }

/* ---------- WIN ---------- */
.win-panel { display: flex; gap: 12px; align-items: flex-start; min-height: 96px; }
.face {
  flex: none; width: 64px; height: 56px; border-radius: 12px; background: var(--ink); color: var(--bg);
  display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: 17px;
  letter-spacing: .05em; position: relative; font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  box-shadow: var(--shadow);
}
.face::after { content: ""; position: absolute; bottom: -8px; left: 50%; width: 22px; height: 6px; margin-left: -11px; background: var(--ink); border-radius: 0 0 6px 6px; }
.title-face .face { width: 88px; height: 76px; font-size: 22px; }
.face.thinking { animation: bob .6s ease-in-out infinite; }
.bubble {
  flex: 1; position: relative; background: var(--surface); border: 1.5px solid var(--ink); border-radius: var(--radius);
  padding: 12px 14px 14px; min-height: 76px; font-size: 15px; line-height: 1.6; cursor: pointer; box-shadow: var(--shadow);
}
.bubble::before { content: ""; position: absolute; left: -8px; top: 20px; width: 12px; height: 12px; background: var(--surface); border-left: 1.5px solid var(--ink); border-bottom: 1.5px solid var(--ink); transform: rotate(45deg); }
.bubble p { margin: 0; white-space: pre-wrap; }
.bubble.system { border-style: dashed; }
.bubble.system p { font-weight: 700; }
.bubble .next { position: absolute; right: 10px; bottom: 4px; font-size: 11px; color: var(--muted); animation: blink 1s steps(2) infinite; }

/* ---------- chips ---------- */
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 14px 0 10px; min-height: 26px; justify-content: center; }
.chip { font-size: 12px; padding: 3px 10px; border-radius: 999px; background: var(--surface); border: 1px solid var(--line); white-space: nowrap; }
.chip b { font-weight: 800; }
.chip.ping { animation: ping .5s ease-out; }

/* ---------- board ---------- */
.board-wrap { position: relative; margin: 6px auto 8px; }
.board {
  display: grid; grid-template-columns: repeat(3, var(--cell)); grid-template-rows: repeat(3, var(--cell)); gap: 0;
  position: relative;
}
.cell {
  appearance: none; border: none; background: transparent; cursor: pointer; padding: 0; position: relative;
  display: flex; align-items: center; justify-content: center;
  font-size: calc(var(--cell) * .62); font-weight: 300; line-height: 1; touch-action: manipulation;
}
.cell:nth-child(3n+1), .cell:nth-child(3n+2) { border-right: 3px solid var(--ink); }
.cell:nth-child(-n+6) { border-bottom: 3px solid var(--ink); }
.cell:disabled { cursor: default; }
.cell .m { display: block; transform-origin: center; }
.cell.o .m { color: var(--o); }
.cell.x .m { color: var(--x); }
.cell.pop .m { animation: pop .28s cubic-bezier(.2,1.6,.4,1); }
.cell.gift::after { content: "🎁"; position: absolute; top: 4px; right: 6px; font-size: 14px; }
.cell.ghost .m { opacity: .25; }
.cell.lit { background: color-mix(in srgb, var(--x) 14%, transparent); }
.cell.lit.o-lit { background: color-mix(in srgb, var(--o) 14%, transparent); }
.cell.hinted { background: color-mix(in srgb, var(--gold) 22%, transparent); animation: pulse 1s ease-in-out infinite; }
.cell.shake { animation: shake .3s; }
.cell.redirect::before { content: "↔"; position: absolute; font-size: 22px; color: var(--muted); animation: fade .7s forwards; }
.board.flipping { animation: flip .7s ease-in-out; }
.intro-cover { position: absolute; inset: 0; display: flex; align-items: flex-end; justify-content: center; padding-bottom: 6px; }
.intro-cover[hidden] { display: none; }

.legend { text-align: center; margin: 6px 0 0; font-size: 14px; color: var(--muted); }
.legend .o-mark { color: var(--o); font-weight: 800; }
.legend .x-mark { color: var(--x); font-weight: 800; }
.goal { text-align: center; margin: 4px 0 14px; font-size: 12px; color: var(--muted); }
.controls { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: auto; }
.controls .btn { padding: 12px 6px; font-size: 14px; }

/* ---------- overlays ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 20; display: flex; align-items: center; justify-content: center; padding: 16px;
  background: color-mix(in srgb, var(--bg) 70%, transparent); backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
  animation: fade-in .25s;
}
.result-card {
  width: 100%; max-width: 360px; background: var(--surface); border: 1.5px solid var(--ink); border-radius: 20px;
  padding: 28px 20px 22px; text-align: center; box-shadow: var(--shadow);
}
.result-big { margin: 0; font-size: 44px; font-weight: 900; letter-spacing: .04em; }
.result-big.lose { color: var(--x); animation: zoom .5s cubic-bezier(.2,1.4,.4,1); }
.result-big.win { color: var(--o); }
.result-sub { margin: 4px 0 10px; letter-spacing: .3em; font-size: 13px; font-weight: 800; color: var(--muted); padding-left: .3em; }
.result-msg { margin: 0 0 20px; line-height: 1.7; font-size: 14px; white-space: pre-wrap; }

/* ---------- ending ---------- */
#screen-ending { background: #fff; color: #111; }
.ending { margin: auto 0; text-align: center; display: flex; flex-direction: column; gap: 18px; align-items: center; }
.ending-line { min-height: 4.8em; margin: 0; font-size: 16px; line-height: 1.8; white-space: pre-wrap; transition: opacity .6s; }
.ending-big { margin: 0; font-size: clamp(40px, 13vw, 64px); font-weight: 900; letter-spacing: .04em; min-height: 1.2em; }
.ending-small { margin: 0; color: #777; font-size: 13px; min-height: 1.4em; letter-spacing: .1em; }

/* ---------- fx ---------- */
#fx { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 30; }
.noise { position: fixed; inset: 0; pointer-events: none; z-index: 25; opacity: 0; }
body[data-glitch="1"] .bubble p { animation: jitter 5s infinite; }
body[data-glitch="2"] .bubble p, body[data-glitch="3"] .bubble p { animation: jitter 2.6s infinite; }
body[data-glitch="2"] .board, body[data-glitch="3"] .board { animation: rgb 4s infinite; }
body[data-glitch="2"] .noise, body[data-glitch="3"] .noise {
  opacity: .07;
  background: repeating-linear-gradient(0deg, rgba(255,255,255,.5) 0 1px, transparent 1px 3px);
  animation: scan 6s linear infinite;
}
body[data-glitch="3"] .noise { opacity: .12; }
body[data-glitch="3"] #app { animation: tear 7s infinite; }
body.glitch-hit #app { animation: hit .25s steps(3); }

@keyframes pop { 0% { transform: scale(.2); opacity: 0; } 100% { transform: scale(1); opacity: 1; } }
@keyframes shake { 0%,100% { transform: translateX(0); } 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }
@keyframes flip { 0% { transform: rotateY(0); } 50% { transform: rotateY(90deg); } 100% { transform: rotateY(0); } }
@keyframes pulse { 50% { background: color-mix(in srgb, var(--gold) 40%, transparent); } }
@keyframes blink { 50% { opacity: 0; } }
@keyframes bob { 50% { transform: translateY(-3px); } }
@keyframes ping { 0% { transform: scale(1.25); } 100% { transform: scale(1); } }
@keyframes fade { to { opacity: 0; } }
@keyframes fade-in { from { opacity: 0; } }
@keyframes zoom { 0% { transform: scale(2.2); opacity: 0; } 100% { transform: scale(1); opacity: 1; } }
@keyframes flicker { 50% { opacity: .55; } }
@keyframes jitter { 0%, 92%, 100% { transform: none; } 93% { transform: translate(2px, -1px) skewX(8deg); } 95% { transform: translate(-2px, 1px); } 97% { transform: translate(1px, 0) skewX(-6deg); } }
@keyframes rgb { 0%, 88%, 100% { filter: none; } 90% { filter: drop-shadow(3px 0 0 rgba(255,0,60,.6)) drop-shadow(-3px 0 0 rgba(0,160,255,.6)); } 93% { filter: none; transform: translateX(2px); } 95% { filter: drop-shadow(-2px 0 0 rgba(255,0,60,.6)); transform: none; } }
@keyframes scan { from { background-position: 0 0; } to { background-position: 0 300px; } }
@keyframes tear { 0%, 96%, 100% { clip-path: none; transform: none; } 97% { clip-path: inset(30% 0 50% 0); transform: translateX(-8px); } 98% { clip-path: inset(60% 0 10% 0); transform: translateX(6px); } }
@keyframes hit { 0% { transform: translate(4px, 0); filter: invert(1); } 50% { transform: translate(-4px, 2px); } 100% { transform: none; filter: none; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001s !important; animation-iteration-count: 1 !important; transition: none !important; }
  .noise { display: none; }
}
@media (min-height: 760px) { .win-panel { min-height: 110px; } }
