:root {
  --bg: #f4f3ef;
  --surface: #ffffff;
  --surface-2: #f8f7f4;
  --border: #e3e0d8;
  --text: #1c1b19;
  --muted: #6f6b63;
  --accent: #1f6feb;
  --accent-text: #ffffff;
  --ok: #1a7f37;
  --ok-bg: #e6f4ea;
  --warn: #9a6700;
  --warn-bg: #fff4d6;
  --err: #c62828;
  --err-bg: #fdeaea;
  --run: #1f6feb;
  --run-bg: #e8f0fe;
  --yt: #ff0033;
  --tt: #111111;
  --fb: #0866ff;
  --ig: #d62976;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 4px 16px rgba(0, 0, 0, 0.04);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141413;
    --surface: #1d1d1b;
    --surface-2: #252522;
    --border: #34332f;
    --text: #ecebe6;
    --muted: #a19d93;
    --accent: #4c8dff;
    --ok: #4ac26b;
    --ok-bg: #16301f;
    --warn: #e3b341;
    --warn-bg: #3a2e10;
    --err: #ff6b6b;
    --err-bg: #3a1a1a;
    --run: #6ea2ff;
    --run-bg: #172a4a;
    --tt: #f2f2f2;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 'IBM Plex Sans Thai', system-ui, sans-serif;
}

h2 { font-size: 17px; margin: 0 0 14px; }
button, input, select, textarea { font: inherit; color: inherit; }
.muted { color: var(--muted); font-weight: 400; }
.hidden, [hidden] { display: none !important; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 12px 24px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; }
.logo {
  display: grid; place-items: center; width: 30px; height: 30px; border-radius: 9px;
  background: var(--text); color: var(--bg); font-size: 13px;
}
.tabs { display: flex; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 11px; border: 1px solid var(--border); }
.tab { border: 0; background: none; padding: 6px 14px; border-radius: 8px; cursor: pointer; color: var(--muted); font-weight: 500; }
.tab.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.count { font-size: 12px; color: var(--muted); }

/* ---------- layout ---------- */
.layout {
  display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); gap: 20px;
  max-width: 1200px; margin: 24px auto; padding: 0 24px; align-items: start;
}
.accounts-page { display: grid; gap: 20px; max-width: 820px; margin: 24px auto; padding: 0 24px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); }
.history { position: sticky; top: 78px; max-height: calc(100vh - 100px); overflow: auto; }

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; padding: 0 16px; }
  .accounts-page { padding: 0 16px; }
  .history { position: static; max-height: none; }
  .topbar { padding: 10px 16px; }
}

/* ---------- form ---------- */
.field { margin-top: 16px; position: relative; }
.field > label, .label { display: block; font-weight: 500; margin-bottom: 6px; }
.label-row { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.label-row h2 { margin-bottom: 14px; }
input[type='text'], input:not([type]), input[type='datetime-local'], textarea, select {
  width: 100%; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface-2); outline: none;
}
textarea { resize: vertical; }
input:focus, textarea:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent); }
.counter { position: absolute; right: 8px; bottom: -20px; font-size: 12px; color: var(--muted); }
.counter.over { color: var(--err); }

.drop {
  display: block; border: 1.5px dashed var(--border); border-radius: 12px; cursor: pointer;
  background: var(--surface-2); transition: border-color .15s, background .15s;
}
.drop.drag, .drop:hover { border-color: var(--accent); }
.drop-empty { display: grid; justify-items: center; gap: 4px; padding: 34px 16px; text-align: center; }
.drop-icon { width: 44px; height: 44px; display: grid; place-items: center; border-radius: 50%; background: var(--surface); border: 1px solid var(--border); font-size: 18px; margin-bottom: 6px; }
.drop-preview { display: flex; gap: 14px; padding: 12px; align-items: center; }
.drop-preview video { width: 110px; max-height: 196px; border-radius: 8px; background: #000; object-fit: contain; }
.file-meta { display: grid; gap: 2px; min-width: 0; }
.file-meta span:first-child { font-weight: 600; overflow-wrap: anywhere; }
.file-meta .link { justify-self: start; }

.link { border: 0; background: none; color: var(--accent); cursor: pointer; padding: 0; font-weight: 500; }
.link:hover { text-decoration: underline; }

.overrides { margin-top: 26px; border: 1px solid var(--border); border-radius: 10px; padding: 10px 14px; }
.overrides summary { cursor: pointer; font-weight: 500; }
.override-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 10px; }
.override-grid label { font-size: 13px; color: var(--muted); display: grid; gap: 4px; }
@media (max-width: 560px) { .override-grid { grid-template-columns: 1fr; } }

