/* MidiGen — mobile-first. Dark by default, light when the OS asks. */

:root {
  --bg: #12111a;
  --panel: #1c1b26;
  --panel-2: #24222f;
  --line: #322f40;
  --ink: #eceaf4;
  --dim: #9c98b0;
  --accent: #7c5cff;
  --accent-2: #35d6c0;
  --rest: #6b6880;
  --danger: #ff5c7a;
  --radius: 14px;
  --tap: 46px;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f6f5fb; --panel: #ffffff; --panel-2: #f0eef8; --line: #e0dcee;
    --ink: #1b1926; --dim: #6d6884; --rest: #9a96ad;
  }
}

* { box-sizing: border-box; }

/* Class rules like `.field { display: flex }` outrank the UA's [hidden] rule,
   which silently defeats every `el.hidden = true` in the app. Make it stick. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: contain;
}

body {
  padding-bottom: env(safe-area-inset-bottom);
  min-height: 100dvh;
}

/* ── top bar ───────────────────────────────────────────────────────────── */

.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 10px 14px calc(10px + env(safe-area-inset-top)) 14px;
  padding-top: calc(10px + env(safe-area-inset-top));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}

.topbar h1 { margin: 0; font-size: 19px; letter-spacing: -0.4px; font-weight: 700; }
.topbar h1 span { color: var(--accent); }

.tabs { display: flex; gap: 4px; background: var(--panel-2); padding: 3px; border-radius: 999px; }
.tab {
  border: 0; background: transparent; color: var(--dim);
  padding: 7px 14px; border-radius: 999px; font-size: 13px; font-weight: 600;
  cursor: pointer; min-height: 34px;
}
.tab.active { background: var(--accent); color: #fff; }

main { padding: 14px; max-width: 780px; margin: 0 auto; }

.view { display: none; }
.view.active { display: block; }

/* ── panels & fields ───────────────────────────────────────────────────── */

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 14px;
}

.field { display: flex; flex-direction: column; gap: 6px; flex: 1; min-width: 0; }
.field > span { font-size: 12px; color: var(--dim); font-weight: 600; letter-spacing: 0.3px; }
.field.small { flex: 0 1 auto; }

input[type="text"], select {
  width: 100%; min-height: var(--tap);
  background: var(--panel-2); color: var(--ink);
  border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 12px; font-size: 16px; /* 16px: stops iOS zoom-on-focus */
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  letter-spacing: 2px; text-transform: uppercase;
}
select { font-family: inherit; letter-spacing: 0; text-transform: none; }
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

input[type="range"] { width: 100%; accent-color: var(--accent); min-height: 34px; }

.row { display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap; margin-top: 12px; }
.row.buttons { align-items: stretch; }
.row.buttons button { flex: 1 1 auto; }

/* ── buttons ───────────────────────────────────────────────────────────── */

button {
  font: inherit; cursor: pointer;
  min-height: var(--tap);
  border-radius: 10px;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

button.primary {
  background: var(--accent); color: #fff; border: 0;
  font-weight: 700; padding: 0 20px;
}
button.primary.big { font-size: 17px; min-height: 54px; }
button.ghost {
  background: var(--panel-2); color: var(--ink);
  border: 1px solid var(--line); font-weight: 600; padding: 0 16px;
}
button:active { transform: translateY(1px); }
button:disabled { opacity: 0.45; pointer-events: none; }

/* ── keyboard ──────────────────────────────────────────────────────────── */

.kb-head, .buffer-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px; margin-bottom: 10px;
}
.kb-label { font-size: 12px; color: var(--dim); font-weight: 600; letter-spacing: 0.3px; }

.octave-ctl { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--dim); }
.octave-ctl b { color: var(--ink); font-size: 14px; }
.octave-ctl button {
  width: 34px; min-height: 34px; padding: 0;
  background: var(--panel-2); border: 1px solid var(--line); color: var(--ink);
  font-size: 16px; line-height: 1;
}

.keyboard {
  position: relative;
  display: flex;
  height: 168px;
  touch-action: none;          /* a keyboard must not scroll the page mid-glissando */
  user-select: none; -webkit-user-select: none;
}

