:root {
  --bg-glass: rgba(6, 10, 8, 0.68);
  --bg-glass-strong: rgba(5, 8, 6, 0.8);
  --line: rgba(112, 255, 173, 0.22);
  --line-strong: rgba(112, 255, 173, 0.38);
  --text: rgba(226, 255, 231, 0.95);
  --text-muted: rgba(202, 235, 207, 0.72);
  --accent: rgb(102, 255, 150);
  --warn: rgb(255, 196, 88);
  --err: rgb(255, 120, 120);
  --shadow: 0 8px 28px rgba(0, 0, 0, 0.28);
  --radius: 14px;
  --gap: 14px;
  --ui-dim: 0;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
  background: #000;
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
}

body {
  overflow: hidden;
}

canvas {
  display: block;
  width: 100vw;
  height: 100vh;
}

#ui {
  position: fixed;
  inset: 0;
  pointer-events: none;
  padding: 14px;
}

#ui::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: radial-gradient(circle at 20% 14%, rgba(64, 255, 142, 0.07), transparent 60%),
    radial-gradient(circle at 84% 86%, rgba(80, 199, 255, 0.06), transparent 52%),
    rgba(0, 0, 0, var(--ui-dim));
  transition: background-color 160ms ease;
}

#ui.dim-on {
  --ui-dim: 0.42;
}

.panel {
  pointer-events: auto;
  background: linear-gradient(180deg, rgba(9, 14, 11, 0.78), rgba(4, 7, 5, 0.7));
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  backdrop-filter: blur(6px);
}

#topbar {
  position: relative;
  z-index: 3;
  padding: 10px;
}

.toolbar-grid {
  display: grid;
  grid-template-columns: minmax(220px, 1.4fr) repeat(10, minmax(0, 1fr));
  gap: 8px;
  align-items: end;
}

.field {
  min-width: 0;
}

.sample-field {
  display: grid;
  grid-template-rows: auto 40px;
}

.label {
  color: var(--text);
  opacity: 0.9;
  font-size: 12px;
  letter-spacing: 0.03em;
  margin-bottom: 4px;
}

.subtle {
  color: var(--text-muted);
  font-size: 11px;
}

button,
select,
textarea,
input {
  color: var(--text);
  background: rgba(4, 8, 6, 0.72);
  border: 1px solid var(--line);
  border-radius: 10px;
}

button,
select,
input {
  min-height: 40px;
  padding: 0 12px;
  font-size: 13px;
}

button {
  cursor: pointer;
}

button:hover,
select:hover,
input:hover,
textarea:hover {
  border-color: var(--line-strong);
}

button[aria-pressed="true"] {
  border-color: rgba(112, 255, 173, 0.5);
  box-shadow: inset 0 0 0 1px rgba(112, 255, 173, 0.15);
}

button:focus-visible,
select:focus-visible,
textarea:focus-visible,
input:focus-visible,
.chip:focus-visible,
.palette-btn:focus-visible {
  outline: 2px solid rgba(112, 255, 173, 0.5);
  outline-offset: 1px;
}

#status {
  margin-top: 8px;
  color: var(--text-muted);
  font-size: 12px;
  min-height: 18px;
}

#errorBanner {
  margin-top: 8px;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid rgba(255, 120, 120, 0.3);
  background: rgba(90, 10, 10, 0.32);
  color: rgba(255, 214, 214, 0.95);
  font-size: 12px;
}

#workspace {
  position: relative;
  z-index: 2;
  margin-top: var(--gap);
  display: grid;
  grid-template-columns: minmax(340px, 1.2fr) minmax(320px, 0.95fr);
  gap: var(--gap);
  height: calc(100vh - 14px - 14px - 90px - var(--gap));
}

#editorPanel,
#sidePanel {
  min-height: 0;
}

#editorPanel {
  padding: 12px;
  display: grid;
  grid-template-rows: auto auto minmax(180px, 1fr) auto;
  gap: 10px;
  min-width: 0;
}

