:root {
  --bg: #0e1116; --bg2: #161b22; --bg3: #1f2630; --line: #2a3340; --fg: #e8ecf2; --dim: #9aa3b2;
  --accent: #ffd23f; --accent-fg: #1a1400; --pink: #ff3b6b; --teal: #2fb8a4; --err: #ff6b6b;
  --r: 10px; --safe-b: env(safe-area-inset-bottom, 0px); --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg); font: 14px/1.4 Rubik, system-ui, -apple-system, "Segoe UI", sans-serif; overflow: hidden; overscroll-behavior: none; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }

/* loading */
#loading { position: fixed; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; background: var(--bg); z-index: 50; padding: 24px; text-align: center; }
#loading .bar { width: min(280px, 70vw); }
.fatal { color: var(--err); max-width: 420px; }
.spinner { width: 36px; height: 36px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.bar { height: 6px; background: var(--bg3); border-radius: 3px; overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width 0.3s; }

/* header */
#top { height: calc(48px + var(--safe-t)); padding: var(--safe-t) 8px 0; display: flex; align-items: center; gap: 6px; background: var(--bg2); border-bottom: 1px solid var(--line); }
#title { flex: 1; min-width: 0; background: transparent; border: 0; padding: 6px 8px; border-radius: 8px; font-weight: 600; }
#title:focus { outline: none; background: var(--bg3); }
#save { font-size: 12px; color: var(--dim); white-space: nowrap; }
#save[data-state="offline"], #save[data-state="conflict"], #save[data-state="error"] { color: var(--err); }
#save[data-state="saving"], #save[data-state="pending"] { color: var(--accent); }
.icon { background: transparent; border: 0; min-width: 36px; height: 36px; border-radius: 8px; font-size: 18px; }
.icon:hover:not(:disabled) { background: var(--bg3); }
.icon:disabled { opacity: 0.35; }
.icon.big { font-size: 20px; min-width: 44px; background: var(--bg3); }
.btn { background: var(--bg3); border: 1px solid var(--line); border-radius: 999px; padding: 8px 14px; font-weight: 600; white-space: nowrap; }
.btn:hover { border-color: #3a4556; }
.btn.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.btn.danger { color: var(--err); }

/* layout: phone first */
#main { display: flex; flex-direction: column; height: calc(100% - 48px - var(--safe-t)); }
#left { display: flex; flex-direction: column; flex: 1; min-height: 0; }
#stage { flex: 1; min-height: 140px; overflow: hidden; display: flex; align-items: center; justify-content: center; padding: 8px; position: relative; }
#frame { position: relative; background: #000; border-radius: 6px; overflow: hidden; box-shadow: 0 8px 30px rgba(0,0,0,0.5); }
#frame canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
#stage-hit { position: absolute; inset: 0; }
#section { position: absolute; left: 8px; top: 8px; font-size: 10px; font-weight: 800; letter-spacing: 0.08em; padding: 3px 7px; border-radius: 6px; background: rgba(0,0,0,0.55); }
#section:empty { display: none; }
#section[data-kind="drop"] { background: var(--pink); }
#section[data-kind="build"] { background: #b07a1f; }
#beat { position: absolute; right: 10px; top: 10px; width: 10px; height: 10px; border-radius: 50%; background: var(--accent); opacity: 0; }
#beat.on { animation: beat 0.25s ease-out; }
@keyframes beat { from { opacity: 1; transform: scale(1.6); } to { opacity: 0; transform: scale(1); } }
#transport { display: flex; align-items: center; gap: 4px; padding: 4px 8px; }
#time { font-variant-numeric: tabular-nums; color: var(--dim); font-size: 12px; margin: 0 auto 0 6px; }
.snap { font-size: 12px; color: var(--dim); display: flex; gap: 4px; align-items: center; }
select { background: var(--bg3); border: 1px solid var(--line); border-radius: 8px; padding: 5px 8px; }
#tl { width: 100%; height: 190px; display: block; background: var(--bg2); border-top: 1px solid var(--line); flex: none; }
#right { flex: none; display: flex; flex-direction: column; background: var(--bg); border-top: 1px solid var(--line); height: 34vh; min-height: 200px; }
#tools { display: flex; overflow-x: auto; scrollbar-width: none; gap: 2px; padding: 4px 6px; border-bottom: 1px solid var(--line); flex: none; }
#tools::-webkit-scrollbar { display: none; }
#tools button { flex: none; display: flex; flex-direction: column; align-items: center; gap: 1px; background: transparent; border: 0; border-radius: 8px; padding: 5px 10px; font-size: 16px; color: var(--dim); }
#tools button span { font-size: 10px; }
#tools button.on { color: var(--accent); background: var(--bg3); }
#panel { overflow-y: auto; padding: 10px 12px calc(12px + var(--safe-b)); flex: 1; min-height: 120px; }

/* desktop */
@media (min-width: 900px) {
  #main { flex-direction: row; }
  #right { width: 380px; height: auto; max-height: none; border-top: 0; border-left: 1px solid var(--line); }
  #tl { height: 210px; }
}

/* panels */
.panel { display: flex; flex-direction: column; gap: 10px; }
.panel h4 { margin: 6px 0 0; font-size: 11px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--dim); }
.hint { color: var(--dim); margin: 0; font-size: 13px; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.lbl { color: var(--dim); font-size: 12px; min-width: 56px; }
.len { min-width: 72px; text-align: center; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { background: var(--bg3); border: 1px solid var(--line); border-radius: 999px; padding: 6px 11px; font-size: 12px; }
.chip.on { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); font-weight: 700; }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(82px, 1fr)); gap: 8px; }
.tile { background: var(--bg2); border: 1px solid var(--line); border-radius: var(--r); padding: 10px 6px; display: flex; flex-direction: column; align-items: center; gap: 6px; font-size: 11px; text-align: center; }
.tile.on { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.tile-ico { width: 40px; height: 40px; border-radius: 8px; background: var(--bg3); display: grid; place-items: center; font-size: 20px; }
.tiles.styles { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
.tile.style { align-items: flex-start; text-align: left; padding: 12px; }
.tile.style small { color: var(--dim); }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.slider { display: grid; grid-template-columns: 90px 1fr 56px; align-items: center; gap: 8px; }
.slider .val { text-align: right; font-variant-numeric: tabular-nums; color: var(--dim); font-size: 12px; }
input[type=range] { width: 100%; accent-color: var(--accent); }
.toggle { display: flex; align-items: center; gap: 8px; }
.text-input { width: 100%; background: var(--bg2); border: 1px solid var(--line); border-radius: var(--r); padding: 10px; resize: vertical; font-size: 16px; }
.swatches { display: flex; gap: 6px; }
.sw { width: 26px; height: 26px; border-radius: 50%; border: 2px solid var(--line); }
.sw.on { border-color: var(--accent); }
.effect { background: var(--bg2); border: 1px solid var(--line); border-radius: var(--r); padding: 10px; display: flex; flex-direction: column; gap: 8px; }
.effect .row { justify-content: space-between; }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.stat { background: var(--bg2); border-radius: var(--r); padding: 8px; display: flex; flex-direction: column; }
.stat small { color: var(--dim); font-size: 11px; }
.media-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(72px, 1fr)); gap: 6px; }
.media { position: relative; aspect-ratio: 1; border: 0; padding: 0; border-radius: 8px; overflow: hidden; background: var(--bg3); }
.media img { width: 100%; height: 100%; object-fit: cover; display: block; }
.media.used::after { content: '✓'; position: absolute; right: 4px; top: 4px; background: var(--accent); color: var(--accent-fg); border-radius: 50%; width: 18px; height: 18px; font-size: 11px; display: grid; place-items: center; }
.media .badge { position: absolute; left: 4px; bottom: 4px; font-size: 10px; background: rgba(0,0,0,0.6); border-radius: 4px; padding: 1px 4px; }
.hist { display: flex; flex-direction: column; gap: 6px; }
.rev { display: flex; justify-content: space-between; align-items: center; background: var(--bg2); border-radius: var(--r); padding: 8px 10px; }
.rev small { display: block; color: var(--dim); }
.cur { color: var(--accent); font-size: 12px; font-weight: 700; }

