/* ── Reset & Base ── */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  background:
    radial-gradient(circle at 50% 0%, rgba(246, 196, 83, 0.08), transparent 55%),
    linear-gradient(180deg, #1b1916 0%, #141210 100%) fixed;
  color: #e9e1cf;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  overflow-x: hidden;
  touch-action: pan-y;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

#app {
  width: 100%;
  max-width: 500px;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 12px;
}

/* ── HUD ── */
#hud {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 16px;
  background: rgba(255, 245, 225, 0.05);
  border: 1px solid rgba(239, 230, 210, 0.12);
  border-radius: 14px;
  margin-bottom: 12px;
  gap: 8px;
}

.hud-display {
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(0, 0, 0, 0.25);
  padding: 8px 14px;
  border-radius: 10px;
  font-family: 'Iowan Old Style', 'Palatino Linotype', Palatino, Georgia, serif;
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: 1px;
  min-width: 100px;
  justify-content: center;
  border: 1px solid rgba(239, 230, 210, 0.1);
}

.hud-display.hud-mistakes { min-width: 64px; }
.hud-mistakes #mistakes, .hud-mistakes .hud-icon { color: #ff8a80; }

.hud-icon {
  font-size: 1.1rem;
}

#timer {
  color: #f6c453;
}

#difficulty-selector {
  display: flex;
  gap: 6px;
}

.diff-btn {
  padding: 8px 16px;
  border: 1px solid rgba(239, 230, 210, 0.14);
  border-radius: 999px;
  background: transparent;
  color: #cfc5b0;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
}

.diff-btn:hover {
  background: rgba(239, 230, 210, 0.08);
  color: #fff;
}

.diff-btn.active {
  background: #f6c453;
  color: #2a2014;
  border-color: #f6c453;
}

/* ── Sudoku Board ── */
#board-wrapper {
  display: flex;
  justify-content: center;
  width: 100%;
  padding: 4px;
}

#board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  background:
    radial-gradient(rgba(255, 245, 225, 0.035) 1px, transparent 1px) 0 0 / 5px 5px,
    linear-gradient(160deg, #2a2621, #1f1c18);
  border: 2px solid rgba(239, 230, 210, 0.55);
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.5), inset 0 0 40px rgba(0, 0, 0, 0.35);
  border-radius: 10px;
  overflow: hidden;
  width: 100%;
  max-width: 450px;
  aspect-ratio: 1;
}

/* ── Cells ── */
.sudoku-cell {
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  cursor: pointer;
  position: relative;
  transition: background 0.15s ease;
  border: 1px solid rgba(239, 230, 210, 0.1);
  font-size: 1.5rem;
  font-weight: 600;
  font-family: 'Iowan Old Style', 'Palatino Linotype', Palatino, Georgia, serif;
  font-style: italic;
  color: #9cc0ff;
}

/* 3x3 box borders: thicker right/bottom on box boundaries */
.sudoku-cell.box-right {
  border-right: 2px solid rgba(239, 230, 210, 0.45);
}
.sudoku-cell.box-bottom {
  border-bottom: 2px solid rgba(239, 230, 210, 0.45);
}
.sudoku-cell.box-left {
  border-left: 2px solid rgba(239, 230, 210, 0.45);
}
.sudoku-cell.box-top {
  border-top: 2px solid rgba(239, 230, 210, 0.45);
}

/* Given (pre-filled) numbers */
.sudoku-cell.given {
  color: #f3ead8;
  font-weight: 700;
  font-style: normal;
}

/* Selected cell */
.sudoku-cell.selected {
  background: rgba(246, 196, 83, 0.24);
  box-shadow: inset 0 0 0 2px rgba(246, 196, 83, 0.85);
  z-index: 2;
}

/* Same number highlighting */
.sudoku-cell.same-number {
  background: radial-gradient(circle, rgba(156, 192, 255, 0.24) 0 55%, transparent 58%);
}

/* Same row/col/box highlighting */
.sudoku-cell.same-region {
  background-color: rgba(255, 245, 225, 0.05);
}

/* Error cell */
.sudoku-cell.error {
  background: rgba(255, 110, 100, 0.14);
  color: #ff8a80;
  text-decoration: line-through;
  text-decoration-color: rgba(255, 138, 128, 0.6);
}

/* Conflict highlight */
.sudoku-cell.conflict {
  background-color: rgba(255, 110, 100, 0.1);
}

.cell-value.ink-in { animation: ink-in 0.42s cubic-bezier(.3, .8, .3, 1) both; }

