:root {
  --bg-app: #121212;
  --bg-sidebar: #1e1e1e;
  --bg-header: #1e1e1e;
  --bg-panel: #252525;
  --text-primary: #ececec;
  --text-secondary: #a1a1aa;
  --accent: #a5b4fc; /* Soft Purple/Blue */
  --accent-hover: #818cf8;
  --border: #333;
  --danger: #ef4444;
  --success: #10b981;
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --radius-full: 9999px;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.1);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
}

* {
  box-sizing: border-box;
}

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background-color: var(--bg-app);
  color: var(--text-primary);
  overflow: hidden; /* App-like feel */
}

/* --- Layout --- */
.app-container {
  display: grid;
  grid-template-rows: 50px 1fr;
  grid-template-columns: 320px 1fr;
  height: 100vh;
  width: 100vw;
}

.app-header {
  grid-row: 1;
  grid-column: 1 / -1;
  background-color: var(--bg-header);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  z-index: 10;
}

.sidebar {
  grid-row: 2;
  grid-column: 1;
  background-color: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.main-layout {
  display: contents; /* Allows children to participate in the grid defined in .app-container */
}

.canvas-area {
  grid-row: 2;
  grid-column: 2;
  background-color: var(--bg-app);
  position: relative; /* For floating controls */
  overflow: hidden; /* Internal scrolling handled by viewer */
  display: flex;
  flex-direction: column;
}

/* --- Header Elements --- */
.header-left, .header-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand {
  font-weight: 700;
  font-size: 16px;
  color: var(--text-primary);
  white-space: nowrap;
}

.divider-vertical {
  width: 1px;
  height: 24px;
  background-color: var(--border);
}

.title-input {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-primary);
  font-size: 14px;
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  width: 240px;
  transition: all 0.2s;
}

.title-input:hover {
  border-color: var(--border);
}

.title-input:focus {
  outline: none;
  border-color: var(--accent);
  background: var(--bg-panel);
}

.pexels-credit {
  font-size: 12px;
  color: var(--text-secondary);
  text-decoration: none;
}
.pexels-credit:hover {
  color: var(--accent);
}

/* --- Buttons --- */
button {
  cursor: pointer;
  border: none;
  background: transparent;
  color: var(--text-primary);
  font-family: inherit;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}

.icon-btn {
  padding: 6px;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
}
.icon-btn:hover {
  background-color: rgba(255, 255, 255, 0.1);
  color: var(--text-primary);
}

.primary-btn {
  background-color: var(--accent);
  color: #000;
  font-weight: 600;
  padding: 6px 16px;
  border-radius: var(--radius-md);
  font-size: 14px;
}
.primary-btn:hover {
  background-color: var(--accent-hover);
}
.primary-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.secondary-btn {
  background-color: var(--bg-panel);
  border: 1px solid var(--border);
  color: var(--text-primary);
  padding: 6px 12px;
  border-radius: var(--radius-md);
}
.secondary-btn:hover {
  border-color: var(--text-secondary);
}

.sm-btn {
  font-size: 12px;
  padding: 4px 10px;
}
.sm-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.sm-btn.btn-loading { position: relative; }
.sm-btn.btn-loading::after {
  content: "";
  width: 12px;
  height: 12px;
  border: 2px solid rgba(255,255,255,0.6);
  border-top-color: #fff;
  border-radius: 50%;
  animation: sm-spin 0.8s linear infinite;
  margin-left: 6px;
}
@keyframes sm-spin { to { transform: rotate(360deg); } }

.full-width-btn {
  width: 100%;
  background-color: var(--bg-panel);
  border: 1px solid var(--border);
  padding: 10px;
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-weight: 500;
}
.full-width-btn:hover {
  background-color: rgba(255, 255, 255, 0.05);
  border-color: var(--accent);
}
.full-width-btn:disabled { opacity: 0.6; cursor: not-allowed; }
.full-width-btn.btn-loading::after { content: ""; width: 12px; height: 12px; border: 2px solid rgba(255,255,255,0.6); border-top-color: #fff; border-radius: 50%; animation: sm-spin 0.8s linear infinite; margin-left: 6px; }

/* --- Sidebar --- */
.sidebar-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.section-actions { display: flex; align-items: center; /*gap: 8px;*/ }

textarea#scriptArea {
  width: 100%;
  background-color: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  padding: 12px;
  font-size: 14px;
  line-height: 1.6;
  resize: vertical;
  min-height: 120px;
}

