:root {
  --bg: #0f0e0d;
  --panel: #1c1a18;
  --panel-2: #24211e;
  --line: #36312b;
  --ink: #ece3cf;
  --muted: #968c7b;
  --accent: #ff8a2a;
  --accent-soft: rgba(255, 138, 42, 0.16);
  --scope: #8dffb6;
  --learn: #4fc3ff;
  --danger: #e2543a;
  --font: 'Archivo Narrow', 'Arial Narrow', system-ui, sans-serif;
  --mono: 'JetBrains Mono', ui-monospace, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: radial-gradient(ellipse at top, #22201d, var(--bg) 70%);
  color: var(--ink);
  font-family: var(--font);
}

body {
  padding: 24px 16px;
  display: flex;
  justify-content: center;
  user-select: none;
  -webkit-user-select: none;
}

button, select, input {
  font: inherit;
  color: var(--ink);
}

button {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 6px 12px;
  cursor: pointer;
  letter-spacing: 0.04em;
}
button:hover { border-color: var(--muted); }
button:focus-visible, select:focus-visible, input:focus-visible, .knob:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
button.danger { color: #ffb4a6; }
button.danger:hover { border-color: var(--danger); }

kbd {
  font-family: var(--mono);
  font-size: 0.8em;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 3px;
  padding: 0 4px;
  background: var(--panel-2);
}

/* --- Case --------------------------------------------------------------- */

.synth {
  display: flex;
  width: min(1280px, 100%);
  border-radius: 10px;
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.6), 0 2px 0 rgba(255, 255, 255, 0.04) inset;
}

.cheek {
  flex: 0 0 26px;
  background:
    repeating-linear-gradient(92deg, rgba(0, 0, 0, 0.18) 0 2px, transparent 2px 7px),
    linear-gradient(90deg, #5a3218, #7c4722 40%, #5a3218);
}
.cheek.left { border-radius: 10px 0 0 10px; }
.cheek.right { border-radius: 0 10px 10px 0; }

.body {
  flex: 1;
  min-width: 0;
  background: linear-gradient(180deg, #23201d, var(--panel) 30%);
  padding: 16px 18px 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* --- Top bar ------------------------------------------------------------ */

.topbar {
  display: grid;
  grid-template-columns: auto 1fr minmax(220px, 320px) auto;
  gap: 18px;
  align-items: center;
}

.logo {
  font-weight: 700;
  font-size: 30px;
  letter-spacing: 0.22em;
  color: var(--ink);
}
.logo span {
  font-size: 12px;
  letter-spacing: 0.1em;
  color: var(--accent);
  margin-left: 8px;
  vertical-align: super;
}
.logo.big { font-size: 48px; text-align: center; }

.presets {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.presets select, .presets input {
  background: #121110;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 6px 8px;
}
.presets select { min-width: 160px; }
#preset-save { display: flex; gap: 6px; }
#preset-save input { width: 160px; }

.display {
  position: relative;
  height: 64px;
  background: #0a110c;
  border: 1px solid #000;
  border-radius: 6px;
  box-shadow: inset 0 0 18px rgba(0, 0, 0, 0.9), 0 1px 0 rgba(255, 255, 255, 0.05);
  overflow: hidden;
  background-image:
    linear-gradient(rgba(141, 255, 182, 0.06) 1px, transparent 1px),
    linear-gradient(90deg, rgba(141, 255, 182, 0.06) 1px, transparent 1px);
  background-size: 100% 16px, 24px 100%;
}
#scope { width: 100%; height: 100%; display: block; filter: drop-shadow(0 0 3px rgba(141, 255, 182, 0.6)); }
#note-display {
  position: absolute;
  top: 4px;
  right: 8px;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--scope);
  opacity: 0.8;
}

.top-actions {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
}
.top-actions .buttons { display: flex; gap: 6px; }
#midi-status {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--muted);
  max-width: 340px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#midi-status::before {
  content: '';
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  margin-right: 6px;
  background: #4a443c;
  vertical-align: 1px;
}
#midi-status.connected { color: var(--ink); }
#midi-status.connected::before { background: var(--scope); box-shadow: 0 0 6px var(--scope); }

/* --- Modules ------------------------------------------------------------ */

#panel, #perf-panel {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.module {
  flex: 1 1 auto;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 10px 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.module h2 {
  margin: 0 -10px;
  padding: 5px 10px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--muted);
  border-bottom: 1px solid var(--line);
  cursor: help;
}
.module h2::before {
  content: '';
  display: inline-block;
  width: 6px;
  height: 6px;
  background: var(--accent);
  border-radius: 1px;
  margin-right: 8px;
  vertical-align: 1px;
}

.controls {
  display: flex;
  gap: 6px;
  align-items: flex-start;
  justify-content: space-around;
  flex-wrap: wrap;
}

.control {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.control-label {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink);
}

.control-value {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--muted);
  min-height: 1.3em;
  white-space: nowrap;
}

