:root {
  --bg: #0b1020;
  --bg-deep: #080c16;
  --panel: #121a30;
  --panel-soft: #17213c;
  --line: rgba(255, 255, 255, 0.12);
  --text: #f2f6ff;
  --muted: #99a6c7;
  --accent: #f5c451;
  --accent-strong: #ffd66b;
  --focus: #7dd3fc;
  --ok: #6ee7b7;
  --danger: #ff8d8d;
  --radius: 18px;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.42);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", "Noto Sans JP", "Hiragino Sans", "Yu Gothic", sans-serif;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html {
  min-height: 100%;
  background: var(--bg-deep);
}

body {
  min-height: 100svh;
  margin: 0;
  color: var(--text);
  background:
    radial-gradient(1100px 620px at 12% -10%, rgba(88, 101, 242, 0.2), transparent 60%),
    radial-gradient(900px 520px at 112% 112%, rgba(245, 196, 81, 0.13), transparent 58%),
    linear-gradient(180deg, #0c1224 0%, #090e1a 60%, #070b13 100%);
  background-attachment: fixed;
}

button,
select {
  font: inherit;
}

button {
  color: inherit;
}

::selection {
  color: #14100a;
  background: var(--accent-strong);
}

.icon-sprite {
  position: absolute;
  width: 0;
  height: 0;
  overflow: hidden;
}

.sr-only {
  position: absolute !important;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: fixed;
  top: 10px;
  left: 10px;
  z-index: 100;
  padding: 10px 14px;
  color: #14100a;
  background: var(--accent-strong);
  border-radius: 10px;
  font-weight: 700;
  text-decoration: none;
  transform: translateY(-160%);
  transition: transform 0.15s ease;
}

.skip-link:focus-visible {
  transform: translateY(0);
}

.topbar {
  width: min(100%, 1180px);
  margin: 0 auto;
  padding: clamp(16px, 3vw, 28px) clamp(14px, 4vw, 36px) 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 14px 20px;
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin: 0;
  line-height: 1;
}

.brand-num {
  font-size: clamp(2rem, 3.4vw, 2.6rem);
  font-weight: 900;
  letter-spacing: -0.06em;
  color: var(--accent-strong);
  text-shadow: 0 6px 24px rgba(245, 196, 81, 0.25);
}

.brand-name {
  font-size: 0.82rem;
  font-weight: 650;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--muted);
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
  min-width: 0;
}

.language-field {
  min-width: 0;
}

.language-field select {
  height: 44px;
  max-width: 160px;
  padding: 0 30px 0 12px;
  color: var(--text);
  background-color: var(--panel-soft);
  border: 1px solid var(--line);
  border-radius: 12px;
  cursor: pointer;
}

.icon-btn,
.primary-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 44px;
  border-radius: 12px;
  border: 1px solid var(--line);
  cursor: pointer;
  transition: transform 0.12s ease, filter 0.12s ease, background-color 0.12s ease, border-color 0.12s ease;
}

.icon-btn {
  width: 44px;
  flex: 0 0 44px;
  color: var(--text);
  background: var(--panel-soft);
}

.icon-btn.is-off {
  color: var(--muted);
}

.icon-btn .icon,
.primary-btn .icon {
  width: 22px;
  height: 22px;
}

.icon {
  display: block;
  fill: none;
}

.primary-btn {
  padding: 0 18px;
  color: #161004;
  background: linear-gradient(180deg, var(--accent-strong), var(--accent));
  border-color: rgba(255, 255, 255, 0.24);
  font-weight: 800;
  white-space: nowrap;
  box-shadow: 0 8px 22px rgba(245, 196, 81, 0.18);
}

.icon-btn:hover,
.primary-btn:hover {
  filter: brightness(1.08);
}

.icon-btn:active,
.primary-btn:active {
  transform: translateY(1px);
}

.icon-btn[aria-pressed="true"] {
  border-color: rgba(245, 196, 81, 0.46);
}

