:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f1f3f7;
  --border: #e2e6ee;
  --border-strong: #cbd3e1;
  --text: #111827;
  --muted: #64748b;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --green: #059669;
  --amber: #d97706;
  --red: #dc2626;
  --radius: 8px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  min-height: 100vh;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 12px 24px;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 12px; }
.brand-dot {
  width: 34px; height: 34px; border-radius: 10px;
  background: linear-gradient(135deg, #ff6b4a, #ffb347);
}
.brand h1 { font-size: 16px; font-weight: 700; letter-spacing: -0.02em; color: var(--text); }
.brand-sub { font-size: 12px; color: var(--muted); display: block; }

.tabs { display: flex; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 999px; border: 1px solid var(--border); }
.tab {
  background: transparent; border: 1px solid transparent; outline: none; color: var(--muted);
  padding: 8px 18px; border-radius: 999px; font-size: 13px; font-weight: 600;
  cursor: pointer; transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
  display: flex; align-items: center; gap: 6px;
}
.tab:hover { color: var(--text); }
.tab:focus { outline: none; }
.tab.active { background: var(--surface); color: var(--text); border-color: var(--border); }

.badge {
  background: var(--accent); color: #fff; font-size: 11px; font-weight: 700;
  padding: 1px 7px; border-radius: 999px;
}

.status-pill {
  display: flex; align-items: center; gap: 8px; font-size: 12px; font-weight: 600;
  background: var(--surface); border: 1px solid var(--border);
  padding: 6px 12px; border-radius: 999px; color: var(--muted);
}
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--green); }
.status-pill.busy { border-color: var(--accent); color: var(--text); background: rgba(240, 90, 58, 0.06); }
.status-pill.busy .status-dot { background: var(--accent); animation: pulse 1s infinite; }

@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.3; } }

.container { max-width: 1360px; margin: 0 auto; padding: 22px 24px 48px; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

.studio-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.35fr) 390px;
  gap: 24px;
  align-items: start;
}

@media (max-width: 1020px) {
  .studio-grid { grid-template-columns: 1fr; }
  .preview-col { order: -1; }
}

.composer { display: flex; flex-direction: column; gap: 16px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
}

.card-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; margin-bottom: 14px;
}
.card-head h2 { font-size: 14px; font-weight: 700; letter-spacing: -0.01em; color: var(--text); }
.hint { font-size: 12px; color: var(--muted); }

/* Templates */
.template-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.tpl-card {
  background: var(--surface-2); border: 2px solid transparent; border-radius: 6px;
  padding: 10px; cursor: pointer; transition: all 0.15s ease; text-align: center;
}
.tpl-card:hover { border-color: var(--border-strong); }
.tpl-card.selected { border-color: var(--accent); background: rgba(37, 99, 235, 0.08); }
.tpl-thumb-wrap {
  height: 92px; display: flex; align-items: flex-end; justify-content: center;
  background: radial-gradient(circle at center, #e6eaf2, #d4dbe8);
  border-radius: 4px; overflow: hidden; margin-bottom: 8px;
}
.tpl-thumb-wrap img { max-height: 88px; max-width: 100%; object-fit: contain; }
.tpl-name { font-size: 13px; font-weight: 600; color: var(--text); }
.tpl-tags { font-size: 11px; color: var(--muted); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Fields & inputs */
.field { margin-bottom: 12px; }
.field:last-child { margin-bottom: 0; }
.field label { display: block; font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 6px; }

input[type="text"], input[type="password"], textarea, select {
  width: 100%;
  background: #ffffff;
  border: 1px solid #d5dce7;
  color: var(--text);
  border-radius: 6px;
  padding: 10px 12px;
  font-size: 14px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s ease;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); }
textarea { resize: vertical; }

.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.two-col-wide { display: grid; grid-template-columns: 1.3fr 1fr; gap: 20px; align-items: start; }
@media (max-width: 900px) { .two-col-wide, .two-col { grid-template-columns: 1fr; } }
.stack { display: flex; flex-direction: column; gap: 16px; }

/* Sliders & Segmented */
.controls-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; align-items: end; }
.slider-field { display: flex; flex-direction: column; gap: 5px; }
.slider-label { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); line-height: 1.3; }
.slider-label strong { color: var(--text); }

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 34px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: linear-gradient(
    to right,
    var(--accent) 0%,
    var(--accent) var(--pct, 50%),
    var(--surface-2) var(--pct, 50%),
    var(--surface-2) 100%
  );
  padding: 0;
  overflow: hidden;
  cursor: pointer;
  outline: none;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 0;
  height: 34px;
  opacity: 0;
  cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
  width: 0;
  height: 34px;
  border: none;
  background: transparent;
  opacity: 0;
  cursor: pointer;
}
input[type="range"]::-moz-range-track {
  background: transparent;
  border: none;
}

