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

:root {
  --bg: #111;
  --surface: #1e1e1e;
  --border: #333;
  --text: #ddd;
  --accent: #4a9eff;
  --active: #2a6fd4;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', system-ui, sans-serif;
  font-size: 14px;
  height: 100vh;
  overflow: hidden;
}

#overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  cursor: pointer;
}

#overlay-content {
  text-align: center;
  pointer-events: none;
}

#overlay-content h2 {
  font-size: 2.5rem;
  margin-bottom: 0.5rem;
  color: var(--accent);
}

#overlay-content p {
  font-size: 1.1rem;
  color: var(--text);
  margin-bottom: 1.2rem;
}

#btn-demo {
  font-size: 1.1rem;
  padding: 10px 32px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 600;
  letter-spacing: 0.04em;
  transition: background 0.15s, transform 0.1s;
  pointer-events: auto;
}

#btn-demo:hover {
  background: #2a6fd4;
  transform: scale(1.04);
}

.warning {
  font-size: 0.78rem;
  color: #e0a020;
  margin-top: 12px;
  max-width: 360px;
  line-height: 1.4;
}

/* ── Tooltips ────────────────────────────────────────────────────────────── */
/* Usage: add data-tip="..." to any element. */

[data-tip] { position: relative; }

[data-tip]::after {
  content: attr(data-tip);
  position: absolute;
  top: calc(100% + 7px);
  left: 50%;
  transform: translateX(-50%);
  background: #1a1a1a;
  color: #e8e8e8;
  border: 1px solid #444;
  border-radius: 5px;
  padding: 5px 9px;
  font-size: 11.5px;
  line-height: 1.45;
  white-space: normal;
  width: max-content;
  max-width: 210px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.12s;
  z-index: 300;
  box-shadow: 0 3px 10px rgba(0,0,0,0.5);
}

[data-tip]:hover::after {
  opacity: 1;
}

/* Arrow pointing up */
[data-tip]::before {
  content: '';
  position: absolute;
  top: calc(100% + 1px);
  left: 50%;
  transform: translateX(-50%);
  border: 5px solid transparent;
  border-bottom-color: #444;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.12s;
  z-index: 301;
}

[data-tip]:hover::before { opacity: 1; }

/* ── Colour-wheel popover ────────────────────────────────────────────────── */

.dial-anchor {
  position: relative;
}

#color-wheel-popover {
  display: none;
  position: absolute;
  top: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 250;
  background: #1a1a1a;
  border: 1px solid #444;
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 6px 28px rgba(0,0,0,0.7);
  min-width: 196px;
}

#color-wheel-popover.visible { display: block; }

/* ── Dial widget ─────────────────────────────────────────────────────────── */

.cdial-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  user-select: none;
}

.cdial-area {
  position: relative;
  width: 160px;
  height: 160px;
  cursor: grab;
}
.cdial-area:active { cursor: grabbing; }

/* Conic-gradient colour wheel — sits behind the SVG ring */
.cdial-wheel {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 108px;       /* diameter = R_WHEEL*2 in SVG units, scaled to px */
  height: 108px;
  margin-top:  -54px;
  margin-left: -54px;
  border-radius: 50%;
  background: conic-gradient(
    from 0deg,
    hsl(0,   100%, 55%),
    hsl(30,  100%, 55%),
    hsl(60,  100%, 55%),
    hsl(90,  100%, 55%),
    hsl(120, 100%, 55%),
    hsl(150, 100%, 55%),
    hsl(180, 100%, 55%),
    hsl(210, 100%, 55%),
    hsl(240, 100%, 55%),
    hsl(270, 100%, 55%),
    hsl(300, 100%, 55%),
    hsl(330, 100%, 55%),
    hsl(360, 100%, 55%)
  );
  box-shadow: 0 0 0 2px #333;
  pointer-events: none;
}

.cdial-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
}

/* Fixed pointer triangle */
.cdial-pointer {
  fill: #fff;
  filter: drop-shadow(0 1px 2px rgba(0,0,0,0.8));
}

/* Tick marks */
.cdial-tick       { stroke: #aaa; stroke-width: 1.5; }
.cdial-tick-sharp { stroke: #666; stroke-width: 1;   }

/* Note-name labels */
.cdial-label {
  fill: #888;
  font-size: 8px;
  font-family: 'Segoe UI', system-ui, sans-serif;
  pointer-events: none;
}
.cdial-nat {
  fill: #ccc;
  font-size: 9px;
  font-weight: 600;
}

/* Direction toggle */
.cdial-dir {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: #aaa;
  cursor: pointer;
}
.cdial-dir:hover { color: #ddd; }
.cdial-dir-chk   { accent-color: var(--accent); cursor: pointer; }

.cdial-hint {
  font-size: 10px;
  color: #555;
  text-align: center;
}

/* ── About modal ─────────────────────────────────────────────────────────── */
#about-modal {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.75);
  z-index: 200;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

#about-modal.visible {
  display: flex;
}

#about-content {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 28px 32px;
  max-width: 580px;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  position: relative;
  color: var(--text);
  line-height: 1.65;
}

#about-content h2 {
  font-size: 1.5rem;
  color: var(--accent);
  margin-bottom: 12px;
}

