:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-2: #f1f5f9;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-muted: #64748b;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-soft: rgba(79, 70, 229, 0.1);
  --accent: #0ea5e9;
  --success: #10b981;
  --success-soft: rgba(16, 185, 129, 0.12);
  --warning: #f59e0b;
  --warning-soft: rgba(245, 158, 11, 0.14);
  --danger: #ef4444;
  --danger-soft: rgba(239, 68, 68, 0.12);
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.06), 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-lg: 0 18px 40px rgba(15, 23, 42, 0.14);
  --sidebar-w: 256px;
  --topbar-h: 64px;
  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

body.dark-mode {
  --bg: #0b1120;
  --surface: #111827;
  --surface-2: #1e293b;
  --border: #273449;
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --primary: #6366f1;
  --primary-hover: #818cf8;
  --primary-soft: rgba(99, 102, 241, 0.18);
  --shadow: 0 1px 4px rgba(0, 0, 0, 0.35);
  --shadow-lg: 0 24px 50px rgba(0, 0, 0, 0.55);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; font-family: var(--font); background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.5; min-height: 100vh; }
a { color: var(--primary); text-decoration: none; }
button, input, select, textarea { font-family: inherit; font-size: 14px; color: var(--text); }

/* Layout Shell */
.app-shell { display: flex; min-height: 100vh; }
.sidebar {
  width: var(--sidebar-w); background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; position: fixed; top: 0; bottom: 0; left: 0; z-index: 40;
  transition: width 0.2s ease, transform 0.2s ease;
}
.app-shell.sidebar-collapsed .sidebar { width: 74px; }
.app-shell.sidebar-collapsed .sidebar .nav-label,
.app-shell.sidebar-collapsed .sidebar .brand-text,
.app-shell.sidebar-collapsed .sidebar .nav-section-title,
.app-shell.sidebar-collapsed .sidebar .sidebar-quota { display: none; }
.app-shell.sidebar-collapsed .main-area { margin-left: 74px; }

.sidebar-header {
  height: var(--topbar-h); display: flex; align-items: center; gap: 10px;
  padding: 0 18px; border-bottom: 1px solid var(--border);
}
.brand-mark {
  width: 34px; height: 34px; border-radius: 10px; flex-shrink: 0;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  display: flex; align-items: center; justify-content: center; color: #fff; font-weight: 800; font-size: 16px;
}
.brand-text strong { display: block; font-size: 15px; letter-spacing: -0.02em; }
.brand-text span { font-size: 11px; color: var(--text-muted); display: block; }

.sidebar-nav { flex: 1; overflow-y: auto; padding: 12px 10px; }
.nav-section-title { font-size: 10.5px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.08em; color: var(--text-muted); padding: 12px 10px 6px; }
.nav-item {
  display: flex; align-items: center; gap: 11px; padding: 9px 12px; border-radius: var(--radius-sm);
  color: var(--text-muted); font-weight: 500; cursor: pointer; transition: all 0.15s ease; margin-bottom: 2px; border: none; background: transparent; width: 100%; text-align: left;
}
.nav-item:hover { background: var(--surface-2); color: var(--text); }
.nav-item.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.nav-icon { width: 18px; height: 18px; flex-shrink: 0; display: inline-flex; align-items: center; justify-content: center; }

.sidebar-quota { padding: 14px; border-top: 1px solid var(--border); background: var(--surface-2); margin: 10px; border-radius: var(--radius-sm); }
.progress-track { height: 6px; border-radius: 99px; background: var(--border); overflow: hidden; margin-top: 6px; }
.progress-fill { height: 100%; background: linear-gradient(90deg, var(--primary), var(--accent)); border-radius: 99px; transition: width 0.3s ease; }