.control-row { display: flex; align-items: flex-end; justify-content: space-between; gap: 10px; }
.control-row .btn-sm { height: 34px; }
.mini-label { display: block; font-size: 12px; color: var(--muted); line-height: 1.3; margin-bottom: 5px; }
.seg-group { display: inline-flex; align-items: center; height: 34px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px; padding: 3px; }
.seg {
  border: 1px solid transparent; outline: none; background: transparent; color: var(--muted);
  padding: 5px 12px; font-size: 12px; font-weight: 600; border-radius: 999px; cursor: pointer;
}
.seg:focus { outline: none; }
.seg.active { background: var(--surface); color: var(--text); border-color: var(--border); }

/* Subtabs & Backgrounds */
.subtabs { display: flex; gap: 4px; background: var(--surface-2); padding: 3px; border-radius: 999px; border: 1px solid var(--border); }
.subtab {
  border: 1px solid transparent; outline: none; background: transparent; color: var(--muted);
  padding: 5px 12px; border-radius: 999px; font-size: 12px; font-weight: 600; cursor: pointer;
}
.subtab:focus { outline: none; }
.subtab.active { background: var(--surface); color: var(--text); border-color: var(--border); }
.bgtab-panel { display: none; }
.bgtab-panel.active { display: block; }

.bg-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 8px;
  max-height: 270px;
  overflow-y: auto;
  padding-right: 4px;
}
.bg-item {
  position: relative;
  aspect-ratio: 9 / 16;
  border-radius: 6px;
  overflow: hidden;
  border: 2px solid var(--border);
  cursor: pointer;
  background: var(--surface-2);
}
.bg-item:hover { border-color: #94a3b8; }
.bg-item.selected { border-color: var(--accent); }
.bg-item img, .bg-item video { width: 100%; height: 100%; object-fit: cover; display: block; }
.bg-pill {
  position: absolute; bottom: 5px; left: 5px; right: 5px;
  background: rgba(17, 24, 39, 0.78); color: #fff; font-size: 10px; font-weight: 600;
  padding: 2px 6px; border-radius: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.search-row { display: flex; gap: 8px; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.row-gap { display: flex; align-items: center; gap: 8px; }

/* Buttons */
.btn {
  border: none; border-radius: 999px; outline: none; font-family: inherit; font-weight: 600;
  cursor: pointer; transition: all 0.15s ease; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font-size: 13px; padding: 9px 18px; text-decoration: none;
}
.btn:focus { outline: none; }
a.btn:hover { text-decoration: none; }
.btn-lg { padding: 13px 24px; font-size: 14px; }
.btn-sm { padding: 6px 13px; font-size: 12px; border-radius: 999px; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border-strong); }
.btn-secondary:hover { background: #e7ebf2; }
.btn-ghost { background: var(--surface); color: var(--muted); border: 1px solid var(--border); }
.btn-ghost:hover { color: var(--text); border-color: var(--border-strong); }
.btn-danger { background: rgba(220, 38, 38, 0.08); color: var(--red); border: 1px solid rgba(220, 38, 38, 0.25); }
.btn-danger:hover { background: rgba(220, 38, 38, 0.14); }
.upload-btn { cursor: pointer; }

.action-bar { display: flex; gap: 12px; }
.action-bar .btn { flex: 1; }

/* Live 9:16 Phone Preview */
.preview-sticky { position: sticky; top: 82px; }
.preview-header { display: flex; align-items: center; justify-content: space-between; font-size: 13px; font-weight: 600; margin-bottom: 10px; color: var(--text); }

.phone-frame {
  position: relative;
  width: 100%;
  max-width: 370px;
  margin: 0 auto;
  aspect-ratio: 9 / 16;
  background: #111827;
  border-radius: 14px;
  overflow: hidden;
  border: 6px solid #1e2430;
  user-select: none;
}

.preview-bg {
  position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover;
}

.preview-captions {
  position: absolute;
  top: 14.5%;
  left: 6%;
  right: 6%;
  display: flex;
  flex-direction: column;
  align-items: center;
  z-index: 3;
  pointer-events: none;
}

.cap-block { display: flex; flex-direction: column; align-items: center; }
.cap-block + .cap-block { margin-top: 11px; }
.cap-line {
  background: rgba(252, 252, 252, 0.98);
  color: #101010;
  font-family: "Helvetica Neue", Arial, sans-serif;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 7px;
  line-height: 1.18;
  text-align: center;
  white-space: pre-wrap;
}
.cap-line + .cap-line { margin-top: -2px; }

.preview-cat {
  position: absolute;
  bottom: -4px;
  z-index: 2;
  object-fit: contain;
  pointer-events: none;
  transition: transform 0.12s ease;
}

.render-log {
  margin-top: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
  color: var(--muted);
  max-height: 110px;
  overflow-y: auto;
}

/* Queue & Gallery */
.jobs-list { display: flex; flex-direction: column; gap: 10px; }
.job-row {
  display: flex; align-items: center; justify-content: space-between; gap: 14px;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 6px; padding: 12px 14px;
}
.job-left { display: flex; align-items: center; gap: 12px; min-width: 0; }
.job-status-icon {
  width: 28px; height: 28px; border-radius: 50%; flex-shrink: 0;
  display: inline-flex; align-items: center; justify-content: center;
  border: 1px solid var(--border);
}
.job-status-icon svg { width: 15px; height: 15px; display: block; }
.job-status-icon.done {
  color: var(--green); background: rgba(5, 150, 105, 0.11); border-color: rgba(5, 150, 105, 0.3);
}
.job-status-icon.rendering {
  color: var(--accent); background: rgba(37, 99, 235, 0.1); border-color: rgba(37, 99, 235, 0.3);
}
.job-status-icon.pending {
  color: var(--amber); background: rgba(217, 119, 6, 0.1); border-color: rgba(217, 119, 6, 0.3);
}
.job-status-icon.failed {
  color: var(--red); background: rgba(220, 38, 38, 0.1); border-color: rgba(220, 38, 38, 0.3);
}
.spin { animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.job-info { min-width: 0; }
.job-title { font-size: 14px; font-weight: 600; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; color: var(--text); }
.job-sub { font-size: 12px; color: var(--muted); margin-top: 3px; }
.job-err { font-size: 12px; color: var(--red); margin-top: 4px; }

.status-chip {
  font-size: 11px; font-weight: 700; padding: 3px 9px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--border);
  text-transform: uppercase; letter-spacing: 0.03em;
}
.status-chip.done, .status-chip.ok { color: var(--green); border-color: rgba(5, 150, 105, 0.3); background: rgba(5, 150, 105, 0.08); }
.status-chip.pending { color: var(--amber); border-color: rgba(217, 119, 6, 0.3); background: rgba(217, 119, 6, 0.08); }
.status-chip.rendering { color: var(--accent); border-color: rgba(37, 99, 235, 0.35); background: rgba(37, 99, 235, 0.09); }
.status-chip.failed { color: var(--red); border-color: rgba(220, 38, 38, 0.3); background: rgba(220, 38, 38, 0.08); }

.outputs-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 16px;
}
.out-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; overflow: hidden; display: flex; flex-direction: column;
}
.out-card video { width: 100%; aspect-ratio: 9 / 16; background: #000; object-fit: cover; }
.out-body { padding: 12px; display: flex; flex-direction: column; gap: 8px; flex: 1; }
.out-name { font-size: 13px; font-weight: 700; word-break: break-word; color: var(--text); }
.out-meta { font-size: 11px; color: var(--muted); }
.out-actions { display: flex; gap: 6px; margin-top: auto; }
.out-actions .btn { flex: 1; }

/* Reddit list */
.reddit-list { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; max-height: 540px; overflow-y: auto; }
.reddit-item {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px;
  padding: 12px; display: flex; justify-content: space-between; align-items: center; gap: 12px;
}
.reddit-title { font-size: 13px; font-weight: 600; color: var(--text); }
.reddit-meta { font-size: 11px; color: var(--muted); margin-top: 3px; }

.users-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
.user-row {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px;
  padding: 8px 12px; font-size: 13px; font-weight: 600;
}
.divider { height: 1px; background: var(--border); margin: 14px 0; }
.mini-section-title { font-size: 12px; font-weight: 700; color: var(--text); margin-bottom: 8px; }

.code-box {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px;
  padding: 12px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px; color: #1e293b; overflow-x: auto; line-height: 1.55;
}

.toast {
  position: fixed; bottom: 22px; right: 22px; z-index: 100;
  background: #111827; color: #ffffff; border: 1px solid #1f2937;
  padding: 12px 18px; border-radius: 8px; font-size: 13px; font-weight: 600;
  opacity: 0; transform: translateY(12px); pointer-events: none; transition: all 0.2s ease;
}
.toast.show { opacity: 1; transform: translateY(0); }
