:root {
  --bg: #0f1420;
  --surface: #161d2e;
  --surface-2: #1e2740;
  --border: #2a3450;
  --text: #e8ecf6;
  --text-dim: #9aa5c0;
  --accent: #4f7cff;
  --accent-hover: #6690ff;
  --green: #2dd4a0;
  --red: #ff5c72;
  --yellow: #f5b942;
  --radius: 10px;
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  height: 100%;
  font-size: 14px;
  min-width: 320px;
  max-width: 100%;
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

button, input, select {
  font-family: inherit;
  font-size: 14px;
}

button, input, select, textarea {
  min-height: 40px;
}

button {
  touch-action: manipulation;
}

h1 { font-size: 22px; font-weight: 600; margin: 0 0 16px; }
h2 { font-size: 15px; font-weight: 600; margin: 28px 0 12px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.04em; }

/* ---------- Login ---------- */
.login-screen {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: radial-gradient(circle at 30% 20%, #1a2440 0%, #0f1420 60%);
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 40px 36px;
  width: 360px;
  max-width: 100%;
  box-shadow: 0 20px 60px rgba(0,0,0,0.4);
}
.login-logo { font-size: 24px; font-weight: 700; margin-bottom: 4px; }
.login-sub { color: var(--text-dim); font-size: 13px; margin-bottom: 24px; }
.login-card label { display: block; font-size: 12px; color: var(--text-dim); margin: 14px 0 6px; }
.login-card input {
  width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  outline: none;
}
.login-card input:focus { border-color: var(--accent); }
.login-card button[type=submit] {
  width: 100%;
  margin-top: 22px;
  padding: 11px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: white;
  font-weight: 600;
  cursor: pointer;
}
.login-card button[type=submit]:hover { background: var(--accent-hover); }
.login-card button[disabled] { opacity: 0.6; cursor: default; }
.error-msg {
  margin-top: 14px;
  padding: 10px 12px;
  border-radius: 8px;
  background: rgba(255,92,114,0.12);
  color: var(--red);
  font-size: 13px;
}
.login-footer {
  margin-top: 22px;
  font-size: 11px;
  color: var(--text-dim);
  display: flex;
  align-items: center;
  gap: 6px;
}

/* ---------- Status dot ---------- */
.status-dot {
  width: 8px; height: 8px; border-radius: 50%;
  display: inline-block;
}
.status-unknown { background: var(--text-dim); }
.status-ok { background: var(--green); box-shadow: 0 0 6px var(--green); }
.status-down { background: var(--red); box-shadow: 0 0 6px var(--red); }

/* ---------- App shell ---------- */
.app { display: flex; flex-direction: column; height: 100vh; }
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  height: 56px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}
.topbar-left { display: flex; align-items: center; gap: 28px; }
.logo { font-weight: 700; font-size: 16px; }
.tabs { display: flex; gap: 4px; }
.tab-btn {
  background: none; border: none; color: var(--text-dim);
  padding: 8px 14px; border-radius: 8px; cursor: pointer; font-weight: 500;
}
.tab-btn:hover { color: var(--text); background: var(--surface-2); }
.tab-btn.active { color: var(--text); background: var(--surface-2); }
.topbar-right { display: flex; align-items: center; gap: 12px; }
.user-label { color: var(--text-dim); font-size: 13px; }

.content { flex: 1; overflow-y: auto; padding: 28px 32px; }

/* ---------- Stat cards ---------- */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 14px;
}
.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
}
.stat-value { font-size: 26px; font-weight: 700; }
.stat-label { color: var(--text-dim); font-size: 12px; margin-top: 4px; }

.health-grid { display: flex; flex-wrap: wrap; gap: 10px; }
.health-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}

