:root {
  --bg: #0b0e12;
  --bg-elev: #12171f;
  --panel: #161c26;
  --ink: #e6edf5;
  --muted: #8b97a8;
  --line: #2a3342;
  --accent: #6ee7ff;
  --accent-dim: #2a6f7c;
  --warm: #ffb454;
  --good: #7dffa3;
  --warn: #ffd166;
  --rail-w: 12.5rem;
  --side-w: 16.5rem;
  --font: "Avenir Next", "Avenir", "Seravek", "Segoe UI", system-ui, -apple-system, sans-serif;
  --focus: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

/* our display rules must never resurrect [hidden] elements
   (the phantom empty canvas below the Backgrounds frame) */
[hidden] {
  display: none !important;
}

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

body {
  background:
    radial-gradient(1000px 500px at 0% -10%, #1a2740 0%, transparent 55%),
    radial-gradient(800px 400px at 100% 0%, #1c1530 0%, transparent 50%),
    var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 13px;
  line-height: 1.4;
}

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0.5rem;
  z-index: 20;
  background: var(--accent);
  color: #041016;
  padding: 0.5rem 0.75rem;
  font-weight: 700;
}
.skip-link:focus {
  left: 0.5rem;
}

.app {
  min-height: 100%;
  display: grid;
  grid-template-columns: var(--rail-w) 1fr var(--side-w);
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "top top top"
    "rail work side"
    "foot foot foot";
}

.topbar {
  grid-area: top;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem 1.25rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, #141a24, #0f131a);
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.65rem;
}
.brand-mark {
  width: 26px;
  height: 26px;
  background:
    linear-gradient(135deg, var(--accent) 0 33%, transparent 33% 66%, var(--warm) 66%),
    linear-gradient(45deg, #3d5afe 0 50%, #ff5c8a 50%);
}
.brand-kicker {
  margin: 0;
  color: var(--muted);
  font-size: 10px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}
.brand h1 {
  margin: 0;
  font-size: 1.1rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.workspace-switcher {
  display: inline-flex;
  align-items: center;
  padding: 3px;
  border: 1px solid var(--line);
  background: var(--bg);
}
.workspace-switcher a,
.workspace-switcher [aria-current="page"] {
  padding: 0.3rem 0.65rem;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
  line-height: 1.2;
  text-decoration: none;
  text-transform: uppercase;
}
.workspace-switcher a:hover {
  color: var(--ink);
}
.workspace-switcher a:focus-visible {
  outline: none;
  box-shadow: var(--focus);
}
.workspace-switcher [aria-current="page"] {
  background: rgba(110, 231, 255, 0.12);
  color: var(--accent);
}

.tagline {
  margin: 0;
  flex: 1 1 12rem;
  color: var(--muted);
  max-width: 28rem;
}

.topbar-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-left: auto;
  align-items: center;
}

.seed-field {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  color: var(--muted);
  font-size: 11px;
}
.seed-field input {
  width: 7.5rem;
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--ink);
  font: inherit;
  padding: 0.35rem 0.45rem;
}

.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  font: inherit;
  padding: 0.4rem 0.65rem;
  cursor: pointer;
}
.btn:hover {
  border-color: var(--accent-dim);
  color: var(--accent);
}
.btn:focus-visible,
input:focus-visible,
select:focus-visible,
.mode-btn:focus-visible {
  outline: none;
  box-shadow: var(--focus);
}
.btn.primary {
  border-color: var(--accent-dim);
  background: rgba(110, 231, 255, 0.12);
  color: var(--accent);
}

.rail {
  grid-area: rail;
  border-right: 1px solid var(--line);
  background: var(--bg-elev);
  padding: 0.6rem 0.4rem;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  overflow: auto;
}

.mode-btn {
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: auto auto;
  column-gap: 0.5rem;
  width: 100%;
  text-align: left;
  border: 1px solid transparent;
  background: transparent;
  color: var(--ink);
  font: inherit;
  padding: 0.5rem;
  cursor: pointer;
}
.mode-btn:hover {
  background: rgba(110, 231, 255, 0.06);
  border-color: var(--line);
}
.mode-btn[aria-current="page"] {
  background: rgba(110, 231, 255, 0.1);
  border-color: var(--accent-dim);
}
.mode-key {
  grid-row: 1 / span 2;
  width: 1.35rem;
  height: 1.35rem;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  font-size: 11px;
  color: var(--muted);
}
.mode-btn[aria-current="page"] .mode-key {
  border-color: var(--accent);
  color: var(--accent);
}
.mode-name {
  font-weight: 700;
  letter-spacing: 0.03em;
}

.workspace {
  grid-area: work;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

.frame-chrome {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.45rem 0.85rem;
  background: #0a0d12;
  border-bottom: 1px solid var(--line);
}
.frame-label {
  color: var(--ink);
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.02em;
}
.status-pill {
  margin-left: auto;
  border: 1px solid var(--good);
  color: var(--good);
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 0.12rem 0.5rem;
  border-radius: 999px;
}
.status-pill.busy {
  border-color: var(--warm);
  color: var(--warm);
  animation: pulse-busy 1s ease-in-out infinite;
}
@keyframes pulse-busy {
  50% {
    opacity: 0.55;
  }
}
@media (prefers-reduced-motion: reduce) {
  .status-pill.busy {
    animation: none;
  }
}

.stage {
  flex: 1;
  min-height: 20rem;
  display: grid;
  /* top-aligned so every mode's canvas shares the same top edge */
  place-items: start center;
  overflow: auto;
  padding: 1.25rem 1rem;
  background:
    repeating-linear-gradient(
      0deg,
      transparent,
      transparent 2px,
      rgba(0, 0, 0, 0.06) 2px,
      rgba(0, 0, 0, 0.06) 3px
    ),
    #080a0e;
}

.canvas-stack {
  position: relative;
  display: grid;
  place-items: center;
}

/* animated FX modes: big looping preview over the exportable filmstrip */
.fx-wrap {
  display: grid;
  gap: 1rem;
  justify-items: center;
}
.fx-preview {
  width: min(420px, 80vw);
  background: #05070b;
}
.fx-strip {
  width: min(560px, 90vw, 100%);
  max-height: 120px;
  object-fit: contain;
  background: #05070b;
}
.canvas-stack .gen-canvas.overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  /* stacked layer: an opaque background here hides the GL canvas beneath */
  background: transparent;
  box-shadow: none;
}

.gen-canvas {
  display: block;
  /* Standard stage footprint: every mode presents at the same width,
     aspect preserved (all defaults are 512×512 so modes match at rest). */
  width: min(560px, 90vw, 100%);
  height: auto;
  max-height: min(70vh, 900px);
  /* one backdrop tone for every mode (sprites bg + planets clear match) */
  background: #0b0f16;
  box-shadow: 0 0 0 1px var(--line), 12px 12px 0 rgba(0, 0, 0, 0.35);
  image-rendering: auto;
  /* if max-height clamps a tall canvas, letterbox instead of distorting */
  object-fit: contain;
}
.gen-canvas.pixelated {
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.mode-iframe {
  width: min(100%, 920px);
  /* standard footprint: 512 canvas + progress strip */
  height: min(78vh, 620px);
  min-height: 520px;
  border: 0;
  background: #111;
  box-shadow: 0 0 0 1px var(--line), 12px 12px 0 rgba(0, 0, 0, 0.35);
}

.starscapes-stack {
  width: 100%;
  justify-items: stretch;
}
.starscapes-stack .mode-iframe {
  width: 100%;
}

.recipe {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 0.85rem;
  padding: 0.4rem 0.75rem;
  border-top: 1px solid var(--line);
  background: #0a0d12;
  color: var(--muted);
  font-size: 11px;
}
.recipe-label {
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-size: 10px;
}
#recipe-text {
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

.sidebar {
  grid-area: side;
  border-left: 1px solid var(--line);
  background: var(--bg-elev);
  padding: 0.75rem;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}
.sidebar-title {
  margin: 0;
  font-size: 11px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
}
.controls {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}
.ctrl-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.35rem 0.5rem;
  align-items: center;
  color: var(--ink);
}
.ctrl-label {
  color: var(--muted);
  font-size: 11px;
}
.ctrl-row input[type="number"],
.ctrl-row input[type="text"],
.ctrl-row select {
  width: 7.5rem;
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--ink);
  font: inherit;
  padding: 0.3rem 0.4rem;
}
.ctrl-row input[type="range"] {
  width: 6.5rem;
}
.ctrl-row input[type="color"] {
  width: 2.4rem;
  height: 1.6rem;
  border: 1px solid var(--line);
  background: transparent;
  padding: 0;
}
.ctrl-row input[type="checkbox"] {
  width: 1rem;
  height: 1rem;
  justify-self: end;
}
.ctrl-val {
  font-size: 10px;
  color: var(--muted);
  min-width: 2rem;
  text-align: right;
}
.sidebar-hint {
  margin-top: auto;
  color: var(--muted);
  font-size: 11px;
}

