:root {
  --bg: #F4F6F8;
  --surface: #FFFFFF;
  --surface-alt: #FAFBFC;
  --border: #E1E6EB;
  --border-strong: #C7D0D9;
  --text: #172033;
  --text-muted: #5E6B7A;
  --accent: #1D5C74;
  --accent-bright: #2C87A6;
  --accent-soft: #E4EEF1;
  --green: #1E8E5A;
  --green-soft: #E6F5EC;
  --amber: #B4790A;
  --amber-soft: #FBF0DC;
  --red: #C4392B;
  --red-soft: #FBE7E4;
  --bg-glow: rgba(29,92,116,0.07);
  --bg-grid: rgba(23,32,51,0.035);
  --shadow: 0 1px 2px rgba(23,32,51,0.05), 0 6px 16px rgba(23,32,51,0.06);
  --radius: 14px;
  --radius-sm: 8px;
  --font-display: 'Space Grotesk', ui-sans-serif, system-ui, sans-serif;
  --font-body: 'Inter', ui-sans-serif, system-ui, -apple-system, 'Segoe UI', sans-serif;
}

[data-theme="dark"] {
  --bg: #10151B;
  --surface: #171F28;
  --surface-alt: #1D2733;
  --border: #2A3441;
  --border-strong: #3A4756;
  --text: #E7ECF1;
  --text-muted: #94A3B3;
  --accent: #4FA8C4;
  --accent-bright: #6FC3DD;
  --accent-soft: rgba(79,168,196,0.14);
  --green: #3FBE81;
  --green-soft: rgba(63,190,129,0.14);
  --amber: #E3A63B;
  --amber-soft: rgba(227,166,59,0.14);
  --red: #E2685A;
  --red-soft: rgba(226,104,90,0.14);
  --bg-glow: rgba(79,168,196,0.09);
  --bg-grid: rgba(231,236,241,0.045);
  --shadow: 0 1px 2px rgba(0,0,0,0.3), 0 10px 28px rgba(0,0,0,0.4);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background-color: var(--bg);
  background-image:
    radial-gradient(ellipse 1100px 480px at 50% -12%, var(--bg-glow), transparent 65%),
    linear-gradient(var(--bg-grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--bg-grid) 1px, transparent 1px);
  background-size: 100% 100%, 34px 34px, 34px 34px;
  background-attachment: fixed;
  color: var(--text);
  font-family: var(--font-body);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  transition: background-color 0.2s ease, color 0.2s ease;
}

