/* ==========================================================================
   NOCTURNE — styles
   Palette: ink navy, charcoal, warm amber, subtle teal, ivory/ebony keys.
   Single committed dark look (a midnight studio), so no light-theme blocks.
   ========================================================================== */

:root {
  color-scheme: dark;

  /* ---- Backdrop ---------------------------------------------------------
     Replace `none` with url("assets/backdrop.jpg") (or any image path) to
     drop in the studio photograph. The CSS fallback below stays underneath
     and shows through if the image is missing or still loading. */
  --backdrop-image: none;

  --ink: #0a101c;
  --ink-2: #0f1624;
  --charcoal: #161a22;
  --panel: rgba(16, 21, 32, 0.78);
  --panel-edge: rgba(243, 236, 220, 0.09);
  --amber: #e8a84a;
  --amber-soft: rgba(232, 168, 74, 0.16);
  --amber-glow: rgba(232, 168, 74, 0.45);
  --teal: #5fb8ad;
  --teal-soft: rgba(95, 184, 173, 0.16);
  --rec: #e2604b;
  --ivory: #f3ecdc;
  --text: #e8e1d2;
  --muted: #939aa9;
  --faint: #5c6474;

  /* Arabic faces sit in each stack, so Arabic glyphs fall through to them */
  --font-display: "Bodoni Moda", "Amiri", "Didot", "Bodoni 72", Georgia, serif;
  --font-body: "Instrument Sans", "IBM Plex Sans Arabic", "Segoe UI", system-ui, -apple-system, sans-serif;
  --font-mono: "IBM Plex Mono", "IBM Plex Sans Arabic", ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --font-arabic-display: "Amiri", "Noto Naskh Arabic", "Times New Roman", serif;

  --radius: 10px;
  --gutter: clamp(16px, 3vw, 36px);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--ink);
  color: var(--text);
  font-family: var(--font-body);
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

/* ---------------------------------------------------------------- Backdrop */
.backdrop {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background:
    /* legibility veil over whatever sits beneath */
    linear-gradient(180deg, rgba(7, 10, 17, 0.62) 0%, rgba(7, 10, 17, 0.25) 38%, rgba(7, 10, 17, 0.88) 100%),
    /* replaceable photograph */
    var(--backdrop-image) center / cover no-repeat,
    /* CSS fallback studio: desk lamp, monitor spill, acoustic slats */
    radial-gradient(38% 34% at 84% 4%, rgba(232, 168, 74, 0.24), transparent 72%),
    radial-gradient(30% 26% at 8% 28%, rgba(95, 184, 173, 0.13), transparent 70%),
    radial-gradient(60% 40% at 50% 108%, rgba(232, 168, 74, 0.10), transparent 70%),
    repeating-linear-gradient(90deg, rgba(243, 236, 220, 0.022) 0 2px, transparent 2px 54px),
    linear-gradient(180deg, #0d1626 0%, #0a0f19 55%, #06080d 100%);
}

.backdrop::after {
  /* the lamp breathes very slightly */
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(22% 20% at 84% 6%, rgba(232, 168, 74, 0.10), transparent 70%);
  animation: lamp 9s ease-in-out infinite alternate;
}
@keyframes lamp { from { opacity: 0.55; } to { opacity: 1; } }

/* --------------------------------------------------------------------- App */
.app {
  position: relative;
  z-index: 1;
  min-height: 100%;
  display: grid;
  grid-template-rows: auto auto 1fr;
  gap: clamp(14px, 2.2vh, 24px);
  padding-inline: var(--gutter);
  padding-block: clamp(14px, 2.4vh, 26px);
  max-width: 1480px;
  margin: 0 auto;
}

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ------------------------------------------------------------------ Header */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px 20px;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: baseline; gap: 14px; margin-right: auto; }
.wordmark {
  font-family: var(--font-display);
  font-weight: 500;
  font-size: clamp(26px, 3.4vw, 38px);
  letter-spacing: 0.26em;
  text-transform: uppercase;
  color: var(--ivory);
  margin: 0;
  line-height: 1;
}
.wordmark em { font-style: italic; color: var(--amber); letter-spacing: 0.1em; }
.version {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--amber);
  border: 1px solid rgba(232, 168, 74, 0.35);
  border-radius: 4px;
  padding: 1px 5px;
  align-self: center;
}
.brand-sub {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}

