:root {
  --bg: #070b14;
  --panel: #0f172a;
  --panel-2: #111827;
  --border: rgba(148, 163, 184, 0.18);
  --text: #e2e8f0;
  --muted: #94a3b8;
  --primary: #0066cc;
  --primary-hover: #1a7ae0;
  --ok: #10b981;
  --warn: #f59e0b;
  --danger: #ef4444;
  --radius: 14px;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--text); min-height: 100%; }
button, input, textarea { font: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; line-height: 1.45; }

.app { min-height: 100vh; display: flex; flex-direction: column; }

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  padding: 0.9rem 1.25rem;
  border-bottom: 1px solid var(--border);
  background: rgba(15, 23, 42, 0.9);
  backdrop-filter: blur(10px);
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand { display: flex; gap: 0.75rem; align-items: center; }
.brand img { border-radius: 10px; }
.brand strong { display: block; font-size: 1rem; }
.brand .muted { font-size: 0.8rem; }
.top-actions { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; justify-content: flex-end; }

.badge {
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  padding: 0.35rem 0.7rem;
  border-radius: 999px;
  background: rgba(245, 158, 11, 0.15);
  color: #fbbf24;
  border: 1px solid rgba(245, 158, 11, 0.35);
}
.badge.ok {
  background: rgba(16, 185, 129, 0.15);
  color: #34d399;
  border-color: rgba(16, 185, 129, 0.35);
}

.btn {
  border: 0;
  border-radius: 999px;
  padding: 0.55rem 1rem;
  font-weight: 600;
  font-size: 0.9rem;
  transition: 0.15s ease;
}
.btn.primary { background: var(--primary); color: white; }
.btn.primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn.secondary { background: rgba(255,255,255,0.08); color: var(--text); border: 1px solid var(--border); }
.btn.secondary:hover { background: rgba(255,255,255,0.12); }
.btn.ghost { background: transparent; color: var(--muted); border: 1px solid transparent; }
.btn.ghost:hover { color: var(--text); border-color: var(--border); }
.btn.danger {
  background: #dc2626;
  color: #fff;
  border: 1px solid transparent;
}
.btn.danger:hover:not(:disabled) {
  background: #ef4444;
}
.btn.danger.ghost { color: #f87171; background: transparent; }
.btn.danger.ghost:hover { color: #fecaca; border-color: rgba(248, 113, 113, 0.45); background: rgba(248, 113, 113, 0.08); }

.confirm-message {
  margin: 0;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.45;
}
.confirm-message strong {
  color: var(--text);
}
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.muted-badge { max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.auth-gate {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 1.5rem;
  background: rgba(8, 12, 20, 0.72);
  backdrop-filter: blur(8px);
}
.auth-gate.hidden { display: none; }
.auth-gate-card {
  width: min(420px, 100%);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 1.5rem;
  box-shadow: var(--shadow);
  text-align: center;
}
.auth-gate-card h2 { margin: 0.75rem 0 0.5rem; font-size: 1.25rem; }
.auth-gate-card p { margin: 0 0 1rem; }
.auth-gate-card .btn { margin-top: 0.35rem; text-decoration: none; display: inline-flex; justify-content: center; align-items: center; }
.btn.block { width: 100%; margin-top: 0.65rem; }

.layout {
  display: grid;
  grid-template-columns: 1fr 380px;
  gap: 1.25rem;
  padding: 1.25rem;
  flex: 1;
}
@media (max-width: 1100px) {
  .layout { grid-template-columns: 1fr; }
}

.section-head h2 { margin: 0 0 0.25rem; font-size: 1.25rem; }
.section-head p { margin: 0 0 1rem; }

.scene-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 1rem;
}
.scene-grid.scene-grid--portrait {
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
}

.scene-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  cursor: grab;
  transition: border-color 0.15s, transform 0.15s, opacity 0.15s;
  display: flex;
  flex-direction: column;
}
.scene-card:active { cursor: grabbing; }
.scene-card:hover { border-color: rgba(59, 130, 246, 0.45); transform: translateY(-2px); }
.scene-card.active { border-color: var(--primary); box-shadow: 0 0 0 1px var(--primary), var(--shadow); }
.scene-card.dragging { opacity: 0.45; transform: scale(0.98); }
.scene-card.drop-target { border-color: var(--ok); box-shadow: 0 0 0 2px rgba(16, 185, 129, 0.35); }

.scene-img-wrap {
  position: relative;
  cursor: zoom-in;
  background: #05070c;
}
.scene-img-wrap img {
  width: 100%;
  aspect-ratio: 16 / 9;
  object-fit: cover;
  display: block;
  background: #000;
}
.scene-card.portrait .scene-img-wrap img {
  aspect-ratio: 9 / 16;
  max-height: 420px;
  object-fit: contain;
  margin: 0 auto;
}
.zoom-hint {
  position: absolute;
  right: 0.65rem;
  bottom: 0.65rem;
  font-size: 0.72rem;
  font-weight: 600;
  padding: 0.28rem 0.55rem;
  border-radius: 999px;
  background: rgba(7, 11, 20, 0.72);
  color: var(--text);
  border: 1px solid var(--border);
  opacity: 0;
  transition: opacity 0.15s;
  pointer-events: none;
}
.scene-img-wrap:hover .zoom-hint { opacity: 1; }
.order-badge {
  position: absolute;
  left: 0.65rem;
  top: 0.65rem;
  width: 28px;
  height: 28px;
  display: grid;
  place-items: center;
  border-radius: 999px;
  background: rgba(0, 102, 204, 0.92);
  color: #fff;
  font-size: 0.8rem;
  font-weight: 700;
  pointer-events: none;
}

.scene-meta { padding: 0.85rem 1rem 1rem; margin-top: auto; }
.scene-meta .row { display: flex; justify-content: space-between; gap: 0.5rem; align-items: baseline; }
.scene-meta h3 { margin: 0; font-size: 1rem; }
.scene-meta .dur {
  font-size: 0.75rem;
  color: var(--muted);
  background: rgba(255,255,255,0.05);
  padding: 0.2rem 0.5rem;
  border-radius: 999px;
}
.scene-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-top: 0.75rem;
}
.scene-edit-btn {
  flex: 1 1 auto;
  min-width: 7.5rem;
}
.btn.mini {
  padding: 0.35rem 0.65rem;
  min-width: 34px;
  border-radius: 8px;
  font-size: 0.85rem;
  border: 1px solid var(--border);
}
.btn.primary.mini {
  border-color: transparent;
}

