:root {
  color-scheme: light;
  --brand: #4f46e5;
  --brand-dark: #3730a3;
  --brand-light: #eef2ff;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-2: #64748b;
  --text-3: #94a3b8;
  --success: #16a34a;
  --warning: #d97706;
  --danger: #dc2626;
  --info: #0284c7;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,.1), 0 1px 2px rgba(0,0,0,.06);
  --shadow-md: 0 4px 6px rgba(0,0,0,.07), 0 2px 4px rgba(0,0,0,.06);
}

/* Dark theme - toggled via [data-theme="dark"] on <html> by /js/theme.js.
   Same token names, different values - every component that uses the tokens
   above (instead of hardcoded colors) gets dark mode for free. */
[data-theme="dark"] {
  color-scheme: dark;
  --brand: #818cf8;
  --brand-dark: #6366f1;
  --brand-light: #1e1b4b;
  --surface: #1e293b;
  --surface-2: #0f172a;
  --border: #334155;
  --text: #f1f5f9;
  --text-2: #94a3b8;
  --text-3: #64748b;
  --shadow: 0 1px 3px rgba(0,0,0,.4), 0 1px 2px rgba(0,0,0,.3);
  --shadow-md: 0 4px 6px rgba(0,0,0,.35), 0 2px 4px rgba(0,0,0,.3);
}
[data-theme="dark"] .sidebar { background: #0b1220; }

[data-theme="dark"] .btn-primary { background: var(--surface); color: var(--text); border: 1px solid var(--brand); }
[data-theme="dark"] .btn-primary:hover { background: var(--border); opacity: 1; }

.theme-toggle {
  display: inline-flex; align-items: center; justify-content: center;
  width: 34px; height: 34px; border-radius: 8px; font-size: 16px; line-height: 1;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  cursor: pointer; flex-shrink: 0;
}
.theme-toggle:hover { border-color: var(--brand); }

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; font-size: 14px; color: var(--text); background: var(--surface-2); line-height: 1.5; }
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
input, textarea, select { font-family: inherit; font-size: 14px; }

/* ── Layout ── */
.layout { display: flex; height: 100vh; overflow: hidden; }
.sidebar { width: 220px; flex-shrink: 0; background: var(--text); color: #fff; display: flex; flex-direction: column; }
.main { flex: 1; overflow-y: auto; display: flex; flex-direction: column; }

/* ── Sidebar ── */
.sidebar-logo { padding: 20px 16px; font-size: 20px; font-weight: 700; color: #fff; letter-spacing: -.5px; border-bottom: 1px solid rgba(255,255,255,.1); }
.sidebar-logo span[data-brand="rest"] { color: var(--brand); }
.sidebar-nav { flex: 1; padding: 12px 0; }
.nav-item { display: flex; align-items: center; gap: 10px; padding: 10px 16px; color: rgba(255,255,255,.7); cursor: pointer; border-radius: 0; transition: background .15s, color .15s; text-decoration: none; font-size: 14px; }
.nav-item:hover { background: rgba(255,255,255,.08); color: #fff; text-decoration: none; }
.nav-item.active { background: var(--brand); color: #fff; }
.nav-item svg { width: 18px; height: 18px; flex-shrink: 0; }
.nav-section { padding: 16px 16px 6px; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .08em; color: rgba(255,255,255,.35); }
.sidebar-footer { padding: 16px; border-top: 1px solid rgba(255,255,255,.1); }
.user-pill { display: flex; align-items: center; gap: 8px; color: rgba(255,255,255,.7); font-size: 13px; }
.avatar { width: 32px; height: 32px; border-radius: 50%; background: var(--brand); display: flex; align-items: center; justify-content: center; color: #fff; font-weight: 600; font-size: 13px; flex-shrink: 0; }

/* ── Top bar ── */
.topbar { height: 56px; background: var(--surface); border-bottom: 1px solid var(--border); display: flex; align-items: center; padding: 0 24px; gap: 12px; flex-shrink: 0; }
.topbar h1 { font-size: 18px; font-weight: 600; flex: 1; }

/* ── Cards / Stats ── */
.page { padding: 24px; }
.stats-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 16px; margin-bottom: 24px; }
.stat-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); }
.stat-card .label { font-size: 12px; color: var(--text-2); font-weight: 500; text-transform: uppercase; letter-spacing: .05em; }
.stat-card .value { font-size: 32px; font-weight: 700; margin-top: 4px; }
.stat-card.open .value { color: var(--brand); }
.stat-card.pending .value { color: var(--warning); }
.stat-card.resolved .value { color: var(--success); }
.stat-card.unassigned .value { color: var(--danger); }

/* ── Buttons ── */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 8px 16px; border-radius: var(--radius); border: none; font-size: 14px; font-weight: 500; cursor: pointer; transition: opacity .15s, box-shadow .15s; }
.btn:hover { opacity: .9; }
.btn-primary { background: var(--brand); color: #fff; }
.btn-secondary { background: var(--surface); color: var(--text); border: 1px solid var(--border); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-sm { padding: 5px 10px; font-size: 13px; }

/* ── Forms ── */
.form-group { margin-bottom: 16px; }
.form-label { display: block; font-size: 13px; font-weight: 500; margin-bottom: 6px; color: var(--text); }
.form-input { display: block; width: 100%; padding: 9px 12px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text); transition: border-color .15s, box-shadow .15s; }
.form-input:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px rgba(79,70,229,.15); }
.form-error { font-size: 12px; color: var(--danger); margin-top: 4px; }

/* ── Auth page ── */
.auth-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: var(--surface-2); }
.auth-card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 40px; width: 100%; max-width: 420px; box-shadow: var(--shadow-md); }
.auth-logo { font-size: 24px; font-weight: 700; color: var(--text); margin-bottom: 24px; text-align: center; }
.auth-logo span[data-brand="rest"] { color: var(--brand); }
.auth-divider { text-align: center; color: var(--text-3); font-size: 13px; margin: 16px 0; }
.auth-footer { text-align: center; margin-top: 20px; color: var(--text-2); font-size: 13px; }

