:root {
  --primary: #2D5BE3;
  --primary-light: #EEF2FF;
  --primary-dark: #1E3FA0;
  --accent: #00C48C;
  --danger: #FF5C5C;
  --warning: #FFB020;
  --gray-50: #F8FAFC;
  --gray-100: #F1F5F9;
  --gray-200: #E2E8F0;
  --gray-300: #CBD5E1;
  --gray-400: #94A3B8;
  --gray-500: #64748B;
  --gray-600: #475569;
  --gray-700: #334155;
  --gray-800: #1E293B;
  --gray-900: #0F172A;
  --sidebar-w: 240px;
  --radius: 16px;
  --shadow-sm: 0 1px 3px rgba(0,0,0,.08);
  --shadow: 0 4px 16px rgba(0,0,0,.08);
}

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

body {
  font-family: 'Sarabun', sans-serif;
  background: var(--gray-50);
  color: var(--gray-800);
  font-size: 14px;
  min-height: 100vh;
}

/* ── Login Screen ── */
#login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #0F172A 0%, #1E3FA0 50%, #2D5BE3 100%);
  position: relative;
  overflow: hidden;
}

#login-screen::before {
  content: '';
  position: absolute;
  inset: 0;
  background:
    radial-gradient(ellipse at 30% 20%, rgba(45,91,227,.3) 0%, transparent 60%),
    radial-gradient(ellipse at 70% 80%, rgba(0,196,140,.2) 0%, transparent 60%);
}

.login-card {
  background: rgba(255,255,255,.97);
  border-radius: 24px;
  padding: 48px 40px;
  width: 400px;
  box-shadow: 0 32px 80px rgba(0,0,0,.3);
  position: relative;
  z-index: 1;
}

.login-logo {
  width: 56px; height: 56px;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  border-radius: 16px;
  display: flex; align-items: center; justify-content: center;
  font-size: 28px;
  margin-bottom: 20px;
}

.login-title {
  font-family: 'Prompt', sans-serif;
  font-size: 24px;
  font-weight: 600;
  color: var(--gray-900);
}

.login-sub {
  color: var(--gray-400);
  font-size: 14px;
  margin: 6px 0 32px;
}

.login-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--gray-500);
  letter-spacing: .5px;
  text-transform: uppercase;
  margin-bottom: 8px;
  display: block;
}

.login-input {
  width: 100%;
  padding: 14px 16px;
  border: 1.5px solid var(--gray-200);
  border-radius: 12px;
  font-size: 15px;
  font-family: 'Sarabun', sans-serif;
  outline: none;
  transition: all .2s;
  background: var(--gray-50);
  margin-bottom: 20px;
}

.login-input:focus {
  border-color: var(--primary);
  background: white;
  box-shadow: 0 0 0 4px rgba(45,91,227,.08);
}

.login-btn {
  width: 100%;
  padding: 14px;
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  font-family: 'Sarabun', sans-serif;
  transition: all .2s;
}

.login-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 8px 24px rgba(45,91,227,.4);
}

.login-btn:disabled {
  opacity: .6;
  cursor: not-allowed;
  transform: none;
}

.login-error {
  color: var(--danger);
  font-size: 13px;
  margin-top: 12px;
  text-align: center;
  display: none;
}

/* ── App ── */
#app { display: none; min-height: 100vh; }

.sidebar {
  position: fixed;
  left: 0; top: 0; bottom: 0;
  width: var(--sidebar-w);
  background: var(--gray-900);
  display: flex;
  flex-direction: column;
  z-index: 100;
}

.sidebar-logo {
  padding: 24px 20px 20px;
  display: flex;
  align-items: center;
  gap: 12px;
  border-bottom: 1px solid rgba(255,255,255,.06);
}

.logo-icon {
  width: 40px; height: 40px;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
}

.logo-text {
  font-family: 'Prompt', sans-serif;
  font-size: 16px;
  font-weight: 600;
  color: white;
}

.logo-sub { font-size: 11px; color: rgba(255,255,255,.4); }

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

.nav-section {
  font-size: 10px;
  font-weight: 700;
  color: rgba(255,255,255,.25);
  letter-spacing: 1px;
  text-transform: uppercase;
  padding: 16px 8px 8px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 12px;
  border-radius: 10px;
  cursor: pointer;
  color: rgba(255,255,255,.5);
  font-size: 13px;
  font-weight: 500;
  transition: all .15s;
  margin-bottom: 2px;
}

.nav-item:hover {
  background: rgba(255,255,255,.06);
  color: rgba(255,255,255,.8);
}

.nav-item.active {
  background: var(--primary);
  color: white;
}

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

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

.user-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 10px;
  background: rgba(255,255,255,.06);
}

.user-avatar {
  width: 32px; height: 32px;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px;
  color: white;
  font-weight: 600;
}

.user-name { font-size: 13px; font-weight: 600; color: white; }
.user-role { font-size: 11px; color: rgba(255,255,255,.4); }

/* ── Main ── */
.main {
  margin-left: var(--sidebar-w);
  min-height: 100vh;
}

