/* ---------- Design tokens ---------- */
:root {
  --bg: #f5f6fa;
  --surface: #ffffff;
  --surface-muted: #f9fafb;
  --border: #e5e7eb;
  --text: #111827;
  --text-muted: #6b7280;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-soft: #eef2ff;
  --success: #059669;
  --success-soft: #ecfdf5;
  --warning: #b45309;
  --warning-soft: #fffbeb;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgb(16 24 40 / 0.05), 0 1px 3px rgb(16 24 40 / 0.06);
  --shadow-lg: 0 12px 32px rgb(16 24 40 / 0.12);
  --sidebar-width: 240px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  line-height: 1.5;
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); font-size: 15px; }
h1, h2, h3, p { margin: 0; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }
[hidden] { display: none !important; }

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

.sidebar {
  position: fixed; inset: 0 auto 0 0; z-index: 30;
  width: var(--sidebar-width);
  display: flex; flex-direction: column; gap: 24px;
  padding: 20px 14px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  transition: transform 0.2s ease;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 0 8px; font-weight: 700; font-size: 18px; color: var(--text); }
.brand:hover { text-decoration: none; }
.brand-mark {
  display: grid; place-items: center; width: 32px; height: 32px;
  border-radius: 9px; background: var(--primary); color: #fff; font-size: 16px;
}
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav a {
  display: flex; align-items: center; gap: 12px;
  padding: 9px 12px; border-radius: var(--radius-sm);
  color: var(--text-muted); font-weight: 500;
}
.nav a:hover { background: var(--surface-muted); color: var(--text); text-decoration: none; }
.nav a.active { background: var(--primary-soft); color: var(--primary); }
.sidebar-footer { margin-top: auto; padding: 0 8px; display: flex; flex-direction: column; align-items: flex-start; gap: 12px; font-size: 12px; color: var(--text-muted); }

