/* Base palette = world 1 (the loading screen's look). During play these
 * variables are overridden inline on <html> by applyPalette() in app.js,
 * which computes every world's colors from the game's own worldcolor.lua
 * values — so there are deliberately no per-world CSS themes here. */
:root {
  color-scheme: light;
  --bg: #d7d9a6;
  --symbol: #686f0b;
  --symbol-soft: rgba(104, 111, 11, 0.48);
  --grid: var(--symbol);
  --line: #686f0b;
  --button-bg: transparent;
  --focus: rgba(104, 111, 11, 0.84);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
  overflow: hidden;
}

body {
  background: var(--bg);
  color: var(--symbol);
  -webkit-tap-highlight-color: transparent;
}

.shell {
  -webkit-user-select: none;
  user-select: none;
}

.icon-button,
.daily-button {
  touch-action: manipulation;
}

button {
  color: inherit;
  font: inherit;
}

[hidden] {
  display: none !important;
}

.shell {
  display: grid;
  height: 100%;
  min-height: 0;
  place-items: center;
  position: relative;
}

.topbar h1,
.topbar p {
  height: 1px;
  margin: -1px;
  overflow: hidden;
  position: absolute;
  white-space: nowrap;
  width: 1px;
}

.toolbar {
  display: flex;
  gap: 14px;
  position: fixed;
  right: 28px;
  right: max(28px, env(safe-area-inset-right));
  top: 24px;
  top: max(24px, env(safe-area-inset-top));
  z-index: 2;
}

.icon-button {
  align-items: center;
  background: var(--button-bg);
  border: 0;
  border-radius: 0;
  cursor: pointer;
  display: inline-flex;
  height: 42px;
  justify-content: center;
  opacity: 0.78;
  padding: 0;
  width: 42px;
}

.icon-button svg {
  fill: none;
  height: 32px;
  stroke: var(--symbol);
  stroke-linecap: square;
  stroke-linejoin: miter;
  stroke-width: 2.1;
  width: 32px;
}

#prevButton,
#nextButton {
  height: clamp(72px, 10vw, 112px);
  opacity: 1;
  position: fixed;
  top: 50%;
  transform: translateY(-50%);
  width: clamp(72px, 10vw, 112px);
  z-index: 3;
}

#prevButton {
  left: clamp(16px, 5vw, 72px);
  left: max(clamp(16px, 5vw, 72px), env(safe-area-inset-left));
}

#nextButton {
  right: clamp(16px, 5vw, 72px);
  right: max(clamp(16px, 5vw, 72px), env(safe-area-inset-right));
}

#prevButton svg,
#nextButton svg {
  height: 100%;
  width: 100%;
}

#prevButton.nav-filled svg,
#nextButton.nav-filled svg {
  fill: var(--symbol);
  stroke: var(--symbol);
  stroke-linejoin: miter;
  stroke-width: 1.8;
}

#prevButton.nav-outline svg,
#nextButton.nav-outline svg {
  fill: none;
  stroke: var(--symbol);
  stroke-linejoin: miter;
  stroke-width: 1.8;
}

/* ID-level specificity on purpose: the base #nextButton rule sets opacity 1,
 * so a bare .nav-locked class rule would silently lose the cascade. */
#nextButton.nav-locked {
  cursor: default;
  opacity: 0.45;
}

.icon-button:hover {
  opacity: 1;
}

