/* ==========================================================================
   NBTC Microwave — Pre-PM Local Control Room Design System
   Zero-dependency CSS with Dark/Light theme, Responsive Grid, & A4 Print Rules
   ========================================================================== */

@font-face {
  font-family: 'THSarabunNew';
  src: url('/assets/fonts/THSarabunNew.ttf') format('truetype');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: 'THSarabunNew';
  src: url('/assets/fonts/THSarabunNew-Bold.ttf') format('truetype');
  font-weight: 700;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: 'THSarabunNew';
  src: url('/assets/fonts/THSarabunNew-Italic.ttf') format('truetype');
  font-weight: 400;
  font-style: italic;
  font-display: swap;
}
@font-face {
  font-family: 'THSarabunNew';
  src: url('/assets/fonts/THSarabunNew-BoldItalic.ttf') format('truetype');
  font-weight: 700;
  font-style: italic;
  font-display: swap;
}

:root {
  --font-ui: 'Noto Sans Thai', 'Sarabun', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --font-report: 'THSarabunNew', 'TH Sarabun New', 'Sarabun', sans-serif;
  --font-mono: ui-monospace, 'SFMono-Regular', Menlo, Consolas, monospace;

  --bg-page: radial-gradient(ellipse at top right, rgba(8, 105, 255, 0.16), transparent 55%),
             linear-gradient(135deg, #020b1b 0%, #041937 50%, #020f25 100%);
  --bg-surface: rgba(7, 23, 46, 0.78);
  --bg-surface-solid: #07172e;
  --bg-surface-alt: rgba(12, 33, 64, 0.72);
  --bg-input: rgba(5, 16, 33, 0.85);
  --bg-nav: linear-gradient(90deg, #021735, #031b40);
  --border: rgba(56, 139, 253, 0.22);
  --border-strong: rgba(56, 189, 248, 0.4);
  --text: #edf6ff;
  --text-muted: #94a3b8;
  --text-soft: #cbd5e1;
  --accent: #06b6d4;
  --accent-strong: #2563eb;
  --emerald: #10b981;
  --emerald-bg: rgba(16, 185, 129, 0.14);
  --rose: #f43f5e;
  --rose-bg: rgba(244, 63, 94, 0.14);
  --amber: #f59e0b;
  --amber-bg: rgba(245, 158, 11, 0.14);
  --shadow-card: 0 12px 32px rgba(0, 0, 0, 0.35);
}

body.theme-light {
  --bg-page: radial-gradient(ellipse at top right, rgba(14, 165, 233, 0.12), transparent 55%),
             linear-gradient(135deg, #f8fafc 0%, #eef2f7 50%, #e2e8f0 100%);
  --bg-surface: rgba(255, 255, 255, 0.94);
  --bg-surface-solid: #ffffff;
  --bg-surface-alt: #f8fafc;
  --bg-input: #ffffff;
  --bg-nav: linear-gradient(90deg, #ffffff, #f8fafc);
  --border: rgba(15, 23, 42, 0.12);
  --border-strong: rgba(2, 132, 199, 0.35);
  --text: #0f172a;
  --text-muted: #64748b;
  --text-soft: #334155;
  --accent: #0284c7;
  --accent-strong: #1d4ed8;
  --emerald: #059669;
  --emerald-bg: rgba(16, 185, 129, 0.12);
  --rose: #e11d48;
  --rose-bg: rgba(225, 29, 72, 0.1);
  --amber: #d97706;
  --amber-bg: rgba(245, 158, 11, 0.12);
  --shadow-card: 0 8px 24px rgba(15, 23, 42, 0.08);
}

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

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  font-family: var(--font-ui);
  background: var(--bg-page);
  background-attachment: fixed;
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

a { color: inherit; text-decoration: none; }
button, input, select, textarea { font-family: inherit; }

/* Boot loader */
.boot-loader {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  color: var(--text-muted);
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.75s linear infinite;
}
.spinner-sm {
  width: 16px;
  height: 16px;
  border-width: 2px;
  display: inline-block;
  vertical-align: middle;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* App Shell */
.shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.navbar {
  position: sticky;
  top: 0;
  z-index: 40;
  height: 74px;
  background: var(--bg-nav);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(14px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  gap: 12px;
}

.nav-left, .nav-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}
.brand img {
  height: 42px;
  width: auto;
  object-fit: contain;
}
.brand-sub {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.14em;
  color: var(--accent);
  text-transform: uppercase;
}
.brand-title {
  font-size: 16px;
  font-weight: 800;
  line-height: 1.2;
}

.nav-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 11px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg-surface-alt);
  font-size: 12px;
  color: var(--text-soft);
}
.dot-live {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--emerald);
  box-shadow: 0 0 8px var(--emerald);
}
.dot-offline {
  background: var(--amber);
  box-shadow: 0 0 8px var(--amber);
}

/* Body Layout */
.layout {
  display: flex;
  flex: 1;
}

.sidebar {
  width: 248px;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  background: var(--bg-surface);
  backdrop-filter: blur(14px);
  padding: 18px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  transition: width 0.2s ease, transform 0.2s ease;
}
.sidebar.collapsed {
  display: none;
}

.sidebar-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 44px;
  padding: 10px 14px;
  border-radius: 12px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-soft);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  text-align: left;
  transition: all 0.15s ease;
}
.sidebar-btn:hover {
  background: var(--bg-surface-alt);
  color: var(--text);
}
.sidebar-btn.active {
  background: linear-gradient(135deg, rgba(6, 182, 212, 0.18), rgba(37, 99, 235, 0.2));
  border-color: var(--border-strong);
  color: var(--accent);
}
.sidebar-footer {
  margin-top: auto;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.main-content {
  flex: 1;
  min-width: 0;
  padding: 20px 16px 40px;
}
@media (min-width: 1024px) {
  .main-content {
    padding: 24px 28px 48px;
  }
}

.container {
  max-width: 1320px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.container-narrow {
  max-width: 900px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* Mobile Sidebar Drawer */
.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(4px);
  z-index: 45;
  display: none;
}
.drawer-backdrop.open { display: block; }

@media (max-width: 1023px) {
  .sidebar {
    position: fixed;
    top: 74px;
    left: 0;
    bottom: 0;
    z-index: 50;
    transform: translateX(-105%);
    display: flex !important;
  }
  .sidebar.mobile-open {
    transform: translateX(0);
  }
  .hide-mobile { display: none !important; }
}

/* Cards & Typography */
.card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 18px;
  box-shadow: var(--shadow-card);
  backdrop-filter: blur(12px);
}
.card-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-bottom: 12px;
  margin-bottom: 14px;
  border-bottom: 1px solid var(--border);
}
.card-title {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 16px;
  font-weight: 700;
  margin: 0;
}