.keyboard .key {
  position: relative;
  flex: 1;
  display: flex; align-items: flex-end; justify-content: center;
  padding-bottom: 8px;
  border: 1px solid var(--line);
  border-radius: 0 0 7px 7px;
  background: linear-gradient(180deg, #fdfdff 0%, #eeecf6 100%);
  color: #2a2836;
  font-size: 12px; font-weight: 700;
  cursor: pointer;
}
.keyboard .key + .key { margin-left: -1px; }
.keyboard .key.black {
  position: absolute; top: 0;
  width: 9.4%; height: 62%;
  margin: 0; z-index: 2;
  background: linear-gradient(180deg, #3b3850 0%, #1b1a26 100%);
  color: #cfc9e6;
  border-color: #15141d;
  border-radius: 0 0 6px 6px;
  font-size: 10px;
}
.keyboard .key.down {
  background: linear-gradient(180deg, var(--accent) 0%, #5b3fe0 100%);
  color: #fff; border-color: var(--accent);
}

.kb-tools { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.kb-tools button { flex: 1 1 auto; min-width: 64px; font-size: 13px; }
.kb-tools button.loading { opacity: .6; pointer-events: none; }

/* Record: ghost when idle, a live red pulse while capturing. */
.record {
  flex: 1 1 100% !important;
  background: var(--panel-2); color: var(--danger);
  border: 1px solid var(--line); font-weight: 700;
}
.record.on {
  background: var(--danger); color: #fff; border-color: var(--danger);
  animation: rec-pulse 1.1s ease-in-out infinite;
}
@keyframes rec-pulse {
  0%, 100% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--danger) 55%, transparent); }
  50%      { box-shadow: 0 0 0 7px color-mix(in srgb, var(--danger) 0%, transparent); }
}

/* Hum: ghost when idle, a live teal pulse while listening. */
.hum {
  flex: 1 1 100% !important;
  background: var(--panel-2); color: var(--accent-2);
  border: 1px solid var(--line); font-weight: 700;
}
.hum.on {
  background: var(--accent-2); color: #06231f; border-color: var(--accent-2);
  animation: rec-pulse 1.1s ease-in-out infinite;
}

.hint { margin: 10px 2px 0; font-size: 12px; color: var(--dim); line-height: 1.5; }

/* ── buffer ────────────────────────────────────────────────────────────── */

.buffer-count { font-size: 12px; color: var(--dim); font-variant-numeric: tabular-nums; }

.buffer {
  display: flex; flex-wrap: wrap; gap: 6px;
  min-height: 44px; align-content: flex-start;
  padding: 8px; border-radius: 10px;
  background: var(--panel-2); border: 1px solid var(--line);
}
.buffer:empty::after {
  content: 'press a key…';
  color: var(--dim); font-size: 13px; align-self: center; padding: 4px;
}
.buffer .chip {
  display: inline-flex; align-items: center; gap: 2px;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 8px; padding: 5px 9px; min-height: 32px;
  font-family: ui-monospace, Menlo, monospace;
  font-size: 14px; font-weight: 700; color: var(--ink);
  cursor: pointer;
}
.buffer .chip.rest { color: var(--rest); }
.buffer .chip .rest-mark { font-size: 11px; font-weight: 700; letter-spacing: .5px; }
.buffer .chip sup { font-size: 10px; color: var(--accent); }
.buffer .chip sub { font-size: 10px; color: var(--dim); }
.buffer .chip:active { border-color: var(--danger); color: var(--danger); }
.buffer .chip .dur {
  font-size: 10px; font-weight: 700; color: var(--accent-2);
  background: color-mix(in srgb, var(--accent-2) 14%, transparent);
  border-radius: 5px; padding: 1px 4px; margin-left: 2px;
}
.buffer .chip.captured .dur { color: var(--accent); background: color-mix(in srgb, var(--accent) 16%, transparent); }

/* Slim "drop a rest here" targets between buffer chips. */
.buffer .ins {
  align-self: stretch; width: 22px; min-height: 32px; padding: 0;
  background: transparent; color: var(--dim);
  border: 1px dashed var(--line); border-radius: 6px;
  font-size: 13px; font-weight: 700; opacity: .55;
}
.buffer .ins:active {
  opacity: 1; color: var(--accent); border-color: var(--accent);
  border-style: solid;
  background: color-mix(in srgb, var(--accent) 12%, transparent);
}

/* ── cards ─────────────────────────────────────────────────────────────── */

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(112px, 1fr));
  gap: 10px;
  margin-bottom: 14px;
}

.card {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 8px 8px;
  text-align: center;
  transition: border-color .12s, box-shadow .12s, transform .12s;
}
.card.playing {
  border-color: var(--accent-2);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent-2) 40%, transparent);
  transform: translateY(-2px);
}
.card.rest .note { color: var(--rest); }

