:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --border: #dde2ea;
  --text: #1d2633;
  --muted: #667085;
  --brand: #0b6fa4;
  --brand-dark: #085a86;
  --brand-soft: #eaf4fa;
  --danger: #c0392b;
  --warn-bg: #fff6e5;
  --warn-border: #f0c36d;

  --idea: #8e6fd8;
  --draft: #8a94a6;
  --scheduled: #0b6fa4;
  --published: #1e9e63;
  --partial: #d98a1c;
  --failed: #c0392b;

  --facebook: #1877f2;
  --instagram: #c13584;
  --linkedin: #0a66c2;
  --tiktok: #111111;

  --radius: 8px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  color: var(--text);
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); }
h1, h2, h3 { margin: 0; }
h2 { font-size: 1.1rem; }
h3 { font-size: 0.95rem; }
[hidden] { display: none !important; }
a { color: var(--brand); }

/* ---------- Login ---------- */
.login {
  max-width: 360px;
  margin: 12vh auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.login h1 { color: var(--brand); font-size: 1.4rem; }
.login label { display: flex; flex-direction: column; gap: 4px; font-weight: 600; font-size: 0.9rem; }
.login-error { color: var(--danger); margin: 0; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 12px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar h1 { font-size: 1.2rem; color: var(--brand); }
.topbar nav { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
.tab {
  background: none;
  border: none;
  padding: 8px 14px;
  border-radius: var(--radius);
  font: inherit;
  color: var(--muted);
  cursor: pointer;
}
.tab:hover { background: var(--bg); }
.tab.active { background: var(--bg); color: var(--text); font-weight: 600; }
.logout { color: var(--muted); font-size: 0.85rem; }

main { padding: 24px; max-width: 1300px; margin: 0 auto; }
.view { display: flex; flex-direction: column; gap: 16px; }

/* ---------- Buttons & inputs ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { border-color: #b9c1cd; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-dark); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 4px 10px; font-size: 0.85rem; }
.icon-btn {
  background: none;
  border: none;
  font-size: 1.6rem;
  line-height: 1;
  cursor: pointer;
  color: var(--muted);
}
.icon-btn.small { font-size: 1.2rem; }
.file-btn input { display: none; }

input, select, textarea {
  font: inherit;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  width: 100%;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: -1px; border-color: transparent; }

.hint { color: var(--muted); font-size: 0.85rem; font-weight: normal; }
.empty { color: var(--muted); text-align: center; padding: 24px; }

/* ---------- Badges ---------- */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  color: #fff;
  background: var(--draft);
  white-space: nowrap;
}
.badge.idea, .chip.idea { background: var(--idea); }
.badge.draft, .chip.draft { background: var(--draft); }
.badge.scheduled, .chip.scheduled, .badge.t-publishing { background: var(--scheduled); }
.badge.published, .chip.published, .badge.t-published { background: var(--published); }
.badge.partially_published, .chip.partially_published { background: var(--partial); }
.badge.failed, .chip.failed, .badge.t-failed { background: var(--failed); }
.badge.t-pending { background: var(--draft); }

/* Platform tag, coloured per network; t-* shows that account's publish state */
.pf {
  display: inline-block;
  padding: 1px 6px;
  margin: 2px 4px 2px 0;
  border-radius: 4px;
  font-size: 0.72rem;
  font-weight: 600;
  color: #fff;
  background: var(--draft);
  white-space: nowrap;
}
.pf.facebook { background: var(--facebook); }
.pf.instagram { background: var(--instagram); }
.pf.linkedin, .pf.linkedin_page { background: var(--linkedin); }
.pf.tiktok { background: var(--tiktok); }
.pf.t-failed { outline: 2px solid var(--failed); outline-offset: 1px; }
.pf.t-published::after { content: " \2713"; }

/* ---------- Dashboard ---------- */
.alerts:empty { display: none; }
.alert {
  background: var(--warn-bg);
  border: 1px solid var(--warn-border);
  border-radius: var(--radius);
  padding: 10px 14px;
  margin-bottom: 8px;
}
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; }
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 5px solid var(--draft);
  border-radius: var(--radius);
  padding: 16px;
  color: var(--muted);
}
.stat span { display: block; font-size: 2rem; font-weight: 700; color: var(--text); }
.stat.idea { border-left-color: var(--idea); }
.stat.scheduled { border-left-color: var(--scheduled); }
.stat.published { border-left-color: var(--published); }

