/* ---- Reset & root ---- */

:root {
  --bg: #07080f;
  --panel: rgba(12, 16, 28, 0.75);
  --panel-border: rgba(148, 163, 184, 0.18);
  --fg: #f5f5ff;
  --muted: #9fa3c5;
  --accent: #7dd3fc;
  --error: #ff5d5d;
  --good: #44d17a;
  --board-max: 580px;
  --region-hl: rgba(125, 211, 252, 0.06);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { height: 100%; }
body {
  height: 100%;
  overflow: hidden;
  color: var(--fg);
  font-family: "Inter", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  background: var(--bg);
  display: flex;
  flex-direction: column;
}

/* ---- Menu screen ---- */

.menu-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 40px 24px;
  text-align: center;
  animation: menuFadeIn 0.4s ease;
}
.menu-screen.hidden { display: none; }
@keyframes menuFadeIn {
  from { opacity: 0; transform: translateY(12px); }
  to   { opacity: 1; transform: translateY(0); }
}

.menu-title {
  font-size: clamp(2rem, 5vw, 3.2rem);
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  margin-bottom: 28px;
}

.menu-stats {
  display: flex;
  gap: 32px;
  margin-bottom: 24px;
}
.menu-stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}
.menu-stat-label {
  font-size: 0.72rem;
  color: var(--muted);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.menu-stat-value {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.menu-tagline {
  color: var(--muted);
  font-size: 0.88rem;
  line-height: 1.6;
  max-width: 380px;
  margin-bottom: 28px;
}

.menu-difficulty {
  margin-bottom: 28px;
}
.menu-diff-label {
  display: block;
  font-size: 0.72rem;
  color: var(--muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 10px;
}
.menu-diff-options {
  display: flex;
  gap: 0;
  border: 1px solid rgba(148, 163, 184, 0.2);
  border-radius: 12px;
  overflow: hidden;
  background: rgba(6, 11, 26, 0.9);
}
.menu-diff-btn {
  flex: 1;
  padding: 10px 18px;
  background: transparent;
  border: none;
  color: var(--muted);
  font-family: inherit;
  font-size: 0.82rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
  position: relative;
}
.menu-diff-btn:not(:first-child)::before {
  content: "";
  position: absolute;
  left: 0;
  top: 20%;
  bottom: 20%;
  width: 1px;
  background: rgba(148, 163, 184, 0.15);
}
.menu-diff-btn:hover { background: rgba(40, 50, 70, 0.6); color: var(--fg); }
.menu-diff-btn.active {
  background: rgba(125, 211, 252, 0.12);
  color: var(--accent);
  font-weight: 600;
}

.menu-actions {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}
.menu-play-btn {
  padding: 12px 48px;
  font-size: 1rem;
  font-weight: 700;
  border-radius: 14px;
  letter-spacing: 0.02em;
}
.menu-continue-btn {
  font-size: 0.82rem;
}

/* ---- Game screen ---- */

.game-screen {
  display: flex;
  flex-direction: column;
  height: 100%;
  animation: menuFadeIn 0.3s ease;
}
.game-screen.hidden { display: none; }

/* ---- Top bar: centered title, actions flanking ---- */

.top {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 12px;
  padding: 0.4rem 0.75rem 0.5rem;
  flex-shrink: 0;
  position: relative;
  z-index: 10;
}

.top-side {
  display: flex;
  align-items: center;
  gap: 8px;
}
.top-side-left  { justify-content: flex-start; }
.top-side-right { justify-content: flex-end; flex-wrap: wrap; }

.top-center { text-align: center; }
.top-center h1 {
  margin: 0;
  font-size: clamp(1.4rem, 3vw, 2.1rem);
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

/* ---- Meta info (time, best, lives) ---- */

.diff-pill {
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--accent);
  background: rgba(125, 211, 252, 0.1);
  border: 1px solid rgba(125, 211, 252, 0.2);
  padding: 3px 10px;
  border-radius: 999px;
}

.meta-label {
  font-size: 0.72rem;
  color: var(--muted);
  font-weight: 500;
}
.meta-value {
  font-size: 0.78rem;
  color: var(--fg);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.meta-sep {
  color: var(--muted);
  opacity: 0.4;
  font-size: 0.7rem;
}

/* ---- Ghost buttons (matches Pong/Hearts pattern) ---- */

.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  border-radius: 999px;
  border: 1px solid transparent;
  padding: 0.35rem 0.85rem;
  font-family: inherit;
  font-size: 0.78rem;
  font-weight: 500;
  cursor: pointer;
  background: transparent;
  color: var(--fg);
  white-space: nowrap;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.08s ease;
}
.btn.ghost {
  border-color: rgba(255, 255, 255, 0.18);
  background: rgba(6, 11, 26, 0.9);
}
.btn.ghost:hover {
  background: rgba(40, 50, 70, 0.8);
  transform: translateY(-1px);
}
.btn.primary {
  background: var(--accent);
  color: #0b0b18;
  border-color: rgba(125, 211, 252, 0.35);
  font-weight: 600;
}
.btn.primary:hover {
  background: #9be0fd;
  transform: translateY(-1px);
}
.btn:disabled { opacity: 0.35; pointer-events: none; }

/* Icon-only buttons — compact circles */
.icon-btn {
  padding: 0.4rem;
  border-radius: 10px;
  min-width: 36px;
  min-height: 36px;
  justify-content: center;
}
.icon-btn .icon {
  width: 18px;
  height: 18px;
}

.hidden { display: none !important; }

/* New-game confirm popup */
.new-game-group {
  position: relative;
}
.confirm-popup {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0.45rem 0.75rem;
  background: radial-gradient(circle at 30% 0%, rgba(30, 40, 70, 0.95), rgba(12, 16, 28, 0.98));
  border: 1px solid rgba(148, 163, 184, 0.2);
  border-radius: 14px;
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.6);
  white-space: nowrap;
  z-index: 50;
  opacity: 0;
  transform: translateY(-4px) scale(0.95);
  transition: opacity 0.15s ease, transform 0.15s ease;
  pointer-events: none;
}
.confirm-popup.show {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}
.confirm-popup-label {
  font-size: 0.78rem;
  color: var(--muted);
  font-weight: 500;
}

/* ---- Lives ---- */

.lives-display { display: flex; gap: 3px; align-items: center; }
.lives-display .heart {
  font-size: 14px;
  color: var(--error);
  transition: opacity 0.3s ease, transform 0.3s ease;
}
.lives-display .heart.lost {
  opacity: 0.2;
  transform: scale(0.8);
}

/* ---- Wrapper: fills remaining space, centers game ---- */

.wrapper {
  flex: 1 1 0;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  padding: 0 16px 16px;
}

.game-area {
  width: min(var(--board-max), 92vmin);
}

/* ---- Board ---- */

.board {
  width: 100%;
  aspect-ratio: 1 / 1;
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  background: #0c1020;
  border: 2px solid rgba(148, 163, 184, 0.25);
  border-radius: 14px;
  overflow: hidden;
}

.cell {
  position: relative;
  border: 0.5px solid rgba(148, 163, 184, 0.15);
  display: grid;
  place-items: center;
  transition: background 0.15s ease;
}

/* 3x3 subgrid separators */
.cell[data-r="0"], .cell[data-r="3"], .cell[data-r="6"] { border-top: 1.5px solid rgba(148, 163, 184, 0.35); }
.cell[data-r="2"], .cell[data-r="5"], .cell[data-r="8"] { border-bottom: 1.5px solid rgba(148, 163, 184, 0.35); }
.cell[data-c="0"], .cell[data-c="3"], .cell[data-c="6"] { border-left: 1.5px solid rgba(148, 163, 184, 0.35); }
.cell[data-c="2"], .cell[data-c="5"], .cell[data-c="8"] { border-right: 1.5px solid rgba(148, 163, 184, 0.35); }

/* Region highlight */
.cell.hl-region { background: var(--region-hl); }

input.sudo {
  width: 100%; height: 100%;
  text-align: center;
  font-size: clamp(16px, 3.2vw, 28px);
  font-family: inherit;
  font-weight: 600;
  background: transparent;
  color: var(--fg);
  border: none; outline: none;
  transition: opacity 0.15s ease, transform 0.15s ease;
}
input.sudo[readonly] { color: var(--muted); font-weight: 400; }

/* Number pop-in */
input.sudo.pop-in { animation: popIn 0.2s ease-out; }
@keyframes popIn {
  0%   { transform: scale(0.5); opacity: 0.3; }
  60%  { transform: scale(1.12); }
  100% { transform: scale(1); opacity: 1; }
}

/* Error shake */
.cell.shake { animation: cellShake 0.35s ease-out; }
@keyframes cellShake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-4px); }
  40% { transform: translateX(4px); }
  60% { transform: translateX(-3px); }
  80% { transform: translateX(2px); }
}

