/* ---------- โทเคนสี: สว่างเป็นค่าเริ่มต้น, มืดตามระบบหรือเลือกเอง ---------- */
:root {
  --paper: #f3f5f8;
  --surface: #ffffff;
  --surface-2: #eef2f7;
  --ink: #14202e;
  --muted: #5d6b7a;
  --line: #dde3ea;
  --brand: #1f4e8c;
  --brand-ink: #ffffff;
  --brand-soft: #e3ecf8;
  --fresh: #c25e00;
  --danger: #b3261e;
  --ok: #1d7a46;
  --shadow: 0 1px 2px rgb(20 32 46 / 6%), 0 4px 14px rgb(20 32 46 / 6%);
  --radius-s: 6px;
  --radius-m: 12px;
  --font: "IBM Plex Sans Thai", "Noto Sans Thai", system-ui, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #0e151d; --surface: #16202b; --surface-2: #1c2834; --ink: #e6ebf0; --muted: #93a0ad;
    --line: #263341; --brand: #8db8f2; --brand-ink: #0e151d; --brand-soft: #1b2d44; --fresh: #f0a43a;
    --danger: #ff8a80; --ok: #6fcf97; --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --paper: #0e151d; --surface: #16202b; --surface-2: #1c2834; --ink: #e6ebf0; --muted: #93a0ad;
  --line: #263341; --brand: #8db8f2; --brand-ink: #0e151d; --brand-soft: #1b2d44; --fresh: #f0a43a;
  --danger: #ff8a80; --ok: #6fcf97; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--paper); color: var(--ink);
  font: 400 16px/1.6 var(--font); font-variant-numeric: tabular-nums;
}
a { color: var(--brand); }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; border-radius: 4px; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
svg { fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- ส่วนหัว ---------- */
.top { position: sticky; top: 0; z-index: 20; background: var(--surface); border-bottom: 1px solid var(--line); }
.top-inner { max-width: 1320px; margin: 0 auto; padding: 10px 20px; display: flex; align-items: center; gap: 16px; }
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--ink); font-weight: 600; font-size: 18px; white-space: nowrap; }
.brand-mark { width: 30px; height: 30px; flex: none; }
.brand-mark rect { fill: var(--brand); stroke: none; }
.brand-mark circle { fill: var(--brand-ink); stroke: none; }
.brand-mark path { stroke: var(--brand-ink); stroke-width: 3; }
.search { flex: 1; max-width: 560px; position: relative; }
.search-icon { position: absolute; left: 12px; top: 50%; width: 18px; height: 18px; transform: translateY(-50%); color: var(--muted); pointer-events: none; }
.search input { width: 100%; padding: 8px 12px 8px 38px; border: 1px solid var(--line); border-radius: 999px; background: var(--surface-2); }
.search input:focus { background: var(--surface); }
.search-hint { position: absolute; left: 14px; top: 100%; margin-top: 2px; font-size: 13px; color: var(--danger); background: var(--surface); padding: 0 6px; border-radius: 4px; }
.search-hint:empty { display: none; }
.top-actions { margin-left: auto; display: flex; gap: 8px; align-items: center; }
.seg { display: flex; border: 1px solid var(--line); border-radius: 999px; padding: 2px; background: var(--surface-2); }
.seg button { border: 0; background: none; border-radius: 999px; padding: 5px 10px; cursor: pointer; color: var(--muted); display: grid; place-items: center; }
.seg button svg { width: 18px; height: 18px; }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.icon-btn { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; width: 36px; height: 36px; display: grid; place-items: center; cursor: pointer; color: var(--muted); padding: 0; }
.icon-btn svg { width: 18px; height: 18px; }
.icon-btn:hover, .seg button:hover { color: var(--ink); }
.pill-btn { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 6px 14px; cursor: pointer; white-space: nowrap; }

