:root {
  --bg: #f7f5f2;
  --surface: #ffffff;
  --surface-soft: #faf9f7;
  --text: #2d3436;
  --text-muted: #636e72;
  --accent: #6c9bcf;
  --accent-hover: #5a89bd;
  --accent-soft: #e8f0fa;
  --success: #7bc47f;
  --success-soft: #e8f5e9;
  --warning: #f4a261;
  --warning-soft: #fff3e6;
  --error: #e76f51;
  --error-soft: #fdecea;
  --given: #2d3436;
  --player: #4a7c59;
  --selected: #fff8e7;
  --highlight: #eef4fb;
  --peer: #f3f8ff;
  --border: #e2ddd6;
  --border-strong: #b8b0a4;
  --shadow: 0 8px 32px rgba(45, 52, 54, 0.08);
  --radius: 16px;
  --radius-sm: 10px;
  --font: "Nunito", system-ui, -apple-system, sans-serif;
  --font-devanagari: "Noto Sans Devanagari", "Nunito", system-ui, sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

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

body {
  font-family: var(--font);
  color: var(--text);
  background:
    radial-gradient(circle at top left, #eef4fb 0%, transparent 40%),
    radial-gradient(circle at bottom right, #f5efe8 0%, transparent 45%),
    var(--bg);
  line-height: 1.5;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

.app {
  max-width: 1080px;
  margin: 0 auto;
  padding: 24px 16px 40px;
}

.header {
  text-align: center;
  margin-bottom: 24px;
}

.header h1 {
  margin: 0 0 6px;
  font-family: var(--font-devanagari);
  font-size: clamp(1.8rem, 4vw, 2.5rem);
  font-weight: 800;
  letter-spacing: 0;
}

.header p {
  margin: 0 auto;
  max-width: 640px;
  color: var(--text-muted);
  font-family: var(--font-devanagari);
  font-size: 0.98rem;
}

.layout {
  display: grid;
  grid-template-columns: 1fr;
  gap: 24px;
  align-items: start;
}

@media (min-width: 900px) {
  .layout {
    grid-template-columns: minmax(0, 1fr) 320px;
  }
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}

.board-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.play-dock {
  width: min(100%, 720px);
}

.play-dock-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 10px;
}

.play-dock-actions .mode-toggle,
.play-dock-actions .action-row {
  width: 100%;
}

.board {
  display: grid;
  grid-template-columns: repeat(9, minmax(0, 1fr));
  grid-template-rows: repeat(9, minmax(0, 1fr));
  width: min(100%, 720px);
  aspect-ratio: 1;
  background: var(--surface-soft);
  border: 3px solid var(--border-strong);
  border-radius: var(--radius-sm);
  overflow: hidden;
  user-select: none;
  touch-action: manipulation;
}

.cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 0;
  padding: 2px;
  font-family: var(--font-devanagari);
  font-size: clamp(0.72rem, 2.2vw, 1rem);
  font-weight: 700;
  line-height: 1.1;
  background: var(--surface);
  border: 1px solid var(--border);
  cursor: pointer;
  overflow: hidden;
  transition: background 0.15s ease, transform 0.1s ease;
}

.cell:nth-child(3n):not(:nth-child(9n)) {
  border-right: 2px solid var(--border-strong);
}

.cell:nth-child(n + 19):nth-child(-n + 27),
.cell:nth-child(n + 46):nth-child(-n + 54) {
  border-bottom: 2px solid var(--border-strong);
}

.cell:hover:not(.given) {
  background: var(--highlight);
}

.cell.selected {
  background: var(--selected);
  box-shadow: inset 0 0 0 2px var(--warning);
}

.cell.peer {
  background: var(--peer);
}

.cell.same-symbol {
  background: var(--accent-soft);
}

.cell.given {
  color: var(--given);
  cursor: default;
}

.cell.player {
  color: var(--player);
}

.cell.error {
  color: var(--error);
  background: var(--error-soft);
}

.cell .value {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
  max-width: 100%;
  max-height: 100%;
  text-align: center;
  word-break: break-word;
  hyphens: auto;
  line-height: 1.05;
}

.cell .notes {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  width: 100%;
  height: 100%;
  padding: 1px;
  font-size: clamp(0.48rem, 1.4vw, 0.62rem);
  font-weight: 700;
  color: var(--text-muted);
}

.cell .notes span {
  display: flex;
  align-items: center;
  justify-content: center;
}

.status-bar {
  width: min(100%, 720px);
  font-family: var(--font-devanagari);
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  background: var(--surface-soft);
  border: 1px solid var(--border);
  font-weight: 600;
  text-align: center;
}

.status-bar.success {
  background: var(--success-soft);
  color: #2e6b33;
  border-color: #c8e6c9;
}

.status-bar.warning {
  background: var(--warning-soft);
  color: #9a5b1a;
  border-color: #ffe0b2;
}

.controls {
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.control-group label {
  display: block;
  margin-bottom: 8px;
  font-family: var(--font-devanagari);
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--text-muted);
  letter-spacing: 0.02em;
}

.action-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.select-input {
  width: 100%;
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-soft)
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%23636e72' d='M1 1l5 5 5-5'/%3E%3C/svg%3E")
    no-repeat right 14px center;
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 10px 36px 10px 14px;
  font: inherit;
  font-family: var(--font-devanagari);
  font-weight: 700;
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.select-input:hover,
.select-input:focus {
  outline: none;
  border-color: var(--accent);
  background-color: var(--highlight);
}

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-soft);
  color: var(--text);
  border-radius: 999px;
  padding: 10px 16px;
  font: inherit;
  font-family: var(--font-devanagari);
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}

