/*
 * Night-camp theme, following the iOS app: deep navy field, warm canvas-orange
 * accent, generous rounding, everything sized for thumbs.
 */

:root {
  --bg:          #0f1730;
  --bg-raised:   #16203f;
  --bg-cell:     #1b2749;
  --bg-cell-alt: #1e2c52;
  --grass:       #223a2e;
  --line:        #2b3a66;
  --accent:      #f5a06a;
  --accent-dim:  #b8734a;
  --text:        #e8edfb;
  --text-dim:    #8f9dc4;
  --text-done:   #4d5a80;
  --error:       #ef6b6b;
  --tree:        #6fbf87;
  --radius:      16px;

  /* Set from JS so the grid and its clue gutters stay square on any screen. */
  --cell: 34px;
  --cols: 8;
  --rows: 8;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  overscroll-behavior: none;
  /*
   * The app is sized to fill the screen exactly (see fitBoard). Locking scroll
   * here is not cosmetic: on an iOS home-screen PWA any body scroll offsets the
   * hit-testing of modal <dialog>s (drawn centered, tapped as if still at
   * scroll 0), so a single stray pixel of overflow makes every modal button
   * miss. Modals live in the top layer and are not clipped by this.
   */
  overflow: hidden;
}

body {
  background:
    radial-gradient(1200px 700px at 78% -8%, #1d2a52 0%, transparent 60%),
    var(--bg);
  color: var(--text);
  font: 500 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui,
        sans-serif;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
           env(safe-area-inset-bottom) env(safe-area-inset-left);
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

/* ---- top bar ---- */

#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px 10px;
  flex: none;
}

.brand { display: flex; align-items: center; gap: 10px; }
.logo { width: 30px; height: 30px; color: var(--accent); flex: none; }

h1 {
  margin: 0;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: #c9d3ef;
}

.actions { display: flex; gap: 10px; }

button.icon {
  width: 42px; height: 42px;
  border: none;
  border-radius: 50%;
  background: #2c3760;
  color: var(--accent);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: transform .12s ease, background .15s ease, opacity .15s ease;
}
button.icon svg { width: 21px; height: 21px; }
button.icon:active { transform: scale(.92); background: #364274; }
button.icon:disabled { opacity: .35; }

/* ---- board ---- */

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 0;
  padding: 0 12px 18px;
}

#status {
  height: 22px;
  margin-bottom: 10px;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-dim);
  text-align: center;
}
#status.error { color: var(--error); }
#status.good  { color: var(--tree); }

#boardwrap { position: relative; }

/*
 * One grid holding the cells plus a clue gutter on the right and bottom, the
 * same arrangement the original game uses.
 */
#board {
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell)) calc(var(--cell) * 0.9);
  grid-template-rows:    repeat(var(--rows), var(--cell)) calc(var(--cell) * 0.9);
  gap: 2px;
}

.cell {
  width: var(--cell);
  height: var(--cell);
  border-radius: 6px;
  background: var(--bg-cell);
  display: grid;
  place-items: center;
  position: relative;
  transition: background .12s ease;
}
.cell.odd { background: var(--bg-cell-alt); }

.cell svg { width: 78%; height: 78%; display: block; pointer-events: none; }