.card .del {
  position: absolute; top: 2px; right: 2px;
  width: 28px; min-height: 28px; padding: 0;
  background: transparent; border: 0; color: var(--dim); font-size: 15px; line-height: 1;
}
.card .del:hover { color: var(--danger); }

.card .note {
  font-size: 30px; font-weight: 700; letter-spacing: -1px;
  font-family: ui-monospace, Menlo, monospace;
  margin: 2px 0 6px;
}

/* pitch nudge: ▲ / ▼ flanking the note, tap to move a semitone + hear it */
.card .pitch { display: flex; align-items: center; justify-content: center; gap: 4px; }
.card .pitch .note { margin: 2px 0 4px; min-width: 46px; }
.card .pitch button {
  width: 30px; min-height: 30px; padding: 0; line-height: 1; font-size: 13px;
  background: var(--panel-2); border: 1px solid var(--line); color: var(--ink);
}
.card .pitch button:active { background: var(--accent-2); border-color: var(--accent-2); color: #06231f; }
.card .note sup { font-size: 14px; font-weight: 600; color: var(--accent); letter-spacing: 0; }
.card .note sub { font-size: 12px; font-weight: 500; color: var(--dim); letter-spacing: 0; }

.card .stepper { display: flex; align-items: center; gap: 2px; }
.card .stepper button {
  flex: 0 0 30px; min-height: 30px; padding: 0;
  background: var(--panel-2); border: 1px solid var(--line); color: var(--ink);
  font-size: 15px; line-height: 1;
}
.card .stepper .val {
  flex: 1; font-size: 12px; color: var(--dim); font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* Per-card "insert rest after" — subtle, at the card foot. */
.card .add-rest {
  width: 100%; min-height: 26px; margin-top: 6px; padding: 0;
  background: transparent; color: var(--dim);
  border: 1px dashed var(--line); border-radius: 7px;
  font-size: 12px; font-weight: 700;
}
.card .add-rest:active {
  color: var(--accent); border-color: var(--accent); border-style: solid;
  background: color-mix(in srgb, var(--accent) 12%, transparent);
}

/* Leading full-width insert bar spanning the whole card grid. */
.card-ins.lead {
  grid-column: 1 / -1;
  min-height: 30px; padding: 0;
  background: transparent; color: var(--dim);
  border: 1px dashed var(--line); border-radius: 9px;
  font-size: 12px; font-weight: 700;
}
.card-ins.lead:active {
  color: var(--accent); border-color: var(--accent); border-style: solid;
  background: color-mix(in srgb, var(--accent) 12%, transparent);
}

/* 2x2 so every modifier stays a real touch target, not a sliver. */
.card .mods {
  display: grid; grid-template-columns: 1fr 1fr; gap: 4px; margin-bottom: 6px;
}
.card .mods button {
  min-height: 32px; padding: 0; font-size: 13px; font-weight: 700;
  background: var(--panel-2); border: 1px solid var(--line); color: var(--dim);
  border-radius: 8px;
}
.card .mods button.on { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ── history ───────────────────────────────────────────────────────────── */

.history { display: flex; flex-direction: column; gap: 10px; }

.hitem {
  display: flex; align-items: center; gap: 10px;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 12px;
}
.hitem .meta { flex: 1; min-width: 0; }
.hitem .name {
  font-weight: 700; font-family: ui-monospace, Menlo, monospace;
  letter-spacing: 1px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.hitem .sub { font-size: 12px; color: var(--dim); margin-top: 2px; }
.hitem button { min-width: 44px; padding: 0 10px; }
.hitem .del { color: var(--danger); }

/* ── misc ──────────────────────────────────────────────────────────────── */

.empty {
  color: var(--dim); text-align: center; padding: 34px 16px;
  font-size: 14px; line-height: 1.7;
}

.controls { position: sticky; bottom: 0; z-index: 10; }

/* Sits at the TOP: the sticky transport lives at the bottom and a toast down
   there covered Play/Save exactly when the user was reaching for them. */
.toast {
  position: fixed; left: 50%; top: calc(64px + env(safe-area-inset-top));
  transform: translate(-50%, -12px);
  background: var(--ink); color: var(--bg);
  padding: 10px 18px; border-radius: 999px; font-size: 14px; font-weight: 600;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
  z-index: 100; max-width: 90vw;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .28);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