/* ---------- Buttons ---------- */
.btn-primary, .btn-secondary {
  border-radius: 8px;
  padding: 8px 16px;
  cursor: pointer;
  font-weight: 600;
  border: 1px solid transparent;
}
.btn-primary { background: var(--accent); color: white; border: none; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { border-color: var(--accent); }
.btn-icon { background: none; border: none; color: var(--text-dim); cursor: pointer; font-size: 16px; }
.btn-sm {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 6px;
  padding: 4px 9px;
  font-size: 12px;
  cursor: pointer;
  margin-right: 4px;
}
.btn-sm:hover { border-color: var(--accent); }
.btn-sm-danger { color: var(--red); }
.btn-sm-danger:hover { border-color: var(--red); }

/* ---------- Sections ---------- */
.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
}
.section-head-controls { display: flex; gap: 10px; }
.section-head-controls select {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
}

/* ---------- Table ---------- */
.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
table { width: 100%; border-collapse: collapse; }
th, td {
  text-align: left;
  padding: 11px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
th { color: var(--text-dim); font-weight: 600; font-size: 11px; text-transform: uppercase; letter-spacing: 0.03em; background: var(--surface-2); }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: rgba(255,255,255,0.02); }
.empty-state { padding: 40px 20px; text-align: center; color: var(--text-dim); font-size: 13px; }

.badge {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 600;
}
.badge-green { background: rgba(45,212,160,0.14); color: var(--green); }
.badge-red { background: rgba(255,92,114,0.14); color: var(--red); }
.badge-yellow { background: rgba(245,185,66,0.14); color: var(--yellow); }
.badge-gray { background: rgba(154,165,192,0.14); color: var(--text-dim); }

/* ---------- API log ---------- */
.api-log {
  flex-shrink: 0;
  border-top: 1px solid var(--border);
  background: var(--surface);
  max-height: 220px;
  display: flex;
  flex-direction: column;
}
.api-log-head {
  padding: 8px 20px;
  font-size: 12px;
  color: var(--text-dim);
  display: flex;
  justify-content: space-between;
  cursor: pointer;
  user-select: none;
  flex-shrink: 0;
  border-bottom: 1px solid var(--border);
}
.api-log-body {
  overflow-y: auto;
  font-family: "SF Mono", Consolas, monospace;
  font-size: 11.5px;
  padding: 4px 0;
}
.api-log.collapsed .api-log-body { display: none; }
.log-row {
  display: grid;
  grid-template-columns: 90px 52px 1fr 60px 70px;
  gap: 10px;
  padding: 4px 20px;
  align-items: center;
  border-bottom: 1px solid rgba(255,255,255,0.03);
}
.log-time { color: var(--text-dim); }
.log-method { font-weight: 700; }
.log-url { color: var(--text-dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.log-status-2xx { color: var(--green); }
.log-status-4xx, .log-status-5xx, .log-status-err { color: var(--red); }
.log-duration { color: var(--text-dim); text-align: right; }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.55);
  display: flex; align-items: center; justify-content: center;
  z-index: 50;
}
.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  width: 460px;
  max-height: 86vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-head h2 { margin: 0; font-size: 16px; }
.modal-form { padding: 18px 20px; overflow-y: auto; }
.modal-form label { display: block; font-size: 12px; color: var(--text-dim); margin: 12px 0 5px; }
.modal-form label:first-child { margin-top: 0; }
.modal-form input, .modal-form select, .modal-form textarea {
  width: 100%;
  padding: 9px 11px;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  outline: none;
}
.modal-form input:focus, .modal-form select:focus, .modal-form textarea:focus { border-color: var(--accent); }
.field-hint { font-size: 11px; color: var(--text-dim); margin-top: 4px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 22px; }
.token-warning {
  margin-top: 4px;
  padding: 9px 12px;
  border-radius: 8px;
  background: rgba(245,185,66,0.14);
  color: var(--yellow);
  font-size: 12.5px;
}
.form-error {
  margin-top: 14px;
  padding: 9px 12px;
  border-radius: 8px;
  background: rgba(255,92,114,0.12);
  color: var(--red);
  font-size: 12.5px;
}