.history {
  border-top: 1px solid var(--line);
  padding-top: 0.55rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}
.history-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: var(--muted);
  font-size: 10px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}
.history-empty {
  margin: 0;
  color: var(--muted);
  font-size: 11px;
}
.history-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  max-height: 10rem;
  overflow: auto;
}
.history-thumb {
  appearance: none;
  border: 1px solid var(--line);
  background: #0a0d12;
  padding: 0.2rem;
  cursor: pointer;
  color: var(--muted);
  font: inherit;
  font-size: 9px;
  width: 4.2rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
}
.history-thumb img {
  width: 56px;
  height: 56px;
  object-fit: cover;
  image-rendering: pixelated;
  background: #000;
}
.history-thumb:hover {
  border-color: var(--accent-dim);
  color: var(--accent);
}
.btn.tiny {
  padding: 0.15rem 0.35rem;
  font-size: 10px;
}
.btn.ghost {
  background: transparent;
}

.sprite-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
}
.sprite-name {
  margin: 0;
  max-width: min(90vw, 36rem);
  text-align: center;
  color: var(--accent);
  font-size: 12px;
  letter-spacing: 0.04em;
}

.toast {
  position: fixed;
  bottom: 1.25rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--panel);
  border: 1px solid var(--accent-dim);
  color: var(--ink);
  padding: 0.5rem 0.85rem;
  font-size: 12px;
  z-index: 50;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
}
.toast[hidden] {
  display: none !important;
}
.sidebar-hint kbd {
  border: 1px solid var(--line);
  background: var(--panel);
  padding: 0.05rem 0.28rem;
  font: inherit;
  font-size: 10px;
  color: var(--ink);
}