/* Knob */
.knob {
  width: 58px;
  cursor: ns-resize;
  touch-action: none;
  border-radius: 6px;
}
.knob-svg { width: 48px; height: 48px; overflow: visible; }
.knob-track { fill: none; stroke: #3d3730; stroke-width: 3.5; stroke-linecap: round; }
.knob-value { fill: none; stroke: var(--accent); stroke-width: 3.5; stroke-linecap: round; }
.knob-cap {
  fill: #2f2b27;
  stroke: #0d0c0b;
  stroke-width: 1;
  filter: drop-shadow(0 2px 2px rgba(0, 0, 0, 0.6));
}
.knob-pointer { stroke: var(--ink); stroke-width: 2.5; stroke-linecap: round; }
.knob:hover .knob-cap, .knob.dragging .knob-cap { fill: #38332e; }
.knob.dragging .control-value { color: var(--accent); }
.knob.learning { outline: 2px dashed var(--learn); outline-offset: 2px; }
.knob.learning .knob-value { stroke: var(--learn); }

/* Button group */
.segmented { padding-top: 4px; }
.seg-buttons {
  display: flex;
  background: #121110;
  border: 1px solid #0b0a09;
  border-radius: 5px;
  padding: 2px;
  gap: 2px;
}
.seg-btn {
  border: none;
  background: transparent;
  padding: 5px 7px;
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
  border-radius: 3px;
  min-width: 30px;
}
.seg-btn:hover { color: var(--ink); }
.seg-btn.on {
  background: var(--accent);
  color: #1a0f05;
  box-shadow: 0 0 10px rgba(255, 138, 42, 0.35);
}
.wave-icon { width: 22px; height: 18px; display: block; }
.wave-icon path { fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linejoin: round; stroke-linecap: round; }

.env-view {
  width: 100%;
  height: 46px;
  background: #121110;
  border-radius: 4px;
}

/* Tempo/transport */
.module-tempo .controls { justify-content: flex-start; gap: 10px; }
.transport {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: auto;
}
.transport button { padding: 5px 10px; font-weight: 700; font-size: 13px; }
.transport button.on {
  background: var(--accent);
  border-color: var(--accent);
  color: #1a0f05;
}
#play { min-width: 74px; }
.beat-led {
  width: 12px;
  height: 12px;
  margin-left: 4px;
  border-radius: 50%;
  background: #3a2414;
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.8);
}
.beat-led.lit { background: var(--accent); box-shadow: 0 0 10px var(--accent); }
.beat-led.lit.accent { background: #ffd36b; box-shadow: 0 0 12px #ffd36b; }

/* Arpeggio */
.dropdown { padding-top: 4px; }
.dropdown select {
  background: #121110;
  border: 1px solid #0b0a09;
  border-radius: 5px;
  padding: 5px 6px;
  font-size: 13px;
  max-width: 190px;
  cursor: pointer;
}
.module-tempo { flex: 0 0 auto; }
.module-arp { flex: 1 1 640px; }
.pattern-view {
  display: flex;
  flex-wrap: wrap;
  gap: 3px;
}
.step {
  flex: 1 1 0;
  min-width: 22px;
  max-width: 44px;
  height: 24px;
  display: grid;
  place-items: center;
  font-family: var(--mono);
  font-size: 11px;
  border-radius: 3px;
  background: #121110;
  color: var(--muted);
  border: 1px solid #0b0a09;
  transition: background 60ms;
}
.step-next, .step-index { background: #2e2924; color: var(--ink); }
.step-tie { color: #6b6357; }
.step.accent { background: #4a3a2a; color: #ffd8b0; }
.step.now { background: var(--accent); color: #1a0f05; box-shadow: 0 0 8px rgba(255, 138, 42, 0.5); }
.step-rest.now, .step-tie.now { background: #5a3a1e; }

/* Looper */
.module-loop { flex: 1 1 100%; flex-direction: row; flex-wrap: wrap; align-items: stretch; }
.module-loop h2 { flex: 1 1 100%; }
.module-loop .controls { flex: 0 0 auto; align-content: flex-start; }
.loop-area {
  flex: 1 1 420px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
}
.loop-area .transport { margin-top: 0; }
.loop-view-slot { flex: 1; min-height: 110px; display: flex; }
.loop-view {
  width: 100%;
  height: 110px;
  background: #121110;
  border: 1px solid #0b0a09;
  border-radius: 4px;
}
button.rec { color: #ff8a7a; min-width: 150px; }
button.rec.on {
  background: #d93a26;
  border-color: #d93a26;
  color: #fff;
  box-shadow: 0 0 12px rgba(217, 58, 38, 0.6);
  animation: rec-pulse 1s ease-in-out infinite;
}
@keyframes rec-pulse { 50% { box-shadow: 0 0 4px rgba(217, 58, 38, 0.3); } }
button:disabled { opacity: 0.4; cursor: default; }
.loop-status { font-family: var(--mono); font-size: 11px; color: var(--muted); margin-left: 6px; }

/* --- Help bar ----------------------------------------------------------- */

.help-bar {
  min-height: 46px;
  padding: 8px 12px;
  background: #121110;
  border: 1px solid var(--line);
  border-left: 3px solid var(--accent);
  border-radius: 4px;
  font-size: 15px;
  line-height: 1.4;
  color: #cfc6b3;
}
.help-bar strong { color: var(--accent); }

/* --- Keyboard ----------------------------------------------------------- */

.keys-area { display: flex; flex-direction: column; gap: 8px; }
.keys-info { font-size: 13px; color: var(--muted); }
.keys-info strong { color: var(--ink); font-family: var(--mono); }

#keyboard {
  position: relative;
  display: flex;
  height: 170px;
  padding: 0;
  background: #000;
  border-radius: 4px;
  border-top: 6px solid #3b0f0a;
  touch-action: none;
  box-shadow: inset 0 6px 8px rgba(0, 0, 0, 0.6);
}

.key { position: relative; cursor: pointer; }
.key.white {
  flex: 1;
  background: linear-gradient(180deg, #e9e4d8, #fbf8f0 85%, #d8d2c4);
  border-right: 1px solid #9f998c;
  border-radius: 0 0 4px 4px;
}
.key.white:last-child { border-right: none; }
.key.white.active { background: linear-gradient(180deg, #d7cfbe, #f2c08f 90%); }
.key.black {
  position: absolute;
  z-index: 2;
  top: 0;
  left: calc(var(--pos) * 100% / var(--whites));
  width: calc(100% / var(--whites) * 0.6);
  height: 62%;
  transform: translateX(-50%);
  background: linear-gradient(180deg, #111, #2d2a27 92%, #111);
  border-radius: 0 0 3px 3px;
  box-shadow: 0 3px 3px rgba(0, 0, 0, 0.5);
}
.key.black.active { background: linear-gradient(180deg, #111, var(--accent)); }
.key.white.arp { background: linear-gradient(180deg, #d7e8dc, #8dffb6 90%); }
.key.black.arp { background: linear-gradient(180deg, #111, #3fbf78); }

.key-name, .key-hint {
  position: absolute;
  left: 0;
  right: 0;
  text-align: center;
  font-family: var(--mono);
  font-size: 10px;
  pointer-events: none;
}
.key-name { bottom: 6px; color: #8a8478; }
.key-hint { bottom: 22px; color: #b7682a; font-weight: 600; }
.key.black .key-hint { bottom: 8px; color: #e7a46b; }

/* --- Start screen and dialog -------------------------------------------- */

#start-overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  background: rgba(8, 7, 6, 0.85);
  backdrop-filter: blur(6px);
  padding: 16px;
}
#start-overlay[hidden] { display: none; }
.start-card {
  text-align: center;
  max-width: 440px;
  color: var(--muted);
  line-height: 1.5;
}
.start-card p { margin: 8px 0 20px; }
.start-card .small { font-size: 13px; }
#start-button {
  font-size: 20px;
  padding: 14px 40px;
  background: var(--accent);
  color: #1a0f05;
  border: none;
  font-weight: 700;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  box-shadow: 0 0 30px rgba(255, 138, 42, 0.35);
}

#start-button:disabled { opacity: 0.35; cursor: not-allowed; box-shadow: none; }
.file-warning {
  color: var(--ink);
  background: #2a1610;
  border: 1px solid var(--danger);
  border-radius: 6px;
  padding: 12px;
  user-select: text;
}
.file-warning code { font-family: var(--mono); color: var(--scope); }
.file-warning a { color: var(--accent); }

dialog#about {
  max-width: 640px;
  background: var(--panel-2);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 24px 28px;
  line-height: 1.55;
  font-size: 16px;
  user-select: text;
}
dialog#about::backdrop { background: rgba(0, 0, 0, 0.7); }
dialog#about h2 { margin-top: 0; color: var(--accent); letter-spacing: 0.04em; }
dialog#about h3 { margin-bottom: 4px; }
dialog#about li { margin-bottom: 4px; }

/* --- Smaller screens ---------------------------------------------------- */

@media (max-width: 900px) {
  .topbar { grid-template-columns: 1fr; }
  .top-actions { align-items: flex-start; }
  .cheek { display: none; }
  body { padding: 0; }
  .body { padding: 12px 16px; }
  #keyboard { height: 130px; }
}
