/* =========================================================================
   PF-Daily — ธีมดำ-ชมพู (Flat, dark-first)
   แนวทางจาก ui-ux-pro-max: token กลาง, คอนทราสต์ตัวอักษร ≥ 4.5:1,
   เป้ากด ≥ 44px, focus ชัด, ไอคอน SVG ชุดเดียว, เคลื่อนไหว 150–200ms
   ========================================================================= */

:root {
  /* พื้นผิว */
  --bg: #09090b;
  --surface: #131316;
  --surface-2: #1b1b20;
  --surface-3: #24242b;
  --border: #2a2a31;
  --border-strong: #3a3a44;

  /* ตัวอักษร (คอนทราสต์บน --surface: 18:1 / 7.2:1 / 5.5:1) */
  --text: #f4f4f5;
  --text-muted: #a1a1aa;
  --text-subtle: #8b8b95;

  /* แบรนด์ชมพู — ตัวหนังสือชมพูบนพื้นมืด 5.3:1, ปุ่มทึบตัวขาว 5.0:1 */
  --primary: #ff2e8a;
  --primary-hover: #ff4d9d;
  --primary-solid: #d1246a;
  --primary-solid-hover: #db2777;
  --primary-soft: rgba(255, 46, 138, 0.12);
  --primary-soft-strong: rgba(255, 46, 138, 0.2);
  --on-primary: #ffffff;
  --link: #ff7ab8;

  /* สถานะ */
  --ok: #34d399;
  --ok-soft: rgba(52, 211, 153, 0.12);
  --warn: #fbbf24;
  --warn-soft: rgba(251, 191, 36, 0.12);
  --err: #f87171;
  --err-soft: rgba(248, 113, 113, 0.12);
  --busy: #c084fc;
  --busy-soft: rgba(192, 132, 252, 0.14);

  /* ระยะ (จังหวะ 4/8) */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;

  /* รูปทรง */
  --radius-sm: 8px;
  --radius: 10px;
  --radius-lg: 14px;
  --radius-pill: 999px;

  /* ตัวอักษร */
  --font: 'Inter', 'IBM Plex Sans Thai', system-ui, -apple-system, 'Segoe UI', 'Noto Sans Thai', sans-serif;
  --text-xs: 0.75rem;
  --text-sm: 0.875rem;
  --text-base: 1rem;
  --text-lg: 1.125rem;
  --text-xl: 1.375rem;

  /* ไอคอน */
  --icon-sm: 16px;
  --icon: 18px;
  --icon-lg: 22px;

  /* การเคลื่อนไหว */
  --ease: cubic-bezier(0.2, 0, 0, 1);
  --dur-fast: 120ms;
  --dur: 180ms;

  --tap: 44px;
  color-scheme: dark;
}

/* ---------- พื้นฐาน ---------- */
*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: var(--text-base);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  padding-bottom: env(safe-area-inset-bottom);
}

h1, h2, h3 { line-height: 1.3; letter-spacing: -0.01em; }
a { color: var(--link); text-underline-offset: 3px; }
a:hover { color: var(--primary-hover); }
.muted { color: var(--text-muted); }
.small-note { font-size: var(--text-xs); margin: 0; }

