:root {
  --bg: #f4f6fb;
  --bg-card: #ffffff;
  --sidebar-bg: #12141d;
  --sidebar-text: #a7adc0;
  --sidebar-active: #ffffff;
  --border: #e6e9f2;
  --text: #1a1d29;
  --text-muted: #6b7280;
  --primary: #5b5bd6;
  --primary-dark: #4747c4;
  --success: #16a34a;
  --danger: #dc2626;
  --warning: #d97706;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(16, 24, 40, .06), 0 1px 2px rgba(16, 24, 40, .04);
  --shadow-lg: 0 10px 30px rgba(16, 24, 40, .12);
  --insta: #E4405F;
  --fb: #1877F2;
  --tiktok: #000000;
  --kwai: #FF4A00;
  --linkedin: #0A66C2;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; }

body {
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
}

.layout { display: flex; min-height: 100vh; }

/* ---------- Sidebar ---------- */
.sidebar {
  width: 240px;
  flex-shrink: 0;
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  padding: 20px 14px;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 8px 22px;
  border-bottom: 1px solid rgba(255,255,255,.08);
  margin-bottom: 16px;
}

.brand-mark {
  width: 34px; height: 34px;
  display: grid; place-items: center;
  border-radius: 9px;
  background: linear-gradient(135deg, var(--primary), #8b5cf6);
  color: #fff; font-weight: 800; font-size: 15px;
}

.brand-name { color: #fff; font-weight: 700; font-size: 18px; letter-spacing: .2px; }
.brand-plus { color: #8b5cf6; }

.sidebar-nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }

.nav-link {
  display: flex; align-items: center; gap: 12px;
  padding: 9px 10px;
  border-radius: 8px;
  color: var(--sidebar-text);
  text-decoration: none;
  font-weight: 500;
  transition: background .15s, color .15s;
}

.nav-link:hover { background: rgba(255,255,255,.06); color: #fff; }
.nav-link.active { background: rgba(91,91,214,.18); color: var(--sidebar-active); }

.nav-icon { font-size: 14px; width: 18px; text-align: center; }

.nav-section-label {
  font-size: 11px; text-transform: uppercase; letter-spacing: .8px;
  color: rgba(167,173,192,.5);
  padding: 18px 10px 6px;
}

.nav-platform { padding: 7px 10px; font-size: 13px; }

.platform-dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }

.sidebar-footer {
  padding: 14px 10px 4px;
  border-top: 1px solid rgba(255,255,255,.08);
  font-size: 12px;
}

.sidebar-server-state { display: inline-flex; align-items: center; gap: 7px; color: var(--sidebar-text); }

.status-pulse {
  width: 8px; height: 8px; border-radius: 50%;
  background: #22c55e;
  box-shadow: 0 0 0 0 rgba(34,197,94,.5);
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(34,197,94,.5); }
  70% { box-shadow: 0 0 0 8px rgba(34,197,94,0); }
  100% { box-shadow: 0 0 0 0 rgba(34,197,94,0); }
}

/* ---------- Main ---------- */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 22px 32px 16px;
}

.topbar-title { font-size: 24px; font-weight: 700; letter-spacing: -.3px; }

.content { padding: 0 32px 40px; flex: 1; }

/* ---------- Cards ---------- */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  margin-bottom: 20px;
}

.card-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 16px; gap: 12px; flex-wrap: wrap;
}

.card-title { font-size: 15px; font-weight: 600; }

.card-toolbar { display: flex; align-items: center; gap: 10px; margin-left: auto; }

.metric-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 16px;
  margin-bottom: 20px;
}

.metric-card { padding: 18px; transition: transform .15s, box-shadow .15s; }
.metric-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }

.metric-label { font-size: 12px; color: var(--text-muted); font-weight: 500; text-transform: uppercase; letter-spacing: .5px; }

.metric-value { font-size: 26px; font-weight: 700; margin-top: 6px; letter-spacing: -.4px; }

.metric-sub { font-size: 12px; color: var(--text-muted); margin-top: 4px; }

.skeleton-card { height: 96px; background: linear-gradient(90deg, #eef0f6 25%, #f7f8fc 50%, #eef0f6 75%); background-size: 200% 100%; animation: shimmer 1.4s infinite; border: none; }

@keyframes shimmer { to { background-position: -200% 0; } }

/* ---------- Grids ---------- */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }

@media (max-width: 1100px) { .grid-2 { grid-template-columns: 1fr; } }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 9px 16px;
  border-radius: 8px;
  font-weight: 600; font-size: 13.5px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: all .15s;
  text-decoration: none;
}

.btn:disabled { opacity: .5; cursor: not-allowed; }

.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-dark); }

