:root {
  --bg: #1E1E1F; --panel: #2A2A2C; --edge: #3A3A3D; --text: #EDE6D6; --sub: #A49E92; --accent: #F07E26; --on-accent: #1E1E1F;
  --step-off: #3B3B3E; --readout-bg: #3B3B3E; --readout-text: #EDE6D6; --trim: #3A3A3D;
  --safe-l: env(safe-area-inset-left); --safe-r: env(safe-area-inset-right); --safe-t: env(safe-area-inset-top); --safe-b: env(safe-area-inset-bottom);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overscroll-behavior: none; }
body { -webkit-tap-highlight-color: transparent; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
input, textarea, select, pre { -webkit-user-select: text; user-select: text; }
#app { height: 100%; display: flex; padding: var(--safe-t) var(--safe-r) 0 var(--safe-l); }
body.wood #app { border-left: 12px solid var(--trim); border-right: 12px solid var(--trim); }
button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { opacity: .4; cursor: default; }
h1, h2, h3 { margin: 0; }
small { color: var(--sub); }

/* ---------- common controls ---------- */
.icon { background: none; border: 0; min-width: 40px; height: 40px; font-size: 20px; border-radius: 20px; position: relative; color: var(--text); }
.icon:active { background: color-mix(in srgb, var(--text) 12%, transparent); }
.icon.star.on, .star.on { color: var(--accent); }
.badge { position: absolute; top: 2px; right: 0; background: var(--accent); color: var(--on-accent); font-size: 11px; font-weight: 700; border-radius: 9px; min-width: 18px; height: 18px; line-height: 18px; padding: 0 4px; }
.chip { background: transparent; border: 1px solid var(--edge); border-radius: 8px; height: 34px; padding: 0 12px; white-space: nowrap; color: var(--text); font-size: 14px; }
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--on-accent); font-weight: 600; }
.chip-mini { font-size: 10px; border: 1px solid var(--edge); border-radius: 4px; padding: 0 4px; margin-left: 6px; vertical-align: middle; color: var(--sub); }
button.text { background: none; border: 0; color: var(--accent); font-weight: 600; padding: 8px 10px; border-radius: 8px; white-space: nowrap; }
button.text:active { background: color-mix(in srgb, var(--accent) 15%, transparent); }
button.filled { background: var(--accent); color: var(--on-accent); border: 0; border-radius: 10px; padding: 10px 16px; font-weight: 600; }
.danger { color: #E5484D !important; }
button.filled.danger { background: #D93A3F; color: #fff !important; }
input, select, textarea { font: inherit; font-size: 16px; color: var(--text); background: color-mix(in srgb, var(--panel) 70%, var(--bg)); border: 1px solid var(--edge); border-radius: 10px; padding: 10px 12px; width: 100%; }
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 0; }
input[type=range] { padding: 0; accent-color: var(--accent); background: none; border: 0; }
input[type=checkbox] { width: auto; accent-color: var(--accent); }
.spacer { flex: 1; }
.hint { color: var(--sub); font-size: 13px; line-height: 1.4; }
.hint.pad, .error.pad { padding: 16px; }
.center { text-align: center; }
.error { color: #E5484D; font-size: 14px; min-height: 1em; margin: 4px 0 0; }

/* hardware buttons */
.hw { position: relative; border: 0; padding: 0; background: none; flex: none; }
.hw canvas { position: absolute; inset: 0; }
.hw-label { position: relative; display: flex; align-items: center; justify-content: center; width: 100%; height: 100%; font-weight: 700; font-size: 18px; line-height: 1; pointer-events: none; }
.hw.small .hw-label { font-size: 11px; }

/* ---------- screens ---------- */
.screen { flex: 1; display: flex; flex-direction: column; min-width: 0; height: 100%; }
.topbar { display: flex; align-items: center; gap: 2px; padding: 6px 6px 6px 12px; min-height: 52px; }
.app-title { font-size: 20px; font-weight: 800; letter-spacing: .02em; padding-left: 4px; }
.sync-dot { width: 9px; height: 9px; border-radius: 5px; margin: 0 8px; background: var(--sub); opacity: .7; }
.sync-dot.ok { background: #2DBE6C; opacity: 1; } .sync-dot.syncing { background: var(--accent); }
.sync-dot.offline { background: transparent; border: 2px solid var(--sub); } .sync-dot.error, .sync-dot.signedout { background: #E5484D; opacity: 1; }

/* auth */
.auth { flex: 1; display: flex; align-items: center; justify-content: center; padding: 24px; }
.auth-box { width: 100%; max-width: 360px; }
.auth-box .logo { font-size: 34px; font-weight: 900; letter-spacing: .02em; color: var(--accent); margin-bottom: 18px; }
.auth-box h1 { font-size: 20px; margin-bottom: 14px; }
.auth-box form { display: flex; flex-direction: column; gap: 10px; }
.auth-box button.filled { margin-top: 6px; padding: 12px; }

/* library */
.lib-head { padding: 0 12px; }
.search { margin-bottom: 8px; }
.chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 6px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.count { font-size: 12px; color: var(--sub); padding: 2px 4px 6px; }
.lib-list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding-bottom: calc(80px + var(--safe-b)); }
.lib-row { display: flex; align-items: center; gap: 8px; padding: 10px 8px 10px 16px; border-bottom: 1px solid color-mix(in srgb, var(--edge) 60%, transparent); cursor: pointer; }
.lib-row:active { background: color-mix(in srgb, var(--text) 6%, transparent); }
.lib-row.current { background: color-mix(in srgb, var(--accent) 16%, transparent); }
.lib-row.selected { background: color-mix(in srgb, var(--accent) 22%, transparent); }
.lib-main { flex: 1; min-width: 0; }
.lib-title { font-weight: 700; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lib-sub { font-size: 13px; color: var(--sub); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lib-meta { font-size: 12px; color: var(--sub); opacity: .85; margin-top: 2px; }
.lib-row .star { background: none; border: 0; font-size: 22px; width: 44px; height: 44px; color: var(--sub); }
.lib-row .star.on { color: var(--accent); }
.check { font-size: 20px; color: var(--accent); }
.row-actions { display: flex; gap: 4px; align-items: center; }
.sel-bar { display: none; position: sticky; bottom: 0; gap: 4px; align-items: center; padding: 8px 12px calc(8px + var(--safe-b)); background: var(--panel); border-top: 1px solid var(--edge); }
.sel-bar.show { display: flex; } .sel-bar span { flex: 1; font-weight: 600; }
.bpm-filter { padding: 10px; display: flex; flex-direction: column; gap: 8px; min-width: 240px; }
.bpm-filter .row { display: flex; align-items: center; gap: 8px; } .bpm-filter input { width: 90px; }

/* two panes on tablets */
.two-pane { flex: 1; display: flex; min-width: 0; height: 100%; }
.two-pane .left { width: 360px; flex: none; display: flex; border-right: 1px solid var(--edge); }
.two-pane .right { flex: 1; display: flex; min-width: 0; }
.empty { flex: 1; display: flex; align-items: center; justify-content: center; color: var(--sub); }

/* pattern screen */
.p-header { display: flex; align-items: center; gap: 6px; padding: 6px 8px; flex-wrap: wrap; row-gap: 6px; }
.p-title { flex: 1 1 140px; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding-left: 4px; }
.p-title b { font-size: 17px; } .p-title span { color: var(--sub); font-size: 13px; }
.p-controls { display: flex; align-items: center; gap: 6px; flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; max-width: 100%; }
.p-controls::-webkit-scrollbar { display: none; }
.edit-tools { display: flex; align-items: center; overflow-x: auto; flex: 1; min-width: 0; scrollbar-width: none; }
.edit-tools::-webkit-scrollbar { display: none; }
.part-chip span { display: inline-block; max-width: 140px; overflow: hidden; text-overflow: ellipsis; vertical-align: bottom; }
.readout { height: 40px; min-width: 52px; padding: 0 8px; border-radius: 6px; border: 1px solid var(--edge); background: var(--readout-bg); color: var(--readout-text); display: inline-flex; flex-direction: column; align-items: center; justify-content: center; flex: none; }
.readout b { font-size: 15px; line-height: 16px; font-variant-numeric: tabular-nums; }
.readout small { font-size: 8px; line-height: 10px; letter-spacing: .08em; color: inherit; opacity: .75; }
.stepper { display: flex; align-items: center; gap: 8px; padding: 10px; }
.stepper .readout { min-width: 64px; }
.hw.big .hw-label { font-size: 22px; }
.grid-wrap { position: relative; flex: 1; margin: 2px 6px calc(4px + var(--safe-b)); overflow-y: auto; -webkit-overflow-scrolling: touch; border-radius: 10px; }
.panel-bg { position: absolute; left: 0; top: 0; pointer-events: none; }
.grid { position: relative; padding: 12px 14px 10px; }
.bar-label { font-size: 11px; color: var(--sub); padding: 6px 2px 2px; }
.t-row { display: flex; align-items: center; padding: 3px 0; }
.t-label { width: 104px; flex: none; display: flex; align-items: center; gap: 3px; padding-right: 6px; }
.t-name { flex: 1; min-width: 0; font-weight: 700; font-size: 13px; letter-spacing: -.01em; height: 32px; display: flex; align-items: center; cursor: pointer; overflow: hidden; }
.t-name.muted { opacity: .4; }
canvas.steps { flex: 1; min-width: 0; width: 100%; display: block; touch-action: pan-y; cursor: pointer; }
.help { font-size: 11px; color: var(--sub); margin: 0 14px calc(8px + var(--safe-b)); }

/* settings / admin */
.screen.simple { overflow: hidden; }
.settings { flex: 1; overflow-y: auto; padding: 4px 16px calc(40px + var(--safe-b)); max-width: 760px; width: 100%; margin: 0 auto; }
.settings section { padding: 14px 0; border-bottom: 1px solid color-mix(in srgb, var(--edge) 60%, transparent); display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.settings h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--sub); }
.skins { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; width: 100%; }
.skin { border: 2px solid; border-radius: 10px; padding: 10px; text-align: left; font-weight: 600; display: flex; flex-direction: column; gap: 6px; }
.skin.on { box-shadow: 0 0 0 2px var(--accent); }
.sw { display: flex; gap: 3px; } .sw i { width: 18px; height: 10px; border-radius: 2px; display: block; }
.kv-row { display: flex; align-items: center; gap: 6px; width: 100%; flex-wrap: wrap; min-height: 44px; }
.kv-row > span { flex: 1; min-width: 160px; }
.seg { display: inline-flex; flex-wrap: wrap; border: 1px solid var(--edge); border-radius: 10px; overflow: hidden; }
.seg button { border: 0; background: none; padding: 9px 12px; min-width: 40px; color: var(--text); }
.seg button.on { background: var(--accent); color: var(--on-accent); font-weight: 700; }
pre.format { white-space: pre-wrap; font-size: 13px; line-height: 1.45; background: color-mix(in srgb, var(--panel) 70%, var(--bg)); padding: 14px; border-radius: 10px; border: 1px solid var(--edge); }
.actions.left { justify-content: flex-start; }

/* menus and dialogs */
.scrim { position: fixed; inset: 0; z-index: 50; }
.popover { position: fixed; z-index: 51; background: var(--panel); color: var(--text); border: 1px solid var(--edge); border-radius: 12px; box-shadow: 0 10px 30px rgba(0,0,0,.35); min-width: 200px; max-width: calc(100vw - 12px); max-height: 70vh; overflow-y: auto; padding: 6px 0; }
.menu-item { display: block; width: 100%; text-align: left; background: none; border: 0; padding: 11px 16px; font-size: 15px; color: var(--text); }
.menu-item:active, .menu-item:hover { background: color-mix(in srgb, var(--text) 8%, transparent); }
.menu-item.bold { font-weight: 800; color: var(--accent); }
.popover hr { border: 0; border-top: 1px solid var(--edge); margin: 4px 0; }
.menu-head { padding: 8px 16px; display: flex; flex-direction: column; gap: 4px; min-width: 240px; }
.menu-head span { font-size: 12px; color: var(--sub); }
.dialog-wrap { position: fixed; inset: 0; z-index: 60; background: rgba(0,0,0,.45); display: flex; align-items: center; justify-content: center; padding: 16px; }
.dialog { background: var(--panel); color: var(--text); border-radius: 16px; padding: 18px; width: 100%; max-width: 440px; max-height: calc(100vh - 32px); overflow-y: auto; box-shadow: 0 16px 40px rgba(0,0,0,.4); }
.dialog h2 { font-size: 18px; margin-bottom: 12px; }
.dialog p { line-height: 1.45; }
.form { display: flex; flex-direction: column; gap: 10px; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--sub); }
.field-label { font-size: 13px; color: var(--sub); margin-top: 10px; }
.actions { display: flex; align-items: center; justify-content: flex-end; gap: 6px; margin-top: 14px; flex-wrap: wrap; }
.check-row { display: flex; align-items: center; gap: 6px; margin-top: 12px; font-size: 15px; }
.step-dialog .seg { margin-top: 4px; }
.send-list { display: flex; flex-direction: column; max-height: 50vh; overflow-y: auto; }
input.link { font-size: 13px; }
#toast { position: fixed; left: 50%; bottom: calc(18px + var(--safe-b)); transform: translate(-50%, 140%); transition: transform .2s; z-index: 70; background: #222; color: #fff; padding: 10px 14px; border-radius: 10px; display: flex; gap: 10px; align-items: center; max-width: calc(100vw - 24px); box-shadow: 0 6px 20px rgba(0,0,0,.35); font-size: 14px; }
#toast.show { transform: translate(-50%, 0); }
#toast button.text { color: #FFB27A; }

@media (max-width: 520px) {
  .p-title { flex-basis: calc(100% - 60px); }
  .p-controls { flex-wrap: wrap; row-gap: 6px; overflow: visible; }
  .t-label { width: 96px; }
  .t-name { font-size: 12px; }
}