.section-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px 8px;
  font-size: 11px;
  color: var(--text-muted);
  background: rgba(4, 7, 6, 0.55);
}

.chip.active {
  border-color: rgba(112, 255, 173, 0.45);
  color: var(--text);
}

.inline-panel {
  border: 1px solid rgba(112, 255, 173, 0.15);
  border-radius: 10px;
  background: rgba(4, 7, 5, 0.36);
  padding: 8px;
}

.inline-panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

.inline-panel-head button {
  min-height: 26px;
  padding: 0 8px;
  font-size: 11px;
}

#experimentPromptList {
  display: grid;
  gap: 6px;
}

.experiment-prompt {
  border: 1px solid rgba(112, 255, 173, 0.12);
  border-radius: 10px;
  background: rgba(3, 6, 4, 0.35);
  padding: 8px;
}

.experiment-prompt-title {
  font-size: 12px;
  color: var(--text);
  margin-bottom: 4px;
}

.experiment-prompt-body {
  font-size: 11px;
  color: var(--text-muted);
}

#editor {
  width: 100%;
  min-height: 0;
  resize: none;
  padding: 12px;
  line-height: 1.4;
  font-size: 17px;
  background: rgba(2, 4, 3, 0.78);
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
}

#stateDetails {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(5, 8, 6, 0.55);
  padding: 8px;
}

#stateDetails > summary {
  cursor: pointer;
  color: var(--text-muted);
  font-size: 12px;
}

#effectiveStateText {
  margin: 8px 0 0;
  font-size: 12px;
  color: var(--text);
  white-space: pre-wrap;
}

#sidePanel {
  display: block;
  min-width: 0;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-gutter: stable;
  padding-right: 2px;
}

#sidePanel > section {
  margin-bottom: var(--gap);
}

#sidePanel > section:last-child {
  margin-bottom: 0;
}

.widget-toggle {
  min-height: 28px;
  padding: 0 8px;
  font-size: 11px;
  white-space: nowrap;
}

#palettePanel,
#macroPanel,
#help {
  padding: 12px;
  min-height: 0;
  overflow: visible;
  display: block;
}

#palettePanel > * + *,
#macroPanel > * + *,
#help > * + * {
  margin-top: 10px;
}

#paletteGroups,
#macroList,
#helpBody {
  min-height: 0;
  overflow: visible;
  max-height: none;
}

#palettePanel.is-compact #paletteGroups {
  display: none;
}

#macroPanel.is-compact .macro-controls,
#macroPanel.is-compact #macroList {
  display: none;
}

#help.is-collapsed #helpTabs,
#help.is-collapsed #helpSearch,
#help.is-collapsed #helpFilters,
#help.is-collapsed #helpBody {
  display: none;
}

#sidePanel.help-focused #palettePanel.is-compact,
#sidePanel.help-focused #macroPanel.is-compact {
  opacity: 0.95;
}

.palette-group {
  margin-bottom: 10px;
}

.palette-group:last-child {
  margin-bottom: 0;
}

.palette-group-title {
  color: var(--text-muted);
  font-size: 11px;
  margin-bottom: 6px;
}

.palette-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(44px, 1fr));
  gap: 6px;
}

.palette-btn {
  min-height: 38px;
  padding: 4px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: rgba(5, 8, 6, 0.72);
  color: var(--text);
  cursor: pointer;
  display: grid;
  place-items: center;
  font-size: 18px;
  line-height: 1;
}

.palette-btn.small {
  font-size: 13px;
}

.palette-btn:hover {
  border-color: var(--line-strong);
}

.macro-controls {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  margin-top: -2px;
}

#macroNameInput {
  min-width: 0;
}

.macro-item {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px;
  margin-bottom: 8px;
  background: rgba(5, 8, 6, 0.45);
}

.macro-item:last-child {
  margin-bottom: 0;
}

.macro-head {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: center;
  margin-bottom: 6px;
}

