/* Brand palette (Kholasa): #4300ff (violet), #3e53eb (blue),
   #d6dbf9 (pale lavender), #090f26 (navy), #231f20 (charcoal), #ffffff. */
:root,
:root[data-theme="dark"] {
  --bg: #090f26;
  --panel: #111730;
  --panel-2: #181f3f;
  --border: #262d52;
  --border-strong: #383f6b;
  --text: #f2f3fb;
  --muted: #9599bf;
  --accent: #6d7bff;
  --accent-solid: #4300ff;
  --accent-dim: #1f2550;
  --accent-contrast: #ffffff;
  --stage-bg: #000;
  --error-bg: #3a1a1a;
  --error-border: #6b2c2c;
  --error-text: #ffb3b3;
}
:root[data-theme="light"] {
  --bg: #ffffff;
  --panel: #f7f8fd;
  --panel-2: #edeffb;
  --border: #e1e4f5;
  --border-strong: #c6cbef;
  --text: #0d1230;
  --muted: #5c6088;
  --accent: #3e53eb;
  --accent-solid: #4300ff;
  --accent-dim: #d6dbf9;
  --accent-contrast: #ffffff;
  --stage-bg: #000;
  --error-bg: #fdecec;
  --error-border: #f3b4b4;
  --error-text: #b42318;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme]) {
    --bg: #ffffff;
    --panel: #f7f8fd;
    --panel-2: #edeffb;
    --border: #e1e4f5;
    --border-strong: #c6cbef;
    --text: #0d1230;
    --muted: #5c6088;
    --accent: #3e53eb;
    --accent-solid: #4300ff;
    --accent-dim: #d6dbf9;
    --accent-contrast: #ffffff;
    --stage-bg: #000;
    --error-bg: #fdecec;
    --error-border: #f3b4b4;
    --error-text: #b42318;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Thmanyah', sans-serif;
  margin: 0;
}

.app {
  display: grid;
  grid-template-columns: 1fr 340px 440px;
  grid-template-rows: auto 1fr;
  height: 100vh;
  overflow: hidden;
}

/* ---------- Header ---------- */
.header {
  grid-column: 1 / -1;
  grid-row: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 24px;
  border-bottom: 1px solid var(--border);
}
.logo { font-size: 15px; font-weight: 700; letter-spacing: 0.3px; }
.logo span { color: var(--accent); }
.header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}
.theme-toggle-btn {
  width: 36px; height: 36px;
  border-radius: 50%;
  border: 1px solid var(--border-strong);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 15px;
  flex-shrink: 0;
}
.theme-toggle-btn:hover { border-color: var(--accent); }
.theme-toggle-btn:disabled { opacity: 0.35; cursor: not-allowed; }
.theme-toggle-btn:disabled:hover { border-color: var(--border-strong); }
.export-btn {
  background: var(--accent-solid);
  color: var(--accent-contrast);
  border: none;
  border-radius: 8px;
  padding: 9px 20px;
  font-family: inherit;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}
.export-btn:active { transform: scale(0.98); }
.export-btn:disabled {
  opacity: 1;
  cursor: not-allowed;
  background: var(--panel-2);
  color: var(--muted);
  border: 1px solid var(--border-strong);
}

/* ---------- Stage (fills the wide remaining space beside the panel) ---------- */
.stage-col {
  grid-column: 1;
  grid-row: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px;
  gap: 20px;
  overflow-y: auto;
  min-height: 0;
}

.dropzone {
  width: 100%;
  max-width: 480px;
  max-height: 78vh;
  aspect-ratio: 9/16;
  border: 1.5px dashed var(--border-strong);
  border-radius: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  cursor: pointer;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
  padding: 20px;
}
.dropzone:hover { border-color: var(--accent); color: var(--text); }
.dropzone .icon { font-size: 32px; color: var(--border-strong); }
.dropzone:hover .icon { color: var(--accent); }
input[type="file"].hidden { display: none; }

