:root {
  --bg: #0e1320;
  --panel: #151b2c;
  --panel2: #1c2438;
  --line: #2a3350;
  --text: #f4f1ea;
  --muted: #9aa4bf;
  --yellow: #ffd23f;
  --cyan: #4cc9f0;
  --coral: #ff6b6b;
  --green: #5ce1a6;
  --r: 10px;
  font-family: -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  color-scheme: dark;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--bg); color: var(--text); display: grid; grid-template-columns: 272px 1fr; height: 100vh; height: 100dvh; overflow: hidden; -webkit-text-size-adjust: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
button { background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 7px 12px; cursor: pointer; }
button:hover:not(:disabled) { border-color: var(--muted); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.primary { background: var(--yellow); color: #111; border-color: var(--yellow); font-weight: 600; }
button.ghost { background: transparent; }
button.danger { color: var(--coral); }
button.small { padding: 3px 8px; font-size: 12px; }
button.wide { width: 100%; }
input, select, textarea { background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; width: 100%; }
input[type="checkbox"], input[type="radio"] { width: auto; padding: 0; accent-color: var(--yellow); vertical-align: middle; }
button:focus-visible, a:focus-visible { outline: 2px solid var(--cyan); outline-offset: 2px; }
textarea { resize: vertical; min-height: 60px; line-height: 1.45; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--cyan); }
label { display: block; color: var(--muted); font-size: 12px; margin: 10px 0 4px; }
a { color: var(--cyan); }
h2 { margin: 0; font-size: 20px; }
h3 { margin: 18px 0 8px; font-size: 15px; }

#side { background: var(--panel); border-right: 1px solid var(--line); padding: 14px; display: flex; flex-direction: column; gap: 10px; overflow: auto; }
.brand { font-weight: 700; font-size: 17px; margin-bottom: 4px; }
.brand span { color: var(--yellow); }
#projects { display: flex; flex-direction: column; gap: 2px; flex: 1; overflow: auto; margin: 0 -6px; padding: 0 6px; }
#side { overflow: hidden; }
#search { padding: 7px 10px; }
.proj { text-align: left; background: transparent; border: 1px solid transparent; display: flex; flex-direction: column; gap: 4px; padding: 8px 10px; line-height: 1.3; }
.proj { flex-shrink: 0; }
.proj:hover { background: var(--panel2); }
.proj.on { background: var(--panel2); border-color: var(--line); }
.proj .t { font-weight: 600; }
.badge { display: inline-block; font-size: 11px; padding: 1px 7px; border-radius: 99px; background: var(--line); color: var(--muted); width: fit-content; }
.badge.kich-ban { color: var(--cyan); }
.badge.giong { color: var(--yellow); }
.badge.dung { color: var(--coral); }
.badge.xong { color: #111; background: var(--green); }

#main { overflow: auto; padding: 0 28px 120px; }
#main > * { max-width: 1180px; }
.empty { color: var(--muted); margin-top: 30vh; text-align: center; }
.head { display: flex; align-items: center; gap: 12px; padding-top: 22px; flex-wrap: wrap; }
.head h2 { line-height: 1.25; }
.head .grow { flex: 1; }
.tabs { display: flex; gap: 4px; margin: 10px 0 16px; border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; background: var(--bg); overflow-x: auto; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tabs button { background: transparent; border: none; border-bottom: 2px solid transparent; border-radius: 0; padding: 12px 14px 10px; color: var(--muted); white-space: nowrap; flex: 0 0 auto; }
.tabs button.on { color: var(--text); border-bottom-color: var(--yellow); }
.tabs button .n { display: inline-grid; place-items: center; width: 18px; height: 18px; border-radius: 50%; background: var(--line); font-size: 11px; margin-right: 6px; }
.tabs button.done .n { background: var(--green); color: #111; }
.row { display: flex; gap: 10px; align-items: end; flex-wrap: wrap; }
.row > * { flex: 1; min-width: 120px; }
.row > .fit { flex: 0 0 auto; min-width: 0; }
.bar { display: flex; gap: 8px; align-items: center; margin: 14px 0; flex-wrap: wrap; }
.hint { color: var(--muted); font-size: 12px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--r); padding: 14px; margin-bottom: 12px; }
.card.dirty { border-color: var(--yellow); }
.scene-head { display: grid; grid-template-columns: 34px 160px 1fr auto; gap: 8px; align-items: center; }
.scene-head .num { color: var(--muted); font-weight: 700; }
.line { display: grid; grid-template-columns: 22px 1fr 1fr 70px auto; gap: 8px; align-items: start; padding: 8px 0; border-top: 1px dashed var(--line); }
.line:first-of-type { border-top: none; }
.line .idx { color: var(--muted); font-size: 12px; padding-top: 9px; }
.line textarea { min-height: 38px; }
.line .say { border-style: dashed; }
.line .tools { display: flex; gap: 3px; padding-top: 4px; }
.line.cols label { margin-top: 0; }
.syl { color: var(--muted); font-size: 11px; }
.vline { display: grid; grid-template-columns: 40px 64px 1fr 40px; gap: 8px; padding: 6px 0; border-top: 1px dashed var(--line); align-items: center; }
.vline.playing { color: var(--yellow); }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 12px; }
.grid img, .grid video { width: 100%; border-radius: 8px; border: 1px solid var(--line); background: #000; }
.chat { display: flex; flex-direction: column; gap: 8px; max-height: 340px; overflow: auto; margin-bottom: 10px; }
.msg { padding: 8px 11px; border-radius: 10px; max-width: 85%; white-space: pre-wrap; }
.msg.me { align-self: flex-end; background: #2b3a5c; }
.msg.ai { align-self: flex-start; background: var(--panel2); }
.msg.err { border: 1px solid var(--coral); }
.outs a { display: block; padding: 4px 0; }
pre.src { white-space: pre-wrap; background: var(--bg); padding: 10px; border-radius: 8px; max-height: 260px; overflow: auto; color: var(--muted); }

#log { position: fixed; left: 272px; z-index: 6; right: 0; bottom: 0; background: #0a0e18; border-top: 1px solid var(--line); display: flex; flex-direction: column; height: 260px; transition: height 0.2s; }
#log.closed { height: 38px; }
#logHead { display: flex; align-items: center; gap: 10px; padding: 6px 14px; cursor: pointer; }
#logHead .grow { flex: 1; }
#logState.run { color: var(--yellow); }
#logState.ok { color: var(--green); }
#logState.bad { color: var(--coral); }
#logBody { flex: 1; margin: 0; padding: 4px 14px 14px; overflow: auto; font: 12px/1.5 ui-monospace, Menlo, monospace; color: #c9d1e6; white-space: pre-wrap; }

dialog { background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 14px; width: min(560px, 92vw); padding: 20px; }
dialog::backdrop { background: rgba(0, 0, 0, 0.6); }
#toast { position: fixed; top: 16px; right: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 9; }
#toast div { background: var(--panel2); border: 1px solid var(--line); padding: 10px 14px; border-radius: 10px; max-width: 380px; }
#toast div.bad { border-color: var(--coral); }
.spin { display: inline-block; width: 10px; height: 10px; border: 2px solid var(--yellow); border-right-color: transparent; border-radius: 50%; animation: s 0.8s linear infinite; }
@keyframes s { to { transform: rotate(360deg); } }
.save-error { color: var(--coral); }
.save-saved { color: var(--green); }
.nopic { color: var(--coral); font-size: 11px; display: block; }
.bar .nopic { display: inline; font-size: inherit; }
.clip-player { position: fixed; right: 16px; bottom: 16px; width: 380px; max-width: calc(100vw - 32px); background: var(--bg, #111); border: 1px solid var(--line); border-radius: 8px; padding: 6px; z-index: 50; box-shadow: 0 6px 24px rgba(0,0,0,.4); }
.clip-player video { width: 100%; display: block; border-radius: 4px; max-height: 60vh; }
.clip-player .bar { display: flex; justify-content: space-between; align-items: center; gap: 6px; font-size: 12px; margin-top: 4px; }
.line.playing textarea { border-color: var(--yellow); }

.shorts .short { background: var(--panel2); }
.shorts .short.off { opacity: .55; }
.shorts .bad { color: var(--coral); }
.shorts .warn { color: var(--yellow); }
.shorts .bar > label { display: inline-flex; gap: 6px; align-items: center; margin: 0; }

/* ---------- khối có tiêu đề, tùy chọn, bước tiếp */
.card { padding: 16px 18px; margin-bottom: 14px; }
.card > h3:first-child, .sec-head h3 { margin: 0; font-size: 15px; }
.card > h3:first-child { margin-bottom: 10px; }
.sec-head { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; margin-bottom: 6px; }
.sec > .bar:last-child { margin-bottom: 0; }
.sec > .hint:last-child, .sec > p.hint:last-child { margin-bottom: 0; }
.opts { display: flex; gap: 10px 20px; align-items: center; flex-wrap: wrap; margin-top: 8px; }
.opts label { display: inline-flex; align-items: center; gap: 6px; margin: 0; font-size: 13px; }
.opts select { width: auto; }
.bar.try { margin: 0 0 6px; flex-wrap: nowrap; }
.bar.try input { min-width: 0 !important; }
.vlines { margin-top: 10px; max-height: 60vh; overflow: auto; }
.outs a { padding: 8px 10px; border-radius: 8px; text-decoration: none; }
.outs a:hover { background: var(--panel2); }
.next { display: flex; justify-content: space-between; gap: 10px; margin: 24px 0 8px; padding-top: 14px; border-top: 1px solid var(--line); }
.next button:last-child { border-color: var(--cyan); color: var(--cyan); }

/* ---------- thanh trên + ngăn kéo (chỉ hiện trên màn hẹp) */
#topbar, #scrim { display: none; }

@media (max-width: 820px) {
  :root { font-size: 15px; }
  body { display: block; }
  input, select, textarea { font-size: 16px; } /* iOS không tự phóng to khi gõ */
  #topbar { display: flex; align-items: center; gap: 6px; position: fixed; top: 0; left: 0; right: 0; height: 52px; padding: 0 6px; padding-top: env(safe-area-inset-top); box-sizing: content-box; background: var(--panel); border-bottom: 1px solid var(--line); z-index: 20; }
  #topbar button { font-size: 20px; min-width: 44px; min-height: 44px; padding: 0; border: none; }
  #topTitle { flex: 1; min-width: 0; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: center; }
  #topTitle span { color: var(--yellow); }
  #side { position: fixed; top: 0; bottom: 0; left: 0; width: min(86vw, 330px); z-index: 30; transform: translateX(-102%); transition: transform 0.22s ease; padding-top: calc(14px + env(safe-area-inset-top)); box-shadow: 0 0 40px rgba(0, 0, 0, 0.5); }
  body.drawer #side { transform: none; }
  #scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.55); z-index: 25; }
  body.drawer #scrim { display: block; }
  #main { position: fixed; inset: 0; top: calc(52px + env(safe-area-inset-top)); padding: 0 14px 110px; -webkit-overflow-scrolling: touch; }
  .head { padding-top: 14px; gap: 8px; }
  .head h2 { font-size: 18px; }
  .head .path, .head .grow { display: none; }
  .tabs { margin: 8px -14px 14px; padding: 0 8px; }
  .tabs button { padding: 12px 10px 10px; }
  .empty { margin-top: 20vh; padding: 0 20px; }

  /* nút to, dễ chạm */
  button { min-height: 40px; padding: 8px 14px; }
  button.small { min-height: 34px; min-width: 34px; padding: 4px 10px; font-size: 13px; }
  .proj { min-height: 0; padding: 10px; }
  .row { gap: 0 10px; }
  .row > * { flex-basis: 100%; }
  .row > .fit { flex: 1 1 100%; }
  .bar { gap: 8px; }
  .bar > button.primary { flex: 1 1 auto; }
  .card { padding: 12px; border-radius: 12px; }
  .grid { grid-template-columns: 1fr; }
  .bar.try { flex-wrap: wrap; }
  .bar.try button { flex: 1; }
  .vlines { max-height: none; }
  .next button { flex: 1; }

  /* kịch bản: mỗi câu xếp dọc */
  .scene-head { grid-template-columns: auto 1fr auto; grid-template-areas: "num sid tools" "title title title"; }
  .scene-head .num { grid-area: num; }
  .scene-head .sid { grid-area: sid; }
  .scene-head > input:not(.sid) { grid-area: title; }
  .scene-head .tools { grid-area: tools; display: flex; gap: 3px; }
  .line.cols { display: none; }
  .line { grid-template-columns: auto auto 1fr; grid-template-areas: "idx pause tools" "text text text" "say say say"; gap: 6px; padding: 12px 0; align-items: center; }
  .line .idx { grid-area: idx; padding-top: 0; display: flex; gap: 8px; align-items: center; }
  .line .idx::after { content: "· nghỉ"; color: var(--muted); }
  .line > textarea:not(.say) { grid-area: text; }
  .line .say { grid-area: say; }
  .line .pause { grid-area: pause; width: 92px; padding: 6px 8px; }
  .line .pause::placeholder { color: var(--muted); }
  .line .tools { grid-area: tools; justify-content: flex-end; padding-top: 0; }
  .nopic { display: inline; }

  .vline { grid-template-columns: 40px 44px 1fr 40px; }
  .chat { max-height: 50vh; }
  .msg { max-width: 92%; }

  #log { left: 0; padding-bottom: env(safe-area-inset-bottom); }
  #log:not(.closed) { height: 60vh; }
  #logHead { min-height: 44px; }
  .clip-player { left: 8px; right: 8px; width: auto; bottom: 52px; }
  #toast { left: 12px; right: 12px; top: calc(60px + env(safe-area-inset-top)); }
  #toast div { max-width: none; }
  dialog { width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; border-radius: 0; border: none; padding: 16px 16px calc(16px + env(safe-area-inset-bottom)); }
}