/* แถบหมวดแบบเลื่อน (มือถือ/แท็บเล็ต) */
.chips { display: none; gap: 6px; overflow-x: auto; padding: 0 16px 10px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 4px 12px; cursor: pointer; color: var(--ink); font-size: 14px; white-space: nowrap; }
.chip small { color: var(--muted); margin-left: 4px; }
.chip[aria-pressed="true"] { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.chip[aria-pressed="true"] small { color: inherit; opacity: 0.8; }

/* ---------- โครงหน้า: แถบข้าง + เนื้อหา ---------- */
.layout { max-width: 1320px; margin: 0 auto; padding: 0 20px; display: grid; grid-template-columns: 260px minmax(0, 1fr); gap: 28px; }
.sidebar { position: sticky; top: var(--head-h, 58px); align-self: start; max-height: calc(100vh - var(--head-h, 58px)); overflow-y: auto; padding: 20px 4px 24px 0; scrollbar-width: thin; }
.side-block + .side-block { margin-top: 20px; }
.side-title { font-size: 13px; font-weight: 600; color: var(--muted); margin: 0 0 6px 10px; }
.side-list { list-style: none; margin: 0; padding: 0; }
.side-item {
  width: 100%; display: flex; align-items: center; gap: 10px; text-align: left;
  border: 0; background: none; border-radius: 8px; padding: 6px 10px; cursor: pointer; color: var(--ink); font-size: 15px;
}
.side-item:hover { background: var(--surface-2); }
.side-item[aria-current="true"] { background: var(--brand-soft); color: var(--brand); font-weight: 600; }
.side-item .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-item .count { font-size: 13px; color: var(--muted); font-weight: 400; }
.side-filter { width: calc(100% - 10px); margin: 0 0 6px 10px; padding: 5px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); font-size: 14px; }
.side-empty { font-size: 14px; color: var(--muted); padding: 4px 10px; }

