:root {
  --bg: #f4f5f8;
  --surface: #ffffff;
  --text: #1a1d23;
  --muted: #5d6472;
  --border: #d9dde4;
  --accent: #4f46e5;
  --accent-text: #ffffff;

  /* Grid colors (read by sketch.js) */
  --cell: #ffffff;
  --gridline: #e2e5eb;
  --wall: #2b303b;
  --open: #a7e8c9;
  --closed: #cfdaf7;
  --path: #f59e0b;
  --start: #16a34a;
  --end: #dc2626;
  --current: #4f46e5;
  --hover: #4f46e5;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a21;
    --text: #e8eaef;
    --muted: #9aa1ad;
    --border: #2c313c;
    --accent: #818cf8;
    --accent-text: #0f1115;

    --cell: #1b1f27;
    --gridline: #262b35;
    --wall: #6b7488;
    --open: #1d5a45;
    --closed: #27365c;
    --path: #fbbf24;
    --start: #22c55e;
    --end: #ef4444;
    --current: #a5b4fc;
    --hover: #a5b4fc;
  }
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.45;
}

.top,
main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 16px;
}

.top {
  padding-top: 20px;
  padding-bottom: 12px;
}

h1 {
  margin: 0;
  font-size: 1.6rem;
  letter-spacing: -0.01em;
}

.sub {
  margin: 4px 0 0;
  color: var(--muted);
}

/* Controls */

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 18px;
  align-items: center;
  margin-bottom: 12px;
}

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

button {
  font: inherit;
  font-size: 0.9rem;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

button:hover {
  border-color: var(--muted);
}

button:disabled {
  opacity: 0.45;
  cursor: default;
}

button:focus-visible,
input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

button.tool[aria-pressed="true"] {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
  min-width: 96px;
  justify-content: center;
}

kbd {
  font-family: inherit;
  font-size: 0.72rem;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0 4px;
}

.dot {
  display: inline-grid;
  place-items: center;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  color: #fff;
  font-size: 0.7rem;
  font-weight: 700;
}

.dot-a { background: var(--start); }
.dot-b { background: var(--end); }

.options label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.9rem;
  color: var(--muted);
}

.speed input {
  width: 110px;
  accent-color: var(--accent);
}

.check input {
  accent-color: var(--accent);
}

/* Grid */

#stage {
  display: flex;
  justify-content: center;
}

#stage canvas {
  display: block;
  border-radius: 6px;
  touch-action: none;
  cursor: crosshair;
}

/* Status and legend */

.footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 24px;
  padding: 12px 0 20px;
}

#status {
  margin: 0;
  flex: 1 1 320px;
  min-height: 1.45em;
}

.stats {
  display: flex;
  gap: 20px;
  margin: 0;
}

.stats div {
  display: flex;
  gap: 6px;
  align-items: baseline;
}

.stats dt {
  color: var(--muted);
  font-size: 0.85rem;
}

.stats dd {
  margin: 0;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.legend {
  flex-basis: 100%;
  display: flex;
  flex-wrap: wrap;
  gap: 6px 16px;
  margin: 0;
  padding: 0;
  list-style: none;
  font-size: 0.85rem;
  color: var(--muted);
}

.legend li {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.swatch {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  border: 1px solid var(--border);
}

.sw-start  { background: var(--start); border-radius: 50%; }
.sw-end    { background: var(--end); border-radius: 50%; }
.sw-wall   { background: var(--wall); }
.sw-open   { background: var(--open); }
.sw-closed { background: var(--closed); }
.sw-path   { background: var(--path); height: 4px; border: 0; }
