:root {
  --bg: #faf7f4; --surface: #fff; --ink: #1f1a17; --muted: #6f6660; --line: #e9e2db;
  --brand: #e8503a; --brand-ink: #fff; --brand-soft: #fdece8;
  --ok: #1f8a4c; --ok-soft: #e3f4ea; --warn: #a86400; --warn-soft: #fff1d6; --err: #c0262d; --err-soft: #fde7e8;
  --tiktok: #111; --facebook: #1877f2; --instagram: #c13584;
  --radius: 14px; --shadow: 0 1px 2px rgba(31,26,23,.06), 0 6px 20px rgba(31,26,23,.06);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #171412; --surface: #221e1b; --ink: #f3ede8; --muted: #a79d95; --line: #3a332e;
    --brand-soft: #3a1d17; --ok-soft: #17301f; --warn-soft: #36290f; --err-soft: #3a1b1d; --tiktok: #f3ede8;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 6px 20px rgba(0,0,0,.3);
  }
}
* { box-sizing: border-box; }
html { color-scheme: light dark; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 16px/1.55 "Sarabun", "Noto Sans Thai", system-ui, -apple-system, "Segoe UI", sans-serif; -webkit-font-smoothing: antialiased; }
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3 { line-height: 1.25; margin: 0; }
a { color: var(--brand); }