.sr-only {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

a, button, input, select { font-family: inherit; color: inherit; }
button { cursor: pointer; }

:focus-visible {
  outline: 2px solid var(--accent-bright);
  outline-offset: 2px;
  border-radius: 4px;
}

.shell {
  max-width: 1320px;
  margin: 0 auto;
  padding: 20px 24px 48px;
}

/* ---------- Topbar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding-bottom: 18px;
  flex-wrap: wrap;
}

.brand { display: flex; align-items: center; gap: 10px; }

.brand-mark {
  width: 34px; height: 34px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.brand-mark img {
  width: 100%; height: 100%;
  object-fit: contain;
}

.brand-name {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 16px;
  letter-spacing: 0.01em;
}

.brand-sub {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: -2px;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 14px;
}

.clock {
  text-align: right;
  font-size: 12.5px;
  color: var(--text-muted);
  line-height: 1.4;
}

.clock-time {
  font-family: var(--font-display);
  font-weight: 600;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
}

.theme-toggle {
  width: 36px; height: 36px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  display: flex; align-items: center; justify-content: center;
  box-shadow: var(--shadow);
  transition: border-color 0.15s ease;
}
.theme-toggle:hover { border-color: var(--accent-bright); }
.theme-toggle svg { width: 17px; height: 17px; stroke: var(--text-muted); }

.admin-link {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--accent);
  text-decoration: none;
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid var(--accent-soft);
  background: var(--accent-soft);
}
.admin-link:hover { border-color: var(--accent-bright); }

.user-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 12px 5px 5px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  text-decoration: none;
  color: var(--text);
  box-shadow: var(--shadow);
}
.user-chip:hover { border-color: var(--border-strong); }
.user-avatar {
  width: 26px; height: 26px;
  border-radius: 999px;
  background: linear-gradient(145deg, var(--accent), var(--accent-bright));
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.user-email { font-size: 12.5px; font-weight: 500; }
@media (max-width: 640px) { .user-email { display: none; } }

/* ---------- Page head ---------- */
.page-head { padding: 6px 0 22px; }
.page-head h1 {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: clamp(22px, 3vw, 30px);
  letter-spacing: 0.01em;
  margin: 0 0 6px;
}
.page-head p {
  margin: 0;
  color: var(--text-muted);
  font-size: 13.5px;
  max-width: 560px;
}
.head-rule {
  height: 3px; width: 46px;
  border-radius: 3px;
  background: linear-gradient(90deg, var(--accent), var(--accent-bright));
  margin: 12px 0 0;
}

/* ---------- Vitals ---------- */
.vitals {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
  margin-bottom: 22px;
}

.vital-card {
  text-align: left;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 6px;
  animation: fadeUp 0.4s ease both;
}
.vital-card.is-active { border-color: var(--accent-bright); }

.vital-top { display: flex; align-items: center; justify-content: space-between; }
.vital-label {
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  font-weight: 600;
}
.vital-number {
  font-family: var(--font-display);
  font-size: 30px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.vital-card[data-key="green"] .vital-number { color: var(--green); }
.vital-card[data-key="amber"] .vital-number { color: var(--amber); }
.vital-card[data-key="red"] .vital-number { color: var(--red); }

.dot {
  width: 8px; height: 8px; border-radius: 999px; flex-shrink: 0;
}
.dot.green { background: var(--green); }
.dot.amber { background: var(--amber); }
.dot.red { background: var(--red); }
.dot.pulse {
  box-shadow: 0 0 0 0 rgba(196,57,43,0.55);
  animation: pulse 1.8s infinite;
}

@keyframes pulse {
  0%   { box-shadow: 0 0 0 0 rgba(196,57,43,0.5); }
  70%  { box-shadow: 0 0 0 8px rgba(196,57,43,0); }
  100% { box-shadow: 0 0 0 0 rgba(196,57,43,0); }
}
@keyframes fadeUp {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ---------- Toolbar ---------- */
.toolbar {
  align-items: flex-end;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}
.toolbar:not([hidden]) { display: flex; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  font-weight: 600;
}

.search-field { flex: 1 1 260px; position: relative; }
.search-field svg {
  position: absolute; left: 12px; top: 50%; transform: translateY(-50%);
  width: 16px; height: 16px; stroke: var(--text-muted); pointer-events: none;
}
.search-field input {
  width: 100%;
  padding: 10px 12px 10px 36px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 13.5px;
}
.search-field input::placeholder { color: var(--text-muted); }

.reset-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
}
.reset-btn:hover { color: var(--text); border-color: var(--border-strong); }

.download-btn {
  align-items: center;
  gap: 8px;
  border: 1px solid var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  font-size: 13px;
  font-weight: 600;
}
.download-btn:not([hidden]) { display: flex; }
.download-btn:hover { filter: brightness(0.97); }
.download-btn:disabled { opacity: 0.6; }
.download-btn svg { width: 16px; height: 16px; flex-shrink: 0; }

/* ---------- Kelola User: create form + per-row actions ---------- */
.create-user-form {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 16px;
}
.create-user-form:not([hidden]) { display: flex; }
.create-user-form .field { flex: 1 1 200px; min-width: 160px; }
.create-user-form input,
.create-user-form select {
  width: 100%;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 13.5px;
  font-family: var(--font-body);
}
.create-user-actions { display: flex; gap: 8px; flex-shrink: 0; }
.create-user-actions .auth-btn { width: auto; padding: 10px 18px; }
.create-user-form .form-error { flex-basis: 100%; margin: 0; text-align: left; }

.row-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.row-action-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  border-radius: 6px;
  padding: 5px 9px;
  font-size: 11.5px;
  font-weight: 600;
  white-space: nowrap;
}
.row-action-btn:hover { color: var(--text); border-color: var(--border-strong); }
.row-action-btn.danger { color: var(--red); border-color: var(--red); }
.row-action-btn.danger:hover { background: var(--red-soft); }
.row-action-btn:disabled { opacity: 0.5; cursor: default; }

.result-count {
  font-size: 12.5px;
  color: var(--text-muted);
  margin: 0 0 10px;
}
.result-count strong { color: var(--text); font-weight: 600; }

/* ---------- Update data (runs RUN_PROGRAM.py) ---------- */
.update-data-row {
  display: flex;
  align-items: flex-start;
  gap: 14px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}

.update-btn {
  align-items: center;
  gap: 8px;
  border: none;
  border-radius: var(--radius-sm);
  background: linear-gradient(135deg, var(--accent), var(--accent-bright));
  color: #fff;
  padding: 10px 16px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.03em;
  box-shadow: var(--shadow);
  flex-shrink: 0;
}
.update-btn:not([hidden]) { display: flex; }
.update-btn:hover { filter: brightness(1.05); }
.update-btn:disabled { opacity: 0.6; cursor: default; filter: none; }
.update-btn svg { width: 15px; height: 15px; }

.update-progress-ring {
  position: relative;
  width: 56px;
  height: 56px;
  flex-shrink: 0;
}
.update-progress-ring:not([hidden]) { display: block; }
.update-progress-ring svg { transform: rotate(-90deg); }
.ring-track {
  fill: none;
  stroke: var(--surface-alt);
  stroke-width: 5;
}
.ring-fill {
  fill: none;
  stroke: var(--accent);
  stroke-width: 5;
  stroke-linecap: round;
  stroke-dasharray: 150.8;
  stroke-dashoffset: 150.8;
  transition: stroke-dashoffset 0.6s ease, stroke 0.3s ease;
}
.ring-fill.is-error { stroke: var(--red); }
.ring-percent {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-display);
  font-size: 12.5px;
  font-weight: 700;
  color: var(--text);
}

