:root {
  --bg: #fbf6ec;
  --card: #ffffff;
  --ink: #4a3f33;
  --ink-soft: #8d857a;
  --accent: #6b4226;
  --accent-2: #ff7a59;
  --grid-line: #ece2d2;
  --clue-bg: #fffdf8;
  --chip-bg: #fff0e8;
  --chip-ink: #c8643f;
  --bad: #ff5a5a;
  --shadow: 0 12px 30px rgba(95, 80, 60, 0.12);
  --shadow-sm: 0 4px 12px rgba(95, 80, 60, 0.1);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }

body {
  font-family: "Nunito", "Quicksand", -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  background: radial-gradient(1200px 800px at 50% -10%, #fffdf8, var(--bg)) fixed;
  color: var(--ink);
  min-height: 100vh;
  display: block;       /* not flex — see Cuppadoku note re: cuppa-game-header */
  padding-top: 52px;
  user-select: none;
  -webkit-user-select: none;
}

.app {
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
  padding: 16px 16px 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

/* ---------- header ---------- */
.app-header { width: 100%; display: flex; align-items: center; justify-content: space-between; }
.brand {
  display: flex; align-items: center; gap: 9px;
  font-weight: 800; font-size: 1.45rem; letter-spacing: -0.02em; color: var(--accent);
}
.brand-sq {
  width: 22px; height: 22px; border-radius: 6px;
  background: linear-gradient(135deg, #ff6b6b 0%, #ffd43b 45%, #4dabf7 100%);
  box-shadow: var(--shadow-sm);
}
.header-actions { display: flex; align-items: center; gap: 8px; }
.icon-btn {
  width: 44px; height: 44px; border-radius: 12px; border: none;
  background: #fff; color: var(--accent); box-shadow: var(--shadow-sm);
  font-size: 1.1rem; font-weight: 800; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn svg { width: 22px; height: 22px; }
.pill-btn {
  min-height: 44px; padding: 0 18px; border-radius: 22px; border: none; cursor: pointer;
  background: linear-gradient(135deg, var(--accent-2), #ff9f43); color: #fff;
  font-weight: 800; font-size: 0.95rem; box-shadow: var(--shadow-sm);
}
.icon-btn:active, .pill-btn:active { transform: scale(0.94); }

/* ---------- status row ---------- */
.status-row { width: 100%; display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.progress-pill {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--chip-bg); color: var(--chip-ink);
  padding: 7px 14px; border-radius: 20px; font-weight: 800; font-size: 0.95rem;
}
.level-title { margin: 0; font-size: 1.15rem; font-weight: 800; color: var(--ink); }

.rules { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.rule-chip {
  background: #fff; color: var(--ink-soft); box-shadow: var(--shadow-sm);
  padding: 6px 12px; border-radius: 14px; font-size: 0.8rem; font-weight: 700;
}

/* ---------- board ---------- */
.board-wrap { width: 100%; display: flex; justify-content: center; }
.board {
  --n: 6;
  position: relative;
  width: min(92vw, 460px);
  aspect-ratio: 1;
  background: var(--card);
  border-radius: 16px;
  box-shadow: var(--shadow);
  container-type: size;
  touch-action: none;            /* drag-to-draw must not scroll the page */
  background-image:
    linear-gradient(var(--grid-line) 1.5px, transparent 1.5px),
    linear-gradient(90deg, var(--grid-line) 1.5px, transparent 1.5px);
  background-size: calc(100% / var(--n)) calc(100% / var(--n));
  background-position: -0.75px -0.75px;
  border: 2px solid #efe6d6;
  overflow: hidden;
}

.cs-regions, .cs-clues, .cs-sel { position: absolute; inset: 0; pointer-events: none; }

/* drawn rectangles */
.cs-region {
  position: absolute;
  border-radius: 10px;
  border: 2.5px solid rgba(0, 0, 0, 0.12);
  display: flex; align-items: center; justify-content: center;
  transition: background-color 0.12s ease, border-color 0.12s ease;
  outline: 1.5px solid #fff;       /* crisp separation between adjacent rects */
  outline-offset: -2.5px;
}
.cs-region.is-correct {
  background: var(--col);
  border-color: rgba(255, 255, 255, 0.85);
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.35), 0 3px 10px rgba(0,0,0,0.12);
}
/* a drawn rectangle with no number yet — a solid, intentional shape (not a glitch) */
.cs-region.is-empty {
  background: #efe9dd;
  border-color: #cbbfa9;
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.55);
}
/* wrong area or more than one number — same calm box, a soft-red border flags the
   mistake (no red FILL, so it never clashes with a vibrant 'correct' rectangle) */
.cs-region.is-bad {
  background: #efe9dd;
  border-color: #eb9b9b;
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.5);
}
.cs-tick { color: #fff; font-weight: 900; font-size: 8cqmin; text-shadow: 0 1px 3px rgba(0,0,0,0.25); opacity: 0.9; }

@keyframes cs-pop { 0% { transform: scale(0.78); } 55% { transform: scale(1.07); } 100% { transform: scale(1); } }
.cs-region.pop { animation: cs-pop 0.34s cubic-bezier(0.34, 1.56, 0.64, 1); }

/* clue numbers — always above regions, readable over any colour */
.cs-clue {
  position: absolute;
  transform: translate(-50%, -50%);
  /* Sized relative to a CELL (= 100cqmin / --n), not the whole board, so the
     bubbles stay proportional on every grid size — a 10×10 cell is half a 5×5
     cell, so its bubble is half the size too. (55%/35% of a cell matches how the
     5×5 boards already looked.) */
  min-width: calc(55cqmin / var(--n)); height: calc(55cqmin / var(--n)); padding: 0 calc(7.5cqmin / var(--n));
  border-radius: 999px;
  background: var(--clue-bg);
  color: #2f2519;
  font-weight: 900; font-size: calc(35cqmin / var(--n)); line-height: 1;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 2px 6px rgba(0,0,0,0.18);
}

/* drag selection preview — shows the area you're sweeping out */
.cs-sel {
  position: absolute;
  background: rgba(255, 169, 77, 0.32);
  border: 2.5px solid #ff922b;
  border-radius: 9px;
  display: flex;
  align-items: center;
  justify-content: center;
}
/* `display:flex` above would otherwise override the `hidden` attribute's
   display:none, leaving the orange selection box stuck over the whole board. */
.cs-sel[hidden] { display: none; }
.cs-sel-n { color: #8a4310; font-weight: 900; font-size: 8cqmin; opacity: 0.9; }

/* ---------- controls ---------- */
.controls { display: flex; gap: 10px; }
.ctrl-btn {
  display: inline-flex; flex-direction: column; align-items: center; gap: 3px;
  min-width: 72px; padding: 9px 12px; border-radius: 14px; border: none; cursor: pointer;
  background: #fff; color: var(--accent); box-shadow: var(--shadow-sm);
  font-weight: 800; font-size: 0.78rem;
}
.ctrl-btn svg { width: 22px; height: 22px; }
.ctrl-btn:active { transform: scale(0.95); }
.ctrl-btn:disabled { opacity: 0.45; }

.subbar { display: flex; align-items: baseline; gap: 14px; }
.timer { font-size: 1.05rem; font-weight: 800; color: var(--ink-soft); font-variant-numeric: tabular-nums; }
.best-time { font-size: 0.85rem; font-weight: 700; color: var(--chip-ink); }
.controls-hint { margin: 2px 0 0; font-size: 0.82rem; color: var(--ink-soft); text-align: center; }

/* ---------- modals ---------- */
.modal-overlay {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(40, 30, 20, 0.45);
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.modal-overlay[hidden] { display: none; }
.modal {
  background: #fff; border-radius: 22px; padding: 26px 24px; max-width: 360px; width: 100%;
  text-align: center; box-shadow: var(--shadow); animation: cs-rise 0.3s ease;
}
@keyframes cs-rise { from { transform: translateY(16px) scale(0.96); opacity: 0; } to { transform: none; opacity: 1; } }
.modal-emoji { font-size: 2.6rem; }
.modal h2 { margin: 6px 0 4px; font-size: 1.5rem; color: var(--accent); }
.modal p { margin: 0 0 14px; color: var(--ink-soft); }
.modal-stats { display: flex; justify-content: center; gap: 22px; margin-bottom: 18px; }
.modal-stats .stat { display: flex; flex-direction: column; }
.modal-stats .stat b { font-size: 1.5rem; color: var(--ink); font-variant-numeric: tabular-nums; }
.modal-stats .stat span { font-size: 0.78rem; color: var(--ink-soft); }
.modal-row { display: flex; gap: 10px; justify-content: center; }
.primary-btn {
  flex: 1; padding: 13px 18px; border-radius: 14px; border: none; cursor: pointer;
  background: linear-gradient(135deg, var(--accent-2), #ff9f43); color: #fff; font-weight: 800; font-size: 1rem;
}
.ghost-btn {
  padding: 13px 18px; border-radius: 14px; border: none; cursor: pointer;
  background: #f3ece0; color: var(--accent); font-weight: 800; font-size: 1rem;
}
.primary-btn:active, .ghost-btn:active { transform: scale(0.97); }
.modal-help { text-align: left; }
.modal-help h2 { text-align: center; }
.help-list { list-style: none; padding: 0; margin: 0 0 18px; display: flex; flex-direction: column; gap: 11px; }
.help-list li { color: var(--ink); font-size: 0.95rem; line-height: 1.45; padding-left: 4px; }

/* ---------- level select ---------- */
.levelselect-panel {
  background: #fff; border-radius: 22px; padding: 20px; max-width: 460px; width: 100%;
  max-height: 84vh; display: flex; flex-direction: column; box-shadow: var(--shadow);
}
.levelselect-head { display: flex; align-items: center; justify-content: space-between; }
.levelselect-head h2 { margin: 0; color: var(--accent); }
.levelselect-sub { margin: 4px 0 14px; font-size: 0.85rem; color: var(--ink-soft); }
.level-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 9px; overflow-y: auto; padding: 2px; }
.level-tile {
  position: relative; aspect-ratio: 1; border: none; cursor: pointer;
  border-radius: 14px; background: #f6f0e6;
  border-bottom: 4px solid var(--band, #ccc);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
}
.level-tile .lt-num { font-weight: 900; font-size: 1.05rem; color: var(--ink); }
.level-tile .lt-sub { font-size: 0.66rem; color: var(--ink-soft); font-weight: 700; }
.level-tile.completed { background: #fff6e9; }
.level-tile.current { outline: 3px solid var(--accent-2); outline-offset: 1px; }
.level-tile .lt-badge { position: absolute; top: 4px; right: 6px; font-size: 0.7rem; color: #ffb400; }

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

@media (max-width: 380px) {
  /* The game name plus four actions cannot share one 320px row. Keep every
     control on-screen and retain proper touch targets by giving actions a row. */
  .app-header {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    justify-items: center;
    gap: 10px;
  }
  .brand { justify-self: center; }
  .header-actions {
    width: 100%;
    justify-content: center;
  }
}