.cell.error { background: rgba(255, 93, 93, 0.12); }
.cell.fixed { background: rgba(255, 255, 255, 0.02); }
.cell.same { background: rgba(167, 139, 250, 0.2); box-shadow: inset 0 0 0 1px rgba(167, 139, 250, 0.3); }
.cell.same.error { background: rgba(255, 93, 93, 0.3); box-shadow: none; }
.cell.focused { box-shadow: inset 0 0 0 2px var(--accent); }

/* Pencil notes */
.notes-grid {
  position: absolute;
  inset: 2px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  pointer-events: none;
  z-index: 1;
}
.notes-grid span {
  display: grid;
  place-items: center;
  font-size: clamp(7px, 1.2vw, 11px);
  color: var(--accent);
  opacity: 0.8;
  line-height: 1;
  font-weight: 600;
}

/* Win cascade */
.cell.win-cascade { animation: winPulse 0.5s ease-out forwards; }
@keyframes winPulse {
  0%   { background: rgba(68, 209, 122, 0); transform: scale(1); }
  50%  { background: rgba(68, 209, 122, 0.25); transform: scale(1.06); }
  100% { background: rgba(68, 209, 122, 0.06); transform: scale(1); }
}

/* ---- Toolbar (notes toggle + numbers bar) ---- */