/* ---------- Stream viewer ---------- */
.stream-modal { width: 720px; max-width: 96vw; }
.stream-body {
  padding: 16px 20px 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.stream-video {
  width: 100%;
  aspect-ratio: 16 / 9;
  background: #000;
  border-radius: 8px;
  display: block;
}
.stream-status {
  font-size: 12.5px;
  color: var(--text-dim);
  min-height: 16px;
}
.stream-status.stream-status-error { color: var(--red); }
.stream-status.stream-status-ok { color: var(--green); }

/* ---------- Archive viewer ---------- */
.archive-modal { width: 720px; max-width: 96vw; }
.archive-body {
  padding: 16px 20px 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
}
.archive-body h3 {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-dim);
  margin: 10px 0 2px;
}
.archive-range {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  flex-wrap: wrap;
}
.archive-range label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 11px;
  color: var(--text-dim);
}
.archive-range input {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 8px 10px;
}
.archive-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 180px;
  overflow-y: auto;
}
.archive-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 12.5px;
}
.archive-row-info {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}
.archive-row-time { color: var(--text); font-family: "SF Mono", Consolas, monospace; font-size: 12px; }
.archive-row-gap { color: var(--text-dim); }
.archive-export-row {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}
.archive-export-status { font-size: 12.5px; color: var(--text-dim); }
.archive-export-status a { color: var(--accent); }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: 240px;
  right: 24px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--green);
  padding: 12px 18px;
  border-radius: 8px;
  font-size: 13px;
  z-index: 60;
  box-shadow: 0 10px 30px rgba(0,0,0,0.4);
}
.toast.toast-error { border-left-color: var(--red); }

.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* ---------- Responsive layer ---------- */
.topbar-left,
.topbar-right,
.tabs,
.section-head-controls {
  min-width: 0;
}

.tab-btn {
  white-space: nowrap;
}

