* { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

/* dark-theme form controls everywhere (no default white boxes) */
input[type="text"], input[type="password"], textarea {
  background: var(--bg3);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 9px;
  font: inherit;
  font-size: 12px;
}
textarea { resize: vertical; line-height: 1.45; width: 100%; }
input[type="text"]:focus, input[type="password"]:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
}
input[type="file"] { color: var(--muted); font-size: 11px; }
input[type="file"]::file-selector-button {
  background: var(--bg3);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 10px;
  margin-right: 8px;
  cursor: pointer;
  font: inherit;
  font-size: 12px;
}
input[type="range"] { accent-color: var(--accent); }

:root {
  --bg: #0d0f14;
  --bg2: #14171f;
  --bg3: #1c2029;
  --border: #2a2f3c;
  --text: #e8eaf0;
  --muted: #8a90a0;
  --accent: #7c5cff;
  --accent2: #19c8a5;
  --music: #b620c9;
  --ambience: #1e9fd4;
  --sfx: #3f74e0;
  --scene: #e0218a;
}

html, body { height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, sans-serif;
  font-size: 13px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---------- top bar ---------- */
#topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 8px 12px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.brand { font-weight: 600; margin-right: 8px; white-space: nowrap; }
.topbar-group { display: flex; align-items: center; gap: 6px; }
.topbar-group.right { margin-left: auto; }
.transport { gap: 10px; }
.timecode { font-family: Consolas, monospace; font-size: 14px; min-width: 70px; }

.btn {
  background: var(--bg3);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
  font-size: 13px;
  white-space: nowrap;
}
.btn:hover { background: #252a36; }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn.accent { background: var(--accent); border-color: var(--accent); }
.btn.accent:hover { background: #8d70ff; }
.btn.accent2 { background: var(--accent2); border-color: var(--accent2); color: #06281f; font-weight: 600; }
.btn.icon { padding: 6px 10px; }
.select {
  background: var(--bg3);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  max-width: 220px;
}

/* ---------- main ---------- */
#main { flex: 1 1 auto; display: flex; flex-direction: column; min-height: 0; }

#previewRow {
  display: flex;
  flex: 0 0 38%;
  min-height: 200px;
  border-bottom: 1px solid var(--border);
}
#videoWrap {
  flex: 1 1 60%;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}
#video { max-width: 100%; max-height: 100%; }

/* empty-state placeholder shown until a video is opened */
#videoPlaceholder {
  position: absolute;
  inset: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 2px dashed var(--border);
  border-radius: 12px;
  color: var(--muted);
  cursor: pointer;
  text-align: center;
  transition: border-color 0.15s, background 0.15s;
}
#videoPlaceholder:hover, #videoPlaceholder.drop-hover, #videoWrap.drop-hover #videoPlaceholder {
  border-color: var(--accent);
  background: rgba(124, 92, 255, 0.07);
  color: var(--text);
}
#videoPlaceholder .vp-icon { font-size: 34px; }
#videoPlaceholder .vp-title { font-size: 16px; font-weight: 600; }
#videoPlaceholder .vp-sub { font-size: 12px; line-height: 1.5; }