.sidebar { display: flex; flex-direction: column; gap: 1rem; }
.tools-panel,
.hint-panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
}
.tools-panel > summary {
  cursor: pointer;
  font-weight: 700;
  font-size: 0.95rem;
  list-style: none;
}
.tools-panel > summary::-webkit-details-marker { display: none; }
.tools-panel > summary::before { content: "▸ "; color: var(--muted); }
.tools-panel[open] > summary::before { content: "▾ "; }
.tools-inner { margin-top: 0.85rem; display: flex; flex-direction: column; gap: 0.65rem; }
.tools-inner h4 {
  margin: 0.75rem 0 0.25rem;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}
.tools-inner h4:first-child { margin-top: 0; }
.hint-panel h3 { margin: 0 0 0.5rem; font-size: 0.95rem; }
.hint-panel .help { margin: 0; padding-left: 1.1rem; color: var(--muted); font-size: 0.88rem; }

.scene-edit-dialog {
  width: min(520px, 96vw);
  max-height: 92vh;
}
.scene-edit-body {
  max-height: 92vh;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}
.scene-edit-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 0.75rem;
}
.scene-edit-head h3 { margin: 0.15rem 0 0; }
.scene-edit-fields {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}
.scene-edit-fields label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.85rem;
  color: var(--muted);
}
.scene-edit-fields input,
.scene-edit-fields textarea {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  padding: 0.55rem 0.7rem;
}
.scene-edit-fields textarea { min-height: 72px; resize: vertical; }
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: var(--shadow);
}
.panel h3 { margin: 0 0 0.65rem; font-size: 0.95rem; }

.history-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-height: 280px;
  overflow-y: auto;
}
.history-item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.35rem 0.5rem;
  padding: 0.55rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: rgba(255,255,255,0.03);
  align-items: center;
}
.history-item .meta {
  min-width: 0;
}
.history-item .label {
  font-size: 0.82rem;
  color: var(--text);
  line-height: 1.25;
  word-break: break-word;
}
.history-item .sub {
  font-size: 0.72rem;
  color: var(--muted);
  margin-top: 0.15rem;
}
.history-item .btn.mini {
  margin: 0;
  white-space: nowrap;
}

.color-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.5rem;
}
.color-row label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.78rem;
  color: var(--muted);
}
.color-row input[type="color"] {
  width: 100%;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: transparent;
  padding: 0;
}