/* Main Area & Topbar */
.main-area { flex: 1; margin-left: var(--sidebar-w); display: flex; flex-direction: column; min-width: 0; transition: margin-left 0.2s ease; }
.topbar {
  height: var(--topbar-h); background: var(--surface); border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between; padding: 0 22px; gap: 14px;
  position: sticky; top: 0; z-index: 30;
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 10px; }
.search-box { position: relative; width: 290px; max-width: 34vw; }
.search-box input {
  width: 100%; padding: 8px 12px 8px 34px; border-radius: 99px; border: 1px solid var(--border);
  background: var(--surface-2); outline: none; transition: border-color 0.15s;
}
.search-box input:focus { border-color: var(--primary); background: var(--surface); }
.search-box .search-ico { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); color: var(--text-muted); pointer-events: none; }
.search-dropdown {
  position: absolute; top: calc(100% + 6px); left: 0; right: 0; min-width: 340px; max-height: 420px; overflow-y: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-lg); padding: 10px; z-index: 60;
}
.ws-select { padding: 6px 10px; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--surface-2); font-weight: 500; max-width: 175px; }
.icon-btn {
  width: 36px; height: 36px; border-radius: var(--radius-sm); border: 1px solid var(--border);
  background: var(--surface); display: inline-flex; align-items: center; justify-content: center; cursor: pointer; position: relative;
}
.icon-btn:hover { background: var(--surface-2); }
.badge-dot { position: absolute; top: -4px; right: -4px; min-width: 17px; height: 17px; padding: 0 4px; border-radius: 99px; background: var(--danger); color: #fff; font-size: 10px; font-weight: 700; display: flex; align-items: center; justify-content: center; }

.page-content { padding: 24px; max-width: 1520px; width: 100%; margin: 0 auto; }
.page-header { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 20px; }
.page-header h1 { margin: 0; font-size: 22px; font-weight: 700; letter-spacing: -0.02em; }
.page-header p { margin: 2px 0 0; color: var(--text-muted); font-size: 13px; }

/* Cards, Grids, Tables */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.card-header { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 14px; }
.card-header h3 { margin: 0; font-size: 15px; font-weight: 600; }
.grid { display: grid; gap: 16px; }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-sidebar { grid-template-columns: 280px minmax(0, 1fr); }

.kpi-card { display: flex; flex-direction: column; gap: 6px; }
.kpi-label { font-size: 12px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; }
.kpi-value { font-size: 26px; font-weight: 800; letter-spacing: -0.03em; }
.kpi-sub { font-size: 12px; color: var(--text-muted); }

/* Buttons & Forms */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px; padding: 8px 14px;
  border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--surface);
  font-weight: 600; cursor: pointer; transition: all 0.15s ease; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-success { background: var(--success); color: #fff; border-color: var(--success); }
.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-sm { padding: 5px 10px; font-size: 12.5px; }
.btn-ghost { background: transparent; border-color: transparent; }

.form-grid { display: grid; gap: 12px; }
.form-row-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.form-row-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
.field label { display: block; font-size: 12px; font-weight: 600; color: var(--text-muted); margin-bottom: 5px; }
.field input, .field select, .field textarea {
  width: 100%; padding: 9px 11px; border-radius: var(--radius-sm); border: 1px solid var(--border);
  background: var(--bg); outline: none; transition: border-color 0.15s;
}
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--primary); background: var(--surface); }
.field textarea { min-height: 92px; resize: vertical; }

/* Pills, Badges & Alerts */
.pill { display: inline-flex; align-items: center; gap: 5px; padding: 2px 9px; border-radius: 99px; font-size: 11.5px; font-weight: 600; background: var(--surface-2); color: var(--text-muted); }
.pill-primary { background: var(--primary-soft); color: var(--primary); }
.pill-success { background: var(--success-soft); color: var(--success); }
.pill-warning { background: var(--warning-soft); color: var(--warning); }
.pill-danger { background: var(--danger-soft); color: var(--danger); }
.alert { padding: 11px 14px; border-radius: var(--radius-sm); font-size: 13px; border: 1px solid var(--border); background: var(--surface-2); margin-bottom: 14px; }
.alert-warning { background: var(--warning-soft); border-color: rgba(245, 158, 11, 0.35); color: var(--text); }
.alert-info { background: var(--primary-soft); border-color: rgba(79, 70, 229, 0.3); color: var(--text); }

/* Tabs */
.tabs { display: flex; flex-wrap: wrap; gap: 6px; border-bottom: 1px solid var(--border); padding-bottom: 10px; margin-bottom: 16px; }
.tab-btn { padding: 7px 13px; border-radius: var(--radius-sm); border: 1px solid transparent; background: transparent; font-weight: 600; color: var(--text-muted); cursor: pointer; }
.tab-btn:hover { background: var(--surface-2); color: var(--text); }
.tab-btn.active { background: var(--primary-soft); color: var(--primary); border-color: rgba(79, 70, 229, 0.25); }

/* Table */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em; color: var(--text-muted); background: var(--surface-2); }
tr:hover td { background: rgba(148, 163, 184, 0.05); }

