:root {
  --bg: #05091e; --bg-rgb: 5 9 30;
  --surface: #080e26; --surface-2: #0d1633;
  --panel: rgb(8 14 38 / 0.96);
  --ink: #f4f0df; --ink-2: #c5c8d7; --ink-3: #7f879f;
  --tint: 124 151 197;
  --line: rgb(var(--tint) / 0.16);
  --fill-1: rgb(var(--tint) / 0.035); --fill-2: rgb(var(--tint) / 0.075); --fill-3: rgb(var(--tint) / 0.12);
  --axis: #8294ad;
  --cyan: #24bbdf; --indigo: #5b98e8; --violet: #9e7be8; --magenta: #d281db; --pink: #ef8fbb;
  --orange: #f6a13a; --green: #4acb91;
  --error: #ff8fb5;
  --grad: linear-gradient(90deg, var(--cyan) 0%, #5bcce1 30%, var(--green) 72%, var(--orange) 100%);
  --shadow: 0 18px 60px rgb(0 0 0 / 0.38);
  --toast-bg: #ffffff; --toast-ink: #030722;
  --font-brand: "DM Serif Display", Georgia, serif;
  --font-ui: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-math: "DM Mono", "JetBrains Mono", ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --gutter: 16px;
  color-scheme: dark;
}
:root[data-theme="light"] {
  --bg: #f3f4fb; --bg-rgb: 243 244 251;
  --surface: #ffffff; --surface-2: #f8f8fe;
  --panel: rgb(255 255 255 / 0.97);
  --ink: #0b1033; --ink-2: #3d4472; --ink-3: #6a7099;
  --tint: 40 50 140;
  --axis: #5b6290;
  --error: #c2185b;
  --shadow: 0 10px 34px rgb(30 40 120 / 0.14);
  --toast-bg: #0b1033; --toast-ink: #ffffff;
  color-scheme: light;
}

* { -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; color: var(--ink); font-family: var(--font-ui); }
html {
  background:
    linear-gradient(rgba(124, 151, 197, .07) 1px, transparent 1px),
    linear-gradient(90deg, rgba(124, 151, 197, .07) 1px, transparent 1px),
    radial-gradient(circle at 72% 25%, rgba(27, 93, 138, .13), transparent 35%),
    var(--bg);
  background-size: 72px 72px, 72px 72px, auto, auto;
}
body { overflow: hidden; overscroll-behavior: none; }
:focus-visible { outline: 2px solid var(--magenta); outline-offset: 2px; }
.grad-text { background: linear-gradient(90deg, var(--cyan), #7addca, var(--orange)); -webkit-background-clip: text; background-clip: text; color: transparent; }
math { font-family: "Latin Modern Math", "STIX Two Math", "Cambria Math", math; }

/* ── layout ── */
.app {
  height: 100%; display: grid; min-height: 0;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto auto auto auto minmax(160px, 1fr) auto auto auto;
  grid-template-areas: "head" "mode" "formula" "keys" "graph" "range" "presets" "dock";
  padding: 0 env(safe-area-inset-right, 0px) 0 env(safe-area-inset-left, 0px);
}
.top { grid-area: head; }
.side { display: contents; }
.seg { grid-area: mode; }
.formula { grid-area: formula; }
.keys { grid-area: keys; }
.range { grid-area: range; }
.presets-wrap { grid-area: presets; }
.graph-wrap { grid-area: graph; }
.dock { grid-area: dock; }

/* ── header ── */
.top {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: calc(16px + env(safe-area-inset-top, 0px)) var(--gutter) 12px;
  border-bottom: 1px solid var(--line);
  background: rgb(5 9 30 / .64);
  backdrop-filter: blur(18px);
}
.brand { display: flex; align-items: center; gap: 9px; text-decoration: none; color: var(--ink); min-width: 0; border-radius: 12px; }
.brand img { width: 30px; height: 30px; display: block; }
.brand b { font-family: var(--font-brand); font-weight: 400; font-size: 23px; white-space: nowrap; }
.brand .tag {
  font-family: var(--font-math); font-size: 10px; font-weight: 500; letter-spacing: 0.14em; color: var(--ink-2);
  border: 1px solid var(--line); border-radius: 999px; padding: 2px 7px;
}
@media (max-width: 400px) { .brand .tag { display: none; } .brand b { font-size: 17px; } }
.top-actions { display: flex; gap: 6px; flex: none; }
.language-btn {
  height: 40px; padding: 0 13px; border: 1px solid var(--line); border-radius: 999px;
  background: var(--fill-1); color: var(--ink-2); font: 600 11px/1 var(--font-math);
  letter-spacing: .05em;
}
.language-btn:hover { background: var(--fill-3); color: var(--ink); }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%; border: 0; background: var(--fill-2);
  display: grid; place-items: center; color: var(--ink); flex: none; transition: background 0.2s, border-color .2s;
  border: 1px solid var(--line);
}
.icon-btn:hover { background: var(--fill-3); }
.icon-btn svg { width: 20px; height: 20px; }
#btn-theme .ic-sun, #btn-theme[aria-pressed="true"] .ic-moon { display: none; }
#btn-theme[aria-pressed="true"] .ic-sun { display: block; }

/* ── mode switch ── */
.seg {
  display: grid; grid-template-columns: 1fr 1fr; gap: 4px; padding: 4px; border: 1px solid var(--line); border-radius: 3px; background: var(--fill-1);
  margin: 4px var(--gutter) 8px;
}
.seg button, .mini-seg button {
  border: 0; background: transparent; color: var(--ink-2); font-weight: 700; font-size: 14px; padding: 8px 10px; border-radius: 10px;
  transition: background 0.2s, color 0.2s;
}
.seg button[aria-checked="true"], .mini-seg button[aria-checked="true"] { background: var(--surface); color: var(--ink); box-shadow: 0 1px 6px rgb(0 0 0 / 0.18); }
.mini-seg { display: inline-flex; gap: 2px; padding: 3px; border-radius: 10px; background: var(--fill-2); }
.mini-seg button { font-size: 13px; padding: 5px 12px; border-radius: 8px; }

/* ── formula field ── */
.formula {
  display: flex; align-items: center; gap: 8px; margin: 0 var(--gutter); padding: 0 6px 0 14px; height: 64px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 3px; transition: border-color 0.2s, box-shadow 0.2s;
}
.formula:focus-within { border-color: var(--cyan); box-shadow: 0 0 0 3px rgb(36 187 223 / 0.1); }
.formula.bad { border-color: var(--error); }
.lhs { font-size: 22px; color: var(--ink-3); white-space: nowrap; cursor: text; }
.f-box { position: relative; flex: 1; min-width: 0; align-self: stretch; display: flex; align-items: center; }
.f-in {
  width: 100%; min-width: 0; border: 0; background: transparent; outline: none; padding: 14px 0;
  font-family: var(--font-math); font-weight: 600; font-size: 19px; color: var(--ink);
}
.f-view {
  position: absolute; inset: 0; display: flex; align-items: center; pointer-events: none; overflow: hidden;
  font-size: 24px; color: var(--ink); white-space: nowrap; background: var(--surface);
}
.f-view:empty, .formula:focus-within .f-view, .formula.bad .f-view { display: none; }
.f-view.long { mask-image: linear-gradient(to right, #000 88%, transparent); }
.clear { width: 36px; height: 36px; flex: none; border: 0; border-radius: 50%; background: transparent; color: var(--ink-3); display: grid; place-items: center; }
.clear:hover { background: var(--fill-2); color: var(--ink); }
.clear svg { width: 18px; height: 18px; }

/* ── math keys ── */
.keys {
  display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; padding: 8px var(--gutter) 2px;
  mask-image: linear-gradient(to right, #000 88%, transparent);
}
.keys::-webkit-scrollbar { display: none; }
.key {
  flex: none; min-width: 42px; height: 36px; padding: 0 10px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--fill-1); color: var(--ink); font-family: var(--font-math); font-weight: 600; font-size: 15px;
  transition: background 0.15s, transform 0.1s;
}
.key:hover { background: var(--fill-3); }
.key:active { transform: scale(0.94); }

/* ── range ── */
.range {
  display: flex; align-items: center; justify-content: center; gap: 8px; padding: 8px var(--gutter) 2px;
  color: var(--ink-3); font-size: 14px;
}
.range .var { font-family: "Latin Modern Math", "STIX Two Math", math; font-style: italic; font-size: 20px; color: var(--ink-2); }
.r-in {
  width: 84px; height: 36px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface);
  text-align: center; font-family: var(--font-math); font-weight: 600; font-size: 15px; outline: none;
}
.r-in:focus { border-color: var(--magenta); }
.r-in.bad { border-color: var(--error); color: var(--error); }

/* ── presets ── */
.presets-wrap { padding: 8px 0 0; min-width: 0; }
.label { margin: 0 var(--gutter) 6px; font-size: 12px; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; color: var(--ink-3); }
.presets {
  display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; padding: 0 var(--gutter) 2px;
  mask-image: linear-gradient(to right, #000 88%, transparent);
}
.presets::-webkit-scrollbar { display: none; }
.chip {
  flex: none; height: 34px; padding: 0 14px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--fill-1); color: var(--ink-2); font-weight: 600; font-size: 13px; white-space: nowrap;
  transition: background 0.2s, color 0.2s, border-color 0.2s;
}
.chip:hover { background: var(--fill-3); color: var(--ink); }
.chip[aria-pressed="true"] { color: var(--ink); border-color: transparent; background: linear-gradient(var(--surface-2), var(--surface-2)) padding-box, var(--grad) border-box; border: 1.5px solid transparent; }

/* ── graph ── */
.graph-wrap {
  position: relative; min-height: 0; margin: 10px var(--gutter) 0; border-radius: 3px;
  background:
    linear-gradient(rgba(124, 151, 197, .055) 1px, transparent 1px),
    linear-gradient(90deg, rgba(124, 151, 197, .055) 1px, transparent 1px),
    radial-gradient(circle at 48% 42%, rgba(29, 95, 134, .11), transparent 40%),
    rgb(5 9 30 / .48);
  background-size: 44px 44px, 44px 44px, auto, auto;
  border: 1px solid var(--line); overflow: hidden;
}
.graph { position: absolute; inset: 0; touch-action: none; cursor: crosshair; }
.graph canvas { display: block; }
.readout-l, .readout-r {
  position: absolute; top: 10px; font-family: var(--font-math); font-size: 12.5px; font-weight: 600; color: var(--ink-2);
  background: rgb(var(--bg-rgb) / 0.7); backdrop-filter: blur(6px); padding: 4px 9px; border-radius: 8px; pointer-events: none;
  transition: opacity 0.2s;
}
.readout-l { left: 10px; }
.readout-r { right: 10px; }
.readout-l:empty, .readout-r:empty { opacity: 0; }
.g-msg, .g-hint {
  position: absolute; left: 50%; transform: translateX(-50%); margin: 0; pointer-events: none; text-align: center;
  font-size: 13px; padding: 6px 12px; border-radius: 999px; background: rgb(var(--bg-rgb) / 0.75); max-width: calc(100% - 24px);
}
.g-msg { top: 50%; translate: 0 -50%; color: var(--error); font-weight: 600; }
.g-msg:empty { display: none; }
.g-hint { bottom: 10px; color: var(--ink-3); transition: opacity 0.4s; }
.g-hint.gone { opacity: 0; }

/* ── dock ── */
.dock {
  display: flex; align-items: center; justify-content: space-around; gap: 4px;
  padding: 8px var(--gutter) calc(10px + env(safe-area-inset-bottom, 0px));
}
.act {
  display: flex; flex-direction: column; align-items: center; gap: 3px; border: 0; background: none; color: var(--ink-2);
  font-size: 11.5px; font-weight: 600; min-width: 58px; padding: 6px 4px; border-radius: 12px; text-decoration: none;
}
.act:hover { color: var(--ink); background: var(--fill-1); }
.act svg { width: 24px; height: 24px; }
.act[aria-pressed="true"], .act[aria-expanded="true"] { color: var(--magenta); }
.kick {
  width: 66px; height: 66px; border-radius: 50%; border: 0; color: #fff; display: grid; place-items: center; flex: none;
  background: linear-gradient(135deg, var(--cyan), var(--green)); box-shadow: 0 8px 28px rgb(36 187 223 / 0.24); transition: transform 0.15s;
}
.kick:hover { transform: scale(1.05); }
.kick:active { transform: scale(0.95); }
.kick svg { width: 30px; height: 30px; grid-area: 1 / 1; }
.kick .ic-stop, .kick.on .ic-play { display: none; }
.kick.on .ic-stop { display: block; }
.kick.on { animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 50% { box-shadow: 0 8px 40px rgb(74 203 145 / 0.48); } }

/* ── sound settings: a bottom sheet on phones ── */
.settings {
  position: fixed; z-index: 20; left: 0; right: 0; bottom: 0; padding: 14px var(--gutter) calc(20px + env(safe-area-inset-bottom, 0px));
  background: var(--panel); border-top: 1px solid var(--line); border-radius: 22px 22px 0 0; box-shadow: var(--shadow);
  transform: translateY(105%); transition: transform 0.3s cubic-bezier(0.2, 0.8, 0.2, 1); visibility: hidden;
}
.settings.open { transform: none; visibility: visible; }
.panel-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.panel-head h2 { margin: 0; font-family: var(--font-brand); font-size: 26px; font-weight: 400; }
.field { display: block; margin: 12px 0; }
.field .row { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 8px; }
.field output { font-family: var(--font-math); color: var(--ink-2); }
.field small { color: var(--ink-3); font-size: 12.5px; line-height: 1.4; }
.field input[type="range"] { width: 100%; accent-color: var(--magenta); }
.pitch-note { color: var(--ink-3); font-size: 12.5px; margin: 4px 0 12px; }
.btn {
  width: 100%; display: flex; align-items: center; justify-content: center; gap: 8px; height: 44px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--fill-1); font-weight: 600;
}
.btn:hover { background: var(--fill-3); }
.btn svg { width: 20px; height: 20px; }

/* ── toast ── */
.toast {
  position: fixed; z-index: 30; left: 50%; bottom: 110px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--toast-bg); color: var(--toast-ink); font-weight: 600; font-size: 14px; padding: 10px 16px; border-radius: 999px;
  box-shadow: var(--shadow); transition: opacity 0.25s, transform 0.25s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.noscript { position: fixed; inset: auto 0 50% 0; text-align: center; }

/* ── desktop ── */
@media (min-width: 900px) {
  .app {
    grid-template-columns: 410px minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr) auto;
    grid-template-areas: "head head" "side graph" "side dock";
  }
  .side {
    grid-area: side; display: flex; flex-direction: column; min-height: 0; overflow-y: auto;
    padding: 4px 0 22px; border-right: 1px solid var(--line); background:
      radial-gradient(circle at 50% 0, rgba(36, 187, 223, .08), transparent 33%),
      rgb(5 9 30 / .58);
  }
  .seg {
    margin: 16px var(--gutter) 12px;
    padding: 5px;
    background: linear-gradient(135deg, rgba(15, 30, 58, .8), rgba(8, 14, 38, .86));
  }
  .seg button { min-height: 40px; }
  .formula {
    height: 72px;
    margin-bottom: 4px;
    box-shadow: inset 0 0 30px rgba(36, 187, 223, .025);
  }
  .lhs { font-size: 24px; }
  .f-in { font-size: 20px; }
  .keys { flex-wrap: wrap; gap: 7px; overflow: visible; mask-image: none; padding-top: 12px; }
  .key {
    min-width: 44px;
    height: 38px;
    border-color: rgba(124, 151, 197, .2);
    background: linear-gradient(145deg, rgba(124, 151, 197, .06), rgba(36, 187, 223, .025));
  }
  .key:hover { border-color: rgba(36, 187, 223, .4); color: var(--cyan); }
  .range {
    justify-content: flex-start;
    gap: 10px;
    margin: 10px var(--gutter) 0;
    padding: 14px;
    border: 1px solid var(--line);
    border-radius: 3px;
    background: rgba(7, 15, 37, .72);
  }
  .range .var { color: var(--cyan); }
  .presets-wrap { padding-top: 18px; }
  .presets { flex-wrap: wrap; gap: 8px; overflow: visible; mask-image: none; }
  .chip { height: 35px; padding: 0 15px; }
  .chip[aria-pressed="true"] { color: #9fe8f5; border-color: rgba(36, 187, 223, .58); }
  .graph-wrap { margin: 8px var(--gutter) 0 10px; }
  .settings {
    position: static; transform: none; visibility: visible; box-shadow: none; border-radius: 16px;
    margin: 16px var(--gutter) 0; border: 1px solid var(--line); background: linear-gradient(135deg, rgb(13 26 53 / .76), rgb(7 12 33 / .72)); padding: 14px 16px;
  }
  .panel-head { padding-bottom: 10px; border-bottom: 1px solid var(--line); }
  .field { margin: 18px 0; }
  .field b { font-size: 13px; color: var(--ink-2); }
  .btn { height: 46px; border-radius: 3px; color: #b9edf4; }
  .settings .close { display: none; }
  #btn-settings { display: none; }
  .dock {
    justify-content: center;
    gap: 32px;
    margin: 0 var(--gutter) 0 10px;
    border-left: 1px solid var(--line);
    border-right: 1px solid var(--line);
    border-bottom: 1px solid var(--line);
    background: rgba(5, 9, 30, .38);
  }
  .dock .act { min-width: 68px; }
  .kick { width: 72px; height: 72px; }
}
