/* ── Design tokens ─────────────────────────────────────────────────────────── */
:root {
  /* ── Board surfaces ────────────────────────────────────── */
  --color-bg: #2F3A34;
  /* deep muted green-gray */
  --color-board: #F4F1E8;
  /* warm parchment — undetermined cells */
  --color-path: #E8D9AC;
  /* warm earth tint — path cells */
  --color-blocked: #4C625A;
  /* slightly desaturated dark green — enclosed cells */
  --color-blocked-border: #41564F;
  /* slightly deeper — blocked cell edge */
  --color-region: #C9C2B4;
  /* neutral — three-sided cells */

  /* ── Structural lines ──────────────────────────────────── */
  --color-hedge: #2E6F57;
  /* rich green — placed hedges */
  --color-grid: #C9C2B4;
  /* light neutral — grid guides */
  --color-dot: #7E978D;
  /* mid-tone — plain vertex dots */

  /* ── Error ──────────────────────────────────────────── */
  --color-error: #D1493F;
  /* warm red — error vertices */
  --color-error-dark: #A7342D;
  /* deep red — error vertex ring */
  --color-error-region: #D97C7440;
  /* translucent — error region fill */
  --color-error-region-border: #C85C5488;
  /* semi-transparent — error perimeter */

  /* ── Clue circles ────────────────────────────────────── */
  --color-clue-fill: #F4F1E8;
  --color-clue-border: #2E6F57;
  --color-clue-text: #2B3A34;
  --color-clue-on-dark-fill: #F1EEE4;
  --color-clue-on-dark-border: #274F41;
  --color-clue-on-dark-text: #22302B;

  /* ── Solved overlay ───────────────────────────────────── */
  --color-overlay-bg: #F2EDE2F2;
  --color-overlay-border: #D8D0C0;
  --color-help-subheading: #2F6A55;
  /* calm green — modal section headings */

  /* ── General text ─────────────────────────────────────── */
  --color-text-ink: #2B3A34;
  --color-text-light: #F4F1E8;
  --color-text-muted: #D6CFC2;
  --color-accent-exit: #7A9BB5;

  /* ── Buttons / controls ────────────────────────────────── */
  --color-btn-bg: #F4F1E8;
  --color-btn-hover: #EEE7D8;
  --color-btn-hover-border: #255A47;
  --color-btn-hover-text: #255A47;
  --color-btn-border: #2E6F57;
  --color-btn-text: #2E6F57;
  --color-btn-disabled-bg: #E8E6E0;
  --color-btn-disabled-border: #CFC8BA;
  --color-btn-disabled-text: #9A9A90;
}

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  overscroll-behavior: none;
  /* prevent pull-to-refresh */
}

body {
  font-family: system-ui, sans-serif;
  background: var(--color-bg);
  color: var(--color-text-ink);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  touch-action: manipulation;
  /* prevent double-tap zoom */
  overscroll-behavior: none;
  /* respect notch / home-indicator on iOS */
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

/* Prevent accidental text selection during taps and long presses */
header,
#toolbar,
#statusMsg {
  user-select: none;
  -webkit-user-select: none;
}

header {
  width: 100%;
  background: #00000040;
  color: #fff;
  padding: 12px 24px;
  text-align: center;
}

header h1 {
  font-family: 'Cinzel', serif;
  font-weight: 700;
  font-size: 2rem;
  letter-spacing: 0.06em;
  color: var(--color-text-muted);
  margin-bottom: 12px;
}

main {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 12px 16px 16px;
  width: 100%;
}

/* Toolbar */
#toolbar {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: center;
  background: transparent;
  width: 100%;
  padding: 10px 16px;
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 6px;
}

body.is-phone .toolbar-group--secondary {
  flex-basis: 100%;
  justify-content: center;
}

#toolbarCenter:not([hidden]) {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 14px;
  width: 100%;
  padding: 2px 0 6px;
}