.macro-name {
  font-size: 12px;
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
}

.macro-actions {
  display: flex;
  gap: 6px;
}

.macro-actions button {
  min-height: 28px;
  padding: 0 8px;
  font-size: 11px;
}

.macro-code {
  white-space: pre-wrap;
  color: var(--text-muted);
  font-size: 11px;
  max-height: 64px;
  overflow: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
}

#help {
  min-height: 240px;
}

#helpSearch {
  width: 100%;
}

.help-section {
  margin-bottom: 12px;
  border: 1px solid rgba(112, 255, 173, 0.12);
  border-radius: 10px;
  padding: 10px;
  background: rgba(4, 6, 5, 0.35);
}

.help-section:last-child {
  margin-bottom: 0;
}

.help-section h3 {
  margin: 0 0 8px;
  font-size: 13px;
  color: var(--text);
}

.help-cta-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.help-cta-row button {
  min-height: 30px;
  font-size: 12px;
}

.help-section p,
.help-section li,
.help-section td,
.help-section th {
  font-size: 12px;
  color: var(--text-muted);
}

.help-section table {
  width: 100%;
  border-collapse: collapse;
}

.help-section th,
.help-section td {
  border-bottom: 1px solid rgba(112, 255, 173, 0.12);
  padding: 4px 6px;
  text-align: left;
  vertical-align: top;
}

.help-section td.glyph-cell {
  font-size: 16px;
  color: var(--text);
}

.help-item {
  border: 1px solid rgba(112, 255, 173, 0.12);
  border-radius: 10px;
  padding: 8px;
  margin-bottom: 8px;
  background: rgba(4, 6, 5, 0.35);
}

.help-item:last-child {
  margin-bottom: 0;
}

.help-item-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.help-item .glyph {
  font-size: 20px;
  line-height: 1;
  color: var(--text);
}

.help-item .name {
  font-size: 12px;
  color: var(--text);
}

.help-item .meta {
  font-size: 11px;
  color: var(--text-muted);
}

.help-item .desc {
  margin-top: 6px;
  font-size: 12px;
  color: var(--text-muted);
}

.help-example {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
  border: 1px solid rgba(112, 255, 173, 0.12);
  border-radius: 10px;
  padding: 8px;
  margin-bottom: 8px;
  background: rgba(4, 6, 5, 0.35);
}

.help-example:last-child {
  margin-bottom: 0;
}

.help-example-code {
  white-space: pre-wrap;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  font-size: 11px;
  color: var(--text-muted);
}

.tutorial-track {
  border: 1px solid rgba(112, 255, 173, 0.12);
  border-radius: 10px;
  padding: 10px;
  margin-bottom: 10px;
  background: rgba(4, 6, 5, 0.35);
}

.tutorial-track:last-child {
  margin-bottom: 0;
}

.tutorial-track-head {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: flex-start;
}

.tutorial-track-title {
  color: var(--text);
  font-size: 13px;
}

.tutorial-track-meta {
  color: var(--text-muted);
  font-size: 11px;
}

.tutorial-track-actions {
  display: flex;
  gap: 6px;
  margin-top: 8px;
}

.tutorial-track-actions button {
  min-height: 30px;
  font-size: 11px;
}

.badge-ok {
  color: rgb(135, 255, 174);
}

.badge-warn {
  color: rgb(255, 210, 115);
}

.badge-err {
  color: rgb(255, 154, 154);
}

#stageRestoreBar {
  position: fixed;
  left: 50%;
  top: 10px;
  transform: translateX(-50%);
  z-index: 30;
  width: min(860px, calc(100vw - 20px));
  pointer-events: auto;
}

.stage-restore-inner {
  display: grid;
  grid-template-columns: auto 1fr auto auto auto;
  gap: 8px;
  align-items: center;
  padding: 8px;
}

.stage-label {
  color: var(--text);
  font-size: 12px;
  letter-spacing: 0.04em;
}

#stageModeFlags {
  min-width: 0;
}

