:root {
  --black: #000;
  --white: #fff;
  --gray-245: #f5f5f5;
  --paper: #f4f4f3;
  --red: #ff0000;
  /* Official Processing brand blue — --processing-blue-mid on processing.org.
     Marks the tabs that export Java code for the processing.waves port. */
  --processing-blue: #0564ff;
  --title: 'Oswald', sans-serif;
  --text: 'Inter', Arial, sans-serif;
  --code: 'Consolas', 'Courier New', monospace;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--paper);
  color: var(--black);
  font-family: var(--text);
}

button, input, select, textarea {
  font: inherit;
}

/* -------- nav (canonical p5.waves #navbar) -------- */
#navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.85rem 2.5rem;
  background: var(--white);
  border-bottom: 1px solid var(--black);
}

.nav-logo {
  font-family: var(--title);
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--black);
}
.nav-logo a { color: inherit; text-decoration: none; }
.nav-logo span { border-bottom: 2px solid var(--black); }

.nav-links {
  display: flex;
  list-style: none;
  align-items: center;
  gap: 2rem;
  margin: 0;
  padding: 0;
}
.nav-links a {
  color: #6a6f78;
  text-decoration: none;
  font-family: var(--title);
  font-size: 0.82rem;
  font-weight: 400;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  transition: color 0.15s;
}
.nav-links a:hover { color: var(--black); }
.nav-links a.active {
  color: var(--black);
  border-bottom: 2px solid var(--black);
  padding-bottom: 2px;
}
/* processing.waves link — same blue convention as the .pde output tabs */
.nav-links a.pde-link { color: var(--processing-blue); }
.nav-links a.pde-link:hover {
  color: var(--processing-blue);
  border-bottom: 2px solid var(--processing-blue);
  padding-bottom: 2px;
}

/* hamburger (mobile) */
.nav-hamburger {
  display: none;
  flex-direction: column;
  justify-content: center;
  gap: 5px;
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px;
  z-index: 101;
}
.nav-hamburger span {
  display: block;
  width: 22px;
  height: 2px;
  background: var(--black);
  border-radius: 2px;
  transition: transform 0.2s, opacity 0.2s;
}
#navbar.nav-open .nav-hamburger span:nth-child(1) { transform: translateY(7px) rotate(45deg); }
#navbar.nav-open .nav-hamburger span:nth-child(2) { opacity: 0; }
#navbar.nav-open .nav-hamburger span:nth-child(3) { transform: translateY(-7px) rotate(-45deg); }

@media (max-width: 760px) {
  #navbar { padding: 0.7rem 1.25rem; position: relative; }
  .nav-hamburger { display: flex; }
  .nav-links {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    flex-direction: column;
    align-items: stretch;
    gap: 0;
    background: var(--white);
    border-bottom: 1px solid var(--black);
    padding: 0.5rem 0 1rem;
    z-index: 100;
  }
  #navbar.nav-open .nav-links { display: flex; }
  .nav-links li { padding: 0; list-style: none; }
  .nav-links a { display: block; padding: 0.75rem 1.5rem; font-size: 0.9rem; }
  .nav-links a.active { border-bottom: none; }
}

.page-shell {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr) 280px;
  grid-template-areas: "left stage right";
  gap: 14px;
  min-height: calc(100vh - 42px);
  padding: 14px;
}

.control-panel-left { grid-area: left; }
.stage { grid-area: stage; }
.control-panel-right { grid-area: right; }

.control-panel {
  display: grid;
  align-content: start;
  gap: 12px;
  height: calc(100vh - 28px);
  overflow: auto;
  border: 1px solid var(--black);
  background: var(--white);
  padding: 14px;
}

.panel-title {
  border: 1px solid var(--black);
  padding: 10px 12px;
  text-align: center;
}

