/* Chord Shape — page styles. No web fonts, no remote assets. */

:root {
  --bg: #0f1013;
  --surface: #16171c;
  --surface-2: #1d1e24;
  --line: rgba(255, 255, 255, 0.08);
  --line-strong: rgba(255, 255, 255, 0.16);
  --text: #ece7dd;
  --muted: #a39d92;
  --accent: #f1c37a;
  --glow: #ffb55a;
  --on-accent: #1b1408;
  --ivory: #e4ddcf;
  --ivory-text: #17181c;
  --ebony: #25262c;
  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-title: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --gutter: 16px;
  --radius: 14px;
  --target: 44px;
  --key-gap: 4px;
  --axis-x: #f1c37a;
  --axis-y: #8fd3c8;
  --axis-z: #c9a7ff;
  color-scheme: dark;
}

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

[hidden] { display: none !important; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  background: var(--bg);
}

body {
  margin: 0;
  min-width: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font-ui);
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, p { margin: 0; }

.visually-hidden {
  position: absolute !important;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

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

/* ---------- Header ---------- */

.site-header {
  padding: 18px var(--gutter) 14px;
  border-bottom: 1px solid var(--line);
}

.site-header h1 {
  font-family: var(--font-title);
  font-weight: 500;
  font-size: 24px;
  line-height: 1.2;
  letter-spacing: 0.01em;
}

.subtitle {
  margin-top: 4px;
  color: var(--muted);
  font-size: 15px;
}

.noscript {
  margin-top: 10px;
  padding: 10px 12px;
  border: 1px solid var(--accent);
  border-radius: 10px;
  color: var(--text);
}

/* ---------- Layout (single column first) ---------- */

.layout {
  display: block;
  padding: 0 var(--gutter) 32px;
}

.stage-region,
.companion,
.panel {
  min-width: 0;
  max-width: 720px;
  margin: 0 auto;
}

.stage-region { padding-top: 16px; }

.stage {
  position: relative;
  height: min(60vh, 100vw);
  min-height: 240px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg);
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

#trace {
  position: absolute;
  top: 0;
  left: 0;
  border-radius: calc(var(--radius) - 1px);
  cursor: grab;
}
#trace:active { cursor: grabbing; }

.canvas-message {
  position: absolute;
  top: 50%;
  left: 50%;
  width: min(80%, 340px);
  transform: translate(-50%, -50%);
  text-align: center;
  color: var(--muted);
  font-size: 15px;
  pointer-events: none;
}
.canvas-message strong {
  display: block;
  margin-bottom: 4px;
  color: var(--text);
  font-weight: 600;
}

.stage-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 16px;
  padding: 10px 2px 0;
}

.trace-kind {
  min-width: 0;
  padding: 4px 12px;
  border: 1px solid rgba(241, 195, 122, 0.35);
  border-radius: 999px;
  color: var(--accent);
  font-size: 13px;
  line-height: 1.4;
}
.trace-kind[data-kind="empty"] { color: var(--muted); border-color: var(--line-strong); }

.view-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 12px;
  min-width: 0;
}

.hint {
  color: var(--muted);
  font-size: 13px;
}

/* ---------- Companion swings ---------- */

.companion { padding-top: 14px; }

.companion-stage {
  position: relative;
  height: min(52vw, 320px);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg);
  overflow: hidden;
}
/* Narrow canvas (< 520 px): the strip stacks above the builder square. */
@media (max-width: 553px) {
  .companion-stage { height: calc(min(52vw, 320px) + 176px); }
}

#companion-canvas { touch-action: auto; }   /* not draggable: let the page scroll */

.companion-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 4px 20px;
  padding: 10px 2px 0;
}

.companion-caption {
  flex: 1 1 240px;
  min-width: 0;
  padding-top: 12px;
  color: var(--muted);
  font-size: 13px;
}

.speed {
  flex: 0 1 auto;
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
}
.speed legend {
  padding: 0;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}
.speed .choices { gap: 0 16px; }
.speed .choice { font-size: 14px; }

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

.panel { padding-top: 8px; }

.group {
  padding: 18px 0;
  border-bottom: 1px solid var(--line);
}

.group h2 {
  margin-bottom: 10px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.help {
  margin-top: 8px;
  color: var(--muted);
  font-size: 13px;
}

.status {
  min-height: 1.5em;
  margin: 8px 0 4px;
  color: var(--accent);
  font-size: 14px;
}

/* ---------- Buttons and inputs ---------- */

button { font: inherit; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--target);
  min-width: var(--target);
  padding: 0 20px;
  border: 1px solid rgba(241, 195, 122, 0.5);
  border-radius: 999px;
  background: transparent;
  color: var(--accent);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 120ms ease, color 120ms ease, border-color 120ms ease;
  -webkit-tap-highlight-color: transparent;
}
.btn:hover { background: rgba(241, 195, 122, 0.08); }
.btn:active { background: rgba(241, 195, 122, 0.16); }

