:root {
  --bg: #f3f5f9;
  --card: #ffffff;
  --text: #1b2433;
  --muted: #627089;
  --line: #dde3ec;
  --field: #f8fafc;
  --primary: #0b6bcb;
  --primary-ink: #ffffff;
  --primary-soft: #e3effc;
  --accent: #e9a319;
  --danger: #c83a3a;
  --ok: #1d8a4e;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 2px 8px rgba(16, 24, 40, .05);
  --radius: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141c;
    --card: #171e29;
    --text: #e4e9f1;
    --muted: #93a0b5;
    --line: #2a3443;
    --field: #111821;
    --primary: #4b9cf0;
    --primary-ink: #08111d;
    --primary-soft: #1a2c44;
    --accent: #f0b43c;
    --danger: #ef6b6b;
    --ok: #4cc286;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
/* Thuộc tính hidden phải luôn thắng các class đặt display (vd .auth, .result, button) */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
}

h2 { font-size: 16px; margin: 0 0 12px; display: flex; align-items: center; gap: 8px; }
h2 small { font-weight: 400; color: var(--muted); font-size: 13px; }
kbd { border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; padding: 0 4px; font-size: 12px; background: var(--field); }

/* ---------- thanh trên cùng ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 10px 20px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; font-size: 17px; white-space: nowrap; }
.project-bar { display: flex; gap: 8px; align-items: center; flex: 1; min-width: 0; }
.project-bar select { max-width: 340px; min-width: 0; flex: 1; }
.save-state { font-size: 13px; color: var(--muted); white-space: nowrap; }
.save-state.ok { color: var(--ok); }
.save-state.err { color: var(--danger); font-weight: 600; }

/* ---------- bố cục ---------- */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 20px;
  padding: 20px;
  max-width: 1440px;
  margin: 0 auto;
  align-items: start;
}
.col-main, .col-side { display: flex; flex-direction: column; gap: 20px; min-width: 0; }
.col-side {
  position: sticky; top: 72px;
  max-height: calc(100vh - 92px);
  overflow-y: auto;
  padding-bottom: 4px;
}
@media (max-width: 960px) {
  .layout { grid-template-columns: 1fr; padding: 16px; }
  .col-side { position: static; max-height: none; overflow: visible; }
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
}
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; flex-wrap: wrap; }
.step {
  display: inline-grid; place-items: center;
  width: 24px; height: 24px; border-radius: 50%;
  background: var(--primary); color: var(--primary-ink);
  font-size: 13px; font-weight: 700;
}

/* ---------- đăng nhập ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.auth-card { width: min(420px, 100%); padding: 28px; }
.brand.big { font-size: 22px; margin-bottom: 6px; }
.tabs { display: flex; gap: 4px; background: var(--field); border: 1px solid var(--line); border-radius: 10px; padding: 4px; margin: 16px 0; }
.tab { flex: 1; border: 0; background: transparent; }
.tab.active { background: var(--card); color: var(--primary); box-shadow: var(--shadow); }
.auth:not(.signup) .signup-only, .auth:not(.signup) .invite-only, .auth:not(.invite) .invite-only { display: none; }
.form-error { color: var(--danger); font-weight: 600; margin: 0 0 12px; }

.user-box { display: flex; align-items: center; gap: 6px; margin-left: auto; }
.user-name { font-weight: 600; font-size: 13px; color: var(--muted); max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

dialog {
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--card); color: var(--text);
  padding: 20px; width: min(400px, calc(100vw - 32px));
  box-shadow: 0 12px 40px rgba(0, 0, 0, .3);
}
dialog::backdrop { background: rgba(10, 16, 26, .55); }

/* ---------- form ---------- */
input[type=text], input[type=email], input[type=password], textarea, select {
  width: 100%;
  font: inherit; color: inherit;
  background: var(--field);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
}
textarea { resize: vertical; line-height: 1.55; }
input:focus-visible, textarea:focus-visible, select:focus-visible, button:focus-visible, .scene-img:focus-visible {
  outline: 2px solid var(--primary); outline-offset: 1px;
}
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.field > span { font-weight: 600; font-size: 13px; display: flex; justify-content: space-between; gap: 8px; }
.field > span small { font-weight: 400; color: var(--muted); }
.field output { font-weight: 400; color: var(--muted); }
.inline { display: inline-flex; align-items: center; gap: 8px; font-weight: 600; font-size: 13px; }
.inline select { width: auto; }
.check { display: flex; align-items: center; gap: 8px; margin: 4px 0 12px; cursor: pointer; }
.check input { width: 16px; height: 16px; accent-color: var(--primary); }
input[type=range] { width: 100%; accent-color: var(--primary); }
.color-field input { width: 60px; height: 32px; padding: 2px; border: 1px solid var(--line); border-radius: 6px; background: var(--field); }

details { border-top: 1px solid var(--line); padding: 10px 0 2px; }
details > summary { cursor: pointer; font-weight: 700; margin-bottom: 10px; list-style-position: inside; }
.card > details:first-of-type { border-top: 0; padding-top: 0; }

