:root {
  --bg-app: #090b12;
  --bg-panel: #111522;
  --bg-elevated: #181e30;
  --bg-input: #0c101b;
  --border: rgba(148, 163, 184, 0.14);
  --border-focus: #6366f1;
  --text-main: #f1f5f9;
  --text-muted: #94a3b8;
  --accent: #6366f1;
  --accent-hover: #4f46e5;
  --accent-pink: #ec4899;
  --success: #10b981;
  --danger: #ef4444;
  --radius: 12px;
}

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

body {
  font-family: 'Segoe UI', Inter, system-ui, -apple-system, sans-serif;
  background: var(--bg-app);
  color: var(--text-main);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Top Header */
.topbar {
  height: 60px;
  padding: 0 20px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-icon {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), var(--accent-pink));
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: bold;
  box-shadow: 0 4px 16px rgba(99, 102, 241, 0.35);
}

.brand-title {
  font-weight: 700;
  font-size: 1.05rem;
  letter-spacing: -0.01em;
}

.brand-title span {
  color: #818cf8;
  font-weight: 600;
}

.brand-sub {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 16px;
}

.template-picker {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.82rem;
  color: var(--text-muted);
}

.template-picker select {
  width: 240px;
  padding: 6px 10px;
  font-size: 0.82rem;
}

.engine-badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
  background: rgba(148, 163, 184, 0.1);
  border: 1px solid var(--border);
}

.engine-badge .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #f59e0b;
}

.engine-badge.ready {
  background: rgba(16, 185, 129, 0.12);
  border-color: rgba(16, 185, 129, 0.35);
  color: #6ee7b7;
}

.engine-badge.ready .dot {
  background: var(--success);
  box-shadow: 0 0 8px var(--success);
}

.engine-badge.error {
  background: rgba(239, 68, 68, 0.12);
  border-color: rgba(239, 68, 68, 0.35);
  color: #fca5a5;
}

.engine-badge.error .dot {
  background: var(--danger);
}

/* Main 3-Column Workspace */
.workspace {
  flex: 1;
  display: grid;
  grid-template-columns: 380px 1fr 340px;
  min-height: 0;
}

.panel {
  background: var(--bg-panel);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow-y: auto;
}

.panel-left {
  border-right: 1px solid var(--border);
}

.panel-right {
  border-left: 1px solid var(--border);
  padding: 16px;
}

.panel-header {
  padding: 14px 16px 10px;
}

.panel-right .panel-header {
  padding: 0 0 12px;
}

.panel-header h2 {
  font-size: 0.92rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: #cbd5e1;
}

/* Source Tabs */
.tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  margin: 0 16px 12px;
  padding: 4px;
  background: var(--bg-input);
  border-radius: 10px;
  border: 1px solid var(--border);
}

.tab {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 0.8rem;
  font-weight: 600;
  padding: 7px 6px;
  border-radius: 7px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.tab:hover {
  color: var(--text-main);
}

.tab.active {
  background: var(--bg-elevated);
  color: #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
}

.tab-content {
  display: none;
  flex: 1;
  flex-direction: column;
  padding: 0 16px 16px;
  min-height: 0;
}

.tab-content.active {
  display: flex;
}

.editor-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.hint {
  font-size: 0.75rem;
  color: var(--text-muted);
}

#htmlEditor {
  flex: 1;
  width: 100%;
  resize: none;
  background: var(--bg-input);
  color: #e2e8f0;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  font-family: 'Cascadia Code', Consolas, 'Courier New', monospace;
  font-size: 12.5px;
  line-height: 1.5;
  outline: none;
}

#htmlEditor:focus {
  border-color: var(--border-focus);
}

/* Upload Drop Zone */
.drop-zone {
  border: 2px dashed rgba(99, 102, 241, 0.4);
  background: rgba(99, 102, 241, 0.04);
  border-radius: 14px;
  padding: 32px 20px;
  text-align: center;
  transition: all 0.2s ease;
  cursor: pointer;
}

.drop-zone.dragover {
  border-color: var(--accent);
  background: rgba(99, 102, 241, 0.12);
}

.drop-icon {
  font-size: 2.2rem;
  margin-bottom: 10px;
}

.drop-title {
  font-weight: 600;
  font-size: 0.92rem;
  margin-bottom: 8px;
}

.drop-desc {
  font-size: 0.78rem;
  color: var(--text-muted);
  line-height: 1.45;
  margin-bottom: 16px;
}

.upload-info {
  margin-top: 14px;
  padding: 12px;
  border-radius: 10px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  font-size: 0.82rem;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

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

.upload-info-row .label {
  color: var(--text-muted);
}

/* Center Stage */
.stage-area {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: radial-gradient(circle at center, #131829 0%, #080a11 100%);
  position: relative;
}

.stage-toolbar {
  height: 48px;
  padding: 0 18px;
  border-bottom: 1px solid var(--border);
  background: rgba(17, 21, 34, 0.75);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.stage-tabs {
  display: flex;
  gap: 8px;
}

.stage-tab {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-muted);
  padding: 5px 12px;
  border-radius: 8px;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
}

.stage-tab:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.stage-tab.active {
  background: rgba(99, 102, 241, 0.16);
  border-color: rgba(99, 102, 241, 0.45);
  color: #c7d2fe;
}

.stage-controls {
  display: flex;
  align-items: center;
  gap: 10px;
}

.res-pill {
  font-size: 0.76rem;
  color: var(--text-muted);
  background: var(--bg-input);
  padding: 4px 10px;
  border-radius: 99px;
  border: 1px solid var(--border);
}

.stage-container {
  flex: 1;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  overflow: hidden;
  min-height: 0;
}

.preview-wrapper {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.iframe-scaler {
  position: relative;
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.65), 0 0 0 1px rgba(255, 255, 255, 0.1);
  border-radius: 6px;
  overflow: hidden;
  background: #000;
}

#previewFrame {
  border: none;
  display: block;
  transform-origin: top left;
  background: #000;
}

/* Video Result View */
.result-wrapper {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
}

.video-box {
  flex: 1;
  min-height: 0;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

#outputVideo {
  max-width: 100%;
  max-height: 100%;
  border-radius: 10px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.7);
  border: 1px solid var(--border);
  background: #000;
}