/* ── Inbox / Ticket list ── */
.inbox-layout { display: flex; flex: 1; overflow: hidden; height: calc(100vh - 56px); }
.ticket-list { width: 340px; flex-shrink: 0; border-right: 1px solid var(--border); overflow-y: auto; background: var(--surface); }
.ticket-item { padding: 14px 16px; border-bottom: 1px solid var(--border); cursor: pointer; transition: background .1s; }
.ticket-item:hover { background: var(--surface-2); }
.ticket-item.active { background: var(--brand-light); border-left: 3px solid var(--brand); }
.ticket-item .contact { font-weight: 600; font-size: 14px; }
.ticket-item .subject { color: var(--text-2); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.ticket-item .meta { display: flex; gap: 8px; align-items: center; margin-top: 6px; }
.ticket-detail { flex: 1; overflow-y: auto; display: flex; flex-direction: column; }
.ticket-header { padding: 16px 24px; border-bottom: 1px solid var(--border); background: var(--surface); display: flex; align-items: center; gap: 12px; }
.ticket-header h2 { font-size: 16px; font-weight: 600; flex: 1; }
.messages-area { flex: 1; padding: 20px 24px; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; }
.message-bubble { max-width: 70%; padding: 10px 14px; border-radius: 12px; font-size: 14px; line-height: 1.5; }
.message-bubble.contact { background: var(--surface-2); border: 1px solid var(--border); align-self: flex-start; border-bottom-left-radius: 4px; }
.message-bubble.agent { background: var(--brand); color: #fff; align-self: flex-end; border-bottom-right-radius: 4px; }
.message-meta { font-size: 11px; margin-top: 4px; opacity: .6; }
.reply-box { padding: 16px 24px; border-top: 1px solid var(--border); background: var(--surface); }
.reply-input { width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius); resize: none; min-height: 80px; font-family: inherit; font-size: 14px; }
.reply-input:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px rgba(79,70,229,.15); }
.reply-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }

/* ── Badges / Pills ── */
.badge { display: inline-flex; align-items: center; padding: 2px 8px; border-radius: 99px; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.badge-open { background: #dbeafe; color: #1d4ed8; }
.badge-pending { background: #fef3c7; color: #92400e; }
.badge-resolved { background: #dcfce7; color: #15803d; }
.badge-closed { background: #f1f5f9; color: #64748b; }
.badge-email { background: #f0fdf4; color: #166534; }
.badge-whatsapp { background: #dcfce7; color: #15803d; }
.badge-instagram { background: #fdf4ff; color: #7e22ce; }
.badge-messenger { background: #eff6ff; color: #1d4ed8; }

/* ── Alert / Toast ── */
.alert { padding: 12px 16px; border-radius: var(--radius); font-size: 14px; margin-bottom: 16px; }
.alert-error { background: #fef2f2; color: var(--danger); border: 1px solid #fecaca; }
.alert-success { background: #f0fdf4; color: var(--success); border: 1px solid #bbf7d0; }

/* ── Loading ── */
.htmx-indicator { opacity: 0; transition: opacity .2s; }
.htmx-request .htmx-indicator, .htmx-request.htmx-indicator { opacity: 1; }
.spinner { display: inline-block; width: 16px; height: 16px; border: 2px solid var(--border); border-top-color: var(--brand); border-radius: 50%; animation: spin .6s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Responsive ── */
@media (max-width: 768px) {
  .sidebar { display: none; }
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .ticket-list { width: 100%; }
  .ticket-detail { display: none; }
  .ticket-detail.visible { display: flex; position: fixed; inset: 0; z-index: 100; background: #fff; }
}

.live-badge {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 6px 14px; border-radius: 999px; font-size: 13px; font-weight: 600;
  text-decoration: none; border: 1px solid var(--border); background: var(--surface);
  color: var(--text); margin-right: 10px; white-space: nowrap; max-width: 60vw; overflow: hidden;
}
.live-badge .live-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.live-badge .live-reason { color: var(--text-2); font-weight: 400; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.live-badge.live .live-dot { background: var(--success); }
.live-badge.live { border-color: rgba(34,197,94,.3); }
.live-badge.offline .live-dot { background: var(--danger); }
.live-badge.offline { border-color: rgba(239,68,68,.3); }
@media (max-width: 768px) {
  .live-badge .live-reason { display: none; }
  .live-badge { max-width: none; }
}