#editorFields label {
  display: block;
  margin-bottom: 0.65rem;
  font-size: 0.8rem;
  color: var(--muted);
}
#editorFields input,
#editorFields textarea {
  width: 100%;
  margin-top: 0.3rem;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 0.55rem 0.7rem;
}
#editorFields textarea { min-height: 72px; resize: vertical; }

.badge.engine {
  background: rgba(148, 163, 184, 0.12);
  color: var(--muted);
  border-color: var(--border);
}
.badge.engine.ok {
  background: rgba(16, 185, 129, 0.15);
  color: #34d399;
  border-color: rgba(16, 185, 129, 0.35);
}

.project-select {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  font-size: 0.72rem;
}
.project-select select {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.45rem 0.85rem;
  min-width: 160px;
}

.layout-row {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  margin-top: 0.85rem;
}
.layout-row label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.78rem;
  color: var(--muted);
}
.layout-row select,
.layout-row input[type="range"] {
  width: 100%;
  accent-color: var(--primary);
}
.layout-row select {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.5rem 0.65rem;
}

.chat-panel .assist-scope {
  margin: 0 0 0.75rem;
  padding: 0.65rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.03);
}
.chat-panel .assist-scope summary {
  cursor: pointer;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text);
  list-style: none;
}
.chat-panel .assist-scope summary::-webkit-details-marker { display: none; }
.chat-panel .assist-scope summary::before {
  content: "▸ ";
  color: var(--muted);
}
.chat-panel .assist-scope[open] summary::before { content: "▾ "; }
.scope-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.65rem;
  margin-top: 0.65rem;
}
@media (max-width: 1100px) {
  .scope-grid { grid-template-columns: 1fr; }
}
.scope-title {
  margin: 0 0 0.35rem;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.scope-title.ok { color: #34d399; }
.scope-title.warn { color: #fbbf24; }
.scope-list {
  margin: 0;
  padding-left: 1rem;
  font-size: 0.75rem;
  line-height: 1.45;
  color: var(--muted);
}
.scope-tip { margin: 0.65rem 0 0; }

.dialog {
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--panel);
  color: var(--text);
  padding: 0;
  max-width: 420px;
  width: calc(100% - 2rem);
  box-shadow: var(--shadow);
}
.dialog.wide {
  max-width: 520px;
}
.advanced-fields {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.65rem 0.75rem;
  background: rgba(255,255,255,0.03);
}
.advanced-fields summary {
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 0.35rem;
}
.advanced-fields[open] summary { margin-bottom: 0.75rem; }
.advanced-fields label { margin-top: 0.55rem; }
.field-label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: 0.65rem;
}
.field-label input {
  width: 100%;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 0.55rem 0.7rem;
}
.dialog::backdrop {
  background: rgba(7, 11, 20, 0.65);
  backdrop-filter: blur(4px);
}

.lightbox {
  border: 0;
  padding: 0;
  width: auto;
  max-width: min(1280px, 96vw);
  max-height: 94vh;
  background: transparent;
  color: var(--text);
}
.lightbox::backdrop {
  background: rgba(3, 6, 14, 0.82);
  backdrop-filter: blur(6px);
}
.lightbox-inner {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  overflow: hidden;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  max-height: 94vh;
  width: fit-content;
  max-width: min(1280px, 96vw);
  margin: 0 auto;
}
.lightbox-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
}
.lightbox-inner img {
  display: block;
  width: auto;
  height: auto;
  max-width: min(100vw - 48px, 420px);
  max-height: calc(94vh - 56px);
  object-fit: contain;
  background: #05070c;
  margin: 0 auto;
}

.dialog-body {
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  padding: 1.25rem;
}
.dialog-body h3 { margin: 0; }
.dialog-body label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.85rem;
  color: var(--muted);
}
.dialog-body input,
.dialog-body select,
.dialog-body textarea {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 0.6rem 0.75rem;
}
.dialog-body textarea {
  min-height: 120px;
  resize: vertical;
  line-height: 1.45;
  font: inherit;
}
#briefImageList {
  margin-top: -0.25rem;
}
.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 0.35rem;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.75rem;
  max-width: 520px;
  padding: 1.5rem 1.25rem;
  border: 1px dashed var(--border);
  border-radius: 16px;
  background: rgba(255,255,255,0.03);
  margin-bottom: 1rem;
}
.empty-state.hidden { display: none; }
.empty-state h3 { margin: 0; font-size: 1.25rem; }
.empty-state > p { margin: 0; line-height: 1.45; }
.empty-steps {
  margin: 0.25rem 0 0.5rem;
  padding-left: 1.2rem;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.55;
}
.empty-steps strong { color: var(--text); }
.empty-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.35rem;
}

