:root {
  --bg: #0a0d12;
  --surface: #121722;
  --surface-raised: #171e2c;
  --surface-soft: #1d2534;
  --field: #0f1420;
  --text: #f4f7fb;
  --muted: #9aa7b8;
  --border: rgba(210, 222, 240, 0.14);
  --border-strong: rgba(210, 222, 240, 0.22);
  --accent: #2dd4bf;
  --accent-2: #f59e0b;
  --good: #34d399;
  --warning: #fbbf24;
  --danger: #fb7185;
  --shadow: 0 18px 44px rgba(0, 0, 0, 0.28);
  --radius: 8px;
  --radius-soft: 8px;
}

* { box-sizing: border-box; }

html {
  scrollbar-gutter: stable;
}

body {
  margin: 0;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--text);
  background: linear-gradient(180deg, #0d1118 0%, var(--bg) 42%, #080b10 100%);
  min-height: 100vh;
  padding: 28px;
}

.shell { max-width: 1280px; margin: 0 auto; }

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 28px;
  margin-bottom: 20px;
}

.eyebrow {
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-size: 12px;
  font-weight: 800;
  margin-bottom: 8px;
}

h1 {
  font-size: clamp(34px, 4vw, 52px);
  line-height: 0.98;
  margin: 0 0 10px;
  letter-spacing: 0;
}

.subtitle {
  margin: 0;
  color: var(--muted);
  font-size: 16px;
  line-height: 1.5;
  max-width: 720px;
}

.controls,
.threshold-controls {
  display: flex;
  gap: 10px;
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px;
  box-shadow: var(--shadow);
  flex-wrap: wrap;
}

.controls {
  align-items: flex-end;
}

.controls button {
  align-self: flex-end;
}

.threshold-controls {
  align-items: flex-end;
  justify-content: flex-end;
  margin-bottom: 14px;
}

.date-field,
.threshold-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.date-field span,
.threshold-field span {
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

select, button, input {
  border: 1px solid var(--border);
  background: var(--field);
  color: #ffffff;
  border-radius: var(--radius);
  padding: 10px 12px;
  font-size: 14px;
  outline: none;
  min-height: 40px;
  font-family: inherit;
}

input::placeholder { color: var(--muted); }

.date-field input {
  min-width: 148px;
  padding: 8px 10px;
}

.controls select {
  min-width: 140px;
}

.threshold-field input {
  width: 54px;
  text-align: center;
  padding: 8px 6px;
}

.threshold-field select {
  min-width: 72px;
  padding: 8px 8px;
}

select option {
  background: var(--field);
  color: #ffffff;
}

button {
  cursor: pointer;
  background: color-mix(in srgb, var(--accent) 18%, var(--field));
  font-weight: 800;
  transition: background 0.18s ease, border-color 0.18s ease, transform 0.18s ease;
}

button:hover {
  transform: translateY(-1px);
  border-color: color-mix(in srgb, var(--accent) 48%, var(--border));
  background: color-mix(in srgb, var(--accent) 26%, var(--field));
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

.status-row {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-bottom: 18px;
  color: var(--muted);
  font-size: 13px;
  flex-wrap: wrap;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: rgba(18, 23, 34, 0.72);
}

.dot {
  width: 10px;
  height: 10px;
  border-radius: 999px;
  background: var(--warning);
  box-shadow: 0 0 24px var(--warning);
}

.dot.online {
  background: var(--good);
  box-shadow: 0 0 24px var(--good);
}

.dot.alert {
  background: var(--danger);
  box-shadow: 0 0 24px var(--danger);
}

.grid,
.section-grid {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  gap: 14px;
}

.section-grid {
  position: relative;
  z-index: 1;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
  position: relative;
  overflow: hidden;
}

.card::before {
  display: none;
}

.section-card {
  grid-column: span 12;
  padding: 20px;
  background: rgba(18, 23, 34, 0.82);
}

.section-header {
  position: relative;
  z-index: 1;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 18px;
  margin-bottom: 16px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--border);
}

.section-toggle-header {
  cursor: pointer;
}

.section-toggle-header:focus-visible {
  outline: 2px solid color-mix(in srgb, var(--accent) 62%, transparent);
  outline-offset: 4px;
  border-radius: var(--radius);
}

.section-toggle-header:hover .event-day-chevron {
  background: rgba(45, 212, 191, 0.22);
}

.section-content {
  position: relative;
  z-index: 1;
}

.section-header h2 {
  margin: 0;
  font-size: 24px;
  letter-spacing: 0;
}

.section-title-row {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.section-subtitle {
  margin-top: 5px;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.45;
}

.metric {
  grid-column: span 3;
  min-height: 138px;
  display: flex;
  flex-direction: column;
  background: var(--surface-raised);
}

.chart-card {
  grid-column: span 6;
  min-height: 390px;
  background: var(--surface-raised);
}

.wide { grid-column: span 12; }

.label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  margin-bottom: 12px;
  position: relative;
  z-index: 1;
}

.value {
  font-size: 38px;
  line-height: 1;
  font-weight: 800;
  letter-spacing: 0;
  position: relative;
  z-index: 1;
  min-height: 42px;
  display: flex;
  align-items: center;
}

.unit {
  color: var(--muted);
  font-size: 16px;
  margin-left: 4px;
  font-weight: 800;
}

.trend {
  margin-top: auto;
  padding-top: 14px;
  color: var(--muted);
  font-size: 13px;
  position: relative;
  z-index: 1;
}

.last-reading-value {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  min-height: 42px;
  line-height: 1.05;
}

.last-reading-date {
  font-size: 14px;
  color: var(--text);
  font-weight: 700;
  letter-spacing: 0;
  margin-bottom: 4px;
}

.last-reading-time {
  font-size: 20px;
  color: var(--text);
  font-weight: 800;
  letter-spacing: 0;
  white-space: nowrap;
}

.threshold-ok { color: var(--text); }

.threshold-alert {
  color: var(--danger) !important;
  text-shadow: 0 0 18px rgba(251, 113, 133, 0.45);
}

.threshold-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 7px;
  border-radius: 999px;
  border: 1px solid rgba(251, 113, 133, 0.35);
  background: rgba(251, 113, 133, 0.12);
  color: #fecdd3;
  font-size: 12px;
  font-weight: 700;
}