#about-content h3 {
  font-size: 1rem;
  color: var(--accent);
  margin-top: 16px;
  margin-bottom: 6px;
}

#about-content p {
  margin-bottom: 8px;
}

#about-content ul {
  padding-left: 20px;
}

#about-content li {
  margin-bottom: 6px;
}

#about-content kbd {
  background: #333;
  border: 1px solid #555;
  border-radius: 3px;
  padding: 1px 5px;
  font-size: 12px;
  font-family: 'Consolas', 'Menlo', monospace;
}

#about-close {
  position: absolute;
  top: 12px;
  right: 14px;
  background: none;
  border: none;
  color: #888;
  font-size: 16px;
  cursor: pointer;
  padding: 4px 8px;
}

#about-close:hover {
  color: var(--text);
  background: none;
}

#app {
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  height: 100vh;
}

header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 8px 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

#header-row1 {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

#loaded-file {
  font-size: 12px;
  color: var(--accent);
  font-style: italic;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

#global-toggles {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
  flex-wrap: wrap;
}

.ctrl-row {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding-top: 5px;
  border-top: 1px solid var(--border);
}

.section-label {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  padding: 2px 7px;
  border-radius: 3px;
  white-space: nowrap;
  min-width: 50px;
  text-align: center;
}

.midi-label  { background: #1a2a1a; color: #6dbf6d; border: 1px solid #3a6a3a; }
.audio-label { background: #1a1a2a; color: #6d9dbf; border: 1px solid #3a4a6a; }
.visual-label{ background: #2a1a2a; color: #bf6dbf; border: 1px solid #6a3a6a; }
.phase-label { background: #1a1200; color: #d4a800; border: 1px solid #6a5200; }

.hidden { display: none; }

.ctrl-group {
  display: flex;
  align-items: center;
  gap: 6px;
}

.ctrl-group label {
  font-size: 12px;
  color: #aaa;
  white-space: nowrap;
}

#sf-scale {
  width: 110px;
  accent-color: var(--accent);
  cursor: pointer;
}

#sf-scale-display {
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  min-width: 38px;
}

h1 {
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--accent);
  white-space: nowrap;
}

#mode-indicator {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  padding: 3px 8px;
  border-radius: 3px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: #aaa;
  white-space: nowrap;
}

#mode-indicator[data-mode="audio"] {
  border-color: #4a9eff88;
  color: var(--accent);
}

#audio-upload-label {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 5px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
  transition: background 0.15s;
  border-left: 2px solid var(--accent);
}

#audio-upload-label:hover { background: var(--border); }

#audio-input { display: none; }

#controls {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

select {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 5px 8px;
  border-radius: 4px;
  font-size: 13px;
  max-width: 220px;
}

button {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 5px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
  transition: background 0.15s;
}

button:hover { background: var(--border); }

button.toggle.active {
  background: var(--active);
  border-color: var(--accent);
  color: #fff;
}

#upload-label {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 5px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
  transition: background 0.15s;
}

#upload-label:hover { background: var(--border); }

#file-input { display: none; }

#piano-row {
  height: 130px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  overflow: hidden;
  flex-shrink: 0;
}

#piano-row.hidden { display: none; }

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

main {
  overflow: hidden;
  position: relative;
}

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

#phase-container {
  position: absolute;
  inset: 0;
}

footer {
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 6px 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

#progress-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
}

#time-display {
  white-space: nowrap;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  color: #aaa;
  min-width: 90px;
}

#progress {
  flex: 1;
  accent-color: var(--accent);
  cursor: pointer;
}

#info {
  display: flex;
  gap: 16px;
  font-size: 12px;
  color: #aaa;
}

#info span { white-space: nowrap; }
#midi-status.midi-active { color: #4f4; }
#midi-status.midi-error  { color: #f66; }

/* ── Sync measurement panel ──────────────────────────────────────────────── */
#sync-panel {
  display: none;
  position: absolute;
  bottom: 16px;
  right: 16px;
  background: rgba(0, 0, 0, 0.82);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 14px;
  font-size: 12px;
  font-family: 'Consolas', 'Menlo', monospace;
  line-height: 1.7;
  color: var(--text);
  z-index: 10;
  min-width: 240px;
  pointer-events: none;
}

#sync-panel.visible {
  display: block;
}

/* ── Fullscreen ────────────────────────────────────────────────────────────── */

