/* ============================================================
   CRM Helpdesk - Design System
   ============================================================ */

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

:root {
  --primary:       #2563eb;
  --primary-dark:  #1d4ed8;
  --primary-light: #eff6ff;
  --success:       #16a34a;
  --warning:       #d97706;
  --danger:        #dc2626;
  --sidebar-bg:    #0f172a;
  --sidebar-text:  #94a3b8;
  --sidebar-hover: #1e293b;
  --sidebar-active:#1e40af;
  --bg:            #f1f5f9;
  --surface:       #ffffff;
  --border:        #e2e8f0;
  --text:          #0f172a;
  --text-muted:    #64748b;
  --radius:        8px;
  --shadow:        0 1px 3px rgba(0,0,0,.08), 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);
  --font:          -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --sidebar-w:     240px;
}

html { font-size: 14px; }
body { font-family: var(--font); background: var(--bg); color: var(--text); min-height: 100vh; }
a    { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
button { cursor: pointer; font-family: var(--font); }
input, textarea, select { font-family: var(--font); font-size: 14px; }
ul { list-style: none; }

/* ── Login Page ─────────────────────────────────────────── */
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #3b0618 0%, #8B1535 60%, #a01a3f 100%);
}

.login-card {
  background: var(--surface);
  border-radius: 12px;
  padding: 48px 40px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
}

.login-logo {
  text-align: center;
  margin-bottom: 32px;
}

.login-logo-img {
  display: block;
  margin: 0 auto 14px;
  max-width: 200px;
  max-height: 72px;
  object-fit: contain;
}

.login-logo p  { color: var(--text-muted); font-size: 13px; margin-top: 4px; }

/* ── App Layout ─────────────────────────────────────────── */
.app { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-w);
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  z-index: 100;
  transition: transform .2s;
}

.sidebar-logo {
  padding: 16px;
  background: #8B1535;
  border-bottom: 1px solid rgba(0,0,0,.2);
  flex-shrink: 0;
}

.sidebar-logo a {
  display: block;
  text-decoration: none;
}

.sidebar-logo-img {
  display: block;
  max-width: 152px;
  max-height: 44px;
  object-fit: contain;
  /* White pill keeps the colour logo readable on the maroon background */
  background: rgba(255,255,255,0.92);
  border-radius: 6px;
  padding: 5px 10px;
  margin-bottom: 10px;
}

.sidebar-system-name {
  color: #ffffff;
  font-size: 12.5px;
  font-weight: 700;
  line-height: 1.35;
  letter-spacing: .01em;
}

.sidebar-company-sub {
  color: rgba(255,190,205,0.85);
  font-size: 11px;
  font-weight: 400;
  margin-top: 2px;
}

.sidebar-nav { flex: 1; padding: 12px 8px; overflow-y: auto; }

.nav-section { margin-bottom: 24px; }
.nav-label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: #475569;
  padding: 0 8px;
  margin-bottom: 4px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 6px;
  color: var(--sidebar-text);
  font-size: 13.5px;
  font-weight: 500;
  transition: background .15s, color .15s;
  cursor: pointer;
  text-decoration: none;
}

.nav-item:hover { background: var(--sidebar-hover); color: #e2e8f0; text-decoration: none; }
.nav-item.active { background: var(--sidebar-active); color: white; }

.nav-item svg { width: 16px; height: 16px; flex-shrink: 0; }
.nav-item .badge {
  margin-left: auto;
  background: var(--danger);
  color: white;
  font-size: 10px;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 10px;
  min-width: 18px;
  text-align: center;
}

.sidebar-footer {
  padding: 12px;
  border-top: 1px solid rgba(255,255,255,.06);
}

.user-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px;
  border-radius: 8px;
  cursor: pointer;
  transition: background .15s;
}

.user-card:hover { background: var(--sidebar-hover); }

.avatar {
  width: 32px; height: 32px;
  background: var(--primary);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  color: white;
  font-weight: 700;
  font-size: 12px;
  flex-shrink: 0;
}

.user-info { flex: 1; min-width: 0; }
.user-info .name  { color: #e2e8f0; font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-info .role  { color: #475569;  font-size: 11px; }

/* ── Main content ───────────────────────────────────────── */
.main { margin-left: var(--sidebar-w); flex: 1; display: flex; flex-direction: column; min-height: 100vh; }

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
  height: 56px;
  display: flex;
  align-items: center;
  gap: 16px;
  position: sticky;
  top: 0;
  z-index: 50;
}

