:root {
  --bg: #fafafa;
  --surface: #ffffff;
  --surface-2: #f4f4f5;
  --border: #d4d4d8;
  --text: #18181b;
  --text-dim: #52525b;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-soft: #dbeafe;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --grid: #e4e4e7;
  --axis: #a1a1aa;
  --wave: #2563eb;
  --handle: #18181b;
  --handle-fill: #ffffff;
  --shadow: 0 8px 32px #0000001a;
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #09090b;
    --surface: #18181b;
    --surface-2: #27272a;
    --border: #3f3f46;
    --text: #fafafa;
    --text-dim: #a1a1aa;
    --accent: #60a5fa;
    --accent-hover: #93c5fd;
    --accent-soft: #1e3a8a55;
    --danger: #ef4444;
    --danger-hover: #f87171;
    --grid: #27272a;
    --axis: #52525b;
    --wave: #60a5fa;
    --handle: #fafafa;
    --handle-fill: #18181b;
    --shadow: 0 8px 32px #0009;
  }
}

[hidden] {
  display: none !important;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  padding: 1rem;
  min-height: 100dvh;
}

@media (min-width: 640px) {
  body { padding: 2rem 1rem; }
}

.container {
  width: 100%;
  max-width: 60rem;
  margin-inline: auto;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.page-header {
  & h1 {
    font-family: Georgia, "Times New Roman", Times, serif;
    font-size: 1.75rem;
    letter-spacing: -0.01em;
    margin-bottom: 0.25rem;
  }

  & .lede {
    color: var(--text-dim);
    font-size: 0.95rem;
    max-width: 50ch;
  }
}

.controls {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.875rem;

  @media (min-width: 640px) {
    grid-template-columns: auto 1fr auto;
    align-items: end;
    gap: 1rem;
  }
}

.ctrl {
  display: flex;
  flex-direction: column;
  gap: 0.375rem;

  & label {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-dim);
    font-weight: 600;
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 0.5rem;
  }

  & .freq-readout,
  & .readout {
    font-variant-numeric: tabular-nums;
    font-size: 0.85rem;
    color: var(--text);
    font-weight: 500;
    text-transform: none;
    letter-spacing: 0;
  }
}

.ctrl-buttons {
  flex-direction: row;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.ctrl-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;

  & > div {
    display: flex;
    flex-direction: column;
    gap: 0.375rem;
  }
}

select,
input[type="range"] {
  font: inherit;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.45rem 0.6rem;
  width: 100%;
}

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

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  padding: 0.55rem 0.95rem;
  font-size: 0.9rem;
  font-weight: 600;
  line-height: 1;
  border: 1px solid transparent;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
  min-height: 2.5rem;

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

.btn-primary {
  background: var(--accent);
  color: white;

  &:hover { background: var(--accent-hover); }

  &.is-active {
    background: var(--danger);
    &:hover { background: var(--danger-hover); }
  }
}

.play-dot {
  width: 0.55rem;
  height: 0.55rem;
  border-radius: 50%;
  background: currentColor;
  opacity: 0.4;
  flex-shrink: 0;
}

.is-playing .play-dot {
  opacity: 1;
  animation: pulse 1.2s ease-in-out infinite;
  box-shadow: 0 0 0 0 currentColor;
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 255, 255, 0.6); }
  50%      { box-shadow: 0 0 0 6px rgba(255, 255, 255, 0); }
}

.btn-ghost {
  background: transparent;
  color: var(--text);
  border-color: var(--border);

  &:hover { background: var(--surface-2); }
}

.btn-danger {
  background: transparent;
  color: var(--danger);
  border-color: var(--danger);

  &:hover {
    background: var(--danger);
    color: white;
  }
}

.stage {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;

  & canvas {
    width: 100%;
    height: auto;
    aspect-ratio: 900 / 320;
    background: var(--surface-2);
    border-radius: 6px;
    touch-action: none;
    cursor: crosshair;
    display: block;
  }

  & .status {
    font-size: 0.85rem;
    color: var(--text-dim);
    padding: 0 0.25rem;
  }
}

.legend {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.125rem;

  & h2 {
    font-size: 0.95rem;
    margin-bottom: 0.5rem;
  }

  & ul {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
    color: var(--text-dim);
    font-size: 0.9rem;
  }

  & code {
    font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
    font-size: 0.85em;
    background: var(--surface-2);
    padding: 0.1rem 0.35rem;
    border-radius: 4px;
  }
}

dialog {
  border: 1px solid var(--border);
  padding: 0;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);

  /* Mobile: full-screen */
  width: 100dvw;
  max-width: 100dvw;
  height: 100dvh;
  max-height: 100dvh;
  margin: 0;
  inset: 0;
  border: none;
  border-radius: 0;

  &::backdrop {
    background: rgba(0, 0, 0, 0.45);
    backdrop-filter: blur(2px);
  }

  @media (min-width: 640px) {
    width: min(28rem, calc(100% - 2rem));
    max-width: 28rem;
    height: auto;
    max-height: calc(100dvh - 2rem);
    margin: auto;
    inset: 0;
    border: 1px solid var(--border);
    border-radius: var(--radius);
  }
}

.dialog-form {
  display: flex;
  flex-direction: column;
  height: 100%;

  @media (min-width: 640px) {
    height: auto;
  }
}


.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.875rem 1rem;
  border-bottom: 1px solid var(--border);

  & h2 {
    font-size: 1rem;
  }
}

.dialog-close {
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 1.5rem;
  line-height: 1;
  width: 2rem;
  height: 2rem;
  border-radius: 6px;
  cursor: pointer;

  &:hover {
    background: var(--surface-2);
    color: var(--text);
  }
}

.dialog-body {
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.875rem;
  flex: 1;
  overflow-y: auto;
}

.dialog-footer {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  border-top: 1px solid var(--border);
}