.toolbar {
  display: flex;
  align-items: stretch;
  gap: 8px;
  margin-top: 10px;
}

.notes-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 8px 14px;
  border-radius: 12px;
  background: rgba(6, 11, 26, 0.9);
  border: 1px solid rgba(255, 255, 255, 0.18);
  color: var(--muted);
  font-family: inherit;
  font-size: 0.6rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
  flex-shrink: 0;
}
.notes-btn:hover { border-color: var(--accent); }
.notes-btn.active {
  background: rgba(125, 211, 252, 0.12);
  border-color: var(--accent);
  color: var(--accent);
}
.notes-btn svg { width: 16px; height: 16px; }

.numbers {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(10, minmax(0, 1fr));
  gap: 0;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 12px;
  overflow: hidden;
  background: rgba(6, 11, 26, 0.9);
}

.numbers button {
  position: relative;
  display: grid;
  place-items: center;
  padding: 12px 0;
  background: transparent;
  color: var(--fg);
  border: 0;
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  transition: background 120ms ease, box-shadow 120ms ease;
}

.numbers button:not(:first-child)::before {
  content: "";
  position: absolute;
  left: 0;
  top: 20%;
  bottom: 20%;
  width: 1px;
  background: rgba(148, 163, 184, 0.15);
}

.numbers button:hover,
.numbers button:focus-visible {
  background: rgba(40, 50, 70, 0.6);
  color: var(--fg);
  outline: none;
}

.numbers button.done {
  background: rgba(68, 209, 122, 0.1);
  box-shadow: inset 0 0 0 2px var(--good);
}
.numbers button.done:hover,
.numbers button.done:focus-visible {
  background: rgba(68, 209, 122, 0.18);
}

/* ---- Overlays (glass morphism, matches Pong/Hearts) ---- */

.overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  z-index: 1000;
  opacity: 0;
  transition: opacity 0.25s ease;
  pointer-events: none;
}
.overlay:not(.hidden) {
  opacity: 1;
  pointer-events: auto;
}