.btn-light { background: #fff; color: var(--text); border-color: var(--border); }
.btn-light:hover:not(:disabled) { background: var(--bg); }

.btn-ghost { background: transparent; color: var(--text-muted); border-color: var(--border); }
.btn-ghost:hover { background: var(--bg); color: var(--text); }

.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #b91c1c; }

.btn-sm { padding: 6px 12px; font-size: 12.5px; }

.btn-block { width: 100%; }

/* ---------- Forms ---------- */
.form-row { display: flex; gap: 14px; align-items: flex-end; flex-wrap: wrap; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px 20px; }
.form-group { display: flex; flex-direction: column; gap: 6px; }
.form-group.span-2 { grid-column: span 2; }

.form-group label { font-size: 13px; font-weight: 600; }
.form-group .hint { font-weight: 400; color: var(--text-muted); font-size: 12px; }

.input {
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  background: #fff;
  color: var(--text);
  width: 100%;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}

.input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(91,91,214,.12); }

.input-sm { padding: 6px 10px; font-size: 13px; width: auto; }

.textarea { resize: vertical; min-height: 120px; line-height: 1.6; }

.input-file { font-size: 13px; }

.select-inline { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text-muted); }

.char-count { font-size: 12px; color: var(--text-muted); text-align: right; }

.form-actions { display: flex; gap: 10px; margin-top: 18px; }

.form-note { font-size: 12.5px; color: var(--text-muted); margin-top: 12px; }

.form-compact .form-group { max-width: 260px; min-width: 180px; }
.form-compact .form-actions { margin: 0; }

/* ---------- Target selector ---------- */
.target-selector { display: flex; flex-wrap: wrap; gap: 10px; }

.target-chip {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 13px; font-weight: 500;
  cursor: pointer;
  transition: all .15s;
  user-select: none;
  background: #fff;
}

.target-chip:hover { border-color: var(--primary); }

.target-chip.selected {
  border-color: var(--primary);
  background: rgba(91,91,214,.08);
  box-shadow: 0 0 0 1px var(--primary);
}

.target-chip .platform-dot { width: 9px; height: 9px; }

/* ---------- Accounts ---------- */
.accounts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 14px;
}

.account-card {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: #fff;
  transition: transform .15s, box-shadow .15s;
}

.account-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }

.account-card-header { display: flex; align-items: center; gap: 12px; }

.account-avatar {
  width: 44px; height: 44px;
  border-radius: 10px;
  display: grid; place-items: center;
  color: #fff;
  font-weight: 700; font-size: 18px;
  text-transform: uppercase;
}

.account-name { font-weight: 600; font-size: 14.5px; line-height: 1.3; }
.account-handle { font-size: 12px; color: var(--text-muted); }

.account-metrics {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px;
  background: var(--bg);
  border-radius: 8px;
  padding: 10px;
}

.account-metric { text-align: center; }
.account-metric .am-value { font-weight: 700; font-size: 14px; }
.account-metric .am-label { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .4px; }

.account-card-actions { display: flex; gap: 8px; }

.accounts-viz { display: flex; flex-direction: column; gap: 14px; }

.account-bars {
  display: flex; align-items: center; gap: 12px;
}

.account-bars .ab-track { flex: 1; height: 10px; background: var(--bg); border-radius: 999px; overflow: hidden; }
.account-bars .ab-fill { height: 100%; border-radius: 999px; transition: width .5s ease; }
.account-bars .ab-value { font-size: 12.5px; font-weight: 700; min-width: 56px; text-align: right; }
.account-bars .ab-name { font-size: 12.5px; color: var(--text-muted); min-width: 90px; }

/* ---------- Table ---------- */
.table-wrap { overflow-x: auto; }

.table { width: 100%; border-collapse: collapse; }

.table th, .table td {
  padding: 11px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
  white-space: nowrap;
}

.table th { font-size: 12px; text-transform: uppercase; letter-spacing: .5px; color: var(--text-muted); font-weight: 600; }

.table tbody tr:last-child td { border-bottom: none; }

.table tbody tr:hover { background: rgba(91,91,214,.03); }

.table-empty { text-align: center; color: var(--text-muted); padding: 28px !important; }

.post-content-cell { max-width: 340px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--text-muted); }

/* ---------- Badges ---------- */
.badge {
  display: inline-flex; align-items: center;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px; font-weight: 600;
}

.badge-neutral { background: var(--bg); color: var(--text-muted); }

.badge-status { text-transform: capitalize; }

.badge-status.published { background: rgba(22,163,74,.12); color: var(--success); }
.badge-status.draft { background: rgba(107,114,128,.12); color: var(--text-muted); }
.badge-status.scheduled { background: rgba(217,119,6,.14); color: var(--warning); }
.badge-status.publishing { background: rgba(91,91,214,.12); color: var(--primary); }
.badge-status.failed { background: rgba(220,38,38,.12); color: var(--danger); }