.update-error-msg {
  color: var(--red);
  background: var(--red-soft);
  font-size: 11.5px;
  font-weight: 500;
  line-height: 1.5;
  white-space: pre-wrap;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  margin: -4px 0 14px;
  max-width: 640px;
}
.update-error-msg:not([hidden]) { display: block; }

@media (prefers-reduced-motion: reduce) {
  .ring-fill { transition: none; }
}

/* ---------- Table ---------- */
/* ---------- Table section title (reusable per-table pattern) ---------- */
.table-section { margin-bottom: 4px; }
.table-section-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 2px 10px;
}
.table-title-dot {
  width: 8px; height: 8px;
  border-radius: 999px;
  background: linear-gradient(135deg, var(--accent), var(--accent-bright));
  flex-shrink: 0;
}
.table-title {
  font-family: var(--font-display);
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text);
  margin: 0;
}

.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: auto;
  max-height: 620px;
}

table {
  border-collapse: separate;
  border-spacing: 0;
  width: 100%;
  min-width: 980px;
}

thead th {
  position: sticky;
  background: var(--surface-alt);
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  font-weight: 600;
  padding: 12px 10px;
  border-bottom: 1px solid var(--border);
  text-align: center;
  height: 44px;
  z-index: 2;
}
thead tr:first-child th { top: 0; }
thead tr:last-child th { top: 44px; box-shadow: inset 0 -1px 0 var(--border); }
/* A header with only ONE row (like TABLE 2's) matches :first-child AND
   :last-child at once -- without this, the :last-child rule above wins
   (declared later, same specificity) and pushes it to top:44px as if a
   second header row existed above it, sticking mid-scroll instead of flush
   at the top. */
thead tr:only-child th { top: 0; box-shadow: inset 0 -1px 0 var(--border); }
thead th.note-head { background: var(--accent-soft); color: var(--accent); }

.th-inner { display: flex; align-items: center; justify-content: center; gap: 4px; }
.th-control {
  background: none; border: none; padding: 3px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 5px; color: var(--text-muted); flex-shrink: 0;
}
.th-control:hover { background: var(--border); }
.th-control.active { color: var(--accent); background: var(--accent-soft); }
.th-icon { width: 12px; height: 12px; }