.cell.tree  { background: #1a2b3d; }
.cell.tree  svg { color: var(--tree); }
.cell.tent  svg { color: var(--accent); }

.cell.grass {
  background: var(--grass);
}
.cell.grass::after {
  content: "";
  width: 7px; height: 7px;
  border-radius: 50%;
  background: #4a7a5c;
}

/* A rule is broken right now (adjacent tents, too many in a line). */
.cell.err { background: #4a2233 !important; box-shadow: inset 0 0 0 2px var(--error); }

/* Legal so far, but disagrees with the solution -- only a hint says this, and
 * it is deliberately a different colour from a live rule violation. */
.cell.mistake {
  box-shadow: inset 0 0 0 2px #e8a33d;
  animation: pulse-warn 1.1s ease-in-out infinite;
}
@keyframes pulse-warn {
  0%, 100% { box-shadow: inset 0 0 0 2px #e8a33d; }
  50%      { box-shadow: inset 0 0 0 3px #ffc46b; }
}

/* The square a hint is pointing at. Tinted as well as ringed -- a ring alone is
 * easy to miss against a dark cell. */
.cell.marked {
  background: #6b4322 !important;
  animation: flash .9s ease-in-out infinite;
}
@keyframes flash {
  0%, 100% { box-shadow: inset 0 0 0 2px var(--accent); }
  50%      { box-shadow: inset 0 0 0 4px var(--accent); }
}

.clue {
  display: grid;
  place-items: center;
  font-size: calc(var(--cell) * 0.46);
  font-weight: 700;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
  transition: color .15s ease;
}
.clue.done { color: var(--text-done); }
.clue.over { color: var(--error); }
.clue.corner { visibility: hidden; }

/* ---- dialogs ---- */

#scrim {
  position: fixed; inset: 0;
  background: rgba(6, 10, 24, .72);
  backdrop-filter: blur(3px);
  z-index: 10;
}
#scrim.hidden { display: none; }

dialog {
  border: none;
  padding: 26px 24px 22px;
  border-radius: 26px;
  background: linear-gradient(180deg, #16214a 0%, #111a38 100%);
  color: var(--text);
  width: min(92vw, 420px);
  box-shadow: 0 24px 60px rgba(0, 0, 0, .55);
  z-index: 20;
}
dialog::backdrop { background: rgba(6, 10, 24, .72); }

.sparkle {
  text-align: center;
  font-size: 30px;
  color: var(--accent);
  line-height: 1;
}

dialog h2 {
  margin: 10px 0 22px;
  text-align: center;
  font-size: 27px;
  font-weight: 700;
  letter-spacing: -0.02em;
}
/* The won dialog autofocuses its heading so neither the readonly code input nor
 * a button steals focus (and shows a ring) when the dialog opens. */
dialog h2:focus { outline: none; }

.group-label {
  display: block;
  font-size: 15px;
  font-weight: 600;
  color: var(--text-dim);
  margin: 0 0 8px 2px;
}

.segmented {
  display: flex;
  gap: 4px;
  background: #1d2a52;
  border-radius: 14px;
  padding: 4px;
  margin-bottom: 20px;
}

.segmented button {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 15px;
  font-weight: 600;
  padding: 11px 4px;
  border-radius: 11px;
  cursor: pointer;
  transition: background .15s ease, color .15s ease;
  white-space: nowrap;
}
.segmented button[aria-pressed="true"] {
  background: var(--accent);
  color: #2a1608;
}
/* Keep the ring for keyboard users, drop it for taps. */
.segmented button:focus { outline: none; }
.segmented button:focus-visible { outline: 2px solid #fff; outline-offset: 1px; }

.hint-text {
  margin: -8px 2px 18px;
  font-size: 13.5px;
  line-height: 1.45;
  color: var(--text-dim);
  min-height: 38px;
}

/* ---- toggle switch ---- */

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: -6px 2px 20px;
}
.toggle-row span {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
}

.switch {
  flex: none;
  width: 52px;
  height: 32px;
  border: none;
  border-radius: 16px;
  background: #2a3560;
  position: relative;
  cursor: pointer;
  transition: background .18s ease;
  padding: 0;
}
.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: #e8edfb;
  transition: transform .18s ease;
}
.switch[aria-checked="true"] { background: var(--accent); }
.switch[aria-checked="true"]::after { transform: translateX(20px); }
.switch:focus { outline: none; }
.switch:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }

/* ---- puzzle identity caption ---- */

#puzzle-info {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  flex-wrap: wrap;
  margin: 12px auto 0;
  padding: 6px 10px;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: var(--text-dim);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  transition: background .15s ease;
}
#puzzle-info:active { background: #1d2a52; }
#info-label { font-weight: 600; }
#info-code {
  color: var(--accent);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  letter-spacing: .02em;
}

.code-note {
  margin: 8px 2px 0;
  font-size: 12.5px;
  line-height: 1.45;
  color: var(--text-dim);
}

/* ---- in-game toolbar ---- */

#toolbar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin: 2px 0 10px;
  flex: none;
}
#toolbar .icon { width: 38px; height: 38px; }
#toolbar .icon svg { width: 19px; height: 19px; }

#move-count {
  min-width: 30px;
  text-align: center;
  font-size: 15px;
  font-weight: 700;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

.toolbar-gap { width: 14px; }

#btn-pencil.active {
  background: var(--accent);
  color: #2a1608;
}

/* ---- pencil marks ---- */

.cell.pencil svg { opacity: .34; }
.cell.grass.pencil { background: var(--bg-cell); }
.cell.grass.pencil::after {
  background: transparent;
  border: 1.5px solid #4a7a5c;
  opacity: .55;
}
.cell.odd.grass.pencil { background: var(--bg-cell-alt); }

/* ---- puzzle codes ---- */

