/* ── SalesArea Design System ─────────────────────────────────────────────── */
:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #22263a;
  --surface3: #2a2f45;
  --border: #2e3350;
  --text: #e2e8f0;
  --text2: #8892a4;
  --accent: #6c63ff;
  --accent2: #9d4edd;
  --success: #22c55e;
  --warning: #f59e0b;
  --danger: #ef4444;
  --info: #38bdf8;
  --radius: 10px;
  --shadow: 0 4px 20px rgba(0,0,0,.35);
  --font: 'Sarabun', 'Segoe UI', sans-serif;
}

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

html { height: 100%; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.6;
  min-height: 100%;
}

/* ── Layout ─────────────────────────────────────────────────────────────── */
.app-shell {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* Sidebar */
.sidebar {
  width: 240px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  position: relative;
  z-index: 10;
}

.sidebar-logo {
  padding: 1.25rem 1.25rem 1rem;
  display: flex;
  align-items: center;
  gap: .65rem;
  border-bottom: 1px solid var(--border);
}
.sidebar-logo .logo-icon {
  width: 34px; height: 34px;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  border-radius: 9px;
  display: flex; align-items: center; justify-content: center;
  font-size: .95rem; color: #fff; font-weight: 900;
  flex-shrink: 0;
}
.sidebar-logo .logo-text {
  font-size: .92rem;
  font-weight: 800;
  line-height: 1.2;
  color: var(--text);
}
.sidebar-logo .logo-sub {
  font-size: .65rem;
  color: var(--text2);
}

.sidebar-section {
  padding: .65rem 1rem .25rem;
  font-size: .6rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text2);
}

.nav-item {
  display: flex;
  align-items: center;
  gap: .65rem;
  padding: .55rem 1.1rem;
  margin: 1px .65rem;
  border-radius: 8px;
  font-size: .84rem;
  font-weight: 500;
  color: var(--text2);
  text-decoration: none;
  transition: all .15s;
}
.nav-item:hover { background: var(--surface2); color: var(--text); }
.nav-item.active {
  background: rgba(108,99,255,.18);
  color: var(--accent);
  font-weight: 700;
}
.nav-item i { width: 16px; text-align: center; font-size: .82rem; }

.sidebar-footer {
  margin-top: auto;
  padding: 1rem;
  border-top: 1px solid var(--border);
}
.user-mini {
  display: flex; align-items: center; gap: .65rem;
  padding: .5rem .65rem;
  border-radius: 8px;
  cursor: pointer;
  transition: background .15s;
  text-decoration: none;
  color: var(--text);
}
.user-mini:hover { background: var(--surface2); }
.user-avatar {
  width: 30px; height: 30px;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: .72rem; font-weight: 900; color: #fff;
  flex-shrink: 0;
}
.user-info { flex: 1; min-width: 0; }
.user-name { font-size: .8rem; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-role { font-size: .65rem; color: var(--text2); }

/* Main area */
.main-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.topbar {
  height: 54px;
  flex-shrink: 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 1.5rem;
  gap: 1rem;
}
.topbar-title {
  font-size: .97rem;
  font-weight: 800;
  color: var(--text);
  flex: 1;
}
.topbar-btn {
  display: inline-flex; align-items: center; gap: .4rem;
  padding: .4rem .9rem;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: .8rem;
  font-weight: 700;
  cursor: pointer;
  text-decoration: none;
  transition: opacity .15s;
}
.topbar-btn:hover { opacity: .85; }
.topbar-btn.ghost {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
}

.page-content {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem;
}

/* ── Cards & Tables ─────────────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  margin-bottom: 1.25rem;
}
.card-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 1rem; flex-wrap: wrap; gap: .5rem;
}
.card-title {
  font-size: .92rem;
  font-weight: 800;
  color: var(--text);
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: .85rem;
  margin-bottom: 1.5rem;
}
.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.1rem;
  position: relative;
  overflow: hidden;
}
.kpi-card::before {
  content: '';
  position: absolute; top: 0; left: 0; right: 0; height: 3px;
  background: var(--kpi-color, var(--accent));
}
.kpi-val { font-size: 1.6rem; font-weight: 900; color: var(--kpi-color, var(--accent)); }
.kpi-label { font-size: .67rem; color: var(--text2); text-transform: uppercase; letter-spacing: .6px; margin-top: .15rem; }

table.sa-table {
  width: 100%;
  border-collapse: collapse;
  font-size: .83rem;
}
table.sa-table thead th {
  padding: .5rem .75rem;
  background: var(--surface2);
  font-size: .65rem;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--text2);
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
  text-align: left;
}
table.sa-table tbody tr {
  border-bottom: 1px solid var(--border);
  transition: background .1s;
}
table.sa-table tbody tr:hover { background: var(--surface2); }
table.sa-table td { padding: .55rem .75rem; vertical-align: middle; }

/* ── Forms ──────────────────────────────────────────────────────────────── */
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 1rem; }
.form-group { display: flex; flex-direction: column; gap: .35rem; }
.form-group label { font-size: .78rem; font-weight: 700; color: var(--text2); }
.form-control {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: .45rem .75rem;
  font-size: .85rem;
  font-family: var(--font);
  width: 100%;
  transition: border-color .15s;
}
.form-control:focus { outline: none; border-color: var(--accent); }
.form-control::placeholder { color: var(--text2); }
textarea.form-control { resize: vertical; min-height: 80px; }
select.form-control { cursor: pointer; }

