:root {
  --paper: #FBF5E4;
  --ink: #1A1A1A;
  --accent: #C04D3A;
  --accent-2: #2A78C2;
  --gold: #F5C84B;
  --muted: #6C6033;
  --border: #1A1A1A;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  line-height: 1.55;
}

.wrap { max-width: 760px; margin: 0 auto; padding: 32px 20px 80px; }

h1 {
  font-family: Georgia, "Times New Roman", serif;
  color: var(--accent);
  font-size: 28px;
  margin: 0 0 6px;
}
h2 { font-family: Georgia, serif; font-size: 18px; margin: 28px 0 10px; border-bottom: 1px solid #e3d8c2; padding-bottom: 6px; }
p.dim { color: var(--muted); font-size: 14px; }

.panel {
  background: #fff;
  border: 2px solid var(--border);
  border-radius: 10px;
  padding: 20px;
  margin: 18px 0;
}

label { display: block; font-weight: 600; font-size: 13px; margin: 14px 0 5px; }
input[type=text], input[type=email], input[type=number], textarea, select {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid #ccc;
  border-radius: 7px;
  font-size: 14px;
  font-family: inherit;
}
textarea { min-height: 80px; resize: vertical; }

.btn {
  display: inline-block;
  background: var(--ink);
  color: var(--paper);
  border: none;
  padding: 11px 20px;
  border-radius: 999px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn--ghost { background: transparent; color: var(--ink); border: 2px solid var(--ink); }
.btn--danger { background: var(--accent); }
.btn--small { padding: 6px 14px; font-size: 13px; }

.chip {
  display: inline-block;
  background: #f3efe7;
  border: 1px solid #e3d8c2;
  border-radius: 999px;
  padding: 2px 10px;
  font-size: 12px;
  font-weight: 600;
  margin: 2px 4px 2px 0;
}
.chip--gold { background: var(--gold); }
.chip--green { background: #cdeccd; }

.cka-grid { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0; }
.cka-grid label {
  display: inline-flex; align-items: center; gap: 6px;
  font-weight: 400; font-size: 13px; margin: 0;
  border: 1px solid #ddd; border-radius: 7px; padding: 6px 10px; cursor: pointer;
}
.cka-grid input { width: auto; }

.list-row {
  display: flex; gap: 10px; align-items: flex-start;
  border: 1px solid #eee; border-radius: 8px; padding: 10px; margin: 8px 0;
}
.list-row > div { flex: 1; }

table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { border: 1px solid #e3d8c2; padding: 8px 10px; text-align: left; }
th { background: #f3efe7; }
tr.clickable { cursor: pointer; }
tr.clickable:hover { background: #faf7f1; }

video { width: 100%; border-radius: 8px; background: #000; }

.rec-controls { display: flex; gap: 10px; margin: 12px 0; align-items: center; flex-wrap: wrap; }
.rec-dot { width: 12px; height: 12px; border-radius: 50%; background: var(--accent); display: none; }
.rec-dot.on { display: inline-block; animation: pulse 1s infinite; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.3; } }

/* ============ Instructor Studio ============ */

.wrap--wide { max-width: 900px; }

.template-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 14px; margin-top: 12px; }
.template-card {
  border: 2px solid var(--border); border-radius: 10px; padding: 16px;
  cursor: pointer; background: #fff; text-align: left; font-family: inherit; font-size: 14px;
  transition: transform 0.08s ease, box-shadow 0.08s ease;
}
.template-card:hover { transform: translateY(-2px); box-shadow: 3px 3px 0 var(--border); }
.template-card .tpl-icon { font-size: 26px; }
.template-card h3 { margin: 8px 0 4px; font-family: Georgia, serif; font-size: 16px; }
.template-card p { margin: 0; color: var(--muted); font-size: 13px; }

.progress-track { background: #eee8d8; border-radius: 999px; height: 10px; overflow: hidden; min-width: 160px; flex: 1; }
.progress-fill { background: var(--accent-2); height: 100%; border-radius: 999px; transition: width 0.2s ease; }
.progress-fill.done { background: #4a9a4a; }
.progress-fill.over { background: var(--gold); }

.storyboard-empty {
  border: 2px dashed #cbbf9f; border-radius: 9px; color: var(--muted); font-size: 13px;
  display: flex; align-items: center; justify-content: center; padding: 12px 18px; flex: 1;
}

/* Modal */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(26,26,26,0.55); z-index: 50;
  display: flex; align-items: flex-start; justify-content: center; padding: 4vh 16px; overflow-y: auto;
}
.modal-box {
  background: var(--paper); border: 2px solid var(--border); border-radius: 12px;
  width: 100%; max-width: 760px; padding: 20px 22px; position: relative;
}
.modal-box h2 { margin-top: 0; }
.modal-close { position: absolute; top: 10px; right: 12px; border: none; background: none;
  font-size: 20px; cursor: pointer; color: var(--muted); }

.region-stage { position: relative; display: inline-block; max-width: 100%; }
.region-stage video { display: block; max-width: 100%; }
.region-overlay { position: absolute; inset: 0; cursor: crosshair; }
.region-rect { position: absolute; border: 2px solid var(--gold); background: rgba(245,200,75,0.15);
  box-shadow: 0 0 0 9999px rgba(0,0,0,0.35); pointer-events: none; }

.seg-actions { display: flex; gap: 8px; flex-wrap: wrap; margin: 10px 0; }
.upload-bar { background: #eee8d8; border-radius: 999px; height: 12px; overflow: hidden; margin: 10px 0; }
.upload-bar > div { background: var(--accent-2); height: 100%; transition: width 0.15s linear; }

.bubble-opts { display: flex; gap: 8px; align-items: center; margin: 0 0 10px 26px; font-size: 13px; }
.bubble-opts select { font-size: 13px; padding: 5px 8px; }

/* ============ Studio v3 — dark editor theme ============ */
/* Everything below is scoped to body.theme-studio (class.html only) so the
   admin pages keep the cream look. Token overrides first: components that
   use the CSS variables restyle themselves; the rest get explicit overrides. */

body.theme-studio {
  --paper: #17181b;
  --ink: #e9e7e3;
  --accent: #e06a54;
  --accent-2: #3d8fe0;
  --muted: #8f8a80;
  --border: #33363c;
  --panel: #1f2124;
  --field: #121316;
  --field-border: #3a3d43;
  --hover: #26282c;
  background: var(--paper);
}

.theme-studio .panel { background: var(--panel); border: 1px solid var(--border); }
.theme-studio h2 { border-bottom-color: var(--border); }
.theme-studio input[type=text], .theme-studio input[type=email], .theme-studio input[type=number],
.theme-studio textarea, .theme-studio select {
  background: var(--field); border-color: var(--field-border); color: var(--ink);
}
.theme-studio .chip { background: var(--hover); border-color: var(--field-border); }
.theme-studio .chip--gold { background: #6e5a1f; }
.theme-studio .chip--green { background: #27512a; }
.theme-studio .template-card { background: var(--panel); border: 1px solid var(--border); color: var(--ink); }
.theme-studio .template-card:hover { box-shadow: 3px 3px 0 #000; }
.theme-studio .list-row { border-color: var(--border); }
.theme-studio .cka-grid label { border-color: var(--field-border); }
.theme-studio .progress-track, .theme-studio .upload-bar { background: var(--hover); }
.theme-studio .modal-overlay { background: rgba(0, 0, 0, 0.65); }
.theme-studio .modal-box { background: var(--panel); border: 1px solid var(--border); }
.theme-studio .storyboard-empty { border-color: var(--field-border); }

/* --- Editor grid --- */

#editor {
  display: grid;
  grid-template-areas: "top top top" "outline stage library" "tl tl tl";
  grid-template-rows: 52px 1fr 236px;
  grid-template-columns: 264px 1fr 300px;
  height: 100vh;
}
#editor.lib-collapsed { grid-template-columns: 264px 1fr 0; }
#editor.lib-collapsed #library-pane { display: none; }

/* --- Top bar --- */

#topbar {
  grid-area: top; display: flex; align-items: center; gap: 12px; padding: 0 14px;
  background: var(--panel); border-bottom: 1px solid var(--border);
}
#tb-brand { font-family: Georgia, serif; font-weight: 700; color: var(--accent); white-space: nowrap; }
#topbar #tb-title {
  flex: 1; max-width: 520px; background: transparent; border: 1px solid transparent;
  font-size: 15px; font-weight: 600; color: var(--ink); padding: 6px 10px; border-radius: 7px;
}
#topbar #tb-title:hover, #topbar #tb-title:focus { border-color: var(--field-border); background: var(--field); }
#topbar #save-msg { margin-left: auto; font-size: 12px; }

/* --- Outline sidebar --- */

#outline {
  grid-area: outline; background: #1b1d20; border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
}
#outline-list { flex: 1; overflow-y: auto; padding: 10px 8px; }
.outline-row {
  display: flex; align-items: center; gap: 8px; padding: 7px 10px; margin: 2px 0;
  border-radius: 8px; border: 1px solid transparent; cursor: pointer; font-size: 13px;
}
.outline-row:hover { background: var(--hover); }
.outline-row.active { background: #26313d; border-color: var(--accent-2); }
.outline-row .o-icon { flex: 0 0 auto; }
.theme-studio .outline-row input.o-title {
  flex: 1; min-width: 0; background: transparent; border: none; color: var(--ink);
  font: inherit; font-weight: 600; padding: 2px 4px; overflow: hidden; text-overflow: ellipsis;
}
.theme-studio .outline-row input.o-title:focus { outline: 1px solid var(--field-border); border-radius: 4px; background: var(--field); }
.outline-row .o-meta { font-size: 11px; color: var(--muted); white-space: nowrap; }
.outline-row .o-dot { width: 8px; height: 8px; border-radius: 50%; background: #4b4f56; flex: 0 0 auto; }
.outline-row .o-dot.ok { background: #43b04a; }
.outline-row .o-tools { display: none; gap: 2px; }
.outline-row:hover .o-tools { display: flex; }
.outline-row .o-tools button {
  background: none; border: none; color: var(--muted); cursor: pointer;
  font-size: 12px; padding: 2px 4px; border-radius: 4px;
}
.outline-row .o-tools button:hover:not(:disabled) { color: var(--ink); background: var(--field); }
.outline-row .o-tools button:disabled { opacity: 0.3; cursor: default; }
.outline-row--details { margin-top: 10px; border-top: 1px solid var(--border); border-radius: 0; padding-top: 12px; }

#outline-add { padding: 8px; border-top: 1px solid var(--border); display: flex; flex-wrap: wrap; gap: 6px; }
#outline-add button {
  background: var(--hover); border: 1px solid var(--field-border); color: var(--ink);
  border-radius: 6px; padding: 4px 9px; font-size: 12px; cursor: pointer;
}
#outline-add button:hover { border-color: var(--ink); }
#outline-footer { padding: 10px 12px; border-top: 1px solid var(--border); font-size: 12px; color: var(--muted); }
#outline-footer .progress-track { min-width: 0; margin-top: 6px; height: 6px; }
#outline-footer a { color: var(--accent-2); }

/* --- Stage --- */

#stage { grid-area: stage; display: flex; flex-direction: column; min-width: 0; min-height: 0; background: #101113; }
#stage-player { flex: 1; display: flex; flex-direction: column; padding: 16px 20px 8px; min-height: 0; }
#player-frame { flex: 1; display: flex; align-items: center; justify-content: center; min-height: 0; position: relative; }
#main-video { width: 100%; height: 100%; object-fit: contain; background: #000; border-radius: 6px; }
#player-hud { display: flex; align-items: center; gap: 12px; padding: 10px 2px 4px; color: var(--muted); font-size: 13px; }
#btn-play {
  width: 36px; height: 36px; border-radius: 50%; background: var(--hover);
  border: 1px solid var(--field-border); color: var(--ink); cursor: pointer; font-size: 13px;
}
#btn-play:hover:not(:disabled) { border-color: var(--ink); }
#btn-play:disabled { opacity: 0.4; cursor: default; }
#player-time { font-variant-numeric: tabular-nums; }
#stage-form { flex: 1; overflow-y: auto; padding: 22px; }
#stage-form .panel { max-width: 760px; margin: 0 auto 18px; }
.stage-form-head { max-width: 760px; margin: 0 auto 4px; display: flex; align-items: baseline; gap: 10px; }
.stage-form-head h2 { border: none; margin: 0; }

/* --- Library pane --- */

#library-pane {
  grid-area: library; background: #1b1d20; border-left: 1px solid var(--border);
  overflow-y: auto; padding: 12px 14px; min-height: 0;
}
#library-pane h3 { margin: 4px 0 6px; font-family: Georgia, serif; font-size: 15px; }
#library-pane .dim { font-size: 12px; }

.lib-card {
  display: flex; gap: 10px; align-items: center; padding: 8px;
  border: 1px solid var(--border); border-radius: 8px; margin: 8px 0;
  background: var(--panel); cursor: grab;
}
.lib-card:active { cursor: grabbing; }
.lib-card .lib-thumb {
  position: relative; flex: 0 0 96px; height: 54px; border-radius: 6px; overflow: hidden;
  background: #0c0d0f; cursor: pointer; display: flex; align-items: center; justify-content: center;
}
.lib-card .lib-thumb img { width: 100%; height: 100%; object-fit: cover; }
.lib-card .lib-thumb .tl-dur { top: auto; bottom: 3px; right: 4px; }
.lib-card .lib-glyph { font-size: 20px; opacity: 0.7; }
.lib-card .lib-info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.lib-card .lib-label {
  font-weight: 600; font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.lib-card .lib-meta { font-size: 11px; color: var(--muted); }
.lib-card .lib-actions { display: flex; flex-direction: column; gap: 4px; }
.lib-card .lib-actions .btn { padding: 2px 9px; font-size: 12px; }
.theme-studio .chip--err { background: #5c2a24; }

/* --- Timeline dock --- */

#timeline {
  grid-area: tl; background: var(--panel); border-top: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
}
#tl-toolbar {
  display: flex; align-items: center; gap: 8px; padding: 8px 12px;
  border-bottom: 1px solid var(--hover); flex-wrap: wrap; font-size: 13px; color: var(--muted);
}
#tl-toolbar .tl-spacer { margin-left: auto; }
#tl-scroll { flex: 1; overflow-x: auto; overflow-y: hidden; padding: 6px 12px 10px; display: flex; flex-direction: column; gap: 4px; }
#tl-ruler {
  height: 12px; flex: none; position: relative; cursor: ew-resize; touch-action: none;
  background: repeating-linear-gradient(to right, #33363c 0 1px, transparent 1px 60px);
  border-bottom: 1px solid var(--hover); min-width: 100%;
}
#tl-playhead {
  position: absolute; top: -17px; bottom: 0; left: 0; width: 2px; background: #fff;
  pointer-events: none; z-index: 5; box-shadow: 0 0 4px rgba(0, 0, 0, 0.8);
}
#tl-playhead::before {
  content: ""; position: absolute; top: 0; left: -4px; border: 5px solid transparent;
  border-top-color: #fff; border-bottom-width: 0;
}
#tl-track { position: relative; flex: 1; min-height: 0; display: flex; gap: 8px; align-items: stretch; min-width: 100%; width: max-content; }
.tl-hint { color: var(--muted); font-size: 13px; display: flex; align-items: center; justify-content: center; flex: 1; }

/* Timeline clips (gap above must stay in sync with TL.GAP in studio.js) */
.tl-clip {
  position: relative; height: 100%; border-radius: 8px; overflow: hidden;
  background: #0c0d0f; border: 2px solid #26282c; cursor: pointer; user-select: none;
}
.tl-clip:hover { border-color: #4b4f56; }
.tl-clip.selected { border-color: var(--accent-2); box-shadow: 0 0 0 2px rgba(61, 143, 224, 0.35); }
.tl-clip .tl-thumbs { position: absolute; inset: 0; display: flex; }
.tl-clip .tl-thumbs img { flex: 1 1 0; min-width: 0; height: 100%; object-fit: cover; pointer-events: none; }
.tl-clip .tl-label {
  position: absolute; left: 6px; right: 6px; bottom: 4px; font-size: 11px; font-weight: 600;
  color: #fff; text-shadow: 0 1px 2px #000; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  pointer-events: none;
}
.tl-clip .tl-dur {
  position: absolute; top: 4px; right: 5px; font-size: 10px; color: #fff;
  background: rgba(0, 0, 0, 0.55); padding: 1px 5px; border-radius: 4px; pointer-events: none;
  font-variant-numeric: tabular-nums;
}
.tl-clip .tl-trim-flag {
  position: absolute; top: 4px; left: 5px; font-size: 10px; color: #fff;
  background: rgba(0, 0, 0, 0.55); padding: 1px 5px; border-radius: 4px; pointer-events: none;
}
.tl-clip .tl-glyph { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-size: 22px; opacity: 0.7; }
.tl-clip.processing { border-style: dashed; opacity: 0.75; }
.tl-handle {
  position: absolute; top: 0; bottom: 0; width: 12px; cursor: ew-resize; z-index: 4;
  background: rgba(61, 143, 224, 0.85); display: flex; align-items: center; justify-content: center;
  touch-action: none;
}
.tl-handle::after { content: ""; width: 2px; height: 40%; background: #fff; border-radius: 2px; }
.tl-handle--l { left: 0; border-radius: 6px 0 0 6px; }
.tl-handle--r { right: 0; border-radius: 0 6px 6px 0; }

/* Clip inspector popover */
#clip-popover {
  position: fixed; z-index: 40; width: 388px; background: var(--panel);
  border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5); font-size: 13px;
}
#clip-popover[hidden] { display: none; }
.ci-head { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; }
.ci-head input { flex: 1; }
.ci-head .modal-close { position: static; }
.ci-section { border-top: 1px solid var(--hover); padding: 10px 0 6px; }
#ci-monitor video { width: 100%; margin-top: 8px; border-radius: 6px; background: #000; }
.tl-clip.dragging { opacity: 0.4; }
.tl-clip.drop-before { box-shadow: -4px 0 0 var(--accent-2); }
.tl-clip.drop-after { box-shadow: 4px 0 0 var(--accent-2); }