/* ---------- open-video popup ---------- */
.modal-box.video-modal-box {
  max-width: 720px;
  width: min(720px, 92vw);
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}
.modal-box.video-modal-box.drop-hover { outline: 2px dashed var(--accent); outline-offset: -6px; }
.video-modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
#videoGrid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  /* Fixed row height. An `auto` row min-content-sizes each tile IGNORING
     its aspect-ratio, collapsing rows to the caption height so tiles
     overflow into the next row (the overlap bug). A fixed row height +
     an absolutely-positioned caption avoids that entirely. */
  grid-auto-rows: 116px;
  gap: 10px;
  overflow-y: auto;
  padding-right: 4px;
}
.video-tile {
  position: relative;
  height: 100%;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg3);
  overflow: hidden;
  cursor: pointer;
  display: block;
  transition: border-color 0.12s, transform 0.12s;
}
.video-tile:hover { border-color: var(--accent); transform: translateY(-1px); }
.video-tile.current { border-color: var(--accent2); }
.video-tile img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.video-tile.no-thumb::after {
  content: '🎞';
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 30px;
  opacity: 0.5;
}
.video-tile .vt-name {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1;
  padding: 14px 7px 5px;
  font-size: 11px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.85) 55%);
}
.video-tile .vt-expiry {
  position: absolute;
  top: 6px;
  left: 6px;
  z-index: 1;
  background: rgba(255, 71, 87, 0.9);
  color: #fff;
  font-size: 10px;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 8px;
}
.video-tile .vt-badge {
  position: absolute;
  top: 6px;
  right: 6px;
  z-index: 1;
  background: rgba(25, 200, 165, 0.9);
  color: #05221c;
  font-size: 10px;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 8px;
}
.video-tile.upload-tile {
  border-style: dashed;
  background: transparent;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  color: var(--muted);
}
.video-tile.upload-tile:hover { color: var(--text); background: rgba(124, 92, 255, 0.07); }
.video-tile.upload-tile .vt-plus { font-size: 28px; line-height: 1; }
.video-tile.upload-tile .vt-name { position: static; background: none; padding: 0; }

#infoPanel {
  flex: 1 1 40%;
  max-width: 420px;
  border-left: 1px solid var(--border);
  background: var(--bg2);
  overflow-y: auto;
  padding: 12px;
}
.panel-title { font-weight: 600; margin-bottom: 8px; font-size: 14px; }
.panel-title.small { font-size: 12px; color: var(--muted); margin-top: 12px; }
.panel-row { display: flex; align-items: center; gap: 8px; margin: 6px 0; }
.muted { color: var(--muted); line-height: 1.5; }

#sceneInfo .scene-title { font-size: 15px; font-weight: 600; color: var(--scene); margin-bottom: 6px; }
#sceneInfo .scene-desc { line-height: 1.5; }
.scene-events { margin-top: 10px; display: flex; flex-direction: column; gap: 3px; }
.scene-event {
  font-size: 12px;
  line-height: 1.45;
  padding: 3px 6px;
  border-left: 2px solid var(--scene);
  background: var(--bg3);
  border-radius: 0 4px 4px 0;
  cursor: pointer;
}
.scene-event:hover { background: #252a36; }
.scene-event .ev-time {
  font-family: Consolas, monospace;
  color: var(--accent2);
  margin-right: 4px;
}

.options-list { display: flex; flex-direction: column; gap: 4px; margin-top: 6px; }
.option-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 8px;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 5px;
  cursor: pointer;
  overflow: hidden;
}
.option-item:hover { border-color: var(--accent); }
.option-item.current { border-color: var(--accent2); background: #12332b; }
.option-item .rank { color: var(--muted); flex: 0 0 auto; font-size: 11px; }
.option-item .name { flex: 1 1 auto; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.option-item .preview-btn {
  flex: 0 0 auto;
  background: var(--bg2);
  border: 1px solid var(--accent2);
  border-radius: 6px;
  color: var(--accent2);
  cursor: pointer;
  font-size: 16px;
  min-width: 44px;
  padding: 6px 0;
  text-align: center;
}
.option-item .preview-btn:hover { background: var(--accent2); color: #06281f; }
.profile-option {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  margin-top: 6px;
  cursor: pointer;
}
.profile-option:hover { border-color: var(--accent); }
.profile-option input { margin-top: 3px; }
.profile-option .p-name { font-weight: 600; }
.profile-option .p-desc { color: var(--muted); font-size: 12px; line-height: 1.4; }
.profile-option .p-est { color: var(--accent2); font-size: 11px; }
.profile-option .p-rec { color: var(--accent2); font-size: 11px; font-weight: 400; }
.show-row { display: flex; gap: 6px; align-items: center; margin-top: 4px; }
#showNewName {
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 5px;
  padding: 6px 8px;
}
#showSettings { margin-top: 6px; padding: 8px; border: 1px solid var(--border); border-radius: 6px; }

.analyze-opts {
  display: flex;
  gap: 16px;
  margin-top: 10px;
  flex-wrap: wrap;
}
.analyze-opts label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }

#scriptText {
  width: 100%;
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 5px;
  padding: 6px 8px;
  margin-top: 6px;
  font-family: inherit;
  resize: vertical;
}

