:root {
  --bg: #ffffff;
  --card: #ffffff;
  --text: #1f2937;
  --muted: #6b7280;
  --border: #e5e7eb;
  --accent: #334155;
  --grid-bg: #ffffff;
  --cell-empty: #fafafa;
  --cell-border: #e5e7eb;
  --error: #ef4444;
  --warn: #fbbf24;
  --shadow: 0 8px 24px rgba(15, 23, 42, 0.08);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  overflow: hidden;
}

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

/* Topbar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: max(env(safe-area-inset-top), 8px) 12px 8px;
  gap: 8px;
  flex-shrink: 0;
  position: relative;
  z-index: 70;
  background: var(--bg);
}

.icon-btn {
  width: 40px;
  height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
  padding: 0;
  transition: background 100ms ease, border-color 100ms ease;
}

.icon-btn:hover, .icon-btn:focus-visible {
  background: #f3f4f6;
  outline: none;
}

.burger {
  position: relative;
  width: 18px;
  height: 2px;
  background: var(--text);
  border-radius: 1px;
  display: block;
}

.burger::before,
.burger::after {
  content: "";
  position: absolute;
  left: 0;
  width: 18px;
  height: 2px;
  background: var(--text);
  border-radius: 1px;
}

.burger::before { top: -6px; }
.burger::after { top: 6px; }

.level-badge {
  font-size: 14px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  font-weight: 500;
}

.icon-reload,
.icon-undo {
  font-size: 22px;
  line-height: 1;
  color: var(--text);
  display: inline-block;
  transform: translateY(-1px);
}

.icon-undo {
  transform: translateY(-1px) scaleX(-1);
}

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

.topbar-actions {
  display: flex;
  gap: 6px;
  align-items: center;
}

/* Menu */
.menu-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(255, 255, 255, 0.85);
  z-index: 50;
  opacity: 1;
  transition: opacity 150ms ease;
}

.menu-backdrop.hidden {
  opacity: 0;
  pointer-events: none;
}

.menu {
  position: fixed;
  top: calc(max(env(safe-area-inset-top), 8px) + 56px);
  left: 8px;
  right: 8px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 14px;
  z-index: 60;
  display: flex;
  flex-direction: column;
  gap: 12px;
  transform-origin: top center;
  transition: opacity 150ms ease, transform 150ms ease;
}

@media (min-width: 640px) {
  .menu {
    left: 50%;
    right: auto;
    width: min(420px, calc(100vw - 24px));
    transform: translateX(-50%);
  }
  .menu.hidden {
    transform: translateX(-50%) scale(0.97) translateY(-4px);
  }
}

.menu.hidden {
  opacity: 0;
  pointer-events: none;
  transform: scale(0.97) translateY(-4px);
}

.menu-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.menu-section + .menu-section {
  border-top: 1px solid var(--border);
  padding-top: 10px;
}

.menu-page {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.menu-page.hidden {
  display: none;
}

.menu-title {
  margin: 4px 4px 8px;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.home-logo {
  margin: 4px 4px 16px;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 10px;
}

.home-logo .logo-num {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 6px;
  background: hsl(200, 65%, 86%);
  border: 1px solid hsl(200, 50%, 56%);
  color: hsl(200, 60%, 24%);
  font-size: 18px;
  font-weight: 700;
  position: relative;
}

.home-logo .logo-num::after {
  content: '';
  position: absolute;
  top: 3px;
  right: 3px;
  width: 3px;
  height: 3px;
  background: hsl(200, 55%, 50%);
  border-radius: 50%;
}

.home-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.puzzle-cell {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 18px 16px;
  border-radius: 12px;
  background: var(--pc-fill, hsl(200, 65%, 88%));
  border: 1px solid var(--pc-stroke, hsl(200, 50%, 60%));
  color: var(--pc-text, hsl(200, 60%, 24%));
  cursor: pointer;
  font-family: inherit;
  text-align: left;
  transition: transform 100ms ease, box-shadow 100ms ease;
  min-height: 78px;
}

.puzzle-cell:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.06);
}

.puzzle-cell:active {
  transform: translateY(0);
}