.audio-status {
  display: inline-flex; align-items: center; gap: 8px;
  font-family: var(--font-mono); font-size: 11px; letter-spacing: 0.08em;
  color: var(--muted);
  padding: 6px 10px;
  border: 1px solid var(--panel-edge);
  border-radius: 999px;
  background: rgba(10, 14, 22, 0.6);
}
.audio-status .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--faint); }
.audio-status[data-state="running"] { color: var(--text); }
.audio-status[data-state="running"] .dot { background: var(--teal); box-shadow: 0 0 8px var(--teal); }
.audio-status[data-state="error"] { color: var(--rec); }
.audio-status[data-state="error"] .dot { background: var(--rec); }

.top-actions { display: flex; gap: 8px; }

/* ----------------------------------------------------------------- Buttons */
.btn {
  appearance: none;
  font: 500 13px/1 var(--font-body);
  color: var(--text);
  background: rgba(243, 236, 220, 0.04);
  border: 1px solid rgba(243, 236, 220, 0.14);
  border-radius: 8px;
  padding: 9px 13px;
  min-height: 36px;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  touch-action: manipulation;
  white-space: nowrap;
}
.btn:hover:not(:disabled) { background: rgba(243, 236, 220, 0.08); border-color: rgba(243, 236, 220, 0.26); }
.btn:disabled { opacity: 0.38; cursor: not-allowed; }
.btn[aria-pressed="true"] {
  color: var(--amber);
  border-color: rgba(232, 168, 74, 0.6);
  background: var(--amber-soft);
}
.btn.icon { width: 36px; padding: 0; font-size: 17px; }
.btn .lamp { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); flex: none; }
.btn[aria-pressed="true"] .lamp { background: var(--amber); box-shadow: 0 0 8px var(--amber-glow); }
.btn.rec[aria-pressed="true"] { color: #f2a193; border-color: rgba(226, 96, 75, 0.7); background: rgba(226, 96, 75, 0.14); }
.btn.rec .lamp { background: #6b3a35; }
.btn.rec[aria-pressed="true"] .lamp { background: var(--rec); box-shadow: 0 0 10px var(--rec); animation: onair 1s steps(2) infinite; }
@keyframes onair { 50% { opacity: 0.35; } }

:focus-visible { outline: 2px solid var(--teal); outline-offset: 2px; border-radius: 6px; }

/* ----------------------------------------------------------------- Console */
.console {
  display: grid;
  grid-template-columns: minmax(260px, 0.9fr) 2.1fr;
  gap: 14px;
}

.readout {
  position: relative;
  padding: 16px 18px 14px;
  border-radius: var(--radius);
  background:
    repeating-linear-gradient(0deg, rgba(255, 255, 255, 0.018) 0 1px, transparent 1px 3px),
    linear-gradient(160deg, #0b1019, #070a10);
  border: 1px solid rgba(232, 168, 74, 0.16);
  box-shadow: inset 0 0 40px rgba(0, 0, 0, 0.6), 0 10px 30px rgba(0, 0, 0, 0.35);
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  gap: 6px;
  min-height: 170px;
  overflow: hidden;
}
.eyebrow {
  font-family: var(--font-mono);
  font-size: 10.5px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--muted);
}
.chord {
  font-family: var(--font-display);
  font-style: italic;
  font-weight: 500;
  font-size: clamp(40px, 5.2vw, 64px);
  line-height: 1.05;
  color: var(--amber);
  text-shadow: 0 0 22px rgba(232, 168, 74, 0.35);
  align-self: center;
  overflow-wrap: anywhere;
}
.chord.idle { color: #3c4150; text-shadow: none; }
.chord .q { font-size: 0.55em; margin-left: 0.04em; font-style: normal; letter-spacing: 0.02em; }
.chord .bass { font-size: 0.5em; color: rgba(232, 168, 74, 0.7); }
.notes {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text);
  min-height: 1.4em;
  overflow-wrap: anywhere;
}
.notes.idle { color: var(--faint); }
.status-row {
  display: flex; flex-wrap: wrap; gap: 6px 14px;
  padding-top: 8px;
  border-top: 1px solid rgba(243, 236, 220, 0.07);
  font-family: var(--font-mono); font-size: 11px; color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.status-row b { color: var(--text); font-weight: 500; }
.pedal-lamp { display: inline-flex; align-items: center; gap: 6px; }
.pedal-lamp::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--faint); }
.pedal-lamp.on { color: var(--amber); }
.pedal-lamp.on::before { background: var(--amber); box-shadow: 0 0 8px var(--amber-glow); }

/* ------------------------------------------------------------------- Rack */
.rack {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}
.module {
  background: var(--panel);
  border: 1px solid var(--panel-edge);
  border-radius: var(--radius);
  padding: 12px 14px 14px;
  display: flex; flex-direction: column; gap: 10px;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  min-width: 0;
}
.module h2 { margin: 0; font: inherit; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.time {
  font-family: var(--font-mono); font-size: 13px; color: var(--text);
  font-variant-numeric: tabular-nums; margin-inline-start: auto;
}
.meta { font-family: var(--font-mono); font-size: 11px; color: var(--muted); }

/* sliders */
.slider { display: grid; grid-template-columns: 58px 1fr 38px; align-items: center; gap: 10px; }
.slider label { font-size: 12.5px; color: var(--muted); }
.slider output { font-family: var(--font-mono); font-size: 12px; text-align: right; color: var(--text); font-variant-numeric: tabular-nums; }

input[type="range"] {
  --val: 50%;
  appearance: none; -webkit-appearance: none;
  width: 100%; height: 22px; margin: 0; background: transparent; cursor: pointer;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 4px; border-radius: 2px;
  background: linear-gradient(90deg, var(--amber) var(--val), rgba(243, 236, 220, 0.14) var(--val));
}
input[type="range"]::-moz-range-track { height: 4px; border-radius: 2px; background: rgba(243, 236, 220, 0.14); }
input[type="range"]::-moz-range-progress { height: 4px; border-radius: 2px; background: var(--amber); }
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 16px; height: 16px; margin-top: -6px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #fff8ea, #cdbf9f 70%);
  border: 1px solid rgba(0, 0, 0, 0.5); box-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
}
input[type="range"]::-moz-range-thumb {
  width: 16px; height: 16px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #fff8ea, #cdbf9f 70%);
  border: 1px solid rgba(0, 0, 0, 0.5); box-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
}
input[type="range"]:focus-visible { outline-offset: 4px; }

.octave-val {
  font-family: var(--font-mono); font-size: 14px; min-width: 64px; text-align: center;
  color: var(--ivory); font-variant-numeric: tabular-nums;
}

.bpm {
  width: 64px; min-height: 36px;
  font: 500 14px var(--font-mono);
  color: var(--ivory);
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid rgba(243, 236, 220, 0.16);
  border-radius: 8px;
  padding: 0 8px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}
.beats { display: flex; gap: 6px; margin-inline-start: auto; }
.beats span {
  width: 10px; height: 10px; border-radius: 50%;
  background: #262b36; border: 1px solid rgba(243, 236, 220, 0.08);
}
.beats span.on { background: var(--teal); box-shadow: 0 0 10px var(--teal); }
.beats span.on.accent { background: var(--amber); box-shadow: 0 0 10px var(--amber-glow); }

/* ------------------------------------------------------------- Instrument */
.instrument { display: flex; flex-direction: column; justify-content: flex-end; gap: 10px; min-height: 0; }

.case {
  position: relative;
  border-radius: 14px 14px 10px 10px;
  padding: 14px clamp(8px, 1.4vw, 18px) clamp(10px, 1.6vw, 18px);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.07) 0, rgba(255, 255, 255, 0) 2px),
    linear-gradient(180deg, #1a1d24 0%, #0c0e12 38%, #07080b 100%);
  border: 1px solid rgba(243, 236, 220, 0.08);
  box-shadow:
    0 30px 60px rgba(0, 0, 0, 0.55),
    0 0 0 1px rgba(0, 0, 0, 0.6),
    inset 0 1px 0 rgba(255, 255, 255, 0.06);
}
.case-top {
  display: flex; justify-content: space-between; align-items: center;
  padding: 0 4px 10px;
  font-family: var(--font-mono); font-size: 10.5px; letter-spacing: 0.18em; text-transform: uppercase;
  color: var(--faint);
}
.case-top .maker { font-family: var(--font-display); font-style: italic; letter-spacing: 0.3em; color: rgba(232, 168, 74, 0.55); font-size: 12px; }

