:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --surface-2: #eef1f7;
  --text: #16202e;
  --text-muted: #4a5568;
  --border: #d3dae6;
  --primary: #1858c4;
  --primary-ink: #ffffff;
  --focus: #0b3ea8;
  --accent: #0f9d76;
  --danger: #b4232a;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(16, 32, 54, 0.08), 0 6px 20px rgba(16, 32, 54, 0.06);
  --gap: 1rem;
  --tap: 44px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e131b;
    --surface: #18202c;
    --surface-2: #202a38;
    --text: #eef2f8;
    --text-muted: #a4b0c0;
    --border: #2c3849;
    --primary: #4c8dff;
    --primary-ink: #06122a;
    --focus: #8fb6ff;
    --accent: #35c79a;
    --danger: #ff6b72;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.4), 0 6px 20px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4 { line-height: 1.2; margin: 0; }

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0);
  white-space: nowrap; border: 0;
}

.is-hidden { display: none !important; }

.skip-link {
  position: absolute;
  left: 0.5rem;
  top: -3rem;
  z-index: 100;
  background: var(--primary);
  color: var(--primary-ink);
  padding: 0.6rem 1rem;
  border-radius: 0 0 var(--radius) var(--radius);
  transition: top 0.15s ease;
}
.skip-link:focus { top: 0; }

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
  border-radius: 4px;
}

/* Header */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--gap);
  padding: 1rem clamp(1rem, 4vw, 2rem);
  padding-top: max(1rem, env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}
.app-header__title { font-size: 1.15rem; }
.app-header__tag { margin: 0.25rem 0 0; font-size: 0.8rem; color: var(--text-muted); max-width: 46ch; }

.main { padding: clamp(1rem, 4vw, 2rem); max-width: 1100px; margin: 0 auto; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--tap);
  padding: 0.6rem 1.1rem;
  border-radius: 999px;
  border: 1px solid transparent;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}
.btn--primary { background: var(--primary); color: var(--primary-ink); }
.btn--primary:hover { filter: brightness(1.05); }
.btn--ghost {
  background: transparent;
  color: var(--text);
  border-color: var(--border);
  min-height: 40px;
}
.btn--ghost:hover { background: var(--surface-2); }

/* Upload */
.upload { max-width: 560px; margin: 2rem auto; text-align: center; }
.upload__heading { font-size: 1.3rem; margin-bottom: 1.25rem; }
.dropzone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 2rem 1.5rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.6rem;
  transition: border-color 0.15s ease, background 0.15s ease;
  cursor: pointer;
}
.dropzone.is-drag { border-color: var(--primary); background: var(--surface-2); }
.dropzone__icon { width: 48px; height: 48px; color: var(--primary); }
.dropzone__text { font-weight: 600; margin: 0; }
.dropzone__hint { margin: 0; color: var(--text-muted); }
.upload__note { color: var(--text-muted); font-size: 0.85rem; margin-top: 1.25rem; }

.status { margin-top: 1rem; font-size: 0.9rem; min-height: 1.4em; }
.status--error { color: var(--danger); font-weight: 600; }
.status--ok { color: var(--accent); font-weight: 600; }

/* Dashboard */
.dashboard { display: flex; flex-direction: column; gap: 1.25rem; }
.dashboard__meta { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0.5rem 1rem; }
.dashboard__heading { font-size: 1.4rem; }
.dashboard__span { color: var(--text-muted); font-size: 0.9rem; margin: 0; }

/* KPI */
.kpi-grid {
  list-style: none;
  margin: 0; padding: 0;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.75rem;
}
.kpi {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.9rem 1rem;
  padding-left: 1.15rem;
  box-shadow: var(--shadow);
  overflow: hidden;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.kpi::before {
  content: "";
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 4px;
  background: var(--primary);
}
.kpi:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 10px rgba(16, 32, 54, 0.12), 0 12px 28px rgba(16, 32, 54, 0.1);
}
.kpi__value { font-size: 1.4rem; font-weight: 700; color: var(--text); }
.kpi__unit { font-size: 0.85rem; font-weight: 600; color: var(--text-muted); }
.kpi__label { font-size: 0.78rem; color: var(--text-muted); margin-top: 0.15rem; }

/* Panels */
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: var(--shadow);
}
.panel__head { display: flex; flex-direction: column; gap: 0.75rem; margin-bottom: 0.75rem; }
.panel__title { font-size: 1.05rem; }
.panel__subtitle { font-size: 0.9rem; color: var(--text-muted); margin: 0 0 0.5rem; text-align: center; }
.panel__summary {
  margin: 0 0 0.9rem;
  font-size: 0.9rem;
  color: var(--text-muted);
  font-weight: 600;
}