/* ---------- target chips ---------- */
.targets { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  display: inline-flex; align-items: center; gap: 8px; padding: 6px 12px 6px 6px;
  border: 1px solid var(--border); border-radius: 999px; cursor: pointer; background: var(--surface-2);
  user-select: none; transition: border-color .15s, background .15s;
}
.chip input { display: none; }
.chip.on { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, var(--surface)); }
.chip .check-dot { width: 16px; height: 16px; border-radius: 50%; border: 1.5px solid var(--border); display: grid; place-items: center; font-size: 10px; }
.chip.on .check-dot { background: var(--accent); border-color: var(--accent); color: #fff; }
.avatar { width: 26px; height: 26px; border-radius: 50%; object-fit: cover; background: var(--border); flex: none; }

.badge {
  display: inline-grid; place-items: center; min-width: 26px; height: 22px; padding: 0 6px; border-radius: 6px;
  font-size: 11px; font-weight: 700; color: #fff; letter-spacing: .02em; flex: none;
}
.badge.youtube { background: var(--yt); }
.badge.tiktok { background: var(--tt); color: var(--bg); }
.badge.facebook { background: var(--fb); }
.badge.instagram { background: linear-gradient(45deg, #feda75, #fa7e1e, #d62976, #962fbf, #4f5bd5); }

.platform-options { display: grid; gap: 8px; margin-top: 14px; }
.opt { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px; padding: 10px 12px; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--border); }
.opt > label:not(.check) { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.opt select { width: auto; padding: 6px 10px; }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 14px; cursor: pointer; }
.hint { flex-basis: 100%; margin: 0; font-size: 13px; color: var(--muted); }
.hint:empty { display: none; }

.seg { display: inline-flex; gap: 4px; padding: 4px; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--border); }
.seg label { padding: 4px 12px; border-radius: 7px; cursor: pointer; font-size: 14px; }
.seg input { display: none; }
.seg label:has(input:checked) { background: var(--surface); box-shadow: var(--shadow); font-weight: 600; }
.schedule input[type='datetime-local'] { margin-top: 8px; max-width: 280px; }

.submit-row { display: grid; gap: 10px; margin-top: 22px; }
.primary {
  border: 0; border-radius: 11px; padding: 12px 18px; font-weight: 600; cursor: pointer;
  background: var(--accent); color: var(--accent-text);
}
.primary:disabled { opacity: .5; cursor: not-allowed; }
.secondary {
  border: 1px solid var(--border); background: var(--surface); border-radius: 9px; padding: 6px 12px; cursor: pointer; font-weight: 500; font-size: 14px;
}
.secondary:hover { border-color: var(--muted); }
.danger { color: var(--err); }
.progress { height: 6px; border-radius: 99px; background: var(--surface-2); overflow: hidden; border: 1px solid var(--border); }
.progress div { height: 100%; width: 0; background: var(--accent); transition: width .2s; }

.empty { color: var(--muted); text-align: center; padding: 24px 0; margin: 0; }
.size-warn { margin: 0; padding: 10px 12px; border-radius: 10px; background: var(--warn-bg); color: var(--warn); font-size: 14px; }

/* ---------- history ---------- */
.posts { display: grid; gap: 12px; }
.post { border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; background: var(--surface-2); }
.post-head { display: flex; justify-content: space-between; gap: 10px; align-items: start; }
.post-title { font-weight: 600; overflow-wrap: anywhere; }
.post-sub { font-size: 13px; color: var(--muted); }
.post-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
.target-list { display: grid; gap: 6px; margin-top: 10px; }
.target-row { display: grid; grid-template-columns: auto 1fr auto; gap: 8px; align-items: center; font-size: 14px; }
.target-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.target-detail { grid-column: 2 / -1; font-size: 13px; color: var(--muted); overflow-wrap: anywhere; margin-top: -2px; }
.target-detail.err { color: var(--err); }
.pill { font-size: 12px; padding: 2px 9px; border-radius: 99px; font-weight: 600; white-space: nowrap; }
.pill.pending { background: var(--surface); color: var(--muted); border: 1px solid var(--border); }
.pill.scheduled { background: var(--warn-bg); color: var(--warn); }
.pill.running { background: var(--run-bg); color: var(--run); }
.pill.done { background: var(--ok-bg); color: var(--ok); }
.pill.failed { background: var(--err-bg); color: var(--err); }
.pill.canceled { background: var(--surface); color: var(--muted); text-decoration: line-through; }
.pill.running::before { content: ''; display: inline-block; width: 7px; height: 7px; margin-right: 5px; border-radius: 50%; background: currentColor; animation: pulse 1s infinite; vertical-align: 1px; }
@keyframes pulse { 50% { opacity: .25; } }
@media (prefers-reduced-motion: reduce) { .pill.running::before { animation: none; } }

/* ---------- accounts ---------- */
.providers { display: grid; gap: 10px; }
.provider { display: grid; grid-template-columns: auto 1fr auto; gap: 12px; align-items: center; padding: 12px 14px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface-2); }
.provider-badges { display: flex; gap: 4px; }
.provider-name { font-weight: 600; }
.provider-sub { font-size: 13px; color: var(--muted); overflow-wrap: anywhere; }
.provider-sub code { font-size: 12px; background: var(--surface); padding: 1px 6px; border-radius: 5px; border: 1px solid var(--border); }
.provider a.secondary { text-decoration: none; }
.provider .disabled { opacity: .55; pointer-events: none; }
@media (max-width: 560px) { .provider { grid-template-columns: auto 1fr; } .provider > :last-child { grid-column: 1 / -1; justify-self: start; } }

.account-list { display: grid; gap: 8px; }
.account { display: grid; grid-template-columns: auto auto 1fr auto auto; gap: 10px; align-items: center; padding: 10px 12px; border: 1px solid var(--border); border-radius: 12px; }
.account-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.account-note { font-size: 12px; color: var(--muted); }
@media (max-width: 560px) { .account { grid-template-columns: auto auto 1fr; } .account > :nth-child(n+4) { grid-column: span 1; } }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; top: 70px; transform: translateX(-50%); z-index: 20;
  max-width: min(560px, calc(100vw - 32px)); padding: 12px 16px; border-radius: 12px;
  background: var(--text); color: var(--bg); box-shadow: 0 8px 30px rgba(0,0,0,.2); font-weight: 500;
}
.toast.error { background: var(--err); color: #fff; }
