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

:root {
  --font: 'IBM Plex Sans Thai', sans-serif;
  --mono: 'IBM Plex Mono', monospace;
  --bg: #F7F6F3;
  --surface: #FFFFFF;
  --surface2: #F0EFEb;
  --border: rgba(0,0,0,0.08);
  --border-strong: rgba(0,0,0,0.15);
  --text: #1A1A18;
  --text2: #6B6B67;
  --text3: #9B9B96;
  --blue: #1A56DB;
  --blue-bg: #EBF2FF;
  --blue-text: #1e40af;
  --purple: #6B3FA0;
  --purple-bg: #F3EAFE;
  --purple-text: #5B21B6;
  --green: #2D7A3A;
  --green-bg: #EDFAF1;
  --green-text: #166534;
  --red: #C0392B;
  --red-bg: #FEF2F2;
  --amber-bg: #FFFBEB;
  --amber-text: #92400E;
  --dealer-bg: #EBF2FF;
  --dealer-text: #1e40af;
  --online-bg: #EDFAF1;
  --online-text: #166534;
  --sarasin-bg: #FFFBEB;
  --sarasin-text: #92400E;
  --sarasin-m-bg: #F5F0FF;
  --sarasin-m-text: #5B21B6;
  --rama-bg: #FFF1EE;
  --rama-text: #9A3412;
  --rama-m-bg: #FCE7F3;
  --rama-m-text: #9D174D;
  --fit-bg: #ECFDF5;
  --fit-text: #065F46;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
}

html, body { height: 100%; font-family: var(--font); background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.5; }

/* ===== SCREENS ===== */
.screen { display: none; min-height: 100vh; }
.screen.active { display: flex; flex-direction: column; }

/* ===== UPLOAD ===== */
.upload-center {
  margin: auto;
  width: 100%;
  max-width: 520px;
  padding: 60px 24px 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}
.logo-mark {
  width: 52px; height: 52px;
  background: var(--text); color: #fff;
  font-size: 18px; font-weight: 600;
  border-radius: 14px;
  display: flex; align-items: center; justify-content: center;
  letter-spacing: 1px;
}
.upload-title { font-size: 22px; font-weight: 600; text-align: center; }
.upload-sub { font-size: 14px; color: var(--text2); text-align: center; margin-top: -8px; }
.upload-box {
  width: 100%;
  border: 1.5px dashed var(--border-strong);
  border-radius: var(--radius);
  padding: 40px 24px;
  text-align: center;
  cursor: pointer;
  transition: border-color .2s, background .2s;
  background: var(--surface);
}
.upload-box:hover, .upload-box.drag-over { border-color: var(--blue); background: var(--blue-bg); }
.upload-icon { color: var(--text3); margin-bottom: 12px; }
.upload-hint { font-size: 14px; color: var(--text2); }
.upload-hint-sub { font-size: 12px; color: var(--text3); margin-top: 4px; }
.link-btn { color: var(--blue); cursor: pointer; text-decoration: underline; }
.upload-status {
  width: 100%; padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-size: 13px;
}
.upload-status.ok { background: var(--green-bg); color: var(--green-text); }
.upload-status.err { background: var(--red-bg); color: var(--red); }
.upload-meta {
  display: flex; gap: 16px; flex-wrap: wrap; justify-content: center;
  font-size: 12px; color: var(--text2);
}
.meta-item { display: flex; align-items: center; gap: 6px; }
.meta-dot { width: 8px; height: 8px; border-radius: 50%; }
.dot-peak { background: var(--blue); }
.dot-order { background: var(--green); }
.dot-cn { background: var(--red); }
.dot-agent { background: var(--purple); }
.history-section { width: 100%; }
.history-title { font-size: 13px; font-weight: 500; color: var(--text2); margin-bottom: 10px; }
.history-list { display: flex; flex-direction: column; gap: 8px; }
.history-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-sm); cursor: pointer;
  transition: border-color .15s;
}
.history-item:hover { border-color: var(--blue); }
.history-item-label { font-size: 13px; font-weight: 500; }
.history-item-meta { font-size: 11px; color: var(--text3); }

