:root {
  color-scheme: light;
  --bg: #f5f6f8;
  --panel: #ffffff;
  --text: #17202a;
  --muted: #667085;
  --line: #d8dee7;
  --accent: #0c6b58;
  --accent-weak: #e3f4ef;
  --danger: #b42318;
  --motion: #12a06f;
  --record: #d58a00;
  --shadow: 0 10px 30px rgb(22 34 51 / 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

[hidden] {
  display: none !important;
}

button,
input,
select,
textarea {
  font: inherit;
}

button,
select,
input {
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
}

button {
  position: relative;
  padding: 0 14px;
  cursor: pointer;
}

button:hover {
  border-color: #9aa6b8;
}

button.primary,
.segmented button.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

button.loading {
  color: transparent;
  pointer-events: none;
}

button.loading::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 16px;
  height: 16px;
  margin: -8px 0 0 -8px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-top-color: transparent;
  border-radius: 50%;
  color: var(--accent);
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.auth-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.auth-card {
  width: min(420px, 100%);
  display: grid;
  gap: 16px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 24px;
}

.auth-card label {
  display: grid;
  gap: 6px;
}

.auth-card input {
  width: 100%;
  padding: 0 10px;
}

.error-line {
  min-height: 20px;
  color: var(--danger);
  font-size: 13px;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(220px, 1fr) auto minmax(260px, 1fr);
  align-items: center;
  gap: 16px;
  padding: 22px 28px 14px;
}

h1,
h2 {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  font-size: 28px;
  line-height: 1.1;
}

h2 {
  font-size: 16px;
  line-height: 1.3;
}

.muted {
  color: var(--muted);
  font-size: 13px;
}

.main-nav {
  display: flex;
  gap: 6px;
  justify-content: center;
}

.main-nav a {
  display: inline-flex;
  align-items: center;
  min-height: 36px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  font-size: 14px;
  text-decoration: none;
}

.main-nav a.active {
  background: #111827;
  border-color: #111827;
  color: #fff;
}

.top-actions,
.panel-head,
.toolbar,
.actions,
.inline-controls,
.selection-controls,
.grid-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
}

.top-actions {
  flex-wrap: wrap;
  justify-content: flex-end;
}

.pill {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 0 10px;
  border-radius: 999px;
  background: #eef2f6;
  color: #334155;
  font-size: 13px;
  white-space: nowrap;
}

.pill.ok {
  background: var(--accent-weak);
  color: var(--accent);
}

.pill.err {
  background: #fee4e2;
  color: var(--danger);
}

.pill.subtle {
  background: #f8fafc;
  color: var(--muted);
}

.page {
  display: none;
}

.page.active {
  display: block;
}

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 18px;
  padding: 0 28px 28px;
}

.page-pad {
  padding: 0 28px 28px;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 16px;
}

.panel-head {
  justify-content: space-between;
  margin-bottom: 12px;
}

.live-panel {
  min-width: 0;
}

.live-frame,
.grid-frame {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 9;
  border: 1px solid #111827;
  border-radius: 6px;
  background: #111827;
}

.live-frame {
  min-height: 320px;
}

.facts {
  display: grid;
  gap: 8px;
  margin: 0 0 14px;
}

.facts div {
  display: grid;
  grid-template-columns: 92px minmax(0, 1fr);
  gap: 8px;
}

.facts dt {
  color: var(--muted);
  font-size: 13px;
}

.facts dd {
  margin: 0;
  min-width: 0;
  overflow-wrap: anywhere;
  font-size: 13px;
}

.channel-list {
  display: grid;
  gap: 6px;
  max-height: 320px;
  overflow: auto;
}

.channel-row {
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  min-height: 32px;
  padding: 5px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  font-size: 13px;
  text-align: left;
}

.channel-row.connected {
  border-color: #a7d9cd;
  background: #f4fbf8;
}

.channel-row strong,
.channel-row span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: #344054;
  font-size: 14px;
}

