:root {
  --bg: #0f1116;
  --bg2: #161922;
  --card: #1a1e29;
  --line: #2a3040;
  --text: #eef1f7;
  --muted: #8d95a8;
  --accent: #ff7a45;
  --accent2: #ffb347;
  --mid: #4fc3f7;
  --ok: #46d39a;
  --shadow: 0 10px 30px rgba(0,0,0,.35);
  --radius: 18px;
}
[data-theme="light"] {
  --bg: #f3f4f8;
  --bg2: #e9ebf2;
  --card: #ffffff;
  --line: #dde1ea;
  --text: #171a22;
  --muted: #646c80;
  --shadow: 0 8px 24px rgba(30,40,70,.10);
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: radial-gradient(1200px 600px at 50% -200px, rgba(255,122,69,.13), transparent 70%), var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }

/* ---------- header ---------- */
.top {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 12px;
  max-width: 1100px; margin: 0 auto; padding: 14px 18px;
  padding-top: max(14px, env(safe-area-inset-top));
}
.brand { display: flex; align-items: center; gap: 10px; font-size: 1.25rem; letter-spacing: .2px; }
.brand b { color: var(--accent); }
.top-actions { display: flex; gap: 8px; justify-self: end; }
.maker-logo {
  justify-self: center; display: block; background: #fff; border-radius: 12px; padding: 5px 12px;
  box-shadow: 0 2px 10px rgba(0,0,0,.18); transition: transform .12s, box-shadow .12s; line-height: 0;
}
[data-theme="light"] .maker-logo { box-shadow: 0 0 0 1px var(--line); }
.maker-logo:hover { transform: translateY(-1px); box-shadow: 0 4px 16px rgba(79,195,247,.35); }
.maker-logo img { height: 36px; width: auto; display: block; }
.foot .dev { color: var(--text); }
.foot .dev a { color: var(--accent); font-weight: 600; text-decoration: none; }
.foot .dev a:hover { text-decoration: underline; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--card); display: grid; place-items: center; font-weight: 700;
}
.icon-btn:hover, .chip:hover { border-color: var(--accent); }

main { max-width: 1100px; margin: 0 auto; padding: 0 18px 20px; }

.card {
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow);
}

/* ---------- stage ---------- */
.stage { padding: 22px 20px 18px; text-align: center; position: relative; overflow: hidden; }

.pendulum { position: relative; height: 90px; margin: -6px auto 6px; width: 220px; }
.pendulum .arc {
  position: absolute; left: calc(50% - 91px); top: -89px; width: 182px; height: 182px;
  border: 2px dashed var(--line); border-radius: 50%; clip-path: polygon(0 62%, 100% 62%, 100% 100%, 0 100%);
}
.pendulum .arm {
  position: absolute; left: 50%; top: 2px; width: 3px; height: 82px; margin-left: -1.5px;
  background: linear-gradient(var(--muted), var(--text)); transform-origin: 50% 0; border-radius: 3px;
  will-change: transform;
}
.pendulum .bob {
  position: absolute; bottom: -9px; left: 50%; width: 18px; height: 18px; margin-left: -9px;
  border-radius: 50%; background: var(--accent); box-shadow: 0 0 14px var(--accent);
}

