:root {
  --bg: #08101f;
  --bg-2: #0d1728;
  --card: #111e34;
  --card-2: #16273f;
  --line: #223a56;
  --text: #e8f1fb;
  --muted: #8ba6c6;
  --primary: #38bdf8;
  --primary-2: #22d3ee;
  --accent: #67e8f9;
  --teal: #2dd4bf;
  --warn: #fbbf24;
  --ok: #34d399;
  --danger: #f87171;
  --radius: 14px;
  --glow: 0 0 0 1px rgba(56,189,248,.25), 0 8px 30px rgba(34,211,238,.12);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: "Segoe UI", Tahoma, system-ui, sans-serif;
  background:
    radial-gradient(900px 500px at 85% -10%, #123a52 0%, rgba(18,58,82,0) 55%),
    radial-gradient(1000px 600px at 10% 110%, #0e2f4a 0%, rgba(14,47,74,0) 50%),
    var(--bg);
  color: var(--text);
  overflow: hidden;
}

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px; border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, rgba(17,30,52,0.85), rgba(13,23,40,0.7));
  backdrop-filter: blur(10px);
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo { font-size: 26px; filter: drop-shadow(0 0 8px rgba(56,189,248,.5)); }
.brand h1 {
  margin: 0; font-size: 18px; letter-spacing: .3px;
  background: linear-gradient(90deg, var(--accent), var(--primary));
  -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent;
}
.tag { margin: 0; font-size: 12px; color: var(--muted); }
.topbar-right { display: flex; align-items: center; gap: 10px; }

.pill { font-size: 12px; padding: 5px 10px; border-radius: 999px; border: 1px solid var(--line); }
.pill-warn { color: var(--warn); border-color: #4a3a1c; background: #2a2110; }
.pill-ok { color: var(--ok); border-color: #1e4230; background: #10241a; }

.layout {
  display: grid; grid-template-columns: 420px 1fr; gap: 16px;
  padding: 16px; height: calc(100% - 61px);
}
.panel { min-height: 0; }
.controls { overflow-y: auto; padding-right: 6px; }
.preview { min-height: 0; }

.card {
  background: linear-gradient(180deg, var(--card) 0%, var(--card-2) 100%);
  border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; margin-bottom: 14px;
  transition: border-color .2s, box-shadow .2s;
}
.card.active-step {
  border-color: var(--primary);
  box-shadow: var(--glow);
}
.card-head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.card-head h2 { font-size: 15px; margin: 0; }
.step {
  width: 24px; height: 24px; border-radius: 50%;
  background: linear-gradient(135deg, var(--primary), var(--teal));
  color: #04121f; font-size: 13px; font-weight: 800;
  display: grid; place-items: center;
  box-shadow: 0 0 12px rgba(56,189,248,.35);
}

.btn {
  border: 1px solid var(--line); background: var(--card-2); color: var(--text);
  padding: 10px 14px; border-radius: 10px; cursor: pointer; font-size: 14px;
  transition: .15s; font-family: inherit;
}
.btn:hover:not(:disabled) { border-color: var(--primary); transform: translateY(-1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { background: linear-gradient(135deg, var(--primary), var(--teal)); border-color: transparent; color: #04121f; font-weight: 700; }
.btn-primary:hover:not(:disabled) { box-shadow: 0 6px 20px rgba(45,212,191,.35); }

/* Distinct colours per action */
.btn-info { background: linear-gradient(135deg, #0284c7, #38bdf8); border-color: transparent; color: #04121f; font-weight: 700; }
.btn-info:hover:not(:disabled) { box-shadow: 0 6px 20px rgba(56,189,248,.35); }
.btn-violet { background: linear-gradient(135deg, #6d5efc, #a78bfa); border-color: transparent; color: #fff; font-weight: 700; }
.btn-violet:hover:not(:disabled) { box-shadow: 0 6px 20px rgba(139,123,255,.4); }
.btn-teal { background: linear-gradient(135deg, #0d9488, #2dd4bf); border-color: transparent; color: #04121f; font-weight: 700; }
.btn-teal:hover:not(:disabled) { box-shadow: 0 6px 20px rgba(45,212,191,.4); }
.btn-success { background: linear-gradient(135deg, #059669, #34d399); border-color: transparent; color: #04121f; font-weight: 700; }
.btn-success:hover:not(:disabled) { box-shadow: 0 6px 20px rgba(52,211,153,.4); }
.btn-accent { background: transparent; border-color: var(--accent); color: var(--accent); font-weight: 600; }
.btn-accent:hover:not(:disabled) { background: rgba(103,232,249,.08); box-shadow: 0 6px 20px rgba(103,232,249,.2); }
.btn-warn { background: linear-gradient(135deg, #d97706, #fbbf24); border-color: transparent; color: #1a1206; font-weight: 700; }
.btn-ghost { background: transparent; }
.btn-block { width: 100%; }

.lbl { display: block; font-size: 13px; color: var(--muted); margin: 10px 0 6px; }
.lbl b { color: var(--accent); }
input[type="range"] { width: 100%; accent-color: var(--primary); }
.range-hint { display: flex; justify-content: space-between; font-size: 11px; color: var(--muted); margin: 4px 0 12px; }

/* Multi-clip list */
.clip-list { margin-top: 12px; display: flex; flex-direction: column; gap: 8px; max-height: 320px; overflow-y: auto; }
.clip-item {
  display: flex; align-items: center; gap: 10px; padding: 8px;
  background: var(--bg-2); border: 1px solid var(--line); border-radius: 10px;
  cursor: grab; transition: border-color .15s, box-shadow .15s, opacity .15s;
}
.clip-item:hover { border-color: var(--primary); }
.clip-item.dragging { opacity: .45; cursor: grabbing; }
.clip-item.drop-target { border-color: var(--accent); box-shadow: var(--glow); }
.clip-idx {
  flex-shrink: 0; width: 20px; height: 20px; border-radius: 50%;
  background: linear-gradient(135deg, var(--primary), var(--teal)); color: #04121f;
  font-size: 11px; font-weight: 800; display: grid; place-items: center;
}
.clip-thumb {
  width: 72px; height: 48px; flex-shrink: 0; border-radius: 6px; object-fit: cover;
  background: #0a1626; border: 1px solid var(--line);
  display: grid; place-items: center; font-size: 18px; color: var(--muted);
}
.clip-thumb.loading {
  position: relative;
  overflow: hidden;
  background: linear-gradient(90deg, #0a1626 10%, #15253a 35%, #0a1626 60%);
  background-size: 220% 100%;
  animation: shimmer 1.2s linear infinite;
}
.clip-thumb.loading::after {
  content: '';
  width: 18px; height: 18px; border-radius: 50%;
  border: 2px solid rgba(255,255,255,.18);
  border-top-color: var(--accent);
  animation: spin .7s linear infinite;
}
.clip-body { flex: 1; min-width: 0; }
.clip-name-row { display: flex; align-items: center; gap: 6px; min-width: 0; }
.clip-name { font-size: 12px; font-weight: 600; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.clip-meta { font-size: 10px; color: var(--muted); margin-top: 2px; }
.clip-badge {
  flex-shrink: 0;
  font-size: 10px;
  padding: 2px 7px;
  border-radius: 999px;
  border: 1px solid var(--line);
  white-space: nowrap;
}
.clip-badge.ready { color: var(--ok); background: rgba(52,211,153,.08); border-color: rgba(52,211,153,.25); }
.clip-badge.pending { color: var(--accent); background: rgba(56,189,248,.08); border-color: rgba(56,189,248,.25); }
.clip-badge.wait { color: var(--warn); background: rgba(251,191,36,.08); border-color: rgba(251,191,36,.25); }
.clip-btns { display: flex; gap: 3px; flex-shrink: 0; }
.clip-btns .cbtn {
  width: 26px; height: 26px; border-radius: 6px; border: 1px solid var(--line);
  background: var(--card-2); color: var(--text); cursor: pointer; font-size: 12px;
  display: grid; place-items: center; padding: 0; transition: .12s;
}
.clip-btns .cbtn:hover:not(:disabled) { border-color: var(--primary); }
.clip-btns .cbtn:disabled { opacity: .3; cursor: not-allowed; }
.clip-btns .cbtn.del:hover { border-color: var(--danger); color: var(--danger); }
.clip-tools { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 10px; }
.clip-tools .muted { margin: 0; line-height: 1.45; }
.clip-tools .btn { padding: 5px 10px; font-size: 12px; }
#btnConcat { margin-top: 10px; }

.fileinfo {
  margin-top: 12px; font-size: 13px; background: var(--bg-2);
  border: 1px solid var(--line); border-radius: 10px; padding: 10px;
}
.fileinfo b { color: var(--text); }
.fileinfo .fname { color: var(--accent); word-break: break-all; }

.stats { margin-top: 12px; display: flex; gap: 10px; flex-wrap: wrap; }
.stat {
  flex: 1; min-width: 90px; background: var(--bg-2); border: 1px solid var(--line);
  border-radius: 10px; padding: 8px 10px; text-align: center;
}
.stat .v { font-size: 18px; font-weight: 700; }
.stat .k { font-size: 11px; color: var(--muted); }

.timeline {
  margin-top: 12px; height: 26px; border-radius: 8px; overflow: hidden;
  display: flex; background: #1a2740; border: 1px solid var(--line);
}
.timeline .keep { background: linear-gradient(180deg, var(--teal), var(--ok)); height: 100%; }
.timeline .cut { background: #24344f; height: 100%; }

.export-opts {
  display: grid; grid-template-columns: 1.4fr 1fr 1fr; gap: 8px; margin: 10px 0 0;
}
.export-opts .input, .export-opts select { margin: 0; width: 100%; }
@media (max-width: 900px) {
  .export-opts { grid-template-columns: 1fr; }
}

.switch { display: flex; align-items: center; gap: 10px; font-size: 14px; cursor: pointer; }
.switch input { width: 18px; height: 18px; accent-color: var(--primary); }

.cap-body.disabled { opacity: .4; pointer-events: none; }
.row { display: flex; align-items: center; gap: 10px; }
.row .lbl { margin: 10px 0 6px; }
select, .input {
  background: var(--bg-2); color: var(--text); border: 1px solid var(--line);
  border-radius: 8px; padding: 8px 10px; font-size: 13px; font-family: inherit; width: 100%;
}
input[type="color"] { width: 100%; height: 34px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg-2); cursor: pointer; }

.style-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 12px; margin: 12px 0; }

.preset-bar { margin-top: 12px; }
.preset-btns { display: flex; flex-wrap: wrap; gap: 6px; }
.preset-btns .preset { flex: 1; min-width: 64px; padding: 7px 8px; font-size: 12px; }
.preset-btns .preset:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }

.switch-row { display: flex; flex-wrap: wrap; gap: 16px; margin-top: 6px; }
.switch-row .switch { font-size: 13px; }

.box-section {
  margin-top: 10px; padding: 10px; border: 1px dashed var(--line);
  border-radius: 10px; background: var(--bg-2);
}
.box-opts { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 12px; margin-top: 8px; }
.box-opts.disabled { opacity: .4; pointer-events: none; }

.cap-editor { margin-top: 8px; }
.cap-editor-head { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.cap-list { max-height: 220px; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; }
.cap-line { display: flex; gap: 8px; align-items: center; }
.cap-line .time { font-size: 10px; color: var(--muted); width: 70px; flex-shrink: 0; }
.cap-line input {
  flex: 1; background: var(--bg-2); color: var(--text); border: 1px solid var(--line);
  border-radius: 6px; padding: 6px 8px; font-size: 12px;
}

.workdir-row {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  margin-bottom: 10px; padding: 8px 10px; background: var(--bg-2);
  border: 1px solid var(--line); border-radius: 8px; font-size: 12px;
}
.workdir-row b { color: var(--accent); word-break: break-all; }
.workdir-row .btn { padding: 6px 10px; font-size: 12px; white-space: nowrap; }

.progress-wrap { margin-top: 12px; }
.progress { height: 10px; background: var(--bg-2); border-radius: 999px; overflow: hidden; border: 1px solid var(--line); }
.progress-bar { height: 100%; width: 0%; background: linear-gradient(90deg, var(--primary), var(--accent)); transition: width .2s; }
.muted { font-size: 12px; color: var(--muted); margin-top: 6px; }

.preview-card { height: 100%; display: flex; flex-direction: column; min-height: 0; }
.video-wrap {
  flex: 1; background: #000; border-radius: 12px; overflow: hidden;
  display: flex; align-items: center; justify-content: center;
  min-height: 320px; position: relative;
}
/* Stage sized to video aspect ratio — portrait clips use full height, not a tiny strip */
.video-stage {
  position: relative; flex-shrink: 0;
  max-width: 100%; max-height: 100%;
  box-shadow: 0 0 0 1px rgba(56,189,248,.15), 0 12px 40px rgba(0,0,0,.45);
  border-radius: 8px; overflow: hidden; background: #000;
}
.video-wrap.is-portrait .video-stage { border-radius: 10px; }
.video-wrap.is-portrait::before {
  content: ''; position: absolute; inset: 0; pointer-events: none;
  background: radial-gradient(ellipse at center, rgba(56,189,248,.06) 0%, transparent 70%);
}
.placeholder { color: var(--muted); font-size: 14px; padding: 20px; text-align: center; position: absolute; inset: 0; display: grid; place-items: center; }
#player { width: 100%; height: 100%; background: #000; object-fit: contain; display: block; }

/* Live caption overlay — fills the video stage (same aspect as the clip) */
.cap-overlay {
  position: absolute; inset: 0; pointer-events: none; overflow: hidden;
}
.cap-overlay .cap-text {
  position: absolute; left: 0; right: 0; text-align: center;
  line-height: 1.25;
  /* Break only at word boundaries (spaces / zero-width spaces), never inside a word */
  white-space: normal; word-break: keep-all; overflow-wrap: normal; line-break: strict;
  font-weight: 700;
}
.cap-overlay .cap-text .cap-inner {
  display: inline-block; padding: 0.1em 0.35em; border-radius: 0.15em;
  box-decoration-break: clone; -webkit-box-decoration-break: clone;
}
.cap-overlay .cap-text .w { transition: color .05s linear; }
.live-badge {
  position: absolute; top: 10px; left: 10px; z-index: 3;
  background: rgba(56,189,248,.9); color: #04121f; font-size: 11px; font-weight: 800;
  padding: 4px 8px; border-radius: 6px; letter-spacing: .5px;
}

/* Processing / busy overlay */
.busy-overlay {
  position: absolute; inset: 0; z-index: 5;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  background: rgba(6,14,26,.78); backdrop-filter: blur(4px);
  color: var(--text);
}
.busy-overlay.hidden { display: none !important; }
.busy-ring { position: relative; width: 96px; height: 96px; }
.busy-ring svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.busy-ring circle { fill: none; stroke-width: 5; }
.busy-ring .track { stroke: rgba(255,255,255,.12); }
.busy-ring .bar {
  stroke: var(--accent); stroke-linecap: round;
  stroke-dasharray: 125.6; stroke-dashoffset: 125.6; transition: stroke-dashoffset .25s ease;
  filter: drop-shadow(0 0 6px rgba(103,232,249,.6));
}
.busy-ring .busy-pct {
  position: absolute; inset: 0; display: grid; place-items: center;
  font-size: 20px; font-weight: 800; color: var(--accent);
}
.busy-stage { font-size: 15px; font-weight: 700; letter-spacing: .3px; }
.busy-msg { font-size: 12px; color: var(--muted); max-width: 80%; text-align: center; min-height: 15px; }
.busy-elapsed { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; }

/* Spinner used inside busy buttons */
.btn.loading { position: relative; color: transparent !important; pointer-events: none; }
.btn.loading::after {
  content: ''; position: absolute; inset: 0; margin: auto;
  width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid rgba(255,255,255,.35); border-top-color: #fff;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -20% 0; }
}

/* Drag & drop hint */
.video-wrap.dragover { outline: 2px dashed var(--accent); outline-offset: -8px; }
.drop-hint {
  position: absolute; inset: 0; z-index: 6; display: none;
  align-items: center; justify-content: center; pointer-events: none;
  background: rgba(56,189,248,.12); color: var(--accent); font-size: 16px; font-weight: 700;
}
.video-wrap.dragover .drop-hint { display: flex; }
.preview-actions { display: flex; gap: 10px; margin-top: 12px; flex-wrap: wrap; }
.preview-actions .btn { flex: 1; }

.hidden { display: none !important; }

.modal { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: grid; place-items: center; z-index: 50; }
.modal-box { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 22px; width: 440px; max-width: 90vw; }
.modal-box h2 { margin: 0 0 6px; font-size: 17px; }
.modal-box a { color: var(--accent); }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 16px; }
.input { margin-top: 10px; }

/* History modal */
.modal-lg { width: 640px; max-width: 92vw; }
.btn-danger { color: var(--danger); border-color: #5a2a2a; }
.btn-danger:hover:not(:disabled) { border-color: var(--danger); background: rgba(248,113,113,.08); }
.hist-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.hist-head h2 { margin: 0; font-size: 17px; }
.hist-head-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.hist-head-actions .btn { padding: 7px 10px; font-size: 12px; }
.hist-list { max-height: 52vh; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; }
.hist-empty { padding: 30px 10px; text-align: center; color: var(--muted); font-size: 13px; }
.hist-item {
  display: flex; align-items: center; gap: 12px; padding: 10px 12px;
  background: var(--bg-2); border: 1px solid var(--line); border-radius: 10px;
}
.hist-item.missing { opacity: .55; }
.hist-thumb {
  width: 54px; height: 54px; flex-shrink: 0; border-radius: 8px;
  background: linear-gradient(135deg, #16324a, #0d2138);
  display: grid; place-items: center; font-size: 22px; border: 1px solid var(--line);
}
.hist-info { flex: 1; min-width: 0; }
.hist-name { font-size: 13px; font-weight: 600; color: var(--text); word-break: break-all; }
.hist-meta { font-size: 11px; color: var(--muted); margin-top: 3px; }
.hist-badge {
  display: inline-block; font-size: 10px; padding: 1px 6px; border-radius: 999px;
  border: 1px solid var(--line); margin-right: 4px; color: var(--accent);
}
.hist-actions { display: flex; gap: 6px; flex-shrink: 0; }
.hist-actions .btn { padding: 6px 8px; font-size: 12px; }

/* Central Auth login gate */
.login-overlay {
  position: fixed; inset: 0; z-index: 100;
  background:
    radial-gradient(800px 500px at 50% -10%, #123a52 0%, rgba(18,58,82,0) 55%),
    radial-gradient(700px 500px at 50% 110%, #0e2f4a 0%, rgba(14,47,74,0) 55%),
    var(--bg);
  display: grid; place-items: center;
}
.login-box { box-shadow: var(--glow), 0 20px 60px rgba(0,0,0,.5); }
.login-overlay.hidden { display: none !important; }
.login-box {
  width: 380px; max-width: 90vw; background: var(--card);
  border: 1px solid var(--line); border-radius: 16px; padding: 26px;
  box-shadow: 0 20px 60px rgba(0,0,0,.5);
}
.login-brand { display: flex; align-items: center; justify-content: center; gap: 10px; margin-bottom: 8px; }
.login-brand h1, .login-brand h2 { margin: 0; font-size: 22px; }
.login-box .input { width: 100%; margin-top: 14px; }
.login-error { color: var(--danger); font-size: 13px; margin-top: 10px; white-space: pre-line; text-align: center; min-height: 18px; }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: #2f3547; border-radius: 999px; }
::-webkit-scrollbar-track { background: transparent; }