.main { flex: 1; min-width: 0; margin-left: var(--sidebar-width); }
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px;
  height: 64px; padding: 0 32px;
  background: rgb(255 255 255 / 0.9); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.page-title { font-size: 18px; font-weight: 600; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.view { max-width: 1100px; margin: 0 auto; padding: 32px; display: flex; flex-direction: column; gap: 24px; }

.topbar .menu-btn { display: none; }
.backdrop { display: none; }

@media (max-width: 860px) {
  .sidebar { transform: translateX(-100%); box-shadow: var(--shadow-lg); }
  .sidebar.open { transform: none; }
  .backdrop.open { display: block; position: fixed; inset: 0; z-index: 20; background: rgb(17 24 39 / 0.4); }
  .main { margin-left: 0; }
  .topbar .menu-btn { display: inline-grid; }
  .topbar { padding: 0 16px; }
  .view { padding: 20px 16px; }
}

/* ---------- Building blocks ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-body { padding: 24px; }
.card-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 18px 24px; border-bottom: 1px solid var(--border); }
.card-header h2 { font-size: 16px; font-weight: 600; }

.section-header { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.section-header h2 { font-size: 20px; font-weight: 600; }
.muted { color: var(--text-muted); }
.small { font-size: 13px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 9px 16px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap;
  transition: background 0.15s, border-color 0.15s;
}
.btn:hover { background: var(--surface-muted); text-decoration: none; }
.btn:disabled { opacity: 0.6; cursor: not-allowed; }
.btn svg { width: 18px; height: 18px; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-danger { color: var(--danger); }
.btn-danger:hover { background: var(--danger-soft); border-color: #fecaca; }
.btn-sm { padding: 6px 12px; font-size: 13px; }
.icon-btn {
  display: inline-grid; place-items: center; width: 36px; height: 36px;
  border: 1px solid transparent; border-radius: var(--radius-sm);
  background: none; color: var(--text-muted); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-muted); color: var(--text); }
.icon-btn.danger:hover { background: var(--danger-soft); color: var(--danger); }

.badge { display: inline-flex; align-items: center; gap: 4px; padding: 2px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; background: var(--surface-muted); color: var(--text-muted); border: 1px solid var(--border); }
.badge-primary { background: var(--primary-soft); color: var(--primary); border-color: #c7d2fe; }
.badge-success { background: var(--success-soft); color: var(--success); border-color: #a7f3d0; }
.badge-warning { background: var(--warning-soft); color: var(--warning); border-color: #fde68a; }
.badge-danger { background: var(--danger-soft); color: var(--danger); border-color: #fecaca; }

/* ---------- Account chip ---------- */
a.account-chip { color: var(--text); }
a.account-chip:hover { text-decoration: none; background: var(--surface-muted); }
.account-chip { display: flex; align-items: center; gap: 8px; max-width: 240px; padding: 4px 12px 4px 4px; border: 1px solid var(--border); border-radius: 999px; font-size: 14px; font-weight: 500; }
.account-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (max-width: 600px) {
  .account-chip { padding: 3px; }
  .account-name { display: none; }
}
.avatar { display: grid; place-items: center; width: 28px; height: 28px; border-radius: 50%; background: linear-gradient(135deg, #f58529, #dd2a7b, #8134af); color: #fff; font-size: 13px; font-weight: 700; overflow: hidden; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }

/* ---------- Stats ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 16px; }
@media (max-width: 600px) {
  .stats { grid-template-columns: 1fr 1fr; gap: 12px; }
  .stat { padding: 16px; }
  .stat-value { font-size: 24px; }
}
.stat { padding: 20px; }
.stat-label { font-size: 13px; color: var(--text-muted); font-weight: 500; }
.stat-value { margin-top: 6px; font-size: 28px; font-weight: 700; letter-spacing: -0.02em; }

/* ---------- Forms ---------- */
.form { display: flex; flex-direction: column; gap: 28px; }
.form-step { display: flex; flex-direction: column; gap: 12px; }
.step-title { display: flex; align-items: center; gap: 10px; font-size: 15px; font-weight: 600; }
.step-number { display: grid; place-items: center; width: 24px; height: 24px; border-radius: 50%; background: var(--primary-soft); color: var(--primary); font-size: 13px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field-row { display: grid; grid-template-columns: 1fr 200px; gap: 12px; }
@media (max-width: 600px) { .field-row { grid-template-columns: 1fr; } }
label { font-size: 14px; font-weight: 500; }
.hint { font-size: 13px; color: var(--text-muted); }
input[type="text"], input[type="url"], textarea, select {
  width: 100%; padding: 10px 12px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text); font: inherit;
  transition: border-color 0.15s, box-shadow 0.15s;
}
textarea { min-height: 88px; resize: vertical; }
input:focus, textarea:focus, select:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.field.has-error input, .field.has-error textarea { border-color: var(--danger); }
.field-error { font-size: 13px; color: var(--danger); }
.field-error:empty { display: none; }
.form-actions { display: flex; justify-content: flex-end; gap: 12px; padding-top: 4px; }

.segmented { display: inline-flex; padding: 4px; gap: 4px; background: var(--surface-muted); border: 1px solid var(--border); border-radius: var(--radius-sm); width: fit-content; }
.segmented label { padding: 6px 14px; border-radius: 6px; cursor: pointer; color: var(--text-muted); }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented label:has(input:checked) { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.segmented label:has(input:focus-visible) { outline: 2px solid var(--primary); }

/* ---------- Media grid ---------- */
.media-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 12px; }
.media-tile {
  position: relative; aspect-ratio: 1; padding: 0; overflow: hidden;
  border: 2px solid transparent; border-radius: var(--radius-sm);
  background: var(--surface-muted); cursor: pointer; font: inherit; color: inherit; text-align: left;
}
.media-tile img { width: 100%; height: 100%; object-fit: cover; display: block; }
.media-tile:hover { border-color: #c7d2fe; }
.media-tile.selected { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.media-tile .media-type { position: absolute; top: 6px; right: 6px; display: grid; place-items: center; width: 24px; height: 24px; border-radius: 6px; background: rgb(0 0 0 / 0.55); color: #fff; }
.media-tile .media-type svg { width: 14px; height: 14px; }
.media-tile .check { position: absolute; top: 6px; left: 6px; display: none; place-items: center; width: 24px; height: 24px; border-radius: 50%; background: var(--primary); color: #fff; }
.media-tile .check svg { width: 14px; height: 14px; stroke-width: 3; }
.media-tile.selected .check { display: grid; }
.media-tile .caption { position: absolute; inset: auto 0 0 0; padding: 18px 8px 6px; font-size: 12px; color: #fff; background: linear-gradient(transparent, rgb(0 0 0 / 0.7)); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.media-tile.any-post { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; border: 2px dashed var(--border); text-align: center; color: var(--text-muted); font-size: 13px; font-weight: 500; padding: 8px; }
.media-tile.any-post.selected { border-style: solid; border-color: var(--primary); color: var(--primary); }

/* ---------- Automation list ---------- */
.flow-list { display: flex; flex-direction: column; }
.flow { display: grid; grid-template-columns: 56px 1fr auto; gap: 16px; align-items: center; padding: 16px 24px; border-bottom: 1px solid var(--border); }
.flow:last-child { border-bottom: none; }
.flow.inactive { opacity: 0.6; }
.flow-thumb { width: 56px; height: 56px; border-radius: var(--radius-sm); background: var(--surface-muted); border: 1px solid var(--border); overflow: hidden; display: grid; place-items: center; color: var(--text-muted); }
.flow-thumb img { width: 100%; height: 100%; object-fit: cover; }
.flow-main { min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.flow-title { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-weight: 600; }
.flow-detail strong { font-weight: 600; color: var(--text); margin-right: 4px; }
.flow-detail { font-size: 13px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.flow-actions { display: flex; align-items: center; gap: 8px; }
@media (max-width: 600px) {
  .flow { grid-template-columns: 44px 1fr; padding: 14px 16px; }
  .flow-thumb { width: 44px; height: 44px; }
  .flow-actions { grid-column: 1 / -1; justify-content: space-between; }
}

.switch { position: relative; display: inline-block; width: 40px; height: 22px; flex-shrink: 0; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; z-index: 1; }
.switch span { position: absolute; inset: 0; border-radius: 999px; background: #d1d5db; transition: background 0.15s; }
.switch span::after { content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; box-shadow: var(--shadow); transition: transform 0.15s; }
.switch input:checked + span { background: var(--primary); }
.switch input:checked + span::after { transform: translateX(18px); }
.switch input:focus-visible + span { outline: 2px solid var(--primary); outline-offset: 2px; }

/* ---------- Activity / tables ---------- */
.activity { display: flex; flex-direction: column; }
.activity-item { display: flex; gap: 12px; align-items: flex-start; padding: 14px 24px; border-bottom: 1px solid var(--border); }
.activity-item:last-child { border-bottom: none; }
.activity-item .text { flex: 1; min-width: 0; }
.activity-item .comment { font-size: 14px; overflow-wrap: anywhere; }
.activity-item .error { margin-top: 4px; font-size: 12px; color: var(--danger); white-space: pre-line; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { padding: 12px 24px; text-align: left; border-bottom: 1px solid var(--border); }
th { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); background: var(--surface-muted); }
tr:last-child td { border-bottom: none; }

/* ---------- Templates / misc ---------- */
.template-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 16px; }
.template { display: flex; flex-direction: column; gap: 10px; padding: 20px; }
.template h3 { font-size: 16px; }
.template .btn { margin-top: auto; align-self: flex-start; }
.preview-bubble { padding: 10px 12px; border-radius: var(--radius-sm); background: var(--surface-muted); font-size: 13px; color: var(--text-muted); white-space: pre-line; }

.empty { display: flex; flex-direction: column; align-items: center; gap: 8px; padding: 48px 24px; text-align: center; }
.empty .empty-icon { display: grid; place-items: center; width: 48px; height: 48px; border-radius: 50%; background: var(--primary-soft); color: var(--primary); margin-bottom: 4px; }
.empty h3 { font-size: 16px; }
.empty p { max-width: 420px; color: var(--text-muted); font-size: 14px; }
.empty .btn { margin-top: 8px; }

.notice { display: flex; gap: 12px; padding: 14px 16px; border-radius: var(--radius-sm); font-size: 14px; background: var(--primary-soft); color: #3730a3; }
.notice.warning { background: var(--warning-soft); color: var(--warning); }
.notice.danger { background: var(--danger-soft); color: var(--danger); }

.prose { display: flex; flex-direction: column; gap: 12px; font-size: 14px; }
.prose h3 { font-size: 15px; margin-top: 8px; }
.prose ol, .prose ul { margin: 0; padding-left: 20px; display: flex; flex-direction: column; gap: 6px; }
.prose code { padding: 1px 6px; border-radius: 4px; background: var(--surface-muted); border: 1px solid var(--border); font-size: 13px; overflow-wrap: anywhere; }

.skeleton { border-radius: var(--radius-sm); background: linear-gradient(90deg, #eef0f4 25%, #f6f7fa 50%, #eef0f4 75%); background-size: 200% 100%; animation: shimmer 1.2s infinite; }
@keyframes shimmer { to { background-position: -200% 0; } }

/* ---------- Login ---------- */
.login-screen { position: fixed; inset: 0; z-index: 40; display: grid; place-items: center; padding: 16px; background: var(--bg); }
.login-card { width: 100%; max-width: 360px; padding: 32px; display: flex; flex-direction: column; gap: 20px; }
.login-card .brand { padding: 0; }
.login-title { font-size: 20px; font-weight: 600; }
.login-switch { text-align: center; }
.link-btn { padding: 0; border: none; background: none; color: var(--primary); font: inherit; font-weight: 500; cursor: pointer; }
.link-btn:hover { text-decoration: underline; }
.user-email { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; color: var(--text); }
input[type="email"], input[type="password"] { width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface); color: var(--text); font: inherit; }

/* ---------- Toasts ---------- */
.toasts { position: fixed; right: 16px; bottom: 16px; z-index: 50; display: flex; flex-direction: column; gap: 8px; max-width: calc(100vw - 32px); }
.toast { padding: 12px 16px; border-radius: var(--radius-sm); background: var(--text); color: #fff; font-size: 14px; box-shadow: var(--shadow-lg); animation: toast-in 0.2s ease; }
.toast.error { background: var(--danger); }
@keyframes toast-in { from { transform: translateY(8px); opacity: 0; } }