.icon {
  width: var(--icon);
  height: var(--icon);
  flex-shrink: 0;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.icon-sm { width: var(--icon-sm); height: var(--icon-sm); }
.platform-facebook { color: #60a5fa; }
.platform-instagram { color: #f472b6; }
.with-icon { display: inline-flex; align-items: center; gap: var(--space-2); min-width: 0; }

:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

/* ---------- หัวเว็บ + แท็บ ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: calc(var(--space-3) + env(safe-area-inset-top)) var(--space-4) var(--space-3);
  background: rgba(9, 9, 11, 0.88);
  backdrop-filter: saturate(140%) blur(12px);
  -webkit-backdrop-filter: saturate(140%) blur(12px);
  border-bottom: 1px solid var(--border);
}
.topbar::after {
  content: '';
  position: absolute;
  inset: auto 0 -1px 0;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--primary) 20%, var(--primary) 80%, transparent);
  opacity: 0.7;
}
.topbar h1 {
  margin: 0;
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-lg);
  font-weight: 700;
  white-space: nowrap;
}
.topbar h1::before {
  content: '';
  display: inline-block;
  flex-shrink: 0;
  width: 30px;
  height: 30px;
  border-radius: var(--radius-sm);
  background: url('icon.svg') center / cover no-repeat;
}
.topbar-actions { display: flex; gap: var(--space-2); align-items: center; min-width: 0; }
.slots {
  font-size: var(--text-sm);
  font-weight: 600;
  white-space: nowrap;
  color: var(--primary);
  background: var(--primary-soft);
  border: 1px solid var(--primary-soft-strong);
  padding: var(--space-1) var(--space-3);
  border-radius: var(--radius-pill);
}
.user-chip { max-width: 34vw; }
.user-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.tabs {
  display: flex;
  gap: var(--space-1);
  padding: var(--space-2) var(--space-4);
  max-width: 1200px;
  margin: 0 auto;
  overflow-x: auto;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tabs .tab {
  min-height: 40px;
  padding: var(--space-2) var(--space-3);
  border: 1px solid transparent;
  border-radius: var(--radius-pill);
  background: transparent;
  color: var(--text-muted);
  font-weight: 600;
  white-space: nowrap;
  gap: var(--space-2);
}
.tabs .tab:hover { color: var(--text); background: var(--surface-2); }
.tabs .tab.active { color: var(--primary); background: var(--primary-soft); border-color: var(--primary-soft-strong); }

/* ---------- แถบแจ้งเตือน ---------- */
.banner {
  max-width: 1200px;
  margin: 0 auto var(--space-2);
  padding: var(--space-3) var(--space-4);
  font-size: var(--text-sm);
  color: var(--link);
  background: var(--primary-soft);
  border: 1px solid var(--primary-soft-strong);
  border-radius: var(--radius);
}
.banner.warn { color: var(--warn); background: var(--warn-soft); border-color: rgba(251, 191, 36, 0.25); }
@media (max-width: 1232px) { .banner { margin-left: var(--space-4); margin-right: var(--space-4); } }

/* ---------- เลย์เอาต์ + การ์ด ---------- */
.layout {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--space-4);
  padding: var(--space-2) var(--space-4) var(--space-6);
  max-width: 1200px;
  margin: 0 auto;
}
@media (min-width: 900px) {
  .layout { grid-template-columns: minmax(380px, 5fr) 6fr; align-items: start; gap: var(--space-5); }
  .layout.single { grid-template-columns: minmax(0, 900px); justify-content: center; }
}

.card {
  min-width: 0;
  padding: var(--space-5);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
}
@media (max-width: 480px) { .card { padding: var(--space-4); } }
.card h2 { margin: 0 0 var(--space-3); font-size: var(--text-lg); font-weight: 700; }
.queue-head { display: flex; justify-content: space-between; align-items: center; gap: var(--space-3); margin-bottom: var(--space-3); }
.queue-head h2 { margin: 0; }

/* ---------- ฟอร์ม ---------- */
form { display: flex; flex-direction: column; gap: var(--space-3); }
label, .legend, fieldset.segmented legend { font-weight: 600; font-size: var(--text-sm); color: var(--text); }
label.inline { font-weight: 500; display: flex; gap: var(--space-2); align-items: center; cursor: pointer; }

input[type='text'], input[type='password'], input[type='url'], input[type='datetime-local'], textarea {
  width: 100%;
  min-height: var(--tap);
  padding: 10px var(--space-3);
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  transition: border-color var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}
input::placeholder, textarea::placeholder { color: var(--text-subtle); }
input:hover, textarea:hover { border-color: #4a4a55; }
input:focus, textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft-strong); }
textarea { resize: vertical; white-space: pre-wrap; line-height: 1.6; }
input[type='checkbox'], input[type='radio'], input[type='range'], progress { accent-color: var(--primary); }
input[type='checkbox'], input[type='radio'] { width: 20px; height: 20px; flex-shrink: 0; margin: 0; cursor: pointer; }

fieldset.segmented { border: 0; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: var(--space-2); }
fieldset.segmented legend { margin-bottom: var(--space-2); width: 100%; padding: 0; }
fieldset.segmented label,
.channel-options label {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-height: var(--tap);
  padding: 10px var(--space-3);
  font-weight: 500;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: border-color var(--dur) var(--ease), background var(--dur) var(--ease);
}
fieldset.segmented label { flex: 1 1 0; min-width: 0; white-space: nowrap; }
@media (max-width: 480px) { fieldset.segmented label { padding-left: 10px; padding-right: 8px; gap: 6px; font-size: var(--text-sm); } }
fieldset.segmented label:hover, .channel-options label:hover { border-color: var(--border-strong); }
fieldset.segmented label:has(input:checked),
.channel-options label:has(input:checked) { border-color: var(--primary); background: var(--primary-soft); }
.channel-options label:has(input:disabled) { opacity: 0.5; cursor: not-allowed; }

.dropzone {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: 108px;
  padding: var(--space-5) var(--space-3);
  text-align: center;
  font-weight: 500;
  color: var(--text-muted);
  background: var(--surface-2);
  border: 1.5px dashed var(--border-strong);
  border-radius: var(--radius-lg);
  cursor: pointer;
  transition: border-color var(--dur) var(--ease), color var(--dur) var(--ease), background var(--dur) var(--ease);
}
.dropzone::before {
  content: '';
  width: 28px;
  height: 28px;
  background: currentColor;
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4'/%3E%3Cpath d='m17 8-5-5-5 5M12 3v12'/%3E%3C/svg%3E") center / contain no-repeat;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4'/%3E%3Cpath d='m17 8-5-5-5 5M12 3v12'/%3E%3C/svg%3E") center / contain no-repeat;
}
.dropzone:hover, .dropzone.dragover { color: var(--primary); border-color: var(--primary); background: var(--primary-soft); }
.dropzone input { display: none; }

.thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(76px, 1fr)); gap: var(--space-2); }
.thumbs figure { margin: 0; position: relative; }
.thumbs img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: var(--radius-sm); display: block; border: 1px solid var(--border); }
.thumbs figcaption {
  position: absolute; top: 4px; left: 4px;
  min-width: 20px; padding: 0 6px;
  font-size: var(--text-xs); font-weight: 700; text-align: center;
  color: #fff; background: rgba(0, 0, 0, 0.7); border-radius: var(--radius-pill);
}