/* Post-win plan/labyrinth view toggle — lives beside the Solved! banner */
.view-toggle {
  padding: 4px 12px;
  font-size: 0.8rem;
  border-radius: 14px;
}

.toolbar-divider {
  width: 1px;
  height: 22px;
  background: var(--color-btn-border);
  opacity: 0.35;
  flex-shrink: 0;
}

/* Labels inside toolbar */
#toolbar label {
  color: var(--color-text-muted);
  font-size: 0.9rem;
}

button {
  padding: 6px 14px;
  font-size: 0.9rem;
  cursor: pointer;
  border: 1px solid var(--color-btn-border);
  border-radius: 6px;
  background: var(--color-btn-bg);
  color: var(--color-btn-text);
  font-family: inherit;
}

button:hover:not(:disabled) {
  background: var(--color-btn-hover);
  border-color: var(--color-btn-hover-border);
  color: var(--color-btn-hover-text);
}

button:disabled {
  background: var(--color-btn-disabled-bg);
  border-color: var(--color-btn-disabled-border);
  color: var(--color-btn-disabled-text);
  cursor: default;
}

select {
  padding: 5px 8px;
  font-size: 0.9rem;
  border: 1px solid var(--color-btn-border);
  border-radius: 6px;
  background: var(--color-btn-bg);
  color: var(--color-btn-text);
  font-family: inherit;
  cursor: pointer;
}

select:hover {
  background: var(--color-btn-hover);
}

/* Canvas */
#canvasWrapper {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 6px;
  width: 100%;
  min-height: 240px;
}

canvas {
  display: block;
  cursor: crosshair;
  touch-action: none;
  /* prevent scroll/zoom when tapping the board */
  /* Lift the board off the dark background */
  box-shadow: 0 4px 24px #00000073, 0 1px 6px #0000004D;
}

#canvasWrapper.paused canvas {
  visibility: hidden;
}

#pauseOverlay[hidden] {
  display: none;
}

#pauseOverlay {
  position: absolute;
  left: 50%;
  top: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  transform: translate(-50%, -50%);
  background-color: #2C6048;
  color: var(--color-text-light);
  border-radius: 10px;
  overflow: hidden;
}

#pauseOverlay::before {
  content: '';
  position: absolute;
  inset: var(--pause-margin, 42px);
  background-color: #3A7060;
  background-image:
    linear-gradient(rgba(244, 241, 232, 0.12) 1px, transparent 1px),
    linear-gradient(90deg, rgba(244, 241, 232, 0.12) 1px, transparent 1px);
  background-size: var(--pause-cell, 60px) var(--pause-cell, 60px);
  background-position: 0 0;
}

.pause-panel {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 18px 22px;
  min-width: min(280px, 100%);
  border: 1px solid rgba(244, 241, 232, 0.3);
  border-radius: 14px;
  background: rgba(36, 49, 44, 0.8);
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.28);
  backdrop-filter: blur(2px);
}

#pauseMessage {
  font-family: 'Cinzel', serif;
  font-size: 1.2rem;
  letter-spacing: 0.04em;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
}

/* Timer display — lives inside the toolbar between the two button groups */
.timer-display {
  font-family: 'Cinzel', serif;
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--color-text-muted);
  letter-spacing: 0.05em;
  white-space: nowrap;
  min-width: 4.5ch;
  text-align: center;
  padding: 0 4px;
}