.controls { display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: end; }

.segmented {
  display: inline-flex;
  background: var(--surface-2);
  border-radius: 999px;
  padding: 3px;
  gap: 2px;
}
.segmented__btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-weight: 600;
  padding: 0.45rem 0.9rem;
  min-height: 38px;
  border-radius: 999px;
  cursor: pointer;
}
.segmented__btn.is-active { background: var(--primary); color: var(--primary-ink); }

.field { display: flex; flex-direction: column; gap: 0.25rem; font-size: 0.8rem; color: var(--text-muted); }
.field--search { min-width: min(16rem, 100%); }
.field--search .select { min-width: 0; }
.select {
  font: inherit;
  min-height: var(--tap);
  padding: 0.4rem 0.6rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}

.chart-box { position: relative; width: 100%; height: 300px; }
.chart-box--doughnut { height: 260px; }

.split { display: grid; grid-template-columns: 1fr; gap: 1.25rem; }

/* Tables */
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.data-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
.data-table caption { text-align: left; }
.data-table th, .data-table td { padding: 0.5rem 0.6rem; text-align: right; white-space: nowrap; }
.data-table th:first-child, .data-table td:first-child { text-align: left; }
.data-table thead th {
  position: sticky; top: 0;
  background: var(--surface-2);
  border-bottom: 2px solid var(--border);
}
.data-table tbody tr { border-bottom: 1px solid var(--border); }
.data-table tbody tr:nth-child(even) { background: var(--surface-2); }
.data-table tfoot th {
  padding: 0.55rem 0.6rem;
  text-align: right;
  white-space: nowrap;
  border-top: 2px solid var(--border);
  background: var(--surface-2);
  font-weight: 700;
}
.data-table tfoot th:first-child { text-align: left; }

.sortable { cursor: pointer; user-select: none; }
.sortable button {
  all: unset;
  cursor: pointer;
  font-weight: 700;
  display: inline-flex;
  gap: 0.3rem;
  align-items: center;
  min-height: 32px;
}
.sortable button:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.sort-arrow { font-size: 0.75em; opacity: 0.6; }
.sort-arrow--active { opacity: 1; color: var(--primary); }

.data-table-wrap { margin-top: 0.75rem; }
.data-table-wrap summary { cursor: pointer; font-size: 0.85rem; color: var(--primary); min-height: var(--tap); display: flex; align-items: center; }

/* Footer */
.app-footer {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.8rem;
  padding: 1.5rem 1rem;
  padding-bottom: max(1.5rem, env(safe-area-inset-bottom));
}

/* Larger screens */
@media (min-width: 620px) {
  .kpi-grid { grid-template-columns: repeat(4, 1fr); }
  .panel__head { flex-direction: row; align-items: center; justify-content: space-between; gap: 1rem; }
  .split { grid-template-columns: 1fr 1fr; align-items: start; }
  /* RFID: give the summary table more room so it never needs a horizontal scrollbar. */
  .split--rfid { grid-template-columns: minmax(200px, 260px) 1fr; }
  .app-header__title { font-size: 1.35rem; }
}

/* Compact spacing for the RFID card table to fit all columns. */
.split--rfid .data-table th,
.split--rfid .data-table td { padding: 0.5rem 0.45rem; }
.split--rfid .data-table { font-size: 0.82rem; }

@media (min-width: 900px) {
  .panel { padding: 1.25rem 1.5rem; }
}

/* Responsive session table -> cards on small screens */
@media (max-width: 619px) {
  .data-table--responsive thead { display: none; }
  .data-table--responsive,
  .data-table--responsive tbody,
  .data-table--responsive tr,
  .data-table--responsive td { display: block; width: 100%; }
  .data-table--responsive tr {
    border: 1px solid var(--border);
    border-radius: 10px;
    margin-bottom: 0.6rem;
    padding: 0.4rem 0.6rem;
    background: var(--surface);
  }
  .data-table--responsive tbody tr:nth-child(even) { background: var(--surface); }
  .data-table--responsive td {
    display: flex;
    justify-content: space-between;
    gap: 1rem;
    text-align: right;
    padding: 0.35rem 0;
    white-space: normal;
    border-bottom: 1px dashed var(--border);
  }
  .data-table--responsive td:last-child { border-bottom: none; }
  .data-table--responsive td::before {
    content: attr(data-label);
    font-weight: 600;
    color: var(--text-muted);
    text-align: left;
  }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