#code-section {
  margin: 0 2px 18px;
  font-size: 14px;
  color: var(--text-dim);
}
#code-section summary {
  cursor: pointer;
  font-weight: 600;
  padding: 4px 0;
  color: var(--text);
}
.code-label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-dim);
  margin: 10px 2px 5px;
}

.code-row { display: flex; gap: 8px; align-items: stretch; margin-bottom: 20px; }
.code-row .code-box { flex: 1; min-width: 0; }

.code-box {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #121b3a;
  color: var(--accent);
  font: inherit;
  font-size: 15px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  letter-spacing: .02em;
}
.code-box::placeholder { color: var(--text-done); letter-spacing: normal; }
.code-box:focus { outline: 2px solid var(--accent); border-color: transparent; }
input[readonly].code-box { cursor: pointer; }

.dlg-buttons button.small,
button.secondary.small {
  flex: none;
  padding: 0 16px;
  font-size: 15px;
  border: none;
  border-radius: 12px;
  background: #2a3560;
  color: var(--text);
  font-weight: 600;
  cursor: pointer;
}

/* ---- statistics ---- */

.stat-tiles {
  display: flex;
  gap: 10px;
  margin-bottom: 18px;
}
.stat-tile {
  flex: 1;
  background: #1d2a52;
  border-radius: 14px;
  padding: 14px 6px 11px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.stat-tile .big {
  font-size: 28px;
  font-weight: 700;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
  line-height: 1;
}
.stat-tile .lbl {
  font-size: 12px;
  color: var(--text-dim);
}

/* Gridsize (rows) x difficulty (columns) matrix. Each cell: solves, and the
 * best unassisted time under it. */
.matrix {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 6px;
  table-layout: fixed;
  font-variant-numeric: tabular-nums;
}
.matrix thead th {
  text-align: center;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-dim);
  padding: 2px 2px 8px;
}
.matrix tbody th {
  text-align: left;
  font-size: 13px;
  font-weight: 600;
  color: #c9d3ef;
  padding: 8px 6px 8px 2px;
}
.matrix td {
  text-align: center;
  padding: 6px 2px;
  border-top: 1px solid var(--line);
  vertical-align: middle;
}
.matrix td.empty { color: var(--text-done); }
.matrix td.has .n {
  display: block;
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
  line-height: 1.1;
}
.matrix td.has .bt { display: block; font-size: 10.5px; color: var(--text-dim); }

/* ---- game history ---- */

.history-list {
  max-height: 46vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  margin-bottom: 12px;
}
.hist-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 2px;
  border-top: 1px solid var(--line);
  font-size: 14px;
}
.hist-row:first-child { border-top: none; }
.hist-icon {
  flex: none;
  width: 20px;
  text-align: center;
  font-size: 15px;
  color: var(--tree);
}
.hist-row.skip .hist-icon { color: var(--text-done); }
.hist-main { flex: 1; min-width: 0; font-weight: 600; color: #c9d3ef; }
.hist-row.skip .hist-main { color: var(--text-dim); font-weight: 500; }
.hist-tags { display: flex; gap: 4px; flex: none; }
.hist-time {
  flex: none;
  min-width: 52px;
  text-align: right;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}
.hist-row.skip .hist-time { font-style: italic; }

.tag {
  font-size: 11px;
  font-weight: 700;
  min-width: 18px;
  text-align: center;
  padding: 1px 5px;
  border-radius: 7px;
  background: #22305a;
  color: var(--text-dim);
}
.tag.hint { background: #3a2c15; color: var(--accent); }
.tag.check { background: #1f3350; color: #8fb7e8; }
#history-legend { text-align: center; margin: 4px 2px 18px; }
#history-legend .tag { display: inline-block; margin: 0 2px; }

.dlg-buttons { display: flex; gap: 12px; }
.dlg-buttons button {
  flex: 1;
  border: none;
  border-radius: 15px;
  padding: 15px 10px;
  font: inherit;
  font-size: 17px;
  font-weight: 650;
  cursor: pointer;
  transition: transform .12s ease, opacity .15s ease;
}
.dlg-buttons button:active { transform: scale(.97); }
/* Ring for keyboard users; nothing on tap or the open-dialog autofocus. */
.dlg-buttons button:focus { outline: none; }
.dlg-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }
.primary   { background: var(--accent); color: #2a1608; }
.secondary { background: #2a3560; color: var(--text); }
.dlg-buttons button:disabled { opacity: .5; }

/* ---- landscape / wide ---- */
@media (min-width: 620px) and (orientation: landscape) {
  h1 { font-size: 24px; }
}