.puzzle-cell.big {
  grid-column: 1 / -1;
  padding: 22px 20px;
  min-height: 92px;
}

.puzzle-cell.slim {
  grid-column: 1 / -1;
  padding: 12px 16px;
  min-height: 0;
}

.puzzle-anchor {
  position: absolute;
  top: 6px;
  right: 7px;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--pc-stroke, currentColor);
  opacity: 0.7;
}

.puzzle-num {
  font-size: 32px;
  font-weight: 800;
  line-height: 1;
  flex-shrink: 0;
  color: var(--pc-text, currentColor);
}

.puzzle-cell.big .puzzle-num {
  font-size: 44px;
}

.puzzle-cell.slim .puzzle-num {
  font-size: 18px;
}

.puzzle-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  flex: 1;
}

.puzzle-title {
  font-size: 17px;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: var(--pc-text, currentColor);
}

.puzzle-cell.big .puzzle-title {
  font-size: 22px;
}

.puzzle-cell.slim .puzzle-title {
  font-size: 14px;
  font-weight: 600;
}

.puzzle-sub {
  font-size: 12px;
  opacity: 0.75;
  font-variant-numeric: tabular-nums;
}

.hue-blue {
  --pc-fill: hsl(200, 65%, 88%);
  --pc-stroke: hsl(200, 50%, 58%);
  --pc-text: hsl(200, 60%, 22%);
}
.hue-amber {
  --pc-fill: hsl(35, 80%, 84%);
  --pc-stroke: hsl(30, 65%, 55%);
  --pc-text: hsl(28, 65%, 24%);
}
.hue-mint {
  --pc-fill: hsl(140, 55%, 86%);
  --pc-stroke: hsl(140, 40%, 50%);
  --pc-text: hsl(140, 60%, 20%);
}
.hue-rose {
  --pc-fill: hsl(340, 70%, 88%);
  --pc-stroke: hsl(340, 55%, 58%);
  --pc-text: hsl(340, 65%, 25%);
}
.hue-lavender {
  --pc-fill: hsl(275, 55%, 90%);
  --pc-stroke: hsl(275, 35%, 60%);
  --pc-text: hsl(275, 50%, 28%);
}

.puzzle-cell .streak-pill {
  position: absolute;
  top: 8px;
  right: 16px;
}

.menu-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 4px;
}

.menu-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}

.back-btn {
  width: 36px;
  height: 36px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
  font-size: 16px;
  cursor: pointer;
  padding: 0;
  font-family: inherit;
}

.back-btn:hover { background: #f3f4f6; }

.menu-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
  font-family: inherit;
  font-size: 15px;
  font-weight: 500;
  color: var(--text);
  text-align: left;
  width: 100%;
}

.menu-item:hover { background: #f3f4f6; }

.menu-item-icon {
  font-size: 18px;
  width: 24px;
  text-align: center;
}

.menu-item-label {
  flex: 1;
}

.menu-item-arrow {
  color: var(--muted);
  font-size: 18px;
}

.menu-item-toggle {
  width: 38px;
  height: 22px;
  background: var(--border);
  border-radius: 99px;
  position: relative;
  transition: background 150ms ease;
  flex-shrink: 0;
}

.menu-item-toggle::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 18px;
  height: 18px;
  background: white;
  border-radius: 50%;
  transition: left 150ms ease;
  box-shadow: 0 1px 2px rgba(0,0,0,0.15);
}

.menu-item.toggle[aria-pressed="true"] .menu-item-toggle {
  background: hsl(140, 60%, 55%);
}

.menu-item.toggle[aria-pressed="true"] .menu-item-toggle::after {
  left: 18px;
}

.menu-help {
  margin: 0 4px 4px;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.4;
}

.streak-pill {
  font-size: 12px;
  background: #fef3c7;
  color: #b45309;
  padding: 2px 8px;
  border-radius: 99px;
  font-weight: 600;
}

.streak-pill.hidden { display: none; }

