:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface2: #f8fafc;
  --accent: #e94560;
  --accent2: #1e40af;
  --text: #0f172a;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --success: #059669;
  --warning: #d97706;
  --danger: #dc2626;
  --info: #2563eb;
  --sidebar-width: 240px;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.04);
}

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

body {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
}

#app-shell {
  display: none;
  width: 100%;
  height: 100vh;
  overflow: hidden;
  flex-direction: row;
}

/* SIDEBAR */
#sidebar {
  width: var(--sidebar-width);
  background: var(--surface);
  border-right: 1px solid var(--border);
  box-shadow: 1px 0 0 var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow-y: auto;
}

.sidebar-brand {
  padding: 20px 16px;
  border-bottom: 1px solid var(--border);
}

.sidebar-brand h1 {
  font-size: 16px;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: 0.5px;
}

.sidebar-brand p {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}

.sidebar-nav { flex: 1; padding: 8px 0; }

.nav-section {
  padding: 12px 16px 4px;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-muted);
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 16px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text-muted);
  transition: all 0.15s;
  border-left: 3px solid transparent;
}

.nav-item:hover { background: var(--surface2); color: var(--text); }
.nav-item.active {
  background: rgba(233,69,96,0.07);
  color: var(--accent);
  border-left-color: var(--accent);
  font-weight: 600;
}

.nav-item .icon { font-size: 16px; width: 20px; text-align: center; }

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
}

.user-avatar {
  width: 32px; height: 32px;
  background: var(--accent);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 700;
}

.user-details { flex: 1; overflow: hidden; }
.user-name { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-role { font-size: 11px; color: var(--text-muted); }

.btn-logout {
  width: 100%;
  padding: 7px;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 12px;
  transition: all 0.15s;
}

.btn-logout:hover { border-color: var(--accent); color: var(--accent); }

/* MAIN */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#topbar {
  height: 56px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 1px 0 var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  flex-shrink: 0;
}

.topbar-title { font-size: 18px; font-weight: 600; }

.topbar-actions { display: flex; gap: 10px; align-items: center; }

#content {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
}

/* VIEWS */
.view { display: none; }
.view.active { display: block; }

/* CARDS */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

.stat-label { font-size: 12px; color: var(--text-muted); margin-bottom: 8px; }
.stat-value { font-size: 28px; font-weight: 700; }
.stat-value.accent { color: var(--accent); }
.stat-value.success { color: var(--success); }
.stat-value.warning { color: var(--warning); }

/* TABLE */
.table-container {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.table-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.table-header h3 { font-size: 14px; font-weight: 600; }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

thead th {
  padding: 10px 16px;
  text-align: left;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  background: var(--surface2);
  border-bottom: 1px solid var(--border);
}

tbody tr { border-bottom: 1px solid var(--border); transition: background 0.1s; }
tbody tr:hover { background: var(--surface2); }
tbody tr:last-child { border-bottom: none; }
td { padding: 12px 16px; }

/* BADGES */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.badge-draft, .badge-pending, .badge-new { background: rgba(136,136,136,0.2); color: #888; }
.badge-sent, .badge-contacted, .badge-scheduled { background: rgba(52,152,219,0.2); color: var(--info); }
.badge-accepted, .badge-paid, .badge-completed, .badge-active, .badge-won { background: rgba(46,204,113,0.2); color: var(--success); }
.badge-rejected, .badge-cancelled, .badge-lost, .badge-expired { background: rgba(231,76,60,0.2); color: var(--danger); }
.badge-in_progress, .badge-approved, .badge-in_use { background: rgba(243,156,18,0.2); color: var(--warning); }
.badge-overdue, .badge-urgent { background: rgba(233,69,96,0.2); color: var(--accent); }

/* BUTTONS */
.btn {
  padding: 8px 16px;
  border-radius: var(--radius);
  border: none;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: all 0.15s;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.btn-primary { background: var(--accent); color: white; }
.btn-primary:hover { background: #c73652; }
.btn-secondary { background: var(--surface2); border: 1px solid var(--border); color: var(--text); }
.btn-secondary:hover { border-color: var(--accent); color: var(--accent); }
.btn-danger { background: rgba(231,76,60,0.2); color: var(--danger); border: 1px solid var(--danger); }
.btn-danger:hover { background: var(--danger); color: white; }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-icon { padding: 6px; border-radius: 6px; }

/* MODAL */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex; align-items: center; justify-content: center;
  z-index: 1000;
  display: none;
}

.modal-overlay.open { display: flex; }

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  width: 560px;
  max-width: 95vw;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: var(--shadow);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px;
  border-bottom: 1px solid var(--border);
}

.modal-header h2 { font-size: 16px; font-weight: 600; }
.modal-close { background: none; border: none; color: var(--text-muted); cursor: pointer; font-size: 20px; }
.modal-close:hover { color: var(--accent); }
.modal-body { padding: 24px; }
.modal-footer { padding: 16px 24px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 10px; }

/* CARD */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

/* FORMS */
.form-group { margin-bottom: 16px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
label { display: block; font-size: 12px; font-weight: 500; color: var(--text-muted); margin-bottom: 6px; }

input, select, textarea {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 8px 12px;
  font-size: 13px;
  outline: none;
  transition: border-color 0.15s;
}

input:focus, select:focus, textarea:focus { border-color: var(--accent); }
textarea { min-height: 80px; resize: vertical; }
select option { background: var(--surface); }

/* LOGIN */
#login-screen {
  position: fixed; inset: 0;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  z-index: 9999;
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 48px 40px;
  width: 400px;
  box-shadow: var(--shadow);
}

.login-card h1 { font-size: 24px; font-weight: 700; color: var(--accent); margin-bottom: 4px; }
.login-card p { color: var(--text-muted); font-size: 14px; margin-bottom: 32px; }
.login-error { color: var(--danger); font-size: 12px; margin-top: 8px; display: none; }

/* SEARCH */
.search-input {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 7px 12px;
  font-size: 13px;
  width: 220px;
}

/* EMPTY STATE */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-muted);
}