.btn-primary {
  min-width: 112px;
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
}
.btn-primary:hover { background: #f6d197; }
.btn-primary:active { background: var(--glow); }

.btn-quiet {
  border-color: var(--line-strong);
  color: var(--text);
  font-weight: 500;
}

.btn-small {
  margin-top: 8px;
  padding: 0 16px;
  font-size: 14px;
}

.btn:disabled,
.btn:disabled:hover {
  background: transparent;
  border-color: var(--line-strong);
  color: var(--muted);
  cursor: not-allowed;
  opacity: 0.6;
}

.choices {
  display: flex;
  flex-wrap: wrap;
  gap: 0 20px;
}

.choice {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-height: var(--target);
  cursor: pointer;
}
.choice input {
  width: 20px;
  height: 20px;
  margin: 0;
  accent-color: var(--accent);
  cursor: pointer;
}
.choice input:disabled { cursor: not-allowed; }

.range-row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) 3.5em;
  align-items: center;
  gap: 4px 12px;
  margin-top: 12px;
}
.range-row-stacked { grid-template-columns: minmax(0, 1fr); }
.range-row label { font-size: 14px; }
.range-row output {
  color: var(--muted);
  font-size: 13px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

input[type="range"] {
  width: 100%;
  min-width: 0;
  height: var(--target);
  margin: 0;
  accent-color: var(--accent);
  background: transparent;
  cursor: pointer;
}
input[type="range"]:disabled { cursor: not-allowed; opacity: 0.5; }

/* ---------- Keyboard: 2 × 6 grid of labeled buttons (narrow) ---------- */

.keyboard {
  position: relative;
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: var(--key-gap);
}

.key {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  min-width: var(--target);
  min-height: 56px;
  margin: 0;
  padding: 4px 2px;
  border: 1px solid transparent;
  border-radius: 10px;
  background: var(--ivory);
  color: var(--ivory-text);
  font-size: 15px;
  font-weight: 600;
  line-height: 1.15;
  cursor: pointer;
  transition: background-color 120ms ease, color 120ms ease, box-shadow 120ms ease;
  -webkit-tap-highlight-color: transparent;
}

.key.black {
  background: var(--ebony);
  border-color: var(--line-strong);
  color: var(--text);
}

.key-name,
.key-alt { display: block; white-space: nowrap; }

.key.black .key-name,
.key.black .key-alt { font-size: 13px; }
.key.black .key-alt { font-weight: 500; opacity: 0.85; }

/* Selected: gold fill, dark text, plus a dot so it is not colour alone. */
.key[aria-pressed="true"],
.key.black[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
  box-shadow: 0 0 18px rgba(255, 181, 90, 0.35);
}
.key[aria-pressed="true"]::before {
  content: "";
  display: block;
  width: 6px;
  height: 6px;
  margin-bottom: 3px;
  border-radius: 50%;
  background: currentColor;
}
.key.black[aria-pressed="true"] .key-alt { opacity: 1; }

.key:not([aria-pressed="true"]):hover { filter: brightness(1.06); }
/* Focus ring drawn inside the key: an outer ring would be covered by the
   neighbouring keys, and raising a focused white key would hide half of the
   black keys beside it. Dark ring on ivory/gold, gold ring on ebony. */
.key:focus-visible {
  outline: 3px solid var(--on-accent);
  outline-offset: -6px;
}
.key.black:focus-visible { outline-width: 2px; outline-offset: -4px; }
.key.black:not([aria-pressed="true"]):focus-visible { outline-color: var(--accent); }

/* ---------- Keyboard: piano layout (≥ 640px) ---------- */

@media (min-width: 640px) {
  .keyboard {
    --key-gap: 3px;
    --white: calc((100% - 6 * var(--key-gap)) / 7);
    grid-template-columns: repeat(7, minmax(0, 1fr));
    height: 168px;
    max-width: 560px;
  }

  /* White keys auto-place into the 7 columns; black keys are taken out of
     the grid and centred on the gap between their two white neighbours. */
  .key {
    justify-content: flex-end;
    height: 100%;
    min-height: 0;
    padding-bottom: 12px;
    border-radius: 0 0 9px 9px;
  }

  .key.black {
    --k: 1;
    position: absolute;
    top: 0;
    left: calc(var(--k) * (var(--white) + var(--key-gap)) - var(--key-gap) / 2 - var(--white) * 0.3);
    z-index: 1;
    width: calc(var(--white) * 0.6);
    min-width: 0;
    height: 60%;
    padding: 0 1px 8px;
    border-radius: 0 0 7px 7px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.45);
  }
  .key.black[data-pc="1"] { --k: 1; }
  .key.black[data-pc="3"] { --k: 2; }
  .key.black[data-pc="6"] { --k: 4; }
  .key.black[data-pc="8"] { --k: 5; }
  .key.black[data-pc="10"] { --k: 6; }

  .key.black .key-name,
  .key.black .key-alt { font-size: 12px; }
}