.menu-label {
  font-size: 12px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.menu-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.menu-btn {
  flex-shrink: 0;
  padding: 9px 12px;
  font-size: 14px;
  font-weight: 500;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  cursor: pointer;
  font-family: inherit;
  min-height: 40px;
  transition: background 100ms ease, border-color 100ms ease;
}

.menu-btn:hover, .menu-btn:focus-visible {
  background: #f3f4f6;
  outline: none;
}

.menu-btn.wide {
  flex: 1 1 auto;
  text-align: center;
}

#level-input {
  flex: 1 1 auto;
  min-width: 0;
  padding: 8px 10px;
  font-size: 16px;
  font-variant-numeric: tabular-nums;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
  color: var(--text);
  text-align: center;
  min-height: 40px;
  font-family: inherit;
}

.menu-size {
  font-size: 13px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.menu-btn.highlight {
  background: var(--text);
  color: white;
  border-color: var(--text);
  font-weight: 600;
}

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

.streak {
  font-size: 13px;
  color: #b45309;
  background: #fef3c7;
  border-radius: 8px;
  padding: 6px 10px;
  text-align: center;
  font-weight: 500;
}

.streak.hidden {
  display: none;
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(40px, 1fr));
  gap: 6px;
  max-height: 50vh;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 2px;
  margin: 0 -2px;
  -webkit-overflow-scrolling: touch;
}

.level-cell {
  position: relative;
  aspect-ratio: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 500;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  padding: 0;
  font-family: inherit;
  color: var(--text);
  min-height: 0;
  min-width: 0;
  width: 100%;
  box-sizing: border-box;
  overflow: hidden;
  transition: background 100ms ease;
}

.level-cell:hover {
  background: #f3f4f6;
}

.level-cell.solved {
  background: hsl(140, 55%, 90%);
  border-color: hsl(140, 40%, 70%);
  color: hsl(140, 50%, 25%);
}

.level-cell.solved .star {
  position: absolute;
  top: 1px;
  right: 2px;
  font-size: 8px;
  color: hsl(40, 90%, 50%);
  line-height: 1;
}

.level-cell.current {
  outline: 2px solid var(--text);
  outline-offset: -2px;
  font-weight: 700;
}

.level-cell .cell-num {
  display: block;
  font-size: 12px;
  line-height: 1;
}

.level-cell .cell-stars {
  display: block;
  font-size: 7px;
  letter-spacing: 0.5px;
  line-height: 1;
  margin-top: 1px;
  color: hsl(40, 90%, 50%);
}

.level-cell.stars-1 .cell-stars { color: hsl(40, 70%, 60%); }
.level-cell.stars-2 .cell-stars { color: hsl(40, 85%, 55%); }
.level-cell.stars-3 .cell-stars { color: hsl(40, 95%, 50%); }

.win-stars {
  display: inline-block;
  color: hsl(40, 95%, 50%);
  font-size: 28px;
  letter-spacing: 4px;
}

.win-time {
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}

.achievement-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 60vh;
  overflow-y: auto;
}

.achievement-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.achievement-row.earned {
  background: hsl(140, 55%, 95%);
  border-color: hsl(140, 40%, 70%);
}

.achievement-row .ach-icon {
  font-size: 24px;
  width: 32px;
  text-align: center;
  filter: grayscale(100%);
  opacity: 0.5;
}

.achievement-row.earned .ach-icon {
  filter: none;
  opacity: 1;
}

.achievement-row .ach-body {
  flex: 1;
  min-width: 0;
}

.achievement-row .ach-title {
  font-weight: 600;
  font-size: 14px;
  color: var(--text);
}

.achievement-row .ach-desc {
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
}

.achievement-row .ach-check {
  color: hsl(140, 50%, 45%);
  font-weight: bold;
  font-size: 18px;
}

.achievement-toasts {
  position: fixed;
  top: 12px;
  bottom: auto;
  left: 50%;
  right: auto;
  transform: translateX(-50%);
  z-index: 9999;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
  width: min(360px, calc(100vw - 24px));
  margin: 0;
  padding: 0;
}

@supports (top: env(safe-area-inset-top)) {
  .achievement-toasts {
    top: calc(env(safe-area-inset-top) + 12px);
  }
}