tbody td {
  padding: 11px 14px;
  border-bottom: 1px solid var(--border);
  text-align: center;
  white-space: nowrap;
  font-size: 13.5px;
}
tbody tr:hover td { background: var(--surface-alt); }
tbody tr.group-start td { border-top: 2px solid var(--border-strong); }

td.col-itemno {
  font-weight: 600;
  font-family: var(--font-display);
  text-align: left;
  background: var(--surface);
}
tbody tr:hover td.col-itemno { background: var(--surface-alt); }

td.num { font-variant-numeric: tabular-nums; text-align: right; }
td.col-warna, td.col-demand { text-align: left; }

.badge {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}
.badge-green { background: var(--green-soft); color: var(--green); }
.badge-amber { background: var(--amber-soft); color: var(--amber); }
.badge-red   { background: var(--red-soft);   color: var(--red); }
.badge-unknown { background: var(--surface-alt); color: var(--text-muted); border: 1px dashed var(--border-strong); }

.dot.unknown { background: var(--text-muted); }

.unknown-warning {
  background: var(--amber-soft);
  color: var(--amber);
  font-size: 12.5px;
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  margin: 0 0 14px;
}

.due-pill { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; }
.due-pill .dot { width: 7px; height: 7px; }
.due-pill.notfound { color: var(--amber); }
.due-pill.overdue { color: var(--red); font-weight: 600; }
.due-pill.upcoming { color: var(--text); }

.empty-state {
  padding: 56px 24px;
  text-align: center;
  color: var(--text-muted);
}
.empty-state h3 { margin: 0 0 6px; color: var(--text); font-family: var(--font-display); }
.empty-state button {
  margin-top: 14px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  font-weight: 500;
}

footer {
  margin-top: 16px;
  display: flex;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 8px;
  font-size: 12px;
  color: var(--text-muted);
}

/* ---------- Column filter/sort popover ---------- */
.col-popover {
  position: fixed;
  z-index: 50;
  width: 224px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  padding: 10px;
  flex-direction: column;
  gap: 8px;
}
/* IMPORTANT: display must only apply when NOT hidden. An unconditional
   `display: flex` on this rule would override the browser's own
   `[hidden] { display: none }` rule (author styles beat the user-agent
   stylesheet regardless of specificity), so `.hidden = true` in JS would
   never actually hide the element. */
.col-popover:not([hidden]) {
  display: flex;
}

.popover-sort-row {
  display: flex;
  gap: 6px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}
.popover-sort-btn {
  flex: 1;
  font-size: 11.5px;
  padding: 6px 4px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  text-align: center;
}
.popover-sort-btn:hover { border-color: var(--border-strong); }
.popover-sort-btn.active { border-color: var(--accent-bright); color: var(--accent); background: var(--accent-soft); }

.popover-filter-body { display: flex; flex-direction: column; gap: 8px; }
.popover-search {
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 12.5px;
  width: 100%;
}
.popover-quick-actions { display: flex; justify-content: space-between; }
.popover-quick-actions button {
  background: none; border: none; font-size: 11px;
  color: var(--accent); font-weight: 600; padding: 2px 0;
}
.popover-quick-actions button:hover { text-decoration: underline; }

.popover-checklist {
  max-height: 180px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}
.check-row {
  display: flex; align-items: center; gap: 8px;
  font-size: 12.5px; padding: 5px 4px; border-radius: 5px; cursor: pointer;
}
.check-row:hover { background: var(--surface-alt); }
.check-row input { accent-color: var(--accent); }
.popover-empty { font-size: 12px; color: var(--text-muted); padding: 8px 4px; margin: 0; }

.popover-close {
  align-self: flex-end;
  font-size: 11.5px;
  color: var(--text-muted);
  background: none;
  border: none;
  padding: 2px 4px;
}
.popover-close:hover { color: var(--text); }

@media (max-width: 780px) {
  .shell { padding: 16px 14px 36px; }
}

@media (prefers-reduced-motion: reduce) {
  .vital-card { animation: none; }
  .dot.pulse { animation: none; }
}