.result-bar {
  width: 100%;
  max-width: 760px;
  padding: 12px 16px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.result-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  font-size: 0.8rem;
  color: #cbd5e1;
}

.meta-chip {
  background: var(--bg-elevated);
  padding: 4px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
}

/* Progress Overlay */
.render-overlay {
  position: absolute;
  inset: 0;
  background: rgba(8, 10, 17, 0.82);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
}

.render-card {
  width: min(90%, 430px);
  background: var(--bg-panel);
  border: 1px solid rgba(99, 102, 241, 0.35);
  border-radius: 18px;
  padding: 28px 24px;
  text-align: center;
  box-shadow: 0 25px 70px rgba(0, 0, 0, 0.65);
}

.spinner-ring {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 3px solid rgba(99, 102, 241, 0.2);
  border-top-color: var(--accent);
  border-right-color: var(--accent-pink);
  margin: 0 auto 14px;
  animation: spin 0.85s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.render-card h3 {
  font-size: 1.05rem;
  margin-bottom: 6px;
}

.render-msg {
  font-size: 0.83rem;
  color: var(--text-muted);
  margin-bottom: 18px;
}

.progress-track {
  height: 10px;
  background: var(--bg-input);
  border-radius: 99px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--accent-pink));
  transition: width 0.18s ease;
}

.progress-stats {
  display: flex;
  justify-content: space-between;
  margin: 10px 0 18px;
  font-size: 0.78rem;
  color: #cbd5e1;
  font-variant-numeric: tabular-nums;
}

/* Alert Banner */
.alert-banner {
  position: absolute;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(127, 29, 29, 0.95);
  border: 1px solid rgba(248, 113, 113, 0.5);
  color: #fecaca;
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 0.84rem;
  display: flex;
  align-items: center;
  gap: 12px;
  z-index: 30;
  max-width: 90%;
}

.alert-banner button {
  background: none;
  border: none;
  color: #fecaca;
  cursor: pointer;
  font-size: 0.9rem;
}

/* Right Settings Form */
.settings-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

label {
  font-size: 0.77rem;
  font-weight: 600;
  color: #cbd5e1;
}

input[type="text"],
input[type="url"],
input[type="number"],
select {
  width: 100%;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text-main);
  font-size: 0.84rem;
  outline: none;
}

input:focus,
select:focus {
  border-color: var(--border-focus);
}

.field-help {
  font-size: 0.74rem;
  color: var(--text-muted);
  margin: 4px 0 8px;
  line-height: 1.4;
}

.color-picker-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg-input);
  height: 35px;
}

.color-picker-wrap input[type="color"] {
  border: none;
  width: 24px;
  height: 24px;
  background: none;
  cursor: pointer;
}

.color-picker-wrap span {
  font-size: 0.8rem;
  font-family: monospace;
  color: var(--text-muted);
}

.summary-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  border-radius: 9px;
  background: rgba(99, 102, 241, 0.08);
  border: 1px solid rgba(99, 102, 241, 0.25);
  font-size: 0.8rem;
}

.summary-box strong {
  color: #a5b4fc;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: 9px;
  border: 1px solid transparent;
  font-size: 0.84rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: all 0.15s ease;
}

.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn-sm {
  padding: 5px 10px;
  font-size: 0.78rem;
}

.btn-lg {
  padding: 12px 18px;
  font-size: 0.94rem;
}

.btn-block {
  width: 100%;
}

.btn-primary {
  background: linear-gradient(135deg, var(--accent), #7c3aed);
  color: #fff;
  box-shadow: 0 6px 20px rgba(99, 102, 241, 0.35);
}

.btn-primary:hover:not(:disabled) {
  filter: brightness(1.1);
}

.btn-secondary {
  background: var(--bg-elevated);
  border-color: var(--border);
  color: var(--text-main);
}

.btn-secondary:hover:not(:disabled) {
  border-color: rgba(148, 163, 184, 0.35);
}

.btn-ghost {
  background: transparent;
  border-color: var(--border);
  color: #cbd5e1;
}

.btn-ghost:hover {
  background: var(--bg-elevated);
}

.btn-danger {
  background: rgba(239, 68, 68, 0.16);
  border-color: rgba(239, 68, 68, 0.4);
  color: #fca5a5;
}

.btn-danger:hover {
  background: rgba(239, 68, 68, 0.28);
}

/* Render History */
.history-section {
  margin-top: 18px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}

.history-section h3 {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.history-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.history-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  font-size: 0.78rem;
}

.history-item-title {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 170px;
}

.history-item-actions {
  display: flex;
  gap: 6px;
}

.history-item-actions a,
.history-item-actions button {
  font-size: 0.74rem;
  color: #818cf8;
  background: none;
  border: none;
  cursor: pointer;
  text-decoration: none;
}

.hidden {
  display: none !important;
}

@media (max-width: 1180px) {
  body {
    overflow: auto;
    height: auto;
  }
  .workspace {
    grid-template-columns: 1fr;
  }
  .stage-container {
    min-height: 460px;
  }
  #htmlEditor {
    min-height: 280px;
  }
}