.cover-picker { display: flex; flex-direction: column; gap: var(--space-2); padding: var(--space-3); background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-lg); }
.cover-picker h3 { margin: 0; font-size: var(--text-base); }
.cover-picker video { width: 100%; max-height: 420px; background: #000; border-radius: var(--radius); }
.cover-picker input[type='range'] { width: 100%; height: 36px; }
.cover-result { display: flex; gap: var(--space-3); align-items: center; }
.cover-result img { width: 64px; height: 114px; object-fit: cover; border-radius: var(--radius-sm); border: 1px solid var(--border); }

.caption-meta { font-size: var(--text-sm); color: var(--text-muted); font-variant-numeric: tabular-nums; }
.caption-meta.over { color: var(--err); font-weight: 600; }
.proofread { display: flex; flex-direction: column; gap: var(--space-2); padding: var(--space-3); background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-lg); }
.proofread p { margin: 0; }
.proofread-head { display: flex; align-items: center; justify-content: space-between; gap: var(--space-2); }
.proofread-head h3 { margin: 0; font-size: var(--text-base); display: flex; align-items: center; gap: var(--space-2); }
.proofread-clip { display: flex; flex-direction: column; gap: var(--space-2); }
.proofread-clip + .proofread-clip { padding-top: var(--space-2); border-top: 1px solid var(--border); }
.proofread-issues, .proofread-texts ul { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: var(--space-1); font-size: var(--text-sm); }
.proofread-issues li { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-1) var(--space-2); }
.proofread-texts summary { cursor: pointer; font-size: var(--text-sm); color: var(--text-muted); min-height: 32px; display: flex; align-items: center; }
.proofread-texts li { white-space: pre-line; overflow-wrap: anywhere; }
.pr-time { color: var(--text-muted); font-variant-numeric: tabular-nums; margin-right: var(--space-2); }
.pr-wrong { color: var(--err); text-decoration: line-through; }
.pr-correct { color: var(--ok); font-weight: 600; }
.pr-accept { margin-left: auto; font-size: var(--text-sm); }
.proofread-issues li.accepted { opacity: 0.55; }
@media (pointer: coarse) { .pr-accept { min-height: 44px; } }
.terms-box { border-top: 1px solid var(--border); padding-top: var(--space-2); display: flex; flex-direction: column; gap: var(--space-2); }
.terms-box summary { display: list-item; cursor: pointer; font-size: var(--text-sm); padding: var(--space-3) 0; }
.terms-box[open] { gap: var(--space-2); }
.terms-box textarea { width: 100%; }
.caption-draft { display: flex; flex-direction: column; gap: var(--space-2); padding-top: var(--space-2); border-top: 1px solid var(--border); }
.caption-draft-text { white-space: pre-wrap; overflow-wrap: anywhere; font-size: var(--text-sm); padding: var(--space-2) var(--space-3); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.caption-draft button { align-self: flex-start; }
.caption-spell:empty { display: none; }
.caption-spell { display: flex; flex-direction: column; gap: var(--space-1); }
.warnings { margin: 0; padding-left: 18px; color: var(--warn); font-size: var(--text-sm); }
.warnings li.error { color: var(--err); }
details > summary { cursor: pointer; color: var(--text-muted); font-size: var(--text-sm); min-height: 32px; display: flex; align-items: center; }
details > summary:hover { color: var(--text); }
.caption-preview { white-space: pre-wrap; word-break: break-word; background: var(--bg); padding: var(--space-3); border: 1px solid var(--border); border-radius: var(--radius); font-family: inherit; margin: var(--space-2) 0 0; }
.after-post { font-size: var(--text-sm); color: var(--text-muted); display: flex; flex-direction: column; gap: var(--space-1); padding: var(--space-3); background: var(--surface-2); border-radius: var(--radius); }
.after-post:empty { display: none; }
.message { margin: 0; min-height: 1.5em; white-space: pre-line; font-size: var(--text-sm); }
.message.error { color: var(--err); }
.message.ok { color: var(--ok); }
progress { width: 100%; height: 6px; border-radius: var(--radius-pill); }

.snippets { display: flex; flex-wrap: wrap; gap: var(--space-2); align-items: center; font-size: var(--text-sm); }
.snippet-list { display: contents; }
.chip { display: inline-flex; align-items: center; border: 1px solid var(--primary-soft-strong); border-radius: var(--radius-pill); overflow: hidden; background: var(--primary-soft); }
.chip button { min-height: 32px; padding: 2px var(--space-3); border: 0; border-radius: 0; background: transparent; color: var(--primary); font-size: var(--text-sm); }
.chip button:hover { background: var(--primary-soft-strong); }
.chip button.x { padding: 2px var(--space-2); color: var(--text-muted); }

/* ---------- ปุ่ม ---------- */
.row { display: flex; gap: var(--space-2); flex-wrap: wrap; }
button, .button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: var(--tap);
  padding: 10px var(--space-4);
  font: inherit;
  font-weight: 600;
  line-height: 1.2;
  color: var(--text);
  background: var(--surface-3);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: background var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease), opacity var(--dur-fast) var(--ease);
}
button:hover:not(:disabled), .button:hover { background: #2e2e37; border-color: #4a4a55; }
button:active:not(:disabled) { opacity: 0.8; }
button.primary { color: var(--on-primary); background: var(--primary-solid); border-color: var(--primary-solid); font-size: var(--text-base); }
button.primary:hover:not(:disabled) { background: var(--primary-solid-hover); border-color: var(--primary-solid-hover); }
button.secondary, .button.secondary { background: transparent; border-color: var(--border-strong); }
button.secondary:hover:not(:disabled), .button.secondary:hover { background: var(--surface-2); }
button.danger { color: var(--err); background: transparent; border-color: rgba(248, 113, 113, 0.45); }
button.danger:hover:not(:disabled) { background: var(--err-soft); border-color: var(--err); }
button.small { min-height: 36px; padding: 6px var(--space-3); font-size: var(--text-sm); }
button.link { min-height: 0; padding: 0; border: 0; background: none; color: var(--primary); text-decoration: underline; text-underline-offset: 3px; }
button.link:hover:not(:disabled) { background: none; color: var(--primary-hover); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
/* เป้ากดบนจอสัมผัสต้อง ≥ 44px */
@media (pointer: coarse) { button.small { min-height: var(--tap); } }

/* ---------- เลือกเพจ ---------- */
.channel-picker { display: flex; flex-direction: column; gap: var(--space-2); }
.group-title { font-size: var(--text-xs); font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.06em; margin: var(--space-1) 0; }
.channel-group:has(.channel-options:empty) { display: none; }
.channel-options { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: var(--space-2); }
.channel-options .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.count { font-size: var(--text-xs); font-weight: 600; color: var(--text-muted); background: var(--surface-3); border-radius: var(--radius-pill); padding: 1px var(--space-2); white-space: nowrap; font-variant-numeric: tabular-nums; }
.count.full { color: var(--err); background: var(--err-soft); }
.empty { padding: var(--space-4); color: var(--text-muted); border: 1px dashed var(--border-strong); border-radius: var(--radius); text-align: center; }
.existing { font-size: var(--text-sm); color: var(--text-muted); display: flex; flex-direction: column; gap: var(--space-2); }

/* ---------- คิวโพส ---------- */
.queue-summary { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: var(--space-2); }
.queue-summary .item { padding: var(--space-2) var(--space-3); font-size: var(--text-sm); background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius); }
.queue-summary .item .label { display: flex; justify-content: space-between; align-items: center; gap: var(--space-2); }
.queue-summary .item .label span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.queue-summary .item b { font-variant-numeric: tabular-nums; }
.meter { height: 4px; margin-top: var(--space-2); background: var(--surface-3); border-radius: var(--radius-pill); overflow: hidden; }
.meter > div { height: 100%; background: var(--primary); border-radius: var(--radius-pill); transition: width var(--dur) var(--ease); }
.system-info { display: flex; flex-wrap: wrap; gap: var(--space-1) var(--space-4); font-size: var(--text-xs); margin-top: var(--space-3); color: var(--text-muted); }
.system-info .err { color: var(--err); font-weight: 600; }

.post-list { display: flex; flex-direction: column; gap: var(--space-3); margin-top: var(--space-4); }
.post-list > h3 { margin: var(--space-3) 0 0; font-size: var(--text-sm); color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.06em; }
.post {
  display: grid;
  grid-template-columns: auto 76px 1fr;
  gap: var(--space-3);
  padding: var(--space-3);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  transition: border-color var(--dur) var(--ease);
}
.post:hover { border-color: var(--border-strong); }
.post.editing { border-color: var(--primary); box-shadow: 0 0 0 1px var(--primary) inset; }
.post .thumb { width: 76px; height: 76px; object-fit: cover; border-radius: var(--radius); background: var(--bg); border: 1px solid var(--border); }
.post .thumb.removed { display: flex; align-items: center; justify-content: center; color: var(--text-subtle); }
.post .body { min-width: 0; display: flex; flex-direction: column; gap: var(--space-2); }
.post .head { display: flex; flex-wrap: wrap; gap: var(--space-1) var(--space-2); align-items: center; }
.post .caption { white-space: pre-wrap; word-break: break-word; font-size: var(--text-sm); max-height: 4.8em; overflow: hidden; color: var(--text); }
.post .actions { display: flex; flex-wrap: wrap; gap: var(--space-2); padding-top: var(--space-1); }
.post .order { display: flex; flex-direction: column; align-items: center; gap: var(--space-1); min-width: 40px; }
.post .order .num { font-weight: 800; color: var(--primary); font-size: var(--text-base); font-variant-numeric: tabular-nums; }
.post .move { display: flex; flex-direction: column; gap: var(--space-1); }
.post .move button { min-height: 32px; width: 36px; padding: 0; }
@media (max-width: 480px) {
  /* มือถือ: ลำดับ + ปุ่มเลื่อน + รูปเล็ก อยู่แถวบน เนื้อหาเต็มความกว้างด้านล่าง */
  .post { grid-template-columns: auto 64px 1fr; align-items: center; }
  .post .thumb { width: 64px; height: 64px; }
  .post .body { grid-column: 1 / -1; }
  .post .order { flex-direction: row; gap: var(--space-2); }
  .post .move { flex-direction: row; }
  .post .order:empty { display: none; }
  .post:has(.order:empty) { grid-template-columns: 64px 1fr; }
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 2px var(--space-2);
  font-size: var(--text-xs);
  font-weight: 700;
  border-radius: var(--radius-pill);
  color: var(--text-muted);
  background: var(--surface-3);
}
.badge::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.scheduled { color: var(--primary); background: var(--primary-soft); }
.badge.publishing { color: var(--busy); background: var(--busy-soft); }
.badge.published { color: var(--ok); background: var(--ok-soft); }
.badge.partial { color: var(--warn); background: var(--warn-soft); }
.badge.failed { color: var(--err); background: var(--err-soft); }
.tag {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  max-width: 100%;
  padding: 1px var(--space-2);
  font-size: var(--text-xs);
  color: var(--text-muted);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-pill);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tag .icon { width: 14px; height: 14px; }

.platform-result { padding-top: var(--space-2); border-top: 1px solid var(--border); font-size: var(--text-sm); }
.platform-result > strong { font-weight: 600; vertical-align: middle; }
.platform-result > div, .platform-result > ul { margin-top: var(--space-1); }
.platform-result ul { margin: 0; padding-left: 18px; }
.platform-result .err { color: var(--err); }
.platform-result .warn { color: var(--warn); }
.task { display: flex; gap: var(--space-2); align-items: center; flex-wrap: wrap; color: var(--text-muted); }
.task.done .icon { color: var(--ok); }

/* ---------- ไฟล์ที่อัปโหลด ---------- */
.storage-list { display: flex; flex-direction: column; gap: var(--space-2); margin-top: var(--space-3); }
.storage-post { display: flex; flex-direction: column; gap: var(--space-2); padding: var(--space-3); background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-lg); }
.storage-post-head { display: grid; grid-template-columns: 1fr auto; gap: var(--space-3); align-items: center; }
.storage-post .head { display: flex; flex-wrap: wrap; gap: var(--space-2); align-items: center; }
.storage-post .caption { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: var(--text-sm); color: var(--text-muted); }
.storage-post .body { min-width: 0; }
.storage-file { display: grid; grid-template-columns: 48px 1fr auto; gap: var(--space-3); align-items: center; padding-top: var(--space-2); border-top: 1px solid var(--border); font-size: var(--text-sm); }
.storage-file .thumb { width: 48px; height: 48px; object-fit: cover; border-radius: var(--radius-sm); background: var(--surface-3); display: flex; align-items: center; justify-content: center; color: var(--text-muted); }
.storage-file .name { font-size: var(--text-xs); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- หน้าต่าง (จัดการเพจ / บัญชี) ---------- */
dialog.sheet {
  width: min(720px, calc(100vw - 32px));
  max-height: 90vh;
  margin: auto;
  padding: 0 var(--space-5) var(--space-5);
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
}
dialog.sheet[open] { animation: sheet-in var(--dur) var(--ease); }
dialog.sheet::backdrop { background: rgba(0, 0, 0, 0.72); backdrop-filter: blur(2px); }
@keyframes sheet-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@media (max-width: 600px) {
  dialog.sheet { width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; border: 0; border-radius: 0; padding: 0 var(--space-4) calc(var(--space-5) + env(safe-area-inset-bottom)); }
}
.sheet-head {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--space-3);
  padding: calc(var(--space-4) + env(safe-area-inset-top)) 0 var(--space-3);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.sheet-head h2 { margin: 0; font-size: var(--text-lg); }
dialog section { display: flex; flex-direction: column; gap: var(--space-2); margin-top: var(--space-5); }
dialog h3 { margin: 0; font-size: var(--text-base); }
.channel-list { display: flex; flex-direction: column; gap: var(--space-3); }
.channel-card { display: flex; flex-direction: column; gap: var(--space-2); padding: var(--space-4); background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-lg); }
.channel-card .title { display: flex; justify-content: space-between; align-items: center; gap: var(--space-2); flex-wrap: wrap; }
.channel-card label { font-size: var(--text-xs); color: var(--text-muted); }
.candidate { display: flex; gap: var(--space-2); align-items: center; min-height: var(--tap); padding: var(--space-2) 0; border-bottom: 1px solid var(--border); font-weight: 500; }
.token-status.ok { color: var(--ok); border-color: rgba(52, 211, 153, 0.4); }
.token-status.bad { color: var(--err); border-color: rgba(248, 113, 113, 0.4); }
.token-status.warn { color: var(--warn); border-color: rgba(251, 191, 36, 0.45); }