button, .btn {
  font: inherit; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 7px 14px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--card); color: var(--text);
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}
button:hover:not(:disabled), .btn:hover { border-color: var(--primary); color: var(--primary); }
button:disabled { opacity: .45; cursor: default; }
button.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
button.primary:hover:not(:disabled) { filter: brightness(1.08); color: var(--primary-ink); }
button.big { width: 100%; padding: 12px; font-size: 16px; }
button.small { padding: 4px 10px; font-size: 13px; }
button.ghost { background: transparent; }
button.danger:hover:not(:disabled), #btnDeleteProject:hover { border-color: var(--danger); color: var(--danger); }

.row { display: flex; align-items: center; gap: 8px; }
.row.wrap { flex-wrap: wrap; }
.spacer { flex: 1; }
.hint { color: var(--muted); font-size: 13px; }
p.hint { margin: 0 0 12px; }
.empty { text-align: center; color: var(--muted); padding: 32px 12px; border: 2px dashed var(--line); border-radius: var(--radius); }

/* ---------- cảnh ---------- */
.scene-list { --thumb-ar: 16 / 9; --thumb-w: 220px; display: flex; flex-direction: column; gap: 12px; }
.scene-list.portrait { --thumb-w: 130px; }
.scene-list.square { --thumb-w: 170px; }
.scene {
  display: grid;
  grid-template-columns: 28px var(--thumb-w) minmax(0, 1fr);
  gap: 12px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--card);
  transition: border-color .15s, box-shadow .15s;
}
.scene.active { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.scene-num {
  font-weight: 700; color: var(--muted); text-align: center;
  padding-top: 4px; font-size: 15px;
}
.scene-img {
  position: relative;
  aspect-ratio: var(--thumb-ar);
  border-radius: 8px;
  overflow: hidden;
  background: var(--field);
  border: 2px dashed var(--line);
  cursor: pointer;
  align-self: start;
}
.scene-img.has-img { border-style: solid; border-color: transparent; }
.scene-img.drag { border-color: var(--primary); background: var(--primary-soft); }
.scene-img img { width: 100%; height: 100%; object-fit: cover; display: none; }
.scene-img.has-img img { display: block; }
.img-empty {
  position: absolute; inset: 0;
  display: grid; place-items: center; text-align: center;
  color: var(--muted); font-size: 12px; line-height: 1.4; padding: 6px 6px 36px;
}
.scene-img.has-img .img-empty { display: none; }
.img-tools {
  position: absolute; left: 6px; right: 6px; bottom: 6px;
  display: flex; gap: 6px; justify-content: center;
}
.img-tools button { background: rgba(255, 255, 255, .92); color: #1b2433; border-color: transparent; }
.scene-img .btn-img-remove { display: none; }
.scene-img.has-img .img-tools { opacity: 0; transition: opacity .15s; }
.scene-img.has-img:hover .img-tools, .scene-img.has-img:focus-within .img-tools, .scene.active .scene-img.has-img .img-tools { opacity: 1; }
.scene-img.has-img .btn-img-remove { display: inline-flex; }
.scene-img.uploading::after {
  content: "Đang tải ảnh…";
  position: absolute; inset: 0; display: grid; place-items: center;
  background: rgba(0, 0, 0, .55); color: #fff; font-size: 13px;
}
.scene-body { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.scene-prompt { border: 0; padding: 0; }
.scene-prompt > summary { font-weight: 600; font-size: 13px; color: var(--muted); margin: 0; }
.scene-prompt[open] > summary { margin-bottom: 6px; }
.scene-prompt textarea { font-size: 13px; margin-bottom: 6px; }
.prompt-flag { color: var(--ok); }
.scene-actions { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }

@media (max-width: 640px) {
  .scene { grid-template-columns: 1fr; }
  .scene-num { text-align: left; padding: 0; }
  .scene-img { width: min(100%, 320px); }
  .scene-list.portrait .scene-img { width: 160px; }
  .topbar { padding: 10px 16px; gap: 8px 12px; }
  .project-bar { flex-basis: 100%; order: 3; }
  .project-bar select { max-width: none; }
  .user-name { display: none; }
}

/* ---------- nhạc nền ---------- */
.music-row { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.music-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }

/* ---------- xuất video ---------- */
.progress { margin-top: 14px; }
.bar { height: 10px; border-radius: 99px; background: var(--primary-soft); overflow: hidden; margin-bottom: 8px; }
.bar > div { height: 100%; width: 0; background: linear-gradient(90deg, var(--primary), var(--accent)); transition: width .4s; }
.result { margin-top: 14px; display: flex; flex-direction: column; gap: 8px; }
.result video { width: 100%; max-height: 420px; border-radius: 8px; background: #000; }

/* ---------- thông báo ---------- */
.toast { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 50; max-width: min(420px, calc(100vw - 32px)); }
.toast-item {
  background: var(--text); color: var(--bg);
  padding: 10px 14px; border-radius: 10px; box-shadow: 0 6px 24px rgba(0, 0, 0, .2);
  transition: opacity .35s, transform .35s;
}
.toast-item.ok { background: var(--ok); color: #fff; }
.toast-item.err { background: var(--danger); color: #fff; }
.toast-item.hide { opacity: 0; transform: translateY(8px); }