.badge-publish { cursor: pointer; transition: background .15s; }
.badge-publish.published:hover { background: rgba(220,38,38,.12); }

/* ---------- Segmented ---------- */
.segmented { display: inline-flex; background: var(--bg); border-radius: 10px; padding: 4px; gap: 2px; }

.seg-btn {
  border: none; background: transparent;
  padding: 7px 14px;
  border-radius: 8px;
  font-weight: 600; font-size: 13px;
  color: var(--text-muted);
  cursor: pointer;
  transition: all .15s;
}

.seg-btn:hover { color: var(--text); }
.seg-btn.active { background: #fff; color: var(--text); box-shadow: var(--shadow); }

/* ---------- Toolbar ---------- */
.toolbar-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; gap: 12px; flex-wrap: wrap; }

/* ---------- Media preview ---------- */
.media-preview { margin-top: 10px; }
.media-preview img, .media-preview video { max-width: 220px; max-height: 160px; border-radius: 8px; border: 1px solid var(--border); }

/* ---------- Modal ---------- */
.modal-backdrop { display: none; }
.modal-backdrop:not([hidden]) {
  position: fixed; inset: 0;
  background: rgba(10,12,20,.5);
  display: grid; place-items: center;
  z-index: 100;
  padding: 20px;
}

.modal {
  background: #fff;
  border-radius: 14px;
  width: 100%; max-width: 520px;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
}

.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 18px 20px 10px; }
.modal-header h3 { font-size: 16px; }

.modal-close { background: none; border: none; font-size: 22px; cursor: pointer; color: var(--text-muted); line-height: 1; }

.modal-body { padding: 10px 20px 18px; }
.modal-footer { display: flex; justify-content: flex-end; padding: 12px 20px 18px; }