.threshold-row-alert td {
  background: rgba(251, 113, 133, 0.075);
}

.chart-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
  position: relative;
  z-index: 1;
  gap: 12px;
  min-height: 34px;
}

.clickable { cursor: pointer; }
.collapsed { display: none; }

.reading-toggle-header {
  border-radius: var(--radius);
  padding: 4px 0;
}

.reading-toggle-header:focus-visible {
  outline: 2px solid color-mix(in srgb, var(--accent) 62%, transparent);
  outline-offset: 4px;
}

.reading-toggle-header:hover .event-day-chevron {
  background: rgba(45, 212, 191, 0.22);
}

.chart-title h2 {
  margin: 0;
  font-size: 18px;
  letter-spacing: 0;
}

.pill {
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 9px;
  font-size: 12px;
  font-weight: 800;
  background: var(--surface-soft);
  white-space: nowrap;
}

canvas {
  position: relative;
  z-index: 1;
  max-height: 320px;
}

.table-wrap {
  overflow-x: auto;
  position: relative;
  z-index: 1;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-raised);
}

table {
  width: 100%;
  border-collapse: collapse;
  color: var(--text);
  font-size: 14px;
}

.events-table {
  table-layout: fixed;
}

.event-time-col { width: 16%; }
.event-detected-col { width: 52%; }
.event-confidence-col { width: 16%; }
.event-action-col { width: 16%; }

th, td {
  padding: 13px 12px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  white-space: nowrap;
}

th {
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  background: rgba(255, 255, 255, 0.025);
}

tr:last-child td {
  border-bottom: 0;
}

.error {
  display: none;
  margin-bottom: 18px;
  padding: 14px 16px;
  border-radius: var(--radius);
  border: 1px solid rgba(251, 113, 133, 0.38);
  background: rgba(251, 113, 133, 0.12);
  color: #fecdd3;
}

.event-day-row td {
  padding: 18px 12px 8px;
  background: transparent;
  color: var(--text);
  font-size: 15px;
  font-weight: 900;
  letter-spacing: 0;
  border-top: 0;
  border-bottom: 0;
}

