/* ── Smart fields (modal) ────────────────────────────────────────────────── */
.smart-fields { margin-top: 14px; border-top: 1px solid var(--line); padding-top: 14px; }
.smart-preview-row { display: flex; gap: 8px; align-items: flex-end; margin-top: 10px; }
.smart-preview-row label { flex: 1; margin: 0; }
.smart-preview-row .button { flex-shrink: 0; height: 38px; }

/* ── Tokens ──────────────────────────────────────────────────────────────── */
:root {
  color-scheme: dark;
  --bg:       #060809;
  --surface:  #0b0e12;
  --card:     #0e1117;
  --line:     #1c2230;
  --line-s:   #141a24;
  --text:     #e2eaff;
  --muted:    #7a8aaa;
  --dim:      #485268;
  --green:    #00ff88;
  --amber:    #ffaa00;
  --red:      #ff4466;
  --cyan:     #00d4ff;
  --accent:   #7c6dfa;

  /* bottom nav safe area */
  --nav-h: calc(58px + env(safe-area-inset-bottom, 0px));

  font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', ui-monospace, monospace;
}

/* ── Reset ───────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }
html { width: 100%; overflow-x: hidden; -webkit-text-size-adjust: 100%; }
body {
  width: 100%; min-height: 100dvh; overflow-x: hidden;
  color: var(--text); background: var(--bg);
  touch-action: pan-y;
  /* subtle scanline texture */
  background-image: repeating-linear-gradient(
    0deg, transparent, transparent 2px,
    rgba(255,255,255,.008) 2px, rgba(255,255,255,.008) 4px
  );
}
button, input, textarea, select { font: inherit; }
button { -webkit-tap-highlight-color: transparent; cursor: pointer; }
button, a { touch-action: manipulation; }
p { line-height: 1.6; }

