:root {
  color-scheme: light dark;
  --ink: light-dark(#111111, #e8e8e8);
  --muted: light-dark(#666666, #9a9a9a);
  --line: light-dark(#d8d8d8, #2e2e2e);
  --panel: light-dark(#f6f6f6, #1a1a1a);
  --paper: light-dark(#ffffff, #0f0f0f);
  --fill: light-dark(#d7352a, #b8443a);
  --tube: light-dark(#111111, #e8e8e8);
  --tube-empty: light-dark(#ffffff, #0f0f0f);
  --warn: light-dark(#111111, #e8e8e8);
  --accent: light-dark(#d7352a, #b8443a);
  --clue: light-dark(#333333, #cfcfcf);
  --clue-met: light-dark(#c4c4c4, #4a4a4a);
  --button-hover: light-dark(#263229, #cfcfcf);
  --cell-hover: light-dark(#dcdcdc, #242424);
  --cell-filled-border: light-dark(#c9c9c9, #3a3a3a);
  --x-mark: light-dark(#2b6fb0, #6fb8e0);
  --cell: 48px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

:root[data-theme="light"] {
  color-scheme: light;
}

:root[data-theme="dark"] {
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--paper);
  color: var(--ink);
}

button,
input,
select {
  font: inherit;
}

button {
  border: 1px solid var(--ink);
  background: var(--ink);
  color: var(--paper);
  border-radius: 6px;
  padding: 0.7rem 0.9rem;
  cursor: pointer;
}

button:hover {
  background: var(--button-hover);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

button:disabled:hover {
  background: var(--ink);
}

.ghost-button {
  background: var(--paper);
  color: var(--ink);
}

.ghost-button:hover {
  background: var(--panel);
}

.page-footer {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 12px;
  padding: 24px 0 16px;
  flex-wrap: wrap;
}

.settings-toggle {
  height: 36px;
  padding: 0 14px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: transparent;
  color: var(--muted);
  border-color: var(--line);
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
}

.settings-toggle:hover {
  color: var(--ink);
  background: var(--panel);
}

.settings-toggle svg {
  width: 18px;
  height: 18px;
}

.settings-dialog {
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--paper);
  color: var(--ink);
  padding: 24px;
  max-width: min(440px, calc(100vw - 32px));
  width: 100%;
  box-sizing: border-box;
}

.settings-dialog::backdrop {
  background: rgba(0, 0, 0, 0.4);
}

.settings-dialog h2 {
  margin: 0 0 4px;
  font-size: 1.15rem;
}

.settings-intro {
  margin: 0 0 18px;
  color: var(--muted);
  font-size: 0.9rem;
}

.settings-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.setting-row {
  display: grid;
  grid-template-columns: auto 1fr;
  column-gap: 12px;
  row-gap: 4px;
  align-items: start;
  cursor: pointer;
  padding: 8px;
  border-radius: 8px;
  text-transform: none;
  font-size: 0.95rem;
  font-weight: 400;
  color: var(--ink);
}

.setting-row:hover {
  background: var(--panel);
}

.setting-row input[type="checkbox"] {
  grid-row: 1 / span 2;
  margin: 2px 0 0;
  width: 18px;
  height: 18px;
  cursor: pointer;
  accent-color: var(--fill);
}

.setting-label {
  font-weight: 600;
}

.setting-desc {
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.4;
}

.settings-done {
  align-self: flex-end;
  margin-top: 4px;
}

.theme-toggle {
  height: 36px;
  padding: 0 14px 0 12px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: transparent;
  color: var(--muted);
  border-color: var(--line);
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
}

.theme-toggle:hover {
  color: var(--ink);
  background: var(--panel);
}

.theme-toggle svg {
  width: 18px;
  height: 18px;
  display: none;
}

.theme-toggle::after {
  content: "Auto";
}

:root:not([data-theme]) .theme-toggle [data-icon="auto"],
:root[data-theme="light"] .theme-toggle [data-icon="light"],
:root[data-theme="dark"] .theme-toggle [data-icon="dark"] {
  display: block;
}

:root[data-theme="light"] .theme-toggle::after {
  content: "Light";
}

:root[data-theme="dark"] .theme-toggle::after {
  content: "Dark";
}

.shell {
  width: min(1180px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 28px 0 40px;
}

.app-nav {
  display: flex;
  justify-content: center;
  align-items: start;
  gap: 12px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}

.app-nav .brand-title {
  margin-right: auto;
}

.create-mode-btn {
  white-space: nowrap;
}

.app-nav div {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: end;
}

.brand-title {
  font-size: clamp(2.1rem, 5vw, 4.6rem);
  line-height: 0.95;
  letter-spacing: 0;
  font-weight: 700;
  color: inherit;
  text-decoration: none;
}

.brand-title:hover {
  text-decoration: underline;
}

.topbar {
  display: grid;
  gap: 16px;
  justify-items: center;
  border-bottom: 1px solid var(--line);
  border-top: 1px solid var(--line);
  padding-top: 22px;
  padding-bottom: 22px;
  margin-bottom: 18px;
}

.creator-intro {
  text-align: center;
}

.creator-message {
  color: var(--muted);
  text-align: center;
}

.creator-message:empty {
  display: none;
}

.creator-message.error {
  color: var(--fill);
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: clamp(2.1rem, 5vw, 4.6rem);
  line-height: 0.95;
  letter-spacing: 0;
}

.topbar p,
.hint,
.rules-panel p {
  color: var(--muted);
  line-height: 1.55;
}

.creator,
.link-panel {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr auto;
  gap: 12px;
  align-items: end;
}

.creator {
  width: min(100%, 820px);
}

.creator-embed {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: end;
  justify-content: center;
  width: min(100%, 460px);
}

.creator-embed label {
  flex: 1 1 220px;
  min-width: 0;
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 700;
  text-transform: uppercase;
}

input,
select {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--paper);
  color: var(--ink);
  padding: 0.55rem 0.7rem;
}

.link-panel {
  grid-template-columns: 1fr auto auto;
  margin: 18px 0 0;
  padding: 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.game {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 16px;
  margin-top: 16px;
  align-items: start;
  container-type: inline-size;
}

.status {
  display: grid;
  grid-template-columns: auto auto auto;
  gap: 24px;
  align-items: center;
  padding: 12px 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  justify-self: center;
  width: max-content;
  max-width: 100%;
}

.rules-panel {
  margin: 22px auto 0;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  max-width: 700px;
  text-align: center;
}

.rules-panel p {
  margin-inline: auto;
}

.label {
  display: block;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  margin-bottom: 5px;
}

.puzzle-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

#resetPuzzle.is-confirming {
  opacity: 0.5;
  pointer-events: none;
}

.reset-confirm {
  position: absolute;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.18);
  z-index: 10;
  white-space: nowrap;
}

.reset-confirm span {
  font-size: 0.85rem;
  color: var(--muted);
}

.reset-confirm button {
  min-height: 32px;
  padding: 4px 12px;
}

.board-controls {
  position: relative;
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  justify-content: center;
  gap: 12px;
  width: 100%;
  max-width: 560px;
  margin: 4px auto 0;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.board-controls label {
  flex: 1 1 140px;
  min-width: 0;
}

.board-controls select {
  min-width: 0;
}

.board-controls button {
  flex: 0 0 auto;
  min-height: 44px;
}

.board-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  --grid-size: 8;
  /* Cap cells larger on small grids so 4x4–7x7 don't look tiny on desktop;
     stays at 48px once grid >=8. */
  --cell-max: max(48px, calc((8 - var(--grid-size, 8)) * 6px + 48px));
  /* Shrink cells when the board overflows its container. 100cqw is the
     game-panel's width (excluding any scrollbar, unlike 100vw). The "+2"
     accounts for the row-clues column on the left and the phantom right
     column that mirrors it for centering on desktop. */
  --cell: min(var(--cell-max), calc((100cqw - 16px) / (var(--grid-size, 8) + 2)));
}

.col-clues {
  display: grid;
  margin-left: var(--cell);
  margin-right: var(--cell);
  gap: 0;
  width: max-content;
}

.row-clues-phantom {
  visibility: hidden;
}

.col-clue,
.row-clue {
  display: grid;
  place-items: center;
  min-width: var(--cell);
  min-height: 42px;
  color: var(--clue);
  font-weight: 800;
}

.row-area {
  display: flex;
  gap: 0;
  width: max-content;
}

.row-clues {
  display: grid;
  gap: 0;
}

.board {
  display: grid;
  gap: 0;
  touch-action: manipulation;
}

.cell {
  width: var(--cell);
  height: var(--cell);
  border: 1px solid var(--line);
  background: var(--paper);
  border-radius: 6px;
  position: relative;
  cursor: pointer;
  padding: 0;
}

.cell:hover {
  border-color: var(--fill);
  background: var(--cell-hover);
}

.thermo-svg {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 1;
}

.tube-outer {
  stroke: var(--tube);
  stroke-width: 14;
  stroke-linecap: butt;
  stroke-linejoin: round;
}

.tube-inner {
  stroke: var(--tube-empty);
  stroke-width: 6;
  stroke-linecap: butt;
  stroke-linejoin: round;
}

.tube-end {
  stroke-linecap: round;
}

.bulb-fill {
  fill: var(--tube-empty);
  stroke: none;
}

.bulb-neck {
  stroke: var(--tube-empty);
  stroke-width: 6;
  stroke-linecap: butt;
}

.bulb-outline {
  stroke: var(--tube);
  stroke-width: 4;
  stroke-linecap: round;
}

.cell.filled .tube-inner {
  stroke: var(--fill);
}

.cell.filled .bulb-fill {
  fill: var(--fill);
}

.cell.filled .bulb-neck {
  stroke: var(--fill);
}

.cell.filled {
  border-color: var(--cell-filled-border);
}

.cell.tree-cell {
  cursor: default;
  border-color: var(--line);
  background: light-dark(#e8f5e9, #1a2e1a);
}

.cell.tree-cell:hover {
  border-color: var(--line);
  background: light-dark(#e8f5e9, #1a2e1a);
}

.cell.tent-cell {
  background: light-dark(#fff8e1, #2a2210);
  border-color: light-dark(#e0c97a, #4a3d1a);
}

.cell.tent-cell:hover {
  background: light-dark(#fff3cd, #332a14);
}

.cell.tent-error {
  border-color: light-dark(#dc3545, #ff4444) !important;
  box-shadow: inset 0 0 0 1px light-dark(#dc3545, #ff4444);
}

.cell-icon {
  position: absolute;
  inset: 8%;
  pointer-events: none;
  z-index: 1;
}

.tree-shadow {
  fill: light-dark(rgba(0, 0, 0, 0.08), rgba(0, 0, 0, 0.25));
}

.tree-trunk {
  fill: light-dark(#6D4C2A, #A07850);
  stroke: none;
}

.tree-foliage-dark {
  fill: light-dark(#2E7D32, #4CAF50);
  stroke: none;
}

.tree-foliage-mid {
  fill: light-dark(#43A047, #66BB6A);
  stroke: none;
}

.tree-foliage-light {
  fill: light-dark(#66BB6A, #81C784);
  stroke: none;
}

.tent-ground {
  stroke: light-dark(#8D6E3A, #A08050);
  stroke-width: 1.5;
  stroke-linecap: round;
}

.tent-left {
  fill: light-dark(#E8A525, #D4952A);
  stroke: none;
}

.tent-right {
  fill: light-dark(#D4941E, #C08520);
  stroke: none;
}

.tent-outline {
  fill: none;
  stroke: light-dark(#A0722A, #806020);
  stroke-width: 1;
  stroke-linejoin: round;
}

.tent-entrance {
  fill: light-dark(#C4851A, #A06E18);
  stroke: none;
  opacity: 0.7;
}

.tent-ridge {
  stroke: light-dark(#A0722A, #806020);
  stroke-width: 0.75;
  stroke-linecap: round;
}

.x-mark {
  position: absolute;
  inset: 18%;
  z-index: 2;
  pointer-events: none;
  overflow: visible;
}

.grass-icon {
  position: absolute;
  inset: 12%;
  z-index: 2;
  pointer-events: none;
  overflow: visible;
}

.grass-mound {
  fill: light-dark(rgba(0, 0, 0, 0.04), rgba(0, 0, 0, 0.2));
}

.grass-blade-foliage {
  stroke: none;
}

.grass-foliage-dark {
  fill: light-dark(#43A047, #4a8c50);
  opacity: 0.7;
}

.grass-foliage-mid {
  fill: light-dark(#66BB6A, #5a9a60);
  opacity: 0.65;
}

.grass-foliage-light {
  fill: light-dark(#81C784, #6aaa6e);
  opacity: 0.6;
}

.cell.grass-marked {
  background: light-dark(#eef7e3, #1e3220);
}

.cell.grass-marked:hover {
  background: light-dark(#e4f1d4, #253a26);
}

.lake-icon {
  position: absolute;
  inset: 6%;
  z-index: 2;
  pointer-events: none;
  overflow: visible;
}

.lake-puddle {
  fill: light-dark(#64B5F6, #1e5a96);
  stroke: light-dark(#1976D2, #0d3a66);
  stroke-width: 1.2;
  stroke-linejoin: round;
  opacity: 0.85;
}

.lake-wave {
  stroke: light-dark(#0D47A1, #b8d8f5);
  stroke-width: 1.4;
  stroke-linecap: round;
  fill: none;
  opacity: 0.65;
}

.cell.lake-marked {
  background: light-dark(#d6eaf8, #152840);
  cursor: default;
}

.cell.lake-marked:hover {
  background: light-dark(#d6eaf8, #152840);
}

.x-mark-line {
  stroke: var(--x-mark);
  stroke-width: 4;
  stroke-linecap: round;
  fill: none;
}

.x-mark-outline {
  stroke: #6a6a6a;
  stroke-width: 7;
  stroke-linecap: round;
  fill: none;
}

.x-mark-text {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: none;
  place-items: center;
  color: var(--x-mark);
  font-size: calc(var(--cell) * 0.5);
  font-weight: 900;
  line-height: 1;
  pointer-events: none;
  text-shadow: 0 1px 0 var(--paper), 1px 0 0 var(--paper), 0 -1px 0 var(--paper), -1px 0 0 var(--paper);
}

:root[data-theme="light"] .x-mark {
  display: none;
}

:root[data-theme="light"] .x-mark-text {
  display: grid;
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme]) .x-mark {
    display: none;
  }
  :root:not([data-theme]) .x-mark-text {
    display: grid;
  }
}

.met {
  color: var(--clue-met);
}

.col-clue.clickable {
  cursor: pointer;
  border-radius: 4px;
}

.col-clue.clickable:hover,
.col-clue.clickable:focus-visible {
  background: var(--clue-hover, rgba(127, 127, 127, 0.15));
  outline: none;
}

.over {
  color: var(--fill);
}

.hint {
  max-width: 620px;
  margin-inline: auto;
  text-align: center;
}

dialog {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 24px;
  color: var(--ink);
}

dialog::backdrop {
  background: rgba(23, 32, 27, 0.38);
}

#winDialog[open] {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: min(420px, calc(100vw - 32px));
}

.gift-secret {
  margin-top: 18px;
  font-size: 1.7rem;
  font-weight: 900;
  color: var(--fill);
  letter-spacing: 0.04em;
  text-align: center;
}

.gift-secret:empty {
  display: none;
}

#giftLink {
  display: inline-block;
  margin: 8px 0 0;
  color: var(--fill);
  font-size: 1.05rem;
  font-weight: 700;
  text-align: center;
  word-break: break-all;
}

#winDialog form[method="dialog"] {
  margin-top: 28px;
  display: flex;
  justify-content: flex-end;
}

/* Sunset win celebration */
.sunset-overlay {
  position: fixed;
  inset: 0;
  z-index: 1;
  pointer-events: none;
  overflow: hidden;
  transition: opacity 1.2s ease;
}

.sunset-overlay[hidden] {
  display: none;
}

.sunset-overlay.fade-out {
  opacity: 0;
}

.sunset-overlay::before {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  top: -30%;
  bottom: 0;
  background: linear-gradient(
    to top,
    rgba(255, 202, 40, 0.6) 0%,
    rgba(255, 143, 0, 0.5) 10%,
    rgba(255, 61, 0, 0.4) 22%,
    rgba(244, 67, 54, 0.32) 32%,
    rgba(233, 30, 99, 0.25) 44%,
    rgba(156, 39, 176, 0.18) 58%,
    rgba(63, 81, 181, 0.12) 72%,
    transparent 88%
  );
  opacity: 0;
  transform: translateY(30%);
}

.sunset-overlay.active::before {
  animation: sunset-rise 2.5s ease-out forwards;
}

@keyframes sunset-rise {
  0% { opacity: 0; transform: translateY(30%); }
  25% { opacity: 1; }
  100% { opacity: 1; transform: translateY(0); }
}

.sunset-overlay::after {
  content: '';
  position: absolute;
  left: 50%;
  bottom: 15%;
  width: 55vmin;
  height: 55vmin;
  border-radius: 50%;
  background: radial-gradient(circle,
    rgba(255, 249, 196, 0.45) 0%,
    rgba(255, 213, 79, 0.3) 20%,
    rgba(255, 179, 0, 0.18) 40%,
    rgba(255, 111, 0, 0.08) 60%,
    transparent 80%
  );
  transform: translate(-50%, 50%) scale(1.8);
  opacity: 0;
}

.sunset-overlay.active::after {
  animation: sun-settle 3s cubic-bezier(0.16, 1, 0.3, 1) 0.2s forwards;
}

@keyframes sun-settle {
  0% { transform: translate(-50%, 50%) scale(1.8); opacity: 0; }
  20% { opacity: 1; }
  100% { transform: translate(-50%, 50%) scale(1); opacity: 1; }
}

@media (max-width: 860px) {
  :root {
    --cell: clamp(34px, 10.35vw, 46px);
  }

  .shell {
    width: min(100vw - 12px, 680px);
    padding: 16px 0 28px;
  }

  .topbar,
  .game {
    grid-template-columns: minmax(0, 1fr);
    gap: 18px;
  }

  .game {
    height: auto;
    grid-auto-rows: auto;
  }

  .topbar {
    padding-top: 16px;
    padding-bottom: 16px;
  }

  .creator {
    grid-template-columns: 1fr;
  }

  .link-panel {
    grid-template-columns: 1fr;
  }

  .app-nav {
    align-items: start;
  }

  .app-nav div {
    display: grid;
    justify-items: end;
  }

  .status {
    gap: 16px;
    padding: 12px;
    align-items: center;
  }

  .rules-panel {
    margin-top: 14px;
    padding: 12px;
  }

  .row-area {
    align-items: start;
  }

  .board-wrap {
    --cell: min(64px, calc((100cqw - 4px) / (var(--grid-size, 8) + 1)));
    margin-left: -4px;
    padding-right: 8px;
  }

  .col-clues {
    margin-right: 0;
  }

  .row-clues-phantom {
    display: none;
  }

  .col-clue,
  .row-clue {
    min-height: 34px;
    font-size: 0.92rem;
  }

  .cell {
    border-radius: 2px;
  }
}