#stageModeFlags .chip {
  white-space: nowrap;
}

/* Stage mode fold/unfold choreography */
#ui.ui-mode-stage-enter #editorPanel,
#ui.ui-mode-stage #editorPanel,
#ui.ui-mode-stage-exit #editorPanel {
  transform-origin: top center;
  transition: transform 420ms cubic-bezier(0.2, 0.8, 0.2, 1), opacity 220ms ease, clip-path 420ms cubic-bezier(0.2, 0.8, 0.2, 1);
}

#ui.ui-mode-stage-enter #sidePanel,
#ui.ui-mode-stage #sidePanel,
#ui.ui-mode-stage-exit #sidePanel {
  transform-origin: top center;
  transition: transform 420ms cubic-bezier(0.2, 0.8, 0.2, 1) 70ms, opacity 220ms ease 70ms, clip-path 420ms cubic-bezier(0.2, 0.8, 0.2, 1) 70ms;
}

#ui.ui-mode-stage-enter #topbar,
#ui.ui-mode-stage #topbar,
#ui.ui-mode-stage-exit #topbar {
  transition: transform 300ms cubic-bezier(0.2, 0.8, 0.2, 1) 120ms, opacity 180ms ease 120ms, max-height 300ms ease 120ms;
  overflow: hidden;
}

#ui.ui-mode-stage-enter #editorPanel,
#ui.ui-mode-stage #editorPanel {
  opacity: 0;
  transform: perspective(800px) rotateX(22deg) scaleY(0.18);
  clip-path: inset(0 0 98% 0 round 14px);
  pointer-events: none;
}

#ui.ui-mode-stage-enter #sidePanel,
#ui.ui-mode-stage #sidePanel {
  opacity: 0;
  transform: perspective(800px) rotateX(16deg) scaleY(0.18);
  clip-path: inset(0 0 98% 0 round 14px);
  pointer-events: none;
}

#ui.ui-mode-stage-enter #topbar,
#ui.ui-mode-stage #topbar {
  transform: translateY(-8px) scaleY(0.92);
  opacity: 0;
  max-height: 0;
  pointer-events: none;
}

#ui.ui-mode-stage #workspace {
  opacity: 0.02;
}

#ui.ui-mode-stage::before {
  background: radial-gradient(circle at 20% 14%, rgba(64, 255, 142, 0.04), transparent 60%),
    radial-gradient(circle at 84% 86%, rgba(80, 199, 255, 0.04), transparent 52%),
    rgba(0, 0, 0, 0.02);
}

#tutorialOverlay,
#welcomeOverlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  pointer-events: auto;
}

#tutorialBackdrop,
.welcome-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.42);
  backdrop-filter: blur(2px);
}

#tutorialHighlight {
  position: absolute;
  border-radius: 12px;
  border: 1px solid rgba(112, 255, 173, 0.45);
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.35), 0 0 22px rgba(112, 255, 173, 0.18);
  pointer-events: none;
  transition: top 180ms ease, left 180ms ease, width 180ms ease, height 180ms ease, opacity 150ms ease;
}

#tutorialCard {
  position: absolute;
  right: 14px;
  bottom: 14px;
  width: min(460px, calc(100vw - 28px));
  padding: 12px;
  display: grid;
  gap: 8px;
}

.tutorial-progress-row {
  display: flex;
  justify-content: space-between;
  gap: 10px;
}

#tutorialTitle {
  margin: 0;
  font-size: 18px;
  color: var(--text);
}

#tutorialBody {
  color: var(--text-muted);
  font-size: 13px;
  line-height: 1.45;
}

#tutorialBody p {
  margin: 0 0 8px;
}

#tutorialBody p:last-child {
  margin-bottom: 0;
}

.tutorial-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.tutorial-actions button {
  min-height: 32px;
  font-size: 12px;
}

#tutorialNextBtn {
  border-color: rgba(112, 255, 173, 0.42);
}