.achievement-toast {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: linear-gradient(135deg, hsl(45, 100%, 96%) 0%, hsl(38, 95%, 88%) 100%);
  border: 1px solid hsl(40, 80%, 65%);
  border-radius: 14px;
  box-shadow: 0 10px 30px rgba(120, 80, 0, 0.25), 0 2px 6px rgba(0,0,0,0.08);
  color: hsl(30, 60%, 20%);
  width: 100%;
  pointer-events: auto;
  animation: toast-in 360ms cubic-bezier(0.2, 0.9, 0.3, 1.2);
  transition: transform 280ms ease, opacity 280ms ease;
}

.achievement-toast.leaving {
  transform: translateY(-20px);
  opacity: 0;
}

.achievement-toast .toast-icon {
  font-size: 28px;
  flex-shrink: 0;
  filter: drop-shadow(0 1px 2px rgba(0,0,0,0.15));
}

.achievement-toast .toast-body {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  flex: 1;
}

.achievement-toast .toast-eyebrow {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 1.2px;
  text-transform: uppercase;
  color: hsl(35, 70%, 35%);
  opacity: 0.85;
}

.achievement-toast .toast-title {
  font-size: 15px;
  font-weight: 600;
  color: hsl(30, 60%, 18%);
}

.achievement-toast .toast-desc {
  font-size: 12px;
  color: hsl(30, 35%, 32%);
  line-height: 1.3;
}

@keyframes toast-in {
  0% { transform: translateY(-30px) scale(0.9); opacity: 0; }
  60% { transform: translateY(4px) scale(1.02); opacity: 1; }
  100% { transform: translateY(0) scale(1); opacity: 1; }
}

.size-row {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 6px;
}

.size-btn {
  text-align: center;
}

.size-btn.active {
  background: var(--text);
  color: white;
  border-color: var(--text);
}

#strict-btn.active {
  background: hsl(220, 30%, 95%);
  border-color: hsl(220, 40%, 70%);
}

.picture-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.picture-cell {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  padding: 10px 12px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  font-family: inherit;
  color: var(--text);
  text-align: left;
  font-weight: 500;
  font-size: 14px;
  transition: background 100ms ease;
}

.picture-cell:hover {
  background: #f3f4f6;
}

.picture-cell.solved {
  background: hsl(140, 55%, 92%);
  border-color: hsl(140, 40%, 70%);
}

.picture-cell.solved .star {
  position: absolute;
  top: 6px;
  right: 8px;
  color: hsl(40, 90%, 50%);
  font-size: 12px;
}

.picture-cell.current {
  outline: 2px solid var(--text);
  outline-offset: -2px;
}

.picture-title {
  font-weight: 600;
}

.picture-size {
  font-size: 11px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

#size-display {
  display: inline-block;
  margin-left: 6px;
  text-transform: none;
  letter-spacing: 0;
}

/* Board */
.board-wrap {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 4px 8px 8px;
}

#board {
  display: block;
  width: min(calc(100vw - 16px), calc(100vh - 100px));
  height: min(calc(100vw - 16px), calc(100vh - 100px));
  touch-action: none;
  user-select: none;
  cursor: pointer;
}

@media (min-width: 768px) {
  .board-wrap {
    padding: 16px;
  }
  #board {
    width: min(720px, calc(100vw - 32px), calc(100vh - 120px));
    height: min(720px, calc(100vw - 32px), calc(100vh - 120px));
  }
}

/* Win overlay */
.win-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.86);
  backdrop-filter: blur(2px);
  z-index: 40;
  transition: opacity 150ms ease;
}

.win-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

.win-card {
  background: var(--card);
  padding: 24px 28px;
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  text-align: center;
  min-width: 220px;
}

.win-card h2 {
  margin: 0 0 4px;
  font-size: 22px;
  font-weight: 700;
}

.win-card p {
  margin: 0 0 14px;
  color: var(--muted);
  font-size: 14px;
}

.win-card button {
  padding: 10px 16px;
  background: var(--text);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  font-family: inherit;
}

.win-card button:hover {
  background: var(--accent);
}
