:root {
  --bg: #1d1442;
  --bg2: #2a1f5c;
  --panel: #322670;
  --panel2: #3d2f86;
  --ink: #fff8ec;
  --muted: #b9b0e6;
  --accent: #ff6b9a;
  --accent2: #ffc845;
  --mint: #4fe3c1;
  --sky: #5ec8ff;
  --line: rgba(255, 255, 255, 0.12);
  --radius: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink); overscroll-behavior: none; }
body { font-family: 'Fredoka', ui-rounded, system-ui, -apple-system, 'Segoe UI', sans-serif; font-size: 16px; -webkit-user-select: none; user-select: none; }
input, select, textarea, button { font: inherit; color: inherit; }
input, textarea { -webkit-user-select: text; user-select: text; }
button { cursor: pointer; border: 0; background: none; }

#app { display: flex; flex-direction: column; height: 100dvh; height: 100vh; max-width: 1100px; margin: 0 auto; }
@supports (height: 100dvh) { #app { height: 100dvh; } }

/* header */
.top { display: flex; align-items: center; gap: 8px; padding: calc(8px + var(--safe-t)) 12px 6px; }
.brand { display: flex; align-items: center; gap: 6px; font-weight: 700; font-size: 20px; flex: none; }
.brand .logo { width: 34px; height: 34px; border-radius: 11px; display: grid; place-items: center; background: linear-gradient(135deg, var(--accent), var(--accent2)); color: #2a1240; font-size: 22px; box-shadow: 0 3px 0 rgba(0,0,0,.25); }
.title { flex: 1; min-width: 0; background: transparent; border: 0; border-bottom: 2px dashed transparent; font-size: 17px; font-weight: 600; padding: 4px 2px; color: var(--ink); }
.title:focus { outline: none; border-bottom-color: var(--accent2); }
.icon { width: 40px; height: 40px; border-radius: 12px; background: var(--panel); font-size: 18px; flex: none; box-shadow: 0 3px 0 rgba(0,0,0,.25); }
.icon:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(0,0,0,.25); }
.icon.toggle { opacity: .45; }
.icon.toggle.on { opacity: 1; }
@media (max-width: 420px) { .brand .word { display: none; } }

/* transport */
.transport { display: flex; align-items: center; gap: 10px; padding: 4px 12px 10px; }
.play { width: 58px; height: 58px; border-radius: 50%; flex: none; font-size: 24px; color: #2a1240; background: linear-gradient(135deg, var(--mint), var(--sky)); box-shadow: 0 5px 0 #1f7f80, 0 8px 18px rgba(79, 227, 193, .35); transition: transform .08s; }
.play:active { transform: translateY(3px); box-shadow: 0 2px 0 #1f7f80; }
.play.playing { background: linear-gradient(135deg, var(--accent), #ff9d6b); box-shadow: 0 5px 0 #9d2f5a, 0 8px 18px rgba(255, 107, 154, .35); }
.tempo { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.tempo .lbl, .style-btn .lbl { font-size: 12px; color: var(--muted); }
.tempo b { color: var(--ink); }
.style-btn { flex: none; display: flex; flex-direction: column; align-items: flex-start; padding: 6px 12px; border-radius: 14px; background: var(--panel); box-shadow: 0 3px 0 rgba(0,0,0,.25); max-width: 42vw; }
.style-btn b { font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }

input[type=range] { -webkit-appearance: none; appearance: none; width: 100%; height: 26px; background: transparent; margin: 0; }
input[type=range]::-webkit-slider-runnable-track { height: 8px; border-radius: 6px; background: var(--panel2); }
input[type=range]::-moz-range-track { height: 8px; border-radius: 6px; background: var(--panel2); }
input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 24px; height: 24px; margin-top: -8px; border-radius: 50%; background: var(--accent2); box-shadow: 0 2px 0 rgba(0,0,0,.3); }
input[type=range]::-moz-range-thumb { width: 24px; height: 24px; border-radius: 50%; background: var(--accent2); border: 0; }

main { flex: 1; min-height: 0; position: relative; }
.tab { position: absolute; inset: 0; display: none; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 0 12px 16px; }
.tab.active { display: block; }
#tab-melody.active { display: flex; flex-direction: column; padding-bottom: 8px; }

/* toolbar */
.toolbar { display: flex; gap: 8px; align-items: center; padding-bottom: 8px; }
.scroll-x { overflow-x: auto; scrollbar-width: none; flex: none; }
.scroll-x::-webkit-scrollbar { display: none; }
.seg { display: flex; background: var(--panel); border-radius: 12px; padding: 3px; flex: none; box-shadow: 0 3px 0 rgba(0,0,0,.25); }
.seg button { min-width: 38px; height: 36px; padding: 0 10px; border-radius: 9px; font-size: 16px; color: var(--muted); white-space: nowrap; }
.seg button.on { background: var(--accent2); color: #2a1240; font-weight: 600; }
.sel { display: flex; align-items: center; gap: 4px; background: var(--panel); border-radius: 12px; padding: 0 4px 0 10px; height: 42px; flex: none; box-shadow: 0 3px 0 rgba(0,0,0,.25); }
select { background: transparent; border: 0; height: 40px; padding: 0 6px; font-weight: 600; max-width: 46vw; }
select option { background: var(--bg2); color: var(--ink); }
select:focus { outline: none; }

/* piano roll */
.roll-wrap { flex: 1; min-height: 200px; position: relative; border-radius: var(--radius); overflow: hidden; background: #160f33; box-shadow: inset 0 0 0 2px var(--line); }
#roll { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; display: block; }
.roll-hint { position: absolute; left: 56px; right: 10px; bottom: 10px; font-size: 13px; color: var(--muted); background: rgba(22, 15, 51, .85); padding: 8px 10px; border-radius: 10px; pointer-events: none; transition: opacity .5s; }
.roll-hint.gone { opacity: 0; }

/* bottom tabs */
.tabs { display: flex; justify-content: space-around; padding: 6px 6px calc(6px + var(--safe-b)); background: var(--bg2); border-top: 1px solid var(--line); flex: none; }
.tabs button { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 1px; font-size: 12px; color: var(--muted); padding: 4px 0; border-radius: 12px; }
.tabs button span { font-size: 22px; line-height: 1.1; }
.tabs button.on { color: var(--ink); background: var(--panel); }

/* cards and sections */
.card { background: var(--panel); border-radius: var(--radius); padding: 14px; margin: 0 0 12px; box-shadow: 0 4px 0 rgba(0,0,0,.2); }
.card h3 { margin: 0 0 10px; font-size: 17px; display: flex; align-items: center; gap: 8px; }
.card h3 .sub { font-weight: 400; font-size: 13px; color: var(--muted); margin-left: auto; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row + .row { margin-top: 10px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { padding: 8px 12px; border-radius: 999px; background: var(--panel2); color: var(--ink); font-size: 14px; min-height: 36px; box-shadow: 0 2px 0 rgba(0,0,0,.25); }
.chip.on { background: var(--accent2); color: #2a1240; font-weight: 600; }
.chip.ghost { background: transparent; box-shadow: inset 0 0 0 2px var(--panel2); }
.btn { padding: 10px 16px; border-radius: 14px; background: var(--panel2); font-weight: 600; min-height: 44px; box-shadow: 0 3px 0 rgba(0,0,0,.25); }
.btn.primary { background: linear-gradient(135deg, var(--accent), #ff9d6b); color: #2a1240; }
.btn.mint { background: linear-gradient(135deg, var(--mint), var(--sky)); color: #12324a; }
.btn:active, .chip:active { transform: translateY(2px); }
.muted { color: var(--muted); font-size: 13px; }
.field { display: flex; flex-direction: column; gap: 4px; flex: 1; min-width: 120px; }
.field > span { font-size: 12px; color: var(--muted); }
.field select, .field input[type=text], .field input[type=number] { background: var(--panel2); border-radius: 12px; border: 0; height: 42px; padding: 0 10px; width: 100%; max-width: none; }
textarea { width: 100%; min-height: 90px; background: #1b1440; border: 2px solid var(--panel2); border-radius: 12px; padding: 10px; font-family: ui-monospace, 'SF Mono', Menlo, Consolas, monospace; font-size: 15px; line-height: 1.5; resize: vertical; }
textarea:focus, input[type=text]:focus { outline: none; border-color: var(--accent2); }

/* chord bar grid */
.bars { display: grid; grid-template-columns: repeat(auto-fill, minmax(78px, 1fr)); gap: 8px; }
.bar { position: relative; background: var(--panel2); border-radius: 12px; min-height: 58px; padding: 18px 6px 6px; text-align: center; font-weight: 600; font-size: 15px; box-shadow: 0 3px 0 rgba(0,0,0,.25); display: flex; gap: 4px; justify-content: center; align-items: center; flex-wrap: wrap; word-break: break-all; }
.bar .n { position: absolute; top: 3px; left: 7px; font-size: 11px; color: var(--muted); font-weight: 400; }
.bar .c2 { color: var(--accent2); }
.bar.empty { color: var(--muted); font-weight: 400; }
.bar.playing { box-shadow: 0 0 0 3px var(--mint); }

/* strum + drum grids */
.cells { display: grid; gap: 4px; }
.cell { height: 40px; border-radius: 9px; background: #1b1440; font-weight: 700; font-size: 15px; color: var(--muted); }
.cell.beat { background: #241a55; }
.cell.x { background: var(--accent); color: #2a1240; }
.cell.o { background: #8a4a78; color: #fff; }
.cell.D { background: var(--mint); color: #12324a; }
.cell.U { background: var(--sky); color: #12324a; }
.cell.now { outline: 2px solid var(--accent2); }
.drumgrid { display: grid; gap: 4px; align-items: center; }
.drumgrid .lab { font-size: 12px; color: var(--muted); white-space: nowrap; padding-right: 4px; }
.drumgrid .cell { height: 34px; border-radius: 6px; min-width: 0; }
.drumgrid { gap: 3px; }
.drum-scroll { overflow-x: auto; }

/* mixer */
.mixrow { display: grid; grid-template-columns: 88px 1fr 46px; gap: 10px; align-items: center; padding: 6px 0; }
.mixrow .name { font-weight: 600; }
.mute { height: 36px; border-radius: 10px; background: var(--panel2); font-size: 16px; }
.mute.on { background: var(--accent); }

/* lists */
.list { display: flex; flex-direction: column; gap: 8px; }
.item { display: flex; align-items: center; gap: 10px; background: var(--panel2); border-radius: 14px; padding: 10px 12px; box-shadow: 0 3px 0 rgba(0,0,0,.2); text-align: left; width: 100%; }
.item .em { font-size: 24px; flex: none; }
.item .t { flex: 1; min-width: 0; }
.item .t b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .t small { color: var(--muted); }
.item .x { flex: none; width: 34px; height: 34px; border-radius: 10px; background: rgba(0,0,0,.2); }

/* bottom sheet */
.sheet-back { position: fixed; inset: 0; background: rgba(8, 4, 24, .55); z-index: 20; }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; z-index: 21; max-height: 88vh; overflow-y: auto; background: var(--bg2); border-radius: 22px 22px 0 0; padding: 10px 14px calc(16px + var(--safe-b)); box-shadow: 0 -10px 40px rgba(0,0,0,.4); max-width: 720px; margin: 0 auto; animation: up .18s ease-out; }
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
.sheet .grab { width: 44px; height: 5px; border-radius: 3px; background: var(--panel2); margin: 0 auto 10px; }
.sheet h2 { margin: 0 0 12px; font-size: 20px; display: flex; align-items: center; gap: 8px; }
.sheet h2 .close { margin-left: auto; }
.sheet .grid12 { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; }
.sheet .grid12 .chip { padding: 8px 0; text-align: center; }
.chord-input { display: flex; gap: 6px; flex-direction: column; align-items: stretch; }
.chord-input input { width: 100%; height: 48px; border-radius: 12px; border: 2px solid var(--panel2); background: #1b1440; padding: 0 12px; font-size: 20px; font-weight: 600; }
.chord-input .ok { font-size: 14px; color: var(--mint); min-height: 18px; padding-left: 4px; }
.chord-input .bad { color: var(--accent); }
.styles { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.style-card { text-align: left; padding: 12px; border-radius: 16px; background: var(--panel); box-shadow: 0 3px 0 rgba(0,0,0,.25); min-height: 84px; }
.style-card .em { font-size: 26px; display: block; }
.style-card b { display: block; margin-top: 4px; }
.style-card small { color: var(--muted); }
.style-card.on { box-shadow: 0 0 0 3px var(--accent2); }
audio { width: 100%; margin: 6px 0 12px; }

/* busy + toast */
.busy { position: fixed; inset: 0; z-index: 40; background: rgba(8, 4, 24, .6); display: grid; place-items: center; }
.busy[hidden] { display: none; }
.busy-card { background: var(--bg2); padding: 22px 28px; border-radius: 20px; text-align: center; font-weight: 600; }
.spinner { width: 42px; height: 42px; border-radius: 50%; border: 5px solid var(--panel2); border-top-color: var(--accent2); margin: 0 auto 12px; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast { position: fixed; left: 50%; bottom: calc(86px + var(--safe-b)); transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 14px; font-weight: 600; z-index: 50; max-width: 90vw; text-align: center; box-shadow: 0 6px 20px rgba(0,0,0,.4); }
.toast[hidden] { display: none; }
[hidden] { display: none !important; }

@media (min-width: 800px) {
  .tab { padding: 0 20px 20px; }
  .top, .transport { padding-left: 20px; padding-right: 20px; }
  .tabs { border-radius: 18px 18px 0 0; }
}