/* video-stage sizes itself around the video's natural aspect ratio,
   set dynamically in JS as --video-ar, so 16:9, 1:1 and 9:16 all
   render at their real shape instead of being force-cropped. */
.video-stage {
  --video-ar: 9/16;
  width: 100%;
  max-width: 480px;
  max-height: 78vh;
  aspect-ratio: var(--video-ar);
  background: var(--stage-bg);
  border-radius: 16px;
  position: relative;
  overflow: hidden;
  display: none;
  margin-inline: auto;
}
.video-stage.active { display: block; }
.video-stage:fullscreen,
.video-stage:-webkit-full-screen {
  max-width: none;
  max-height: none;
  width: 100vw;
  height: 100vh;
  aspect-ratio: unset;
  border-radius: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000;
}
.video-stage:fullscreen video,
.video-stage:-webkit-full-screen video {
  width: auto;
  height: 100%;
  max-width: 100%;
  object-fit: contain;
}
.video-stage video {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #000;
}
.video-stage.fill-mode video {
  object-fit: cover;
}
.caption-layer {
  position: absolute;
  left: 50%;
  top: 82%;
  transform: translate(-50%, -50%);
  width: max-content;
  max-width: 88%;
  display: flex;
  justify-content: center;
  cursor: grab;
  touch-action: none;
}
.caption-layer.dragging {
  cursor: grabbing;
}
.caption-layer.dragging .caption-text {
  outline: 1.5px dashed var(--accent);
  outline-offset: 6px;
}
.guide-line {
  position: absolute;
  background: var(--accent);
  pointer-events: none;
  z-index: 5;
  display: none;
}
.guide-line.visible { display: block; }
.guide-v { top: 0; bottom: 0; width: 1px; transform: translateX(-50%); }
.guide-h { left: 0; right: 0; height: 1px; transform: translateY(-50%); }
.position-presets {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}
.position-preset-btn {
  flex: 1;
  background: var(--panel-2);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: 8px 4px;
  color: var(--text);
  font-size: 12px;
  cursor: pointer;
  text-align: center;
}
.position-preset-btn:hover { border-color: var(--accent); }
.position-preset-btn.active {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-dim);
}
.caption-text {
  display: inline-flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.3em;
  line-height: 1.5;
  text-align: center;
  transition: opacity 0.08s;
}
.caption-word {
  display: inline-block;
  transition: transform 0.12s ease-out, color 0.12s ease-out, background 0.12s ease-out;
  border-radius: 6px;
  padding: 0 0.15em;
}
.caption-word.active {
  transform: scale(var(--hl-scale, 1.12));
}

.stage-controls {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  max-width: 480px;
}
.play-btn {
  width: 36px; height: 36px;
  border-radius: 50%;
  border: 1px solid var(--border-strong);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px;
  flex-shrink: 0;
}
.scrub {
  flex: 1;
  accent-color: var(--accent);
}
.time-label { font-size: 12px; color: var(--muted); min-width: 70px; text-align: left; direction: ltr; }
.replace-link { font-size: 12px; color: var(--muted); cursor: pointer; text-decoration: underline; text-underline-offset: 3px; }
.replace-link:hover { color: var(--text); }

.word-timeline {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  direction: rtl;
}
.word-timeline:empty::after {
  content: 'ارفع فيديو عشان يطلع النص هنا تلقائيًا.';
  font-size: 12px;
  color: var(--muted);
}
.word-timeline-note { margin-top: 14px; }
.word-chip {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  flex: 0 0 auto;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: 14px;
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  padding: 10px 16px;
  cursor: pointer;
  user-select: none;
}
.word-chip .word-chip-time {
  font-size: 10px;
  font-weight: 400;
  color: var(--muted);
  direction: ltr;
}
.word-chip.hidden-word {
  background: transparent;
  color: var(--muted);
  text-decoration: line-through;
  border-style: dashed;
}
.word-chip.current {
  background: var(--accent-solid);
  border-color: var(--accent-solid);
  color: var(--accent-contrast);
}
.word-chip.current .word-chip-time { color: var(--accent-contrast); opacity: 0.8; }

