/* ============================================================
   Sandfall — dark dashboard theme
   ============================================================ */

:root {
  --bg: #121218;
  --bg-panel: #1a1a22;
  --bg-elevated: #22222c;
  --border: #2e2e3a;
  --text: #e6e6ee;
  --text-dim: #9a9aae;
  --accent: #8896ff;
  --danger: #ff6b6b;

  --sand: #e9c46a;
  --water: #457b9d;
  --wall: #6c757d;
  --acid: #55ff33;

  /* Fallback only — js/app.js overrides this at startup from
   * CONFIG.CANVAS_SIZE so the layout always matches the config. */
  --canvas-size: 400px;
  --radius: 10px;
}

* {
  box-sizing: border-box;
}

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

body {
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: radial-gradient(circle at 30% 20%, #17171f 0%, var(--bg) 70%);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

/* ---------- Layout ---------- */

.app {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr) 260px;
  grid-template-areas: "left stage right";
  gap: 24px;
  padding: 24px;
  max-width: 1180px;
  margin: 0 auto;
}

.panel-left {
  grid-area: left;
}

.panel-right {
  grid-area: right;
}

.stage {
  grid-area: stage;
}

.stage {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: start; /* top-align with the panels */
  gap: 12px;
}

/* ---------- Status bar ---------- */

.status-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  /* Fixed width (exactly the canvas frame's width: canvas + 14px padding
   * x2 + 1px border x2) so the bar never reflows as the FPS text grows
   * or shrinks — a content-sized bar can tip the page past the
   * scrollbar threshold and make the layout oscillate. */
  width: min(100%, calc(var(--canvas-size) + 30px));
  /* Items may wrap to a second line but must never overflow the box:
   * overflowing flex items (min-width: auto) widen the page's
   * scrollWidth, and mobile Firefox then widens the layout viewport,
   * stretching the whole layout including the canvas frame. */
  flex-wrap: wrap;
  white-space: nowrap;
  gap: 10px;
  font-size: 0.75rem;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
}

.status-item {
  min-width: 0; /* never force the bar's content past its fixed width */
}

.status-item span {
  color: var(--text);
}

.status-sep {
  color: var(--border);
}

.canvas-frame {
  padding: 14px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) + 6px);
  box-shadow:
    0 20px 50px rgba(0, 0, 0, 0.55),
    inset 0 1px 0 rgba(255, 255, 255, 0.04);
}

#sim-canvas {
  display: block;
  width: var(--canvas-size);
  height: var(--canvas-size);
  background: #1e1e24;
  border: 1px solid var(--border);
  border-radius: 6px;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  cursor: crosshair;
  touch-action: none;
  user-select: none;
}

/* ---------- Panel ---------- */

.panel {
  display: flex;
  flex-direction: column;
  gap: 20px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px 18px;
  height: fit-content;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04);
}

.panel-header h1 {
  margin: 0;
  font-size: 1.5rem;
  letter-spacing: 0.02em;
  background: linear-gradient(90deg, var(--sand), var(--accent));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.subtitle {
  margin: 2px 0 0;
  font-size: 0.8rem;
  color: var(--text-dim);
}

.panel-section h2 {
  margin: 0 0 10px;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--text-dim);
  display: flex;
  align-items: center;
  gap: 8px;
}

.value-badge {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 1px 9px;
  font-size: 0.72rem;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

/* ---------- Material buttons ---------- */

.material-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.material-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 9px 12px;
  background: var(--bg-elevated);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font: inherit;
  font-size: 0.9rem;
  cursor: pointer;
  transition: border-color 0.15s ease, transform 0.1s ease,
    box-shadow 0.15s ease;
}

.material-btn:hover {
  transform: translateY(-1px);
  border-color: #3d3d4d;
}

.material-btn kbd {
  margin-left: auto;
  font-family: inherit;
  font-size: 0.68rem;
  color: var(--text-dim);
  background: #2a2a36;
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 6px;
}

.material-btn.is-active {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent), 0 4px 14px rgba(136, 150, 255, 0.15);
}

.swatch {
  width: 16px;
  height: 16px;
  border-radius: 5px;
  flex: none;
  border: 1px solid rgba(255, 255, 255, 0.12);
}

.swatch-sand { background: var(--sand); }
.swatch-water { background: var(--water); }
.swatch-wall { background: var(--wall); }
.swatch-acid {
  background: var(--acid);
  box-shadow: 0 0 8px rgba(85, 255, 51, 0.7);
}

.swatch-eraser {
  background:
    linear-gradient(
      45deg,
      transparent 42%,
      var(--text-dim) 42%,
      var(--text-dim) 58%,
      transparent 58%
    ),
    #1e1e24;
}

/* ---------- Slider ---------- */

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 6px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 999px;
  outline: none;
  cursor: pointer;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid #10101a;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
}

input[type="range"]::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid #10101a;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
}

/* ---------- Action buttons ---------- */

.action-row {
  display: flex;
  gap: 8px;
}

.action-btn {
  flex: 1;
  padding: 9px 10px;
  font: inherit;
  font-size: 0.85rem;
  color: var(--text);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.action-btn:hover {
  border-color: #3d3d4d;
}

.action-btn.danger {
  color: var(--danger);
}

.action-btn.danger:hover {
  background: rgba(255, 107, 107, 0.1);
  border-color: rgba(255, 107, 107, 0.5);
}

.action-btn.is-active {
  border-color: var(--accent);
  color: var(--accent);
}

.action-btn kbd {
  font-family: inherit;
  font-size: 0.65rem;
  color: var(--text-dim);
  background: #2a2a36;
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 0 5px;
  margin-left: 4px;
}

/* ---------- Footer ---------- */

.panel-footer p {
  margin: 0;
  font-size: 0.75rem;
  line-height: 1.5;
  color: var(--text-dim);
}

/* ---------- Narrow screens (two columns) ---------- */

@media (max-width: 1160px) and (min-width: 761px) {
  .app {
    grid-template-columns: 260px minmax(0, 1fr);
    grid-template-areas:
      "left stage"
      "right stage";
  }
}

/* ---------- Small screens (stacked: canvas, then the panels) ---------- */

@media (max-width: 760px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-areas:
      "stage"
      "left"
      "right";
    justify-items: center;
  }

  .panel {
    width: 100%;
    max-width: var(--canvas-size);
  }

  /* With justify-items: center the stage does not stretch to the grid
   * column; without an explicit width its size floats with its content
   * (the status bar text), which makes the canvas breathe. Pin it. */
  .stage {
    width: 100%;
  }

  /* Keep the material picker compact so the left panel stays short. */
  .material-list {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  .material-btn {
    font-size: 0.8rem;
    padding: 8px 8px;
  }

  .material-btn .material-name {
    max-width: 60px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* Let the frame fill the column exactly so nothing overflows the
   * viewport (an overflowing frame makes mobile browsers widen the
   * layout viewport, which shifts the whole page off-center). */
  .canvas-frame {
    width: 100%;
  }

  #sim-canvas {
    width: 100%;
    height: auto;
    aspect-ratio: 1 / 1;
  }
}