#welcomeCard {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: min(560px, calc(100vw - 24px));
  padding: 16px;
}

#welcomeCard h2 {
  margin: 4px 0 8px;
  color: var(--text);
  font-size: 24px;
}

#welcomeCard p {
  margin: 0;
  color: var(--text-muted);
  font-size: 14px;
  line-height: 1.45;
}

#welcomeActions {
  margin-top: 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

#welcomeActions button {
  min-height: 34px;
}

@media (max-width: 1200px) {
  .toolbar-grid {
    grid-template-columns: minmax(220px, 1fr) repeat(5, minmax(0, 1fr));
  }

  .toolbar-grid > *:nth-child(n + 7) {
    grid-column: span 1;
  }

  #workspace {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(260px, 1fr) minmax(340px, 1fr);
    height: calc(100vh - 14px - 14px - 128px - var(--gap));
  }

  #sidePanel {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: minmax(180px, 1fr) minmax(200px, 1fr);
    overflow: hidden;
    padding-right: 0;
  }

  #palettePanel {
    grid-column: 1;
    grid-row: 1;
  }

  #macroPanel {
    grid-column: 2;
    grid-row: 1;
  }

  #help {
    grid-column: 1 / span 2;
    grid-row: 2;
    min-height: 0;
  }

  #palettePanel,
  #macroPanel,
  #help {
    overflow: hidden;
    display: grid;
    grid-template-rows: auto 1fr;
    gap: 10px;
  }

  #palettePanel > * + *,
  #macroPanel > * + *,
  #help > * + * {
    margin-top: 0;
  }

  #help {
    grid-template-rows: auto auto auto auto 1fr;
  }

  #paletteGroups,
  #macroList,
  #helpBody {
    overflow: auto;
  }

  #tutorialCard {
    width: min(520px, calc(100vw - 20px));
  }
}

@media (max-width: 760px) {
  #ui {
    padding: 10px;
  }

  .toolbar-grid {
    grid-template-columns: 1fr 1fr;
  }

  .sample-field {
    grid-column: 1 / span 2;
  }

  #topbar {
    padding: 8px;
  }

  #workspace {
    margin-top: 10px;
    gap: 10px;
    height: calc(100vh - 10px - 10px - 206px - 10px);
    grid-template-rows: minmax(220px, 1fr) minmax(300px, 1fr);
  }

  #editor {
    font-size: 16px;
  }

  #sidePanel {
    overflow: hidden;
    grid-template-columns: 1fr;
    grid-template-rows: minmax(160px, auto) minmax(130px, auto) minmax(180px, 1fr);
  }

  #palettePanel,
  #macroPanel,
  #help {
    grid-column: auto;
    grid-row: auto;
  }

  .stage-restore-inner {
    grid-template-columns: 1fr 1fr;
  }

  .stage-label {
    grid-column: 1 / span 2;
  }

  #stageModeFlags {
    grid-column: 1 / span 2;
  }

  #tutorialCard {
    left: 10px;
    right: 10px;
    bottom: 10px;
    width: auto;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    scroll-behavior: auto;
  }

  #ui.ui-mode-stage-enter #editorPanel,
  #ui.ui-mode-stage #editorPanel,
  #ui.ui-mode-stage-exit #editorPanel,
  #ui.ui-mode-stage-enter #sidePanel,
  #ui.ui-mode-stage #sidePanel,
  #ui.ui-mode-stage-exit #sidePanel,
  #ui.ui-mode-stage-enter #topbar,
  #ui.ui-mode-stage #topbar,
  #ui.ui-mode-stage-exit #topbar,
  #tutorialHighlight {
    transition: none;
  }

  #ui.ui-mode-stage-enter #editorPanel,
  #ui.ui-mode-stage #editorPanel,
  #ui.ui-mode-stage-enter #sidePanel,
  #ui.ui-mode-stage #sidePanel {
    transform: none;
    clip-path: inset(0 0 100% 0 round 14px);
  }
}