@keyframes ink-in {
  0% { clip-path: inset(0 100% 0 0); filter: blur(2px); transform: scale(1.15); opacity: 0.4; }
  100% { clip-path: inset(0 0 0 0); filter: blur(0); transform: scale(1); opacity: 1; }
}

.sudoku-cell.shake { animation: cell-shake 0.35s ease-out; }

@keyframes cell-shake {
  20%, 60% { transform: translateX(-3px); }
  40%, 80% { transform: translateX(3px); }
}

.sudoku-cell.unit-done::after {
  content: '';
  position: absolute;
  inset: 8% 0;
  background: linear-gradient(90deg, transparent, rgba(246, 196, 83, 0.55), transparent);
  border-radius: 4px;
  animation: highlighter 0.8s ease-out var(--d, 0ms) both;
  pointer-events: none;
}

@keyframes highlighter {
  0% { transform: scaleX(0); transform-origin: left; opacity: 0.9; }
  45% { transform: scaleX(1); transform-origin: left; opacity: 0.9; }
  100% { transform: scaleX(1); opacity: 0; }
}

#board.solved .sudoku-cell { animation: solved-ripple 0.9s ease-out var(--d, 0ms) both; }

@keyframes solved-ripple {
  0% { background-color: transparent; }
  35% { background-color: rgba(246, 196, 83, 0.35); transform: scale(1.06); }
  100% { background-color: transparent; transform: scale(1); }
}

/* Hint revealed animation */
.sudoku-cell.hint-reveal {
  animation: hintPulse 0.6s ease;
}

@keyframes hintPulse {
  0% { background: rgba(246, 196, 83, 0.5); transform: scale(1.1); }
  50% { background: rgba(246, 196, 83, 0.25); }
  100% { background: transparent; transform: scale(1); }
}

/* ── Pencil Marks ── */
.pencil-marks {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  pointer-events: none;
}

.pencil-mark {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.55rem;
  font-weight: 600;
  font-style: normal;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  color: rgba(239, 230, 210, 0.55);
  line-height: 1;
}

.pencil-marks.notes-in { animation: notes-in 0.35s ease-out var(--d, 0ms) both; }

@keyframes notes-in {
  from { opacity: 0; transform: scale(0.8); }
  to { opacity: 1; transform: scale(1); }
}

/* ── Controls ── */
#controls {
  width: 100%;
  max-width: 450px;
  margin-top: 16px;
}

#number-pad {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 6px;
  margin-bottom: 12px;
  max-width: 100%;
}

.num-btn {
  border: 1px solid rgba(239, 230, 210, 0.14);
  border-radius: 12px;
  background: linear-gradient(180deg, rgba(255, 245, 225, 0.08), rgba(255, 245, 225, 0.03));
  color: #f3ead8;
  font-family: 'Iowan Old Style', 'Palatino Linotype', Palatino, Georgia, serif;
  font-size: 1.4rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.15s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 8px 0;
}

.num-btn:hover {
  background: rgba(246, 196, 83, 0.14);
  border-color: rgba(246, 196, 83, 0.7);
}

.num-btn.num-done-pop { animation: num-pop 0.5s cubic-bezier(.2, 1.6, .4, 1); }

@keyframes num-pop {
  40% { transform: scale(1.25); background: rgba(246, 196, 83, 0.45); }
  100% { transform: scale(1); }
}

.num-btn:active {
  transform: scale(0.92);
}

.num-btn.num-complete {
  opacity: 0.3;
  pointer-events: none;
}

.num-btn.num-active {
  background: #f6c453;
  color: #2a2014;
  border-color: #f6c453;
}

/* Action buttons */
#action-buttons {
  display: flex;
  gap: 8px;
  justify-content: center;
}

.action-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 10px 6px;
  border: 1px solid rgba(239, 230, 210, 0.12);
  border-radius: 12px;
  background: rgba(255, 245, 225, 0.04);
  color: #cfc5b0;
  font-size: 0.72rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
  min-height: 56px;
}

.action-btn:hover {
  background: rgba(255, 245, 225, 0.1);
  color: #fff;
}

.action-btn:active {
  transform: scale(0.95);
}

.action-btn.active {
  background: rgba(156, 192, 255, 0.16);
  color: #9cc0ff;
  border-color: #9cc0ff;
}

.action-btn-primary {
  border-color: rgba(246, 196, 83, 0.6);
  color: #f6c453;
}

.action-btn-primary:hover {
  background: rgba(246, 196, 83, 0.14);
  color: #ffd77a;
}

.action-icon {
  font-size: 1.3rem;
}

