/* ============================================================
   SolarCRM — Design tokens
   ฐาน: เถ้าถ่าน-น้ำเงินเข้ม (charcoal-navy)
   เด่น: อำพันสุริยะ (solar amber) — สื่อถึงพลังงานแสงอาทิตย์
   ============================================================ */
:root {
  --ink:        #15191E;
  --panel:      #1E242B;
  --panel-2:    #252C34;
  --line:       #323B45;
  --text:       #E8EAED;
  --text-muted: #9AA5B1;

  --amber:      #F2A93B;
  --amber-dim:  #F2A93B22;
  --teal:       #3DBD9A;
  --teal-dim:   #3DBD9A22;
  --red:        #E2483D;
  --red-dim:    #E2483D22;
  --blue:       #5B9BD5;
  --blue-dim:   #5B9BD522;
  --violet:     #9B7FE0;
  --violet-dim: #9B7FE022;

  --radius: 10px;
  --radius-lg: 16px;
  --font-thai: 'Noto Sans Thai', 'Inter', sans-serif;
  --font-en:   'Inter', 'Noto Sans Thai', sans-serif;
}

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

body {
  background: var(--ink);
  color: var(--text);
  font-family: var(--font-thai);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

a { color: inherit; text-decoration: none; }
button { font-family: inherit; cursor: pointer; }

:focus-visible {
  outline: 2px solid var(--amber);
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}

/* ---------- Layout shell ---------- */
.app-shell {
  display: grid;
  grid-template-columns: 240px 1fr;
  min-height: 100vh;
}

.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--line);
  padding: 20px 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px 20px;
  margin-bottom: 8px;
  border-bottom: 1px solid var(--line);
}

.brand-mark {
  width: 34px; height: 34px;
  border-radius: 9px;
  background: linear-gradient(135deg, var(--amber), #d9871f);
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; color: var(--ink); font-size: 16px;
  flex-shrink: 0;
}

.brand-name { font-weight: 700; font-size: 15px; }
.brand-sub { font-size: 11px; color: var(--text-muted); }

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius);
  color: var(--text-muted);
  font-size: 14px;
  font-weight: 500;
  transition: background .15s, color .15s;
}
.nav-item:hover { background: var(--panel-2); color: var(--text); }
.nav-item.active { background: var(--amber-dim); color: var(--amber); }
.nav-item svg { width: 18px; height: 18px; flex-shrink: 0; }

.sidebar-footer {
  margin-top: auto;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}
.user-chip {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; border-radius: var(--radius);
  font-size: 13px; color: var(--text-muted);
}
.user-avatar {
  width: 28px; height: 28px; border-radius: 50%;
  background: var(--panel-2); display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 700; color: var(--text);
  flex-shrink: 0;
}
.logout-link { font-size: 13px; color: var(--text-muted); padding: 8px 10px; display: block; }
.logout-link:hover { color: var(--red); }