/* ---------- Toasts ---------- */
.alert {
  background: #fff8e6;
  border: 1px solid #f5d28a;
  border-radius: 10px;
  padding: 14px 16px;
  font-size: 13.5px;
  line-height: 1.5;
  max-width: 640px;
}
.alert a { color: var(--accent, #2563eb); }
.alert code { background: rgba(0,0,0,.05); }

.toast-container {
  position: fixed; bottom: 24px; right: 24px;
  display: flex; flex-direction: column; gap: 10px;
  z-index: 200;
}

.toast {
  background: #1a1d29;
  color: #fff;
  padding: 12px 18px;
  border-radius: 10px;
  font-size: 13.5px;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  display: flex; align-items: center; gap: 10px;
  animation: slideUp .25s ease;
  max-width: 360px;
}

.toast.success::before { content: "✓"; color: #4ade80; font-weight: 700; }
.toast.error::before { content: "✕"; color: #f87171; font-weight: 700; }
.toast.info::before { content: "ℹ"; color: #93c5fd; font-weight: 700; }

@keyframes slideUp { from { transform: translateY(12px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

/* ---------- Misc ---------- */
.empty-state { grid-column: 1 / -1; text-align: center; color: var(--text-muted); padding: 30px; }

code {
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  font-size: 12.5px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 5px;
}

.settings-form { max-width: 560px; }
.settings-form .form-group { max-width: 100%; }

.detail-loading { text-align: center; color: var(--text-muted); padding: 40px; }

.error-card { text-align: center; padding: 50px 30px; }
.error-title { font-size: 20px; margin-bottom: 10px; }
.error-message { color: var(--text-muted); margin-bottom: 20px; }

.platform-badge { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; font-size: 13px; }
.platform-badge .platform-dot { width: 9px; height: 9px; }

.link-btn-row { display: flex; gap: 8px; flex-wrap: wrap; }

@media (max-width: 900px) {
  .sidebar { width: 64px; }
  .sidebar-brand .brand-name, .nav-link span:not(.nav-icon), .nav-section-label, .sidebar-footer { display: none; }
  .nav-link { justify-content: center; }
  .content { padding: 0 16px 30px; }
  .topbar { padding: 18px 16px 14px; }
  .form-grid { grid-template-columns: 1fr; }
  .form-group.span-2 { grid-column: span 1; }
}
/* ---------- Login ---------- */
.login-body { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.login-wrap { width: 100%; max-width: 380px; }
.login-brand { display: flex; align-items: center; justify-content: center; gap: 10px; margin-bottom: 20px; }
.login-brand-name { font-weight: 700; font-size: 20px; letter-spacing: .2px; color: var(--text); }
.login-card { padding: 28px; }
.login-card .card-title { font-size: 18px; margin-bottom: 4px; }
.login-card .form-note { margin: 0 0 18px; }
.login-form { display: flex; flex-direction: column; gap: 14px; }
.login-form .btn { margin-top: 6px; }
.login-error {
  margin-bottom: 14px; padding: 10px 12px; border-radius: 8px;
  background: #fef2f2; border: 1px solid #fecaca; color: var(--danger); font-size: 13px;
}

/* ---------- Signed-in user (topbar) ---------- */
.topbar-user { display: flex; align-items: center; gap: 10px; margin-left: 12px; }
.topbar-username { color: var(--text-muted); font-size: 13px; }
.topbar-user form { margin: 0; }

/* ---------- AI post creation ---------- */
.ai-card { border: 1px solid #ddd6fe; background: linear-gradient(180deg, #faf5ff 0%, var(--bg-card) 120px); }
.ai-card .textarea { min-height: 80px; }
.ai-actions { margin-top: 0; grid-column: span 2; }
.ai-image-row { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-top: 10px; }
.ai-image-selected { outline: 3px solid var(--primary); outline-offset: 2px; }
.media-preview .ai-preview-note { font-size: 12px; color: var(--text-muted); margin-top: 6px; }
@media (max-width: 900px) { .ai-actions { grid-column: span 1; } }
.settings-section-title { font-size: 18px; font-weight: 700; margin: 28px 0 6px; scroll-margin-top: 16px; }
.badge-status.awaiting_approval { background: rgba(217,119,6,.14); color: var(--warning); }
.badge-status.rejected, .badge-status.expired { background: rgba(107,114,128,.12); color: var(--text-muted); }
.badge-status.partial { background: rgba(217,119,6,.14); color: var(--warning); }

/* ---------- Post manager ---------- */
.planner-switch { display: flex; align-items: center; gap: 10px; font-weight: 600; cursor: pointer; }
.planner-switch input { width: 18px; height: 18px; }
.planner-autonomy { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.autonomy-option { display: flex; flex-direction: column; gap: 4px; padding: 12px 14px; border: 1px solid var(--border); border-radius: 10px; cursor: pointer; font-weight: 400; }
.autonomy-option input { margin-bottom: 4px; }
.autonomy-option span { font-size: 12.5px; color: var(--text-muted); }
.autonomy-option:has(input:checked) { border-color: var(--primary); background: rgba(91,91,214,.05); }
.weekday-picker, .account-picker { display: flex; flex-wrap: wrap; gap: 8px; }
.weekday-picker label, .account-picker label { display: flex; align-items: center; gap: 6px; padding: 6px 12px; border: 1px solid var(--border); border-radius: 999px; font-weight: 500; cursor: pointer; }
.weekday-picker label:has(input:checked), .account-picker label:has(input:checked) { border-color: var(--primary); background: rgba(91,91,214,.06); }
.account-picker label.disabled { opacity: .5; cursor: not-allowed; }
.slot-form { margin-top: 18px; padding-top: 18px; border-top: 1px dashed var(--border); }
.slot-row, .queue-item { display: flex; align-items: flex-start; gap: 14px; padding: 12px 0; border-bottom: 1px solid var(--border); }
.slot-row:last-child, .queue-item:last-child { border-bottom: none; }
.slot-row .slot-main, .queue-item .queue-main { flex: 1; min-width: 0; }
.slot-time { font-size: 20px; font-weight: 700; min-width: 64px; }
.queue-when { min-width: 120px; font-weight: 600; }
.queue-when .hint { display: block; font-weight: 400; }
.queue-content { white-space: pre-wrap; line-height: 1.5; margin: 6px 0; max-height: 7.5em; overflow: hidden; }
.queue-content.expanded { max-height: none; }
.queue-media img { width: 96px; height: 96px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); }
.queue-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.queue-error { color: var(--danger); font-size: 13px; margin-top: 4px; }
@media (max-width: 900px) { .planner-autonomy { grid-template-columns: 1fr; } .slot-row, .queue-item { flex-wrap: wrap; } }
.playbook-auto { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-top: 14px; font-size: 13.5px; }
.playbook-auto .planner-switch { font-weight: 500; }
.playbook-history { margin-top: 16px; }
.playbook-history summary { cursor: pointer; font-weight: 600; font-size: 13.5px; }
.history-row { display: flex; align-items: flex-start; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.history-row:last-child { border-bottom: none; }
.history-row .history-preview { flex: 1; min-width: 0; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.learning-title { font-size: 14px; font-weight: 700; margin: 18px 0 8px; }
.learning-title:first-of-type { margin-top: 0; }
.exp-status { font-weight: 600; }
.exp-status.better { color: var(--success); }
.exp-status.worse { color: var(--danger); }
.exp-status.similar, .exp-status.collecting { color: var(--text-muted); }
.time-suggestion { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.time-suggestion:last-child { border-bottom: none; }
.time-suggestion .ts-main { flex: 1; }