.action-label {
  white-space: nowrap;
}

/* ── Best Time Display ── */
#best-time-display {
  margin-top: 12px;
  font-size: 0.85rem;
  color: #666;
  text-align: center;
}

#best-time-display span {
  color: #f6c453;
  font-weight: 700;
}

/* ── Start Overlay ── */
#start-overlay {
  position: fixed;
  top: 0; left: 0;
  width: 100%; height: 100%;
  background: rgba(15, 15, 26, 0.95);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 50;
}

#start-overlay.hidden {
  display: none;
}

.overlay-box {
  text-align: center;
  padding: 30px;
}

.overlay-box h1 {
  font-size: 40px;
  margin-bottom: 8px;
  color: #f3ead8;
  font-family: 'Iowan Old Style', 'Palatino Linotype', Palatino, Georgia, serif;
  letter-spacing: 2px;
}

.overlay-subtitle {
  color: #888;
  margin-bottom: 16px;
}

.how-to-play {
  max-width: 340px;
  text-align: left;
  margin: 0 auto 16px;
  padding: 14px 18px;
  background: rgba(255, 255, 255, 0.04);
  border-radius: 8px;
  font-size: 13px;
  line-height: 1.7;
  color: #aaa;
}

.how-to-play-title {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 2px;
  color: #666;
  margin-bottom: 8px;
  text-transform: uppercase;
}

.overlay-btn {
  padding: 12px 36px;
  font-size: 16px;
  font-weight: 700;
  border: 0;
  border-radius: 999px;
  background: #f6c453;
  color: #2a2014;
  cursor: pointer;
  letter-spacing: 1px;
  transition: all 0.2s ease;
}

.overlay-btn:hover {
  background: #ffd77a;
}

/* ── Win Overlay ── */
#win-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 15, 26, 0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  backdrop-filter: blur(6px);
  animation: overlayIn 0.3s ease;
}

@keyframes overlayIn {
  0% { opacity: 0; }
  100% { opacity: 1; }
}

#win-overlay.hidden {
  display: none;
}

#win-overlay .overlay-box {
  background: linear-gradient(160deg, #2a2621, #1f1c18);
  border: 1px solid rgba(239, 230, 210, 0.25);
  border-radius: 16px;
  padding: 32px 40px;
  max-width: 380px;
  width: 90%;
  animation: slideUp 0.4s ease;
  z-index: 101;
}

@keyframes slideUp {
  0% { transform: translateY(40px); opacity: 0; }
  100% { transform: translateY(0); opacity: 1; }
}

#win-title {
  font-size: 1.7rem;
  margin-bottom: 16px;
  letter-spacing: 1px;
  color: #f6c453;
  font-family: 'Iowan Old Style', 'Palatino Linotype', Palatino, Georgia, serif;
}

#win-stats {
  font-size: 0.95rem;
  color: #a0a0b8;
  line-height: 1.8;
  margin-bottom: 20px;
}

#win-stats span {
  color: #e0e0e0;
  font-weight: 700;
}

#best-times-section {
  margin-bottom: 20px;
}

#best-times-section.hidden {
  display: none;
}

#best-times-section h3 {
  color: #ffa502;
  font-size: 1rem;
  margin-bottom: 8px;
}

#best-times-list {
  list-style: none;
  padding: 0;
  counter-reset: rank;
}

#best-times-list li {
  counter-increment: rank;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 0;
  font-size: 0.9rem;
  color: #a0a0b8;
  font-family: 'Courier New', monospace;
}

#best-times-list li::before {
  content: counter(rank) ".";
  color: #ffa502;
  font-weight: 700;
  min-width: 24px;
  text-align: right;
}

#best-times-list li.new-score {
  color: #2ed573;
  font-weight: 700;
}

#win-overlay .overlay-btn {
  background: #f6c453;
  color: #2a2014;
}

#win-overlay .overlay-btn:hover {
  background: #ffd77a;
}

/* ── Confetti Canvas ── */
#confetti-canvas {
  position: fixed;
  top: 0; left: 0;
  width: 100%; height: 100%;
  pointer-events: none;
  z-index: 102;
}