/* ---- layout ---- */
.topbar { position: sticky; top: 0; z-index: 20; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line); }
.topbar-in { max-width: 1180px; margin: 0 auto; padding: 10px 16px; display: flex; align-items: center; gap: 16px; }
.logo { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 18px; margin-right: 4px; }
.logo i { width: 28px; height: 28px; border-radius: 8px; background: var(--brand); display: grid; place-items: center; }
.logo i::after { content: ""; border-left: 10px solid #fff; border-top: 6px solid transparent; border-bottom: 6px solid transparent; margin-left: 3px; }
.nav { display: flex; gap: 4px; overflow-x: auto; flex: 1; scrollbar-width: none; }
.nav button { background: none; border: 0; padding: 8px 14px; border-radius: 999px; cursor: pointer; color: var(--muted); white-space: nowrap; font-weight: 500; }
.nav button:hover { background: var(--brand-soft); }
.nav button[aria-current="page"] { background: var(--ink); color: var(--bg); }
.who { display: flex; align-items: center; gap: 10px; font-size: 14px; color: var(--muted); white-space: nowrap; }
main { max-width: 1180px; margin: 0 auto; padding: 24px 16px 120px; }
.page-head { display: flex; flex-wrap: wrap; align-items: end; justify-content: space-between; gap: 12px; margin-bottom: 18px; }
.page-head p { margin: 4px 0 0; color: var(--muted); }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

/* ---- controls ---- */
.btn { border: 1px solid var(--line); background: var(--surface); padding: 9px 16px; border-radius: 10px; cursor: pointer; font-weight: 600; min-height: 40px; }
.btn:hover { border-color: var(--ink); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.primary:hover { filter: brightness(.94); }
.btn.danger { color: var(--err); }
.btn.sm { padding: 5px 11px; min-height: 32px; font-size: 14px; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
.btn:focus-visible, .nav button:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible { outline: 3px solid color-mix(in srgb, var(--brand) 45%, transparent); outline-offset: 2px; }
input[type=text], input[type=email], input[type=password], input[type=search], input[type=url], input[type=number], input[type=datetime-local], input[type=file], textarea, select {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface);
}
textarea { min-height: 84px; resize: vertical; }
label.f { display: block; margin-bottom: 14px; }
label.f > span { display: block; font-weight: 600; font-size: 14px; margin-bottom: 5px; }
.hint { color: var(--muted); font-size: 13px; margin-top: 4px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.badge { display: inline-flex; align-items: center; gap: 5px; padding: 2px 10px; border-radius: 999px; font-size: 13px; font-weight: 600; background: var(--line); }
.badge.tiktok { background: var(--tiktok); color: var(--bg); }
.badge.facebook { background: var(--facebook); color: #fff; }
.badge.instagram { background: var(--instagram); color: #fff; }
.badge.scheduled { background: var(--warn-soft); color: var(--warn); }
.badge.posting { background: var(--brand-soft); color: var(--brand); }
.badge.posted { background: var(--ok-soft); color: var(--ok); }
.badge.failed { background: var(--err-soft); color: var(--err); }
.badge.canceled { background: var(--line); color: var(--muted); }

/* ---- clip grid ---- */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 16px; }
.clip { overflow: hidden; display: flex; flex-direction: column; position: relative; }
.clip.sel { outline: 3px solid var(--brand); outline-offset: -1px; }
.thumb { position: relative; aspect-ratio: 9 / 16; background: #2a2320 center / cover no-repeat; border: 0; padding: 0; cursor: pointer; width: 100%; display: block; }
.thumb .play { position: absolute; inset: 0; display: grid; place-items: center; background: linear-gradient(transparent 55%, rgba(0,0,0,.45)); opacity: 0; transition: opacity .15s; }
.thumb:hover .play, .thumb:focus-visible .play { opacity: 1; }
.thumb .play::after { content: ""; width: 54px; height: 54px; border-radius: 50%; background: rgba(255,255,255,.92) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M9 6l10 6-10 6z' fill='%231f1a17'/%3E%3C/svg%3E") center / 28px no-repeat; }
.thumb .dur { position: absolute; right: 8px; bottom: 8px; background: rgba(0,0,0,.65); color: #fff; font-size: 12px; padding: 1px 7px; border-radius: 6px; }
.pick { position: absolute; top: 8px; left: 8px; width: 28px; height: 28px; border-radius: 8px; background: rgba(255,255,255,.92); display: grid; place-items: center; box-shadow: 0 1px 4px rgba(0,0,0,.3); cursor: pointer; }
.pick input { width: 18px; height: 18px; margin: 0; accent-color: var(--brand); cursor: pointer; }
.clip-body { padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 4px; flex: 1; }
.clip-body h3 { font-size: 15px; }
.clip-body .prod { font-size: 13px; color: var(--muted); }
.clip-actions { display: flex; gap: 6px; margin-top: auto; padding-top: 8px; flex-wrap: wrap; }
.empty { text-align: center; padding: 56px 16px; color: var(--muted); }
.empty h3 { color: var(--ink); margin-bottom: 6px; }

.selbar { position: fixed; left: 50%; bottom: 18px; transform: translateX(-50%); z-index: 30; background: var(--ink); color: var(--bg); padding: 10px 12px 10px 20px; border-radius: 999px; display: flex; align-items: center; gap: 14px; box-shadow: 0 8px 30px rgba(0,0,0,.3); max-width: calc(100vw - 24px); }
.selbar .btn { border-color: transparent; }

/* ---- queue ---- */
.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 14px; }
.tabs button { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 6px 14px; cursor: pointer; }
.tabs button[aria-pressed="true"] { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.post { display: grid; grid-template-columns: 56px 1fr auto; gap: 14px; padding: 12px; align-items: center; }
.post + .post { margin-top: 10px; }
.post .pt { width: 56px; aspect-ratio: 9/16; border-radius: 8px; background: #2a2320 center / cover; }
.post h3 { font-size: 15px; }
.post .meta { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-top: 4px; font-size: 14px; color: var(--muted); }
.post .err { color: var(--err); font-size: 13px; margin-top: 4px; }
.post .acts { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
@media (max-width: 640px) { .post { grid-template-columns: 48px 1fr; } .post .acts { grid-column: 1 / -1; justify-content: flex-start; } .post .pt { width: 48px; } }

/* ---- accounts ---- */
.cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 16px; }
.plat { padding: 16px; }
.plat h3 { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; }
.acc { display: flex; justify-content: space-between; gap: 8px; align-items: center; padding: 8px 0; border-top: 1px solid var(--line); }
.notice { background: var(--warn-soft); color: var(--warn); border-radius: 10px; padding: 10px 14px; margin-bottom: 16px; font-size: 14px; }

/* ---- table ---- */
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 14px; border-bottom: 1px solid var(--line); }
th { font-size: 13px; color: var(--muted); font-weight: 600; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 18px; }
.stat { padding: 14px 16px; }
.stat b { display: block; font-size: 28px; }
.stat span { color: var(--muted); font-size: 14px; }

/* ---- modal ---- */
.overlay { position: fixed; inset: 0; background: rgba(20,14,10,.55); z-index: 50; display: grid; place-items: center; padding: 16px; overflow-y: auto; }
.modal { width: min(560px, 100%); max-height: calc(100vh - 32px); overflow-y: auto; padding: 22px; }
.modal.wide { width: min(420px, 100%); }
.modal h2 { margin-bottom: 14px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.modal video { width: 100%; max-height: 70vh; background: #000; border-radius: 10px; display: block; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 14px; }
.chip { background: var(--brand-soft); color: var(--brand); border-radius: 8px; padding: 2px 10px; font-size: 14px; }
.check { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; margin-bottom: 6px; cursor: pointer; }
.check input { width: 18px; height: 18px; accent-color: var(--brand); }
.check:has(input:checked) { border-color: var(--brand); background: var(--brand-soft); }

/* ---- auth ---- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth .card { width: min(400px, 100%); padding: 28px; }
.auth .logo { margin-bottom: 4px; font-size: 22px; }
.auth p.sub { color: var(--muted); margin: 0 0 18px; }
.auth .switch { text-align: center; margin-top: 14px; font-size: 14px; }
.link { background: none; border: 0; color: var(--brand); cursor: pointer; padding: 0; text-decoration: underline; }
.err-msg { color: var(--err); font-size: 14px; margin: 0 0 10px; min-height: 1px; }

#toasts { position: fixed; top: 14px; right: 14px; z-index: 100; display: flex; flex-direction: column; gap: 8px; }
.toast { background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; box-shadow: var(--shadow); max-width: 340px; font-size: 15px; }
.toast.err { background: var(--err); color: #fff; }
a.btn { color: inherit; text-decoration: none; display: inline-flex; align-items: center; }
@media (max-width: 640px) { .topbar-in { flex-wrap: wrap; row-gap: 4px; } .logo { flex: 1; } .nav { order: 3; flex-basis: 100%; } .who span { display: none; } .grid { grid-template-columns: repeat(2, 1fr); gap: 10px; } }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }

/* ---- AI caption ---- */
.ai { margin: -6px 0 16px; padding: 12px; border: 1px dashed var(--line); border-radius: 12px; background: color-mix(in srgb, var(--brand-soft) 45%, transparent); }
.ai-opt { display: block; width: 100%; text-align: left; margin-top: 6px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); cursor: pointer; line-height: 1.5; }
.ai-opt:hover { border-color: var(--brand); }
.ai-opt.on { border-color: var(--brand); background: var(--brand-soft); }
.ai-opt:focus-visible { outline: 3px solid color-mix(in srgb, var(--brand) 45%, transparent); outline-offset: 2px; }