textarea {
    width: 100%;
    background-color: #4b5563;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    padding: 12px;
    font-size: 14px;
    line-height: 1.6;
    resize: vertical;
    min-height: 120px;
}

textarea#scriptArea:focus {
  outline: none;
  border-color: var(--accent);
}

.script-area-wrapper { position: relative; }
.script-zoom-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 28px;
  height: 28px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: rgba(255, 255, 255, 0.05);
  color: var(--text-secondary);
  z-index: 1;
}
.script-zoom-btn:hover { background-color: rgba(255, 255, 255, 0.1); color: var(--text-primary); }

/* Grid item overlay buttons */
.grid-btn:disabled { opacity: 0.7; cursor: not-allowed; }
.grid-btn.btn-loading { position: relative; }
.grid-btn.btn-loading::after {
  content: "";
  width: 12px;
  height: 12px;
  border: 2px solid rgba(255,255,255,0.6);
  border-top-color: #fff;
  border-radius: 50%;
  animation: sm-spin 0.8s linear infinite;
  margin-left: 6px;
}
@keyframes sm-spin { to { transform: rotate(360deg); } }

/* Status List */
.status-list {
  gap: 12px;
}
.status-block {
  background-color: rgba(255, 255, 255, 0.02);
  padding: 8px 12px;
  border-radius: var(--radius-md);
  border: 1px solid rgba(255, 255, 255, 0.05);
}
.status-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 6px;
  font-size: 12px;
}
.status-title {
  color: var(--text-secondary);
}
.status-text {
  color: var(--accent);
  font-family: monospace;
}
.progress {
  height: 4px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: var(--radius-full);
  overflow: hidden;
}
.progress-bar {
  height: 100%;
  width: 0;
  background-color: var(--accent);
  transition: width 0.3s ease;
}

/* --- Viewer / Stage --- */
.viewer {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
}

.canvas-controls {
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-panel);
  padding: 4px 12px;
  border-radius: var(--radius-full);
  border: 1px solid var(--border);
}
.orientation-label {
  font-size: 12px;
  color: var(--text-secondary);
}

/* Toggle Switch */
.toggle-switch {
  position: relative;
  display: inline-block;
  width: 32px;
  height: 18px;
}
.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}
.slider {
  position: absolute;
  cursor: pointer;
  top: 0; left: 0; right: 0; bottom: 0;
  background-color: #4b5563;
  transition: .4s;
  border-radius: 34px;
}
.slider:before {
  position: absolute;
  content: "";
  height: 14px;
  width: 14px;
  left: 2px;
  bottom: 2px;
  background-color: white;
  transition: .4s;
  border-radius: 50%;
}
input:checked + .slider {
  background-color: var(--accent);
}
input:checked + .slider:before {
  transform: translateX(14px);
}

.stage-wrap {
  position: relative;
  margin-bottom: 24px;
  box-shadow: var(--shadow-lg);
}

.stage {
  position: relative;
  background: #000;
  border-radius: var(--radius-lg);
  overflow: hidden;
  border: 1px solid var(--border);
}
.stage #overlayCanvas{
  position:absolute;
  left:0;
  top:0;
  width:100%;
  height:100%;
  pointer-events:none;
  z-index:3;
}

.stage-media {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: none;
}

.subtitle {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: 10%;
  color: #fff;
  background: rgba(0, 0, 0, 0.6);
  padding: 8px 16px;
  border-radius: 8px;
  font-size: 18px;
  max-width: 90%;
  text-align: center;
  backdrop-filter: blur(4px);
  text-shadow: 0 1px 2px rgba(0,0,0,0.5);
}

.credit {
  position: absolute;
  right: 12px;
  bottom: 12px;
  font-size: 10px;
  color: rgba(255, 255, 255, 0.8);
  background: rgba(0, 0, 0, 0.4);
  padding: 4px 8px;
  border-radius: 4px;
}

/* Orientation Logic */
.viewer.orientation-horizontal .stage {
  width: min(800px, 90vw);
  aspect-ratio: 16/9;
}
.viewer.orientation-vertical {
  flex-direction: row;
  justify-content: center;
  align-items: flex-start;
  gap: 24px;
}
.viewer.orientation-vertical .stage {
  width: min(360px, 40vw);
  aspect-ratio: 9/16;
}
.viewer.orientation-vertical .assets {
  width: min(400px, 40vw);
}