.topbar-title { font-size: 16px; font-weight: 600; flex: 1; }

.page-content { padding: 24px; flex: 1; }

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

.card-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
}

.card-header h3 { font-size: 14px; font-weight: 600; flex: 1; }
.card-body { padding: 20px; }

/* ── Stats grid ─────────────────────────────────────────── */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 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-label { font-size: 12px; font-weight: 500; color: var(--text-muted); text-transform: uppercase; letter-spacing: .05em; }
.stat-value { font-size: 28px; font-weight: 700; margin-top: 4px; }
.stat-card.open     .stat-value { color: var(--primary); }
.stat-card.pending  .stat-value { color: var(--warning); }
.stat-card.resolved .stat-value { color: var(--success); }
.stat-card.total    .stat-value { color: var(--text); }

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

table { width: 100%; border-collapse: collapse; }
th {
  text-align: left;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-muted);
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: #f8fafc;
}
td { padding: 13px 16px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tr:last-child td { border-bottom: none; }
tbody tr { transition: background .1s; cursor: pointer; }
tbody tr:hover { background: #f8fafc; }

/* ── Badges ─────────────────────────────────────────────── */
.badge-status, .badge-priority {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  text-transform: capitalize;
}

.badge-status.open     { background: #dbeafe; color: #1d4ed8; }
.badge-status.pending  { background: #fef3c7; color: #92400e; }
.badge-status.resolved { background: #dcfce7; color: #166534; }
.badge-status.closed   { background: #f1f5f9; color: #64748b; }

.badge-priority.low    { background: #f1f5f9; color: #64748b; }
.badge-priority.medium { background: #dbeafe; color: #1d4ed8; }
.badge-priority.high   { background: #fef3c7; color: #92400e; }
.badge-priority.urgent { background: #fee2e2; color: #991b1b; }

/* ── Buttons ────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  border: none;
  transition: background .15s, box-shadow .15s;
}

.btn-primary   { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-secondary { background: var(--surface); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: #f8fafc; }
.btn-danger    { background: var(--danger); color: white; }
.btn-danger:hover { background: #b91c1c; }
.btn-success   { background: var(--success); color: white; }
.btn-success:hover { background: #15803d; }
.btn-sm { padding: 5px 12px; font-size: 12px; }
.btn-icon { padding: 8px; border-radius: 6px; background: transparent; border: 1px solid var(--border); }
.btn-icon:hover { background: var(--bg); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ── Forms ──────────────────────────────────────────────── */
.form-group { margin-bottom: 16px; }
.form-label { display: block; font-size: 13px; font-weight: 500; margin-bottom: 6px; color: var(--text); }
.form-label.required::after { content: ' *'; color: var(--danger); }

.form-control {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  color: var(--text);
  background: var(--surface);
  transition: border-color .15s, box-shadow .15s;
  outline: none;
}

.form-control:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37,99,235,.1);
}

.form-control::placeholder { color: #94a3b8; }
textarea.form-control { resize: vertical; min-height: 100px; }

.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.form-hint { font-size: 11px; color: var(--text-muted); margin-top: 4px; }

/* ── Search / Filter bar ────────────────────────────────── */
.filter-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.search-box {
  position: relative;
  flex: 1;
  min-width: 200px;
}

.search-box svg {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
}

.search-box input {
  padding-left: 34px;
  width: 100%;
}

/* ── Ticket detail ──────────────────────────────────────── */
.ticket-layout {
  display: grid;
  grid-template-columns: 1fr 300px;
  gap: 20px;
  align-items: start;
}

.ticket-header {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}

.ticket-title-row {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 10px;
}

.ticket-title { font-size: 18px; font-weight: 600; flex: 1; }
.ticket-meta  { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.ticket-number { font-size: 12px; color: var(--text-muted); font-family: monospace; }

/* ── Conversation thread ────────────────────────────────── */
.thread { display: flex; flex-direction: column; gap: 12px; margin-bottom: 16px; }

.message-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.message-item.note { border-left: 3px solid var(--warning); }
.message-item.email_out { border-left: 3px solid var(--primary); }
.message-item.email_in  { border-left: 3px solid var(--border); }

.message-header {
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  border-bottom: 1px solid var(--border);
  background: #fafafa;
}

.message-type-badge {
  font-size: 10px;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 99px;
  text-transform: uppercase;
}

.type-email_in  { background: #f1f5f9; color: #475569; }
.type-email_out { background: #dbeafe; color: #1d4ed8; }
.type-note      { background: #fef3c7; color: #92400e; }

.message-from   { font-weight: 600; font-size: 13px; }
.message-time   { margin-left: auto; font-size: 11px; color: var(--text-muted); }

.message-body {
  padding: 16px;
  font-size: 13.5px;
  line-height: 1.7;
  color: var(--text);
}

.message-body.html-body { }
.message-body.html-body img { max-width: 100%; }

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

.reply-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
  padding: 0 16px;
  gap: 0;
}

.reply-tab {
  padding: 12px 16px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  transition: color .15s, border-color .15s;
  background: none;
  border-top: none; border-left: none; border-right: none;
}

.reply-tab.active    { color: var(--primary); border-bottom-color: var(--primary); }
.reply-tab:hover     { color: var(--text); }

.reply-editor { padding: 16px; }
.reply-editor textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px;
  font-size: 13.5px;
  line-height: 1.6;
  resize: vertical;
  min-height: 120px;
  color: var(--text);
  outline: none;
  transition: border-color .15s;
}
.reply-editor textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,.1); }

.reply-toolbar {
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  border-top: 1px solid var(--border);
  background: #f8fafc;
  border-radius: 0 0 var(--radius) var(--radius);
}

/* ── Ticket sidebar ─────────────────────────────────────── */
.ticket-sidebar .card { margin-bottom: 16px; }
.detail-item { margin-bottom: 12px; }
.detail-label { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .05em; color: var(--text-muted); margin-bottom: 4px; }
.detail-value { font-size: 13px; color: var(--text); }

.tag { display: inline-flex; align-items: center; gap: 4px; background: var(--primary-light); color: var(--primary); padding: 2px 8px; border-radius: 99px; font-size: 11px; font-weight: 600; }
.tag-remove { cursor: pointer; opacity: .6; }
.tag-remove:hover { opacity: 1; }
.tags-list { display: flex; flex-wrap: wrap; gap: 6px; }

/* ── Collision warning ──────────────────────────────────── */
.collision-banner {
  background: #fef3c7;
  border: 1px solid #f59e0b;
  border-radius: 8px;
  padding: 10px 14px;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: #92400e;
}

/* ── Empty state ────────────────────────────────────────── */
.empty-state {
  text-align: center;
  padding: 48px 24px;
  color: var(--text-muted);
}

.empty-state svg { width: 48px; height: 48px; margin-bottom: 12px; opacity: .3; }
.empty-state h3  { font-size: 15px; font-weight: 600; color: var(--text); margin-bottom: 6px; }
.empty-state p   { font-size: 13px; }

/* ── Modal ──────────────────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 200;
  backdrop-filter: blur(2px);
  opacity: 0; pointer-events: none;
  transition: opacity .2s;
}

.modal-overlay.open { opacity: 1; pointer-events: all; }

.modal {
  background: var(--surface);
  border-radius: 12px;
  box-shadow: 0 20px 60px rgba(0,0,0,.2);
  width: 100%;
  max-width: 520px;
  max-height: 90vh;
  overflow-y: auto;
  transform: scale(.95);
  transition: transform .2s;
}

.modal-overlay.open .modal { transform: scale(1); }

.modal-header {
  padding: 20px 24px 16px;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 12px;
}

.modal-header h2 { font-size: 16px; font-weight: 600; flex: 1; }
.modal-body    { padding: 20px 24px; }
.modal-footer  { padding: 16px 24px; border-top: 1px solid var(--border); display: flex; gap: 8px; justify-content: flex-end; }

/* ── Toast notifications ────────────────────────────────── */
#toast-container {
  position: fixed;
  bottom: 24px; right: 24px;
  display: flex; flex-direction: column; gap: 8px;
  z-index: 999;
}

.toast {
  background: #1e293b;
  color: white;
  padding: 12px 16px;
  border-radius: 8px;
  font-size: 13px;
  max-width: 320px;
  box-shadow: var(--shadow-md);
  display: flex; align-items: center; gap: 10px;
  animation: slideIn .2s ease;
}

.toast.success { background: #166534; }
.toast.error   { background: #991b1b; }
.toast.warning { background: #92400e; }

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

/* ── Loading ────────────────────────────────────────────── */
.spinner {
  display: inline-block;
  width: 18px; height: 18px;
  border: 2px solid rgba(255,255,255,.3);
  border-top-color: white;
  border-radius: 50%;
  animation: spin .7s linear infinite;
}

.spinner.dark {
  border-color: rgba(0,0,0,.1);
  border-top-color: var(--primary);
}

@keyframes spin { to { transform: rotate(360deg); } }

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

/* ── Settings tabs ──────────────────────────────────────── */
.settings-layout { display: grid; grid-template-columns: 220px 1fr; gap: 24px; align-items: start; }

.settings-nav { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 8px; box-shadow: var(--shadow); }

.settings-nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px;
  border-radius: 6px;
  font-size: 13px; font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  transition: background .1s, color .1s;
  text-decoration: none;
}

.settings-nav-item:hover { background: var(--bg); color: var(--text); text-decoration: none; }
.settings-nav-item.active { background: var(--primary-light); color: var(--primary); }

.settings-panel { display: none; }
.settings-panel.active { display: block; }

/* ── Inbox list rows ────────────────────────────────────── */
.inbox-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
.inbox-row-label  { font-size: 13px; font-weight: 600; color: var(--text); min-width: 100px; }
.inbox-row-email  { font-size: 13px; color: var(--text-muted); flex: 1; }
.inbox-row-badge  { font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 10px; }
.inbox-row-badge.active   { background: #dcfce7; color: #16a34a; }
.inbox-row-badge.inactive { background: #f1f5f9; color: #94a3b8; }

/* ── Notification toggle rows ───────────────────────────── */
.notif-toggle-row {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 14px 0;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.notif-toggle-row:hover { background: none; }
.notif-toggle-body       { flex: 1; }
.notif-toggle-title      { font-size: 13.5px; font-weight: 600; color: var(--text); margin-bottom: 3px; }
.notif-toggle-desc       { font-size: 12px; color: var(--text-muted); line-height: 1.5; }

/* Hide native checkbox, replace with custom switch */
.notif-cb { display: none; }

.notif-switch {
  position: relative;
  display: inline-block;
  width: 42px;
  height: 24px;
  flex-shrink: 0;
}
.notif-switch::before {
  content: '';
  position: absolute;
  inset: 0;
  background: #cbd5e1;
  border-radius: 12px;
  transition: background .2s;
}
.notif-switch::after {
  content: '';
  position: absolute;
  top: 3px; left: 3px;
  width: 18px; height: 18px;
  background: white;
  border-radius: 50%;
  transition: transform .2s;
  box-shadow: 0 1px 3px rgba(0,0,0,.2);
}
.notif-cb:checked + .notif-switch::before { background: var(--primary); }
.notif-cb:checked + .notif-switch::after  { transform: translateX(18px); }

/* ── Pagination ─────────────────────────────────────────── */
.pagination {
  display: flex; align-items: center; gap: 4px;
  justify-content: center;
  padding: 16px;
  border-top: 1px solid var(--border);
}

.page-btn {
  min-width: 32px; height: 32px;
  display: flex; align-items: center; justify-content: center;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: background .1s, border-color .1s;
}

.page-btn:hover   { background: var(--bg); }
.page-btn.active  { background: var(--primary); color: white; border-color: var(--primary); }
.page-btn:disabled { opacity: .4; cursor: not-allowed; }

/* ── Divider ────────────────────────────────────────────── */
.divider { height: 1px; background: var(--border); margin: 20px 0; }

/* ── Responsiveness ─────────────────────────────────────── */
@media (max-width: 1024px) {
  .ticket-layout { grid-template-columns: 1fr; }
}

@media (max-width: 768px) {
  .sidebar { transform: translateX(-100%); }
  .sidebar.open { transform: translateX(0); }
  .main { margin-left: 0; }
  .form-row { grid-template-columns: 1fr; }
  .settings-layout { grid-template-columns: 1fr; }
  .stats-grid { grid-template-columns: 1fr 1fr; }
}