/* ---------- Panel (right) ---------- */
.words-col {
  grid-column: 2;
  grid-row: 2;
  border-inline-start: 1px solid var(--border);
  padding: 20px;
  overflow-y: auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
.words-col-title {
  font-size: 13px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 14px;
}

.panel-col {
  grid-column: 3;
  grid-row: 2;
  border-inline-start: 1px solid var(--border);
  padding: 20px;
  overflow-y: auto;
  min-height: 0;
}

.section {
  margin-bottom: 14px;
  padding: 16px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.section-title {
  font-size: 13px;
  color: var(--text);
  margin-bottom: 14px;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 8px;
}
.section-title::before {
  content: '';
  width: 3px;
  height: 14px;
  border-radius: 2px;
  background: linear-gradient(var(--accent-solid), var(--accent));
  flex-shrink: 0;
}

.field-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}
.field-row label {
  font-size: 13px;
  color: var(--muted);
  min-width: 78px;
  flex-shrink: 0;
}
input[type="range"] { flex: 1; min-width: 0; accent-color: var(--accent); }
.field-val { font-size: 12px; color: var(--muted); min-width: 26px; text-align: left; }

select {
  flex: 1;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: 8px 10px;
  font-family: inherit;
  font-size: 13px;
}

/* The 4 main color fields (text/background/highlight colors) are driven by
   a custom swatch button + preset popover (js/color-picker.js) — the
   native <input type="color"> stays in the DOM as the source of truth
   (everything else in the app still just reads its .value) but is hidden;
   the small "مخصص" native picker inside the popover keeps the classic look. */
input[type="color"].native-color-input {
  /* Explicit top/left (not just position:absolute) so the browser doesn't
     fall back to this element's natural in-flow position when computing
     where to place it — without this, invisible inputs deep in the panel
     were inflating the whole page's scrollable height far past the real
     content, leaving a large dead area to scroll into. */
  position: absolute;
  top: 0; left: 0;
  width: 1px; height: 1px;
  opacity: 0;
  pointer-events: none;
}
.color-popover-custom input[type="color"] {
  width: 32px; height: 28px;
  padding: 0;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: none;
  cursor: pointer;
}
.color-swatch {
  width: 32px; height: 32px;
  border-radius: 8px;
  border: 2px solid var(--border-strong);
  padding: 0;
  cursor: pointer;
  flex-shrink: 0;
}
.color-swatch:hover { border-color: var(--accent); }
.color-popover {
  position: fixed;
  z-index: 100;
  background: var(--panel);
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  padding: 12px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.4);
  width: 204px;
}
.color-popover-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 6px;
  margin-bottom: 10px;
}
.color-swatch-option {
  width: 100%;
  aspect-ratio: 1;
  border-radius: 6px;
  cursor: pointer;
  border: 2px solid transparent;
}
.color-swatch-option:hover { border-color: var(--muted); }
.color-swatch-option.selected { border-color: var(--accent); }
.color-popover-custom {
  display: flex;
  align-items: center;
  gap: 8px;
  border-top: 1px solid var(--border);
  padding-top: 10px;
}
.color-popover-custom label { font-size: 12px; color: var(--muted); flex: 1; }

.upload-mini {
  border: 1px dashed var(--border-strong);
  border-radius: 8px;
  padding: 10px;
  text-align: center;
  font-size: 12px;
  color: var(--muted);
  cursor: pointer;
  margin-top: 8px;
}
.upload-mini:hover { border-color: var(--accent); color: var(--text); }

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.toggle-row label { font-size: 13px; color: var(--text); }
.switch {
  position: relative;
  width: 40px; height: 22px;
  background: var(--panel-2);
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  cursor: pointer;
  flex-shrink: 0;
}
.switch.on { background: var(--accent-dim); border-color: var(--accent); }
.switch .knob {
  position: absolute;
  top: 2px; right: 2px;
  width: 16px; height: 16px;
  border-radius: 50%;
  background: var(--muted);
  transition: transform 0.15s;
}
.switch.on .knob { background: var(--accent); transform: translateX(-18px); }