/* ── Mobile Responsive ── */
@media (max-width: 500px) {
  #app {
    padding: 8px;
  }

  #hud {
    padding: 8px 10px;
    gap: 6px;
    flex-wrap: wrap;
    justify-content: center;
  }

  .hud-display {
    font-size: 1rem;
    padding: 6px 10px;
    min-width: 85px;
  }

  .diff-btn {
    padding: 6px 10px;
    font-size: 0.75rem;
  }

  .sudoku-cell {
    font-size: 1.2rem;
  }

  .pencil-mark {
    font-size: 0.45rem;
  }

  .num-btn {
    font-size: 1.2rem;
    min-height: 44px;
    border-radius: 8px;
  }

  .action-btn {
    padding: 8px 4px;
    min-height: 50px;
  }

  .action-icon {
    font-size: 1.1rem;
  }

  .action-label {
    font-size: 0.65rem;
  }
}

@media (max-width: 339px) {
  #number-pad {
    grid-template-columns: repeat(5, 1fr);
    gap: 5px;
  }
}

@media (max-width: 420px) {
  #number-pad {
    gap: 4px;
  }

  .num-btn {
    font-size: 1.2rem;
    min-height: 42px;
  }
}

@media (max-width: 360px) {
  .sudoku-cell {
    font-size: 1rem;
  }

  .pencil-mark {
    font-size: 0.38rem;
  }

  .num-btn {
    font-size: 1rem;
    min-height: 38px;
  }

  #number-pad {
    gap: 4px;
  }

  #action-buttons {
    gap: 4px;
  }
}

#board {
  max-width: min(450px, max(240px, calc(100vh - var(--gp-header-h, 80px) - 250px)));
  max-width: min(450px, max(240px, calc(100dvh - var(--gp-header-h, 80px) - 250px)));
}

#start-overlay,
#win-overlay {
  top: var(--gp-header-h, 80px);
  height: auto;
  bottom: 0;
  overflow-y: auto;
  align-items: safe center;
}

@media (max-width: 500px) {
  #hud { flex-wrap: nowrap; justify-content: space-between; }
  .hud-display { min-width: 0; padding: 6px 8px; letter-spacing: 1px; }
  #difficulty-selector { gap: 4px; }
  .diff-btn { padding: 8px 7px; font-size: 0.72rem; }
  #controls { margin-top: 10px; }
  #number-pad { margin-bottom: 8px; }
}

@media (pointer: coarse) {
  .diff-btn { min-height: 36px; }
}

@media (orientation: landscape) and (max-height: 500px) {
  #app {
    max-width: 860px;
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    grid-template-rows: auto 1fr;
    column-gap: 16px;
    align-items: start;
    padding: 8px;
  }
  #hud { grid-column: 2; grid-row: 1; margin-bottom: 8px; flex-wrap: nowrap; }
  #board-wrapper { grid-column: 1; grid-row: 1 / span 2; width: auto; padding: 0; }
  #board {
    width: max(200px, calc(100vh - var(--gp-header-h, 48px) - 30px));
    width: max(200px, calc(100dvh - var(--gp-header-h, 48px) - 30px));
    max-width: none;
  }
  #controls { grid-column: 2; grid-row: 2; margin-top: 0; max-width: none; }
  #number-pad { grid-template-columns: repeat(5, 1fr); }
  .num-btn { min-height: 40px; }
  .action-btn { min-height: 46px; }
  #best-time-display { display: none; }
}

@media (orientation: landscape) and (max-height: 500px) {
  #start-overlay .overlay-box { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); column-gap: 24px; align-items: center; align-content: center; justify-items: center; width: min(780px, calc(100% - 24px)); max-width: none; }
  #start-overlay .overlay-box > * { grid-column: 2; }
  #start-overlay .overlay-box > .how-to-play { grid-column: 1; grid-row: 1 / span 6; margin: 0; max-width: 340px; width: 100%; }
}

@media (pointer: coarse) and (min-width: 700px) and (min-height: 700px) {
  #app { max-width: 640px; }
  #controls { max-width: 600px; }
  #board {
    max-width: min(600px, max(240px, calc(100vh - var(--gp-header-h, 80px) - 250px)));
    max-width: min(600px, max(240px, calc(100dvh - var(--gp-header-h, 80px) - 250px)));
  }
}

#board {
  grid-template-columns: repeat(9, minmax(0, 1fr));
  grid-template-rows: repeat(9, minmax(0, 1fr));
}