/* ===== PROCESSING ===== */
.processing-center { margin: auto; text-align: center; padding: 60px; }
.spinner {
  width: 36px; height: 36px; margin: 0 auto 16px;
  border: 2.5px solid var(--border); border-top-color: var(--text);
  border-radius: 50%; animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.processing-msg { font-size: 14px; color: var(--text2); }

/* ===== HEADER ===== */
.header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 28px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.header-left { display: flex; align-items: center; gap: 12px; }
.header-logo {
  width: 34px; height: 34px; background: var(--text); color: #fff;
  font-size: 13px; font-weight: 600; border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
}
.header-title { font-size: 15px; font-weight: 600; }
.header-period { font-size: 12px; color: var(--text2); }
.header-right { display: flex; gap: 10px; }

/* ===== BUTTONS ===== */
.btn-primary {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 16px; font-size: 13px; font-family: var(--font); font-weight: 500;
  background: #1D6F42; color: #fff; border: none;
  border-radius: var(--radius-sm); cursor: pointer; transition: background .15s;
}
.btn-primary:hover { background: #155232; }
.btn-outline {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 14px; font-size: 13px; font-family: var(--font);
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm); cursor: pointer; transition: background .15s;
}
.btn-outline:hover { background: var(--bg); }
.btn-ghost {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 6px 12px; font-size: 13px; font-family: var(--font);
  background: transparent; color: var(--text2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm); cursor: pointer; transition: background .15s;
}
.btn-ghost:hover { background: var(--surface); }
.btn-sm { padding: 4px 10px; font-size: 12px; }

/* ===== DASHBOARD BODY ===== */
.dash-body { padding: 20px 28px 40px; flex: 1; }

/* ===== FILTERS ===== */
.filters-bar {
  display: flex; align-items: flex-end; gap: 16px; flex-wrap: wrap;
  margin-bottom: 18px;
}
.filter-group { display: flex; flex-direction: column; gap: 4px; }
.filter-label { font-size: 11px; color: var(--text3); font-weight: 500; text-transform: uppercase; letter-spacing: .04em; }
.filter-select {
  padding: 7px 10px; font-size: 13px; font-family: var(--font);
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  min-width: 140px; cursor: pointer;
}
.filter-select:focus { outline: none; border-color: var(--blue); }

/* ===== KPI ===== */
.kpi-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px; margin-bottom: 20px; }
.kpi-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px 16px;
}
.kpi-main { background: var(--blue-bg); border-color: rgba(26,86,219,0.2); }
.kpi-main .kpi-value { color: var(--blue-text); }
.kpi-maint { background: var(--purple-bg); border-color: rgba(107,63,160,0.2); }
.kpi-maint .kpi-value { color: var(--purple-text); }
.kpi-fit { background: var(--fit-bg); border-color: rgba(6,95,70,0.2); }
.kpi-fit .kpi-value { color: var(--fit-text); }
.kpi-cn .kpi-value { color: var(--red); }
.kpi-label { font-size: 11px; color: var(--text2); font-weight: 500; margin-bottom: 4px; }
.kpi-value { font-size: 20px; font-weight: 600; font-family: var(--mono); letter-spacing: -.02em; }
.kpi-sub { font-size: 11px; color: var(--text3); margin-top: 2px; }

/* ===== SECTION ===== */
.section-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 8px;
}
.section-title { font-size: 13px; font-weight: 600; color: var(--text); text-transform: uppercase; letter-spacing: .04em; }
.section-note { font-size: 11px; color: var(--text3); }