.coach-strip {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-top: 0.85rem;
  padding: 0.7rem 0.9rem;
  border-radius: 12px;
  border: 1px solid rgba(59, 130, 246, 0.35);
  background: rgba(59, 130, 246, 0.12);
  font-size: 0.88rem;
  line-height: 1.35;
}
.coach-strip.hidden { display: none; }

.tour-dialog .tour-body { gap: 0.7rem; }
.tour-kicker { margin: 0; letter-spacing: 0.04em; text-transform: uppercase; }
.tour-progress {
  display: flex;
  gap: 0.35rem;
}
.tour-progress span {
  flex: 1;
  height: 4px;
  border-radius: 999px;
  background: rgba(255,255,255,0.1);
}
.tour-progress span.active {
  background: var(--primary, #3b82f6);
}
.tour-tips {
  margin: 0;
  padding-left: 1.15rem;
  color: var(--muted);
  font-size: 0.88rem;
  line-height: 1.5;
}
.tour-tips li { margin-bottom: 0.25rem; }
.tour-actions {
  justify-content: space-between;
  align-items: center;
  width: 100%;
}
.tour-nav { display: flex; gap: 0.5rem; }

@media (max-width: 900px) {
  .topbar { flex-direction: column; align-items: stretch; }
  .top-actions { justify-content: flex-start; }
}
.msg { margin-bottom: 0.65rem; font-size: 0.88rem; line-height: 1.4; }
.msg .who { font-weight: 700; font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); display: block; margin-bottom: 0.15rem; }
.msg.user .who { color: #93c5fd; }
.msg.bot .who { color: #6ee7b7; }
.msg.system .who { color: #fbbf24; }

.chat-form { display: flex; gap: 0.4rem; }
.chat-form input {
  flex: 1;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 999px;
  padding: 0.6rem 0.9rem;
}
.chip-row { display: flex; flex-wrap: wrap; gap: 0.35rem; margin-top: 0.65rem; }
.chip {
  border: 1px solid var(--border);
  background: rgba(255,255,255,0.04);
  color: var(--muted);
  border-radius: 999px;
  padding: 0.3rem 0.65rem;
  font-size: 0.75rem;
}
.chip:hover { color: var(--text); border-color: rgba(59,130,246,0.45); }

.pending-list { white-space: pre-wrap; max-height: 160px; overflow: auto; }
.help { margin: 0; padding-left: 1.1rem; color: var(--muted); font-size: 0.85rem; line-height: 1.55; }

.toast {
  margin: auto;
  position: fixed;
  inset: auto auto 1.25rem 50%;
  transform: translateX(-50%);
  background: #111827;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.75rem 1.1rem;
  border-radius: 16px;
  box-shadow: var(--shadow);
  max-width: min(640px, 92vw);
  max-height: 40vh;
  overflow: auto;
  text-align: center;
  font-size: 0.9rem;
  white-space: pre-wrap;
  cursor: pointer;
}
.toast:not(:popover-open) {
  display: none !important;
}
.toast.toast-fallback-open {
  display: block !important;
}
.toast.ok { border-color: rgba(16,185,129,0.4); }
.toast.err {
  border-color: rgba(239,68,68,0.55);
  background: #1f1215;
}

.busy-badge {
  background: rgba(59, 130, 246, 0.2);
  border: 1px solid rgba(59, 130, 246, 0.55);
  color: #93c5fd;
  animation: busy-pulse 1.2s ease-in-out infinite;
}
@keyframes busy-pulse {
  50% { opacity: 0.65; }
}

dialog.busy {
  margin: 0;
  padding: 0;
  border: none;
  width: 100vw;
  height: 100vh;
  max-width: 100vw;
  max-height: 100vh;
  background: rgba(7, 11, 20, 0.62);
  color: var(--text);
  backdrop-filter: blur(3px);
}
dialog.busy::backdrop {
  background: transparent;
}
dialog.busy[open] {
  display: flex;
  align-items: center;
  justify-content: center;
}
.busy-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
  padding: 1.5rem 1.75rem;
  text-align: center;
  max-width: min(420px, 90vw);
}
.busy-label {
  font-size: 1rem;
  font-weight: 600;
}
.busy-hint {
  line-height: 1.4;
  max-width: 28rem;
}
.spinner {
  width: 36px; height: 36px;
  border-radius: 50%;
  border: 3px solid rgba(255,255,255,0.15);
  border-top-color: var(--primary);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
