/* Color palettes; the canvas reads the same variables from app.js. */
:root {
  color-scheme: light;
  /* Soft, warm palette with clearly distinct roles. */
  --bg: #f4efe6;            /* window background (linen) */
  --panel: #fbf8f2;         /* header card, inputs */
  --border: #e2d9cb;
  --shadow: #e3dbcd;
  --text: #4a4540;
  --muted: #8a8178;
  --hover: #f1eadf;         /* hovered button */
  --unbuilt: #d9d1c3;       /* not yet carved */
  --floor: #fffcf7;         /* carved passages */
  --wall: #4b5a6b;          /* slate blue-gray */
  --accent-fg: #ffffff;     /* text on the accent (wall-colored) button */
  --accent-hover: #5d6e81;
  --accent-pressed: #3c4856;
  --visit: #d4e5f1;         /* explored by solver (mist blue) */
  --solution: #4fa3a5;      /* solver's shortest path (teal) */
  --start: #8cc59f;         /* sage green */
  --goal: #e58f8f;          /* dusty rose */
  --mark-text: #ffffff;     /* labels on start/goal, player outline */
  --player: #f2a65a;        /* warm orange */
  --trail: #b7a4d9;         /* lavender */

  --font: "Microsoft JhengHei UI", "Microsoft JhengHei", "PingFang TC",
          "Noto Sans CJK TC", "Noto Sans TC", system-ui, sans-serif;
}

/* Night version of the same hues: dark charcoal floor, light walls. */
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #1d2025;            /* window background (charcoal) */
  --panel: #272b31;
  --border: #3a4048;
  --shadow: #131518;
  --text: #e6e1d8;
  --muted: #9b958b;
  --hover: #323740;
  --unbuilt: #3c414a;
  --floor: #2a2e35;
  --wall: #a9bbcd;          /* pale slate */
  --accent-fg: #1d2025;
  --accent-hover: #bfcedd;
  --accent-pressed: #8ea2b6;
  --visit: #2e4659;         /* deep blue */
  --solution: #5cc6c8;
  --start: #5fae7b;
  --goal: #d67474;
  --mark-text: #1d2025;
  --player: #f2a65a;
  --trail: #a48ed6;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 12px;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 var(--font);
}

:focus-visible { outline: 2px solid var(--solution); outline-offset: 2px; }

/* ---- header ---- */

.card {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 16px;
  padding: 8px 16px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.card h1 { margin: 0; font-size: 20px; font-weight: 700; }

.stats {
  margin-left: auto;
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 4px 16px;
}

.muted { color: var(--muted); }
.stats .muted { font-size: 15px; }

.timer {
  font-size: 32px;
  font-weight: 700;
  color: var(--wall);
  font-variant-numeric: tabular-nums;
}

/* ---- controls ---- */

.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 10px;
  padding: 6px 0;
}

.controls label { display: inline-flex; align-items: center; gap: 4px; }
.sound { margin-left: auto; }

.btn {
  font: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 14px;
  cursor: pointer;
}
.btn:hover { background: var(--hover); }
.btn:active { background: var(--border); }

.btn.accent { color: var(--accent-fg); background: var(--wall); border-color: var(--wall); }
.btn.accent:hover { background: var(--accent-hover); }
.btn.accent:active { background: var(--accent-pressed); }

input[type="number"], select {
  font: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 3px 6px;
}
input[type="number"] { width: 4.5em; }
select { margin-left: 2px; }
input[type="range"] { width: 100px; accent-color: var(--wall); }
input[type="checkbox"] { accent-color: var(--wall); }

/* ---- maze ---- */

.play { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 8px; }
.board { position: relative; flex: 1; min-height: 200px; }

/* Absolutely positioned so the canvas size never feeds back into the layout.
   touch-action: none lets swipes move the player instead of scrolling the page. */
#maze {
  position: absolute; inset: 0; width: 100%; height: 100%;
  display: block; outline: none; touch-action: none;
}

/* ---- on-screen direction pad (touch devices and narrow windows) ---- */

.dpad {
  --pad: 58px;
  display: none;
  grid-template: repeat(3, var(--pad)) / repeat(3, var(--pad));
  gap: 6px;
  align-self: center;
}
.dpad .up { grid-area: 1 / 2; }
.dpad .left { grid-area: 2 / 1; }
.dpad .right { grid-area: 2 / 3; }
.dpad .down { grid-area: 3 / 2; }

.restart-btn {
  align-self: center;
  white-space: nowrap;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.pad {
  font: 22px/1 var(--font);
  color: var(--wall);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 2px 0 var(--shadow);
  cursor: pointer;
  touch-action: manipulation;   /* no double-tap zoom */
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;  /* no long-press menu on iOS */
  -webkit-tap-highlight-color: transparent;
}
.pad.pressed, .pad:active { background: var(--border); transform: translateY(1px); box-shadow: none; }
@media (hover: hover) {
  .pad:hover { background: var(--hover); }
}

.help-touch { display: none; }

@media (pointer: coarse), (max-width: 820px) {
  .dpad { display: grid; }
  .help-keys { display: none; }
  .help-touch { display: inline; }
}

/* Phones: give the maze as much room as possible. */
@media (max-width: 600px) {
  body { padding: 8px; }
  .card { padding: 6px 12px; gap: 4px 12px; }
  .card h1 { font-size: 17px; }
  .stats .muted { font-size: 13px; }
  /* Restart, state, moves and timer share one full-width row. */
  .stats { width: 100%; margin-left: 0; justify-content: space-between; align-items: center; gap: 4px 8px; }
  .stats .timer-label { display: none; }
  .restart-btn { padding: 3px 10px; }
  .timer { font-size: 24px; }
  .dpad { --pad: 54px; }
}

/* Phones held sideways: put the pad beside the maze, not below it, and
   squeeze the chrome so the short screen goes to the maze. */
@media (orientation: landscape) and (max-height: 500px) {
  body { padding: 6px 10px; gap: 2px; font-size: 13px; }
  .card { padding: 2px 12px; }
  .card h1 { font-size: 16px; }
  .card .btn { padding: 2px 10px; }
  .stats .muted { font-size: 12px; }
  .timer { font-size: 20px; }
  .controls { padding: 2px 0; gap: 4px 8px; }
  .btn, input[type="number"], select { padding: 1px 8px; }
  .play { flex-direction: row; }
  .board { min-height: 0; }
  .dpad { --pad: 48px; }
  footer .legend { display: none; }
  #status { margin: 0; }
}

/* ---- footer ---- */

#status { margin: 0 0 4px; min-height: 1.4em; }

.legend {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 12px;
  color: var(--muted);
}
.legend .item { display: inline-flex; align-items: center; gap: 5px; }
.help { margin-left: auto; }

.swatch { display: inline-block; width: 12px; height: 12px; border-radius: 2px; }
.swatch.oval { border-radius: 50%; }
.swatch.line { height: 5px; border-radius: 3px; }
.sw-start { background: var(--start); }
.sw-goal { background: var(--goal); }
.sw-player { background: var(--player); }
.sw-trail { background: var(--trail); }
.sw-visit { background: var(--visit); }
.sw-solution { background: var(--solution); }