.user-label {
  max-width: 260px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.section-head-controls select {
  min-width: 180px;
}

.table-wrap {
  overflow: auto;
}

td {
  vertical-align: top;
  overflow-wrap: anywhere;
}

td[title] {
  max-width: 280px;
  overflow: hidden;
  text-overflow: ellipsis;
}

td.cam-actions {
  min-width: 220px;
}

td.cam-actions .btn-sm {
  margin-bottom: 4px;
}

@media (max-width: 980px) {
  .topbar {
    align-items: stretch;
    flex-direction: column;
    gap: 12px;
    height: auto;
    min-height: 56px;
    padding: 12px 16px;
  }

  .topbar-left {
    align-items: flex-start;
    flex-direction: column;
    gap: 10px;
  }

  .tabs {
    width: 100%;
    overflow-x: auto;
    padding-bottom: 2px;
    scrollbar-width: thin;
  }

  .tab-btn {
    background: rgba(255,255,255,0.02);
  }

  .topbar-right {
    width: 100%;
    justify-content: space-between;
  }

  .user-label {
    max-width: none;
    flex: 1;
  }

  .content {
    padding: 22px 18px;
  }

  .section-head {
    align-items: flex-start;
    flex-direction: column;
    gap: 12px;
  }

  .section-head-controls {
    width: 100%;
    flex-wrap: wrap;
  }

  .section-head-controls select,
  .section-head-controls .btn-primary {
    flex: 1 1 220px;
  }
}

@media (max-width: 760px) {
  html,
  body {
    font-size: 13px;
    height: auto;
    min-height: 100%;
    overscroll-behavior-x: none;
  }

  body {
    position: relative;
    width: 100%;
  }

  button,
  input,
  select,
  textarea {
    font-size: 16px;
  }

  .app {
    height: auto;
    min-height: 100vh;
    min-height: 100dvh;
    overflow-x: hidden;
  }

  h1 {
    font-size: 20px;
  }

  h2 {
    font-size: 13px;
    margin-top: 22px;
  }

  .login-screen {
    min-height: 100dvh;
    padding: 20px;
  }

  .login-card {
    width: 100%;
    max-width: 390px;
    padding: 28px 22px;
    border-radius: 14px;
  }

  .login-footer {
    align-items: flex-start;
    flex-wrap: wrap;
    line-height: 1.45;
  }

  .content {
    flex: 0 0 auto;
    overflow: visible;
    padding: 18px 12px;
  }

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

  .stat-card {
    padding: 14px;
  }

  .stat-value {
    font-size: 22px;
  }

  .health-grid {
    display: grid;
    grid-template-columns: 1fr;
  }

  .table-wrap {
    background: transparent;
    border: none;
    border-radius: 0;
    overflow: visible;
  }

  table,
  thead,
  tbody,
  tr,
  th,
  td {
    display: block;
  }

  thead {
    display: none;
  }

  tbody {
    display: grid;
    gap: 12px;
  }

  tbody tr {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    padding: 8px 0;
  }

  tbody tr:hover {
    background: var(--surface);
  }

  td {
    display: grid;
    grid-template-columns: minmax(98px, 36%) 1fr;
    gap: 12px;
    align-items: start;
    border-bottom: 1px solid rgba(255,255,255,0.045);
    padding: 10px 14px;
    font-size: 13px;
  }

  tbody tr td:last-child {
    border-bottom: none;
  }

  td::before {
    content: attr(data-label);
    color: var(--text-dim);
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.03em;
    line-height: 1.35;
    text-transform: uppercase;
  }

  td[title] {
    max-width: none;
  }

  td.cam-actions {
    min-width: 0;
  }

  td.cam-actions .btn-sm {
    width: 100%;
    margin: 0 0 6px;
  }

  .empty-state {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
  }

  .api-log {
    max-height: none;
    overflow: hidden;
  }

  .api-log-head {
    gap: 10px;
    padding: 9px 12px;
  }

  .api-log-head span:first-child {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .api-log-body {
    max-height: 126px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .log-row {
    grid-template-columns: 64px 44px minmax(0, 1fr) 42px;
    gap: 6px;
    padding: 6px 12px;
  }

  .log-duration {
    display: none;
  }

  .modal-overlay {
    align-items: flex-end;
    padding: 0;
    overflow: hidden;
  }

  .modal {
    width: 100%;
    max-width: none;
    max-height: min(92vh, 92dvh);
    border-right: none;
    border-bottom: none;
    border-left: none;
    border-radius: 16px 16px 0 0;
  }

  .modal-head {
    padding: 14px 16px;
  }

  .modal-form {
    max-height: calc(92dvh - 58px);
    padding: 16px;
    -webkit-overflow-scrolling: touch;
  }

  .modal-actions {
    position: sticky;
    bottom: 0;
    background: linear-gradient(180deg, rgba(22,29,46,0.86), var(--surface) 35%);
    margin: 18px -16px -16px;
    padding: 14px 16px 16px;
  }

  .modal-actions .btn-secondary,
  .modal-actions .btn-primary {
    flex: 1;
  }

  .toast {
    right: 12px;
    bottom: 184px;
    left: 12px;
    text-align: center;
  }
}

@media (max-width: 460px) {
  .topbar {
    padding: 10px 12px;
  }

  .topbar-right {
    align-items: stretch;
    flex-wrap: wrap;
    gap: 8px;
  }

  .topbar-right .status-dot {
    margin-top: 15px;
  }

  .user-label {
    flex-basis: calc(100% - 28px);
  }

  #logoutBtn {
    width: 100%;
  }

  .stat-grid {
    grid-template-columns: 1fr;
  }

  .section-head-controls {
    flex-direction: column;
  }

  .section-head-controls select,
  .section-head-controls .btn-primary,
  .section-head > .btn-primary {
    width: 100%;
    flex-basis: auto;
  }

  td {
    grid-template-columns: 1fr;
    gap: 5px;
  }

  .log-row {
    grid-template-columns: 56px 40px minmax(0, 1fr) 34px;
    font-size: 10.5px;
  }
}
