/* Hello World app styles. Every colour and size is a variable defined once here. */

:root {
  /* colours (dark: the panels are the point, the interface stays out of the way) */
  --background: #0e0e0e; /* page */
  --surface: #181818; /* sidebar and control backgrounds */
  --surface-raised: #232323; /* inputs and buttons */
  --border: #333333; /* dividers and outlines */
  --text: #ececec; /* main text */
  --text-muted: #a0a0a0; /* labels, notes */
  --accent: #4ea1ff; /* focus, armed MIDI button, slider thumbs */
  --warning: #ffb44e; /* alerts */
  --error-color: #ff4d4d; /* the Error plot line (red, as in chapter 4) */
  --knowledge-color: #36d6e7; /* the Knowledge gain plot line (cyan, as in chapter 4) */
  --panel-background: #000000; /* behind the four images */

  /* sizes */
  --sidebar-width: 26rem; /* settings column */
  --control-width: 12rem; /* cap on sliders, selects and text inputs (never full width) */
  --number-width: 5rem; /* number boxes beside sliders: room for the longest value shown, such as 0.000001 */
  --layer-number-width: 4.5rem; /* filters boxes in the layers table */
  --label-width: 9rem; /* setting names column: the longest name beside a slider fits on one line */
  --input-min-width: 6rem; /* the least a slider, list or text box shrinks to before the name gives up width */
  --plot-height: 7rem; /* each plot's height */
  --panel-columns: 4; /* Stimulus, Seeing, Reminiscing and Dreaming side by side */
  --gap: 0.75rem; /* general spacing */
  --gap-small: 0.375rem; /* tight spacing */
  --radius: 4px; /* corner rounding */
  --font: 14px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif; /* interface text */
  --font-small: 12px; /* notes and captions */
  --font-section-title: 0.95rem; /* settings section titles */
  --disabled-opacity: 0.4; /* how faint disabled buttons and switched-off controls look */
  --collapse-duration: 200ms; /* how long a settings section takes to slide open or closed */
  --narrow-panel-columns: 2; /* panels per row on a narrow screen: a 2 x 2 grid */
  --touch-target: 2.75rem; /* least height of a control on a touch screen (44 px, the usual finger-sized target) */
  --touch-checkbox: 1.25rem; /* checkbox size on a touch screen */
  --touch-input-font: 16px; /* text size in inputs on a touch screen: iOS Safari zooms into any input with smaller text */
  --quick-button-padding: 0.4rem; /* space either side of the Q */
  interpolate-size: allow-keywords; /* lets a height animate to and from "auto", for the sliding sections */
}

* {
  box-sizing: border-box;
}

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

body {
  background: var(--background);
  color: var(--text);
  font: var(--font);
  display: flex;
  flex-direction: column;
}