.felt {
  height: 7px;
  border-radius: 2px 2px 0 0;
  background: linear-gradient(180deg, #6b2233, #3e1320);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.08), 0 2px 3px rgba(0, 0, 0, 0.6);
  position: relative; z-index: 3;
}

.keybed {
  --wcount: 22;
  --ww: calc(100% / var(--wcount));
  position: relative;
  display: flex;
  height: clamp(170px, 34vh, 300px);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  cursor: pointer;
  background: #050506;
}

.key {
  position: relative;
  display: flex; flex-direction: column; justify-content: flex-end; align-items: center;
  gap: 4px;
  font-family: var(--font-mono);
  transition: transform 70ms ease-out, box-shadow 70ms ease-out, background 90ms ease-out;
}
.key.white {
  flex: 1 1 0;
  margin-right: 1px;
  border-radius: 0 0 6px 6px;
  padding-bottom: 14px;
  background: linear-gradient(180deg, #d8cfbd 0%, #f4eee1 5%, #f3ecdc 62%, #eae1cd 92%, #d9ccb3 100%);
  box-shadow:
    inset 0 -7px 0 #d3c5aa,
    inset -1px 0 0 rgba(0, 0, 0, 0.14),
    inset 1px 0 0 rgba(255, 255, 255, 0.6),
    0 4px 5px rgba(0, 0, 0, 0.45);
  color: #6f6552;
  z-index: 1;
}
.key.white:last-child { margin-right: 0; }
.key.black {
  position: absolute;
  top: 0;
  left: calc(var(--ww) * var(--x) - var(--ww) * 0.29);
  width: calc(var(--ww) * 0.58);
  height: 62%;
  border-radius: 0 0 4px 4px;
  padding-bottom: 12px;
  background: linear-gradient(180deg, #0c0d10 0%, #1d2025 70%, #34373e 84%, #16181c 86%, #0d0e11 100%);
  box-shadow:
    inset 1px 0 0 rgba(255, 255, 255, 0.07),
    inset -1px 0 0 rgba(0, 0, 0, 0.6),
    2px 5px 6px rgba(0, 0, 0, 0.55);
  color: rgba(243, 236, 220, 0.55);
  z-index: 2;
}

/* pressed */
.key.white.down {
  transform: translateY(2px);
  background: linear-gradient(180deg, #c9bfa9 0%, #ebe3d2 6%, #efe4cd 62%, #ecd9b4 100%);
  box-shadow:
    inset 0 -3px 0 #cdb991,
    inset -1px 0 0 rgba(0, 0, 0, 0.18),
    inset 0 8px 14px rgba(0, 0, 0, 0.12),
    0 1px 2px rgba(0, 0, 0, 0.4);
}
.key.black.down {
  transform: translateY(2px);
  background: linear-gradient(180deg, #08090b 0%, #181a1f 78%, #2b2e34 90%, #121317 92%, #0b0c0e 100%);
  box-shadow: inset 1px 0 0 rgba(255, 255, 255, 0.05), 1px 2px 3px rgba(0, 0, 0, 0.6);
}
.key::after {
  content: "";
  position: absolute;
  left: 18%; right: 18%; bottom: 3px;
  height: 3px; border-radius: 2px;
  background: var(--amber);
  box-shadow: 0 0 10px var(--amber-glow);
  opacity: 0;
  transition: opacity 120ms;
}
.key.down::after { opacity: 1; }
.key.sustained::after { opacity: 0.55; background: var(--teal); box-shadow: 0 0 8px var(--teal); }

.key .kb, .key .nm { pointer-events: none; line-height: 1; }
.key .kb { font-size: clamp(9px, 0.95vw, 12px); font-weight: 500; text-transform: uppercase; opacity: 0.9; display: flex; flex-direction: column; align-items: center; gap: 2px; }
.key .kb i { font-style: normal; opacity: 0.55; font-size: 0.85em; }
.key .nm { font-size: clamp(8px, 0.8vw, 10.5px); opacity: 0; }
.key.white .nm { color: #9a8a6c; }
.key.c .nm { opacity: 0.85; color: #8a6a36; }
.keybed.names .key .nm { opacity: 0.85; }
.keybed.names .key.black .nm { color: rgba(243, 236, 220, 0.7); }
.keybed.no-hints .key .kb { display: none; }

.hint {
  margin: 0;
  font-size: 12px;
  color: var(--muted);
  display: flex; flex-wrap: wrap; gap: 4px 16px;
}
.hint kbd, .help kbd {
  font-family: var(--font-mono); font-size: 11px;
  padding: 1px 6px; border-radius: 4px;
  background: rgba(243, 236, 220, 0.08); border: 1px solid rgba(243, 236, 220, 0.14);
  color: var(--text);
}

/* --------------------------------------------------------- Notation switch */
.seg { display: inline-flex; border: 1px solid rgba(243, 236, 220, 0.14); border-radius: 8px; padding: 2px; gap: 2px; }
.seg .btn { border: 0; min-height: 30px; padding: 6px 10px; background: transparent; }
.seg .btn[aria-checked="true"] { background: var(--amber-soft); color: var(--amber); }
.seg .btn[lang="ar"], .btn.lang[lang="ar"] { font-family: "IBM Plex Sans Arabic", var(--font-body); font-size: 14px; }

/* ------------------------------------------------------------------ Arabic */
/* Arabic letters must join, so no tracking; small mono labels move to the Arabic sans */
html[lang="ar"] body { font-size: 15px; }
html[lang="ar"] .eyebrow,
html[lang="ar"] .brand-sub,
html[lang="ar"] .case-top span:last-child,
html[lang="ar"] .meta,
html[lang="ar"] .status-row,
html[lang="ar"] .notes,
html[lang="ar"] .audio-status {
  letter-spacing: 0;
  font-family: "IBM Plex Sans Arabic", var(--font-body);
}
html[lang="ar"] .eyebrow { font-size: 13px; font-weight: 500; }
html[lang="ar"] .meta, html[lang="ar"] .audio-status { font-size: 12.5px; }
html[lang="ar"] .notes { font-size: 15px; }
html[lang="ar"] .help h2 { font-family: var(--font-arabic-display); font-style: normal; font-size: 30px; }
html[lang="ar"] .hint { font-size: 13px; }
/* sliders fill from the right in right-to-left layouts */
[dir="rtl"] input[type="range"]::-webkit-slider-runnable-track {
  background: linear-gradient(270deg, var(--amber) var(--val), rgba(243, 236, 220, 0.14) var(--val));
}
/* the instrument itself always reads low to high, left to right */
.case, .keybed { direction: ltr; }

/* solfège names: Arabic words, upright, a little larger */
.solfege .chord { font-family: var(--font-arabic-display); font-style: normal; font-weight: 700; }
.solfege .chord .q { font-weight: 400; }
.keybed.solfege .key .nm { font-family: "IBM Plex Sans Arabic", var(--font-body); font-size: clamp(9px, 0.95vw, 12px); font-weight: 500; }
.keybed.solfege .key.black .nm { font-size: clamp(8px, 0.8vw, 10px); }
.key .nm { white-space: nowrap; }

/* ------------------------------------------------------------------- Help */
.help {
  width: min(560px, calc(100% - 32px));
  max-height: calc(100% - 48px);
  overflow: auto;
  color: var(--text);
  background: #0e131d;
  border: 1px solid rgba(232, 168, 74, 0.22);
  border-radius: 14px;
  padding: 22px 24px;
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.7);
}
.help::backdrop { background: rgba(4, 6, 10, 0.7); }
.help h2 { font-family: var(--font-display); font-style: italic; font-weight: 500; font-size: 28px; margin: 0 0 12px; color: var(--ivory); }
.help dl { display: grid; grid-template-columns: max-content 1fr; gap: 8px 16px; margin: 0 0 16px; }
.help dt { color: var(--amber); font-family: var(--font-mono); font-size: 12px; padding-top: 1px; }
.help dd { margin: 0; color: var(--text); }
.help p { margin: 0 0 16px; color: var(--muted); max-width: 62ch; }
.help .row { justify-content: flex-end; }

/* ------------------------------------------------------------ Responsive */
@media (max-width: 900px) {
  .console { grid-template-columns: 1fr; }
  .readout { min-height: 0; grid-template-rows: auto auto auto auto; }
}
@media (max-width: 560px) {
  .brand-sub { display: none; }
  /* keyboard first on phones: readout, keys, then the rack */
  .console { display: contents; }
  .readout { order: 1; }
  .instrument { order: 2; }
  .rack { order: 3; grid-template-columns: 1fr; }
  .readout { padding: 12px 14px; gap: 4px; }
  .chord { font-size: 36px; }
  .keybed { height: clamp(150px, 30vh, 230px); }
  .key.white { padding-bottom: 10px; }
  .help dl { grid-template-columns: 1fr; gap: 2px 0; }
  .help dd { margin-bottom: 8px; }
}
@media (hover: none) and (pointer: coarse) {
  .keybed .key .kb { display: none; }
  .hint .kbd-only { display: none; }
}
@media (orientation: landscape) and (max-height: 500px) {
  .console { grid-template-columns: 1fr 1.6fr; }
  .readout { min-height: 0; }
  .chord { font-size: 34px; }
  .keybed { height: 46vh; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
  .key.down { transform: none !important; }
}
