@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&family=Noto+Sans+Thai:wght@400;500;600;700;800&display=swap');

:root {
  --sidebar-expanded: 260px;
  --sidebar-collapsed: 82px;
  --topbar-height: 72px;
  --primary-950: #06142f;
  --primary-900: #081b3a;
  --primary-800: #0b2853;
  --blue-700: #1d4ed8;
  --blue-600: #2563eb;
  --blue-500: #3b82f6;
  --blue-100: #dbeafe;
  --blue-50: #eff6ff;
  --green-600: #059669;
  --green-100: #d1fae5;
  --amber-600: #d97706;
  --amber-100: #fef3c7;
  --red-600: #dc2626;
  --red-100: #fee2e2;
  --slate-950: #0f172a;
  --slate-800: #1e293b;
  --slate-700: #334155;
  --slate-600: #475569;
  --slate-500: #64748b;
  --slate-400: #94a3b8;
  --slate-300: #cbd5e1;
  --slate-200: #e2e8f0;
  --slate-100: #f1f5f9;
  --slate-50: #f8fafc;
  --white: #ffffff;
  --page-bg: #f5f7fb;
  --card: #ffffff;
  --border: #e6eaf0;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-md: 0 8px 30px rgba(15, 23, 42, 0.07);
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
}

* { box-sizing: border-box; }
html { min-width: 320px; background: var(--page-bg); }
body {
  margin: 0;
  min-height: 100vh;
  color: var(--slate-800);
  background: var(--page-bg);
  font-family: 'Inter', 'Noto Sans Thai', system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  line-height: 1.45;
}
button, input, select, textarea { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, a:focus-visible {
  outline: 3px solid rgba(37, 99, 235, 0.28);
  outline-offset: 2px;
}
[hidden], .is-hidden { display: none !important; }

.app-shell { min-height: 100vh; }

.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  z-index: 60;
  display: flex;
  width: var(--sidebar-expanded);
  flex-direction: column;
  overflow: hidden;
  color: #dbeafe;
  background:
    radial-gradient(circle at 18% 100%, rgba(86, 45, 255, 0.45), transparent 23%),
    radial-gradient(circle at 95% 92%, rgba(0, 191, 255, 0.38), transparent 29%),
    linear-gradient(180deg, var(--primary-950), var(--primary-900) 66%, #061a39);
  box-shadow: 10px 0 36px rgba(2, 12, 30, 0.16);
  transition: width 220ms ease, transform 220ms ease;
}
.sidebar::after {
  content: '';
  position: absolute;
  right: -20%;
  bottom: -2%;
  left: -20%;
  height: 150px;
  background: linear-gradient(160deg, transparent 15%, rgba(49, 46, 129, 0.5) 40%, rgba(14, 165, 233, 0.46) 68%, transparent 90%);
  filter: blur(12px);
  transform: rotate(-5deg);
  pointer-events: none;
}
.sidebar__brand {
  position: relative;
  z-index: 2;
  display: flex;
  min-height: var(--topbar-height);
  align-items: center;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.09);
}
.brand-mark {
  display: grid;
  width: 44px;
  height: 44px;
  flex: 0 0 44px;
  place-items: center;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 13px;
  color: #fff;
  background: linear-gradient(135deg, #38bdf8, #2563eb 55%, #6d28d9);
  box-shadow: 0 9px 24px rgba(37, 99, 235, 0.38);
  font-size: 22px;
}
.brand-copy { min-width: 0; white-space: nowrap; }
.brand-copy strong { display: block; color: #fff; font-size: 16px; letter-spacing: -0.2px; }
.brand-copy span { color: #93c5fd; font-size: 10px; }

.sidebar__nav {
  position: relative;
  z-index: 2;
  flex: 1;
  overflow-y: auto;
  padding: 20px 12px 180px;
  scrollbar-width: thin;
}
.nav-section { margin: 0 8px 8px; color: #7092bd; font-size: 10px; font-weight: 800; letter-spacing: 0.11em; text-transform: uppercase; }
.nav-section:not(:first-child) { margin-top: 24px; }
.nav-item {
  display: flex;
  width: 100%;
  min-height: 48px;
  align-items: center;
  gap: 13px;
  margin: 4px 0;
  padding: 11px 14px;
  border: 1px solid transparent;
  border-radius: 10px;
  color: #bfd2eb;
  background: transparent;
  cursor: pointer;
  text-align: left;
  transition: color 160ms ease, background 160ms ease, border-color 160ms ease, transform 160ms ease;
}
.nav-item:hover { color: #fff; background: rgba(255,255,255,0.08); transform: translateX(2px); }
.nav-item.is-active {
  color: #fff;
  border-color: rgba(125, 211, 252, 0.23);
  background: linear-gradient(100deg, rgba(14, 165, 233, 0.54), rgba(67, 56, 202, 0.88));
  box-shadow: 0 9px 24px rgba(30, 64, 175, 0.32);
}
.nav-item__icon { display: grid; width: 22px; flex: 0 0 22px; place-items: center; font-size: 18px; }
.nav-item__label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; font-weight: 600; }
.nav-item__badge { margin-left: auto; padding: 2px 7px; border-radius: 999px; color: #bfdbfe; background: rgba(255,255,255,0.12); font-size: 9px; }

.sidebar__footer {
  position: absolute;
  z-index: 4;
  right: 0;
  bottom: 0;
  left: 0;
  padding: 14px 12px 18px;
  background: linear-gradient(0deg, rgba(6, 20, 47, 0.98) 65%, rgba(6, 20, 47, 0));
}
.user-card {
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr) 34px;
  align-items: center;
  gap: 10px;
  padding: 10px;
  border: 1px solid rgba(255,255,255,0.11);
  border-radius: 12px;
  background: rgba(255,255,255,0.06);
  backdrop-filter: blur(9px);
}
.user-avatar { display: grid; width: 36px; height: 36px; place-items: center; border-radius: 50%; color: #0b2853; background: #dbeafe; font-weight: 800; }
.user-meta { min-width: 0; }
.user-meta strong, .user-meta span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-meta strong { color: #fff; font-size: 12px; }
.user-meta span { color: #93c5fd; font-size: 10px; text-transform: capitalize; }
.icon-button--sidebar { width: 32px; height: 32px; color: #bfdbfe; background: transparent; border: 0; border-radius: 8px; cursor: pointer; }
.icon-button--sidebar:hover { color: #fff; background: rgba(255,255,255,0.1); }

.main-shell { min-height: 100vh; margin-left: var(--sidebar-expanded); transition: margin-left 220ms ease; }
.topbar {
  position: sticky;
  top: 0;
  z-index: 45;
  display: flex;
  min-height: var(--topbar-height);
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 0 28px;
  border-bottom: 1px solid var(--border);
  background: rgba(255,255,255,0.92);
  backdrop-filter: blur(14px);
}
.topbar__left, .topbar__right { display: flex; align-items: center; gap: 12px; }
.topbar__title { min-width: 0; }
.topbar__title strong { display: block; color: var(--slate-950); font-size: 15px; }
.topbar__title span { display: block; color: var(--slate-500); font-size: 11px; }
.icon-button {
  display: inline-grid;
  width: 40px;
  height: 40px;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--slate-600);
  background: var(--white);
  cursor: pointer;
  transition: border-color 160ms ease, color 160ms ease, background 160ms ease;
}
.icon-button:hover { color: var(--blue-600); border-color: #bfdbfe; background: var(--blue-50); }
.mobile-menu-button { display: none; }
.status-pill { display: inline-flex; align-items: center; gap: 7px; padding: 7px 10px; border: 1px solid #d1fae5; border-radius: 999px; color: #047857; background: #ecfdf5; font-size: 11px; font-weight: 700; }
.status-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--green-600); box-shadow: 0 0 0 4px rgba(5,150,105,0.1); }

body.sidebar-collapsed .sidebar { width: var(--sidebar-collapsed); }
body.sidebar-collapsed .main-shell { margin-left: var(--sidebar-collapsed); }
body.sidebar-collapsed .brand-copy,
body.sidebar-collapsed .nav-section,
body.sidebar-collapsed .nav-item__label,
body.sidebar-collapsed .nav-item__badge,
body.sidebar-collapsed .user-meta,
body.sidebar-collapsed .icon-button--sidebar { display: none; }
body.sidebar-collapsed .sidebar__brand { justify-content: center; padding-inline: 12px; }
body.sidebar-collapsed .nav-item { justify-content: center; padding-inline: 10px; }
body.sidebar-collapsed .user-card { display: flex; justify-content: center; grid-template-columns: none; padding: 9px 4px; }

.page-content { max-width: 1720px; margin: 0 auto; padding: 28px; }
.page-view { animation: fade-in 180ms ease; }
@keyframes fade-in { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
.page-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 18px; margin-bottom: 18px; }
.page-heading h1 { margin: 0 0 5px; color: var(--slate-950); font-size: clamp(22px, 2.1vw, 30px); letter-spacing: -0.6px; }
.page-heading p { margin: 0; color: var(--slate-500); font-size: 13px; }
.page-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 9px; }

.card {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--card);
  box-shadow: var(--shadow-sm);
}
.card__header { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; padding: 19px 20px 14px; }
.card__title { margin: 0; color: var(--slate-950); font-size: 14px; font-weight: 700; }
.card__subtitle { margin: 4px 0 0; color: var(--slate-500); font-size: 11px; }
.card__body { padding: 0 20px 20px; }

.filter-card { margin-bottom: 18px; padding: 16px; }
.filter-grid { display: grid; grid-template-columns: repeat(4, minmax(150px, 1fr)); gap: 12px; }
.field { min-width: 0; }
.field label { display: block; margin-bottom: 6px; color: var(--slate-600); font-size: 10px; font-weight: 800; letter-spacing: 0.05em; text-transform: uppercase; }
.field input, .field select, .field textarea {
  width: 100%;
  min-height: 40px;
  padding: 9px 11px;
  border: 1px solid var(--slate-200);
  border-radius: 8px;
  color: var(--slate-800);
  background: #fff;
  outline: none;
  transition: border 160ms ease, box-shadow 160ms ease;
}
.field textarea { min-height: 86px; resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus { border-color: #93c5fd; box-shadow: 0 0 0 3px rgba(37,99,235,0.1); }
.field input:disabled, .field select:disabled, .field textarea:disabled { color: var(--slate-500); background: var(--slate-100); cursor: not-allowed; }
.field--span-2 { grid-column: span 2; }

.kpi-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px; margin-bottom: 18px; }
.kpi-card { display: grid; min-height: 132px; grid-template-columns: minmax(0, 1fr) 54px; gap: 14px; padding: 20px; overflow: hidden; }
.kpi-label { color: var(--slate-600); font-size: 11px; font-weight: 700; }
.kpi-value { margin-top: 8px; color: var(--slate-950); font-size: clamp(22px, 2.2vw, 30px); font-weight: 800; letter-spacing: -0.7px; }
.kpi-sub { display: flex; align-items: center; gap: 5px; margin-top: 8px; color: var(--slate-500); font-size: 10px; font-weight: 600; }
.kpi-icon { display: grid; width: 54px; height: 54px; place-items: center; border-radius: 15px; font-size: 21px; }
.kpi-icon--blue { color: var(--blue-600); background: var(--blue-50); }
.kpi-icon--green { color: var(--green-600); background: #ecfdf5; }
.kpi-icon--amber { color: var(--amber-600); background: #fff7ed; }
.kpi-icon--violet { color: #7c3aed; background: #f5f3ff; }
.text-positive { color: var(--green-600) !important; }
.text-negative { color: var(--red-600) !important; }

.charts-grid { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); gap: 14px; margin-bottom: 18px; }
.chart-card { grid-column: span 6; min-height: 390px; }
.chart-card--wide { grid-column: span 12; min-height: 420px; }
.chart-card--third { grid-column: span 4; }
.chart-wrap { position: relative; width: 100%; height: 305px; }
.chart-card--wide .chart-wrap { height: 325px; }

.table-card { overflow: hidden; margin-bottom: 18px; }
.table-toolbar { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; padding: 0 20px 16px; }
.summary-grid { display: grid; grid-template-columns: repeat(4, minmax(0,1fr)); gap: 10px; padding: 0 20px 16px; }
.summary-box { padding: 13px; border: 1px solid var(--border); border-radius: 9px; background: var(--slate-50); }
.summary-box span { display: block; color: var(--slate-500); font-size: 9px; font-weight: 800; letter-spacing: .05em; text-transform: uppercase; }
.summary-box strong { display: block; margin-top: 5px; color: var(--slate-950); font-size: 17px; }
.table-scroll { width: 100%; overflow: auto; border-top: 1px solid var(--border); }
table { width: 100%; border-collapse: collapse; font-size: 11px; }
th, td { padding: 11px 13px; border-bottom: 1px solid #edf0f4; text-align: left; vertical-align: middle; white-space: nowrap; }
th { position: sticky; top: 0; z-index: 2; color: var(--slate-500); background: #fafbfc; font-size: 9px; font-weight: 800; letter-spacing: 0.04em; text-transform: uppercase; }
tbody tr:hover { background: #fafcff; }
td.is-wrap { min-width: 210px; white-space: normal; }
td.is-right, th.is-right { text-align: right; }
.empty-row td { padding: 34px; color: var(--slate-500); text-align: center; }

.button {
  display: inline-flex;
  min-height: 40px;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 8px 14px;
  border: 1px solid transparent;
  border-radius: 8px;
  cursor: pointer;
  font-size: 11px;
  font-weight: 700;
  transition: transform 150ms ease, box-shadow 150ms ease, background 150ms ease, border-color 150ms ease;
}
.button:hover:not(:disabled) { transform: translateY(-1px); }
.button:disabled { opacity: .55; cursor: not-allowed; }
.button--primary { color: #fff; background: var(--blue-600); box-shadow: 0 5px 14px rgba(37,99,235,.22); }
.button--primary:hover:not(:disabled) { background: var(--blue-700); }
.button--success { color: #fff; background: var(--green-600); }
.button--secondary { color: var(--slate-700); border-color: var(--slate-200); background: #fff; }
.button--secondary:hover:not(:disabled) { color: var(--blue-600); border-color: #bfdbfe; background: var(--blue-50); }
.button--danger { color: #b91c1c; border-color: #fecaca; background: #fff; }
.button--small { min-height: 32px; padding: 5px 10px; font-size: 10px; }

.notice { display: flex; align-items: flex-start; gap: 11px; margin-bottom: 16px; padding: 13px 15px; border: 1px solid #bfdbfe; border-radius: 10px; color: #1e40af; background: #eff6ff; font-size: 11px; }
.notice--warning { color: #92400e; border-color: #fde68a; background: #fffbeb; }
.notice--danger { color: #991b1b; border-color: #fecaca; background: #fef2f2; }
.notice i { margin-top: 1px; }

.form-card { margin-bottom: 18px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 14px; }
.form-actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 16px; }
.upload-dropzone { display: grid; min-height: 190px; place-items: center; padding: 26px; border: 2px dashed #bfdbfe; border-radius: 12px; background: linear-gradient(180deg, #f8fbff, #eff6ff); text-align: center; }
.upload-dropzone i { color: var(--blue-500); font-size: 34px; }
.upload-dropzone h3 { margin: 12px 0 4px; color: var(--slate-950); font-size: 15px; }
.upload-dropzone p { margin: 0 0 14px; color: var(--slate-500); font-size: 11px; }
.upload-meta { margin-top: 12px; color: var(--slate-500); font-size: 10px; }

.badge { display: inline-flex; align-items: center; gap: 5px; padding: 3px 7px; border-radius: 999px; font-size: 9px; font-weight: 800; }
.badge--green { color: #047857; background: var(--green-100); }
.badge--red { color: #b91c1c; background: var(--red-100); }
.badge--blue { color: #1d4ed8; background: var(--blue-100); }
.badge--slate { color: var(--slate-600); background: var(--slate-100); }

.loading-overlay {
  position: fixed;
  inset: 0;
  z-index: 999;
  display: none;
  place-items: center;
  background: rgba(248,250,252,0.84);
  backdrop-filter: blur(6px);
}
.loading-overlay.is-visible { display: grid; }
.loading-box { min-width: 210px; padding: 22px; border: 1px solid var(--border); border-radius: 14px; background: #fff; box-shadow: var(--shadow-md); text-align: center; }
.spinner { width: 38px; height: 38px; margin: 0 auto 12px; border: 4px solid var(--slate-200); border-top-color: var(--blue-600); border-radius: 50%; animation: spin 800ms linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.loading-box strong { display: block; color: var(--slate-800); font-size: 12px; }

.toast-region { position: fixed; z-index: 1000; right: 18px; bottom: 18px; display: grid; width: min(380px, calc(100vw - 36px)); gap: 9px; }
.toast { display: grid; grid-template-columns: 24px minmax(0,1fr) 24px; gap: 9px; align-items: start; padding: 13px; border: 1px solid var(--border); border-radius: 11px; background: #fff; box-shadow: var(--shadow-md); animation: toast-in 180ms ease; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }
.toast--success > i { color: var(--green-600); }
.toast--error > i { color: var(--red-600); }
.toast--info > i { color: var(--blue-600); }
.toast strong { display: block; color: var(--slate-800); font-size: 11px; }
.toast span { display: block; margin-top: 2px; color: var(--slate-500); font-size: 10px; }
.toast button { border: 0; color: var(--slate-400); background: transparent; cursor: pointer; }

.mobile-overlay { position: fixed; inset: 0; z-index: 55; display: none; background: rgba(2, 6, 23, .48); backdrop-filter: blur(2px); }
.mobile-overlay.is-visible { display: block; }

.error-state { display: grid; min-height: 58vh; place-items: center; text-align: center; }
.error-state__box { max-width: 520px; padding: 30px; }
.error-state i { color: var(--red-600); font-size: 38px; }
.error-state h1 { margin: 14px 0 7px; color: var(--slate-950); font-size: 22px; }
.error-state p { margin: 0 0 18px; color: var(--slate-500); font-size: 12px; }

@media (max-width: 1240px) {
  .kpi-grid { grid-template-columns: repeat(2, minmax(0,1fr)); }
  .chart-card--third { grid-column: span 6; }
  .filter-grid { grid-template-columns: repeat(2, minmax(0,1fr)); }
}

@media (max-width: 900px) {
  .sidebar { width: min(290px, 86vw); transform: translateX(-104%); }
  .sidebar.is-mobile-open { transform: translateX(0); }
  .main-shell, body.sidebar-collapsed .main-shell { margin-left: 0; }
  body.sidebar-collapsed .sidebar { width: min(290px, 86vw); }
  body.sidebar-collapsed .brand-copy,
  body.sidebar-collapsed .nav-section,
  body.sidebar-collapsed .nav-item__label,
  body.sidebar-collapsed .nav-item__badge,
  body.sidebar-collapsed .user-meta,
  body.sidebar-collapsed .icon-button--sidebar { display: initial; }
  body.sidebar-collapsed .sidebar__brand { justify-content: flex-start; padding-inline: 18px; }
  body.sidebar-collapsed .nav-item { justify-content: flex-start; padding-inline: 14px; }
  body.sidebar-collapsed .user-card { display: grid; grid-template-columns: 36px minmax(0,1fr) 34px; justify-content: normal; padding: 10px; }
  .desktop-collapse-button { display: none; }
  .mobile-menu-button { display: inline-grid; }
  .topbar { padding: 0 16px; }
  .page-content { padding: 20px 16px; }
  .page-heading { align-items: stretch; flex-direction: column; }
  .page-actions { justify-content: flex-start; }
  .chart-card, .chart-card--wide, .chart-card--third { grid-column: span 12; }
  .table-toolbar, .summary-grid { grid-template-columns: repeat(2, minmax(0,1fr)); }
}

@media (max-width: 620px) {
  .topbar__title span, .status-pill { display: none; }
  .page-content { padding: 16px 12px; }
  .filter-grid, .kpi-grid, .form-grid, .table-toolbar, .summary-grid { grid-template-columns: 1fr; }
  .field--span-2 { grid-column: span 1; }
  .kpi-card { min-height: 118px; }
  .chart-card, .chart-card--wide { min-height: 360px; }
  .chart-wrap, .chart-card--wide .chart-wrap { height: 280px; }
  .card__header { padding: 16px 15px 12px; }
  .card__body { padding: 0 15px 16px; }
  .table-toolbar, .summary-grid { padding-inline: 15px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; }
}

body.fatal-mode .main-shell { margin-left: 0 !important; }