/* On a win the timer slot becomes the celebration banner */
.timer-display--solved {
  color: var(--color-path);
  font-size: 1.35rem;
  animation: solved-pop 500ms cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes solved-pop {
  from {
    transform: scale(0.6);
    opacity: 0;
  }

  to {
    transform: scale(1);
    opacity: 1;
  }
}

/* Status pill — shown below the board */
#statusMsg {
  font-size: 0.9rem;
  font-weight: bold;
  text-align: center;
  padding: 8px 16px;
  border-radius: 20px;
  min-height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ── Fixed icon buttons (top-right corner) ──────────────────────────────────── */

.icon-btn {
  position: fixed;
  top: calc(env(safe-area-inset-top, 0px) + 20px);
  z-index: 900;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--color-hedge);
  color: #fff;
  font-family: 'Cinzel', serif;
  font-size: 18px;
  font-weight: bold;
  border: none;
  padding: 0;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.icon-btn:hover:not(:disabled) {
  background: #3A8468;
}

.help-btn {
  right: calc(env(safe-area-inset-right, 0px) + 20px);
}

.prefs-btn {
  right: calc(env(safe-area-inset-right, 0px) + 64px);
  font-size: 20px;
}

.print-btn {
  right: calc(env(safe-area-inset-right, 0px) + 108px);
}

.print-btn svg {
  display: block;
}

/* ── Modal animations ───────────────────────────────────────────────────────── */

@keyframes backdrop-in {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

@keyframes backdrop-out {
  from {
    opacity: 1;
  }

  to {
    opacity: 0;
  }
}

@keyframes modal-in {
  from {
    opacity: 0;
    transform: scale(0.95);
  }

  to {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes modal-out {
  from {
    opacity: 1;
    transform: scale(1);
  }

  to {
    opacity: 0;
    transform: scale(0.95);
  }
}

/* ── Shared modal backdrop ───────────────────────────────────────────────────── */

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 1000;
  align-items: center;
  justify-content: center;
  background: #2F3A34E0;
  animation: backdrop-in 200ms ease-out forwards;
}

/* display:flex must live here so the browser's [hidden]{display:none} is not
   overridden by a same-specificity class rule. */
.modal-backdrop:not([hidden]) {
  display: flex;
}

.modal-backdrop.closing {
  animation: backdrop-out 150ms ease-in forwards;
}

/* ── Shared modal box ────────────────────────────────────────────────────────── */

.modal-box {
  position: relative;
  max-width: 560px;
  width: calc(100% - 48px);
  max-height: 80vh;
  overflow-y: auto;
  background: var(--color-board);
  border: 3px solid var(--color-hedge);
  border-radius: 12px;
  padding: 32px;
  box-shadow: 0 8px 32px #00000066;
  animation: modal-in 200ms ease-out forwards;
}

.modal-backdrop.closing .modal-box {
  animation: modal-out 150ms ease-in forwards;
}

/* ── Shared modal close button ───────────────────────────────────────────────── */

.modal-close {
  position: absolute;
  top: 16px;
  right: 16px;
  background: none;
  border: none;
  color: var(--color-hedge);
  font-size: 28px;
  line-height: 1;
  padding: 0 4px;
  cursor: pointer;
  border-radius: 4px;
}

.modal-close:hover:not(:disabled) {
  background: none;
  color: var(--color-error);
}

/* ── Shared modal title ──────────────────────────────────────────────────────── */

.modal-title {
  font-family: 'Cinzel', serif;
  font-size: 24px;
  font-weight: 700;
  color: var(--color-hedge);
  text-align: center;
  margin-bottom: 20px;
}

/* ── New-game confirmation modal ─────────────────────────────────────────────── */

.confirm-modal {
  max-width: 400px;
}

.confirm-message {
  font-size: 15px;
  line-height: 1.5;
  color: var(--color-text-ink);
  margin-bottom: 20px;
}

.confirm-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.btn-primary {
  background: var(--color-hedge);
  border-color: var(--color-hedge);
  color: #fff;
}

.btn-primary:hover:not(:disabled) {
  background: #3A8468;
  border-color: #3A8468;
  color: #fff;
}

/* ── Preferences modal ───────────────────────────────────────────────────────── */

.prefs-modal {
  max-width: 420px;
}

.prefs-body {
  font-size: 15px;
  color: var(--color-text-ink);
}

.prefs-section-heading {
  font-weight: 700;
  color: var(--color-help-subheading);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  margin: 18px 0 4px;
}

.prefs-section-heading:first-child {
  margin-top: 0;
}

.pref-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 0;
  border-bottom: 1px solid rgba(46, 111, 87, 0.12);
}

.pref-row:last-child {
  border-bottom: none;
}

.pref-label-group {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.pref-label {
  font-size: 15px;
  font-weight: 500;
  color: var(--color-text-ink);
}

.pref-desc {
  font-size: 12px;
  color: #6A7870;
}

/* Toggle switch */

.toggle-switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 26px;
  flex-shrink: 0;
  cursor: pointer;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
  position: absolute;
}

.toggle-track {
  position: absolute;
  inset: 0;
  background: #C0BAB0;
  border-radius: 13px;
  transition: background 0.2s;
}

.toggle-track::before {
  content: '';
  position: absolute;
  width: 20px;
  height: 20px;
  background: #fff;
  border-radius: 50%;
  top: 3px;
  left: 3px;
  transition: transform 0.2s;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
}

.toggle-switch input:checked+.toggle-track {
  background: var(--color-hedge);
}

.toggle-switch input:checked+.toggle-track::before {
  transform: translateX(18px);
}

/* ── Help modal content ──────────────────────────────────────────────────────── */

.help-body {
  font-size: 15px;
  line-height: 1.6;
  color: var(--color-text-ink);
}

.help-body p {
  margin-bottom: 8px;
}

.help-label {
  font-weight: bold;
  color: var(--color-hedge);
}

/* Inline line-style swatches in the "Placing hedges" list */
.help-hedge {
  font-weight: 600;
  white-space: nowrap;
}

.help-hedge::before {
  content: '';
  display: inline-block;
  width: 28px;
  vertical-align: middle;
  margin-right: 6px;
}

.help-hedge.undecided::before {
  border-top: 1.5px solid var(--color-grid);
}

.help-hedge.hedge::before {
  border-top: 6px solid var(--color-hedge);
  border-radius: 3px;
}

.help-hedge.removed::before {
  border-top: 1.5px dashed #C0BAB0;
}

.help-subheading {
  font-weight: bold;
  color: var(--color-help-subheading);
  margin-top: 14px;
  margin-bottom: 4px;
}

.help-body ul {
  margin: 0 0 8px 0;
  padding-left: 20px;
}

.help-body ul li {
  margin-bottom: 4px;
  padding-left: 4px;
}

.help-body ul li::marker {
  color: var(--color-hedge);
}

/* Solved example board inside the help modal */
.help-demo {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  margin: 10px 0 4px;
}

.help-demo canvas {
  cursor: default;
  box-shadow: none;
  border-radius: 6px;
  max-width: 100%;
  height: auto;
}

.help-demo-caption {
  font-size: 0.85rem;
  font-style: italic;
  color: #4A5A52;
  text-align: center;
  max-width: 340px;
}

.help-subtle {
  font-size: 0.9em;
  /* smaller than other section headers */
  color: #4A6B5D;
  /* more muted green */
  font-weight: 500;
  /* lighter than other headers */
  margin-top: 16px;
  margin-bottom: 4px;
}

.help-subtle-text {
  font-style: italic;
  color: #4A5A52;
}

/* ── Phone layout ────────────────────────────────────────────────────────────── */
/* Padding is reduced so the board fills as much of the screen as possible.      */

body.is-phone header {
  padding: 8px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

body.is-phone header h1 {
  font-size: 1.3rem;
  margin-bottom: 0;
  flex: 1;
  text-align: center;
}

/* On phones the icon buttons move into the header flow — undo fixed positioning */
body.is-phone .icon-btn {
  position: relative;
  top: auto;
  right: auto;
  width: 36px;
  height: 36px;
  flex-shrink: 0;
}

body.is-phone main {
  flex: 1;
  padding: 4px;
}

body.is-phone #canvasWrapper {
  flex: 1;
}

body.is-phone #toolbar {
  padding: 6px 4px;
  gap: 8px;
}

/* Larger touch targets on phones (Apple HIG: 44px minimum) */
body.is-phone button,
body.is-phone select {
  min-height: 44px;
  padding: 8px 14px;
}