:root {
  --bg: #0f1117; --surface: #1a1d2e; --border: #2d3748;
  --text: #e2e8f0; --muted: #718096; --accent: #3182ce;
  --green: #38a169; --red: #e53e3e; --orange: #dd6b20;
  --radius: 12px; --max-w: 960px;
}
* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { min-height: 100%; background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; }

/* ── Layout ── */
.container { max-width: var(--max-w); margin: 0 auto; padding: 0 16px; }
.view { display: none; min-height: 100vh; padding-bottom: 72px; }
.view.active { display: block; }

/* ── Top bar ── */
.topbar { position: sticky; top: 0; z-index: 100; background: var(--surface);
  border-bottom: 1px solid var(--border); padding: 12px 16px;
  display: flex; align-items: center; gap: 10px; }
.topbar h1 { font-size: 1rem; font-weight: 700; flex: 1;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex-shrink: 0; }
.topbar .dot.ok { background: #48bb78; }
.back-btn { background: none; border: none; color: var(--accent); font-size: 1.2rem;
  cursor: pointer; padding: 6px 10px; border-radius: 8px; font-weight: 700; flex-shrink: 0; }
.back-btn:hover { background: var(--border); }

/* ── Bottom nav ── */
.nav { position: fixed; bottom: 0; left: 0; right: 0; z-index: 200;
  background: var(--surface); border-top: 1px solid var(--border);
  display: flex; padding-bottom: env(safe-area-inset-bottom); }
.nav-btn { flex: 1; display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 3px; padding: 10px 4px; border: none; background: none;
  color: var(--muted); font-size: 0.65rem; font-weight: 600; cursor: pointer; min-height: 48px; }
.nav-btn .icon { font-size: 1.3rem; }
.nav-btn.active { color: var(--accent); }

/* ── Cards ── */
.card { background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden; margin-bottom: 16px; }
.card-header { padding: 14px 16px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 10px; }
.card-header h2 { font-size: 0.95rem; font-weight: 700; flex: 1; }
.card-body { padding: 16px; }

/* ── Channel grid ── */
.ch-grid { display: grid; gap: 16px; padding: 20px 0;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
.ch-card { background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 20px; cursor: pointer; transition: border-color .2s; }
.ch-card:hover { border-color: var(--accent); }
.ch-card .ch-icon { font-size: 2rem; margin-bottom: 8px; }
.ch-card .ch-name { font-size: 1.1rem; font-weight: 700; margin-bottom: 4px; }
.ch-card .ch-meta { font-size: 0.78rem; color: var(--muted); }
.ch-count { display: inline-flex; align-items: center; gap: 4px;
  background: var(--border); border-radius: 99px; padding: 2px 10px;
  font-size: 0.72rem; font-weight: 700; }
.ch-mode { display: inline-block; padding: 1px 7px; border-radius: 4px;
  font-size: 0.6rem; font-weight: 800; letter-spacing: 0.5px;
  vertical-align: middle; margin-left: 6px; }
.ch-mode-st { background: #2d4a3e; color: #68d391; }
.ch-mode-sc { background: #2d3a4e; color: #90cdf4; }
.ch-mode-ac { background: #4a2d4e; color: #d6bcfa; }

/* ── Channel filter bar ── */
.ch-filter-bar { display: flex; gap: 8px; padding: 16px 0 0; flex-wrap: wrap; }
.ch-filter-btn { background: var(--surface); border: 1px solid var(--border);
  border-radius: 99px; padding: 6px 16px; font-size: 0.78rem; font-weight: 600;
  color: var(--muted); cursor: pointer; transition: all .2s; }
.ch-filter-btn:hover { border-color: var(--accent); color: var(--text); }
.ch-filter-btn.active { background: var(--accent); border-color: var(--accent);
  color: #fff; }

/* ── Story list ── */
.story-row { display: flex; align-items: center; gap: 12px; padding: 14px 0;
  border-bottom: 1px solid var(--border); cursor: pointer; }
.story-row:hover { background: rgba(49, 130, 206, 0.06); }
.story-row .sr-info { flex: 1; min-width: 0; }
.story-row .sr-title { font-size: 0.9rem; font-weight: 600;
  overflow: hidden; text-overflow: ellipsis;
  display: -webkit-box; -webkit-line-clamp: 2; line-clamp: 2; -webkit-box-orient: vertical; }
.story-row .sr-meta { font-size: 0.72rem; color: var(--muted); margin-top: 2px; }

/* ── Forms ── */
label { font-size: 0.8rem; color: var(--muted); margin-bottom: 6px; display: block; }
select, input[type=text], textarea {
  width: 100%; background: var(--bg); border: 1px solid var(--border);
  color: var(--text); border-radius: 8px; padding: 12px;
  font-size: 1rem; margin-bottom: 12px; appearance: none; font-family: inherit; }
select:focus, input:focus, textarea:focus { outline: none; border-color: var(--accent); }
textarea { min-height: 80px; resize: vertical; }
.toggle-row { display: flex; justify-content: space-between; align-items: center;
  padding: 12px 0; border-bottom: 1px solid var(--border); }
.toggle-row:last-child { border-bottom: none; }
.toggle-row label { margin: 0; font-size: 0.88rem; color: var(--text); }
.switch { position: relative; width: 44px; height: 26px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; inset: 0; background: #2d3748; border-radius: 13px;
  transition: .2s; cursor: pointer; }
.slider:before { content: ''; position: absolute; width: 20px; height: 20px;
  left: 3px; bottom: 3px; background: white; border-radius: 50%; transition: .2s; }
input:checked + .slider { background: var(--accent); }
input:checked + .slider:before { transform: translateX(18px); }

/* ── Buttons ── */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; border-radius: 10px; border: none; cursor: pointer;
  font-size: 0.92rem; font-weight: 700; padding: 0 18px; transition: opacity .15s; }
.btn:active { opacity: .75; }
.btn-primary { background: var(--accent); color: white; }
.btn-success { background: var(--green); color: white; }
.btn-danger { background: var(--red); color: white; }
.btn-ghost { background: var(--border); color: var(--text); }
.btn-sm { min-height: 34px; font-size: 0.8rem; padding: 0 12px; border-radius: 8px; }
.btn-row { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }

/* ── Badges ── */
.badge { display: inline-flex; align-items: center; padding: 3px 10px;
  border-radius: 99px; font-size: 0.7rem; font-weight: 700; white-space: nowrap; }
.badge-idea, .badge-idea_generating { background: #2d2b4e; color: #a78bfa; }
.badge-queued { background: #2d3748; color: #a0aec0; }
.badge-running, .badge-plan, .badge-approve, .badge-render, .badge-generating
  { background: #2b4c7e; color: #90cdf4; }
.badge-preview { background: #234e6b; color: #7ec8e3; }
.badge-gate1, .badge-gate2 { background: #744210; color: #fbd38d; }
.badge-publish { background: #553c9a; color: #d6bcfa; }
.badge-done { background: #1c4532; color: #9ae6b4; }
.badge-error { background: #742a2a; color: #feb2b2; }
.badge-aborted { background: #2d3748; color: #a0aec0; }

/* ── Phase stepper ── */
.phases { display: flex; gap: 0; margin-bottom: 20px; }
.phase-step { flex: 1; text-align: center; padding: 8px 4px;
  font-size: 0.65rem; font-weight: 700; color: var(--muted);
  border-bottom: 3px solid var(--border); transition: all .2s; }
.phase-step.active { color: var(--accent); border-color: var(--accent); }
.phase-step.done { color: #48bb78; border-color: #48bb78; }

/* ── Scene cards ── */
.scene-card { background: var(--bg); border: 1px solid var(--border);
  border-radius: 12px; margin-bottom: 12px; overflow: hidden; }
.scene-card-header { display: flex; align-items: center; gap: 8px;
  padding: 10px 14px; background: #161929; border-bottom: 1px solid var(--border); }
.scene-num { background: var(--accent); color: white; border-radius: 6px;
  padding: 2px 8px; font-size: 0.72rem; font-weight: 800; flex-shrink: 0; }
.scene-meta-chips { display: flex; flex-wrap: wrap; gap: 6px; flex: 1; }
.chip { background: #2a2f45; color: #a0aec0; border-radius: 6px;
  padding: 3px 8px; font-size: 0.68rem; font-weight: 600; white-space: nowrap; }
.chip-shot { background: #1a3a4a; color: #76cfe8; }
.chip-char { background: #1a3a2a; color: #76e8a0; }
.chip-dur { background: #2a2a1a; color: #e8d876; }
.scene-frame { position: relative; background: #0a0c18;
  min-height: 80px; display: flex; align-items: center; justify-content: center; }
.scene-frame video { width: 100%; max-height: 260px; display: block; }
.scene-frame .placeholder { display: flex; flex-direction: column; align-items: center;
  gap: 6px; color: #4a5568; padding: 20px; text-align: center; }
.overlay-badge { position: absolute; bottom: 8px; left: 0; right: 0;
  text-align: center; pointer-events: none; }
.overlay-badge span { background: rgba(0,0,0,.75); color: white;
  font-size: 0.8rem; font-weight: 700; padding: 4px 12px;
  border-radius: 6px; display: inline-block; }
.scene-narration { padding: 10px 14px; font-size: 0.88rem; line-height: 1.6;
  color: #c8d0dc; border-bottom: 1px solid var(--border); }
.preview-row { display: flex; flex-wrap: wrap; gap: 8px; padding: 10px 14px;
  border-bottom: 1px solid var(--border); }
.scene-edit { display: none; padding: 14px; }
.scene-edit.open { display: block; }

/* ── Media ── */
.card video, .card audio { width: 100%; border-radius: 8px; display: block; margin-top: 8px; }
.thumb { width: 100%; border-radius: 8px; display: block; margin-top: 8px; object-fit: cover; }

/* ── Log ── */
.log-box { background: #0a0c14; border-radius: 8px; padding: 12px;
  font-family: 'SF Mono', Monaco, monospace; font-size: 0.72rem;
  color: #a0aec0; max-height: 260px; overflow-y: auto;
  white-space: pre-wrap; word-break: break-all; }
.log-err { color: #fc8181; }

/* ── Gate ── */
.gate-banner { background: #3d2a00; border: 1px solid var(--orange);
  border-radius: 10px; padding: 14px; margin-bottom: 16px; }
.gate-banner p { font-size: 0.9rem; color: #fbd38d; margin-bottom: 12px; }

/* ── Spinner ── */
.spin { display: inline-block; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Admin rows ── */
.admin-row { display: flex; align-items: center; gap: 12px; padding: 12px 0;
  border-bottom: 1px solid var(--border); cursor: pointer; }
.admin-row:hover { background: rgba(49, 130, 206, 0.06); }
.admin-row .ar-ch { font-size: 0.72rem; color: var(--muted); width: 90px; flex-shrink: 0; }
.admin-row .ar-info { flex: 1; min-width: 0; }
.admin-row .ar-title { font-size: 0.85rem; font-weight: 600;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.admin-row .ar-meta { font-size: 0.7rem; color: var(--muted); }

/* ── Channel config ── */
.cfg-row { display: flex; justify-content: space-between; align-items: center;
  padding: 8px 0; border-bottom: 1px solid var(--border); }
.cfg-row:last-child { border-bottom: none; }
.cfg-label { font-size: 0.8rem; color: var(--muted); flex-shrink: 0; min-width: 110px; }
.cfg-val { font-size: 0.85rem; text-align: right; word-break: break-all; }
.cfg-val code { background: var(--bg); padding: 2px 6px; border-radius: 4px; font-size: 0.78rem; }
.cfg-profile { background: var(--bg); border: 1px solid var(--border);
  border-radius: 10px; margin-bottom: 10px; overflow: hidden; }
.cfg-profile-default { border-color: #38a169; }
.cfg-profile-header { display: flex; align-items: center; gap: 8px;
  padding: 10px 14px; border-bottom: 1px solid var(--border); }
.cfg-profile-name { font-weight: 700; font-size: 0.9rem; flex: 1; }
.cfg-profile-body { padding: 8px 14px; }

/* ── Login ── */
.login-wrap { display: flex; align-items: center; justify-content: center;
  min-height: 100vh; padding: 20px; }
.login-card { background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; padding: 40px 32px; width: 100%; max-width: 380px; }
.login-logo { font-size: 3rem; text-align: center; margin-bottom: 8px; }
.login-title { font-size: 1.2rem; font-weight: 800; text-align: center;
  margin-bottom: 28px; color: var(--text); }
.login-label { font-size: 0.78rem; font-weight: 600; color: var(--muted);
  margin-bottom: 6px; display: block; }
.login-card input[type=email],
.login-card input[type=password] { width: 100%; background: var(--bg);
  border: 1px solid var(--border); color: var(--text); border-radius: 10px;
  padding: 12px 14px; font-size: 0.95rem; margin-bottom: 16px; }
.login-card input:focus { outline: none; border-color: var(--accent); }
.login-btn { width: 100%; min-height: 48px; font-size: 1rem; border-radius: 10px;
  margin-top: 4px; }
.login-error { background: #742a2a; color: #feb2b2; border-radius: 8px;
  padding: 10px 14px; font-size: 0.82rem; margin-bottom: 12px; text-align: center; }

/* ── Angles grid ── */
.angles-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

/* ── Responsive ── */

/* Small phones */
@media (max-width: 479px) {
  .container { padding: 0 10px; }
  .topbar { padding: 10px 12px; gap: 6px; }
  .topbar h1 { font-size: 0.88rem; }
  .card-header { padding: 10px 12px; gap: 6px; }
  .card-header h2 { font-size: 0.85rem; }
  .card-body { padding: 12px; }

  /* Phase stepper — scroll on small screens */
  .phases { overflow-x: auto; -webkit-overflow-scrolling: touch;
    scrollbar-width: none; gap: 0; }
  .phases::-webkit-scrollbar { display: none; }
  .phase-step { min-width: 54px; font-size: 0.58rem; padding: 6px 3px; white-space: nowrap; }

  /* Button rows */
  .btn-row { gap: 6px; }
  .btn-sm { min-height: 32px; font-size: 0.72rem; padding: 0 10px; }

  /* Story list */
  .story-row { padding: 10px 0; gap: 8px; }
  .story-row .sr-title { font-size: 0.82rem; }
  .story-row .sr-meta { font-size: 0.66rem; }

  /* Channel grid */
  .ch-grid { grid-template-columns: 1fr; gap: 10px; padding: 12px 0; }
  .ch-card { padding: 14px; }
  .ch-card .ch-name { font-size: 0.95rem; }

  /* Channel filter bar */
  .ch-filter-bar { gap: 6px; overflow-x: auto; flex-wrap: nowrap;
    -webkit-overflow-scrolling: touch; padding-bottom: 4px; }
  .ch-filter-btn { padding: 5px 12px; font-size: 0.7rem; white-space: nowrap; flex-shrink: 0; }

  /* Scene cards */
  .scene-card-header { padding: 8px 10px; gap: 6px; }
  .scene-meta-chips { gap: 4px; }
  .chip { font-size: 0.6rem; padding: 2px 6px; }
  .scene-narration { padding: 8px 10px; font-size: 0.8rem; }
  .preview-row { padding: 8px 10px; gap: 6px; }

  /* Log */
  .log-box { font-size: 0.65rem; padding: 8px; max-height: 200px; }

  /* Admin rows */
  .admin-row { padding: 10px 0; gap: 8px; }
  .admin-row .ar-ch { width: 70px; font-size: 0.65rem; }
  .admin-row .ar-title { font-size: 0.78rem; }

  /* Forms */
  select, input[type=text], textarea { font-size: 0.88rem; padding: 10px; }

  /* Nav */
  .nav-btn { font-size: 0.58rem; padding: 8px 2px; }
  .nav-btn .icon { font-size: 1.1rem; }

  /* Config */
  .cfg-row { flex-direction: column; align-items: flex-start; gap: 2px; }
  .cfg-val { text-align: left; }

  /* Angles grid — single column on mobile */
  .angles-grid { grid-template-columns: 1fr; }

  /* Storytime/AC tab bar — horizontal scroll */
  .st-tabs { overflow-x: auto; -webkit-overflow-scrolling: touch;
    scrollbar-width: none; white-space: nowrap; }
  .st-tabs::-webkit-scrollbar { display: none; }

  /* Prevent horizontal overflow globally */
  .card-body { overflow-x: hidden; }
  .card-body textarea { font-size: 0.82rem; }
}

/* Tablets */
@media (min-width: 480px) and (max-width: 767px) {
  .ch-grid { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
  .phases { overflow-x: auto; -webkit-overflow-scrolling: touch;
    scrollbar-width: none; }
  .phases::-webkit-scrollbar { display: none; }
  .phase-step { min-width: 60px; font-size: 0.6rem; }
}

@media (min-width: 768px) {
  .container { padding: 0 24px; }
  .ch-grid { grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
}
@media (min-width: 1024px) {
  .container { padding: 0 32px; }
  .nav { max-width: var(--max-w); left: 50%; transform: translateX(-50%);
    border-radius: 16px 16px 0 0; }
}
