/* Source Sans 3 by Adobe, SIL Open Font License: fonts/LICENSE.md. */
@font-face {
  font-family: 'Source Sans 3';
  font-style: normal;
  font-weight: 200 900;
  font-display: swap;
  src: url('./fonts/SourceSans3VF-Upright.ttf.woff2') format('woff2');
}

:root {
  color-scheme: light;
  --ink: #111111;
  --muted: #5b5b5b;
  --paper: #faf7f2;
  --paper-2: #f3ede2;
  --line: rgba(17, 17, 17, 0.18);
  --clay: #d96b3f;
  --grid-minor: rgba(17, 17, 17, 0.045);
  --grid-major: rgba(17, 17, 17, 0.12);
  --axis: rgba(17, 17, 17, 0.42);
  --red: var(--clay);
  --blue: #506c85;
  --shade: rgba(17, 17, 17, 0.04);
  --hover-col: rgba(80, 108, 133, 0.10);
  --sans: 'Source Sans 3', 'Helvetica Neue', Arial, sans-serif;
  --math: Georgia, 'Times New Roman', serif;
}

*, *::before, *::after { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 17px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
button:focus-visible, input:focus-visible, a:focus-visible {
  outline: 3px solid var(--clay);
  outline-offset: 3px;
}

.app {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 26px clamp(16px, 4vw, 24px) 32px;
}
.course-navigation { display: flex; }
.applet-heading { margin: 32px 0; }
.course-name { margin: 0 0 8px; color: var(--muted); font-size: 15px; font-weight: 600; }
.course-code { color: var(--clay); }
h1 { margin: 0; font-size: clamp(36px, 4.5vw, 56px); font-weight: 600; line-height: 1.05; letter-spacing: -0.035em; }
.applet-description { margin: 8px 0 0; color: var(--muted); font-size: 16px; line-height: 1.4; }
.fourier-app {
  --blue: #2a8f88;
  --hover-col: rgba(42, 143, 136, 0.10);
  --math: 'KaTeX_Main', Georgia, 'Times New Roman', serif;
}
.fourier-app .katex { font-size: 1em; }
.formula {
  margin-top: 6px;
  min-height: 3em;
  padding-block: 2px;
  font-family: var(--math);
  font-size: clamp(18px, 1.9vw, 22px);
  text-align: center;
}
.formula .katex-display { margin: 0; }

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 280px;
  grid-template-areas: 'wave waveform' 'coef sound';
  gap: 20px;
  align-items: start;
}
.workspace > * { min-width: 0; }
.wave-sheet { grid-area: wave; }
.waveform-controls { grid-area: waveform; }
.coef-sheet { grid-area: coef; }
.sound-controls { grid-area: sound; }
.sheet, .controls { border: 1.5px solid var(--ink); border-radius: 20px; background: var(--paper); }
.sheet { padding: 20px 12px 0; }
.sheet-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 8px 16px;
}
h2 { margin: 0; font-size: 14px; font-weight: 700; line-height: 1.35; letter-spacing: 0.1em; text-transform: uppercase; color: var(--muted); }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; color: var(--muted); font-size: 14px; }
.legend > span { display: inline-flex; align-items: center; gap: 6px; }
/* Reserve label space so hovering and changing the harmonic count cannot move the legend. */
.legend-sum { flex: 0 0 6em; }
.legend-harmonic { flex: 0 0 5.5em; }
.legend-harmonic[aria-hidden='true'] { visibility: hidden; }
.legend .math-label { font-family: var(--math); font-variant-numeric: tabular-nums; white-space: nowrap; }
.legend sub { font-size: 0.7em; line-height: 0; }
.m, .legend i:not(.sw) { font-family: var(--math); font-style: italic; }
.sw { display: inline-block; width: 18px; height: 0; border-top: 2px solid; }
.sw.fn { border-color: var(--ink); opacity: 0.45; }
.sw.sum { border-color: var(--red); }
.sw.harm { border-color: var(--blue); border-top-style: dashed; }

/* Explicit plot heights keep canvas pixels and hover text from changing the layout. */
.plot { position: relative; height: 260px; }
.wave-sheet .plot { height: clamp(240px, 25vw, 300px); }
.plot canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: crosshair;
  user-select: none;
  -webkit-user-select: none;
}
#coefCanvas { cursor: ns-resize; }
.hint { position: absolute; top: 12px; right: 24px; color: var(--muted); font-size: 13px; pointer-events: none; }
.hint.gone { visibility: hidden; }
.wave-footer {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  align-items: center;
  gap: 4px;
  margin-top: 8px;
  padding: 16px 8px 10px;
  border-top: 1px solid var(--line);
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}
.hover-read { grid-column: 1 / -1; min-height: 22px; color: var(--muted); font-size: 13px; }
.coef-info {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: minmax(24px, auto);
  align-content: start;
  gap: 4px 12px;
  margin-top: 12px;
  padding: 12px 8px 16px;
  border-top: 1px solid var(--line);
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}