/* ── Badges & Status ─────────────────────────────────────────────────────── */
.badge {
  display: inline-flex; align-items: center;
  padding: 2px 10px;
  border-radius: 100px;
  font-size: .68rem;
  font-weight: 700;
  white-space: nowrap;
}
.badge-success  { background: rgba(34,197,94,.15);  color: #22c55e; }
.badge-warning  { background: rgba(245,158,11,.15); color: #f59e0b; }
.badge-danger   { background: rgba(239,68,68,.15);  color: #ef4444; }
.badge-info     { background: rgba(56,189,248,.15); color: #38bdf8; }
.badge-default  { background: var(--surface2); color: var(--text2); }
.badge-purple   { background: rgba(157,78,221,.15); color: #9d4edd; }
.badge-accent   { background: rgba(108,99,255,.15); color: var(--accent); }

/* Focus indicators */
.focus-healthy { color: #22c55e; }
.focus-concern { color: #f59e0b; }
.focus-warning { color: #ef4444; }
.focus-risk    { color: #a855f7; }

/* ── Buttons ─────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; gap: .4rem;
  padding: .42rem .95rem;
  border-radius: 8px;
  font-size: .82rem;
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: all .15s;
  text-decoration: none;
  font-family: var(--font);
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { opacity: .88; }
.btn-success { background: var(--success); color: #fff; }
.btn-danger  { background: rgba(239,68,68,.12); color: var(--danger); border: 1px solid rgba(239,68,68,.3); }
.btn-danger:hover { background: rgba(239,68,68,.22); }
.btn-ghost   { background: var(--surface2); color: var(--text); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--surface3); }
.btn-sm { padding: .28rem .65rem; font-size: .75rem; }
.btn-icon { padding: .38rem .5rem; }

/* ── Modal ───────────────────────────────────────────────────────────────── */
.modal-overlay {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,.6);
  z-index: 1000;
  align-items: center;
  justify-content: center;
}
.modal-overlay.open { display: flex; }
.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1.5rem;
  width: 560px;
  max-width: 95vw;
  max-height: 90vh;
  overflow-y: auto;
  position: relative;
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 1.25rem;
}
.modal-title { font-size: 1rem; font-weight: 800; }
.modal-close {
  background: none; border: none; cursor: pointer;
  color: var(--text2); font-size: 1.1rem; padding: .2rem;
}
.modal-close:hover { color: var(--text); }

/* ── Flash ───────────────────────────────────────────────────────────────── */
.flash {
  padding: .75rem 1.1rem;
  border-radius: 9px;
  margin-bottom: 1rem;
  font-size: .85rem;
  font-weight: 600;
  display: flex; align-items: center; gap: .6rem;
}
.flash-success { background: rgba(34,197,94,.12); border: 1px solid rgba(34,197,94,.3); color: #22c55e; }
.flash-error   { background: rgba(239,68,68,.12); border: 1px solid rgba(239,68,68,.3); color: #ef4444; }
.flash-warning { background: rgba(245,158,11,.12); border: 1px solid rgba(245,158,11,.3); color: #f59e0b; }

/* ── Login page ──────────────────────────────────────────────────────────── */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
}
.login-box {
  width: 400px;
  max-width: 95vw;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 2.5rem 2rem;
  box-shadow: var(--shadow);
}
.login-logo {
  text-align: center;
  margin-bottom: 2rem;
}
.login-logo .logo-circle {
  width: 56px; height: 56px;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  border-radius: 16px;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 1.4rem; color: #fff; font-weight: 900;
  margin-bottom: .75rem;
}
.login-logo h1 { font-size: 1.3rem; font-weight: 900; }
.login-logo p  { font-size: .82rem; color: var(--text2); margin-top: .2rem; }

/* ── Miscellaneous ───────────────────────────────────────────────────────── */
.divider { border: none; border-top: 1px solid var(--border); margin: 1rem 0; }
.text-muted { color: var(--text2); font-size: .8rem; }
.text-right { text-align: right; }
.text-center { text-align: center; }
.d-flex { display: flex; }
.align-center { align-items: center; }
.gap-sm { gap: .5rem; }
.gap-md { gap: 1rem; }
.flex-wrap { flex-wrap: wrap; }
.flex-1 { flex: 1; }
.mb-0 { margin-bottom: 0; }
.mb-1 { margin-bottom: .5rem; }
.mb-2 { margin-bottom: 1rem; }
.fw-bold { font-weight: 700; }
.font-mono { font-family: 'Courier New', monospace; font-size: .8rem; }

/* Search input */
.search-input {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: .4rem .85rem;
  font-size: .82rem;
  font-family: var(--font);
  min-width: 220px;
}
.search-input:focus { outline: none; border-color: var(--accent); }

/* Pagination */
.pagination { display: flex; gap: .3rem; align-items: center; }
.page-btn {
  min-width: 32px; height: 32px;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text2);
  border-radius: 6px;
  font-size: .78rem;
  cursor: pointer;
  text-decoration: none;
  transition: all .15s;
}
.page-btn:hover, .page-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* Action dropdown */
.action-menu { position: relative; display: inline-block; }
.action-menu-btn {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text2);
  border-radius: 6px;
  padding: .3rem .55rem;
  cursor: pointer;
  font-size: .78rem;
}
.action-menu-btn:hover { color: var(--text); }
.action-dropdown {
  display: none;
  position: absolute;
  right: 0;
  top: calc(100% + 4px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 9px;
  min-width: 160px;
  z-index: 100;
  overflow: hidden;
  box-shadow: var(--shadow);
}
.action-menu.open .action-dropdown { display: block; }
.action-dropdown a, .action-dropdown button {
  display: flex; align-items: center; gap: .5rem;
  padding: .55rem .9rem;
  width: 100%;
  background: none;
  border: none;
  color: var(--text);
  font-size: .82rem;
  cursor: pointer;
  text-decoration: none;
  font-family: var(--font);
}
.action-dropdown a:hover, .action-dropdown button:hover { background: var(--surface2); }
.action-dropdown .danger { color: var(--danger); }

/* Print */
@media print {
  .sidebar, .topbar, .no-print { display: none !important; }
  body, html { background: #fff !important; color: #000 !important; }
  .card { border: 1px solid #ccc !important; background: #fff !important; }
}

/* Responsive */
@media (max-width: 768px) {
  .sidebar { display: none; }
  .form-grid { grid-template-columns: 1fr; }
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
}