.main {
  padding: 28px 32px 60px;
  max-width: 1320px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-bottom: 22px;
  gap: 16px;
  flex-wrap: wrap;
}
.page-title { font-size: 22px; font-weight: 700; margin: 0 0 4px; }
.page-sub { font-size: 13px; color: var(--text-muted); margin: 0; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 9px 16px;
  border-radius: var(--radius);
  font-size: 14px; font-weight: 600;
  border: 1px solid transparent;
  background: var(--panel-2);
  color: var(--text);
  transition: transform .1s, background .15s;
}
.btn:hover { background: var(--line); }
.btn:active { transform: scale(0.98); }
.btn-primary { background: var(--amber); color: #1A1303; }
.btn-primary:hover { background: #e09b2e; }
.btn-ghost { background: transparent; border-color: var(--line); }
.btn-danger { background: transparent; color: var(--red); border-color: var(--red-dim); }
.btn-danger:hover { background: var(--red-dim); }
.btn-sm { padding: 6px 11px; font-size: 13px; }
.btn-icon { padding: 8px; }

/* ---------- Cards / stats ---------- */
.grid { display: grid; gap: 16px; }
.grid-stats { grid-template-columns: repeat(4, 1fr); }
.grid-2 { grid-template-columns: 1.4fr 1fr; }

@media (max-width: 1100px) {
  .grid-stats { grid-template-columns: repeat(2, 1fr); }
  .grid-2 { grid-template-columns: 1fr; }
  .app-shell { grid-template-columns: 1fr; }
  .sidebar { display: none; }
}

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 18px 20px;
}

.stat-card .stat-label {
  display: flex; justify-content: space-between; align-items: center;
  color: var(--text-muted); font-size: 13px; margin-bottom: 10px;
}
.stat-card .stat-value { font-size: 28px; font-weight: 800; font-family: var(--font-en); }
.stat-card .stat-foot { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.stat-card.accent-amber .stat-value { color: var(--amber); }
.stat-card.accent-teal { background: linear-gradient(155deg, var(--teal-dim), var(--panel) 60%); border-color: #3DBD9A40; }
.stat-card.accent-teal .stat-value { color: var(--teal); }

.card-title { font-size: 14px; font-weight: 700; margin: 0 0 14px; }

/* ---------- Source bars ---------- */
.source-row { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; font-size: 13px; }
.source-name { width: 90px; color: var(--text-muted); flex-shrink: 0; }
.source-bar-bg { flex: 1; height: 8px; background: var(--panel-2); border-radius: 4px; overflow: hidden; }
.source-bar-fill { height: 100%; background: linear-gradient(90deg, var(--amber), #d9871f); border-radius: 4px; }
.source-count { width: 24px; text-align: right; font-family: var(--font-en); font-weight: 600; }

/* ---------- Pipeline donut legend ---------- */
.legend-row { display: flex; align-items: center; gap: 8px; font-size: 13px; margin-bottom: 8px; color: var(--text-muted); }
.legend-dot { width: 9px; height: 9px; border-radius: 50%; flex-shrink: 0; }
.legend-count { margin-left: auto; font-weight: 600; color: var(--text); font-family: var(--font-en); }

/* ---------- Followup list ---------- */
.followup-item {
  display: flex; justify-content: space-between; align-items: center;
  padding: 11px 4px; border-bottom: 1px solid var(--line);
  font-size: 14px;
}
.followup-item:last-child { border-bottom: none; }
.followup-name { font-weight: 600; }
.pill { font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 99px; }
.pill-overdue { background: var(--red-dim); color: var(--red); }
.pill-today { background: var(--amber-dim); color: var(--amber); }
.pill-upcoming { background: var(--panel-2); color: var(--text-muted); }

/* ---------- Badges ---------- */
.badge { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; font-weight: 600; padding: 4px 10px; border-radius: 99px; white-space: nowrap; }
.badge::before { content: ''; width: 6px; height: 6px; border-radius: 50%; }
.badge-new { background: var(--blue-dim); color: var(--blue); }
.badge-new::before { background: var(--blue); }
.badge-talking { background: var(--violet-dim); color: var(--violet); }
.badge-talking::before { background: var(--violet); }
.badge-sent { background: var(--amber-dim); color: var(--amber); }
.badge-sent::before { background: var(--amber); }
.badge-pending { background: #E8923333; color: #E89233; }
.badge-pending::before { background: #E89233; }
.badge-won { background: var(--teal-dim); color: var(--teal); }
.badge-won::before { background: var(--teal); }
.badge-later { background: var(--panel-2); color: var(--text-muted); }
.badge-later::before { background: var(--text-muted); }

.lead { font-size: 12px; font-weight: 700; display: inline-flex; align-items: center; gap: 5px; }
.lead::before { content: '🔥'; font-size: 11px; }
.lead-hot { color: var(--red); }
.lead-warm { color: var(--amber); }
.lead-warm::before { content: '🟡'; }
.lead-cold { color: var(--blue); }
.lead-cold::before { content: '❄️'; }

/* ---------- Table ---------- */
.table-wrap { overflow-x: auto; }
table.crm-table { width: 100%; border-collapse: collapse; font-size: 14px; }
table.crm-table th {
  text-align: left; font-size: 12px; color: var(--text-muted); font-weight: 600;
  padding: 10px 14px; border-bottom: 1px solid var(--line); white-space: nowrap;
}
table.crm-table td { padding: 13px 14px; border-bottom: 1px solid var(--line); vertical-align: middle; }
table.crm-table tbody tr { transition: background .12s; }
table.crm-table tbody tr:hover { background: var(--panel-2); }
table.crm-table tbody tr:last-child td { border-bottom: none; }

.cust-name { font-weight: 600; }
.cust-meta { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; }
.icon-btn {
  width: 30px; height: 30px; display: flex; align-items: center; justify-content: center;
  border-radius: 8px; color: var(--text-muted); background: transparent; border: none;
}
.icon-btn:hover { background: var(--line); color: var(--text); }
.icon-btn svg { width: 15px; height: 15px; }

/* ---------- Search / filter bar ---------- */
.toolbar { display: flex; gap: 10px; margin-bottom: 18px; flex-wrap: wrap; align-items: center; }
.search-box {
  flex: 1; min-width: 220px;
  display: flex; align-items: center; gap: 8px;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 9px 14px;
}
.search-box input {
  background: transparent; border: none; outline: none; color: var(--text);
  font-size: 14px; width: 100%; font-family: var(--font-thai);
}
.search-box svg { width: 16px; height: 16px; color: var(--text-muted); flex-shrink: 0; }
select.filter-select {
  background: var(--panel); border: 1px solid var(--line); color: var(--text);
  border-radius: var(--radius); padding: 9px 12px; font-size: 14px; font-family: var(--font-thai);
}

/* ---------- Forms ---------- */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-grid.single { grid-template-columns: 1fr; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 13px; font-weight: 600; color: var(--text-muted); }
.field input, .field select, .field textarea {
  background: var(--panel-2); border: 1px solid var(--line); color: var(--text);
  border-radius: var(--radius); padding: 10px 12px; font-size: 14px; font-family: var(--font-thai);
  width: 100%;
}
.field textarea { resize: vertical; min-height: 80px; }
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--amber); }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 22px; padding-top: 18px; border-top: 1px solid var(--line); }

/* ---------- Auth pages ---------- */
.auth-wrap {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: radial-gradient(circle at 30% 20%, #2a2210 0%, var(--ink) 55%);
  padding: 20px;
}
.auth-card { width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius-lg); padding: 34px 30px; }
.auth-logo { display: flex; align-items: center; gap: 10px; justify-content: center; margin-bottom: 22px; }
.auth-title { text-align: center; font-size: 19px; font-weight: 700; margin: 0 0 4px; }
.auth-sub { text-align: center; font-size: 13px; color: var(--text-muted); margin: 0 0 26px; }
.auth-error { background: var(--red-dim); color: var(--red); border-radius: var(--radius); padding: 10px 14px; font-size: 13px; margin-bottom: 16px; }
.auth-success { background: var(--teal-dim); color: var(--teal); border-radius: var(--radius); padding: 10px 14px; font-size: 13px; margin-bottom: 16px; }

/* ---------- Timeline (ประวัติการคุย) ---------- */
.timeline { display: flex; flex-direction: column; gap: 0; }
.timeline-item { position: relative; padding: 0 0 22px 26px; border-left: 2px solid var(--line); }
.timeline-item:last-child { border-left-color: transparent; padding-bottom: 0; }
.timeline-dot {
  position: absolute; left: -6px; top: 2px;
  width: 11px; height: 11px; border-radius: 50%;
  background: var(--amber); border: 2px solid var(--panel);
}
.timeline-head { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; margin-bottom: 5px; }
.timeline-channel { font-size: 12px; font-weight: 700; color: var(--amber); }
.timeline-time { font-size: 12px; color: var(--text-muted); }
.timeline-note { font-size: 14px; color: var(--text); white-space: pre-wrap; line-height: 1.55; }
.timeline-author { font-size: 11px; color: var(--text-muted); margin-top: 4px; }
.timeline-empty { color: var(--text-muted); font-size: 13px; padding: 10px 0; }

/* ---------- Detail page ---------- */
.detail-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.detail-name { font-size: 22px; font-weight: 700; margin: 0 0 6px; }
.detail-meta-row { display: flex; gap: 14px; flex-wrap: wrap; font-size: 13px; color: var(--text-muted); }
.detail-meta-row span strong { color: var(--text); font-weight: 600; }
.info-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px; margin-bottom: 4px; }
.info-item .info-label { font-size: 12px; color: var(--text-muted); margin-bottom: 3px; }
.info-item .info-value { font-size: 14px; font-weight: 600; }

/* ---------- Status rail (signature element) ---------- */
.status-rail { display: flex; align-items: center; gap: 3px; }
.status-rail .seg { width: 16px; height: 5px; border-radius: 3px; background: var(--line); }
.status-rail .seg.filled { background: var(--amber); }
.status-rail .seg.won { background: var(--teal); }

/* ---------- Empty state ---------- */
.empty-state { text-align: center; padding: 60px 20px; color: var(--text-muted); }
.empty-state .empty-icon { font-size: 36px; margin-bottom: 12px; }
.empty-state p { margin: 0 0 16px; font-size: 14px; }

/* ---------- Toast / flash ---------- */
.flash { padding: 11px 16px; border-radius: var(--radius); font-size: 13px; margin-bottom: 16px; }
.flash-success { background: var(--teal-dim); color: var(--teal); }
.flash-error { background: var(--red-dim); color: var(--red); }

/* ---------- Modal (slide-over) ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(10,12,15,0.6);
  display: none; align-items: stretch; justify-content: flex-end; z-index: 50;
}
.overlay.open { display: flex; }
.slide-panel {
  width: 100%; max-width: 480px; background: var(--panel);
  border-left: 1px solid var(--line); padding: 26px 28px; overflow-y: auto;
  height: 100vh;
}
.slide-panel-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
.slide-panel-title { font-size: 17px; font-weight: 700; margin: 0; }