.modal {
  background: radial-gradient(circle at 30% 0%, rgba(30, 40, 70, 0.9), rgba(12, 16, 28, 0.95));
  border: 1px solid rgba(148, 163, 184, 0.16);
  border-radius: 20px;
  padding: 1.5rem 2rem;
  text-align: center;
  max-width: 380px;
  width: 90%;
  box-shadow: 0 24px 56px rgba(0, 0, 0, 0.8);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  transform: scale(0.92) translateY(8px);
  transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.overlay:not(.hidden) .modal {
  transform: scale(1) translateY(0);
}

.modal h2 {
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  margin-bottom: 6px;
}
.modal p {
  color: var(--muted);
  font-size: 0.82rem;
  margin-bottom: 4px;
  line-height: 1.5;
}
.modal .btn {
  margin-top: 16px;
}
.modal-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 16px;
}
.modal-actions .btn { margin-top: 0; }

/* ---- Stats modal ---- */

.stats-modal { max-width: 420px; text-align: left; }
.stats-modal h2 { text-align: center; margin-bottom: 14px; }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 16px;
}
.stat-card {
  text-align: center;
  padding: 10px 6px;
  background: rgba(255, 255, 255, 0.04);
  border-radius: 10px;
  border: 1px solid rgba(148, 163, 184, 0.1);
}
.stat-value {
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}
.stat-label {
  font-size: 0.68rem;
  color: var(--muted);
  margin-top: 2px;
  font-weight: 500;
}

.stats-section-title {
  font-size: 0.72rem;
  color: var(--muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 8px;
}

.stats-diff-grid {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 16px;
}
.stats-diff-card {
  display: grid;
  grid-template-columns: 8px 1fr;
  grid-template-rows: auto auto;
  column-gap: 10px;
  row-gap: 2px;
  align-items: center;
  padding: 8px 12px;
  background: rgba(255, 255, 255, 0.03);
  border-radius: 8px;
  border: 1px solid rgba(148, 163, 184, 0.08);
}
.stats-diff-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  grid-row: 1 / -1;
}
.stats-diff-name {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--fg);
}
.stats-diff-detail {
  grid-column: 2;
  font-size: 0.7rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.stats-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 12px;
}

/* ---- Best time flash ---- */

.new-best-flash {
  animation: bestFlash 1.5s ease-out;
}
@keyframes bestFlash {
  0%, 100% { color: var(--fg); }
  15% { color: #44d17a; text-shadow: 0 0 10px rgba(68, 209, 122, 0.6); }
  30% { color: var(--fg); }
  45% { color: #44d17a; text-shadow: 0 0 10px rgba(68, 209, 122, 0.6); }
  60% { color: var(--fg); }
  75% { color: #44d17a; text-shadow: 0 0 8px rgba(68, 209, 122, 0.4); }
  90% { color: var(--fg); text-shadow: none; }
}

/* ---- Resume indicator ---- */

.resume-indicator {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(12, 16, 28, 0.9);
  border: 1px solid var(--panel-border);
  color: var(--fg);
  font-size: 0.78rem;
  padding: 0.4rem 1.2rem;
  border-radius: 999px;
  z-index: 999;
  opacity: 0;
  transition: opacity 0.3s ease;
  pointer-events: none;
  backdrop-filter: blur(8px);
}
.resume-indicator.show { opacity: 1; }

/* ---- Confetti ---- */

#confettiCanvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1001;
}

/* ---- Responsive ---- */

@media (max-width: 600px) {
  .menu-screen { padding: 32px 16px; }
  .menu-diff-btn { padding: 10px 12px; font-size: 0.75rem; }
  .top {
    grid-template-columns: 1fr;
    text-align: center;
    gap: 6px;
    padding: 0.3rem 0.5rem;
  }
  .top-side { justify-content: center; flex-wrap: wrap; }
  .top-center h1 { font-size: 1.3rem; }
  .wrapper { padding: 0 8px 8px; }
}

@media (max-width: 420px) {
  .numbers {
    grid-template-columns: repeat(5, minmax(0, 1fr));
  }
}