.columns { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}
.panel .hint { margin: 4px 0 8px; }

.post-list, .account-list { list-style: none; margin: 8px 0 0; padding: 0; }
.post-list li, .account-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 8px;
  border-top: 1px solid var(--border);
}
.post-list li { cursor: pointer; }
.post-list li:hover { background: var(--bg); }
.post-list li.empty, .account-list li.empty { cursor: default; border: none; justify-content: center; }
.post-list li.empty:hover { background: none; }
.post-list .when { width: 120px; flex-shrink: 0; font-size: 0.85rem; color: var(--muted); }
.what { flex: 1; min-width: 0; }
.what strong { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.connect-buttons { display: flex; flex-wrap: wrap; gap: 8px; }

/* ---------- Calendar ---------- */
.cal-toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.cal-toolbar h2 { margin: 0 12px; min-width: 160px; }
.calendar {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  background: var(--border);
  gap: 1px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.calendar .dow {
  background: var(--surface);
  padding: 8px;
  text-align: center;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
}
.day {
  background: var(--surface);
  min-height: 110px;
  padding: 4px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
}
.day:hover { background: #fafbfd; }
.day.other { background: #f9fafb; }
.day.other .num { color: #b0b7c3; }
.day.today .num { background: var(--brand); color: #fff; border-radius: 999px; }
.day.drop-target { outline: 2px dashed var(--brand); outline-offset: -3px; }
.day .num {
  align-self: flex-end;
  font-size: 0.8rem;
  min-width: 22px;
  height: 22px;
  line-height: 22px;
  text-align: center;
  padding: 0 4px;
}
.chip {
  color: #fff;
  font-size: 0.78rem;
  padding: 3px 6px;
  border-radius: 4px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
}
.chip[draggable=true] { cursor: grab; }
.chip:hover { filter: brightness(1.1); }
.chip-pf { opacity: 0.8; font-size: 0.7rem; }

/* ---------- Posts table ---------- */
.filters { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.filters input[type=search] { flex: 1; min-width: 200px; }
.filters select { width: auto; }
.posts-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.posts-table th, .posts-table td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); }
.posts-table th { font-size: 0.8rem; color: var(--muted); font-weight: 600; }
.posts-table tbody tr { cursor: pointer; }
.posts-table tbody tr:hover { background: var(--bg); }
.posts-table td:first-child { white-space: nowrap; color: var(--muted); font-size: 0.9rem; }

/* ---------- Media ---------- */
.media-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; }
.media-card {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.media-card img, .video-ph { display: block; width: 100%; aspect-ratio: 1; object-fit: cover; background: #e9edf2; }
.video-ph { display: flex; align-items: center; justify-content: center; color: var(--muted); font-weight: 600; }
.media-info { display: flex; flex-direction: column; padding: 6px 8px; font-size: 0.8rem; }
.media-info span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.media-card .icon-btn { position: absolute; top: 4px; right: 4px; background: rgba(255, 255, 255, 0.9); border-radius: 999px; width: 28px; height: 28px; }
.selectable .media-card { cursor: pointer; }
.selectable .media-card.selected { outline: 3px solid var(--brand); }
.selectable .media-card.attached { opacity: 0.4; cursor: default; }

/* ---------- Editor dialog ---------- */
dialog {
  border: none;
  border-radius: 12px;
  padding: 0;
  width: min(820px, 95vw);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
}
dialog::backdrop { background: rgba(20, 30, 45, 0.45); }
#post-form { display: flex; flex-direction: column; gap: 14px; padding: 20px 24px; }
.editor-head, .picker-head { display: flex; align-items: center; gap: 12px; }
.editor-head h2 { flex: 1; order: -1; }
.editor-head #editor-status { order: 0; }
.editor-head .icon-btn { order: 1; }
.picker-head { padding: 20px 24px 12px; justify-content: space-between; }
#post-form label, .media-field .label { display: flex; flex-direction: column; gap: 4px; font-weight: 600; font-size: 0.9rem; }
#post-form label .hint { display: inline; }
fieldset { border: none; padding: 0; margin: 0; }
legend { font-weight: 600; font-size: 0.9rem; margin-bottom: 6px; padding: 0; }

.account-checks { display: flex; flex-wrap: wrap; gap: 8px; }
#post-form .pf-check {
  flex-direction: row;
  align-items: center;
  gap: 0;
  font-weight: normal;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 10px;
  cursor: pointer;
}
.pf-check:has(input:checked) { border-color: var(--brand); background: var(--brand-soft); }
.pf-check:has(input:disabled) { opacity: 0.7; cursor: default; }
.pf-check input { width: auto; margin: 0 6px 0 0; }

details summary { cursor: pointer; font-weight: 600; font-size: 0.9rem; color: var(--brand); }
#overrides { display: flex; flex-direction: column; gap: 10px; margin-top: 10px; }

.row { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }
.counts { display: flex; flex-wrap: wrap; gap: 12px; font-size: 0.8rem; color: var(--muted); margin-top: -8px; }
.counts .over { color: var(--danger); font-weight: 600; }

.media-field { display: flex; flex-direction: column; gap: 6px; }
.media-field > div:last-child { display: flex; gap: 8px; }
.media-strip { display: flex; flex-wrap: wrap; gap: 8px; }
.media-strip:empty { display: none; }
.strip-item { width: 110px; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.strip-item img, .strip-item .video-ph { width: 110px; height: 110px; object-fit: cover; display: flex; }
.strip-actions { display: flex; }
.strip-actions button { flex: 1; border: none; border-top: 1px solid var(--border); background: var(--surface); cursor: pointer; padding: 3px; }
.strip-actions button:hover:not(:disabled) { background: var(--bg); }
.strip-actions button:disabled { color: #c5cbd4; cursor: default; }

.problems { margin: 0; padding: 10px 14px 10px 30px; background: var(--warn-bg); border: 1px solid var(--warn-border); border-radius: var(--radius); font-size: 0.85rem; }
.problems:empty { display: none; }
.target-results:empty { display: none; }
.target-results ul { list-style: none; padding: 0; margin: 8px 0 0; display: flex; flex-direction: column; gap: 6px; }
.target-error { color: var(--danger); font-size: 0.85rem; margin-top: 2px; }

.editor-actions { display: flex; align-items: center; gap: 8px; padding-top: 8px; border-top: 1px solid var(--border); }
#media-picker .editor-actions { padding: 12px 24px 20px; }
#picker-grid { padding: 0 24px; max-height: 60vh; overflow-y: auto; }
.spacer { flex: 1; }

/* ---------- Toast ---------- */
#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(150%);
  max-width: min(600px, 90vw);
  background: var(--text);
  color: #fff;
  padding: 10px 18px;
  border-radius: var(--radius);
  white-space: pre-line;
  transition: transform 0.25s;
  z-index: 100;
}
#toast.show { transform: translateX(-50%) translateY(0); }
#toast.error { background: var(--danger); }

/* ---------- Small screens ---------- */
@media (max-width: 800px) {
  .topbar { flex-wrap: wrap; gap: 8px; padding: 10px 16px; }
  main { padding: 16px; }
  .stats { grid-template-columns: 1fr 1fr; }
  .columns, .row { grid-template-columns: 1fr; }
  .day { min-height: 70px; }
  .posts-table { display: block; overflow-x: auto; }
}