.icon-box {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: rgba(6, 182, 212, 0.14);
  color: var(--accent);
  border: 1px solid var(--border-strong);
  flex-shrink: 0;
}
.icon-box.emerald { background: var(--emerald-bg); color: var(--emerald); border-color: rgba(16,185,129,0.35); }
.icon-box.rose { background: var(--rose-bg); color: var(--rose); border-color: rgba(244,63,94,0.35); }
.icon-box.amber { background: var(--amber-bg); color: var(--amber); border-color: rgba(245,158,11,0.35); }

/* Buttons & Inputs */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 40px;
  padding: 8px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-surface-alt);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
  white-space: nowrap;
}
.btn:hover:not(:disabled) { border-color: var(--border-strong); transform: translateY(-1px); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-icon {
  min-width: 40px;
  min-height: 40px;
  padding: 8px;
}
.btn-primary {
  background: linear-gradient(135deg, #2563eb, #06b6d4);
  border-color: transparent;
  color: #ffffff;
}
.btn-emerald {
  background: linear-gradient(135deg, #059669, #10b981);
  border-color: transparent;
  color: #ffffff;
}
.btn-danger {
  background: var(--rose-bg);
  border-color: rgba(244, 63, 94, 0.45);
  color: var(--rose);
}
.btn-amber {
  background: var(--amber-bg);
  border-color: rgba(245, 158, 11, 0.45);
  color: var(--amber);
}
.btn-sm {
  min-height: 32px;
  padding: 5px 10px;
  font-size: 12px;
  border-radius: 8px;
}

.input, .select, .textarea {
  width: 100%;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text);
  padding: 10px 12px;
  font-size: 14px;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.input:focus, .select:focus, .textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(6, 182, 212, 0.18);
}
.input:disabled, .select:disabled, .textarea:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}
.label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-soft);
  margin-bottom: 6px;
}
.req { color: var(--rose); }