/* --- Assets Grid --- */
.assets {
  /*width: 100%;*/
  max-width: 1000px;
}
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(16%, 1fr));
  gap: 12px;
}
.grid-item {
  position: relative;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
  background: var(--bg-panel);
  cursor: pointer;
  transition: transform 0.2s, border-color 0.2s;
}
.grid-item:hover {
  transform: translateY(-2px);
  border-color: var(--accent);
}
.viewer.orientation-horizontal .grid-item {
  aspect-ratio: 16/9;
}
.viewer.orientation-vertical .grid-item {
  aspect-ratio: 9/16;
}
.grid-item img, .grid-item video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* --- Floating Toolbar --- */
.floating-toolbar {
  position: absolute;
  bottom: 32px;
  left: 50%;
  transform: translateX(-50%);
  background-color: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  padding: 8px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  box-shadow: var(--shadow-lg);
  z-index: 100;
  cursor: move;
  -webkit-user-select: none;
  user-select: none;
  touch-action: none;
  resize: none;
  width: max-content;
  box-sizing: border-box;
}

.tool-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--text-primary);
  font-size: 14px;
  font-weight: 500;
  padding: 8px 12px;
  border-radius: var(--radius-full);
}
.tool-btn:hover {
  background-color: rgba(255, 255, 255, 0.1);
}
.tool-btn.record-btn {
  color: var(--text-primary);
}
.tool-btn.record-btn:hover {
  background-color: rgba(239, 68, 68, 0.1);
}
.tool-btn.recording-btn {
  background-color: rgba(239, 68, 68, 0.1);
}

.tool-btn.play-btn {
  color: var(--text-primary);
}
.tool-btn.play-btn:hover {
  background-color: rgba(239, 68, 68, 0.1);
}
.tool-btn.playing-btn {
  background-color: rgba(239, 68, 68, 0.1);
}


.record-dot {
  width: 10px;
  height: 10px;
  background-color: currentColor;
  border-radius: 50%;
}

/* --- Modals --- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(2px);
  z-index: 1000;
  display: none;
  align-items: center;
  justify-content: center;
}

.modal-content {
  background: var(--bg-app);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  max-height: 90vh;
}

.large-modal {
  width: min(92vw, 1080px);
  height: 80vh;
  max-height: 80vh;
}
.settings-modal {
  width: min(92vw, 800px);
}

.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.modal-progress-row { padding: 8px 20px; display: flex; align-items: center; border-bottom: 1px solid var(--border); }
.modal-title {
  font-size: 18px;
  font-weight: 600;
}

.modal-body {
  padding: 20px;
  overflow-y: auto;
      height: 100%;
}

.preview-video {
  width: 100%;
  height: auto;
  border-radius: var(--radius-md);
  background: #000;
}

/* History List */
.history-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 280px;
  overflow-y: auto;
}
.history-item {
  display: grid;
  grid-template-columns: 72px 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 8px;
  border: 1px solid var(--border-color, #1f2937);
  border-radius: var(--radius-md);
  background: var(--panel-bg, #0f1218);
}
.history-thumb {
  width: 72px;
  height: 72px;
  object-fit: cover;
  border-radius: var(--radius-sm);
  background: #000;
}
.history-info {
  font-size: 12px;
  color: var(--muted-text, #9ca3af);
}
.history-actions .sm-btn {
  margin-left: 6px;
}

/* Preview Modal Layout */
.preview-layout {
  display: grid;
  grid-template-columns: 1fr min(30vw, 320px);
  gap: 16px;
      height: 100%;
}
.preview-main {
  display: flex;
  align-items: center;
  justify-content: center;
}
.preview-side {
  overflow-y: auto;
      border: 1px solid var(--border);
    padding: 10px;
}
.preview-side .history-list {
  max-height: none;
  height: 100%;
}

/* Settings Groups */
.settings-group {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 16px;
  margin-bottom: 16px;
}
.settings-modal .settings-group {
  max-height: calc(90vh - 180px);
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}
.row {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  margin-bottom: 12px;
}
.row:last-child {
  margin-bottom: 0;
}
.row label {
  font-size: 13px;
  color: var(--text-secondary);
  min-width: 80px;
}
.row input, .row select {
  background: var(--bg-app);
  border: 1px solid var(--border);
  color: var(--text-primary);
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  font-size: 13px;
}
.row input:focus, .row select:focus {
  outline: none;
  border-color: var(--accent);
}

.hidden {
  display: none !important;
}

/* Ken Burns Effect */
@keyframes kb-zoom {
  from { transform: scale(1.05); }
  to { transform: scale(1.15); }
}
#stageImage.kenburns {
  animation: kb-zoom var(--kb-duration, 4s) linear forwards;
  transform-origin: 50% 50%;
  will-change: transform;
}