/* ── Form elements ───────────────────────────────────────────────────────── */
input, textarea, select {
  min-width: 0; width: 100%; color: var(--text);
  background: #050709;
  border: 1px solid var(--line);
  border-radius: 6px; outline: 0;
  font-size: .82rem;
}
input, select { height: 40px; padding: 0 11px; }
textarea { padding: 10px 11px; resize: none; line-height: 1.6; }
input:focus, textarea:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px #7c6dfa1a;
}
label {
  display: grid; gap: 6px;
  color: var(--muted); font-size: .72rem; font-weight: 600; letter-spacing: .06em;
  text-transform: uppercase;
}
label.span2 { grid-column: span 2; }
code { padding: 2px 6px; border-radius: 4px; color: var(--cyan); background: #0a1820; font-size: .8em; }

/* ── Auth ────────────────────────────────────────────────────────────────── */
.auth-page {
  display: grid; place-items: center; min-height: 100dvh; padding: 20px;
  background: var(--bg);
  background-image:
    radial-gradient(ellipse 60% 50% at 50% -10%, #7c6dfa12, transparent 70%),
    repeating-linear-gradient(0deg, transparent, transparent 2px, rgba(255,255,255,.006) 2px, rgba(255,255,255,.006) 4px);
}
.auth-page[hidden] { display: none !important; }
.auth-card {
  width: min(100%, 360px); padding: 32px;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--card);
}
.auth-logo { display: flex; align-items: center; gap: 12px; margin-bottom: 24px; }
.auth-brand-name { font-size: .62rem; font-weight: 800; letter-spacing: .18em; color: var(--dim); line-height: 1.5; }
.auth-card h1 { font-size: 1.4rem; font-weight: 800; letter-spacing: -.02em; margin-bottom: 20px; color: var(--text); }
.auth-card label { margin-top: 12px; }
.auth-card .button { margin-top: 20px; }
.form-error { display: block; min-height: 16px; margin-top: 8px; color: var(--red); font-size: .72rem; }

/* ── Overlays ────────────────────────────────────────────────────────────── */
.overlay {
  position: fixed; inset: 0; z-index: 200;
  display: grid; place-items: center; padding: 16px;
  background: rgba(3,4,6,.9); backdrop-filter: blur(14px);
}
.modal-card {
  width: min(100%, 440px); max-height: calc(100dvh - 40px); overflow-y: auto;
  padding: 24px; border: 1px solid var(--line); border-radius: 12px;
  background: var(--card);
}
.modal-card--wide { width: min(100%, 580px); }
.modal-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.modal-heading h2 { font-size: 1.1rem; font-weight: 800; letter-spacing: -.02em; }
.modal-copy { color: var(--muted); font-size: .8rem; margin-bottom: 14px; }
.login-fields { display: grid; gap: 10px; }
.login-fields > .button { margin-top: 0; }
.modal-scroll { display: grid; gap: 0; }
.modal-fieldset { padding: 14px 0; border: 0; border-bottom: 1px solid var(--line-s); min-width: 0; }
.modal-fieldset:last-of-type { border-bottom: 0; }
.modal-fieldset legend {
  padding-bottom: 10px; color: var(--text); font-size: .78rem;
  font-weight: 800; letter-spacing: .04em; text-transform: uppercase;
}
.modal-actions { display: flex; align-items: center; gap: 8px; padding-top: 16px; flex-wrap: wrap; }
.modal-actions .form-error { flex: 1 1 100%; }
.hint-text { margin: 4px 0 8px; color: var(--dim); font-size: .72rem; }
.field-hint { color: var(--dim); font-size: .68rem; font-weight: 500; text-transform: none; letter-spacing: 0; }
.badge {
  display: inline-block; margin-left: 6px; padding: 1px 6px;
  border-radius: 4px; border: 1px solid var(--line);
  color: var(--dim); font-size: .58rem; font-weight: 700; letter-spacing: .06em;
  vertical-align: middle;
}
.start-mode-tabs { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }

/* ── Brand mark ──────────────────────────────────────────────────────────── */
.brand-mark {
  display: grid; place-items: center; flex-shrink: 0;
  width: 32px; height: 32px; border-radius: 8px;
  background: var(--accent); color: #fff;
  font-weight: 900; font-size: .9rem; font-family: inherit;
  box-shadow: 0 0 20px #7c6dfa40;
}

/* ── App shell ───────────────────────────────────────────────────────────── */
.app-shell {
  display: flex; flex-direction: column; min-height: 100dvh;
  max-width: 680px; margin: 0 auto;
}

/* ── Top bar ─────────────────────────────────────────────────────────────── */
.top-bar {
  position: sticky; top: 0; z-index: 50;
  display: flex; align-items: center; justify-content: space-between;
  height: 54px; padding: 0 16px;
  border-bottom: 1px solid var(--line);
  background: #060809f0; backdrop-filter: blur(12px);
}
.top-bar__brand { display: flex; align-items: center; gap: 10px; }
.top-bar__title {
  font-size: .88rem; font-weight: 800; letter-spacing: .02em;
  display: flex; flex-direction: column; line-height: 1.1;
}
.top-bar__title small { font-size: .45rem; letter-spacing: .22em; color: var(--dim); font-weight: 700; }
.top-bar__actions { display: flex; align-items: center; gap: 7px; }

/* ── Panels ──────────────────────────────────────────────────────────────── */
.panels { flex: 1; overflow: hidden; }
.tab-panel { min-height: calc(100dvh - 54px - var(--nav-h)); }
.tab-panel[hidden] { display: none !important; }
.panel-inner {
  display: grid; gap: 16px;
  padding: 18px 16px calc(var(--nav-h) + 16px);
}
.panel-header { display: grid; gap: 2px; }
.panel-header h1 { font-size: 1.6rem; font-weight: 900; letter-spacing: -.04em; line-height: 1.1; }
.panel-header--row { display: flex; align-items: flex-end; justify-content: space-between; }
.panel-header--row h1 { font-size: 1.4rem; }

/* ── Eyebrow ─────────────────────────────────────────────────────────────── */
.eyebrow { color: var(--accent); font-size: .6rem; font-weight: 800; letter-spacing: .2em; }

/* ── Stats grid ──────────────────────────────────────────────────────────── */
.stats-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.stat-tile {
  padding: 14px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--card); display: flex; flex-direction: column; gap: 6px;
}
.stat-tile--accent { border-color: #7c6dfa28; background: #0e0d18; }
.stat-tile__label { font-size: .62rem; font-weight: 700; letter-spacing: .1em; color: var(--dim); text-transform: uppercase; }
.stat-tile__value { font-size: 2rem; font-weight: 900; letter-spacing: -.05em; line-height: 1; color: var(--text); }
.stat-tile__value.good { color: var(--green); text-shadow: 0 0 20px #00ff8840; }

/* ── Log section ─────────────────────────────────────────────────────────── */
.section-block { display: grid; gap: 10px; }
.section-block__head { display: grid; gap: 4px; }
.section-block__title { font-size: 1rem; font-weight: 800; letter-spacing: -.02em; }
.log-tools { display: flex; align-items: center; gap: 6px; margin-top: 4px; }
.log-tools select { height: 32px; padding: 0 8px; font-size: .72rem; flex: 1; }
.log-output {
  max-height: 300px; padding: 12px;
  border: 1px solid var(--line); border-radius: 8px;
  background: #030405; color: var(--muted);
  font-size: .68rem; line-height: 1.75;
  white-space: pre-wrap; word-break: break-all; overflow-y: auto;
}

/* ── Channel list ────────────────────────────────────────────────────────── */
.channel-list { display: grid; gap: 8px; }
.empty-hint {
  padding: 28px 20px; text-align: center;
  border: 1px dashed var(--line); border-radius: 10px;
  color: var(--muted); font-size: .8rem;
}

/* ── Channel card ────────────────────────────────────────────────────────── */
.channel-card {
  padding: 14px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--card); cursor: pointer;
  transition: border-color .15s;
}
.channel-card:hover { border-color: #2e3a52; }
.channel-top { display: flex; align-items: flex-start; gap: 10px; margin-bottom: 10px; }
.channel-mark {
  display: grid; place-items: center; flex-shrink: 0;
  width: 34px; height: 34px; border-radius: 8px;
  background: #181430; color: #9d90ff; font-weight: 900;
}
.channel-meta { flex: 1; min-width: 0; }
.ch-label { font-size: .9rem; font-weight: 800; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ch-channels { margin-top: 2px; color: var(--muted); font-size: .68rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.channel-badges { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 8px; }
.provider-badge, .context-badge {
  height: 18px; padding: 0 7px;
  border-radius: 4px; font-size: .6rem; font-weight: 700; letter-spacing: .04em;
  display: inline-flex; align-items: center;
}
.provider-badge { color: #b0a8ff; background: #1a1848; border: 1px solid #2d2870; }
.context-badge  { color: var(--cyan); background: #071620; border: 1px solid #0e3040; overflow: hidden; max-width: 160px; text-overflow: ellipsis; white-space: nowrap; }
.last-caption {
  padding: 6px 9px; border-radius: 6px; background: #030405;
  color: var(--dim); font-size: .68rem; margin-bottom: 10px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.channel-actions { display: flex; flex-wrap: wrap; gap: 5px; }
.channel-actions .button { flex: 1 1 auto; font-size: .72rem; height: 34px; }

/* Metric grid inside channel card */
.metric-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 5px; margin-bottom: 10px; }
.metric-grid div { padding: 7px 8px; border: 1px solid var(--line-s); border-radius: 6px; background: #030405; }
.metric-grid span { display: block; color: var(--dim); font-size: .58rem; font-weight: 700; letter-spacing: .05em; text-transform: uppercase; }
.metric-grid b { display: block; margin-top: 2px; font-size: .84rem; font-weight: 800; color: var(--text); }
.metric-failed { color: var(--red) !important; }

/* Status chips */
.status-chip, .status {
  display: inline-flex; align-items: center; gap: 5px;
  height: 26px; padding: 0 9px;
  border: 1px solid var(--line); border-radius: 5px;
  color: var(--muted); background: var(--surface);
  font-size: .64rem; font-weight: 800; letter-spacing: .04em; text-transform: uppercase;
  white-space: nowrap; overflow: hidden; max-width: 120px;
}
.status::before, .status-chip::before {
  content: ""; width: 5px; height: 5px; border-radius: 50%;
  background: currentColor; flex-shrink: 0;
}
.pending, .status.waiting        { color: var(--amber); border-color: #3d2c0a; background: #160f04; }
.ready, .status.running,
.status.backfilling,
.status.watching, .good          { color: var(--green); border-color: #0a3020; background: #040f09; }
.status.watching                 { color: var(--cyan); border-color: #083040; }
.failed, .status.stopped, .bad   { color: var(--red); border-color: #3d0a14; background: #160408; }

.tg-mini-pill { font-size: .6rem; max-width: 50px; }
.tg-mini-pill::before { display: none; }

/* ── Settings ────────────────────────────────────────────────────────────── */
.settings-stack { display: grid; gap: 8px; }
.settings-card {
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--card); overflow: hidden;
}
.settings-card__head {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 16px; border-bottom: 1px solid var(--line-s);
  background: #0a0d12;
}
.settings-card__num {
  display: grid; place-items: center; width: 22px; height: 22px;
  border: 1px solid #3a3470; border-radius: 5px;
  color: var(--accent); background: #141240;
  font-size: .62rem; font-weight: 800;
}
.settings-card__title { font-size: .8rem; font-weight: 800; letter-spacing: .06em; text-transform: uppercase; }
.settings-card fieldset { padding: 14px 16px 0; border: 0; min-width: 0; }
.card-desc { color: var(--muted); font-size: .74rem; margin-bottom: 12px; }
.card-footer {
  display: flex; align-items: center; gap: 8px;
  justify-content: flex-end;
  padding: 12px 16px 14px;
}
.form-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.compact-grid { max-width: 360px; }

/* Session status */
.session-status-row { padding: 0 16px 14px; }
.session-divider { border-top: 1px solid var(--line-s); margin-bottom: 12px; }
.session-inner { display: grid; gap: 8px; }
.session-text { color: var(--muted); font-size: .74rem; }
.session-controls { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }

/* AI prompt */
.ai-prompt-field { position: relative; margin-top: 12px; }
.ai-prompt-field .field-hint { display: block; margin-bottom: 6px; }
.ai-prompt-field .button.tiny { position: absolute; top: -2px; right: 0; }
.ai-prompt-field textarea { height: 130px; }

/* Save row */
.section-save-row { display: flex; align-items: center; gap: 8px; justify-content: flex-end; margin-top: 12px; }
.ai-test-result-row { min-height: 0; overflow: hidden; transition: margin-top .2s; padding: 0 16px; }
.ai-test-result-row:has(.ai-test-result.ok),
.ai-test-result-row:has(.ai-test-result.fail) { margin-top: 6px; padding-bottom: 4px; }
.ai-test-result {
  display: none; font-size: .78rem; font-weight: 600;
  padding: 7px 11px; border-radius: 6px;
  width: 100%; box-sizing: border-box; word-break: break-word;
}
.ai-test-result.ok   { display: block; background: #040f09; color: var(--green); border: 1px solid #0a3020; }
.ai-test-result.fail { display: block; background: #160408; color: var(--red);   border: 1px solid #3d0a14; }

/* Caption preview */
.caption-preview-section { display: grid; gap: 10px; padding: 0 16px 14px; border-top: 1px solid var(--line-s); margin-top: 12px; padding-top: 14px; }
.preview-result {
  min-height: 54px; padding: 10px 12px; border-radius: 7px;
  border: 1px solid var(--line-s); background: #030405;
  color: var(--muted); font-size: .8rem; line-height: 1.6;
}
.preview-result.error { color: var(--red); border-color: #3d0a14; }
.output-meta    { display: block; margin-bottom: 5px; color: var(--dim); font-size: .68rem; }
.output-caption { display: block; color: var(--text); font-size: .94rem; font-weight: 800; word-break: break-all; }

/* Mode tabs */
.mode-tab {
  height: 38px; border: 1px solid var(--line); border-radius: 6px;
  color: var(--muted); background: #050709;
  font-size: .72rem; font-weight: 700; letter-spacing: .04em; text-transform: uppercase;
  transition: border-color .12s, color .12s, background .12s;
  white-space: nowrap; overflow: hidden;
}
.mode-tab.selected { border-color: var(--accent); color: var(--text); background: #1a1740; }

/* ── Buttons ─────────────────────────────────────────────────────────────── */
.button {
  display: inline-flex; align-items: center; justify-content: center; gap: 5px;
  height: 38px; padding: 0 16px; border-radius: 7px;
  font-size: .78rem; font-weight: 800; letter-spacing: .02em;
  transition: opacity .12s, box-shadow .12s; white-space: nowrap;
}
.button:disabled { opacity: .3; cursor: not-allowed; }
.button.primary { border: none; color: #fff; background: var(--accent); box-shadow: 0 2px 10px #7c6dfa30; }
.button.primary:not(:disabled):hover { box-shadow: 0 4px 18px #7c6dfa50; }
.button.ghost { border: 1px solid var(--line); color: var(--muted); background: transparent; }
.button.ghost:not(:disabled):hover { border-color: #3a4560; color: var(--text); }
.button.wide { width: 100%; }
.button.sm { height: 32px; padding: 0 12px; font-size: .74rem; }
.button.tiny { height: 24px; padding: 0 9px; font-size: .64rem; border-radius: 5px; }

.icon-btn {
  display: grid; place-items: center; width: 32px; height: 32px;
  border: 1px solid var(--line); border-radius: 7px;
  color: var(--muted); background: var(--surface);
  font-size: 1.1rem; line-height: 1; cursor: pointer;
  transition: color .12s, border-color .12s;
}
.icon-btn:hover { color: var(--text); border-color: #3a4560; }

/* ── Bottom nav ──────────────────────────────────────────────────────────── */
.bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 50;
  display: flex;
  max-width: 680px; margin: 0 auto;
  height: var(--nav-h);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  border-top: 1px solid var(--line);
  background: #060809f5; backdrop-filter: blur(14px);
}
.tab-btn {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 4px;
  border: none; background: transparent;
  color: var(--dim); font-size: .58rem; font-weight: 700;
  letter-spacing: .06em; text-transform: uppercase;
  cursor: pointer; transition: color .13s;
  padding-bottom: 2px;
}
.tab-btn .nav-icon { width: 20px; height: 20px; stroke: currentColor; transition: stroke .13s; }
.tab-btn.selected, .tab-btn[aria-selected="true"] { color: var(--accent); }
.tab-btn.selected .nav-icon { stroke: var(--accent); filter: drop-shadow(0 0 6px #7c6dfa60); }

/* ── Toast ───────────────────────────────────────────────────────────────── */
.toast {
  position: fixed; bottom: calc(var(--nav-h) + 12px); left: 50%;
  transform: translateX(-50%) translateY(20px);
  z-index: 300; padding: 9px 16px;
  border: 1px solid var(--line); border-radius: 8px;
  background: var(--card); color: var(--text);
  font-size: .78rem; font-weight: 700;
  white-space: nowrap; pointer-events: none; opacity: 0;
  transition: transform .2s cubic-bezier(.34,1.56,.64,1), opacity .16s;
}
.toast.show { transform: translateX(-50%) translateY(0); opacity: 1; }
.toast.show.error { background: #160408; border-color: #3d0a14; color: var(--red); }

/* ── Desktop ─────────────────────────────────────────────────────────────── */
@media (min-width: 640px) {
  .panel-inner { padding: 24px 24px calc(var(--nav-h) + 20px); }
  .stats-grid { grid-template-columns: repeat(4, 1fr); }
  .stat-tile__value { font-size: 2.2rem; }
  .channel-list { grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
  .tab-btn { font-size: .62rem; }
  .tab-btn .nav-icon { width: 22px; height: 22px; }
}

/* ── Responsive small ────────────────────────────────────────────────────── */
@media (max-width: 480px) {
  .form-grid { grid-template-columns: 1fr; }
  .form-grid label.span2 { grid-column: span 1; }
  .start-mode-tabs { grid-template-columns: 1fr; }
  .metric-grid { grid-template-columns: repeat(2, 1fr); }
}