/* ---------- ล็อกอิน ---------- */
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
  background:
    radial-gradient(600px 300px at 50% -10%, rgba(255, 46, 138, 0.16), transparent 70%),
    var(--bg);
}
.login-wrap { width: 100%; max-width: 400px; }
.login-card { display: flex; flex-direction: column; gap: var(--space-2); }
.login-wrap .login-card { padding: var(--space-6) var(--space-5); }
.login-title { margin: 0; display: flex; align-items: center; gap: var(--space-3); font-size: var(--text-xl); font-weight: 700; }
.login-title::before {
  content: '';
  display: inline-block;
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  border-radius: var(--radius);
  background: url('icon.svg') center / cover no-repeat;
}
.login-links { display: flex; flex-wrap: wrap; justify-content: center; gap: var(--space-2) var(--space-4); margin: var(--space-4) 0 0; font-size: var(--text-xs); }
.login-links a { color: var(--text-muted); }
.login-links a:hover { color: var(--link); }

/* ---------- หน้าเอกสาร (ข้อกำหนด / ความเป็นส่วนตัว / ลบข้อมูล) — เปิดได้โดยไม่ต้องล็อกอิน ---------- */
.doc-page { padding: var(--space-5) var(--space-4) var(--space-6); }
.doc { max-width: 760px; margin: 0 auto; }
.doc .card { padding: var(--space-5); }
.doc .login-title { margin-bottom: var(--space-4); }
.doc h1 { margin: 0 0 var(--space-1); font-size: var(--text-xl); }
.doc h2 { margin: var(--space-5) 0 var(--space-2); font-size: var(--text-lg); }
.doc p, .doc li { line-height: 1.7; }
.doc ul, .doc ol { padding-left: 1.3em; margin: var(--space-2) 0; }
.doc li { margin: var(--space-1) 0; }
.doc .doc-nav { display: flex; flex-wrap: wrap; gap: var(--space-2) var(--space-4); margin-top: var(--space-5); padding-top: var(--space-4); border-top: 1px solid var(--border); font-size: var(--text-sm); }
.login-card label { margin-top: var(--space-2); }
.login-card button.primary { margin-top: var(--space-4); }

