:root {
  --background: #ffffff;
  --ink: #000000;
  --line: #dedede;
  --blue: #0066cc;
  --cell: 44px;
  font-family: "Lora", Georgia, "Times New Roman", serif;
  color: var(--ink);
  background: var(--background);
  font-synthesis: none;
}

* { box-sizing: border-box; }
html, body { width: 100%; height: 100%; }
body { margin: 0; overflow: hidden; background: var(--background); color: var(--ink); }
button, input { font: inherit; }
[hidden] { display: none !important; }

.app { position: relative; width: 100%; height: 100%; overflow: hidden; }
.grid-viewport { position: absolute; inset: 0; overflow: hidden; touch-action: none; cursor: var(--tool-cursor, crosshair); }
.grid-viewport.pan-mode, .grid-viewport.pan-mode .cell { cursor: grab; }
.grid-viewport.is-panning, .grid-viewport.is-panning .cell { cursor: grabbing; user-select: none; }
.grid-stage { position: absolute; top: 0; left: 0; transform-origin: 0 0; will-change: transform; }
.grid {
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell));
  grid-template-rows: repeat(var(--rows), var(--cell));
  border: 2px solid #595959;
  background: white;
}

.cell {
  position: relative;
  width: var(--cell);
  height: var(--cell);
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  background: white;
  color: var(--ink);
  cursor: var(--tool-cursor, crosshair);
  overflow: hidden;
}
.cell:hover { background: #f8f8f8; }
.cell:focus-visible { z-index: 3; outline: 2px solid var(--blue); outline-offset: -2px; }
.tile-glyph {
  position: relative;
  z-index: 2;
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
  color: var(--ink);
  font: 500 calc(var(--cell) * .34)/1 ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  pointer-events: none;
}
.tile-glyph svg { width: 68%; height: 68%; fill: none; stroke: var(--tile-color); stroke-width: 2.6; stroke-linecap: round; stroke-linejoin: round; }
.cell.start .tile-glyph { --tile-color: #3878b9; }
.cell.key .tile-glyph { color: #a66700; }
.cell.lock .tile-glyph { color: #333; font-weight: 600; }
.cell.wall .tile-glyph { --tile-color: #505050; }
.cell.wall .tile-glyph svg { width: 76%; height: 76%; }

.path-overlay { position: absolute; inset: 2px; z-index: 4; overflow: visible; pointer-events: none; }
.path-overlay path { fill: none; stroke-linecap: round; stroke-linejoin: round; }
.path-halo { stroke: rgba(255,255,255,.94); stroke-width: 11px; }
.path-line { stroke: var(--blue); stroke-width: 6px; filter: drop-shadow(0 1px 2px rgba(0,102,204,.3)); }
.path-dot {
  fill: var(--blue);
  stroke: rgba(255,255,255,.96);
  stroke-width: 4px;
  opacity: 0;
  transform-box: fill-box;
  transform-origin: center;
}
.path-dot.visible { opacity: 1; }
.path-end-dot.visible { animation: dotIn 240ms cubic-bezier(.2,.9,.25,1.3); }
.path-spark { opacity: 0; }
.path-spark.visible { opacity: 1; }
.path-spark-halo { fill: rgba(0,102,204,.22); animation: sparkPulse 850ms ease-in-out infinite; }
.path-spark-core { fill: white; stroke: var(--blue); stroke-width: 3px; filter: drop-shadow(0 0 4px rgba(0,102,204,.95)); }

.toolbar {
  position: fixed;
  z-index: 10;
  left: 50%;
  bottom: 22px;
  max-width: calc(100vw - 24px);
  min-height: 52px;
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 7px;
  border: 1px solid #d7d7d7;
  border-radius: 15px;
  background: rgba(255,255,255,.94);
  box-shadow: 0 6px 24px rgba(0,0,0,.08);
  backdrop-filter: blur(12px);
  transform: translateX(-50%);
}
.result-popover {
  position: absolute;
  left: 50%;
  bottom: calc(100% + 12px);
  min-width: max-content;
  padding: 9px 13px;
  border: 1px solid #d7d7d7;
  border-radius: 11px;
  background: rgba(255,255,255,.98);
  box-shadow: 0 8px 28px rgba(0,0,0,.1);
  color: #303030;
  font-size: 14px;
  line-height: 1;
  transform: translateX(-50%);
  transform-origin: bottom center;
}
.result-popover::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -5px;
  width: 9px;
  height: 9px;
  border-right: 1px solid #d7d7d7;
  border-bottom: 1px solid #d7d7d7;
  background: white;
  transform: translateX(-50%) rotate(45deg);
}
.result-popover:not([hidden]) { animation: resultBubbleIn 340ms cubic-bezier(.2,.9,.25,1.25); }
.popover-anchor { position: relative; display: grid; }
.divider { width: 1px; height: 24px; margin: 0 3px; background: #e3e3e3; }
button, input {
  height: 38px;
  border: 0;
  border-radius: 9px;
  outline: none;
  background: transparent;
  color: var(--ink);
}
button {
  position: relative;
  min-width: 38px;
  padding: 0 10px;
  cursor: pointer;
  transition: color 130ms ease, background-color 130ms ease, transform 130ms ease;
}
button:hover, input:hover { background: #f2f2f2; }
button:active { transform: scale(.94); }
button:disabled { opacity: .35; cursor: not-allowed; }
button:focus-visible, input:focus-visible { outline: 2px solid var(--blue); outline-offset: 1px; }
.icon-button, .tool, .run { display: grid; place-items: center; padding: 0; }
.icon-button svg, .tool svg { width: 21px; height: 21px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.tool { font: 500 15px/1 ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace; }
.tool svg { stroke-width: 2.5; }
.pointer-icon { fill: currentColor !important; stroke-width: 1.8 !important; }
.tool.active { background: var(--ink); color: white; }
.add-button.active { background: var(--ink); color: white; }
.add-button[aria-expanded="true"], #settingsButton[aria-expanded="true"] { background: var(--ink); color: white; }
.menu-tool > span { display: grid; place-items: center; }
.run { background: #164c2f; color: white; }
.run:hover { background: #0f3f25; }
.run.running { background: #1e6a3f; }
.run #runIcon { width: 23px; height: 23px; display: grid; place-items: center; line-height: 1; }
.run #runIcon svg { width: 23px; height: 23px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }

.help-button {
  position: fixed;
  z-index: 20;
  top: 20px;
  right: 20px;
  width: 36px;
  min-width: 36px;
  height: 36px;
  padding: 0;
  border-radius: 50%;
  color: #8a8a8a;
}
.help-button:hover { background: #f2f2f2; color: #666; }
.help-button svg { width: 27px; height: 27px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.help-button .help-dot { fill: currentColor; stroke: none; }

.help-backdrop {
  position: fixed;
  z-index: 50;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(255,255,255,.74);
  backdrop-filter: blur(3px);
}
.help-backdrop:not([hidden]) { animation: fadeIn 180ms ease-out; }
.help-dialog {
  width: min(480px, 100%);
  max-height: calc(100vh - 40px);
  overflow: auto;
  padding: 28px 30px 24px;
  border: 1px solid #d3d3d3;
  border-radius: 18px;
  background: white;
  box-shadow: 0 22px 70px rgba(0,0,0,.16);
  color: #252525;
  outline: none;
  animation: helpDialogIn 260ms cubic-bezier(.2,.85,.25,1.1);
}
.help-dialog h1 { margin: 0 0 10px; font-size: 24px; font-weight: 500; line-height: 1.2; }
.help-dialog > p { margin: 0; color: #595959; font-size: 15px; line-height: 1.55; }
.help-list { display: grid; gap: 8px; margin: 22px 0 24px; padding: 0; list-style: none; }
.help-list li { min-height: 42px; display: grid; grid-template-columns: 38px 1fr; align-items: center; gap: 12px; color: #444; font-size: 14px; line-height: 1.4; }
.help-tool-icon { width: 38px; height: 38px; display: grid; place-items: center; border: 1px solid #e0e0e0; border-radius: 9px; color: #343434; }
.help-tool-icon svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.help-play-icon { border: 0; background: #164c2f; color: white; font: 500 15px/1 ui-monospace, SFMono-Regular, Menlo, monospace; }
.continue-button { min-width: 112px; display: block; margin: 0 auto; padding: 0 18px; background: #111; color: white; }
.continue-button:hover { background: #333; }

.popover {
  position: absolute;
  z-index: 25;
  bottom: calc(100% + 11px);
  padding: 7px;
  border: 1px solid #d7d7d7;
  border-radius: 12px;
  background: rgba(255,255,255,.98);
  box-shadow: 0 8px 28px rgba(0,0,0,.11);
  backdrop-filter: blur(12px);
  transform-origin: bottom center;
}
.settings-popover:not([hidden]), .add-popover:not([hidden]) { animation: centeredBubbleIn 300ms cubic-bezier(.2, .9, .25, 1.25); }
.lock-picker:not([hidden]) { animation: bubbleIn 300ms cubic-bezier(.2, .9, .25, 1.25); }
.settings-popover { left: 50%; width: 184px; padding: 8px 10px; transform: translateX(-50%); }
.settings-popover label { min-height: 38px; display: flex; align-items: center; justify-content: space-between; gap: 20px; color: #303030; font-size: 14px; }
.settings-popover input { width: 58px; height: 30px; padding: 0 8px; border: 1px solid #dedede; border-radius: 7px; text-align: right; font: 13px/1 ui-monospace, SFMono-Regular, Menlo, monospace; background: #fff; }
.settings-popover input:focus { border-color: #888; }
.settings-popover input::-webkit-inner-spin-button { opacity: .65; }
.add-popover { left: 50%; display: grid; grid-template-columns: repeat(4, 42px); gap: 3px; transform: translateX(-50%); }
.menu-tool { width: 42px; min-width: 42px; height: 42px; }
.menu-tool.active { background: #ededed; color: var(--ink); }
.lock-picker { position: absolute; left: calc(100% + 9px); bottom: 0; padding: 7px; border: 1px solid #d7d7d7; border-radius: 12px; background: rgba(255,255,255,.98); box-shadow: 0 8px 28px rgba(0,0,0,.11); }
#lockChoices { display: grid; gap: 3px; }
.lock-choice { width: 36px; min-width: 36px; height: 36px; display: grid; place-items: center; padding: 0; }
.lock-choice svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2.5; stroke-linecap: round; stroke-linejoin: round; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 88px;
  z-index: 30;
  max-width: calc(100vw - 32px);
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  color: #555;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 8px);
  transition: opacity 160ms ease, transform 160ms ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }

@keyframes dotIn { from { opacity: 0; transform: scale(.55); } to { opacity: 1; transform: scale(1); } }
@keyframes sparkPulse { 0%, 100% { r: 8px; opacity: .35; } 50% { r: 12px; opacity: .08; } }
@keyframes resultBubbleIn {
  0% { opacity: 0; transform: translateX(-50%) translateY(9px) scale(.84); }
  70% { opacity: 1; transform: translateX(-50%) translateY(-2px) scale(1.04); }
  100% { opacity: 1; transform: translateX(-50%) translateY(0) scale(1); }
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes helpDialogIn { from { opacity: 0; transform: translateY(10px) scale(.96); } to { opacity: 1; transform: translateY(0) scale(1); } }
@keyframes centeredBubbleIn {
  0% { opacity: 0; transform: translateX(-50%) translateY(11px) scale(.72); }
  68% { opacity: 1; transform: translateX(-50%) translateY(-2px) scale(1.06); }
  100% { opacity: 1; transform: translateX(-50%) translateY(0) scale(1); }
}
@keyframes bubbleIn {
  0% { opacity: 0; transform: translateY(11px) scale(.72); }
  68% { opacity: 1; transform: translateY(-2px) scale(1.06); }
  100% { opacity: 1; transform: translateY(0) scale(1); }
}

@media (max-width: 700px) {
  .toolbar { bottom: 14px; }
  .help-button { top: 12px; right: 12px; }
  .help-dialog { padding: 24px 20px 20px; }
  .divider { margin: 0 1px; }
  button { min-width: 36px; height: 36px; padding: 0 8px; }
  .lock-picker { left: auto; right: calc(100% + 9px); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