.sudoku-cell {
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

@media (orientation: landscape) and (max-height: 500px) {
  .sudoku-cell { font-size: 1rem; }
}

[data-theme="light"] html,
[data-theme="light"] body { background: var(--gp-page-grad) fixed; color: var(--gp-ink); }
[data-theme="light"] #hud { background: var(--gp-surface); color: var(--gp-ink); box-shadow: inset 0 0 0 1px var(--gp-line), var(--gp-shadow); }
[data-theme="light"] .diff-btn { background: var(--gp-surface-2); color: var(--gp-ink-2); }
[data-theme="light"] .diff-btn:hover { background: #e9e6f7; color: var(--gp-ink); }
[data-theme="light"] #best-time-display { color: var(--gp-ink-2); }
[data-theme="light"] #best-time-display span, [data-theme="light"] #best-times-section h3, [data-theme="light"] #best-times-list li::before { color: #c2410c; }
[data-theme="light"] #start-overlay, [data-theme="light"] #win-overlay { background: var(--gp-overlay); }
[data-theme="light"] .overlay-box h1 { color: var(--gp-ink); }
[data-theme="light"] .overlay-subtitle { color: var(--gp-ink-2); }
[data-theme="light"] .how-to-play { background: rgba(31, 27, 69, 0.05); border-color: var(--gp-line); color: var(--gp-ink-2); }
[data-theme="light"] .overlay-btn { color: #2a2014; }
[data-theme="light"] #board { background: radial-gradient(rgba(80, 60, 30, 0.05) 1px, transparent 1px) 0 0 / 5px 5px, linear-gradient(160deg, #fffdf7, #f6f0e2); border-color: rgba(60, 50, 40, 0.6); box-shadow: 0 16px 40px rgba(80, 60, 30, 0.16), inset 0 0 30px rgba(120, 90, 40, 0.06); }
[data-theme="light"] .sudoku-cell { border-color: rgba(60, 50, 40, 0.14); color: #2a4d9c; }
[data-theme="light"] .sudoku-cell.box-right { border-right-color: rgba(60, 50, 40, 0.55); }
[data-theme="light"] .sudoku-cell.box-bottom { border-bottom-color: rgba(60, 50, 40, 0.55); }
[data-theme="light"] .sudoku-cell.box-left { border-left-color: rgba(60, 50, 40, 0.55); }
[data-theme="light"] .sudoku-cell.box-top { border-top-color: rgba(60, 50, 40, 0.55); }
[data-theme="light"] .sudoku-cell.given { color: #2b2620; }
[data-theme="light"] .sudoku-cell.selected { background: rgba(255, 214, 70, 0.42); box-shadow: inset 0 0 0 2px rgba(214, 160, 20, 0.9); }
[data-theme="light"] .sudoku-cell.same-region { background-color: rgba(80, 60, 30, 0.05); }
[data-theme="light"] .sudoku-cell.same-number { background: radial-gradient(circle, rgba(42, 77, 156, 0.16) 0 55%, transparent 58%); }
[data-theme="light"] .sudoku-cell.error { color: #c92a2a; background: rgba(201, 42, 42, 0.08); }
[data-theme="light"] .sudoku-cell.conflict { background-color: rgba(201, 42, 42, 0.07); }
[data-theme="light"] .pencil-mark { color: #8a8178; }
[data-theme="light"] .num-btn { background: #fffdf7; color: #2b2620; border-color: rgba(60, 50, 40, 0.18); box-shadow: 0 2px 6px rgba(80, 60, 30, 0.08); }
[data-theme="light"] .action-btn { background: #fffdf7; color: #4a4238; border-color: rgba(60, 50, 40, 0.16); }
[data-theme="light"] .action-btn.active { color: #2a4d9c; border-color: #2a4d9c; background: rgba(42, 77, 156, 0.08); }
[data-theme="light"] .action-btn-primary { color: #a16207; border-color: rgba(161, 98, 7, 0.5); }
[data-theme="light"] .hud-display { background: #fffdf7; border-color: rgba(60, 50, 40, 0.14); }
[data-theme="light"] #timer { color: #a16207; }
[data-theme="light"] .hud-mistakes #mistakes, [data-theme="light"] .hud-mistakes .hud-icon { color: #c92a2a; }
[data-theme="light"] .diff-btn.active { background: #2b2620; color: #fffdf7; border-color: #2b2620; }
[data-theme="light"] .overlay-box h1 { color: #2b2620; }
[data-theme="light"] #win-title { color: #a16207; }
[data-theme="light"] #win-overlay .overlay-box { background: var(--gp-surface); color: var(--gp-ink); box-shadow: var(--gp-shadow); }
[data-theme="light"] #win-title, [data-theme="light"] #best-times-list li.new-score { color: #15803d; }
[data-theme="light"] #win-stats, [data-theme="light"] #best-times-list li { color: var(--gp-ink-2); }
[data-theme="light"] #win-stats span { color: var(--gp-ink); }
[data-theme="light"] #win-overlay .overlay-btn { color: #fff; }

.gp-btn-music.off { opacity: .4; }