/* ไอคอนแหล่งข่าว (favicon หรืออักษรย่อ) */
.src-icon { width: 18px; height: 18px; border-radius: 4px; flex: none; display: grid; place-items: center; overflow: hidden; font-size: 11px; font-weight: 600; color: #fff; }
.src-icon img { width: 100%; height: 100%; object-fit: contain; background: #fff; }

.main { min-width: 0; padding: 20px 0 64px; }
.bar { display: flex; align-items: flex-end; gap: 12px; }
.bar-text { flex: 1; min-width: 0; }
.bar h1 { margin: 0; font-size: 24px; font-weight: 600; line-height: 1.3; display: flex; align-items: center; gap: 10px; }
.bar h1 .src-icon { width: 24px; height: 24px; }
.subline { margin: 2px 0 0; color: var(--muted); font-size: 14px; }
.active-filters { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.active-filters:empty { display: none; }
.filter-pill { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 3px 6px 3px 12px; font-size: 14px; }
.filter-pill button { border: 0; background: var(--surface-2); border-radius: 50%; width: 22px; height: 22px; cursor: pointer; color: var(--muted); line-height: 1; }
.mobile-only { display: none; }

/* ---------- ข่าว ---------- */
.day { margin-top: 24px; }
.day-head { display: flex; align-items: baseline; gap: 10px; margin: 0 0 12px; font-size: 16px; font-weight: 600; }
.day-head small { color: var(--muted); font-weight: 400; font-size: 14px; }

.card { position: relative; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-m); overflow: hidden; box-shadow: var(--shadow); display: flex; flex-direction: column; }
.card:hover { border-color: color-mix(in srgb, var(--brand) 45%, var(--line)); }
.card:hover .card-title a { color: var(--brand); }
.card-media { aspect-ratio: 16 / 9; background: var(--surface-2); overflow: hidden; display: grid; place-items: center; }
.card-media img { width: 100%; height: 100%; object-fit: cover; display: block; }
.card-media .ph { font-size: 34px; font-weight: 600; color: #fff; }
.card-body { padding: 12px 14px 14px; display: flex; flex-direction: column; gap: 6px; flex: 1; }
.card-src { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); min-width: 0; }
.card-src .src-btn {
  position: relative; z-index: 2; display: inline-flex; align-items: center; gap: 6px; min-width: 0;
  border: 0; background: none; padding: 0; cursor: pointer; color: var(--ink); font-size: 13px; font-weight: 500;
}
.card-src .src-btn span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-src .src-btn:hover span { text-decoration: underline; }
.card-src time { margin-left: auto; white-space: nowrap; }
.card-src time.fresh { color: var(--fresh); font-weight: 600; }
.card-title { margin: 0; font-size: 17px; font-weight: 600; line-height: 1.45; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.card-title a { color: var(--ink); text-decoration: none; }
.card-title a::after { content: ""; position: absolute; inset: 0; z-index: 1; } /* ทั้งการ์ดกดได้ */
.card-title a:visited { color: var(--muted); }
.card-summary { margin: 0; font-size: 14.5px; color: var(--muted); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.save-btn { position: relative; z-index: 2; align-self: flex-start; margin-top: auto; border: 0; background: none; cursor: pointer; color: var(--muted); padding: 0; font-size: 13px; }
.save-btn[aria-pressed="true"] { color: var(--fresh); }

/* มุมมองการ์ด */
.view-grid .items { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 16px; }

/* มุมมองรายการ: แถวกะทัดรัด รูปเล็กด้านขวา */
.view-list .items { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-m); box-shadow: var(--shadow); }
.view-list .card { flex-direction: row-reverse; border: 0; border-radius: 0; box-shadow: none; background: none; align-items: flex-start; gap: 0; }
.view-list .card + .card { border-top: 1px solid var(--line); }
.view-list .card-media { width: 148px; flex: none; margin: 14px 14px 14px 0; border-radius: 8px; }
.view-list .card-title { font-size: 16.5px; -webkit-line-clamp: 2; }

.status { text-align: center; color: var(--muted); padding: 32px 0; }
.status:empty { display: none; }
.status button { margin-top: 10px; }
.sentinel { height: 1px; }

/* ลิ้นชักแหล่งข่าว (มือถือ) */
.drawer { border: 0; padding: 0; margin: 0 0 0 auto; height: 100dvh; max-height: 100dvh; width: min(360px, 88vw); background: var(--surface); color: var(--ink); }
.drawer::backdrop { background: rgb(0 0 0 / 40%); }
.drawer-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 8px; }
.drawer-head h2 { margin: 0; font-size: 18px; }
.drawer .side-filter { width: calc(100% - 32px); margin: 0 16px 8px; }
.drawer .side-list { padding: 0 8px 24px; }

@media (max-width: 960px) {
  .layout { grid-template-columns: minmax(0, 1fr); padding: 0 16px; }
  .sidebar { display: none; }
  .chips { display: flex; }
  .mobile-only { display: inline-block; }
}
@media (max-width: 640px) {
  .top-inner { padding: 8px 16px; gap: 10px; flex-wrap: wrap; }
  .brand span { display: none; }
  .search { order: 3; flex-basis: 100%; max-width: none; }
  .bar h1 { font-size: 20px; }
  .view-grid .items { grid-template-columns: minmax(0, 1fr); }
  .view-list .card-media { width: 96px; margin: 12px 12px 12px 0; }
  .view-list .card-summary { display: none; }
  .view-list .card-body { padding: 10px 12px; }
}
@media (prefers-reduced-motion: no-preference) {
  .card { transition: border-color 0.15s; }
}

/* ---------- แอดมิน ---------- */
.admin .top-inner { display: block; }
.brand-row { display: flex; align-items: center; gap: 12px; }
.admin .brand span { color: var(--muted); font-weight: 400; margin-left: 6px; }
.admin main { max-width: 1200px; margin: 0 auto; padding: 0 16px 64px; }
.tabs { display: flex; gap: 4px; padding: 10px 0 0; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-m); padding: 16px; margin-top: 16px; }
.panel h2 { margin: 0 0 12px; font-size: 17px; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; }
.stat b { display: block; font-size: 22px; }
.stat span { font-size: 13px; color: var(--muted); }
.form-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.form-row input[type="url"], .form-row input[type="text"], .form-row input[type="password"] { flex: 1 1 240px; }
input[type="url"], input[type="text"], input[type="password"], input[type="number"], select, textarea {
  padding: 6px 10px; border: 1px solid var(--line); border-radius: var(--radius-s); background: var(--paper);
}
textarea { width: 100%; min-height: 120px; }
.btn { border: 1px solid var(--brand); background: var(--brand); color: var(--brand-ink); border-radius: var(--radius-s); padding: 6px 14px; cursor: pointer; }
.btn.secondary { background: none; color: var(--ink); border-color: var(--line); }
.btn.danger { background: none; color: var(--danger); border-color: var(--line); }
.btn:disabled { opacity: 0.5; cursor: default; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-weight: 600; color: var(--muted); white-space: nowrap; }
td .url { color: var(--muted); font-size: 12px; word-break: break-all; }
td .err { color: var(--danger); font-size: 12px; }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 6px; vertical-align: 1px; }
.dot.ok { background: var(--ok); }
.dot.warn { background: var(--fresh); }
.dot.bad { background: var(--danger); }
.dot.off { background: var(--line); }
.actions { display: flex; gap: 4px; flex-wrap: wrap; }
.actions .btn { padding: 2px 8px; font-size: 13px; }
.note { font-size: 13px; color: var(--muted); }
.toast { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); z-index: 50; background: var(--ink); color: var(--paper); padding: 8px 16px; border-radius: var(--radius-s); font-size: 14px; }
.sample { font-size: 13px; margin: 8px 0 0; padding-left: 18px; }