.topbar {
  background: white;
  border-bottom: 1px solid var(--gray-200);
  padding: 0 28px;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 50;
  box-shadow: var(--shadow-sm);
}

.topbar-title {
  font-family: 'Prompt', sans-serif;
  font-size: 18px;
  font-weight: 600;
  color: var(--gray-900);
}

.topbar-sub { font-size: 12px; color: var(--gray-400); }
.topbar-right { display: flex; align-items: center; gap: 10px; }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 9px 16px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  border: 1.5px solid var(--gray-200);
  background: white;
  color: var(--gray-600);
  transition: all .15s;
  font-family: 'Sarabun', sans-serif;
}

.btn:hover {
  border-color: var(--primary);
  color: var(--primary);
  background: var(--primary-light);
}

.btn-primary {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.btn-primary:hover {
  background: var(--primary-dark);
  border-color: var(--primary-dark);
  color: white;
}

.btn-success {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

.content { padding: 24px 28px; }

.filter-bar {
  background: white;
  border-radius: var(--radius);
  padding: 16px 20px;
  margin-bottom: 20px;
  display: flex;
  align-items: flex-end;
  gap: 14px;
  flex-wrap: wrap;
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--gray-200);
}

.filter-group { display: flex; flex-direction: column; gap: 5px; }

.filter-label {
  font-size: 11px;
  font-weight: 700;
  color: var(--gray-400);
  text-transform: uppercase;
  letter-spacing: .5px;
}

.filter-input, .filter-select {
  padding: 9px 12px;
  border: 1.5px solid var(--gray-200);
  border-radius: 10px;
  font-size: 13px;
  font-family: 'Sarabun', sans-serif;
  outline: none;
  background: var(--gray-50);
  color: var(--gray-700);
}

.filter-input:focus, .filter-select:focus { border-color: var(--primary); }

.tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 20px;
  background: white;
  border-radius: 12px;
  padding: 4px;
  box-shadow: var(--shadow-sm);
  width: fit-content;
  border: 1px solid var(--gray-200);
}

.tab {
  padding: 8px 18px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  color: var(--gray-400);
  transition: all .2s;
}

.tab.active { background: var(--primary); color: white; }
.tab:not(.active):hover { background: var(--gray-100); color: var(--gray-600); }

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 14px;
  margin-bottom: 20px;
}

.kpi-card {
  background: white;
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--gray-200);
  transition: transform .2s;
  position: relative;
  overflow: hidden;
}

.kpi-card::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 3px;
  background: var(--kpi-color, var(--primary));
}

.kpi-card:hover { transform: translateY(-2px); }

.kpi-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 12px;
}

.kpi-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--gray-400);
  text-transform: uppercase;
  letter-spacing: .4px;
}

.kpi-icon { font-size: 22px; }

.kpi-value {
  font-family: 'Prompt', sans-serif;
  font-size: 32px;
  font-weight: 700;
  color: var(--gray-900);
  line-height: 1;
}

.kpi-delta { font-size: 12px; margin-top: 6px; }

.charts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(440px, 1fr));
  gap: 16px;
  margin-bottom: 20px;
}

.chart-card {
  background: white;
  border-radius: var(--radius);
  padding: 22px;
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--gray-200);
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 18px;
}

.chart-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--gray-800);
}

.chart-wrap { position: relative; height: 250px; }

.compare-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-bottom: 20px;
}

.table-card {
  background: white;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--gray-200);
}

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

.table-title { font-size: 15px; font-weight: 600; color: var(--gray-800); }

.table-search {
  padding: 8px 14px;
  border: 1.5px solid var(--gray-200);
  border-radius: 10px;
  font-size: 13px;
  font-family: 'Sarabun', sans-serif;
  outline: none;
  width: 220px;
}

.table-search:focus { border-color: var(--primary); }

.table-wrap { overflow-x: auto; }

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

thead th {
  background: var(--gray-50);
  padding: 11px 16px;
  text-align: left;
  font-weight: 600;
  color: var(--gray-500);
  border-bottom: 1px solid var(--gray-200);
  white-space: nowrap;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .4px;
}

tbody tr:hover { background: var(--primary-light); }

tbody td {
  padding: 10px 16px;
  border-bottom: 1px solid var(--gray-100);
  white-space: nowrap;
  color: var(--gray-700);
}

tbody tr:last-child td { border-bottom: none; }

.badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 600;
}

.badge-active { background: #DCFCE7; color: #15803D; }
.badge-replaced { background: #FEE2E2; color: #DC2626; }

.loading-overlay { text-align: center; padding: 80px 20px; }

.spinner {
  width: 40px; height: 40px;
  border: 3px solid var(--gray-200);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .6s linear infinite;
  margin: 0 auto 16px;
}

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

.tab-content { display: none; }
.tab-content.active { display: block; }

@media (max-width: 900px) {
  :root { --sidebar-w: 0px; }
  .sidebar { display: none; }
  .charts-grid, .compare-grid { grid-template-columns: 1fr; }
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .content { padding: 16px; }
}

@media print {
  .sidebar, .topbar, .filter-bar, .tabs, .topbar-right { display: none !important; }
  .main { margin-left: 0; }
}
