/* ---------- Theme tokens ---------- */
:root, :root[data-theme="light"] {
  --bg: #f5f6fa; --panel: #ffffff; --panel2: #f0f2f7; --line: #e3e6ee; --line2: #d3d8e3;
  --text: #171a23; --muted: #667085; --faint: #98a2b3;
  --accent: #6d4aff; --accent-weak: rgba(109, 74, 255, .10); --accent-text: #ffffff;
  --ok: #12a150; --ok-weak: rgba(18,161,80,.12); --warn: #c77700; --warn-weak: rgba(245,165,36,.16);
  --bad: #d92d4b; --bad-weak: rgba(217,45,75,.12); --info: #0b84c6; --info-weak: rgba(11,132,198,.12);
  --shadow: 0 1px 2px rgba(16,24,40,.05), 0 4px 16px rgba(16,24,40,.06);
  --input: #ffffff; color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #0f1115; --panel: #171a21; --panel2: #1f232c; --line: #2a2f3a; --line2: #363c49;
  --text: #e8eaf0; --muted: #98a0b3; --faint: #6b7385;
  --accent: #8b6dff; --accent-weak: rgba(139,109,255,.16); --accent-text: #ffffff;
  --ok: #34d17b; --ok-weak: rgba(52,209,123,.14); --warn: #f5a524; --warn-weak: rgba(245,165,36,.16);
  --bad: #ff5c7a; --bad-weak: rgba(255,92,122,.15); --info: #38bdf8; --info-weak: rgba(56,189,248,.15);
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.25);
  --input: #0f1115; color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root[data-theme="system"] {
    --bg: #0f1115; --panel: #171a21; --panel2: #1f232c; --line: #2a2f3a; --line2: #363c49;
    --text: #e8eaf0; --muted: #98a0b3; --faint: #6b7385;
    --accent: #8b6dff; --accent-weak: rgba(139,109,255,.16); --accent-text: #ffffff;
    --ok: #34d17b; --ok-weak: rgba(52,209,123,.14); --warn: #f5a524; --warn-weak: rgba(245,165,36,.16);
    --bad: #ff5c7a; --bad-weak: rgba(255,92,122,.15); --info: #38bdf8; --info-weak: rgba(56,189,248,.15);
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.25);
    --input: #0f1115; color-scheme: dark;
  }
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 14px/1.5 "Segoe UI", Inter, system-ui, -apple-system, sans-serif; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3, h4 { margin: 0; line-height: 1.25; }
code, pre { font-family: ui-monospace, Consolas, monospace; font-size: 12.5px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Layout ---------- */
.app { display: grid; grid-template-columns: 236px 1fr; min-height: 100vh; }
.side { background: var(--panel); border-right: 1px solid var(--line); padding: 16px 12px; display: flex; flex-direction: column; gap: 2px; position: sticky; top: 0; height: 100vh; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; padding: 4px 10px 18px; }
.logo { width: 28px; height: 28px; border-radius: 8px; background: linear-gradient(135deg, #7c5cff, #ff4fa3); display: grid; place-items: center; color: #fff; font-size: 12px; }
.nav { display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-radius: 9px; color: var(--muted); cursor: pointer; text-decoration: none !important; }
.nav:hover { background: var(--panel2); color: var(--text); }
.nav.active { background: var(--accent-weak); color: var(--accent); font-weight: 600; }
.nav svg { width: 18px; height: 18px; flex: none; }
button.nav { border: 0; background: none; font: inherit; width: 100%; text-align: left; }
.g6 { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
.nav .badge { margin-left: auto; }
.side-foot { margin-top: auto; display: flex; flex-direction: column; gap: 8px; padding: 10px 6px 0; border-top: 1px solid var(--line); }
.user { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.avatar { width: 30px; height: 30px; border-radius: 50%; background: var(--accent-weak); color: var(--accent); display: grid; place-items: center; font-weight: 700; flex: none; overflow: hidden; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
main { min-width: 0; }
.top { position: sticky; top: 0; z-index: 5; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(8px); border-bottom: 1px solid var(--line); padding: 12px 28px; display: flex; align-items: center; gap: 12px; min-height: 60px; }
.top h1 { font-size: 18px; margin-right: auto; }
.crumbs { color: var(--muted); font-size: 13px; }
.content { padding: 24px 28px 64px; max-width: 1280px; }

/* ---------- Components ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; background: var(--panel); border: 1px solid var(--line2); border-radius: 9px; padding: 7px 13px; cursor: pointer; white-space: nowrap; text-decoration: none !important; color: var(--text); }
.btn:hover { background: var(--panel2); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--bad); }
.btn.ok { color: var(--ok); }
.btn.sm { padding: 4px 10px; font-size: 12.5px; border-radius: 8px; }
.btn.ghost { background: none; border-color: transparent; }
.btn.icon { padding: 6px; width: 34px; height: 34px; }
.btn svg { width: 16px; height: 16px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.spacer { flex: 1; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.grid { display: grid; gap: 16px; }
.g2 { grid-template-columns: repeat(auto-fit, minmax(360px, 1fr)); }
.g3 { grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }
.g4 { grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 18px; box-shadow: var(--shadow); min-width: 0; }
.card h3 { font-size: 15px; margin-bottom: 12px; }
.card .sub { color: var(--muted); font-size: 12.5px; margin: -8px 0 12px; }
.kpi .v { font-size: 26px; font-weight: 700; letter-spacing: -.01em; }
.kpi .l { color: var(--muted); font-size: 12.5px; }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.small { font-size: 12.5px; }
.tiny { font-size: 11.5px; }
.mono { font-family: ui-monospace, Consolas, monospace; }
.nowrap { white-space: nowrap; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.empty { padding: 36px 16px; text-align: center; color: var(--muted); }
.empty h3 { color: var(--text); margin-bottom: 6px; }
hr { border: 0; border-top: 1px solid var(--line); margin: 16px 0; }

table { width: 100%; border-collapse: collapse; }
th { text-align: left; color: var(--muted); font-weight: 600; font-size: 12px; padding: 8px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
td { padding: 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
tr.click { cursor: pointer; }
tr.click:hover td { background: var(--panel2); }
.table-wrap { overflow-x: auto; }

.pill { display: inline-flex; align-items: center; gap: 5px; padding: 2px 9px; border-radius: 20px; font-size: 11.5px; font-weight: 600; white-space: nowrap; background: var(--panel2); color: var(--muted); }
.pill.ok { background: var(--ok-weak); color: var(--ok); }
.pill.warn { background: var(--warn-weak); color: var(--warn); }
.pill.bad { background: var(--bad-weak); color: var(--bad); }
.pill.info { background: var(--info-weak); color: var(--info); }
.pill.accent { background: var(--accent-weak); color: var(--accent); }
.badge { background: var(--warn); color: #000; border-radius: 10px; padding: 0 7px; font-size: 11px; font-weight: 700; }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; background: var(--faint); flex: none; }
.dot.ok { background: var(--ok); } .dot.bad { background: var(--bad); } .dot.warn { background: var(--warn); }

/* forms */
.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 14px; min-width: 0; }
.field > label, .label { font-size: 12.5px; color: var(--muted); font-weight: 500; }
.field .help { font-size: 12px; color: var(--faint); }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=time], input[type=search], select, textarea {
  background: var(--input); border: 1px solid var(--line2); border-radius: 9px; padding: 8px 10px; outline: none; width: 100%; min-width: 0;
}
textarea { resize: vertical; min-height: 70px; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-weak); }
input[type=color] { width: 40px; height: 34px; border: 1px solid var(--line2); border-radius: 8px; background: none; padding: 2px; cursor: pointer; }
input[type=range] { width: 100%; accent-color: var(--accent); }
input[type=checkbox], input[type=radio] { accent-color: var(--accent); width: 16px; height: 16px; }
.check { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.three { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.section-title { font-size: 11.5px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 20px 0 10px; font-weight: 700; }
.section-title:first-child { margin-top: 0; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { border: 1px solid var(--line2); border-radius: 20px; padding: 5px 12px; cursor: pointer; font-size: 12.5px; user-select: none; display: inline-flex; gap: 6px; align-items: center; background: var(--panel); }
.chip input { display: none; }
.chip:has(input:checked), .chip.on { background: var(--accent-weak); border-color: var(--accent); color: var(--accent); font-weight: 600; }
.seg { display: inline-flex; border: 1px solid var(--line2); border-radius: 10px; padding: 3px; background: var(--panel2); gap: 2px; }
.seg button, .seg label { border: 0; background: none; padding: 5px 12px; border-radius: 7px; cursor: pointer; color: var(--muted); font-size: 13px; display: inline-flex; align-items: center; gap: 6px; }
.seg input { display: none; }
.seg button.on, .seg label:has(input:checked) { background: var(--panel); color: var(--text); box-shadow: var(--shadow); font-weight: 600; }
.option-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px; }
.option-card { border: 1.5px solid var(--line2); border-radius: 12px; padding: 12px 14px; cursor: pointer; display: block; background: var(--panel); }
.option-card input { display: none; }
.option-card b { display: block; margin-bottom: 2px; }
.option-card:has(input:checked) { border-color: var(--accent); background: var(--accent-weak); }
.hint { font-size: 12.5px; color: var(--muted); background: var(--panel2); border-radius: 10px; padding: 10px 12px; margin: 4px 0 14px; }
.hint.warn { background: var(--warn-weak); color: var(--text); }
.hint.bad { background: var(--bad-weak); color: var(--text); }
.hint.ok { background: var(--ok-weak); color: var(--text); }

/* tabs */
.app > *, .content { min-width: 0; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 20px; overflow-x: auto; }
.tab { padding: 10px 14px; color: var(--muted); border-bottom: 2px solid transparent; cursor: pointer; white-space: nowrap; text-decoration: none !important; font-weight: 500; }
.tab:hover { color: var(--text); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }

/* modal + toast */
.modal-bg { position: fixed; inset: 0; background: rgba(10, 12, 18, .55); display: flex; align-items: flex-start; justify-content: center; z-index: 50; overflow: auto; padding: 48px 16px; }
.modal-bg[hidden] { display: none; }
.modal { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; width: min(560px, 100%); padding: 22px; box-shadow: var(--shadow); }
.modal.wide { width: min(920px, 100%); }
.modal h2 { font-size: 17px; margin-bottom: 14px; }
.modal .foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
#toasts { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 60; }
.toast { background: var(--panel); border: 1px solid var(--line); border-left: 4px solid var(--accent); padding: 10px 14px; border-radius: 10px; box-shadow: var(--shadow); max-width: 380px; transition: opacity .3s; }
.toast.bad { border-left-color: var(--bad); } .toast.ok { border-left-color: var(--ok); }

/* auth */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.auth .card { width: min(420px, 100%); padding: 28px; }
.auth h1 { font-size: 22px; margin: 14px 0 4px; }

/* video + preview */
.phone { width: 100%; max-width: 300px; aspect-ratio: 9/16; border-radius: 18px; background: #000; display: block; box-shadow: var(--shadow); object-fit: contain; }
canvas.phone { object-fit: initial; }
.swatches { display: flex; gap: 6px; flex-wrap: wrap; }
.swatch { width: 36px; height: 26px; border-radius: 7px; cursor: pointer; border: 2px solid var(--line); }
.swatch:hover { border-color: var(--accent); }
.list-item { padding: 10px 12px; border-radius: 10px; cursor: pointer; border: 1px solid transparent; }
.list-item:hover { background: var(--panel2); }
.list-item.active { border-color: var(--accent); background: var(--accent-weak); }

/* channel cards */
.ch-head { display: flex; align-items: center; gap: 12px; }
.ch-avatar { width: 44px; height: 44px; border-radius: 12px; background: #ff0033; color: #fff; display: grid; place-items: center; font-weight: 700; flex: none; overflow: hidden; }
.ch-avatar img { width: 100%; height: 100%; object-fit: cover; }
.switch { position: relative; width: 40px; height: 22px; flex: none; display: inline-block; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; background: var(--line2); border-radius: 22px; cursor: pointer; transition: .2s; }
.switch span::before { content: ""; position: absolute; width: 16px; height: 16px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: .2s; }
.switch input:checked + span { background: var(--ok); }
.switch input:checked + span::before { transform: translateX(18px); }

/* wizard */
.steps { display: flex; gap: 6px; margin-bottom: 22px; flex-wrap: wrap; }
.step { display: flex; align-items: center; gap: 8px; padding: 6px 12px 6px 6px; border-radius: 20px; background: var(--panel2); color: var(--muted); font-size: 12.5px; font-weight: 600; }
.step i { width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; background: var(--line2); color: var(--text); font-style: normal; font-size: 11.5px; }
.step.on { background: var(--accent-weak); color: var(--accent); }
.step.on i { background: var(--accent); color: #fff; }
.step.done i { background: var(--ok); color: #fff; }

/* calendar */
.cal { display: grid; grid-template-columns: repeat(7, minmax(130px, 1fr)); gap: 8px; overflow-x: auto; }
.cal .day { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 8px; min-height: 220px; }
.cal .day.today { border-color: var(--accent); }
.cal .day h4 { font-size: 12px; color: var(--muted); margin-bottom: 8px; }
.ev { font-size: 11.5px; padding: 6px 7px; border-radius: 7px; background: var(--panel2); margin-bottom: 6px; cursor: pointer; border-left: 3px solid var(--accent); }
.ev b { display: block; font-weight: 600; }
.ev:hover { filter: brightness(.97); }

/* scene editor */
.scene { display: grid; grid-template-columns: 28px 1fr 220px 34px; gap: 8px; align-items: start; padding: 8px 0; border-bottom: 1px solid var(--line); }
.scene .n { color: var(--faint); font-weight: 700; padding-top: 8px; text-align: right; }
.log { font-size: 12.5px; max-height: 360px; overflow: auto; }
.log div { padding: 7px 0; border-bottom: 1px solid var(--line); display: flex; gap: 10px; }
.log time { color: var(--faint); white-space: nowrap; font-variant-numeric: tabular-nums; }
.bar { height: 6px; background: var(--panel2); border-radius: 4px; overflow: hidden; }
.bar span { display: block; height: 100%; background: var(--accent); border-radius: 4px; }
.bar.warn span { background: var(--warn); } .bar.bad span { background: var(--bad); }
.spark { width: 100%; height: 120px; display: block; }
.diff-old { color: var(--bad); text-decoration: line-through; }
.diff-new { color: var(--ok); }
.theme-toggle svg { width: 18px; height: 18px; }

/* AI characters */
.char-poses { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 6px; }
.char-pose { position: relative; text-align: center; background: var(--panel2); border-radius: 10px; padding: 6px 4px; }
.char-pose img, .char-missing { width: 100%; height: 120px; object-fit: contain; display: grid; place-items: center; }
.char-pose .btn { position: absolute; top: 2px; right: 2px; padding: 2px 4px; opacity: .6; }
.char-pose .btn:hover { opacity: 1; }
@media (max-width: 900px) { .char-poses { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
.char-view { display: grid; grid-template-columns: minmax(0, 1fr) 260px; gap: 18px; align-items: start; }
.char-stage { position: relative; display: flex; align-items: center; justify-content: center; min-height: 420px; border-radius: 14px;
  background: repeating-conic-gradient(var(--panel2) 0 25%, var(--panel) 0 50%) 0 0 / 24px 24px; }
.char-stage img { max-width: 100%; max-height: 62vh; object-fit: contain; padding: 16px; }
.char-nav { position: absolute; top: 50%; transform: translateY(-50%); font-size: 26px; line-height: 1; padding: 6px 12px; }
.char-nav[data-step="-1"] { left: 6px; } .char-nav[data-step="1"] { right: 6px; }
.chip.on { border-color: var(--accent); background: var(--accent-weak); color: var(--text); }
@media (max-width: 900px) { .char-view { grid-template-columns: minmax(0, 1fr); } .char-stage { min-height: 300px; } .char-stage img { max-height: 46vh; } }
/* templates page: list | editor | preview */
.fig-box { border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; margin: 14px 0 0; min-width: 0; }
.fig-box legend { font-weight: 600; font-size: 13px; padding: 0 6px; }
[hidden] { display: none !important; }
.tpl-layout { grid-template-columns: 220px minmax(0, 1fr) 300px; align-items: start; }
.tpl-preview { position: sticky; top: 80px; }
.ref-result { grid-template-columns: minmax(0, 1fr) 230px; gap: 16px; align-items: start; margin-top: 6px; }
@media (max-width: 1100px) {
  .tpl-layout { grid-template-columns: 200px minmax(0, 1fr); }
  .tpl-preview { grid-column: 1 / -1; position: static; display: flex; flex-direction: column; align-items: center; }
}
@media (max-width: 900px) {
  .tpl-layout { grid-template-columns: minmax(0, 1fr); }
  /* template list becomes a swipeable strip, preview comes before the editor */
  .tpl-list { display: flex; gap: 6px; overflow-x: auto; padding: 8px !important; }
  .tpl-list .list-item { flex: 0 0 auto; min-width: 150px; margin: 0; }
  .tpl-preview { order: 1; grid-column: auto; }
  .tpl-preview .phone { max-width: 230px; }
  #tf { order: 2; }
  #tf .row { flex-wrap: wrap; }
  .ref-result { grid-template-columns: minmax(0, 1fr); }
  .ref-preview { display: flex; flex-direction: column; align-items: center; order: -1; }
  .ref-preview .phone { max-width: 200px; }
  .hide-sm { display: none; }
  /* fixed side-by-side panels (video page, channel page) stack on phones */
  .grid[style*="grid-template-columns:minmax(0,1fr) 3"] { grid-template-columns: minmax(0, 1fr) !important; }
  .top { flex-wrap: wrap; }
  #pageActions { display: flex; gap: 6px; flex-wrap: wrap; }
  .modal-bg { padding: 12px 8px; }
  .modal { padding: 16px; }
  .app { grid-template-columns: 1fr; }
  .side { position: static; height: auto; flex-direction: row; overflow-x: auto; padding: 8px; gap: 4px; }
  .brand, .side-foot .user { display: none; }
  .side-foot { margin: 0; border: 0; padding: 0; flex-direction: row; }
  .nav span.lbl { display: none; }
  .content { padding: 16px; }
  .top { padding: 10px 16px; }
  .two, .three { grid-template-columns: 1fr; }
  .scene { grid-template-columns: 22px 1fr; }
  .scene > :nth-child(3), .scene > :nth-child(4) { grid-column: 2; }
}
.spinner { display: inline-block; width: 14px; height: 14px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; vertical-align: -2px; margin-right: 6px; }
@keyframes spin { to { transform: rotate(360deg); } }