.icon-button:focus-visible {
  opacity: 1;
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.icon-button:disabled {
  cursor: default;
  opacity: 0.22;
  outline: 0;
}

.stage {
  align-content: center;
  display: grid;
  gap: clamp(18px, 3vh, 28px);
  justify-items: center;
  max-width: 100vw;
  /* 100% (the .shell chain), NOT 100vh: on mobile 100vh is the large
   * viewport, which overflows the overflow-hidden page and clips the rule
   * dots whenever the browser toolbar is visible. */
  min-height: 100%;
  padding: clamp(54px, 8vh, 72px) clamp(84px, 12vw, 150px) clamp(52px, 8vh, 76px);
  width: 100%;
}

.board-zone {
  align-items: center;
  display: grid;
  justify-items: center;
  width: 100%;
}

.board {
  background: transparent;
  cursor: default;
  overflow: visible;
  touch-action: none;
}

.grid rect {
  fill: var(--grid);
}

.visited-cell {
  fill: var(--line);
  opacity: 0.15;
  pointer-events: none;
}

.player-path-solid,
.player-path-segment {
  fill: var(--line);
  pointer-events: none;
}

/* The level's own example line (panel 1 only): dotted, dash = gap = 1/8
 * cell, drawn at grid-line weight like the game's drawDottedLine. */
.hint-dotted {
  pointer-events: none;
  stroke: var(--symbol);
  stroke-dasharray: 0.125 0.125;
  stroke-width: 0.05;
}

.symbol-outline {
  fill: none;
  stroke-linecap: square;
  stroke-linejoin: miter;
  stroke-width: 0.055;
}

.symbol-outline.symbol-filled {
  fill: var(--symbol);
}

.signal-bar {
  align-items: center;
  display: grid;
  gap: 20px;
  justify-items: center;
  max-width: min(100%, 760px);
}

.rule-dots {
  display: flex;
  gap: clamp(26px, 4vw, 46px);
  justify-content: center;
  flex-wrap: wrap;
  min-height: 58px;
  max-width: 100%;
}

.rule-dot {
  background: transparent;
  border: 5.3px solid var(--symbol);
  border-radius: 50%;
  display: block;
  height: 48px;
  opacity: 1;
  width: 48px;
}

.rule-dot.hit {
  background: var(--symbol);
  border-color: var(--symbol);
}

.rule-dot.miss {
  border-color: var(--symbol);
  opacity: 1;
}

@media (max-width: 760px) {
  body {
    overflow: auto;
  }

  .shell {
    min-height: 100%;
  }

  .stage {
    padding: 70px 64px 64px;
  }

  .toolbar {
    gap: 6px;
    justify-content: flex-end;
    left: 12px;
    left: max(12px, env(safe-area-inset-left));
    right: 12px;
    right: max(12px, env(safe-area-inset-right));
    top: 14px;
    top: max(14px, env(safe-area-inset-top));
  }

  .icon-button {
    height: 36px;
    width: 36px;
  }

  .icon-button svg {
    height: 27px;
    width: 27px;
  }

  #prevButton,
  #nextButton {
    height: 54px;
    top: 50%;
    width: 54px;
  }

  #prevButton {
    left: 10px;
    left: max(10px, env(safe-area-inset-left));
  }

  #nextButton {
    right: 10px;
    right: max(10px, env(safe-area-inset-right));
  }

  #prevButton svg,
  #nextButton svg {
    height: 100%;
    width: 100%;
  }

  .rule-dots {
    gap: 14px;
    min-height: 42px;
  }

  .rule-dot {
    height: 36px;
    width: 36px;
  }
}

@media (max-width: 420px) {
  .stage {
    padding-left: 54px;
    padding-right: 54px;
  }

  #prevButton,
  #nextButton {
    height: 44px;
    width: 44px;
  }

  #prevButton {
    left: 6px;
    left: max(6px, env(safe-area-inset-left));
  }

  #nextButton {
    right: 6px;
    right: max(6px, env(safe-area-inset-right));
  }

  .rule-dot {
    height: 30px;
    width: 30px;
  }
}

/* Landscape phones: width-based queries never fire (a phone on its side is
 * 700-900px wide), so compact chrome must key on the short height instead. */