.btn:hover {
  background: var(--highlight);
  border-color: var(--accent);
}

.btn:active {
  transform: scale(0.98);
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.btn.primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.btn.active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: #3d5f84;
}

.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.symbol-pad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.symbol-btn {
  min-height: 36px;
  padding: 6px 4px;
  font-family: var(--font-devanagari);
  font-size: clamp(0.78rem, 2vw, 0.92rem);
  border-radius: var(--radius-sm);
  text-align: center;
  line-height: 1.1;
}

.symbol-btn.complete {
  opacity: 0.45;
}

.mode-toggle {
  display: flex;
  gap: 8px;
}

.mode-toggle .btn {
  flex: 1;
}

.alphabet-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.alphabet-chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 32px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: #3d5f84;
  font-family: var(--font-devanagari);
  font-weight: 700;
  font-size: 0.88rem;
}

.footer-note kbd {
  display: inline-block;
  padding: 1px 6px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-soft);
  font-size: 0.8rem;
}

.footer-note {
  margin-top: 20px;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.85rem;
}

.confetti-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 9000;
}

.victory-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(45, 52, 54, 0.45);
  backdrop-filter: blur(4px);
  z-index: 9100;
  animation: victory-fade-in 0.35s ease;
}

.victory-overlay[hidden] {
  display: none !important;
}