.beats { display: flex; justify-content: center; flex-wrap: wrap; gap: 8px; margin: 4px auto 6px; max-width: 760px; }
.beat {
  width: 46px; height: 64px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--bg2); padding: 6px; display: flex; flex-direction: column-reverse; gap: 4px;
  position: relative; transition: transform .06s, box-shadow .12s;
}
.beat .seg { flex: 1; border-radius: 4px; background: var(--line); transition: background .1s; }
.beat[data-level="1"] .seg:nth-of-type(1) { background: #8a93a8; }
.beat[data-level="3"] .seg { background: color-mix(in srgb, var(--accent) 60%, #5a6378); }
.beat[data-level="2"] .seg:nth-of-type(-n+2) { background: color-mix(in srgb, var(--mid) 55%, #5a6378); }
.beat[data-level="0"]::after {
  content: "×"; position: absolute; inset: 0; display: grid; place-items: center;
  font-size: 1.6rem; color: var(--muted);
}
.beat .num { position: absolute; top: -18px; left: 0; right: 0; font-size: .72rem; color: var(--muted); }
.beats { padding-top: 16px; }
.beat.on { transform: scale(1.08); box-shadow: 0 0 0 2px var(--text), 0 0 18px rgba(255,255,255,.25); }
.beat.on[data-level="3"] { box-shadow: 0 0 0 2px var(--accent), 0 0 22px var(--accent); }
.beat.on[data-level="3"] .seg { background: var(--accent); }
.beat.on[data-level="2"] .seg:nth-of-type(-n+2) { background: var(--mid); }
.beat.on[data-level="1"] .seg:nth-of-type(1) { background: var(--text); }
.beats.muted-bar .beat.on { box-shadow: 0 0 0 2px var(--muted); opacity: .6; }
.beats.many .beat { width: 36px; height: 54px; }

.hint { margin: 2px 0 10px; font-size: .78rem; color: var(--muted); }

.tempo { display: flex; align-items: center; justify-content: center; gap: 18px; }
.round {
  width: 58px; height: 58px; border-radius: 50%; border: 1px solid var(--line);
  background: var(--bg2); font-size: 1.8rem; line-height: 1; user-select: none;
}
.round:active { transform: scale(.94); }
.bpm-wrap { min-width: 200px; }
.bpm {
  width: 210px; text-align: center; font-size: 5.2rem; font-weight: 800; letter-spacing: -2px;
  background: transparent; border: 0; outline: none; padding: 0; line-height: 1;
  font-variant-numeric: tabular-nums; -moz-appearance: textfield;
}
.bpm::-webkit-outer-spin-button, .bpm::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.bpm-label { color: var(--muted); font-size: .95rem; margin-top: 2px; }
#tempoName { color: var(--accent2); font-style: italic; }

.slider { width: 100%; accent-color: var(--accent); height: 28px; }
.slider.big { max-width: 620px; margin: 14px auto 4px; display: block; }

.quick { display: flex; justify-content: center; gap: 8px; margin: 4px 0 12px; }

.controls { display: flex; align-items: center; justify-content: center; gap: 22px; margin: 8px 0 12px; }
.play {
  width: 96px; height: 96px; border-radius: 50%; border: 0;
  background: linear-gradient(145deg, var(--accent2), var(--accent)); color: #fff;
  box-shadow: 0 10px 30px rgba(255,122,69,.45); display: grid; place-items: center;
  transition: transform .08s;
}
.play:active { transform: scale(.95); }
.play svg { width: 46px; height: 46px; }
.play .i-stop { display: none; }
body.playing .play .i-stop { display: block; }
body.playing .play .i-play { display: none; }
body.playing .play { background: linear-gradient(145deg, #ff6a6a, #e0445a); box-shadow: 0 10px 30px rgba(224,68,90,.45); }
.tap {
  width: 76px; height: 76px; border-radius: 22px; border: 1px solid var(--line);
  background: var(--bg2); font-weight: 800; letter-spacing: 1px; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 2px; user-select: none;
}
.tap small { font-weight: 500; font-size: .68rem; color: var(--muted); letter-spacing: 0; min-height: 1em; }
.tap:active, .tap.hit { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 18%, var(--bg2)); }

.status { display: flex; justify-content: center; flex-wrap: wrap; gap: 8px 18px; color: var(--muted); font-size: .9rem; }
.status b { color: var(--text); font-variant-numeric: tabular-nums; }
.pill { padding: 2px 10px; border-radius: 99px; background: var(--bg2); border: 1px solid var(--line); color: var(--text); }

/* ---------- panels ---------- */
.panels { display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px; margin-top: 16px; }
.panel { padding: 16px 18px 18px; }
.panel h2 { margin: 0 0 6px; font-size: 1.05rem; }
.lbl { display: flex; justify-content: space-between; margin: 14px 0 6px; font-size: .85rem; color: var(--muted); }
.lbl b { color: var(--text); font-weight: 600; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--line); background: var(--bg2); border-radius: 10px; padding: 7px 11px;
  font-size: .9rem; line-height: 1.1;
}
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.subdiv .chip { display: flex; flex-direction: column; align-items: center; min-width: 62px; gap: 2px; }
.subdiv .glyph { font-size: 1.25rem; line-height: 1.1; }
.subdiv small { font-size: .7rem; opacity: .85; }

.sig { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.stepper { display: flex; align-items: center; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: var(--bg2); }
.stepper button { width: 40px; height: 42px; border: 0; background: transparent; font-size: 1.3rem; }
.stepper span { min-width: 34px; text-align: center; font-size: 1.4rem; font-weight: 700; }
.slash { font-size: 1.6rem; color: var(--muted); }
select, .panel input[type=number], .save-row input {
  background: var(--bg2); border: 1px solid var(--line); border-radius: 12px; padding: 9px 10px;
}
select { font-size: 1.3rem; font-weight: 700; height: 44px; padding: 0 12px; }
select:focus, input:focus { outline: 2px solid color-mix(in srgb, var(--accent) 60%, transparent); outline-offset: 1px; }

.toggles { display: grid; gap: 10px; }
.switch { display: flex; align-items: center; gap: 10px; cursor: pointer; user-select: none; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch span {
  width: 42px; height: 24px; border-radius: 99px; background: var(--line); position: relative; flex: none;
  transition: background .15s;
}
.switch span::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%;
  background: #fff; transition: transform .15s;
}
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(18px); }
.switch input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.switch em { font-style: normal; }

.train-block { padding: 12px 0; border-bottom: 1px dashed var(--line); }
.train-block:last-child { border-bottom: 0; padding-bottom: 0; }
.train-block .lbl { margin-top: 0; }
.row3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 10px; }
.row3 label { display: grid; gap: 4px; }
.row3 small { color: var(--muted); font-size: .72rem; }
.row3 input { width: 100%; }

.save-row { display: flex; gap: 8px; margin: 10px 0; }
.save-row input { flex: 1; min-width: 0; }
.btn {
  border: 0; border-radius: 12px; padding: 9px 16px; background: var(--accent); color: #fff; font-weight: 600;
}
.presets { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; max-height: 300px; overflow: auto; }
.presets li {
  display: flex; align-items: center; gap: 8px; padding: 8px 10px; border: 1px solid var(--line);
  border-radius: 12px; background: var(--bg2);
}
.presets .p-load { flex: 1; text-align: left; border: 0; background: transparent; padding: 0; min-width: 0; }
.presets .p-name { display: block; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.presets .p-meta { display: block; font-size: .78rem; color: var(--muted); }
.presets .p-del { border: 0; background: transparent; color: var(--muted); font-size: 1.2rem; padding: 2px 6px; }
.presets .p-del:hover { color: #ff6a6a; }

.foot { text-align: center; padding: 10px 18px 30px; color: var(--muted); font-size: .9rem; }
.foot p { margin: 4px 0; }

#flash { position: fixed; inset: 0; pointer-events: none; background: var(--accent); opacity: 0; z-index: 50; }
#flash.go { animation: flash .18s ease-out; }
@keyframes flash { from { opacity: .35; } to { opacity: 0; } }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: .92rem;
  transition: .2s; pointer-events: none; z-index: 60; max-width: 90vw; text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

dialog {
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--card); color: var(--text);
  max-width: 440px; width: calc(100% - 32px); box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(0,0,0,.5); }
dialog h3 { margin-top: 0; }
.keys { width: 100%; border-collapse: collapse; margin-bottom: 10px; }
.keys td { padding: 6px 4px; border-bottom: 1px solid var(--line); }
kbd {
  display: inline-block; padding: 2px 7px; border-radius: 6px; border: 1px solid var(--line);
  background: var(--bg2); font-size: .82rem; font-family: inherit;
}

/* ---------- responsive ---------- */
@media (max-width: 760px) {
  .panels { grid-template-columns: 1fr; }
}
@media (max-width: 560px) {
  .top { grid-template-columns: 1fr auto; row-gap: 10px; }
  .maker-logo { grid-column: 1 / -1; grid-row: 1; }
  .maker-logo img { height: 30px; }
  .brand { font-size: 1.1rem; white-space: nowrap; gap: 8px; }
  .brand img { width: 30px; height: 30px; }
}
@media (max-width: 480px) {
  main { padding: 0 10px 16px; }
  .stage { padding: 16px 10px 14px; }
  .bpm { font-size: 4.2rem; width: 160px; }
  .bpm-wrap { min-width: 150px; }
  .tempo { gap: 10px; }
  .round { width: 52px; height: 52px; }
  .beat { width: 38px; height: 56px; }
  .beats.many .beat { width: 28px; height: 46px; padding: 4px; }
  .controls { gap: 16px; }
  .play { width: 86px; height: 86px; }
  .tap { width: 68px; height: 68px; }
  .pendulum { transform: scale(.8); transform-origin: 50% 0; height: 74px; }
}
@media (prefers-reduced-motion: reduce) {
  #flash.go { animation: none; }
}
