:root {
  --bg: #f6f4ef;
  --paper: #ffffff;
  --ink: #1f2430;
  --muted: #6b7180;
  --line: #e4e0d6;
  --navy: #1d2b4f;
  --navy-2: #2c3e6b;
  --red: #d64545;
  --red-bg: #fdeeee;
  --red-line: #f1b9b9;
  --green: #238a52;
  --green-bg: #e8f6ee;
  --green-line: #a9dcbd;
  --gold: #d99a0b;
  --gold-bg: #fff6de;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(20, 24, 40, .06), 0 4px 16px rgba(20, 24, 40, .06);
}

* { box-sizing: border-box; }
html { scroll-padding-top: 150px; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.65 "Noto Sans Thai", "Sarabun", "Thonburi", "Leelawadee UI", system-ui, sans-serif;
  -webkit-text-size-adjust: 100%;
}
button, input { font: inherit; color: inherit; }
[hidden] { display: none !important; }
body:not(.is-admin) .admin-only { display: none !important; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 30;
  background: var(--navy); color: #fff;
  box-shadow: 0 2px 12px rgba(0, 0, 0, .15);
}
.topbar-inner {
  max-width: 1200px; margin: 0 auto; padding: 10px 16px 6px;
  display: flex; align-items: center; gap: 18px; flex-wrap: wrap;
}
.brand { min-width: 0; flex: 1 1 260px; }
.brand-title { font-weight: 700; font-size: 18px; line-height: 1.3; }
.brand-sub { font-size: 13px; opacity: .75; }
.progress-wrap { flex: 1 1 220px; max-width: 360px; }
.progress-label { font-size: 13px; opacity: .9; margin-bottom: 3px; }
.progress { height: 8px; border-radius: 99px; background: rgba(255, 255, 255, .18); overflow: hidden; }
.progress-fill { height: 100%; width: 0; background: linear-gradient(90deg, #4cc47f, #7ee0a4); transition: width .4s; }
.toolbar {
  max-width: 1200px; margin: 0 auto; padding: 4px 16px 10px;
  display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap;
}
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  border: 1px solid rgba(255, 255, 255, .25); background: transparent; color: #fff;
  padding: 3px 12px; border-radius: 99px; font-size: 14px; cursor: pointer;
}
.chip b { font-weight: 600; margin-left: 2px; }
.chip.active { background: #fff; color: var(--navy); border-color: #fff; }
.chip.red b { color: #ffb3b3; } .chip.red.active b { color: var(--red); }
.chip.green b { color: #9ff0bf; } .chip.green.active b { color: var(--green); }
.chip.gold b { color: #ffd772; } .chip.gold.active b { color: var(--gold); }
.admin-tools { display: flex; gap: 6px; flex-wrap: wrap; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 0; border-radius: 8px; padding: 7px 14px; cursor: pointer;
  background: #3b82f6; color: #fff; font-weight: 600; font-size: 14px; text-decoration: none;
}
.btn:hover { filter: brightness(1.05); }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.ghost { background: rgba(255, 255, 255, .12); border: 1px solid rgba(255, 255, 255, .3); }
.btn.ghost-dark { background: transparent; color: inherit; border: 1px solid currentColor; opacity: .85; }
.icon-btn {
  width: 36px; height: 36px; border-radius: 50%; border: 1px solid var(--line);
  background: #fff; cursor: pointer; font-size: 20px; line-height: 1; display: inline-grid; place-items: center;
}
.icon-btn:hover { background: #f1efe9; }
.icon-btn.small { width: 28px; height: 28px; font-size: 14px; }

/* ---------- layout ---------- */
.layout { max-width: 1200px; margin: 0 auto; padding: 18px 16px 80px; display: grid; grid-template-columns: 230px 1fr; gap: 28px; }
.toc { position: sticky; top: 140px; align-self: start; max-height: calc(100vh - 160px); overflow: auto; font-size: 14px; }
.toc a {
  display: flex; justify-content: space-between; gap: 8px; padding: 7px 10px; border-radius: 8px;
  color: var(--ink); text-decoration: none; line-height: 1.35;
}
.toc a:hover { background: #ebe7dd; }
.toc .toc-count { font-size: 12px; color: var(--muted); white-space: nowrap; }
.toc .toc-count.done { color: var(--green); font-weight: 600; }
.script { min-width: 0; }
.howto {
  background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 16px; font-size: 14px; color: #3d4250; margin-bottom: 18px;
}
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; vertical-align: middle; margin: 0 2px 0 6px; }
.dot.red { background: var(--red); } .dot.green { background: var(--green); }
.loading, .empty-note { color: var(--muted); padding: 40px 0; text-align: center; }

@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; gap: 0; }
  .toc { display: none; }
  html { scroll-padding-top: 170px; }
}

/* ---------- sections & segments ---------- */
.section { margin-bottom: 34px; }
.section-head {
  display: flex; align-items: baseline; justify-content: space-between; gap: 10px;
  border-bottom: 2px solid var(--navy); padding-bottom: 6px; margin-bottom: 14px;
}
.section-head h2 { margin: 0; font-size: 20px; color: var(--navy); }
.section-head .sec-count { font-size: 13px; color: var(--muted); white-space: nowrap; }

.seg { position: relative; margin-bottom: 10px; }
.seg.has { margin-top: 92px; }


.seg-body {
  width: 100%; text-align: left; cursor: pointer;
  display: grid; grid-template-columns: 58px 1fr auto; gap: 12px; align-items: start;
  padding: 11px 14px; border-radius: var(--radius);
  background: var(--red-bg); border: 1px solid var(--red-line); border-left: 6px solid var(--red);
  transition: transform .12s, box-shadow .12s;
}
.seg-body:hover { transform: translateY(-1px); box-shadow: var(--shadow); }
.seg-body:focus-visible { outline: 3px solid #3b82f6; outline-offset: 2px; }
.seg.has .seg-body { background: var(--green-bg); border-color: var(--green-line); border-left-color: var(--green); }
.seg.kind-heading .txt { font-weight: 700; color: var(--navy); }
.seg.kind-subheading .txt { font-weight: 600; color: var(--navy-2); }

.tc { font-variant-numeric: tabular-nums; font-size: 13px; color: var(--muted); line-height: 1.3; padding-top: 3px; }
.tc small { display: block; font-size: 11px; opacity: .8; }
.txt { font-size: 17px; line-height: 1.6; }
.txt .line { display: block; }
.status {
  font-size: 12px; font-weight: 600; white-space: nowrap; padding: 2px 9px; border-radius: 99px; margin-top: 3px;
  color: var(--red); background: #fff; border: 1px solid var(--red-line);
}
.seg.has .status { color: var(--green); border-color: var(--green-line); }
.status .star { color: var(--gold); }

/* speech bubble with thumbnails pointing at the segment */
.bubble {
  position: absolute; left: 70px; bottom: calc(100% + 12px);
  display: flex; gap: 6px; align-items: center;
  background: #fff; border: 2px solid var(--green); border-radius: 14px;
  padding: 6px; box-shadow: var(--shadow); max-width: calc(100% - 80px); cursor: pointer;
}
.bubble::after, .bubble::before {
  content: ""; position: absolute; left: 22px; border: solid transparent;
}
.bubble::before { top: 100%; border-width: 11px 9px 0; border-top-color: var(--green); }
.bubble::after { top: 100%; left: 24px; border-width: 8px 7px 0; border-top-color: #fff; }
.thumb {
  position: relative; width: 64px; height: 64px; flex: 0 0 auto; border-radius: 9px; overflow: hidden;
  background: #e9e6de; display: grid; place-items: center; font-size: 24px; color: #8b8f99;
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb .play {
  position: absolute; right: 3px; bottom: 3px; background: rgba(0, 0, 0, .6); color: #fff;
  font-size: 10px; line-height: 1; padding: 3px 5px; border-radius: 5px;
}
.thumb.sel { outline: 3px solid var(--gold); outline-offset: -3px; }
.thumb.sel::after {
  content: "★"; position: absolute; left: 3px; top: 2px; color: var(--gold); font-size: 14px;
  text-shadow: 0 0 3px #fff, 0 0 3px #fff;
}
.bubble .more { font-size: 14px; font-weight: 600; color: var(--green); padding: 0 8px; white-space: nowrap; }

@media (max-width: 560px) {
  .seg-body { grid-template-columns: 1fr; gap: 4px; padding: 10px 12px; }
  .tc { display: flex; gap: 8px; padding: 0; }
  .tc small { display: inline; }
  .status { justify-self: start; }
  .txt { font-size: 16px; }
  .bubble { left: 12px; max-width: calc(100% - 12px); }
  .thumb { width: 56px; height: 56px; }
  .seg.has { margin-top: 84px; }
}

/* ---------- dialogs ---------- */
dialog { border: 0; padding: 0; color: var(--ink); }
dialog::backdrop { background: rgba(15, 20, 35, .55); }
.modal {
  width: min(860px, calc(100vw - 32px)); max-height: calc(100vh - 40px);
  border-radius: 16px; background: var(--bg); box-shadow: 0 20px 60px rgba(0, 0, 0, .3);
  overflow: hidden; display: none; flex-direction: column;
}
.modal[open] { display: flex; }
.modal.small { width: min(400px, calc(100vw - 32px)); }
.modal-head {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 12px 16px; background: var(--paper); border-bottom: 1px solid var(--line);
}
.modal-meta { font-size: 14px; color: var(--muted); min-width: 0; }
.modal-meta b { color: var(--ink); }
.pill { display: inline-block; font-size: 12px; font-weight: 600; padding: 1px 8px; border-radius: 99px; margin-left: 6px; }
.pill.red { background: var(--red-bg); color: var(--red); }
.pill.green { background: var(--green-bg); color: var(--green); }
.modal-nav { display: flex; gap: 6px; flex: 0 0 auto; }
.modal-body { padding: 16px; overflow: auto; }
.quote {
  margin: 0 0 16px; padding: 14px 18px; background: var(--paper); border-radius: var(--radius);
  border-left: 6px solid var(--navy); font-size: 19px; line-height: 1.65;
}
.quote .line { display: block; }

.upload-box { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: 18px; }
.note-field { display: block; margin-bottom: 10px; }
.field span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 2px; }
.field input {
  width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: #fbfaf7;
}
.field input:focus { outline: 2px solid #3b82f6; border-color: transparent; background: #fff; }
.field input.need { border-color: var(--red); background: var(--red-bg); }
.dropzone {
  display: flex; align-items: center; justify-content: center; gap: 14px; text-align: left;
  border: 2px dashed #b9c2d8; border-radius: var(--radius); padding: 18px; cursor: pointer;
  background: #f4f7fd; color: #3d4a6b; font-size: 14px; transition: background .15s, border-color .15s;
}
.dropzone:hover, .dropzone.over { background: #e6eefc; border-color: #3b82f6; }
.dropzone input { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
.dz-icon {
  width: 44px; height: 44px; border-radius: 50%; background: #3b82f6; color: #fff;
  display: grid; place-items: center; font-size: 26px; flex: 0 0 auto;
}
.dz-text b { font-size: 16px; }

.media-title { font-size: 15px; margin: 0 0 10px; color: var(--navy); }
.media-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 12px; }
.media-card { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; display: flex; flex-direction: column; }
.media-card.sel { border: 2px solid var(--gold); background: var(--gold-bg); }
.media-card.pending { opacity: .85; }
.media-prev {
  position: relative; aspect-ratio: 4 / 3; background: #e9e6de; display: grid; place-items: center;
  cursor: pointer; border: 0; padding: 0; width: 100%; font-size: 36px; color: #8b8f99; overflow: hidden;
}
.media-prev img { width: 100%; height: 100%; object-fit: cover; display: block; }
.media-prev .play { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); width: 44px; height: 44px; border-radius: 50%; background: rgba(0, 0, 0, .55); color: #fff; font-size: 18px; display: grid; place-items: center; }
.media-prev .badge-sel { position: absolute; left: 6px; top: 6px; background: var(--gold); color: #fff; font-size: 12px; font-weight: 600; padding: 1px 8px; border-radius: 99px; }
.media-info { padding: 8px 10px; font-size: 13px; line-height: 1.45; flex: 1; }
.media-info .who { font-weight: 600; }
.media-info .sub { color: var(--muted); font-size: 12px; word-break: break-all; }
.media-info .note { margin-top: 3px; }
.media-actions { display: flex; gap: 4px; padding: 0 8px 8px; flex-wrap: wrap; }
.act {
  border: 1px solid var(--line); background: #fff; border-radius: 7px; padding: 3px 8px; font-size: 12px; cursor: pointer; text-decoration: none; color: var(--ink);
}
.act:hover { background: #f1efe9; }
.act.star.on { background: var(--gold); border-color: var(--gold); color: #fff; }
.act.del { color: var(--red); }
.bar { height: 6px; background: #e4e0d6; border-radius: 99px; overflow: hidden; margin-top: 6px; }
.bar i { display: block; height: 100%; width: 0; background: #3b82f6; transition: width .2s; }
.err { color: var(--red); }

@media (max-width: 560px) {
  .topbar { position: relative; }
  html { scroll-padding-top: 12px; }
  .brand-sub { display: none; }
  .topbar-inner { gap: 8px 12px; }
  .modal { width: 100%; max-width: 100%; height: 100%; max-height: 100%; inset: 0; border-radius: 0; margin: 0; }
  .field-row { grid-template-columns: 1fr; }
  .media-grid { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .quote { font-size: 17px; }
}

.viewer { background: #0d0f14; color: #fff; width: 100vw; height: 100dvh; max-width: 100vw; max-height: 100dvh; margin: 0; }
.viewer[open] { display: flex; flex-direction: column; }
.viewer-stage { flex: 1; min-height: 0; display: grid; place-items: center; padding: 50px 12px 8px; }
.viewer-stage img, .viewer-stage video { max-width: 100%; max-height: 100%; object-fit: contain; }
.viewer-cap { padding: 8px 16px 16px; text-align: center; font-size: 14px; opacity: .85; }
.viewer-close { position: absolute; top: 10px; right: 12px; background: rgba(255, 255, 255, .15); color: #fff; border-color: transparent; }
.nopreview { text-align: center; opacity: .8; }

.login { padding: 20px; background: var(--paper); }
.login h3 { margin: 0 0 4px; }
.login input { width: 100%; padding: 9px 12px; border: 1px solid var(--line); border-radius: 8px; margin-top: 10px; }
.login-err { color: var(--red); font-size: 13px; min-height: 20px; margin-top: 4px; }
.login-actions { display: flex; justify-content: flex-end; gap: 8px; }
.muted { color: var(--muted); font-size: 14px; margin: 0; }

/* ---------- upload tray ---------- */
.tray {
  position: fixed; right: 16px; bottom: 16px; z-index: 40; width: min(340px, calc(100vw - 32px));
  background: var(--paper); border-radius: 14px; box-shadow: 0 10px 40px rgba(0, 0, 0, .25); overflow: hidden;
}
.tray-head { display: flex; align-items: center; justify-content: space-between; padding: 8px 10px 8px 14px; background: var(--navy); color: #fff; font-size: 14px; font-weight: 600; }
.tray-head .icon-btn { background: transparent; color: #fff; border-color: rgba(255, 255, 255, .3); }
.tray-list { list-style: none; margin: 0; padding: 6px 12px 10px; max-height: 260px; overflow: auto; font-size: 13px; }
.tray.collapsed .tray-list { display: none; }
.tray-list li { padding: 6px 0; border-bottom: 1px solid var(--line); }
.tray-list li:last-child { border-bottom: 0; }
.tray-list .row { display: flex; justify-content: space-between; gap: 8px; }
.tray-list .fname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tray-list .pct { color: var(--muted); white-space: nowrap; }
.tray-list li.done .pct { color: var(--green); }
.tray-list li.failed .pct { color: var(--red); }
.tray-list .retry { border: 0; background: none; color: #3b82f6; cursor: pointer; padding: 0; font-size: 12px; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); z-index: 50;
  background: #1f2430; color: #fff; padding: 9px 18px; border-radius: 99px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