.panel-title h1 {
  margin: 0;
  font-family: var(--title);
  font-weight: 600;
  font-size: 1.6rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.subtitle {
  margin: 4px 0 0;
  font-family: var(--code);
  font-size: 0.78rem;
  color: #666;
}

h2 {
  margin: 0;
  font-family: var(--title);
  font-weight: 400;
  font-size: 0.94rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

p { margin: 0; }

.control-group {
  display: grid;
  gap: 9px;
  border: 1px solid var(--black);
  background: var(--gray-245);
  padding: 10px;
}

details.control-group {
  padding: 0;
}

details.control-group > summary {
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  list-style: none;
  padding: 10px;
}

details.control-group > summary::-webkit-details-marker {
  display: none;
}

details.control-group > summary::after {
  content: "+";
  font-family: var(--code);
  font-weight: 700;
}

details[open].control-group > summary::after {
  content: "−";
}

details.control-group > summary:hover {
  background: var(--black);
  color: var(--white);
}

details.control-group > :not(summary) {
  margin: 0 10px 9px;
}

details.control-group > :last-child:not(summary) {
  margin-bottom: 10px;
}

.scene-tabs {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 4px;
}

.scene-tabs .tab-btn {
  background: var(--white);
  color: var(--black);
  border: 1px solid var(--black);
  padding: 7px 6px;
  font-size: 0.78rem;
  font-family: var(--code);
  cursor: pointer;
}

.scene-tabs .tab-btn:hover {
  background: var(--gray-245);
}

.scene-tabs .tab-btn.active {
  background: var(--black);
  color: var(--white);
}

label {
  display: grid;
  gap: 5px;
  font-size: 0.86rem;
}

label.inline {
  display: flex;
  align-items: center;
  gap: 8px;
}

.label-text {
  font-size: 0.86rem;
}

.label-text.accent-text {
  color: var(--red);
}

select, button {
  width: 100%;
  border: 1px solid var(--black);
  border-radius: 0;
  background: var(--white);
  color: var(--black);
  padding: 7px 9px;
}

select {
  appearance: none;
  font-family: var(--text);
}

input[type="range"],
input[type="checkbox"] {
  accent-color: #888;
}

input[type="range"] {
  width: 100%;
}

.value {
  font-family: var(--code);
  font-size: 0.78rem;
}

/* Slider rows: name on the left, its current value on the right of the same
   line, with the range track on its own full-width row below them. */
label:has(> input[type="range"]) {
  grid-template-columns: 1fr auto;
  column-gap: 8px;
  align-items: baseline;
}

label:has(> input[type="range"]) > .value {
  grid-row: 1;
  grid-column: 2;
  justify-self: end;
}

label:has(> input[type="range"]) > input[type="range"] {
  grid-row: 2;
  grid-column: 1 / -1;
}

button {
  cursor: pointer;
  background: var(--black);
  color: var(--white);
  font-family: var(--title);
  font-weight: 400;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  transition: background 120ms ease, color 120ms ease;
}

button:hover {
  background: var(--white);
  color: var(--black);
}

/* Scene visibility is toggled by JS via .scene-hidden — see applySceneVisibility() */
.scene-hidden {
  display: none !important;
}

/* Conditional sub-controls: collapse when their gating checkbox is unchecked.
   Uses :has() so no JS class-toggling is needed. The host section opts in
   with .conditional-host so we don't accidentally hide unrelated labels. */

/* Shift section: Hold/Morph appear only when shift is on; Mix only when off. */
.conditional-host:has(#shift-enable:not(:checked)) .show-when-shift,
.conditional-host:has(#shift-enable:checked) .show-when-no-shift {
  display: none !important;
}

/* Wave Source is moot while Shift auto-cycles the wave, and #shift-enable
   lives in a sibling section — so scope this from the panel, not the host. */
.control-panel-left:has(#shift-enable:checked) .hide-when-shift {
  display: none !important;
}

/* Range section: Preset / Min / Max appear only when "Map output to range" is on. */
.conditional-host:has(#range-enable:not(:checked)) .show-when-range {
  display: none !important;
}

/* Render section: Point Color appears only when "Show points" is on. */
.conditional-host:has(#show-points:not(:checked)) .show-when-points {
  display: none !important;
}

/* Stage */
.stage {
  display: grid;
  /* Canvas row hugs the fixed 600px canvas; the code panel takes all
     leftover viewport height. With auto auto, align-content stretched
     both rows equally and the canvas (fixed height) left a visible gap. */
  grid-template-rows: auto minmax(220px, 1fr);
  gap: 10px;
  min-width: 0;
  height: calc(100vh - 28px);
}

/* Surprise Me / About live in the left panel between Scene and Shift/Mix,
   laid out as two equal buttons like the scene tabs above them. */
.scene-actions {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 4px;
}

#canvas-wrap {
  width: 600px;
  height: 600px;
  max-width: 100%;
  aspect-ratio: 1 / 1;
  border: 1px solid var(--black);
  background: var(--white);
  overflow: hidden;
  justify-self: start;
}

#canvas-wrap canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.code-section {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 10px;
  min-height: 220px;
}

.code-section.single {
  grid-template-columns: minmax(0, 1fr);
  width: 600px;
  max-width: 100%;
}

.badge-button {
  width: 100%;
  padding: 6px 14px;
  font-size: 0.78rem;
  font-family: var(--code);
  background: var(--white);
  color: var(--black);
  border: 1px solid var(--black);
  cursor: pointer;
  text-align: center;
}

.badge-button:hover {
  background: var(--black);
  color: var(--white);
}

/* About modal */
.modal[hidden] {
  display: none;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
}

.modal-card {
  position: relative;
  width: min(640px, calc(100vw - 32px));
  max-height: calc(100vh - 64px);
  background: var(--white);
  border: 1px solid var(--black);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--black);
  background: var(--gray-245);
}