button,
input,
select {
  font: inherit;
  color: var(--text);
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

button {
  padding: 0.25rem 0.6rem;
  cursor: pointer;
}

button:disabled,
input:disabled {
  opacity: var(--disabled-opacity);
  cursor: default;
}

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

/* ---- top bar */

.topbar {
  display: flex;
  align-items: center;
  gap: var(--gap);
  padding: var(--gap-small) var(--gap);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.topbar h1 {
  font-size: 1rem;
  margin: 0;
}

/* The status line changes every update, so it stays on one line: in a narrow window its end is
   cut off (with "…") instead of wrapping, which would make the top bar taller and push the
   panels and settings down. Its full text shows as a tooltip (main.js). */
.status {
  margin: 0;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.actions {
  display: flex;
  gap: var(--gap-small);
  flex-wrap: wrap;
}

/* Errors and notices: a bar along the bottom of the window. It always keeps at least one line
   of height, so a message appearing or disappearing never moves the panels or settings; a long
   message grows the bar upwards from the bottom edge. */
.alerts {
  margin: 0;
  padding: var(--gap-small) var(--gap);
  box-sizing: content-box; /* so min-height counts only the text, exactly as one line of it measures */
  min-height: 1lh; /* one line of text */
  color: var(--warning);
  border-top: 1px solid var(--border);
}

/* ---- layout */

.layout {
  flex: 1;
  display: flex;
  min-height: 0;
}

.stage {
  flex: 1;
  min-width: 0;
  padding: var(--gap);
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  overflow: auto;
  background: var(--background);
}

.panels {
  display: grid;
  grid-template-columns: repeat(var(--panel-columns), minmax(0, 1fr));
  gap: var(--gap);
}

.panels figure {
  margin: 0;
}

.panels canvas {
  width: 100%;
  aspect-ratio: 1;
  display: block;
  background: var(--panel-background);
}

.panels figcaption {
  margin-top: var(--gap-small);
  color: var(--text-muted);
  text-align: center;
}

.plots {
  display: grid;
  gap: var(--gap-small);
}

.plots canvas {
  width: 100%;
  height: var(--plot-height);
  background: var(--surface);
  border-radius: var(--radius);
}

.stage:fullscreen {
  justify-content: center;
}

.fatal {
  max-width: 40rem;
  margin: auto;
  color: var(--warning);
  font-size: 1.1rem;
}

/* ---- settings sidebar */

.sidebar {
  width: var(--sidebar-width);
  flex-shrink: 0;
  overflow-y: auto;
  background: var(--surface);
  border-left: 1px solid var(--border);
  padding: var(--gap);
}

.midi-status {
  margin: var(--gap) 0 0;
  color: var(--text-muted);
  font-size: var(--font-small);
}

.control-group {
  margin-bottom: var(--gap);
  padding-bottom: var(--gap);
  border-bottom: 1px solid var(--border);
}

/* a section's title: click it to collapse or open the section */
.control-group > summary {
  font-size: var(--font-section-title);
  font-weight: bold;
  cursor: pointer;
}

.control-group[open] > summary {
  margin-bottom: var(--gap-small);
}

/* A section's contents slide open and closed: their height animates between zero and their
   natural height. Keeping content-visibility in the transition keeps the contents visible
   while the section closes, instead of vanishing at the start. */
.control-group::details-content {
  block-size: 0;
  overflow: hidden;
  transition:
    block-size var(--collapse-duration) ease,
    content-visibility var(--collapse-duration) allow-discrete;
}

.control-group[open]::details-content {
  block-size: auto;
}

/* people who ask their system for less motion get sections that open and close at once */
@media (prefers-reduced-motion: reduce) {
  .control-group::details-content {
    transition: none;
  }
}

.group-note {
  margin: 0 0 var(--gap-small);
  color: var(--text-muted);
  font-size: var(--font-small);
}

/* Every setting is one row: its name, its inputs, then the Q button at the right end. */
.control-row {
  display: flex;
  align-items: center;
  gap: var(--gap-small);
  margin-bottom: var(--gap-small);
}

/* the Q button: a toggle, filled with the accent colour while its setting is in Quick Controls */
.quick-button {
  margin-left: auto;
  padding-inline: var(--quick-button-padding);
  font-size: var(--font-small);
  color: var(--text-muted);
}

.quick-button[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--background);
}

/* in Quick Controls, the name of the section each setting comes from: the same size as the
   setting names, in the full text colour */
.quick-group-title {
  margin: var(--gap-small) 0;
  color: var(--text);
}

.control-row[hidden] {
  display: none;
}

/* A setting's name takes --label-width, and gives up some of it (wrapping onto a second line)
   only when a narrow screen leaves the row too little room. */
.control-label {
  flex: 0 1 var(--label-width);
  min-width: 0;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: var(--gap-small);
}

.kind-toggle .control-label {
  flex-basis: auto;
}

/* Sliders, lists and text boxes take the room left in their row, up to --control-width. On a
   narrow screen they keep at least --input-min-width, and the setting's name gives up width
   instead, so the row stays one row. */
.control-input {
  flex: 1 1 0;
  min-width: var(--input-min-width);
  max-width: var(--control-width);
}

.control-number {
  flex: 0 0 var(--number-width);
  min-width: 0;
  font-variant-numeric: tabular-nums;
}

.midi-button {
  font-size: var(--font-small);
  color: var(--text-muted);
}

.midi-button.bound {
  color: var(--text);
}

.midi-button.armed {
  border-color: var(--accent);
  color: var(--accent);
}

/* The layers table cannot fit on one row: its name and Q button share the first line, then the
   table and the Add layer button follow on lines of their own. */
.kind-layers {
  flex-wrap: wrap;
  max-width: 100%;
  overflow-x: auto; /* on the narrowest phones the layers table scrolls sideways inside its row, not the page */
}

.kind-layers .layers,
.kind-layers .add-layer {
  order: 1; /* after the name and the Q button */
}

.kind-layers .layers {
  flex-basis: 100%;
}

.layers {
  border-collapse: collapse;
  font-size: var(--font-small);
}

.layers th {
  color: var(--text-muted);
  font-weight: normal;
  text-align: left;
  padding: 0 var(--gap-small) var(--gap-small) 0;
}

.layers td {
  padding: 0 var(--gap-small) var(--gap-small) 0;
}

.layer-number {
  width: var(--layer-number-width);
}

input[type="range"] {
  accent-color: var(--accent);
  padding: 0;
}

/* ---- narrow screens (phones, small tablets)
   Below this width the settings column no longer fits beside the panels. The page then becomes
   one column that scrolls as a whole: top bar, panels in a 2 x 2 grid, plots, then the settings
   at the full screen width. (CSS cannot read a variable in a media query, so the width is here
   only.) */
@media (max-width: 900px) {
  :root {
    --panel-columns: var(--narrow-panel-columns);
  }

  html,
  body {
    height: auto;
  }

  body {
    min-height: 100%;
  }

  .layout {
    flex-direction: column;
  }

  .stage {
    overflow: visible;
  }

  .sidebar {
    width: auto;
    overflow: visible;
    border-left: none;
    border-top: 1px solid var(--border);
  }

  /* the status line gets a line of its own under the title and buttons */
  .status {
    order: 1;
    flex-basis: 100%;
  }

  /* the page scrolls, so the alert bar stays pinned to the bottom of the screen */
  .alerts {
    position: sticky;
    bottom: 0;
    background: var(--background);
  }
}

/* ---- touch screens: finger-sized controls */
@media (pointer: coarse) {
  button,
  select,
  input:not([type="checkbox"]) {
    min-height: var(--touch-target);
    font-size: var(--touch-input-font);
  }

  input[type="checkbox"] {
    width: var(--touch-checkbox);
    height: var(--touch-checkbox);
  }

  .control-group > summary {
    min-height: var(--touch-target);
    display: list-item;
    align-content: center;
  }
}
