:root {
  color-scheme: dark;
  --ink: #17130f;
  --ink-soft: #292018;
  --paper: #f2e8cf;
  --paper-dim: #d4c49f;
  --vermilion: #d1462f;
  --vermilion-dark: #862719;
  --gold: #d7a83c;
  --indigo: #1f4f65;
  --line: rgba(242, 232, 207, 0.18);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
  margin: 0;
  overflow-x: hidden;
  color: var(--paper);
  background:
    radial-gradient(circle at 50% -20%, rgba(209, 70, 47, 0.23), transparent 42%),
    repeating-linear-gradient(93deg, rgba(255,255,255,.018) 0 1px, transparent 1px 7px),
    var(--ink);
  font-family: "Hiragino Mincho ProN", "Yu Mincho", "YuMincho", serif;
}

button, input { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }
button:focus-visible, input:focus-visible, canvas:focus-visible {
  outline: 3px solid #ffd260;
  outline-offset: 3px;
}

.game-shell { width: min(1480px, 100%); margin: 0 auto; padding: 18px clamp(12px, 2vw, 32px) calc(22px + var(--safe-bottom)); }

.topbar { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 24px; margin-bottom: 14px; }
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark { display: grid; place-items: center; flex: 0 0 44px; height: 44px; border: 1px solid var(--vermilion); border-radius: 50%; color: var(--vermilion); font-weight: 800; transform: rotate(-5deg); }
.eyebrow, .kicker { margin: 0 0 2px; color: var(--gold); font-size: 11px; font-weight: 800; letter-spacing: .25em; text-transform: uppercase; }
h1 { margin: 0; font-size: clamp(18px, 2vw, 28px); line-height: 1.1; letter-spacing: .08em; white-space: nowrap; }
.stats { display: flex; border-inline: 1px solid var(--line); }
.stat { min-width: 118px; padding: 2px 20px; text-align: center; border-right: 1px solid var(--line); }
.stat:last-child { border-right: 0; }
.stat span { display: block; color: var(--paper-dim); font-family: system-ui, sans-serif; font-size: 10px; font-weight: 700; letter-spacing: .12em; }
.stat strong { display: block; margin-top: 3px; font-family: ui-monospace, monospace; font-size: 17px; }
.top-actions { display: flex; justify-content: flex-end; gap: 8px; }
.icon-button, .close-button { color: var(--paper); background: transparent; border: 1px solid var(--line); cursor: pointer; }
.icon-button { min-width: 68px; padding: 9px 11px; border-radius: 3px; font-family: system-ui, sans-serif; font-size: 12px; font-weight: 700; }
.icon-button:hover { border-color: var(--gold); color: #ffd260; }

.stage-frame { position: relative; aspect-ratio: 16 / 9; min-height: 280px; overflow: hidden; border: 1px solid rgba(215,168,60,.4); border-radius: 4px; background: #b9c78d; box-shadow: 0 26px 80px rgba(0,0,0,.42); isolation: isolate; }
.stage-frame::before { position: absolute; inset: 8px; z-index: 4; border: 1px solid rgba(255,255,255,.16); pointer-events: none; content: ""; }
#game-canvas { display: block; width: 100%; height: 100%; touch-action: none; }

.overlay-panel { position: absolute; z-index: 10; color: var(--ink); background: rgba(244, 234, 208, .96); box-shadow: 0 16px 70px rgba(0,0,0,.35); transition: opacity .2s ease, transform .2s ease; }
.overlay-panel::before { position: absolute; inset: 7px; border: 1px solid rgba(69,50,29,.22); pointer-events: none; content: ""; }
.is-hidden { display: none !important; }

.title-panel { top: 50%; left: clamp(24px, 6vw, 90px); width: min(430px, calc(100% - 48px)); padding: clamp(24px, 4vw, 52px); transform: translateY(-50%) rotate(-.6deg); }
.seal { position: absolute; top: 28px; right: 25px; display: grid; place-items: center; width: 58px; height: 58px; color: var(--vermilion); border: 3px double currentColor; font-weight: 900; transform: rotate(8deg); opacity: .88; }
.title-panel h2 { margin: 8px 0 16px; font-size: clamp(34px, 5vw, 68px); line-height: .95; letter-spacing: -.05em; }
.title-panel h2 em { color: var(--vermilion); font-style: normal; }
.lead { margin: 0 0 24px; color: #5e4a34; line-height: 1.75; }
.panel-actions { display: flex; align-items: center; gap: 20px; }
.brush-button, .action-button { color: #fff7df; background: var(--vermilion); border: 0; font-weight: 900; cursor: pointer; box-shadow: inset 0 -4px 0 rgba(0,0,0,.2), 0 7px 18px rgba(134,39,25,.25); }
.brush-button { padding: 13px 22px; border-radius: 2px 8px 3px 7px; font-size: 17px; }
.brush-button:hover, .action-button:hover { filter: brightness(1.09); transform: translateY(-1px); }
.text-button { padding: 8px 2px; color: #5d4431; background: transparent; border: 0; border-bottom: 1px solid currentColor; cursor: pointer; font-weight: 800; }
.key-help { margin: 20px 0 0; color: #796b57; font-family: system-ui, sans-serif; font-size: 11px; }
kbd { padding: 2px 5px; border: 1px solid #a9977f; border-radius: 3px; background: rgba(255,255,255,.45); }

.ranking-panel, .result-panel { top: 50%; left: 50%; width: min(680px, calc(100% - 30px)); max-height: calc(100% - 30px); padding: 28px 34px; overflow: auto; transform: translate(-50%, -50%); }
.panel-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 20px; }
.panel-heading h2 { margin: 4px 0 20px; font-size: clamp(24px, 3.6vw, 40px); }
.close-button { width: 38px; height: 38px; color: var(--ink); border-color: rgba(0,0,0,.2); font-size: 25px; cursor: pointer; }
.ranking-table-wrap { overflow-x: auto; }
.ranking-status { margin: -10px 0 12px; color: #665844; font-family: system-ui, sans-serif; font-size: 12px; font-weight: 700; }
.ranking-status[data-mode="online"]::before { color: #2d725d; content: "● "; }
.ranking-status[data-mode="local"]::before { color: #a46d2c; content: "● "; }
.ranking-status[data-mode="offline"]::before { color: #9a3d2d; content: "● "; }
table { width: 100%; border-collapse: collapse; font-family: system-ui, sans-serif; }
th { color: #796b57; font-size: 11px; letter-spacing: .12em; text-align: left; }
td, th { padding: 9px 10px; border-bottom: 1px solid rgba(61,45,30,.14); white-space: nowrap; }
td:nth-child(1) { width: 44px; color: var(--vermilion); font-weight: 900; }
td:nth-child(3) { font-family: ui-monospace, monospace; font-weight: 800; }
.rank-1 td { color: #7a5410; background: rgba(215,168,60,.18); font-weight: 900; }
.rank-2 td { background: rgba(104,123,125,.1); font-weight: 800; }
.rank-3 td { background: rgba(167,99,53,.09); font-weight: 800; }
.empty-row td { padding: 38px 10px; color: #7c6a52; text-align: center; }
.danger-button { margin-top: 20px; padding: 8px 11px; color: #8a2a1c; background: transparent; border: 1px solid rgba(138,42,28,.35); cursor: pointer; font-size: 12px; }

.result-panel { width: min(560px, calc(100% - 30px)); text-align: center; }
.result-panel > .kicker { margin-top: 4px; }
.result-panel h2 { margin: 5px 0 0; color: var(--vermilion); font-family: ui-monospace, monospace; font-size: clamp(56px, 8vw, 92px); line-height: 1; }
.result-panel h2 small { margin-left: 5px; color: #76563c; font-size: 20px; }
.record-badge { margin: -4px auto 6px; color: var(--vermilion); font-size: 28px; font-weight: 900; animation: record-pop .6s cubic-bezier(.2,.9,.2,1.3) both; }
.ranking-qualification { margin: 12px 0 0; color: var(--vermilion-dark); font-size: 13px; font-weight: 900; }
.result-flavor { margin: 6px 0 16px; color: #6c5841; }
.judgement-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 15px 0; }
.judgement-row div { padding: 9px 5px; background: rgba(44,36,27,.06); }
.judgement-row span { display: block; color: #86755f; font-size: 10px; }
.judgement-row strong { display: block; margin-top: 3px; color: #2e5d68; font-family: system-ui, sans-serif; font-size: 13px; }
.name-label { display: block; margin: 14px 0 6px; color: #715d45; font-size: 12px; font-weight: 800; }
.name-entry { display: flex; width: min(350px, 100%); margin: 0 auto; }
.name-entry input { min-width: 0; flex: 1; padding: 11px; color: var(--ink); background: rgba(255,255,255,.65); border: 1px solid #b8a88e; border-right: 0; }
.brush-button.compact { padding: 10px 16px; font-size: 14px; box-shadow: none; }
.result-actions { display: flex; align-items: center; justify-content: center; gap: 24px; margin-top: 15px; }
.secondary-button { padding: 10px 17px; color: #fff7df; background: var(--indigo); border: 0; cursor: pointer; font-weight: 900; }

.toast { position: absolute; bottom: 18px; left: 50%; z-index: 20; padding: 8px 15px; color: var(--paper); background: rgba(23,19,15,.9); border: 1px solid var(--gold); border-radius: 3px; font-size: 13px; opacity: 0; pointer-events: none; transform: translate(-50%, 12px); transition: .2s ease; }
.toast.is-visible { opacity: 1; transform: translate(-50%, 0); }

.control-deck { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 24px; padding: 17px 8px 0; }
.instruction { display: flex; align-items: center; gap: 12px; }
.instruction > span { display: grid; place-items: center; width: 42px; height: 42px; color: var(--gold); border: 1px solid var(--gold); border-radius: 50%; font-weight: 900; }
.instruction small { display: block; color: var(--paper-dim); font-family: system-ui, sans-serif; font-size: 10px; }
.instruction strong { display: block; margin-top: 3px; font-size: 14px; }
.action-button { min-width: 250px; padding: 13px 38px 12px; clip-path: polygon(3% 7%, 97% 0, 100% 88%, 4% 100%); font-size: 20px; letter-spacing: .12em; }
.action-button small { display: block; margin-top: 2px; color: rgba(255,255,255,.65); font-family: system-ui, sans-serif; font-size: 8px; letter-spacing: .2em; }
.action-button:disabled { cursor: default; filter: grayscale(.5); opacity: .55; transform: none; }
.one-button-note { justify-self: end; margin: 0; color: var(--paper-dim); font-size: 12px; font-style: italic; }

@keyframes record-pop { from { opacity: 0; transform: scale(.45) rotate(-8deg); } to { opacity: 1; transform: scale(1) rotate(-2deg); } }

@media (max-width: 900px) {
  .topbar { grid-template-columns: 1fr auto; gap: 10px; }
  .stats { grid-column: 1 / -1; grid-row: 2; justify-content: stretch; order: 3; }
  .stat { flex: 1; min-width: 0; padding-inline: 10px; }
  .control-deck { grid-template-columns: 1fr auto; }
  .one-button-note { display: none; }
}

@media (max-width: 620px) {
  body { padding-bottom: calc(84px + var(--safe-bottom)); }
  .game-shell { padding: 10px 8px 0; }
  .brand-mark { flex-basis: 34px; height: 34px; font-size: 13px; }
  h1 { font-size: 17px; }
  .eyebrow { font-size: 8px; }
  .icon-button { min-width: 54px; padding: 7px; font-size: 10px; }
  .stats { background: rgba(255,255,255,.025); }
  .stat strong { font-size: 14px; }
  .stage-frame { min-height: 245px; aspect-ratio: 4 / 3; }
  .title-panel { left: 16px; width: calc(100% - 32px); padding: 25px 26px; }
  .title-panel h2 { font-size: 42px; }
  .title-panel .lead { font-size: 13px; }
  .seal { top: 20px; right: 20px; width: 45px; height: 45px; font-size: 13px; }
  .ranking-panel, .result-panel { padding: 22px 20px; }
  .control-deck { position: fixed; right: 0; bottom: 0; left: 0; z-index: 30; display: block; padding: 8px 12px calc(8px + var(--safe-bottom)); background: rgba(23,19,15,.96); border-top: 1px solid rgba(215,168,60,.45); }
  .instruction { display: none; }
  .action-button { width: 100%; min-width: 0; min-height: 60px; }
  .key-help { display: none; }
}

@media (max-height: 650px) and (orientation: landscape) {
  .game-shell { padding-top: 6px; }
  .topbar { margin-bottom: 6px; }
  .brand-mark { display: none; }
  .stats { position: absolute; top: 8px; left: 50%; z-index: 40; transform: translateX(-50%); }
  .control-deck { padding-top: 7px; }
  .action-button { padding-block: 8px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; }
}