.event-day-toggle {
  width: 100%;
  min-height: 34px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 10px;
  align-items: center;
  text-align: left;
  border: 0;
  border-bottom: 1px solid var(--border);
  border-radius: 0;
  background: transparent;
  padding: 0 0 8px;
}

.event-day-toggle:hover {
  transform: none;
  border-color: color-mix(in srgb, var(--accent) 44%, var(--border));
  background: transparent;
}

.event-day-title {
  min-width: 0;
  overflow-wrap: anywhere;
}

.event-day-meta {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
}

.event-day-chevron {
  width: 28px;
  height: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
  background: rgba(45, 212, 191, 0.14);
  color: #99f6e4;
  font-size: 18px;
  line-height: 1;
}

.event-day-collapsed {
  display: none;
}

.event-video-row td {
  background: rgba(45, 212, 191, 0.055);
  color: var(--text);
  border-top: 1px solid rgba(45, 212, 191, 0.14);
  vertical-align: top;
}

.event-time {
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.event-summary {
  display: flex;
  flex-direction: column;
  gap: 8px;
  white-space: normal;
}

.event-title {
  color: var(--text);
  font-size: 15px;
  font-weight: 900;
  letter-spacing: 0;
}

.event-tags {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.event-chip {
  display: inline-flex;
  align-items: center;
  border: 1px solid rgba(45, 212, 191, 0.26);
  background: rgba(45, 212, 191, 0.10);
  color: #99f6e4;
  border-radius: 999px;
  padding: 4px 8px;
  font-size: 12px;
  font-weight: 800;
}

.event-chip.secondary {
  border-color: rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.06);
  color: var(--muted);
}

.event-confidence {
  color: var(--text);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.event-meta {
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}

.pagination {
  position: relative;
  z-index: 1;
  display: flex;
  justify-content: center;
  gap: 6px;
  flex-wrap: wrap;
  margin-top: 14px;
}

.pagination button {
  min-width: 40px;
  padding: 8px 11px;
}

.pagination button.active {
  border-color: rgba(45, 212, 191, 0.58);
  background: rgba(45, 212, 191, 0.20);
  color: var(--text);
  font-weight: 900;
}

.pagination button:disabled {
  opacity: 0.35;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 22px;
  background: rgba(0, 0, 0, 0.72);
  backdrop-filter: blur(10px);
}

.modal-overlay.hidden {
  display: none;
}

.modal-card {
  width: min(960px, 100%);
  max-height: calc(100vh - 44px);
  overflow: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 14px;
}

.modal-title {
  color: var(--text);
  font-size: 20px;
  font-weight: 900;
  letter-spacing: 0;
}

.modal-meta {
  color: var(--muted);
  font-size: 13px;
  margin-top: 4px;
}

.modal-close {
  width: 42px;
  height: 42px;
  border-radius: var(--radius);
  padding: 0;
  font-size: 26px;
  line-height: 1;
}

#modalVideoPlayer {
  width: 100%;
  max-height: 70vh;
  border-radius: var(--radius);
  background: #000;
  border: 1px solid var(--border);
  display: block;
}

@media (min-width: 981px) {
  .controls {
    flex-wrap: nowrap;
  }
}

@media (max-width: 980px) {
  body { padding: 20px; }
  .topbar { flex-direction: column; gap: 18px; }
  .controls { width: 100%; justify-content: space-between; }
  .date-field { flex: 1; }
  .date-field input { width: 100%; }
  .section-header { align-items: flex-start; flex-direction: column; }
  .threshold-controls { width: 100%; justify-content: flex-start; }
  .metric,
  .chart-card { grid-column: span 12; }
}

@media (max-width: 720px) {
  body { padding: 12px; }
  .shell { max-width: 100%; }
  .topbar { margin-bottom: 18px; }
  .eyebrow { font-size: 10px; margin-bottom: 8px; }
  h1 { font-size: 34px; line-height: 0.96; }
  .subtitle { font-size: 14px; }
  .status-row {
    gap: 6px 10px;
    font-size: 12px;
    line-height: 1.45;
    margin-bottom: 14px;
  }
  .status-row span:nth-child(3),
  .status-row span:nth-child(5),
  .status-row span:nth-child(7) { display: none; }
  .card,
  .section-card { border-radius: var(--radius); padding: 14px; }
  .grid,
  .section-grid { gap: 12px; }
  .section-header h2 { font-size: 22px; }
  .chart-title { align-items: flex-start; flex-direction: column; gap: 8px; }
  .reading-toggle-header {
    align-items: center;
    flex-direction: row;
    justify-content: space-between;
  }
  .chart-title h2 { font-size: 18px; }
  .pill,
  .threshold-pill { font-size: 11px; }
  .value { font-size: 34px; }
  .metric {
    grid-column: span 6;
    min-height: 118px;
  }
  .chart-card { min-height: 340px; }
  canvas { max-height: 260px; }

  .controls {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    border-radius: var(--radius);
    padding: 8px;
  }

  .controls select {
    grid-column: 1 / -1;
    width: 100%;
  }

  .date-field,
  .date-field input {
    width: 100%;
  }

  .threshold-controls {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    align-items: end;
    border-radius: var(--radius);
    padding: 8px;
  }

  .threshold-field,
  .threshold-field input,
  .threshold-field select,
  .threshold-controls button { width: 100%; }

  .threshold-field input,
  .threshold-field select,
  .threshold-controls button { min-height: 42px; }

  th, td { padding: 10px 8px; font-size: 12px; }

  .events-table {
    display: block;
    table-layout: auto;
    width: 100%;
    min-width: 0;
  }

  .events-table colgroup {
    display: none;
  }

  .events-table thead {
    display: none;
  }

  #eventsTable,
  #eventsTable tr,
  #eventsTable td {
    display: block;
    width: 100%;
  }

  #eventsTable tr.event-day-collapsed {
    display: none;
  }

  .event-day-row td {
    padding: 16px 8px 8px;
    border-bottom: 0;
    background: transparent;
  }

  .event-day-toggle {
    grid-template-columns: minmax(0, 1fr) auto;
    min-height: 34px;
    gap: 6px 8px;
    padding: 0 0 8px;
  }

  .event-day-meta {
    grid-column: 1;
    font-size: 11px;
  }

  .event-day-chevron {
    grid-column: 2;
    grid-row: 1 / span 2;
  }

  .event-video-row {
    margin: 8px 0 12px;
    border: 1px solid rgba(45, 212, 191, 0.18);
    border-radius: var(--radius);
    background: rgba(45, 212, 191, 0.055);
    overflow: hidden;
  }

  #eventsTable .event-video-row td {
    display: grid;
    grid-template-columns: minmax(72px, 0.34fr) minmax(0, 1fr);
    gap: 10px;
    align-items: start;
    padding: 10px 12px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    background: transparent;
    white-space: normal;
    min-width: 0;
  }

  #eventsTable .event-video-row td::before {
    content: attr(data-label);
    color: var(--muted);
    font-size: 11px;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 0.04em;
  }

  #eventsTable .event-video-row td:last-child {
    border-bottom: 0;
  }

  #eventsTable .event-video-row td[data-label="Action"] {
    grid-template-columns: 1fr;
  }

  #eventsTable .event-video-row td[data-label="Action"]::before {
    display: none;
  }

  .play-event-button {
    width: 100%;
    min-height: 42px;
  }

  .event-summary { gap: 6px; min-width: 0; }
  .event-confidence,
  .event-time {
    overflow-wrap: anywhere;
  }
  .event-title { font-size: 14px; }
  .event-chip { font-size: 11px; padding: 3px 7px; }
  .pagination { justify-content: flex-start; }
  .pagination button { min-width: 36px; padding: 8px 10px; }

  .modal-overlay { padding: 10px; }
  .modal-card { border-radius: var(--radius); padding: 12px; }
  .modal-title { font-size: 17px; }
  #modalVideoPlayer { max-height: 62vh; }
}

@media (max-width: 420px) {
  body { padding: 10px; }
  h1 { font-size: 31px; }
  .controls { grid-template-columns: 1fr; }
  .threshold-controls { grid-template-columns: 1fr; }
  .metric { grid-column: span 12; }
  .last-reading-time { font-size: 18px; }
}


.pagination-info,
.pagination-ellipsis {
  display: inline-flex;
  align-items: center;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  padding: 8px 4px;
}

.pagination-info {
  padding-left: 8px;
}

.event-tags {
  max-width: 100%;
}

@media (max-width: 720px) {
  .pagination-info {
    flex-basis: 100%;
    padding-left: 0;
  }
}