.modal-header h2 {
  margin: 0;
  font-family: var(--title);
  font-weight: 500;
  font-size: 1rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.modal-close {
  width: 32px;
  height: 32px;
  padding: 0;
  font-size: 1.2rem;
  background: var(--white);
  color: var(--black);
  border: 1px solid var(--black);
  cursor: pointer;
}

.modal-close:hover {
  background: var(--black);
  color: var(--white);
}

.modal-body {
  padding: 16px 18px;
  overflow: auto;
  font-size: 0.9rem;
  line-height: 1.5;
}

.modal-body h3 {
  margin: 18px 0 4px;
  font-family: var(--title);
  font-weight: 500;
  font-size: 0.95rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.modal-body h3:first-child {
  margin-top: 0;
}

.modal-body p {
  margin: 0 0 8px;
}

.modal-body code {
  font-family: var(--code);
  font-size: 0.84rem;
  background: var(--gray-245);
  padding: 1px 5px;
}

.editor-section {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-width: 0;
  border: 1px solid var(--black);
  background: var(--white);
}

.editor-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--black);
  background: var(--gray-245);
}

.output-tabs {
  display: flex;
  gap: 0;
}

.output-tab {
  width: auto;
  padding: 5px 14px;
  font-size: 0.78rem;
  font-family: var(--code);
  background: var(--white);
  color: var(--black);
  border: 1px solid var(--black);
  border-right: none;
  cursor: pointer;
  text-transform: none;
  letter-spacing: 0;
  font-weight: 400;
}

.output-tab:last-child {
  border-right: 1px solid var(--black);
}

.output-tab.active {
  background: var(--black);
  color: var(--white);
}

.output-tab:hover:not(.active) {
  background: var(--gray-245);
}

/* Processing export tabs — Processing Foundation blue keeps the two
   ecosystems apart at a glance: black = p5.js, blue = Processing (Java). */
.output-tab.pde {
  color: var(--processing-blue);
}

.output-tab.pde.active {
  background: var(--processing-blue);
  border-color: var(--processing-blue);
  color: var(--white);
}

.output-tab.pde:hover:not(.active) {
  background: #eef3ff;
}

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

.editor-actions button {
  width: auto;
  padding: 5px 14px;
  font-size: 0.78rem;
  background: var(--white);
  color: var(--black);
}

.editor-actions button:hover {
  background: var(--black);
  color: var(--white);
}

.record-group {
  display: inline-flex;
  align-items: stretch;
  gap: 4px;
}

.record-group select,
.record-group input[type="number"] {
  width: auto;
  font-family: var(--code);
  font-size: 0.78rem;
  padding: 4px 6px;
  border: 1px solid var(--black);
  background: var(--white);
  color: var(--black);
}

.record-group select {
  min-width: 64px;
  padding-right: 8px;
}

.record-group input[type="number"] {
  width: 44px;
  text-align: right;
  -moz-appearance: textfield;
}

.record-group input[type="number"]::-webkit-outer-spin-button,
.record-group input[type="number"]::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.record-unit {
  align-self: center;
  font-family: var(--code);
  font-size: 0.72rem;
  color: var(--black);
  opacity: 0.55;
  margin-left: -2px;
}

.record-status {
  margin: 6px 0 0;
  font-family: var(--code);
  font-size: 0.72rem;
  line-height: 1.4;
  color: var(--red);
}

.record-status[data-tone="info"] {
  color: var(--black);
  opacity: 0.6;
}

#record-start.recording {
  background: var(--red);
  color: var(--white);
  border-color: var(--red);
}

#record-start.encoding {
  background: var(--gray-245);
  color: var(--black);
  cursor: progress;
}

.code-output {
  display: block;
  width: 100%;
  height: 100%;
  min-height: 190px;
  border: 0;
  resize: none;
  padding: 10px;
  color: var(--black);
  background: var(--white);
  font-family: var(--code);
  font-size: 0.78rem;
  line-height: 1.45;
}

.code-output:focus {
  outline: 2px solid var(--black);
  outline-offset: -2px;
}

.code-output.user-edited {
  border-top: 2px solid var(--red);
}

/* Below 1280px we stack: stage first, then both panels. Keeps the canvas
   prominent and avoids awkward narrow-strip layouts. */
@media (max-width: 1280px) {
  /* Dissolve the three column containers so their children reflow into one
     mobile column, then order them into a natural flow:
     title -> scene -> canvas -> remaining left controls -> right controls
     -> code/export. Desktop layout above is untouched. */
  .page-shell {
    display: flex;
    flex-direction: column;
    gap: 14px;
  }
  .control-panel-left,
  .control-panel-right,
  .stage {
    display: contents;
  }
  .control-panel-left > * { order: 4; }
  .control-panel-left > .panel-title { order: 1; }
  .control-panel-left > .scene-picker { order: 2; }
  .stage > #canvas-wrap { order: 3; }
  .control-panel-right > * { order: 5; }
  .stage > .code-section { order: 6; }

  .control-panel, .stage {
    height: auto;
  }
  .code-section {
    grid-template-columns: 1fr;
  }
  .code-output {
    resize: vertical;
  }

  /* Keep the canvas square when it shrinks below 600px. The base rule sets
     both width and height to 600px, which makes the browser ignore
     aspect-ratio; once max-width shrinks the width, the fixed 600px height
     leaves the canvas taller than wide and scenes get centred with white
     bands. Let aspect-ratio drive the height instead. */
  #canvas-wrap {
    height: auto;
  }
}
