:root {
  --bg: #0f172a;
  --card: #111827;
  --line: #334155;
  --text: #f8fafc;
  --muted: #94a3b8;
  --soft: #cbd5e1;
  --green: #22c55e;
  --violet: #a855f7;
  --pause: #ef4444;
  --gold: #e6c15a;
  --well: #020617;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
html { background: var(--bg); }
body {
  margin: 0;
  color: var(--text);
  background: var(--bg);
  font: 16px/1.4 "Segoe UI", "PingFang SC", "Noto Sans SC", "Hiragino Sans", "Noto Sans JP", "Malgun Gothic", "Noto Sans KR", "Segoe UI Historic", sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, select, input { font: inherit; color: inherit; }
button {
  border: 0;
  border-radius: 12px;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
button:focus-visible, select:focus-visible, a:focus-visible, input:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}
a { color: #38bdf8; }

.menu { min-height: 100dvh; overflow: auto; }
.menu-inner {
  width: min(520px, 100%);
  margin: 0 auto;
  padding: 20px 20px calc(28px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.brand {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  text-align: center;
}
.brand svg { width: 88px; height: 88px; }
.brand h1 { margin: 0; font-size: 28px; }
.subtitle { margin: 0; color: var(--muted); text-align: center; }

.play {
  min-height: 52px;
  font-size: 18px;
  font-weight: 700;
}
.play.single { background: var(--green); color: #fff; }
.play.ai { background: var(--violet); color: #fff; }
.record { margin: -6px 0 0; color: var(--muted); font-size: 14px; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.card h2 { margin: 0; font-size: 16px; }
.hint { margin: 0; color: var(--muted); font-size: 13px; }
.swatches {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  padding-bottom: 2px;
}
.swatch {
  width: 46px;
  height: 46px;
  flex: 0 0 auto;
  border-radius: 50%;
  border: 4px solid transparent;
  padding: 0;
}
.swatch[aria-pressed="true"] { border-color: #fff; }
.now { margin: 0; color: #e2e8f0; }
select, .card input[type="range"] {
  width: 100%;
}
select {
  min-height: 48px;
  padding: 8px 12px;
  border-radius: 10px;
  border: 0;
  background: #1e293b;
  color: #fff;
}
input[type="range"] { accent-color: var(--violet); }
.toggle {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
}
.toggle input { width: 22px; height: 22px; accent-color: var(--green); }
.ghost, .linkish {
  background: transparent;
  color: #38bdf8;
  min-height: 44px;
}
.linkish { padding: 0; }
.links { display: flex; flex-direction: column; align-items: center; gap: 6px; padding-top: 4px; }
.links a, .links button { font-size: 15px; }

#game {
  position: fixed;
  inset: 0;
  z-index: 2;
  background: var(--bg);
  user-select: none;
  padding:
    max(4px, env(safe-area-inset-top))
    max(4px, env(safe-area-inset-right))
    max(4px, env(safe-area-inset-bottom))
    max(4px, env(safe-area-inset-left));
  display: grid;
  grid-template-areas:
    "head head"
    "side controls"
    "board controls";
  grid-template-columns: minmax(0, 1fr) clamp(108px, 30vw, 168px);
  grid-template-rows: auto auto minmax(0, 1fr);
  gap: 6px;
  overflow: hidden;
  overscroll-behavior: none;
}
.game-head {
  grid-area: head;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto auto;
  gap: 8px;
  align-items: center;
}
.icon-btn, .pause-btn {
  background: #334155;
  color: #fff;
  min-width: 48px;
  height: 40px;
}
.pause-btn { background: var(--pause); padding: 0 12px; }
.icon-btn.off { background: #1f2937; }
.titles { min-width: 0; }
.mode {
  font-size: 17px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.score {
  color: var(--soft);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.side {
  grid-area: side;
  display: grid;
  grid-template-columns: 72px minmax(0, 1fr);
  grid-template-rows: auto auto;
  gap: 4px 6px;
  align-items: center;
  min-width: 0;
}
.next-box {
  grid-row: 1 / span 2;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 2px;
  text-align: center;
}
.next-label { font-size: 12px; color: var(--muted); }
#next { width: 88px; height: 64px; display: block; margin: 0 auto; }
.banner {
  min-height: 28px;
  border-radius: 8px;
  text-align: center;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: .04em;
  padding: 4px 8px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.cards {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}
.score-card {
  background: var(--card);
  border: 2px solid var(--line);
  border-radius: 10px;
  padding: 4px 8px;
  min-width: 0;
}
.score-card strong, .score-card span, .score-card em {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.score-card strong { font-size: 12px; }
.score-card span { font-size: 20px; font-variant-numeric: tabular-nums; }
.score-card em { font-style: normal; font-size: 11px; color: var(--gold); min-height: 14px; }
.status {
  display: none;
  margin: 0;
  color: var(--pause);
  text-align: center;
  font-size: 13px;
}
.status.show { display: block; grid-column: 1 / -1; }
.board-wrap {
  grid-area: board;
  min-height: 0;
  height: 100%;
  border: 0;
  border-radius: 0;
  background: #2c3340;
  overflow: hidden;
  touch-action: none;
}
canvas { display: block; touch-action: none; }
#board { width: 100%; height: 100%; }
.controls {
  grid-area: controls;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: stretch;
  gap: 6px;
  min-height: 0;
  height: 100%;
}
.pad { direction: ltr; flex: 1; min-height: 0; display: flex; }
.arrows {
  flex: 1;
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: repeat(5, minmax(44px, 1fr));
  gap: 6px;
  direction: ltr;
}
.left, .rotate, .right, .down, .drop { grid-column: 1; }
.down { grid-row: 1; }
.left { grid-row: 2; }
.right { grid-row: 3; }
.rotate { grid-row: 4; }
.drop { grid-row: 5; }
.arrows button {
  height: 100%;
  min-height: 44px;
  appearance: none;
  -webkit-appearance: none;
  background: #0e7490;
  color: #fff;
  -webkit-text-fill-color: #fff;
  font-family: inherit;
  font-size: clamp(16px, 4.5vw, 22px);
  font-weight: 700;
  line-height: 1;
  padding: 0;
  display: grid;
  place-items: center;
}
.arrows button svg {
  width: 32px;
  height: 32px;
  display: block;
  pointer-events: none;
}
.arrows button:disabled {
  background: #334155;
  color: #94a3b8;
  -webkit-text-fill-color: #94a3b8;
}
.hero {
  width: 100%;
  height: 44px;
  border-radius: 12px;
  background: #1e293b;
  font-size: 26px;
  line-height: 1;
  flex: 0 0 auto;
}
.hero[aria-pressed="false"] { opacity: .35; }

@media (orientation: landscape) and (max-height: 520px), (min-width: 900px) {
  #game {
    grid-template-areas:
      "head head"
      "board side"
      "board controls";
    grid-template-columns: minmax(0, 1fr) 280px;
    grid-template-rows: auto minmax(0, 1fr) auto;
    gap: 8px;
    padding:
      max(8px, env(safe-area-inset-top))
      max(8px, env(safe-area-inset-right))
      max(8px, env(safe-area-inset-bottom))
      max(8px, env(safe-area-inset-left));
  }
  .side {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto;
    align-content: start;
    overflow: auto;
  }
  .next-box { grid-row: auto; padding: 4px; }
  #next { width: 88px; height: 64px; }
  .cards { grid-template-columns: 1fr 1fr; }
  .controls {
    height: auto;
    flex-direction: row;
    align-items: center;
    justify-content: center;
  }
  .pad { flex: 0 0 auto; display: block; }
  .arrows {
    width: auto;
    height: auto;
    flex: 0 0 auto;
    grid-template-columns: 56px 56px 56px;
    grid-template-rows: auto auto auto;
  }
  .rotate { grid-column: 2; grid-row: 1; }
  .left { grid-column: 1; grid-row: 2; }
  .down { grid-column: 2; grid-row: 2; }
  .right { grid-column: 3; grid-row: 2; }
  .drop { grid-column: 1 / span 3; grid-row: 3; }
  .arrows button { height: 48px; font-size: 16px; }
  .arrows button svg { width: 26px; height: 26px; }
  .hero { width: 56px; height: 56px; border-radius: 50%; font-size: 32px; }
}

.overlay {
  position: fixed;
  inset: 0;
  z-index: 5;
  background: rgba(15, 23, 42, .72);
  display: grid;
  place-items: center;
  padding: 24px;
}
.dialog {
  width: min(420px, 100%);
  max-height: min(80dvh, 640px);
  overflow: auto;
  background: var(--card);
  border: 1px solid var(--gold);
  border-radius: 16px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: center;
}
.dialog h2 { margin: 0; font-size: 22px; }
.dialog p { margin: 0; color: var(--soft); white-space: pre-wrap; }
.dialog button {
  min-height: 48px;
  background: var(--green);
  color: #fff;
  font-weight: 700;
}