@media (max-height: 560px) {
  .stage {
    gap: 8px;
    padding: 40px 60px 20px;
  }

  .rule-dots {
    gap: 12px;
    min-height: 30px;
  }

  .rule-dot {
    height: 26px;
    width: 26px;
    border-width: 4px;
  }

  .toolbar {
    gap: 4px;
    right: 8px;
    right: max(8px, env(safe-area-inset-right));
    top: 8px;
    top: max(8px, env(safe-area-inset-top));
  }

  .icon-button {
    height: 34px;
    width: 34px;
  }

  .icon-button svg {
    height: 25px;
    width: 25px;
  }

  #prevButton,
  #nextButton {
    height: 48px;
    width: 48px;
  }

  #prevButton {
    left: 6px;
    left: max(6px, env(safe-area-inset-left));
  }

  #nextButton {
    right: 6px;
    right: max(6px, env(safe-area-inset-right));
  }
}

.world-map-screen {
  width: 100%;
  max-width: 980px;
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: clamp(14px, 3vw, 26px) clamp(16px, 4vw, 30px);
  overflow: hidden;
  color: var(--symbol);
}

.map-header {
  display: flex;
  justify-content: flex-end;
  align-items: flex-start;
  gap: 18px;
  flex-wrap: wrap;
  width: 100%;
  margin-bottom: clamp(8px, 2vw, 16px);
}

.map-title-zone {
  align-items: flex-end;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.daily-button {
  align-items: center;
  background: transparent;
  border: 2px solid var(--symbol);
  border-radius: 4px;
  color: var(--symbol);
  cursor: pointer;
  display: inline-flex;
  font-size: 15px;
  font-weight: 700;
  gap: 7px;
  height: 38px;
  padding: 0 14px;
}

.daily-button.solved {
  background: var(--symbol);
  color: var(--bg);
}

.daily-button .daily-streak {
  font-size: 12px;
  font-weight: 600;
  opacity: 0.75;
}

/* The map canvas is the pinch-zoom viewport: the .map-zoom wrapper inside is
 * translated/scaled by app.js, so the canvas itself never scrolls. */
.map-canvas {
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
  position: relative;
  touch-action: none;
  width: 100%;
}

.map-zoom {
  transform-origin: 0 0;
  width: 100%;
}

@media (max-width: 560px) {
  .world-map-screen {
    max-width: none;
  }

  .map-header {
    align-items: stretch;
    flex-direction: column-reverse;
  }

  .map-title-zone {
    align-items: flex-start;
    text-align: left;
  }

}

/* ===== Main menu (Campaign | Daily split) ===== */

.main-menu {
  width: 100%;
  max-width: 980px;
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: clamp(14px, 3vw, 26px) clamp(16px, 4vw, 30px);
  color: var(--symbol);
}

.menu-split {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  gap: clamp(12px, 2.5vw, 22px);
}

.menu-half {
  flex: 1 1 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(8px, 1.6vh, 14px);
  background: transparent;
  border: 2px solid var(--symbol);
  border-radius: 10px;
  color: var(--symbol);
  cursor: pointer;
  padding: 18px;
  transition: background-color 120ms ease, color 120ms ease;
}

.menu-half:hover,
.menu-half:focus-visible {
  background: color-mix(in srgb, var(--symbol) 12%, transparent);
}

.menu-half:active {
  background: color-mix(in srgb, var(--symbol) 20%, transparent);
}

.menu-half-glyph {
  font-size: clamp(26px, 5vh, 40px);
  line-height: 1;
}

.menu-half-glyph svg {
  width: clamp(30px, 6vh, 46px);
  height: clamp(30px, 6vh, 46px);
  fill: currentColor;
}

.menu-half-name {
  font-size: clamp(22px, 4.5vh, 34px);
  font-weight: 800;
  letter-spacing: 0.04em;
}

.menu-half-sub {
  font-size: clamp(12px, 2vh, 15px);
  font-weight: 600;
  opacity: 0.75;
  text-align: center;
}

/* Portrait phones: stack the halves, still an even split. */
@media (max-width: 560px), (orientation: portrait) {
  .menu-split {
    flex-direction: column;
  }
}

/* ===== Lua-engine build additions ===== */

.loading-overlay {
  align-items: center;
  background: var(--bg);
  display: flex;
  inset: 0;
  justify-content: center;
  /* Short viewports: let the data-gate card scroll instead of clipping. */
  overflow: auto;
  padding: 16px;
  position: fixed;
  z-index: 40;
}

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

.loading-card {
  align-items: center;
  color: var(--symbol);
  display: grid;
  gap: 18px;
  justify-items: center;
  font-size: 15px;
}

.loading-mark {
  animation: loading-spin 1.1s linear infinite;
  border: 5px solid var(--symbol-soft);
  border-radius: 50%;
  border-top-color: var(--symbol);
  height: 44px;
  width: 44px;
}

@keyframes loading-spin {
  to { transform: rotate(360deg); }
}

.data-card {
  gap: 14px;
  margin: auto; /* stays centered, yet scrollable when taller than the view */
  max-width: min(520px, 88vw);
  padding: 16px;
  text-align: center;
}

.data-card h2 {
  font-size: 20px;
  margin: 0;
}

.data-card p {
  line-height: 1.5;
  margin: 0;
}

.data-card a {
  color: var(--symbol);
}

.data-card code {
  background: var(--symbol-soft);
  border-radius: 4px;
  font-size: 0.9em;
  padding: 1px 5px;
}

.data-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
}