input[type="date"],
input[type="datetime-local"],
input[type="number"],
select {
  padding: 0 10px;
}

input[type="checkbox"] {
  min-height: 18px;
  width: 18px;
}

.archive-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 430px;
  gap: 18px;
  padding: 0 28px 28px;
}

.archive-player-panel {
  min-width: 0;
}

.archive-player-panel video {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 9;
  border: 1px solid #111827;
  border-radius: 6px;
  background: #111827;
}

.timeline-card {
  margin-top: 14px;
}

.timeline-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}

.timeline {
  position: relative;
  height: 54px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #eef2f6;
  cursor: crosshair;
}

.timeline-empty {
  display: grid;
  height: 100%;
  place-items: center;
  color: var(--muted);
  font-size: 13px;
}

.timeline-segment,
.timeline-selection,
.timeline-playhead {
  position: absolute;
  top: 0;
  bottom: 0;
}

.timeline-segment {
  min-width: 2px;
  opacity: 0.82;
  padding: 0;
  min-height: 0;
  border: 0;
  border-radius: 0;
  appearance: none;
}

.timeline-segment.record {
  background: var(--record);
}

.timeline-segment.motion {
  background: var(--motion);
}

.timeline-selection {
  z-index: 3;
  border-left: 2px solid #111827;
  border-right: 2px solid #111827;
  background: rgb(17 24 39 / 0.18);
  pointer-events: none;
}

.timeline-playhead {
  z-index: 4;
  width: 2px;
  background: #111827;
  pointer-events: none;
}

.selection-controls {
  flex-wrap: wrap;
  margin-top: 12px;
}

.archive-list-panel {
  min-width: 0;
}

.table-wrap {
  max-height: 64vh;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

th,
td {
  padding: 10px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: middle;
}

th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: #f8fafc;
  color: #475467;
  font-weight: 600;
}

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

tbody tr {
  cursor: pointer;
}

tbody tr:hover,
tbody tr.active {
  background: #f4fbf8;
}

.event {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 0 8px;
  border-radius: 999px;
  background: #eef2f6;
}

.event.motion {
  background: var(--accent-weak);
  color: var(--accent);
}

.event.record {
  background: #fff1d6;
  color: #8a4b00;
}

.camera-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(360px, 1fr));
  gap: 18px;
}

.grid-toolbar {
  flex-wrap: wrap;
  margin-bottom: 12px;
}

.grid-tile {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 12px;
}

.grid-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  font-size: 13px;
}

.grid-title span {
  color: var(--muted);
}

.settings-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 18px;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.segmented button {
  min-height: 42px;
  padding: 0 10px;
  white-space: normal;
}

.form-grid {
  display: grid;
  gap: 12px;
  margin-bottom: 14px;
}

.form-grid label {
  justify-content: space-between;
}

.form-grid input,
.form-grid select {
  min-width: 160px;
}

.user-list {
  display: grid;
  gap: 6px;
  margin-top: 12px;
  font-size: 13px;
}

.user-list div {
  display: flex;
  justify-content: space-between;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
}

@media (max-width: 1180px) {
  .topbar,
  .layout,
  .archive-shell,
  .settings-grid {
    grid-template-columns: 1fr;
  }

  .topbar {
    align-items: flex-start;
  }

  .main-nav {
    justify-content: flex-start;
    overflow-x: auto;
    width: 100%;
  }

  .top-actions {
    justify-content: flex-start;
  }
}

@media (max-width: 720px) {
  .topbar,
  .layout,
  .archive-shell,
  .page-pad {
    padding-left: 16px;
    padding-right: 16px;
  }

  .top-actions,
  .inline-controls,
  .selection-controls {
    justify-content: flex-start;
    flex-wrap: wrap;
  }

  .segmented,
  .camera-grid {
    grid-template-columns: 1fr;
  }

  .live-frame {
    min-height: 220px;
  }

  .table-wrap {
    max-height: 52vh;
  }
}