button:focus-visible,
select:focus-visible,
a:focus-visible,
.board:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 3px;
}

.main {
  --board-size: min(94vw, 560px, max(280px, calc(100svh - 230px)));
  width: 100%;
  padding: clamp(18px, 3.5vw, 34px) 12px 26px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.win-panel {
  width: var(--board-size);
  padding: 14px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  background:
    linear-gradient(120deg, rgba(110, 231, 183, 0.14), rgba(245, 196, 81, 0.1)),
    var(--panel);
  border: 1px solid rgba(110, 231, 183, 0.34);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  animation: rise 0.28s ease-out both;
}

.win-panel h2 {
  margin: 0 0 2px;
  font-size: 1.2rem;
}

.win-panel p {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
  font-variant-numeric: tabular-nums;
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.hud {
  width: var(--board-size);
  min-height: 40px;
  padding: 0 4px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.stats {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.stat {
  display: flex;
  align-items: baseline;
  gap: 7px;
  white-space: nowrap;
}

.stat-label {
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.stat-value {
  font-size: 1.08rem;
  font-variant-numeric: tabular-nums;
}

.stat-divider {
  color: rgba(255, 255, 255, 0.25);
}

.review-badge {
  padding: 5px 10px;
  color: #052119;
  background: var(--ok);
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  animation: rise 0.18s ease-out both;
}

.board {
  position: relative;
  width: var(--board-size);
  aspect-ratio: 1 / 1;
  padding: clamp(7px, 1.4vw, 11px);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: clamp(6px, 1.4vw, 10px);
  background:
    linear-gradient(145deg, rgba(255, 255, 255, 0.055), transparent 45%),
    var(--panel);
  border: 1px solid var(--line);
  border-radius: clamp(20px, 4vw, 28px);
  box-shadow: var(--shadow), inset 0 1px 0 rgba(255, 255, 255, 0.06);
  touch-action: manipulation;
  user-select: none;
}

.cell {
  position: relative;
  min-width: 0;
  min-height: 0;
  border-radius: clamp(11px, 2.5vw, 16px);
}

.cell.empty {
  background: rgba(255, 255, 255, 0.028);
  border: 1px dashed rgba(255, 255, 255, 0.14);
  box-shadow: inset 0 2px 12px rgba(0, 0, 0, 0.22);
}

.cell.is-cursor::after {
  content: "";
  position: absolute;
  inset: -3px;
  z-index: 3;
  border: 3px solid var(--ok);
  border-radius: calc(clamp(11px, 2.5vw, 16px) + 3px);
  box-shadow: 0 0 0 4px rgba(110, 231, 183, 0.14), 0 0 22px rgba(110, 231, 183, 0.2);
  pointer-events: none;
}

.tile {
  position: relative;
  width: 100%;
  padding: 0;
  font: inherit;
  appearance: none;
  -webkit-tap-highlight-color: transparent;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  color: #fff;
  background:
    linear-gradient(145deg, hsl(var(--hue, 220) 58% 38%), hsl(var(--hue, 220) 68% 23%));
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: inherit;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.24),
    inset 0 -2px 0 rgba(0, 0, 0, 0.18),
    0 5px 12px rgba(0, 0, 0, 0.24);
  cursor: pointer;
  transition: filter 0.12s ease, box-shadow 0.12s ease;
}

.tile::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 0;
  background: linear-gradient(160deg, rgba(255, 255, 255, 0.14), transparent 46%);
  pointer-events: none;
}

.tile:hover {
  filter: brightness(1.12);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.3),
    inset 0 -2px 0 rgba(0, 0, 0, 0.18),
    0 7px 18px rgba(0, 0, 0, 0.32);
}

.tile-icon {
  position: absolute;
  top: 15%;
  left: 15%;
  width: 70%;
  height: 70%;
  color: #fff;
  opacity: 0.23;
  pointer-events: none;
}

.tile-num {
  position: relative;
  z-index: 1;
  font-size: clamp(1.35rem, 6.5vw, 2.8rem);
  font-weight: 900;
  line-height: 1;
  letter-spacing: -0.04em;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.36);
}

.board.is-review .tile {
  filter: saturate(0.8) brightness(0.88);
}

.board.is-review .tile:hover {
  filter: saturate(0.8) brightness(0.88);
}

.hint {
  width: var(--board-size);
  margin: 0;
  padding: 0 4px;
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.5;
  text-align: center;
}

.help-dialog {
  width: min(540px, calc(100vw - 28px));
  max-height: min(82svh, 680px);
  padding: 0;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: 0 30px 90px rgba(0, 0, 0, 0.7);
  overflow: auto;
}

.help-dialog::backdrop {
  background: rgba(3, 7, 16, 0.72);
  backdrop-filter: blur(5px);
}

.dialog-head {
  position: sticky;
  top: 0;
  z-index: 2;
  padding: 18px 18px 12px 22px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  background: linear-gradient(180deg, var(--panel) 75%, rgba(18, 26, 48, 0.92));
}

.dialog-head h2 {
  margin: 0;
  font-size: 1.15rem;
}

.key-list {
  margin: 0;
  padding: 4px 22px 2px;
}

.key-row {
  padding: 9px 0;
  display: grid;
  grid-template-columns: 190px 1fr;
  align-items: baseline;
  gap: 14px;
  border-top: 1px solid rgba(255, 255, 255, 0.075);
}

.key-row:first-child {
  border-top: 0;
}

.key-row dt,
.key-row dd {
  margin: 0;
}

.key-row dd {
  color: var(--muted);
  font-size: 0.92rem;
}

kbd {
  display: inline-block;
  padding: 4px 8px;
  color: var(--text);
  background: rgba(255, 255, 255, 0.075);
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-bottom-color: rgba(255, 255, 255, 0.28);
  border-radius: 7px;
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.24);
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 700;
}