.coef-info > span { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.coef-info[aria-hidden='true'] > span { visibility: hidden; }
.coef-info .k { color: var(--muted); }
.coef-info sub { font-size: 0.7em; line-height: 0; }

.controls { display: flex; flex-direction: column; gap: 24px; padding: 20px; }
.group { display: flex; flex-direction: column; gap: 12px; }
.preset-grid, .transform-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 9px 12px;
  border: 1px solid var(--ink);
  border-radius: 999px;
  background: transparent;
  font-size: 14px;
  font-weight: 600;
  line-height: 1.2;
}
.btn:hover, .mini:hover, .seg button:hover { background: var(--paper-2); }
.btn[aria-pressed='true'], .btn.sound.on { background: var(--ink); color: var(--paper); }
.waveform-controls .btn { border-radius: 8px; text-align: left; }
.transform-grid .btn { justify-content: flex-start; padding: 8px; }
.btn.preset {
  display: grid;
  grid-template-columns: 26px minmax(0, 1fr);
  gap: 8px;
  padding: 8px;
}
.btn.preset svg { display: block; flex-shrink: 0; width: 26px; height: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; font-size: 14px; }
.field-row label { color: var(--muted); }
.field-row.harmonics-control { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; gap: 12px; }
.val { font-weight: 600; white-space: nowrap; font-variant-numeric: tabular-nums; }
.stepper { display: inline-flex; align-items: center; gap: 4px; flex-shrink: 0; }
.stepper .val { min-width: 3ch; text-align: center; }
.mini {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: 1px solid var(--ink);
  border-radius: 50%;
  background: transparent;
  font-size: 20px;
  line-height: 1;
}
.mini:disabled { opacity: 0.35; cursor: default; background: transparent; }
input[type='range'] { width: 100%; height: 28px; margin: 0; accent-color: var(--ink); cursor: pointer; }
.seg { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); padding: 3px; border: 1px solid var(--ink); border-radius: 999px; }
.seg button { min-height: 36px; border: 0; border-radius: 999px; padding: 6px 12px; background: transparent; font-size: 14px; font-weight: 600; line-height: 1.2; }
.seg button[aria-pressed='true'] { background: var(--ink); color: var(--paper); }
.check { display: flex; align-items: center; gap: 8px; min-height: 32px; padding: 0 8px; margin-bottom: 6px; font-size: 14px; cursor: pointer; }
.check[hidden] { display: none; }
.check input { width: 18px; height: 18px; margin: 0; accent-color: var(--ink); flex-shrink: 0; }
.sound-controls { gap: 16px; background: var(--paper-2); }
.btn.sound { gap: 9px; width: 100%; }
.dot { width: 9px; height: 9px; border-radius: 50%; border: 2px solid currentColor; }
.sound.on .dot { background: currentColor; }
footer { margin-top: 40px; color: var(--muted); font-size: 13px; line-height: 1.5; }

/* Keep both plots and controls in view on a desktop, allowing for browser chrome. */
@media (min-width: 1100px) {
  .app { padding-block: 20px; }
  .applet-heading { margin-block: 10px; }
  .course-name { margin-bottom: 4px; }
  h1 { font-size: 48px; }
  .workspace { grid-template-columns: repeat(2, minmax(0, 1fr)); grid-template-areas: 'wave coef' 'waveform sound'; gap: 16px; }
  .wave-sheet, .coef-sheet { display: flex; flex-direction: column; align-self: stretch; }
  .wave-sheet .plot, .coef-sheet .plot { flex-grow: 1; }
  .sheet { padding-top: 16px; }
  .sheet-head { padding-bottom: 8px; }
  .wave-sheet .plot, .coef-sheet .plot { height: clamp(240px, calc(100svh - 720px), 300px); }
  .wave-footer { padding-top: 12px; }
  .coef-info { margin-top: 4px; padding-block: 6px; }
  .check { min-height: 24px; margin-bottom: 2px; }
  .controls { padding: 16px; gap: 16px; }
  .waveform-controls { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .group { gap: 8px; }
  .preset-grid, .transform-grid { gap: 6px; }
  .btn { min-height: 40px; padding-block: 8px; }
  footer { margin-top: 20px; }
}
@media (max-width: 900px) {
  .workspace { grid-template-columns: minmax(0, 1fr); grid-template-areas: 'wave' 'waveform' 'coef' 'sound'; }
  .waveform-controls { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .sound-controls { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); align-items: center; }
  .sound-controls h2 { grid-column: 1 / -1; }
}
@media (max-width: 600px) {
  .applet-heading { margin: 28px 0; }
  .sheet { padding: 18px 8px 0; }
  .wave-footer { gap: 8px; }
  .waveform-controls, .sound-controls { display: flex; }
  .sound-controls { align-items: stretch; }
  .preset-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .btn.preset { grid-template-columns: minmax(0, 1fr); gap: 6px; min-height: 64px; }
  .transform-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .transform-grid .btn { padding-inline: 6px; }
  .coef-sheet .sheet-head { align-items: stretch; flex-direction: column; }
  .seg { width: 100%; }
  .seg button { padding-inline: 6px; }
  .coef-info { grid-template-columns: repeat(2, minmax(0, 1fr)); grid-template-rows: repeat(2, minmax(24px, auto)); }
}
@media (max-width: 380px) {
  .preset-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .btn.preset { grid-template-columns: 26px minmax(0, 1fr); gap: 8px; min-height: 44px; }
}
@media (max-width: 360px) {
  .controls { padding: 16px; }
  .transform-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