/* Badges */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  border: 1px solid var(--border);
  background: var(--bg-surface-alt);
}
.badge-emerald { background: var(--emerald-bg); color: var(--emerald); border-color: rgba(16,185,129,0.4); }
.badge-rose { background: var(--rose-bg); color: var(--rose); border-color: rgba(244,63,94,0.4); }
.badge-amber { background: var(--amber-bg); color: var(--amber); border-color: rgba(245,158,11,0.4); }
.badge-cyan { background: rgba(6,182,212,0.14); color: var(--accent); border-color: rgba(6,182,212,0.4); }

/* Grid Helpers */
.grid-4 { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; }
.grid-rounds { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 10px; }

@media (max-width: 1100px) {
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-rounds { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
@media (max-width: 767px) {
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
  .grid-rounds { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 520px) {
  .grid-4 { grid-template-columns: 1fr; }
}

/* KPI Card */
.kpi-card {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 8px;
}
.kpi-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 600;
}
.kpi-value {
  font-size: 30px;
  font-weight: 800;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}
.kpi-sub {
  font-size: 12px;
  color: var(--text-muted);
}

/* Round Card */
.round-tile {
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-surface-alt);
  cursor: pointer;
  text-align: left;
  color: var(--text);
  transition: all 0.15s ease;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.round-tile:hover { border-color: var(--border-strong); }
.round-tile.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(6, 182, 212, 0.22);
}
.round-tile-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  font-weight: 700;
}
.progress-track {
  width: 100%;
  height: 6px;
  border-radius: 999px;
  background: rgba(148, 163, 184, 0.22);
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, #2563eb, #06b6d4);
  transition: width 0.25s ease;
}
.progress-fill.done { background: linear-gradient(90deg, #059669, #10b981); }
.progress-fill.closed { background: var(--rose); }

/* Charts */
.bar-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 9px;
  font-size: 12.5px;
}
.bar-label {
  width: 112px;
  flex-shrink: 0;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.bar-track {
  flex: 1;
  height: 12px;
  border-radius: 999px;
  background: rgba(148, 163, 184, 0.18);
  overflow: hidden;
}
.bar-val {
  width: 72px;
  text-align: right;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text-soft);
}

.donut-wrap {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-around;
  gap: 18px;
}
.donut-svg-box {
  position: relative;
  width: 168px;
  height: 168px;
}
.donut-center {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}
.donut-legend {
  flex: 1;
  min-width: 210px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.legend-btn {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 9px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-surface-alt);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
}
.legend-btn:hover { border-color: var(--border-strong); }

/* Tables */
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--border);
  border-radius: 12px;
}
.table {
  width: 100%;
  min-width: 680px;
  border-collapse: collapse;
  font-size: 13px;
  text-align: left;
}
.table th {
  padding: 11px 12px;
  background: var(--bg-surface-alt);
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
  white-space: nowrap;
}
.table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.table tr:last-child td { border-bottom: none; }
.table tbody tr:hover { background: rgba(56, 189, 248, 0.05); }

.pagination {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 12px;
  font-size: 12.5px;
  color: var(--text-muted);
}

/* Radio & Upload */
.radio-cards { display: flex; flex-wrap: wrap; gap: 12px; }
.radio-card {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 18px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-input);
  cursor: pointer;
  font-weight: 600;
  font-size: 14px;
}
.radio-card.selected-emerald {
  border-color: var(--emerald);
  background: var(--emerald-bg);
  color: var(--emerald);
}
.radio-card.selected-rose {
  border-color: var(--rose);
  background: var(--rose-bg);
  color: var(--rose);
}

.dropzone {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 2px dashed var(--border-strong);
  border-radius: 14px;
  padding: 24px 16px;
  text-align: center;
  cursor: pointer;
  background: rgba(6, 182, 212, 0.05);
}
.dropzone:hover { background: rgba(6, 182, 212, 0.1); }