.empty-state .icon { font-size: 48px; margin-bottom: 16px; }
.empty-state p { font-size: 14px; }

/* ACTIONS COL */
.actions { display: flex; gap: 6px; }

/* TABS */
.tabs { display: flex; gap: 4px; margin-bottom: 20px; }
.tab {
  padding: 8px 16px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 13px;
  color: var(--text-muted);
  transition: all 0.15s;
}
.tab.active, .tab:hover { background: var(--surface); color: var(--text); }
.tab.active { color: var(--accent); }

/* ASSISTANT RESPONSE BODY */
.ai-response-body {
  background: #f8f9fa !important;
  color: #1a1a2e !important;
  border-radius: 6px;
  padding: 16px 20px;
  font-size: 13px;
  line-height: 1.7;
}

.ai-response-body h1,
.ai-response-body h2,
.ai-response-body h3 {
  color: #1a1a2e !important;
  font-weight: 700;
  line-height: 1.3;
  margin: 20px 0 8px;
}

.ai-response-body h1 { font-size: 18px; border-bottom: 2px solid #e94560; padding-bottom: 8px; }
.ai-response-body h2 { font-size: 15px; border-bottom: 1px solid #ccc; padding-bottom: 4px; }
.ai-response-body h3 { font-size: 13px; }

.ai-response-body p { margin: 8px 0; color: #1a1a2e !important; }

.ai-response-body ul,
.ai-response-body ol {
  padding-left: 20px;
  margin: 8px 0;
  color: #1a1a2e !important;
}

.ai-response-body li { margin-bottom: 4px; color: #1a1a2e !important; }

.ai-response-body span { color: #1a1a2e !important; }

.ai-response-body table {
  width: 100% !important;
  border-collapse: collapse !important;
  margin: 12px 0 !important;
  font-size: 13px !important;
  background: white !important;
}

.ai-response-body thead th {
  background: #e94560 !important;
  color: white !important;
  padding: 9px 12px !important;
  text-align: left;
  font-size: 12px !important;
  font-weight: 600 !important;
  text-transform: none !important;
  letter-spacing: 0 !important;
  border: none !important;
}

.ai-response-body tbody tr { background: white !important; }
.ai-response-body tbody tr:nth-child(even) { background: #f4f4f4 !important; }
.ai-response-body tbody tr:hover { background: #eaeaea !important; }
.ai-response-body td { padding: 7px 12px !important; color: #1a1a2e !important; border-bottom: 1px solid #e8e8e8 !important; }

.ai-response-body strong { font-weight: 700 !important; color: #1a1a2e !important; }
.ai-response-body em { font-style: italic; }

.ai-response-body hr {
  border: none !important;
  border-top: 1px solid #ddd !important;
  margin: 16px 0 !important;
}

/* ASSISTANT */
.chat-container {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  height: 620px;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.chat-msg {
  max-width: 80%;
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 13px;
  line-height: 1.5;
}

.chat-msg.assistant:has(.ai-response-body) {
  max-width: 98%;
  padding: 12px;
}

.chat-msg.user { align-self: flex-end; background: var(--accent); color: white; }
.chat-msg.assistant { align-self: flex-start; background: var(--surface2); }

.chat-input-row {
  display: flex;
  gap: 10px;
  padding: 12px;
  border-top: 1px solid var(--border);
  align-items: flex-end;
}

.chat-input-row input,
.chat-input-row textarea { flex: 1; }

/* SCROLLBAR */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* LOADING */
.loading { color: var(--text-muted); font-size: 13px; padding: 20px; text-align: center; }
.spinner {
  display: inline-block;
  width: 16px; height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* TOAST */
#toast-container {
  position: fixed;
  bottom: 20px; right: 20px;
  z-index: 9998;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  font-size: 13px;
  box-shadow: var(--shadow);
  animation: slideIn 0.2s ease;
  max-width: 320px;
}

.toast.success { border-left: 3px solid var(--success); }
.toast.error { border-left: 3px solid var(--danger); }
.toast.info { border-left: 3px solid var(--info); }

@keyframes slideIn { from { transform: translateX(20px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

/* RESPONSIVE */
@media (max-width: 768px) {
  #sidebar { position: fixed; left: -240px; height: 100%; z-index: 100; transition: left 0.3s; }
  #sidebar.open { left: 0; }
  .form-row { grid-template-columns: 1fr; }
}