.help-note {
  margin: 0;
  padding: 10px 22px;
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.5;
}

.help-note:last-child {
  padding-bottom: 22px;
}

.noscript {
  width: min(94vw, 560px);
  margin: 20px auto;
  padding: 14px;
  border: 1px solid var(--danger);
  border-radius: var(--radius);
}

@media (max-width: 660px) {
  .topbar {
    padding: 14px 12px 0;
    gap: 12px;
  }

  .brand-num {
    font-size: 2rem;
  }

  .brand-name {
    font-size: 0.72rem;
  }

  .toolbar {
    width: 100%;
    display: grid;
    grid-template-columns: minmax(0, 1fr) 44px 44px auto;
    margin-left: 0;
  }

  .language-field select {
    width: 100%;
    max-width: none;
  }

  .primary-btn {
    padding: 0 13px;
    font-size: 0.88rem;
  }

  .main {
    padding-top: 16px;
    gap: 10px;
  }

  .win-panel {
    flex-wrap: wrap;
    gap: 10px;
  }

  .stat-label {
    font-size: 0.64rem;
  }

  .stat-value {
    font-size: 1rem;
  }

  .hint {
    font-size: 0.72rem;
  }

  .key-row {
    grid-template-columns: 1fr;
    gap: 5px;
  }
}

@media (max-width: 360px) {
  .toolbar {
    grid-template-columns: 44px 44px minmax(0, 1fr);
  }

  .language-field {
    grid-column: 1 / -1;
  }

  #sound-btn {
    grid-column: 1;
    grid-row: 2;
  }

  #help-btn {
    grid-column: 2;
    grid-row: 2;
  }

  #new-btn {
    grid-column: 3;
    grid-row: 2;
    justify-self: end;
  }
}

@media (max-width: 380px) {
  .brand-name {
    font-size: 0.66rem;
    letter-spacing: 0.06em;
  }

  .primary-btn {
    padding: 0 10px;
  }

  .hint {
    font-size: 0.67rem;
  }
}

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