/* overlays */
.toast { position: fixed; left: 50%; bottom: calc(20px + var(--safe-b)); transform: translate(-50%, 20px); opacity: 0; background: #2a3340; padding: 10px 16px; border-radius: 999px; z-index: 80; transition: 0.25s; max-width: 90vw; text-align: center; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { background: #5a2530; }
.modal-back { position: fixed; inset: 0; background: rgba(0,0,0,0.6); display: grid; place-items: center; z-index: 70; padding: 16px; }
.modal { background: var(--bg2); border: 1px solid var(--line); border-radius: 16px; padding: 18px; width: min(440px, 100%); display: flex; flex-direction: column; gap: 10px; }
.modal h2 { margin: 0; font-size: 18px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; margin-top: 6px; }
.busy { position: fixed; inset: 0; background: rgba(14,17,22,0.85); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; z-index: 60; }
body:not(.ready) #top, body:not(.ready) #main { visibility: hidden; }
@media (min-width: 900px) { #tools { flex-wrap: wrap; overflow: visible; } }
details.more { border: 1px solid var(--line); border-radius: var(--r); padding: 0 10px; }
details.more > summary { cursor: pointer; padding: 10px 0; color: var(--dim); font-weight: 600; list-style: none; }
details.more > summary::before { content: '▸ '; }
details.more[open] > summary::before { content: '▾ '; }
details.more > .panel { padding-bottom: 10px; }