#librarySearch, #apiKeyInput, #addQuery, #addSearch {
  width: 100%;
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 5px;
  padding: 6px 8px;
  margin-top: 4px;
}

/* ---------- timeline ---------- */
#timelineSection { flex: 1 1 auto; display: flex; flex-direction: column; min-height: 0; }
#timelineScroll { flex: 1 1 auto; overflow: auto; position: relative; }
#timeline { position: relative; min-width: 100%; }
#timelineHint { flex: 0 0 auto; padding: 4px 12px; border-top: 1px solid var(--border); font-size: 11px; }

#ruler {
  height: 24px;
  position: relative;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  margin-left: 110px;
}
.ruler-tick {
  position: absolute;
  top: 0;
  height: 100%;
  border-left: 1px solid var(--border);
  color: var(--muted);
  font-size: 10px;
  padding: 2px 0 0 3px;
  pointer-events: none;
}

.track { display: flex; border-bottom: 1px solid var(--border); }
.track-label {
  flex: 0 0 110px;
  padding: 6px 8px;
  background: var(--bg2);
  font-size: 12px;
  border-right: 1px solid var(--border);
  position: sticky;
  left: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 6px;
}
.addlayer-btn {
  margin-left: auto;
  background: var(--bg3);
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 11px;
  width: 20px; height: 18px;
  cursor: pointer;
  line-height: 1;
}
.addlayer-btn:hover { color: var(--accent2); border-color: var(--accent2); }
.mute-btn {
  background: var(--bg3);
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 10px;
  width: 20px; height: 18px;
  cursor: pointer;
}
.mute-btn.active { background: #c0392b; color: #fff; border-color: #c0392b; }

.track-body { position: relative; flex: 1 1 auto; height: 64px; }
.video-track .track-body { height: 46px; }

/* ---------- script beats row ---------- */
.beats-track .track-body { height: 34px; }
.beat-block {
  position: absolute;
  top: 4px; bottom: 4px;
  background: linear-gradient(180deg, #ffb85c33, #ffb85c14);
  border: 1px solid #ffb85c;
  border-radius: 4px;
  padding: 2px 6px;
  font-size: 11px;
  overflow: hidden;
  cursor: pointer;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.beat-block:hover { background: #ffb85c40; }
.beat-block.selected { background: #ffb85c55; outline: 2px solid #ffb85c; z-index: 2; }
.beat-block .num { color: #ffb85c; font-weight: 700; margin-right: 4px; }
.beats-hint { font-size: 11px; padding: 8px; white-space: nowrap; }
.beat-info-title { font-weight: 600; font-size: 14px; color: #ffb85c; }
.beat-scene-row {
  display: flex;
  gap: 8px;
  padding: 4px 6px;
  border-radius: 5px;
  cursor: pointer;
  font-size: 12px;
}
.beat-scene-row:hover { background: var(--bg3); }
.beat-scene-row .bs-time { color: var(--muted); min-width: 44px; font-variant-numeric: tabular-nums; }
.beat-scene-row .bs-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.scene-block {
  position: absolute;
  top: 4px; bottom: 4px;
  background: linear-gradient(180deg, #e0218a33, #e0218a1a);
  border: 1px solid var(--scene);
  border-radius: 4px;
  padding: 3px 6px;
  font-size: 11px;
  overflow: hidden;
  cursor: pointer;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.scene-block:hover, .scene-block.active { background: #e0218a55; }
.scene-block .num { color: var(--scene); font-weight: 700; margin-right: 4px; }

.clip {
  position: absolute;
  top: 5px; bottom: 5px;
  border-radius: 4px;
  overflow: hidden;
  cursor: grab;
  background: #7c5cff2e; /* default for user-added layers */
  border: 1px solid var(--accent);
  user-select: none;
}
.clip[data-track="music"] { background: #b620c92e; border-color: var(--music); }
.clip[data-track="ambience"] { background: #1e9fd42e; border-color: var(--ambience); }
.clip[data-track="sfx"] { background: #3f74e02e; border-color: var(--sfx); }
.clip.selected { outline: 2px solid #fff; z-index: 3; }
.clip .clip-name {
  font-size: 10px;
  padding: 1px 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: none;
}
.clip canvas { position: absolute; left: 0; bottom: 0; width: 100%; height: 60%; pointer-events: none; opacity: 0.8; }
.clip .handle {
  position: absolute;
  top: 0; bottom: 0;
  width: 7px;
  cursor: ew-resize;
  z-index: 2;
}
.clip .handle.left { left: 0; }
.clip .handle.right { right: 0; }
.clip .handle:hover { background: #ffffff33; }

/* fade in/out: gradient overlay + draggable corner squares (Premiere-style) */
.clip .fade-overlay {
  position: absolute;
  top: 0; bottom: 0;
  pointer-events: none;
  z-index: 1;
}
.clip .fade-overlay.in { left: 0; background: linear-gradient(to right, #000c, #0000); }
.clip .fade-overlay.out { right: 0; background: linear-gradient(to left, #000c, #0000); }
.clip .fade-h {
  position: absolute;
  top: 1px;
  width: 10px; height: 10px;
  background: #e8eaf0;
  border: 1px solid #10131a;
  border-radius: 2px;
  cursor: ew-resize;
  z-index: 4;
  opacity: 0.55;
}
.clip .fade-h:hover, .clip.selected .fade-h { opacity: 1; }

#playhead {
  position: absolute;
  top: 0; bottom: 0;
  left: 110px;
  width: 7px;
  margin-left: -3px;
  background: linear-gradient(to right, transparent 3px, #ff4757 3px, #ff4757 4px, transparent 4px);
  z-index: 8;
  cursor: ew-resize;
}
#playhead::before {
  content: "";
  position: absolute;
  top: 0; left: -2px;
  border: 6px solid transparent;
  border-top-color: #ff4757;
}

/* ---------- export menu / add track ---------- */
.export-wrap { position: relative; }
#exportMenu, #projectMenu {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px;
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 230px;
  box-shadow: 0 8px 24px #0009;
}
#exportMenu button, #projectMenu button {
  background: none;
  border: none;
  color: var(--text);
  text-align: left;
  padding: 8px 10px;
  border-radius: 5px;
  cursor: pointer;
  font-size: 13px;
}
#exportMenu button:hover, #projectMenu button:hover { background: var(--bg3); }
#projectMenu { left: 0; right: auto; }
.btn.small { padding: 3px 8px; font-size: 11px; }
.add-track-row .track-label { border-right: none; }
.track-name { cursor: text; }
.export-links { display: flex; flex-direction: column; gap: 6px; margin-top: 12px; text-align: left; }
.export-links a { color: var(--accent2); text-decoration: none; }
.export-links a:hover { text-decoration: underline; }

/* ---------- overlays ---------- */
#overlay, #settingsModal, #videoModal, #analyzeModal, #savesModal, #rematchModal, #historyModal, #assetsModal, #loginModal {
  position: fixed;
  inset: 0;
  background: #000a;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
#analyzeModal .modal-box, #savesModal .modal-box {
  max-height: 86vh;
  overflow-y: auto;
}
#overlayBox, .modal-box {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 24px 32px;
  min-width: 340px;
  max-width: 480px;
  text-align: center;
}
.modal-box { text-align: left; }
#overlayTitle { font-size: 16px; font-weight: 600; margin-bottom: 8px; }
#overlayBarWrap {
  position: relative;
  margin-top: 14px;
  height: 18px;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 9px;
  overflow: hidden;
}
#overlayBarFill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent), var(--accent2));
  transition: width 0.6s ease;
}
#overlayBarText {
  position: absolute;
  inset: 0;
  font-size: 11px;
  line-height: 18px;
  text-align: center;
  color: var(--text);
}

.spinner {
  margin: 16px auto 0;
  width: 26px; height: 26px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.settings-label { display: block; margin: 10px 0 6px; }
.modal-actions { display: flex; gap: 8px; margin-top: 16px; justify-content: flex-end; }