/* ===== TABLE ===== */
.table-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden; margin-bottom: 16px;
  box-shadow: var(--shadow);
}
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; table-layout: fixed; }
.data-table th {
  text-align: left; font-weight: 500; font-size: 11px; color: var(--text2);
  padding: 9px 12px; border-bottom: 1px solid var(--border);
  background: #FAFAF8; text-transform: uppercase; letter-spacing: .03em;
}
.data-table td { padding: 8px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.data-table tr:last-child td { border-bottom: none; }
.num { text-align: right; font-family: var(--mono); font-size: 12px; }

/* dept rows */
.row-dept-main td { background: #F7F6F3; font-weight: 600; cursor: pointer; }
.row-dept-main:hover td { background: #EEEEE9; }
.row-dept-sub td { background: #FBFAF9; font-weight: 500; cursor: pointer; }
.row-dept-sub:hover td { background: #F2F1EE; }
.row-agent td { }
.row-agent:hover td { background: #FAFAF8; }
.row-agent td:first-child { padding-left: 28px; color: var(--text2); }
.row-agent.hide { display: none; }
.row-total td { background: #F7F6F3; font-weight: 600; border-top: 1.5px solid var(--border-strong); }

.indent-bar {
  display: inline-block; width: 12px; height: 2px;
  background: var(--border-strong); margin-right: 6px;
  vertical-align: middle;
}
.chv {
  display: inline-block; font-size: 11px; margin-right: 4px;
  color: var(--text3); transition: transform .15s; font-style: normal;
}
.chv.open { transform: rotate(90deg); }

/* pill badges */
.pill {
  display: inline-block; font-size: 10px; padding: 2px 8px;
  border-radius: 20px; font-weight: 500; letter-spacing: .02em;
}
.pill-dealer { background: var(--dealer-bg); color: var(--dealer-text); }
.pill-online { background: var(--online-bg); color: var(--online-text); }
.pill-sarasin { background: var(--sarasin-bg); color: var(--sarasin-text); }
.pill-sarasin-m { background: var(--sarasin-m-bg); color: var(--sarasin-m-text); }
.pill-rama { background: var(--rama-bg); color: var(--rama-text); }
.pill-rama-m { background: var(--rama-m-bg); color: var(--rama-m-text); }
.pill-fit { background: var(--fit-bg); color: var(--fit-text); }

.cn-val { color: var(--red); }
.net-val { color: var(--blue-text); font-weight: 600; }
.so-link { color: var(--blue); font-family: var(--mono); font-size: 11px; }

.hidden { display: none !important; }

/* ===== HISTORY ACTIONS ===== */
.history-header { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 10px; }
.history-subtitle { font-size: 11px; color: var(--text3); }
.history-item { display: flex; align-items: center; gap: 10px; padding: 10px 14px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); }
.history-item:hover { border-color: var(--border-strong); }
.history-item-info { flex: 1; cursor: pointer; }
.history-item-actions { display: flex; gap: 6px; flex-shrink: 0; }
.btn-action {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 5px 10px; font-size: 12px; font-family: var(--font);
  border-radius: var(--radius-sm); cursor: pointer; border: 1px solid var(--border);
  background: var(--surface); color: var(--text2); transition: all .15s;
}
.btn-action:hover { border-color: var(--border-strong); }
.btn-replace:hover { background: var(--blue-bg); border-color: rgba(26,86,219,0.3); color: var(--blue-text); }
.btn-delete:hover  { background: var(--red-bg); border-color: rgba(192,57,43,0.25); color: var(--red); }

/* ===== SYNC STATUS ===== */
.sync-bar {
  display: flex; align-items: center; gap: 8px;
  font-size: 12px; color: var(--text3);
  padding: 6px 0 0;
}
.sync-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--green); flex-shrink: 0; }
.sync-dot.syncing { background: var(--amber-text); animation: pulse .8s ease-in-out infinite; }
.sync-dot.err { background: var(--red); }
@keyframes pulse { 0%,100%{opacity:1} 50%{opacity:.4} }

/* ===== RESPONSIVE ===== */
@media (max-width: 768px) {
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .header { padding: 12px 16px; }
  .dash-body { padding: 16px; }
}