.footer {
  grid-area: foot;
  padding: 0.45rem 1rem;
  border-top: 1px solid var(--line);
  background: #0a0d12;
  color: var(--muted);
  font-size: 11px;
}
.footer p {
  margin: 0;
}

/* Mobile action bar — hidden on desktop */
.actionbar {
  display: none;
}

@media (max-width: 960px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-areas:
      "top"
      "rail"
      "work"
      "side"
      "foot";
  }
  .rail {
    flex-direction: row;
    flex-wrap: wrap;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }
  .mode-btn {
    flex: 1 1 8rem;
  }
  .sidebar {
    border-left: 0;
    border-top: 1px solid var(--line);
  }
}

/* Phones: art first, actions at the thumb, one-line everything */
@media (max-width: 720px) {
  .topbar {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.55rem 0.85rem;
  }
  .tagline,
  .seed-field,
  #btn-generate,
  #btn-random,
  #btn-vary {
    display: none; /* seed lives in Controls; actions live in the bottom bar */
  }
  .brand-kicker {
    display: none;
  }
  .topbar-actions {
    margin-left: auto;
    display: flex;
    gap: 0.4rem;
  }
  .topbar-actions .btn {
    padding: 0.4rem 0.7rem;
    font-size: 12px;
  }

  /* mode rail → one scrollable row of chips */
  .rail {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding: 0.45rem 0.6rem;
    gap: 0.35rem;
  }
  .rail::-webkit-scrollbar {
    display: none;
  }
  .mode-btn {
    flex: 0 0 auto;
    width: auto;
    display: flex;
    align-items: center;
    border: 1px solid var(--line);
    border-radius: 999px;
    padding: 0.4rem 0.95rem;
    white-space: nowrap;
  }
  .mode-key {
    display: none; /* number keys mean nothing on touch */
  }

  .stage {
    min-height: 0;
    padding: 0.6rem 0.6rem 0.9rem;
  }
  .mode-iframe {
    min-height: 200px;
  }

  /* clearance for the fixed bar + iOS home indicator */
  .footer {
    padding-bottom: calc(4.6rem + env(safe-area-inset-bottom));
  }

  .actionbar {
    display: grid;
    grid-template-columns: 2fr 3fr;
    gap: 0.6rem;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 30;
    padding: 0.6rem 0.85rem calc(0.6rem + env(safe-area-inset-bottom));
    background: rgba(10, 13, 18, 0.92);
    backdrop-filter: blur(10px);
    border-top: 1px solid var(--line);
  }
  .actionbar .btn {
    min-height: 46px;
    font-size: 15px;
    border-radius: 10px;
  }
  .actionbar .btn.primary {
    background: var(--accent);
    color: #06222b;
    border-color: var(--accent);
    font-weight: 700;
  }
  body.typing .actionbar {
    display: none; /* keyboard up — give the viewport back */
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
  }
}