/* ---------- ผู้ใช้ (แอดมิน) ---------- */
.user-list { display: flex; flex-direction: column; gap: var(--space-2); }
.user-row { display: flex; flex-wrap: wrap; gap: var(--space-3); align-items: center; justify-content: space-between; padding: var(--space-3) var(--space-4); background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-lg); }
.user-row .who { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.user-row .who small { color: var(--text-muted); font-size: var(--text-xs); }
.user-row .actions { display: flex; flex-wrap: wrap; gap: var(--space-2); }
.form-grid { display: grid; gap: var(--space-3); grid-template-columns: 1fr; margin-bottom: var(--space-2); }
@media (min-width: 640px) { .form-grid { grid-template-columns: 1fr 1fr auto; align-items: end; } }
.form-grid label { display: block; margin-bottom: var(--space-1); }
#usersMessage { margin: var(--space-2) 0; }

/* ---------- มือถือ: หัวเว็บบรรทัดเดียว ---------- */
@media (max-width: 480px) {
  .topbar { padding-left: var(--space-3); padding-right: var(--space-3); gap: var(--space-2); }
  .topbar .slots { display: none; }
  .topbar h1 { font-size: var(--text-base); }
  .topbar-actions { gap: var(--space-1); }
  .topbar-actions button { padding-left: 10px; padding-right: 10px; }
  .tabs { padding-left: var(--space-3); padding-right: var(--space-3); }
  .layout { padding-left: var(--space-3); padding-right: var(--space-3); }
}
/* จอแคบมาก: ปุ่มจัดการเพจเหลือแต่ไอคอน (มี aria-label) */
@media (max-width: 360px) {
  #openChannelsBtn span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
}

/* ---------- ปุ่มกลับขึ้นด้านบน (ลอยมุมขวาล่าง โผล่เมื่อเลื่อนลงมา) ---------- */
.to-top {
  position: fixed; right: calc(var(--space-4) + env(safe-area-inset-right)); bottom: calc(var(--space-4) + env(safe-area-inset-bottom));
  z-index: 15; width: 48px; height: 48px; min-height: 0; padding: 0; border-radius: var(--radius-pill);
  background: var(--primary-solid); color: var(--on-primary); border-color: transparent; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.5);
}
.to-top:hover:not(:disabled) { background: var(--primary-solid-hover); border-color: transparent; }
.to-top .icon { width: var(--icon-lg); height: var(--icon-lg); }