.style-toggle-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.style-chip {
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: 8px 4px;
  text-align: center;
  font-size: 12px;
  color: var(--muted);
  cursor: pointer;
  background: var(--panel-2);
}
.style-chip.active { border-color: var(--accent); color: var(--accent); }

.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}
.chip-row .style-chip { flex: 0 0 auto; padding: 8px 14px; }

.preset-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-bottom: 10px;
}
.preset-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  padding: 12px 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  background: var(--panel-2);
  cursor: pointer;
}
.preset-btn:hover { border-color: var(--accent); }

/* ---------- Word timeline inline edit ---------- */
.word-chip-input {
  width: 70px;
  background: transparent;
  border: none;
  color: inherit;
  font-family: 'Thmanyah', sans-serif;
  font-size: 12px;
  text-align: center;
  direction: rtl;
  outline: none;
}

.note {
  font-size: 11px;
  color: var(--muted);
  line-height: 1.6;
  margin-top: 8px;
}

.processing-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.75);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--text);
  font-size: 13px;
  text-align: center;
  padding: 20px;
}
.spinner {
  width: 28px; height: 28px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.error-banner {
  background: var(--error-bg);
  border: 1px solid var(--error-border);
  color: var(--error-text);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 12px;
  margin-bottom: 14px;
  display: none;
}

/* ---------- Export modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 20px;
}
.modal-card {
  background: var(--panel);
  border: 1px solid var(--border-strong);
  border-radius: 14px;
  padding: 22px;
  width: 100%;
  max-width: 360px;
  max-height: 90vh;
  overflow-y: auto;
}
.modal-card-wide { max-width: 620px; }
.modal-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
}
.modal-close-btn {
  width: 30px; height: 30px;
  border-radius: 50%;
  border: 1px solid var(--border-strong);
  background: var(--panel-2);
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
}
.modal-close-btn:hover { border-color: var(--accent); }
.modal-actions {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  margin-top: 16px;
}
.modal-actions .export-btn { width: 100%; padding: 13px; font-size: 14px; }
.export-privacy-note { margin: 0; text-align: center; }
.secondary-btn {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: 9px 20px;
  font-family: inherit;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}

.export-group-title {
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
  margin: 16px 0 8px;
}
.export-group-title:first-of-type { margin-top: 0; }
.export-two-col {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}
.export-option-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.export-option-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.export-option-card {
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  padding: 10px 8px;
  text-align: center;
  cursor: pointer;
  background: var(--panel-2);
  flex: 1;
  min-width: 70px;
}
.export-option-card.active { border-color: var(--accent-solid); background: var(--accent-dim); }
.export-option-card.disabled { opacity: 0.35; cursor: not-allowed; }
.export-option-title { font-size: 14px; font-weight: 700; color: var(--text); }
.export-option-subtitle { font-size: 10px; color: var(--muted); margin-top: 2px; }

.export-stats-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 8px;
  margin-top: 18px;
}
.export-stat { text-align: center; }
.export-stat-label { font-size: 11px; color: var(--muted); margin-bottom: 4px; }
.export-stat-value { font-size: 15px; font-weight: 700; color: var(--text); direction: ltr; }
.progress-track {
  width: 100%;
  height: 6px;
  background: var(--panel-2);
  border-radius: 3px;
  overflow: hidden;
  margin-top: 6px;
}
.progress-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 0.15s linear;
}

/* ---------- Narrow screens: stack video above panel, video area
   stays fixed height at top, panel scrolls independently below ---------- */
@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; grid-template-rows: auto auto auto 1fr; }
  .stage-col { grid-row: 2; grid-column: 1; overflow: hidden; min-height: auto; max-height: 45vh; }
  .words-col { grid-row: 3; grid-column: 1; border-inline-start: none; border-top: 1px solid var(--border); max-height: 25vh; }
  .panel-col { grid-row: 4; grid-column: 1; border-inline-start: none; border-top: 1px solid var(--border); }
}