.victory-card {
  max-width: 360px;
  width: 100%;
  padding: 28px 24px;
  text-align: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 24px 64px rgba(45, 52, 54, 0.22);
  animation: victory-pop 0.55s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.victory-emoji {
  margin: 0 0 8px;
  font-size: 3rem;
  line-height: 1;
  animation: victory-bounce 0.9s ease infinite;
}

.victory-title {
  margin: 0 0 8px;
  font-family: var(--font-devanagari);
  font-size: 1.75rem;
  font-weight: 800;
  color: var(--text);
}

.victory-text {
  margin: 0 0 20px;
  font-family: var(--font-devanagari);
  font-weight: 600;
  color: var(--text-muted);
}

.board.win-celebration {
  animation: board-glow 1.2s ease-in-out infinite alternate;
  border-color: var(--success);
}

.board.win-celebration .cell .value {
  animation: cell-pop 0.45s ease backwards;
}

.board.win-celebration .cell:nth-child(1) .value { animation-delay: 0ms; }
.board.win-celebration .cell:nth-child(2) .value { animation-delay: 18ms; }
.board.win-celebration .cell:nth-child(3) .value { animation-delay: 36ms; }
.board.win-celebration .cell:nth-child(4) .value { animation-delay: 54ms; }
.board.win-celebration .cell:nth-child(5) .value { animation-delay: 72ms; }
.board.win-celebration .cell:nth-child(6) .value { animation-delay: 90ms; }
.board.win-celebration .cell:nth-child(7) .value { animation-delay: 108ms; }
.board.win-celebration .cell:nth-child(8) .value { animation-delay: 126ms; }
.board.win-celebration .cell:nth-child(9) .value { animation-delay: 144ms; }
.board.win-celebration .cell:nth-child(10) .value { animation-delay: 162ms; }
.board.win-celebration .cell:nth-child(11) .value { animation-delay: 180ms; }
.board.win-celebration .cell:nth-child(12) .value { animation-delay: 198ms; }
.board.win-celebration .cell:nth-child(13) .value { animation-delay: 216ms; }
.board.win-celebration .cell:nth-child(14) .value { animation-delay: 234ms; }
.board.win-celebration .cell:nth-child(15) .value { animation-delay: 252ms; }
.board.win-celebration .cell:nth-child(16) .value { animation-delay: 270ms; }
.board.win-celebration .cell:nth-child(17) .value { animation-delay: 288ms; }
.board.win-celebration .cell:nth-child(18) .value { animation-delay: 306ms; }
.board.win-celebration .cell:nth-child(19) .value { animation-delay: 324ms; }
.board.win-celebration .cell:nth-child(20) .value { animation-delay: 342ms; }
.board.win-celebration .cell:nth-child(21) .value { animation-delay: 360ms; }
.board.win-celebration .cell:nth-child(22) .value { animation-delay: 378ms; }
.board.win-celebration .cell:nth-child(23) .value { animation-delay: 396ms; }
.board.win-celebration .cell:nth-child(24) .value { animation-delay: 414ms; }
.board.win-celebration .cell:nth-child(25) .value { animation-delay: 432ms; }
.board.win-celebration .cell:nth-child(26) .value { animation-delay: 450ms; }
.board.win-celebration .cell:nth-child(27) .value { animation-delay: 468ms; }
.board.win-celebration .cell:nth-child(28) .value { animation-delay: 486ms; }
.board.win-celebration .cell:nth-child(29) .value { animation-delay: 504ms; }
.board.win-celebration .cell:nth-child(30) .value { animation-delay: 522ms; }
.board.win-celebration .cell:nth-child(31) .value { animation-delay: 540ms; }
.board.win-celebration .cell:nth-child(32) .value { animation-delay: 558ms; }
.board.win-celebration .cell:nth-child(33) .value { animation-delay: 576ms; }
.board.win-celebration .cell:nth-child(34) .value { animation-delay: 594ms; }
.board.win-celebration .cell:nth-child(35) .value { animation-delay: 612ms; }
.board.win-celebration .cell:nth-child(36) .value { animation-delay: 630ms; }
.board.win-celebration .cell:nth-child(37) .value { animation-delay: 648ms; }
.board.win-celebration .cell:nth-child(38) .value { animation-delay: 666ms; }
.board.win-celebration .cell:nth-child(39) .value { animation-delay: 684ms; }
.board.win-celebration .cell:nth-child(40) .value { animation-delay: 702ms; }
.board.win-celebration .cell:nth-child(41) .value { animation-delay: 720ms; }
.board.win-celebration .cell:nth-child(42) .value { animation-delay: 738ms; }
.board.win-celebration .cell:nth-child(43) .value { animation-delay: 756ms; }
.board.win-celebration .cell:nth-child(44) .value { animation-delay: 774ms; }
.board.win-celebration .cell:nth-child(45) .value { animation-delay: 792ms; }
.board.win-celebration .cell:nth-child(46) .value { animation-delay: 810ms; }
.board.win-celebration .cell:nth-child(47) .value { animation-delay: 828ms; }
.board.win-celebration .cell:nth-child(48) .value { animation-delay: 846ms; }
.board.win-celebration .cell:nth-child(49) .value { animation-delay: 864ms; }
.board.win-celebration .cell:nth-child(50) .value { animation-delay: 882ms; }
.board.win-celebration .cell:nth-child(51) .value { animation-delay: 900ms; }
.board.win-celebration .cell:nth-child(52) .value { animation-delay: 918ms; }
.board.win-celebration .cell:nth-child(53) .value { animation-delay: 936ms; }
.board.win-celebration .cell:nth-child(54) .value { animation-delay: 954ms; }
.board.win-celebration .cell:nth-child(55) .value { animation-delay: 972ms; }
.board.win-celebration .cell:nth-child(56) .value { animation-delay: 990ms; }
.board.win-celebration .cell:nth-child(57) .value { animation-delay: 1008ms; }
.board.win-celebration .cell:nth-child(58) .value { animation-delay: 1026ms; }
.board.win-celebration .cell:nth-child(59) .value { animation-delay: 1044ms; }
.board.win-celebration .cell:nth-child(60) .value { animation-delay: 1062ms; }
.board.win-celebration .cell:nth-child(61) .value { animation-delay: 1080ms; }
.board.win-celebration .cell:nth-child(62) .value { animation-delay: 1098ms; }
.board.win-celebration .cell:nth-child(63) .value { animation-delay: 1116ms; }
.board.win-celebration .cell:nth-child(64) .value { animation-delay: 1134ms; }
.board.win-celebration .cell:nth-child(65) .value { animation-delay: 1152ms; }
.board.win-celebration .cell:nth-child(66) .value { animation-delay: 1170ms; }
.board.win-celebration .cell:nth-child(67) .value { animation-delay: 1188ms; }
.board.win-celebration .cell:nth-child(68) .value { animation-delay: 1206ms; }
.board.win-celebration .cell:nth-child(69) .value { animation-delay: 1224ms; }
.board.win-celebration .cell:nth-child(70) .value { animation-delay: 1242ms; }
.board.win-celebration .cell:nth-child(71) .value { animation-delay: 1260ms; }
.board.win-celebration .cell:nth-child(72) .value { animation-delay: 1278ms; }
.board.win-celebration .cell:nth-child(73) .value { animation-delay: 1296ms; }
.board.win-celebration .cell:nth-child(74) .value { animation-delay: 1314ms; }
.board.win-celebration .cell:nth-child(75) .value { animation-delay: 1332ms; }
.board.win-celebration .cell:nth-child(76) .value { animation-delay: 1350ms; }
.board.win-celebration .cell:nth-child(77) .value { animation-delay: 1368ms; }
.board.win-celebration .cell:nth-child(78) .value { animation-delay: 1386ms; }
.board.win-celebration .cell:nth-child(79) .value { animation-delay: 1404ms; }
.board.win-celebration .cell:nth-child(80) .value { animation-delay: 1422ms; }
.board.win-celebration .cell:nth-child(81) .value { animation-delay: 1440ms; }

@keyframes victory-fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes victory-pop {
  0% {
    opacity: 0;
    transform: scale(0.85) translateY(12px);
  }
  100% {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

@keyframes victory-bounce {
  0%,
  100% {
    transform: translateY(0) scale(1);
  }
  50% {
    transform: translateY(-6px) scale(1.08);
  }
}

@keyframes board-glow {
  from {
    box-shadow: 0 0 0 0 rgba(123, 196, 127, 0.35), var(--shadow);
  }
  to {
    box-shadow: 0 0 28px 6px rgba(123, 196, 127, 0.55), var(--shadow);
  }
}

@keyframes cell-pop {
  0% {
    transform: scale(0.2);
    opacity: 0;
  }
  70% {
    transform: scale(1.15);
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  .victory-overlay,
  .victory-card,
  .victory-emoji,
  .board.win-celebration,
  .board.win-celebration .cell .value {
    animation: none !important;
  }
}

@media (max-width: 899px) {
  .app {
    padding:
      max(10px, env(safe-area-inset-top))
      10px
      max(12px, env(safe-area-inset-bottom));
  }

  .header {
    margin-bottom: 10px;
  }

  .header h1 {
    font-size: clamp(1.35rem, 6vw, 1.75rem);
    margin-bottom: 4px;
  }

  .header p {
    font-size: 0.8rem;
    line-height: 1.35;
    max-width: none;
  }

  .layout {
    gap: 10px;
  }

  .panel {
    padding: 10px;
    border-radius: 12px;
  }

  .board-wrap {
    gap: 8px;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
  }

  .board {
    width: 100%;
    max-width: none;
    border-width: 2px;
  }

  .cell {
    padding: 1px;
    font-size: clamp(0.5rem, 2.65vw, 0.68rem);
  }

  .cell .notes {
    font-size: clamp(0.38rem, 1.8vw, 0.5rem);
  }

  .status-bar {
    width: 100%;
    min-height: 38px;
    padding: 8px 10px;
    font-size: 0.85rem;
  }

  .play-dock {
    width: 100%;
    margin-top: 2px;
  }

  .play-dock > label {
    margin-bottom: 6px;
    font-size: 0.85rem;
  }

  .symbol-pad {
    gap: 5px;
  }

  .symbol-btn {
    min-height: 44px;
    padding: 8px 4px;
    font-size: clamp(0.72rem, 3.2vw, 0.88rem);
  }

  .controls {
    order: -1;
    padding: 10px 12px;
  }

  .controls .control-group {
    margin: 0;
  }

  .control-group--reference {
    display: none;
  }

  .controls .control-group label {
    margin-bottom: 6px;
    font-size: 0.85rem;
  }

  .select-input {
    padding: 12px 36px 12px 14px;
    min-height: 44px;
  }

  .btn {
    min-height: 44px;
    padding: 10px 14px;
  }

  .footer-note {
    display: none;
  }
}