/* Calendar */
.cal-grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 6px; }
.cal-day-head { font-size: 11px; font-weight: 700; text-transform: uppercase; color: var(--text-muted); padding: 6px; text-align: center; }
.cal-cell {
  min-height: 114px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 6px; display: flex; flex-direction: column; gap: 4px; transition: border-color 0.15s, background 0.15s;
}
.cal-cell.drag-over { border-color: var(--primary); background: var(--primary-soft); }
.cal-cell.other-month { opacity: 0.45; }
.cal-date-num { font-size: 11.5px; font-weight: 700; color: var(--text-muted); display: flex; justify-content: space-between; align-items: center; }
.cal-event {
  font-size: 11.5px; padding: 4px 6px; border-radius: 6px; background: var(--primary-soft); color: var(--text);
  border-left: 3px solid var(--primary); cursor: grab; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.cal-event.status-published { border-left-color: var(--success); background: var(--success-soft); }
.cal-event.status-failed { border-left-color: var(--danger); background: var(--danger-soft); }
.cal-event.status-draft { border-left-color: var(--warning); background: var(--warning-soft); }

/* Media Grid & Canvas Editor */
.media-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(195px, 1fr)); gap: 14px; }
.media-card { border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; background: var(--surface); display: flex; flex-direction: column; }
.media-thumb { height: 140px; background: var(--surface-2); display: flex; align-items: center; justify-content: center; overflow: hidden; }
.media-thumb img { width: 100%; height: 100%; object-fit: cover; }
.media-meta { padding: 10px; display: flex; flex-direction: column; gap: 6px; }
.canvas-stage { background: repeating-conic-gradient(#cbd5e1 0% 25%, #f8fafc 0% 50%) 50% / 16px 16px; border-radius: var(--radius-sm); padding: 12px; display: flex; justify-content: center; align-items: center; overflow: auto; max-height: 460px; }
.canvas-stage canvas { max-width: 100%; height: auto; box-shadow: var(--shadow-lg); }

/* Chat / AI Studio */
.chat-log { height: 420px; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; padding: 14px; background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius-sm); }
.chat-msg { max-width: 82%; padding: 11px 14px; border-radius: 12px; white-space: pre-wrap; word-break: break-word; }
.chat-msg.user { align-self: flex-end; background: var(--primary); color: #fff; }
.chat-msg.assistant { align-self: flex-start; background: var(--surface); border: 1px solid var(--border); }

/* Auth Screen */
.auth-screen { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px; background: radial-gradient(circle at top right, rgba(79,70,229,0.16), transparent 50%), var(--bg); }
.auth-card { width: 100%; max-width: 450px; background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 30px; box-shadow: var(--shadow-lg); }

/* Modal & Toast */
.modal-backdrop { position: fixed; inset: 0; background: rgba(15, 23, 42, 0.6); z-index: 80; display: flex; align-items: center; justify-content: center; padding: 18px; }
.modal-box { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; width: 100%; max-width: 680px; max-height: 90vh; overflow-y: auto; padding: 22px; box-shadow: var(--shadow-lg); }
.modal-lg { max-width: 960px; }
#toast-container { position: fixed; right: 20px; bottom: 20px; z-index: 100; display: flex; flex-direction: column; gap: 8px; }
.toast { padding: 11px 16px; border-radius: 10px; background: #0f172a; color: #fff; font-weight: 500; box-shadow: var(--shadow-lg); max-width: 380px; animation: fadeUp 0.2s ease; }
.toast.success { background: #059669; }
.toast.error { background: #dc2626; }
@keyframes fadeUp { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

.notif-popover { position: absolute; right: 0; top: calc(100% + 8px); width: 350px; max-height: 420px; overflow-y: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-lg); z-index: 65; padding: 12px; }
.muted { color: var(--text-muted); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12.5px; }
.flex { display: flex; align-items: center; gap: 8px; }
.flex-between { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.wrap { flex-wrap: wrap; }
.mt-12 { margin-top: 12px; }
.mt-16 { margin-top: 16px; }
.mb-12 { margin-bottom: 12px; }

@media (max-width: 1100px) {
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-sidebar { grid-template-columns: 1fr; }
}
@media (max-width: 820px) {
  .sidebar { transform: translateX(-100%); }
  .app-shell.mobile-open .sidebar { transform: translateX(0); }
  .main-area { margin-left: 0 !important; }
  .grid-4, .grid-3, .grid-2, .form-row-2, .form-row-3 { grid-template-columns: 1fr; }
  .search-box { display: none; }
  .cal-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
