/* Kho kiến thức — mobile-first, iPhone ưu tiên. Desktop nới lưới 2–3 cột. */
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Arial, sans-serif;
  background: #f1f5f9; color: #1f2937; font-size: 15px; line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}
a { color: #6d28d9; text-decoration: none; }
button { font-family: inherit; cursor: pointer; }

/* ---- Thanh trên (dính) ---- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 10px;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 14px 10px;
  background: #6d28d9; color: #fff;
}
.brand { font-weight: 800; font-size: 16px; flex: 1; min-width: 0; }
.nav { display: flex; align-items: center; gap: 4px; }
.nav a { color: #ede9fe; padding: 6px 10px; border-radius: 8px; font-weight: 700; font-size: 13px; }
.nav a.on { background: rgba(255, 255, 255, .2); color: #fff; }
.nav a.logout { font-size: 16px; }

.wrap { max-width: 1100px; margin: 0 auto; padding: 12px 14px calc(env(safe-area-inset-bottom, 0px) + 24px); }

/* ---- Thanh tìm + lọc ---- */
.toolbar { position: sticky; top: 52px; z-index: 10; background: #f1f5f9; padding: 8px 0; margin: -4px 0 4px; }
.search {
  width: 100%; padding: 11px 14px; border: 1px solid #cbd5e1; border-radius: 10px;
  font-size: 15px; background: #fff; outline: none;
}
.search:focus { border-color: #a78bfa; box-shadow: 0 0 0 3px #ede9fe; }
.selrow { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 8px; }
.sel {
  width: 100%; padding: 9px 10px; border: 1px solid #cbd5e1; border-radius: 10px;
  font-size: 13px; background: #fff; color: #334155; appearance: none;
}

.listhead { display: flex; align-items: center; gap: 10px; margin: 6px 2px 10px; }
.count { font-size: 13px; color: #64748b; font-weight: 700; }
.clearflt { font-size: 12px; color: #dc2626; font-weight: 700; }

/* ---- Lưới thẻ ---- */
.cards { display: grid; grid-template-columns: 1fr; gap: 10px; }
.card {
  display: block; background: #fff; border: 1px solid #e2e8f0; border-radius: 12px;
  padding: 12px 14px; color: inherit; transition: box-shadow .15s, transform .05s;
}
.card:active { transform: scale(.99); }
.card:hover { box-shadow: 0 4px 14px rgba(15, 23, 42, .08); }
.card-title { font-weight: 700; font-size: 15px; line-height: 1.35; color: #111827; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 8px; }
.chip {
  font-size: 10.5px; font-weight: 700; padding: 2px 7px; border-radius: 999px;
  background: #eef2ff; color: #4338ca; white-space: nowrap;
}
.chip.lv { background: #dcfce7; color: #166534; }
.chip.story { background: #fef3c7; color: #92400e; }
.src { margin-top: 8px; font-size: 12px; color: #64748b; }

/* ---- Chi tiết thẻ ---- */
.back { display: inline-block; margin: 2px 0 8px; font-weight: 700; font-size: 13px; }
.card-h1 { font-size: 19px; line-height: 1.3; margin: 4px 0 10px; color: #0f172a; }
.src-line { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 14px; }
.src-item { font-size: 13px; color: #475569; }

.sec { background: #fff; border: 1px solid #e2e8f0; border-radius: 12px; padding: 12px 14px; margin-bottom: 10px; }
.story-box { background: #fffbeb; border-color: #fde68a; }
.sec-h { display: flex; align-items: center; gap: 8px; font-weight: 800; font-size: 14px; color: #7c3aed; margin-bottom: 8px; }
.story-box .sec-h { color: #92400e; }
.story-text { margin: 0; font-size: 15px; }

.point { padding: 8px 0; border-top: 1px dashed #e2e8f0; }
.point:first-of-type { border-top: 0; padding-top: 0; }
.p-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.p-head strong { font-size: 14.5px; color: #111827; }
.p-body { margin-top: 4px; font-size: 14px; color: #334155; }

.copy {
  flex: 0 0 auto; border: 1px solid #ddd6fe; background: #f5f3ff; color: #6d28d9;
  border-radius: 8px; padding: 3px 8px; font-size: 12px; font-weight: 700;
}
.copy:active { background: #ede9fe; }
.sec-h .copy { margin-left: auto; }

/* ---- Bộ tiêu chí ---- */
.crits { display: grid; grid-template-columns: 1fr; gap: 10px; }
.crit { background: #fff; border: 1px solid #e2e8f0; border-radius: 12px; padding: 12px 14px; }
.crit-h { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.crit-h .w { font-size: 11px; font-weight: 700; color: #b45309; background: #fef3c7; padding: 1px 7px; border-radius: 999px; }
.crit-h strong { font-size: 14.5px; color: #111827; }
.crit-desc { margin-top: 6px; font-size: 13.5px; color: #475569; }
.crit-lbl { font-size: 12px; font-weight: 800; margin-top: 8px; }
.crit-lbl.ok { color: #166534; }
.crit-lbl.no { color: #b91c1c; }
.crit-ok { margin: 2px 0 0 4px; font-size: 13px; color: #166534; }
.crit-no { margin: 2px 0 0 4px; font-size: 13px; color: #b91c1c; }
.crit-vd { margin-top: 8px; font-size: 13px; color: #334155; background: #eff6ff; border: 1px solid #dbeafe; border-radius: 8px; padding: 7px 10px; }
.ct-img-btn { margin-top: 10px; width: 100%; padding: 10px; border: 0; border-radius: 10px; background: #6d28d9; color: #fff; font-weight: 800; font-size: 13.5px; cursor: pointer; }
.ct-img-btn:hover:not(:disabled) { background: #5b21b6; }
.ct-img-btn:disabled { opacity: .6; cursor: not-allowed; }

/* ---- Trạng thái rỗng ---- */
.empty { text-align: center; padding: 40px 16px; color: #475569; font-weight: 600; }
.muted { color: #94a3b8; font-weight: 500; }

/* ---- Đăng nhập ---- */
.login-page { display: flex; align-items: center; justify-content: center; min-height: 100vh; background: #0f172a; }
.login-card { width: min(340px, 88vw); background: #fff; border-radius: 16px; padding: 26px 22px; box-shadow: 0 20px 60px rgba(0, 0, 0, .4); }
.login-logo { text-align: center; font-size: 20px; font-weight: 800; color: #6d28d9; margin-bottom: 18px; }
.login-card input[type=password] { width: 100%; padding: 12px 14px; border: 1px solid #cbd5e1; border-radius: 10px; font-size: 16px; margin-bottom: 12px; }
.login-card button { width: 100%; padding: 12px; border: 0; border-radius: 10px; background: #6d28d9; color: #fff; font-weight: 800; font-size: 15px; }
.login-err { background: #fee2e2; color: #b91c1c; border-radius: 8px; padding: 8px 12px; font-size: 13px; font-weight: 700; margin-bottom: 12px; text-align: center; }
.login-ok { background: #dcfce7; color: #166534; border-radius: 8px; padding: 8px 12px; font-size: 13px; font-weight: 700; margin-bottom: 12px; text-align: center; }
.login-link { display: block; text-align: center; margin-top: 12px; font-size: 13px; font-weight: 700; color: #6d28d9; }

/* ---- Toast copy ---- */
.toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom, 0px) + 20px); transform: translateX(-50%) translateY(20px);
  background: #111827; color: #fff; padding: 9px 16px; border-radius: 999px; font-size: 13px; font-weight: 700;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 50;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---- Chi tiết thẻ: 2 cột (trái kiến thức · phải kịch bản) ---- */
.card-2col { display: grid; grid-template-columns: 1fr; gap: 14px; align-items: start; }
.card-left { min-width: 0; }
.card-right { min-width: 0; }

.script-panel { background: #fff; border: 1px solid #e2e8f0; border-radius: 12px; padding: 14px; }
.gen-label { display: block; font-size: 12px; font-weight: 700; color: #475569; margin: 10px 0 4px; }
.gen-label:first-of-type { margin-top: 4px; }
.gen-sel { width: 100%; padding: 10px 11px; border: 1px solid #cbd5e1; border-radius: 10px; font-size: 14px; background: #fff; color: #334155; appearance: none; }
.gen-hint { font-size: 12px; color: #7c3aed; margin: 6px 2px 0; min-height: 16px; }
.gen-btn { width: 100%; margin-top: 12px; padding: 12px; border: 0; border-radius: 10px; background: #6d28d9; color: #fff; font-weight: 800; font-size: 15px; cursor: pointer; }
.gen-btn:hover:not(:disabled) { background: #5b21b6; }
.gen-btn:disabled { opacity: .5; cursor: not-allowed; }
.gen-status { font-size: 13px; font-weight: 600; margin-top: 8px; min-height: 18px; color: #475569; }
.gen-status.ok { color: #166534; }
.gen-status.err { color: #b91c1c; }
.gen-status.info { color: #1e40af; }

.gen-out-wrap { margin-top: 12px; border-top: 1px solid #e2e8f0; padding-top: 12px; }
.gen-out-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; font-weight: 800; font-size: 13px; color: #6d28d9; }
.gen-out { display: flex; flex-direction: column; gap: 10px; }
.gen-title { font-weight: 800; font-size: 16px; color: #0f172a; padding: 2px 2px 4px; }

/* 1 dòng kịch bản (xem) + nút sửa */
.script-line { display: flex; align-items: flex-start; gap: 8px; padding: 6px 0; border-top: 1px dashed #eef2f7; }
.script-line:first-of-type { border-top: 0; }
.sl-text { flex: 1; font-size: 14px; line-height: 1.55; color: #334155; white-space: pre-wrap; word-wrap: break-word; }
.sl-edit { flex: 0 0 auto; border: 0; background: transparent; cursor: pointer; font-size: 13px; opacity: .5; padding: 0 2px; border-radius: 6px; }
.sl-edit:hover { opacity: 1; background: #f1f5f9; }
/* 1 dòng ở chế độ sửa */
.script-line.editing { flex-direction: column; }
.sl-input { width: 100%; border: 1px solid #a78bfa; border-radius: 8px; padding: 8px 10px; font-size: 14px; line-height: 1.5; font-family: inherit; resize: vertical; }
.sl-bar { display: flex; gap: 6px; margin-top: 6px; }
.sl-save { border: 0; background: #16a34a; color: #fff; font-weight: 700; font-size: 12px; border-radius: 8px; padding: 5px 12px; cursor: pointer; }
.sl-cancel { border: 1px solid #e2e8f0; background: #fff; color: #64748b; font-weight: 700; font-size: 12px; border-radius: 8px; padding: 5px 10px; cursor: pointer; }

/* ---- Trang Cài đặt AI ---- */
.set-form { max-width: 520px; margin-top: 12px; }
.set-in { width: 100%; padding: 11px 13px; border: 1px solid #cbd5e1; border-radius: 10px; font-size: 14px; margin-bottom: 4px; }
.bg-preview { margin-bottom: 10px; border-radius: 12px; overflow: hidden; border: 1px solid #e2e8f0; max-width: 200px; }
.bg-preview img { display: block; width: 100%; height: auto; }

/* ---- Desktop: nới lưới ngang ---- */
@media (min-width: 620px) { .cards { grid-template-columns: 1fr 1fr; } .crits { grid-template-columns: 1fr 1fr; } }
@media (min-width: 1000px) { .cards { grid-template-columns: 1fr 1fr 1fr; } }
/* Trang chi tiết: mở rộng HẾT màn hình để 2 cột chứa nhiều nội dung hơn */
.wrap-wide { max-width: none; }
@media (min-width: 1200px) { .wrap-wide { padding-left: 28px; padding-right: 28px; } }

/* Chi tiết: 2 cột từ 900px, cột phải (kịch bản) dính theo cuộn */
@media (min-width: 900px) {
  .card-2col { grid-template-columns: 1.35fr 1fr; gap: 22px; }
  .card-right { position: sticky; top: 78px; max-height: calc(100vh - 92px); overflow-y: auto; }
}

/* ================= Kho video + Kho phim ================= */
.da-toolrow { display: flex; gap: 8px; align-items: center; }
.da-toolrow .search { flex: 1; min-width: 0; }
.da-new-btn {
  flex: 0 0 auto; padding: 11px 14px; border-radius: 10px; background: #6d28d9; color: #fff;
  font-weight: 800; font-size: 14px; white-space: nowrap;
}
.da-new-btn:hover { background: #5b21b6; }

/* Khối gập (tạo mới / sửa cảnh / nhân vật) */
.da-new { background: #fff; border: 1px solid #e2e8f0; border-radius: 12px; margin-bottom: 10px; }
.da-new > summary {
  padding: 11px 14px; font-weight: 800; font-size: 13.5px; color: #6d28d9; cursor: pointer; list-style: none;
}
.da-new > summary::-webkit-details-marker { display: none; }
.da-new > summary::before { content: '▸ '; }
.da-new[open] > summary::before { content: '▾ '; }
.da-form { padding: 0 14px 14px; }

.da-f { display: block; margin-bottom: 8px; min-width: 0; }
.da-f > span { display: block; font-size: 12px; font-weight: 700; color: #475569; margin-bottom: 3px; }
.da-ta {
  width: 100%; padding: 10px 12px; border: 1px solid #cbd5e1; border-radius: 10px;
  font-size: 14px; line-height: 1.5; font-family: inherit; resize: vertical; min-height: 72px;
}
.da-ta-big { min-height: 240px; }
.da-ta:focus, .set-in:focus { border-color: #a78bfa; outline: none; box-shadow: 0 0 0 3px #ede9fe; }

/* Lưới ngang — tận dụng bề ngang, không để 1 ô nhỏ chiếm trọn 1 hàng */
.da-grid2, .da-grid4 { display: grid; gap: 8px; }
.da-grid2 { grid-template-columns: 1fr 1fr; }
.da-grid4 { grid-template-columns: 1fr 1fr; }
.da-f-wide { grid-column: 1 / -1; }
@media (min-width: 720px) { .da-grid4 { grid-template-columns: repeat(4, 1fr); } }

.da-btnrow { display: flex; gap: 8px; align-items: stretch; }
.da-btnrow .gen-btn { flex: 1; margin-top: 0; }
.da-del {
  flex: 0 0 auto; border: 1px solid #fecaca; background: #fef2f2; color: #b91c1c;
  border-radius: 10px; padding: 0 14px; font-size: 15px; font-weight: 800; cursor: pointer;
}
.da-del:hover { background: #fee2e2; }

/* Đầu trang chi tiết */
.da-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.da-head .card-h1 { flex: 1; min-width: 0; margin-bottom: 6px; }
.da-tt-form { flex: 0 0 auto; }
.da-tt-form .gen-sel { width: auto; min-width: 140px; padding: 7px 10px; font-size: 13px; }
.da-sum { display: flex; flex-wrap: wrap; gap: 6px; margin: 0 0 12px; }
.da-sum span {
  font-size: 11.5px; font-weight: 700; padding: 3px 9px; border-radius: 999px;
  background: #eef2ff; color: #4338ca;
}
.da-log { margin-top: 5px; font-size: 13px; color: #475569; }

/* Danh sách cảnh */
.sc-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 10px; }
.sc { border: 1px solid #e2e8f0; border-radius: 10px; background: #fff; }
.sc[data-tt="xong"] { border-left: 3px solid #16a34a; }
.sc[data-tt="loi"] { border-left: 3px solid #dc2626; }
.sc[data-tt="dang_tao"] { border-left: 3px solid #2563eb; }
.sc[data-tt="loai"] { border-left: 3px solid #94a3b8; opacity: .65; }
.sc-sum {
  display: flex; align-items: center; gap: 8px; padding: 9px 12px; cursor: pointer;
  list-style: none; font-size: 13px;
}
.sc-sum::-webkit-details-marker { display: none; }
.sc-no {
  flex: 0 0 auto; min-width: 24px; height: 24px; border-radius: 6px; background: #6d28d9; color: #fff;
  font-weight: 800; font-size: 12px; display: flex; align-items: center; justify-content: center;
}
.sc-time { flex: 0 0 auto; font-weight: 800; font-size: 12px; color: #b45309; background: #fef3c7; padding: 2px 7px; border-radius: 999px; }
.sc-kieu { flex: 0 0 auto; font-size: 15px; }
.sc-txt { flex: 1; min-width: 0; color: #334155; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sc-tt { flex: 0 0 auto; font-size: 11.5px; font-weight: 700; }
.sc .da-form { padding: 4px 12px 12px; border-top: 1px dashed #e2e8f0; }