/* CSS pseudo-fullscreen: fills the browser window without triggering a macOS
   Space switch. Applied by the toolbar button. */
body.app-fullscreen #app {
  position: fixed;
  inset: 0;
  z-index: 100;
  height: 100vh;
  padding-top: 0;
  grid-template-rows: 1fr;
}

body.app-fullscreen header,
body.app-fullscreen footer,
body.app-fullscreen #piano-row,
body.app-fullscreen #mobile-backdrop {
  display: none !important;
}

/* Native fullscreen (F11 / browser-triggered) still supported */
:fullscreen header,
:fullscreen footer,
:fullscreen #piano-row {
  display: none;
}

:fullscreen #app {
  grid-template-rows: 1fr;
}

/* Safari / older Chrome */
:-webkit-full-screen header,
:-webkit-full-screen footer,
:-webkit-full-screen #piano-row {
  display: none;
}

:-webkit-full-screen #app {
  grid-template-rows: 1fr;
}

/* ── Mobile layout (≤ 768 px) ──────────────────────────────────────────────── */

/* Hidden on desktop */
#mobile-strip    { display: none; }
#mobile-backdrop { display: none; }
#mobile-exit-fs  { display: none; }

@media (max-width: 768px) {
  /* Fixed compact header — clipped to strip height when panel is closed */
  header {
    position: fixed;
    top: 0; left: 0; right: 0;
    z-index: 50;
    padding: 0;
    gap: 0;
    max-height: 52px;
    overflow: hidden;
    transition: max-height 0.25s ease;
  }

  header.panel-open {
    max-height: 80vh;
    overflow-y: auto;
    padding-bottom: 10px;
  }

  /* ── Mobile strip ── */
  #mobile-strip {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 0 12px;
    min-height: 52px;
    flex-shrink: 0;
    border-bottom: 1px solid var(--border);
  }

  .mobile-brand {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--accent);
    flex: 1;
    white-space: nowrap;
  }

  #mobile-mode-pill {
    font-size: 11px;
    font-weight: 600;
    padding: 3px 7px;
    border-radius: 3px;
    border: 1px solid var(--border);
    background: var(--bg);
    color: #aaa;
    white-space: nowrap;
  }

  #mobile-mode-pill[data-mode="audio"],
  #mobile-mode-pill[data-mode="mic"] {
    border-color: #4a9eff88;
    color: var(--accent);
  }

  #mobile-transport { display: flex; gap: 6px; }

  #mobile-play-btn {
    background: var(--active);
    border-color: var(--accent);
    color: #fff;
    font-size: 15px;
    padding: 0 14px;
    min-height: 36px;
    border-radius: 4px;
  }

  #mobile-stop-btn {
    font-size: 15px;
    padding: 0 10px;
    min-height: 36px;
  }

  #mobile-settings-btn {
    font-size: 20px;
    line-height: 1;
    padding: 0 8px;
    min-height: 36px;
    background: none;
    border: none;
    color: var(--text);
    cursor: pointer;
  }

  /* ── Desktop row 1 becomes part of the panel — hide decorative elements ── */
  #header-row1 {
    padding: 6px 12px;
    border-top: 1px solid var(--border);
  }

  #header-row1 h1,
  #header-row1 #mode-indicator,
  #header-row1 #loaded-file { display: none; }

  #header-row1 #global-toggles { margin-left: 0; flex-wrap: wrap; }

  /* Ctrl rows get side padding inside the panel */
  .ctrl-row { padding: 6px 12px; }

  /* ── App layout: header is out of flow (fixed), so grid has 3 rows ── */
  #app {
    padding-top: 52px;
    grid-template-rows: auto 1fr auto;
  }

  /* ── Backdrop ── */
  #mobile-backdrop {
    position: fixed;
    inset: 52px 0 0 0;
    background: rgba(0, 0, 0, 0.55);
    z-index: 49;
    display: none;
  }

  #mobile-backdrop.visible { display: block; }

  /* ── Touch targets ── */
  button          { min-height: 40px; }
  select          { min-height: 38px; font-size: 14px; }
  input[type="range"] { min-height: 32px; }

  /* Piano is shorter on mobile to give more canvas space */
  #piano-row { height: 100px; }

  /* Pseudo-fullscreen and native fullscreen: remove the padding-top */
  body.app-fullscreen #app,
  :fullscreen #app,
  :-webkit-full-screen #app { padding-top: 0; grid-template-rows: 1fr; }

  /* Floating exit button — only visible on mobile in pseudo-fullscreen */
  body.app-fullscreen #mobile-exit-fs {
    display: flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    top: 14px;
    right: 14px;
    z-index: 200;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.45);
    border: 1px solid rgba(255, 255, 255, 0.18);
    color: #fff;
    font-size: 18px;
    line-height: 1;
    cursor: pointer;
    padding: 0;
  }
}