.photo-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 12px;
  margin-top: 12px;
}
.photo-thumb {
  position: relative;
  aspect-ratio: 1 / 1;
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: #0f172a;
}
.photo-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.photo-thumb-actions {
  position: absolute;
  top: 6px;
  right: 6px;
  display: flex;
  gap: 4px;
}
.photo-thumb-cap {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 4px 6px;
  font-size: 11px;
  color: #fff;
  background: linear-gradient(transparent, rgba(0,0,0,0.85));
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Alerts & Login */
.alert {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  font-size: 13px;
  line-height: 1.5;
}
.alert-rose { background: var(--rose-bg); border-color: rgba(244,63,94,0.45); color: var(--rose); }
.alert-emerald { background: var(--emerald-bg); border-color: rgba(16,185,129,0.45); color: var(--emerald); }
.alert-amber { background: var(--amber-bg); border-color: rgba(245,158,11,0.45); color: var(--amber); }
.alert-cyan { background: rgba(6,182,212,0.12); border-color: rgba(6,182,212,0.4); color: var(--accent); }

.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
}
.login-card {
  width: 100%;
  max-width: 430px;
}

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(2, 6, 23, 0.78);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px;
}
.modal-box {
  width: 100%;
  max-width: 920px;
  max-height: 92vh;
  overflow-y: auto;
  background: var(--bg-surface-solid);
  border: 1px solid var(--border-strong);
  border-radius: 18px;
  padding: 20px;
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.55);
  color: var(--text);
}
.modal-box.modal-sm { max-width: 480px; }
.modal-box.modal-xl { max-width: 1060px; }

.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
}
.tab-btn {
  padding: 8px 13px;
  border-radius: 10px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}
.tab-btn.active {
  background: rgba(6, 182, 212, 0.16);
  border-color: var(--border-strong);
  color: var(--accent);
}

/* Toast */
.toast-host {
  position: fixed;
  bottom: 18px;
  right: 18px;
  z-index: 80;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 360px;
}
.toast {
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--border-strong);
  background: var(--bg-surface-solid);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.4);
}
.toast.success { border-color: var(--emerald); }
.toast.error { border-color: var(--rose); }
.toast.warning { border-color: var(--amber); }

/* ==========================================================================
   A4 Official Report Styling (Screen Preview + Print to PDF)
   ========================================================================== */
.a4-sheet {
  width: 210mm;
  min-height: 296mm;
  margin: 0 auto 16px;
  padding: 12mm 14mm;
  background: #ffffff;
  color: #000000;
  font-family: var(--font-report);
  font-size: 15px;
  line-height: 1.35;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.35);
  box-sizing: border-box;
}
.a4-sheet table {
  width: 100%;
  border-collapse: collapse;
  color: #000000;
}
.a4-sheet th, .a4-sheet td {
  border: 1px solid #000000;
  padding: 4px 8px;
  vertical-align: top;
}
.a4-section-bar {
  background: #f1f5f9;
  border: 1px solid #000000;
  font-weight: 700;
  padding: 3px 8px;
  font-size: 15.5px;
  margin-top: 8px;
}
.a4-photo-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-top: 10px;
}
.a4-photo-box {
  border: 1px solid #000;
  padding: 6px;
  text-align: center;
  break-inside: avoid;
}
.a4-photo-box img {
  max-width: 100%;
  max-height: 82mm;
  object-fit: contain;
  display: block;
  margin: 0 auto 4px;
}
.a4-page-break {
  page-break-before: always;
  break-before: page;
}

@media (max-width: 820px) {
  .a4-sheet {
    width: 100%;
    min-height: auto;
    padding: 12px;
  }
}

#print-root { display: none; }

@media print {
  @page {
    size: A4 portrait;
    margin: 8mm;
  }
  body {
    background: #ffffff !important;
    color: #000000 !important;
  }
  #app, #modal-root, #toast-root {
    display: none !important;
  }
  #print-root {
    display: block !important;
  }
  .a4-sheet {
    width: 100% !important;
    min-height: auto !important;
    margin: 0 !important;
    padding: 4mm 6mm !important;
    box-shadow: none !important;
  }
}