.data-actions button {
  background: var(--symbol);
  border: none;
  border-radius: 8px;
  color: var(--bg);
  cursor: pointer;
  font: inherit;
  font-weight: 600;
  padding: 10px 16px;
}

.data-actions button:hover {
  opacity: 0.85;
}

#dataKeepButton {
  background: transparent;
  border: 1px solid var(--symbol);
  color: var(--symbol);
}

.data-error {
  color: #d05050;
}

.rule-dot.numbered {
  position: relative;
}

.rule-dot.numbered::after {
  color: var(--symbol);
  content: attr(data-badge);
  font-size: 12px;
  font-weight: 700;
  left: 50%;
  position: absolute;
  top: calc(100% + 4px);
  transform: translateX(-50%);
  white-space: nowrap;
}

/* ===== SVG world map + special-level additions ===== */

.map-svg {
  display: block;
  height: auto;
  margin: 0 auto;
  width: min(94vw, calc((100vh - 170px) * 14 / 17), 900px);
}

/* Short screens (landscape phones): fit the map to the width instead of the
 * height — it starts readable and pans/zooms inside the canvas viewport. */
@media (max-height: 560px) {
  .map-svg {
    width: min(88vw, 900px);
  }
}

.map-node-svg {
  cursor: pointer;
}

.map-node-svg circle:first-child {
  transition: stroke-width 120ms ease;
}

.map-node-svg:hover circle:first-child {
  stroke-width: 0.11;
}

.map-world-number {
  user-select: none;
  -webkit-user-select: none;
}

/* Gentle "you are here" pulse on the current-level ring. */
.map-current-ring {
  animation: map-current-pulse 2.4s ease-in-out infinite;
}

@keyframes map-current-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.45; }
}

@media (prefers-reduced-motion: reduce) {
  .map-current-ring {
    animation: none;
  }
}

.map-node-svg:focus {
  outline: none;
}

.map-node-svg:focus-visible circle:first-child {
  stroke-width: 0.14;
}

.map-node-svg:focus circle:first-child {
  stroke-width: 0.12;
}

.player-path-ghost {
  opacity: 0.45;
}

.level-tag {
  color: var(--symbol);
  font-size: 22px;
  font-weight: 700;
  left: 22px;
  left: max(22px, env(safe-area-inset-left));
  margin: 0;
  position: fixed;
  top: 16px;
  top: max(16px, env(safe-area-inset-top));
  z-index: 5;
}

/* Must trail the base .level-tag rule above to win the cascade. */
@media (max-height: 560px) {
  .level-tag {
    font-size: 16px;
    left: 12px;
    left: max(12px, env(safe-area-inset-left));
    top: 10px;
    top: max(10px, env(safe-area-inset-top));
  }
}