/* ---------- Selected table ---------- */

.selected {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}
.selected th {
  padding: 0 8px 6px 0;
  border-bottom: 1px solid var(--line-strong);
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  text-align: left;
}
.selected td {
  padding: 8px 8px 8px 0;
  border-bottom: 1px solid var(--line);
}
.selected th:last-child,
.selected td:last-child { padding-right: 0; text-align: right; }
.selected .empty-row td { color: var(--muted); text-align: left; }
.selected tr.row-new { animation: row-in 200ms ease-out both; }
@keyframes row-in {
  from { opacity: 0; transform: translateY(-6px); }
  to { opacity: 1; transform: none; }
}
.axis-tag {
  display: inline-block;
  min-width: 1.6em;
  padding: 0 6px;
  border-radius: 6px;
  background: rgba(241, 195, 122, 0.12);
  color: var(--accent);
  font-weight: 600;
  text-align: center;
}
.axis-tag[data-axis="y"] { background: rgba(143, 211, 200, 0.14); color: var(--axis-y); }
.axis-tag[data-axis="z"] { background: rgba(201, 167, 255, 0.14); color: var(--axis-z); }

/* ---------- How does this work? ---------- */

.how {
  padding: 6px 0 4px;
  border-bottom: 1px solid var(--line);
}

.how summary {
  display: flex;
  align-items: center;
  min-height: var(--target);
  color: var(--accent);
  font-weight: 600;
  cursor: pointer;
  list-style-position: inside;
}

.how-body {
  padding: 4px 0 14px;
  font-size: 14px;
  color: var(--text);
}
.how-body h3 {
  margin: 16px 0 4px;
  font-size: 14px;
  font-weight: 600;
}
.how-body h3:first-child { margin-top: 6px; }
.how-body p { color: #d3cdc2; }
.how-body code,
.how-body var {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
  font-style: normal;
  color: var(--accent);
  overflow-wrap: anywhere;
}

.ratio-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(76px, 1fr));
  gap: 4px 8px;
  margin: 8px 0 10px;
  padding: 0;
  list-style: none;
  font-variant-numeric: tabular-nums;
}
.ratio-list li {
  padding: 4px 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
}
.ratio-list span {
  display: inline-block;
  min-width: 1.6em;
  color: var(--muted);
}

/* ---------- Two columns (≥ 960px) ---------- */

@media (min-width: 960px) {
  .site-header { padding: 20px 28px 16px; }
  .site-header h1 { font-size: 28px; }

  .layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 360px;
    padding: 0;
  }

  .stage-col {
    display: grid;
    /* The trace takes the spare height but never shrinks below its own
       minimum, so a short window scrolls this column instead of overlapping. */
    grid-template-rows: minmax(min-content, 1fr) auto;
    min-width: 0;
  }

  .stage-region {
    display: flex;
    flex-direction: column;
    max-width: none;
    margin: 0;
    padding: 20px 24px 0 28px;
  }

  .companion {
    max-width: none;
    margin: 0;
    padding: 14px 24px 20px 28px;
  }
  .companion-stage { height: 200px; }

  .stage {
    flex: 1 1 auto;
    height: 72vh;
    min-height: 360px;
  }

  .panel {
    max-width: none;
    margin: 0;
    padding: 4px 16px 24px;
    border-left: 1px solid var(--line);
    background: var(--surface);
  }

  .keyboard { max-width: none; }
}

/* Tall enough: lock the page to the viewport so the canvas fills it and
   the panel scrolls on its own. Short windows keep normal page scrolling. */
@media (min-width: 960px) and (min-height: 560px) {
  body {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    height: 100vh;
    height: 100dvh;
  }

  .layout {
    grid-template-rows: minmax(0, 1fr);
    min-height: 0;
  }

  .stage-col { min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
  .stage { height: auto; min-height: 280px; }

  .panel {
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
  }
}

@media (min-width: 960px) and (min-height: 901px) {
  .companion-stage { height: 240px; }
}

/* ---------- Reduced motion ---------- */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    transition: none !important;
    animation: none !important;
    scroll-behavior: auto !important;
  }
}
