:root {
  --bg: #0d0f14;
  --panel: #161a22;
  --panel-2: #1e232d;
  --line: #2a303c;
  --text: #eef1f6;
  --dim: #8c95a6;
  --accent: #3d8bff;
  --red: #ff4d4f;
  --amber: #ffb020;
  --green: #31c46c;
  --purple: #a66bff;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body {
  font: 15px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-user-select: none; user-select: none;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}
button { font: inherit; color: inherit; border: 0; background: none; padding: 0; cursor: pointer; touch-action: manipulation; }
input, textarea { font: inherit; color: var(--text); -webkit-user-select: text; user-select: text; }
.hidden { display: none !important; }
.spacer { height: 12px; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.shrink { min-width: 0; }

#app { min-height: 100%; display: flex; flex-direction: column; }

/* Pairing */
.pair {
  flex: 1; display: flex; flex-direction: column; gap: 18px;
  padding: calc(28px + var(--safe-top)) calc(22px + var(--safe-right)) calc(28px + var(--safe-bottom)) calc(22px + var(--safe-left));
  max-width: 520px; width: 100%; margin: 0 auto;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand img { width: 52px; height: 52px; border-radius: 12px; }
.brand h1 { margin: 0; font-size: 22px; }
.brand p { margin: 2px 0 0; color: var(--dim); font-size: 13px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.card h2 { margin: 0 0 8px; font-size: 15px; }
.card p { margin: 0 0 12px; color: var(--dim); }
.steps { margin: 0; padding-left: 20px; color: var(--dim); }
.steps li { margin: 4px 0; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 46px; padding: 0 18px; border-radius: 12px;
  background: var(--panel-2); border: 1px solid var(--line); font-weight: 600;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--red); }
.btn.wide { width: 100%; }
.btn:active { transform: scale(0.98); }
.row { display: flex; gap: 10px; align-items: center; }
.field {
  width: 100%; min-height: 46px; padding: 10px 12px; border-radius: 12px;
  background: var(--bg); border: 1px solid var(--line); outline: none;
}
.field:focus { border-color: var(--accent); }
.studio-list { display: flex; flex-direction: column; gap: 8px; }
.studio-item { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-radius: 12px; background: var(--panel-2); text-align: left; }
.studio-item .name { flex: 1; font-weight: 600; }
.studio-item .sub { color: var(--dim); font-size: 12px; }
.hint { color: var(--dim); font-size: 13px; }
.install { border-color: #2c4f86; background: #13213a; }

/* Studio */
.top {
  position: sticky; top: 0; z-index: 5;
  padding: calc(10px + var(--safe-top)) calc(14px + var(--safe-right)) 10px calc(14px + var(--safe-left));
  background: rgba(13, 15, 20, 0.92); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--line);
}
.top-row { display: flex; align-items: center; gap: 10px; }
.studio-name { flex: 1; min-width: 0; text-align: left; font-weight: 700; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.studio-name small { display: block; font-weight: 500; font-size: 12px; color: var(--dim); }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--dim); display: inline-block; margin-right: 6px; vertical-align: 1px; }
.dot.on { background: var(--green); box-shadow: 0 0 8px var(--green); }
.dot.wait { background: var(--amber); }
.dot.off { background: var(--red); }
.icon-btn { width: 40px; height: 40px; border-radius: 12px; background: var(--panel-2); display: inline-flex; align-items: center; justify-content: center; font-size: 18px; }

.body {
  flex: 1; display: flex; flex-direction: column; gap: 12px;
  padding: 12px calc(14px + var(--safe-right)) calc(88px + var(--safe-bottom)) calc(14px + var(--safe-left));
  max-width: 900px; width: 100%; margin: 0 auto;
}
.offline .live-area { opacity: 0.45; pointer-events: none; }
.banner { padding: 10px 12px; border-radius: 12px; background: #3a2410; color: #ffd39a; font-size: 13px; }

.preview {
  position: relative; width: 100%; aspect-ratio: 16 / 9; border-radius: var(--radius);
  background: #000 center / contain no-repeat; border: 1px solid var(--line); overflow: hidden;
}
.preview img { width: 100%; height: 100%; object-fit: contain; display: block; }
.preview .stamp {
  position: absolute; right: 8px; bottom: 8px; padding: 3px 8px; border-radius: 8px;
  background: rgba(0, 0, 0, 0.6); color: #cfd6e2; font-size: 11px;
}
.preview .empty { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: var(--dim); font-size: 13px; }
.preview .badges { position: absolute; left: 8px; top: 8px; display: flex; gap: 6px; }
.badge { padding: 3px 8px; border-radius: 8px; font-size: 11px; font-weight: 700; letter-spacing: 0.04em; background: rgba(0, 0, 0, 0.6); }
.badge.rec { background: var(--red); }
.badge.live { background: var(--purple); }
.badge.pause { background: var(--amber); color: #201400; }

.transport { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.tbtn {
  min-height: 64px; border-radius: var(--radius); background: var(--panel); border: 1px solid var(--line);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-weight: 700;
}
.tbtn small { font-weight: 500; color: var(--dim); font-size: 12px; font-variant-numeric: tabular-nums; }
.tbtn.rec.on { background: var(--red); border-color: var(--red); }
.tbtn.rec.on small, .tbtn.live.on small { color: rgba(255, 255, 255, 0.85); }
.tbtn.pause.on { background: var(--amber); border-color: var(--amber); color: #201400; }
.tbtn.pause.on small { color: #4a3300; }
.tbtn.live.on { background: var(--purple); border-color: var(--purple); }
.tbtn:disabled { opacity: 0.4; }

.section-head { display: flex; align-items: center; justify-content: space-between; margin: 6px 2px 0; }
.section-head h3 { margin: 0; font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--dim); }
.link { color: var(--accent); font-weight: 600; font-size: 14px; padding: 6px 4px; }

/* Deck */
.deck { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 10px; }
.key {
  position: relative; aspect-ratio: 1; border-radius: 16px; padding: 8px;
  background: linear-gradient(180deg, #232935, #1a1f28); border: 1px solid var(--line);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; text-align: center;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05), 0 2px 6px rgba(0, 0, 0, 0.35);
}
.key .glyph { font-size: 26px; line-height: 1; }
.key .label { font-size: 12px; font-weight: 600; line-height: 1.15; max-height: 2.4em; overflow: hidden; word-break: break-word; }
.key:active { transform: scale(0.96); }
.key.on { background: linear-gradient(180deg, #2f6fe0, #2457b8); border-color: #4b8dff; }
.key.red.on { background: linear-gradient(180deg, #ff5d5f, #d93436); border-color: #ff7a7c; }
.key.amber.on { background: linear-gradient(180deg, #ffc04d, #e69a00); border-color: #ffd27a; color: #201400; }
.key.purple.on { background: linear-gradient(180deg, #b57fff, #8a4ae6); border-color: #c79bff; }
.key.stale { opacity: 0.35; }
.key.add { border-style: dashed; background: transparent; color: var(--dim); box-shadow: none; }
.editing .key:not(.add) { animation: wiggle 0.28s ease-in-out infinite alternate; }
.editing .key:not(.add)::after {
  content: "⋯"; position: absolute; top: 4px; right: 6px; font-size: 14px; color: var(--dim);
}
@keyframes wiggle { from { transform: rotate(-1.2deg); } to { transform: rotate(1.2deg); } }

/* Lists */
.list { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.item { display: flex; align-items: center; gap: 10px; min-height: 54px; padding: 8px 8px 8px 14px; border-top: 1px solid var(--line); }
.item:first-child { border-top: 0; }
.item .main { flex: 1; min-width: 0; text-align: left; display: flex; align-items: center; gap: 10px; min-height: 40px; }
.item .title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 600; }
.item .sub { color: var(--dim); font-size: 12px; }
.item.active { background: #15243d; }
.item.active .title { color: #9fc4ff; }
.num { width: 26px; height: 26px; border-radius: 8px; background: var(--panel-2); color: var(--dim); font-size: 12px; font-weight: 700; display: inline-flex; align-items: center; justify-content: center; flex: none; }
.item.active .num { background: var(--accent); color: #fff; }
.more { width: 40px; height: 40px; border-radius: 10px; font-size: 20px; color: var(--dim); flex: none; }

.switch { position: relative; width: 52px; height: 32px; border-radius: 16px; background: #3a414f; flex: none; transition: background 0.15s; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 26px; height: 26px; border-radius: 50%; background: #fff; transition: transform 0.15s; }
.switch.on { background: var(--green); }
.switch.on::after { transform: translateX(20px); }

.slides { display: grid; grid-template-columns: 1fr auto 1fr; gap: 10px; align-items: center; }
.slides .count { font-variant-numeric: tabular-nums; color: var(--dim); min-width: 70px; text-align: center; }
.slides .btn { min-height: 56px; font-size: 18px; }

/* Audio */
.bus { padding: 12px 14px; border-top: 1px solid var(--line); display: grid; grid-template-columns: 1fr auto; gap: 8px 12px; align-items: center; }
.bus:first-child { border-top: 0; }
.bus .name { font-weight: 600; }
.bus .db { color: var(--dim); font-size: 12px; font-variant-numeric: tabular-nums; margin-left: 6px; }
.mute { min-width: 54px; height: 36px; border-radius: 10px; background: var(--panel-2); border: 1px solid var(--line); font-weight: 700; font-size: 13px; }
.mute.on { background: var(--red); border-color: var(--red); color: #fff; }
.fader { grid-column: 1 / -1; display: flex; flex-direction: column; gap: 6px; }
.meter { height: 6px; border-radius: 3px; background: #252b36; overflow: hidden; }
.meter i { display: block; height: 100%; width: 0; background: linear-gradient(90deg, var(--green) 0 70%, var(--amber) 70% 88%, var(--red) 88%); background-size: 100vw 100%; transition: width 0.12s linear; }
input[type=range] { -webkit-appearance: none; appearance: none; width: 100%; height: 34px; background: transparent; margin: 0; }
input[type=range]::-webkit-slider-runnable-track { height: 6px; border-radius: 3px; background: #3a414f; }
input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 26px; height: 26px; border-radius: 50%; background: #fff; margin-top: -10px; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.5); }
input[type=range]::-moz-range-track { height: 6px; border-radius: 3px; background: #3a414f; }
input[type=range]::-moz-range-thumb { width: 26px; height: 26px; border-radius: 50%; background: #fff; border: 0; }
.bus.muted input[type=range] { opacity: 0.5; }

/* Tabs */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6;
  display: grid; grid-template-columns: repeat(4, 1fr);
  padding: 6px calc(8px + var(--safe-right)) calc(6px + var(--safe-bottom)) calc(8px + var(--safe-left));
  background: rgba(13, 15, 20, 0.94); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--line);
}
.tab { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 6px 0; color: var(--dim); font-size: 11px; font-weight: 600; }
.tab .glyph { font-size: 21px; line-height: 1.1; }
.tab.on { color: var(--accent); }

/* Sheets */
.scrim { position: fixed; inset: 0; z-index: 20; background: rgba(0, 0, 0, 0.55); display: flex; align-items: flex-end; justify-content: center; animation: fade 0.15s; }
.sheet {
  width: 100%; max-width: 560px; max-height: 86vh; overflow-y: auto;
  background: var(--panel); border-radius: 18px 18px 0 0; border: 1px solid var(--line); border-bottom: 0;
  padding: 18px calc(16px + var(--safe-right)) calc(16px + var(--safe-bottom)) calc(16px + var(--safe-left));
  display: flex; flex-direction: column; gap: 10px; animation: rise 0.18s ease-out;
}
.sheet h2 { margin: 0 0 2px; font-size: 17px; }
.sheet p { margin: 0; color: var(--dim); }
.sheet .group-title { margin: 8px 2px 0; color: var(--dim); font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; }
.sheet .choices { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 8px; }
.sheet .choice { min-height: 46px; padding: 8px 10px; border-radius: 12px; background: var(--panel-2); border: 1px solid var(--line); text-align: left; display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 14px; }
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translateY(40px); opacity: 0.5; } }

/* Scanner */
.scanner { position: fixed; inset: 0; z-index: 30; background: #000; display: flex; flex-direction: column; }
.scanner video { flex: 1; width: 100%; object-fit: cover; }
.scanner .frame { position: absolute; left: 50%; top: 45%; width: min(70vw, 300px); aspect-ratio: 1; transform: translate(-50%, -50%); border: 3px solid rgba(255, 255, 255, 0.85); border-radius: 22px; box-shadow: 0 0 0 100vmax rgba(0, 0, 0, 0.45); }
.scanner .bar { position: absolute; left: 0; right: 0; bottom: 0; padding: 18px 18px calc(18px + var(--safe-bottom)); display: flex; flex-direction: column; gap: 10px; align-items: center; text-align: center; }
.scanner .bar p { margin: 0; color: #e6e9ef; }

#toast {
  position: fixed; left: 50%; bottom: calc(96px + var(--safe-bottom)); z-index: 40; transform: translateX(-50%) translateY(20px);
  max-width: 90vw; padding: 10px 14px; border-radius: 12px; background: #2b313d; color: var(--text); font-size: 14px;
  opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s; box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.error { background: #5a1f22; }

@media (min-width: 760px) {
  .studio-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 12px; align-items: start; }
  .deck { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); }
}