/* ---------- ลดการเคลื่อนไหวตามที่ผู้ใช้ตั้งไว้ ---------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition-duration: 0.01ms !important; scroll-behavior: auto !important; }
}

/* ---------- ไฟล์ที่เลือก: จัดลำดับก่อนโพส ---------- */
#albumPreview { grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: var(--space-3); }
.media-item { display: flex; flex-direction: column; gap: var(--space-1); }
.media-item img, .media-item video {
  width: 100%; aspect-ratio: 1; object-fit: cover; display: block;
  border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--bg);
}
.media-item figcaption { display: inline-flex; align-items: center; gap: 2px; }
.media-tools { display: grid; grid-template-columns: repeat(3, 1fr); gap: var(--space-1); }
.media-tools button { min-height: 32px; padding: 0; }
@media (pointer: coarse) { .media-tools button { min-height: 40px; } }
.caption-block { display: flex; flex-direction: column; gap: var(--space-3); }

/* ---------- ตัวอย่างโพส ---------- */
.post-preview-box { border: 1px solid var(--border); border-radius: var(--radius-lg); background: var(--surface-2); }
.post-preview-box > summary { gap: var(--space-2); padding: var(--space-2) var(--space-3); font-weight: 600; color: var(--text); }
.post-preview { display: flex; flex-direction: column; gap: var(--space-3); padding: 0 var(--space-3) var(--space-3); }
/* ตัวอย่าง 1 ชุดต่อเพจ/บัญชีที่เลือก */
.pv-post { display: flex; flex-direction: column; gap: var(--space-3); min-width: 0; }
.pv-post + .pv-post { padding-top: var(--space-4); border-top: 1px solid var(--border-strong); }
.pv-head { display: flex; align-items: center; gap: var(--space-2); }
.pv-avatar {
  display: inline-grid; place-items: center; flex-shrink: 0;
  width: 36px; height: 36px; border-radius: 50%;
  background: var(--surface-3); border: 1px solid var(--border-strong);
}
.pv-avatar.small { width: 28px; height: 28px; }
.pv-avatar.small .icon { width: 14px; height: 14px; }
.pv-who { display: flex; flex-direction: column; min-width: 0; line-height: 1.3; }
.pv-who strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pv-who small { font-size: var(--text-xs); }
.pv-empty { margin: 0; padding: var(--space-5) 0; text-align: center; }
.post-preview img, .post-preview video { display: block; width: 100%; background: #000; }

/* อัลบั้ม: แสดงครบทุกรูป */
.pv-album { display: grid; grid-template-columns: repeat(2, 1fr); gap: 2px; border-radius: var(--radius); overflow: hidden; }
.pv-album.count-1 { grid-template-columns: 1fr; }
.pv-album.count-3 figure:first-child, .pv-album.count-5 figure:first-child { grid-column: 1 / -1; }
.pv-album figure { margin: 0; position: relative; }
.pv-album img, .pv-album video { aspect-ratio: 1; object-fit: cover; }
.pv-album.count-1 img, .pv-album.count-1 video { aspect-ratio: auto; max-height: 520px; object-fit: contain; }
.pv-album figcaption, .pv-story figcaption {
  position: absolute; top: 6px; left: 6px; padding: 0 6px;
  font-size: var(--text-xs); font-weight: 700; color: #fff; background: rgba(0, 0, 0, 0.65); border-radius: var(--radius-pill);
}

/* Reels / สตอรี่: แนวตั้ง 9:16 */
.pv-reel { display: flex; justify-content: center; }
.pv-reel video { width: min(100%, 300px); aspect-ratio: 9 / 16; object-fit: cover; border-radius: var(--radius); }
.pv-stories { display: flex; gap: var(--space-2); overflow-x: auto; scroll-snap-type: x mandatory; padding-bottom: var(--space-1); }
.pv-story { position: relative; flex: 0 0 140px; margin: 0; scroll-snap-align: start; }
.pv-story img, .pv-story video { aspect-ratio: 9 / 16; object-fit: cover; border-radius: var(--radius); border: 1px solid var(--border); }

.pv-caption { white-space: pre-wrap; word-break: break-word; font-size: var(--text-sm); line-height: 1.6; }
.pv-comment { display: flex; gap: var(--space-2); align-items: flex-start; padding-top: var(--space-2); border-top: 1px solid var(--border); font-size: var(--text-sm); }
.pv-comment > div { background: var(--surface-3); padding: var(--space-2) var(--space-3); border-radius: var(--radius-lg); min-width: 0; word-break: break-word; }
fieldset.segmented label span { overflow: hidden; text-overflow: ellipsis; }
@media (max-width: 400px) { fieldset.segmented label .icon { display: none; } }
/* จอแคบมาก (≤360px): ซ่อนวงกลม radio — ปุ่มที่เลือกยังเห็นชัดจากกรอบ/พื้นชมพู และกดได้ทั้งปุ่ม */
@media (max-width: 360px) {
  fieldset.segmented label { justify-content: center; padding-left: 6px; padding-right: 6px; position: relative; }
  fieldset.segmented input[type='radio'] { position: absolute; opacity: 0; pointer-events: none; }
  fieldset.segmented label:has(input:focus-visible) { outline: 2px solid var(--primary); outline-offset: 2px; }
}
