:root {
  color-scheme: light;
  --bg: #eef5fb;
  --surface: #ffffff;
  --surface-soft: #f6faff;
  --surface-strong: #e9f4ff;
  --text: #122033;
  --muted: #617086;
  --line: #d7e3ee;
  --primary: #0d8cff;
  --primary-strong: #0767c4;
  --primary-soft: #dcefff;
  --success: #14a35b;
  --success-soft: #def8eb;
  --warning: #d58b00;
  --warning-soft: #fff3d4;
  --danger: #d92d42;
  --danger-soft: #ffe2e7;
  --shadow: 0 18px 45px rgba(33, 64, 94, 0.14);
  --page-glow: radial-gradient(circle at 12% -8%, rgba(13, 140, 255, 0.16), transparent 32rem);
  --radius: 22px;
  --radius-small: 14px;
  --sidebar-width: 286px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #07111d;
  --surface: #0d1a2a;
  --surface-soft: #111f31;
  --surface-strong: #14283e;
  --text: #eef7ff;
  --muted: #9eb0c5;
  --line: #22374d;
  --primary: #4dabff;
  --primary-strong: #89c9ff;
  --primary-soft: #102d49;
  --success: #38d17f;
  --success-soft: #103321;
  --warning: #ffc14a;
  --warning-soft: #3b2b0c;
  --danger: #ff6677;
  --danger-soft: #3d111b;
  --shadow: 0 20px 55px rgba(0, 0, 0, 0.35);
  --page-glow: radial-gradient(circle at 18% -10%, rgba(77, 171, 255, 0.10), transparent 30rem);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--page-glow), var(--bg);
  color: var(--text);
}

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  gap: 26px;
  padding: 24px;
  background: linear-gradient(180deg, #0767c4, #0d8cff);
  color: white;
}

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

.brand-mark,
.login-logo {
  display: grid;
  place-items: center;
  background: white;
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.18);
}

.brand-mark {
  width: 62px;
  height: 62px;
  border-radius: 19px;
}

.brand-mark img,
.login-logo img {
  width: 76%;
  height: 76%;
  object-fit: contain;
}

.brand-title {
  font-size: 24px;
  font-weight: 900;
  letter-spacing: 0;
}

.brand-subtitle {
  margin-top: 3px;
  font-size: 13px;
  font-weight: 800;
  opacity: 0.84;
}

.nav-list {
  display: grid;
  gap: 10px;
}

.nav-button,
.mobile-tab {
  border: 1px solid transparent;
  color: inherit;
  background: rgba(255, 255, 255, 0.12);
  border-radius: 16px;
  min-height: 50px;
  padding: 0 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  font-weight: 850;
}

.nav-button.active,
.mobile-tab.active {
  background: rgba(255, 255, 255, 0.96);
  color: #075da9;
  box-shadow: 0 14px 30px rgba(0, 0, 0, 0.14);
}

.nav-icon {
  font-size: 20px;
  line-height: 1;
}

.sidebar-footer {
  margin-top: auto;
}

.session-chip {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  border-radius: 999px;
  padding: 10px 13px;
  background: rgba(0, 0, 0, 0.16);
  font-size: 13px;
  font-weight: 800;
}

.status-dot {
  width: 9px;
  height: 9px;
  border-radius: 999px;
  background: #ffd166;
}

.status-dot.ready {
  background: #3ee38b;
}

.main-panel {
  padding: max(22px, env(safe-area-inset-top)) max(22px, env(safe-area-inset-right)) max(32px, env(safe-area-inset-bottom)) max(22px, env(safe-area-inset-left));
  min-width: 0;
}

.topbar,
.section-header,
.panel-title-row,
.pagination-row,
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
}

.topbar {
  margin-bottom: 18px;
}

.topbar-title {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.topbar-logo {
  display: none;
  place-items: center;
  width: 58px;
  height: 58px;
  flex: 0 0 auto;
  border-radius: 18px;
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.12);
  padding: 0;
}

.topbar-logo img {
  width: 76%;
  height: 76%;
  object-fit: contain;
}

.topbar h1,
.section-header h2,
.login-card h2,
.modal-header h2 {
  margin: 0;
  line-height: 1.08;
}

.topbar h1 {
  font-size: clamp(30px, 4vw, 48px);
  font-weight: 950;
}

.topbar-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  flex-wrap: wrap;
}

.theme-control {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 850;
}

.theme-control select {
  min-width: 96px;
}

.mobile-tabs {
  display: none;
}

.content-view,
.login-panel {
  display: none;
}

.content-view.active,
.login-panel.active {
  display: block;
}

.login-panel {
  min-height: min(680px, calc(100vh - 140px));
  place-items: center;
}

.login-panel.active {
  display: grid;
}

.login-card {
  width: min(100%, 460px);
  border: 1px solid var(--line);
  border-radius: 30px;
  padding: 30px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.login-logo {
  width: 82px;
  height: 82px;
  border-radius: 26px;
  margin-bottom: 18px;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--primary-strong);
  font-size: 12px;
  font-weight: 950;
  letter-spacing: 0.04em;
  text-transform: none;
}

.muted-text {
  color: var(--muted);
  margin: 4px 0 0;
}

.api-key-form,
.filter-card,
.detail-grid {
  display: grid;
  gap: 14px;
}

.api-key-form {
  margin-top: 20px;
}

label {
  display: grid;
  gap: 7px;
  color: var(--text);
  font-size: 13px;
  font-weight: 900;
}

input,
select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 14px;
  height: 46px;
  min-height: 46px;
  padding: 0 13px;
  background: var(--surface);
  color: var(--text);
  outline: none;
  line-height: 46px;
}

input:focus,
select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--primary) 18%, transparent);
}

.primary-button,
.ghost-button,
.danger-ghost-button,
.warning-button,
.danger-button {
  border: 1px solid transparent;
  border-radius: 15px;
  min-height: 46px;
  padding: 0 16px;
  font-weight: 950;
  transition: transform 120ms ease, box-shadow 120ms ease, background 120ms ease;
}

.primary-button:active,
.ghost-button:active,
.danger-ghost-button:active,
.warning-button:active,
.danger-button:active,
.nav-button:active,
.mobile-tab:active {
  transform: scale(0.98);
}

.primary-button {
  background: linear-gradient(180deg, var(--primary), #0877d9);
  color: white;
  box-shadow: 0 12px 24px color-mix(in srgb, var(--primary) 26%, transparent);
}

.primary-button.compact {
  min-width: 112px;
}

.section-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
}

.ghost-button {
  border-color: var(--line);
  color: var(--primary-strong);
  background: var(--surface);
}

.danger-ghost-button {
  border-color: color-mix(in srgb, var(--danger) 34%, var(--line));
  color: var(--danger);
  background: var(--surface);
}

.warning-button {
  background: var(--warning);
  color: #1b1608;
}

.danger-button {
  background: var(--danger);
  color: white;
}

.section-header {
  margin: 22px 0 14px;
}

.section-header h2 {
  font-size: clamp(22px, 3vw, 32px);
}

.stats-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 14px;
}

.stat-section {
  display: grid;
  gap: 9px;
}

.stat-section-title {
  color: var(--muted);
  font-size: 12px;
  font-weight: 950;
  letter-spacing: 0.10em;
  text-transform: uppercase;
  padding-left: 4px;
}

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

.stat-card,
.panel-card,
.filter-card {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.stat-card {
  min-height: 124px;
  padding: 18px;
  display: grid;
  align-content: space-between;
}

.stat-success {
  border-color: color-mix(in srgb, var(--success) 35%, var(--line));
  background: var(--success-soft);
}

.stat-danger {
  border-color: color-mix(in srgb, var(--danger) 35%, var(--line));
  background: var(--danger-soft);
}

.stat-label {
  color: var(--muted);
  font-size: 13px;
  font-weight: 900;
}

.stat-value {
  font-size: 34px;
  font-weight: 950;
  line-height: 1;
}

.panel-card,
.filter-card {
  margin-top: 16px;
  padding: 18px;
}

.table-wrap {
  overflow-x: auto;
  margin-top: 14px;
  border: 1px solid var(--line);
  border-radius: 18px;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 760px;
}

.data-table th,
.data-table td {
  padding: 13px 14px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: middle;
}

.data-table th {
  color: var(--muted);
  font-size: 12px;
  font-weight: 950;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  background: var(--surface-soft);
}

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

.data-table tbody tr {
  cursor: pointer;
}

.data-table tbody tr:hover {
  background: var(--surface-soft);
}

.person-cell {
  display: grid;
  gap: 3px;
}

.person-name {
  font-weight: 950;
}

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

.status-badge,
.access-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 28px;
  border-radius: 999px;
  padding: 0 10px;
  font-size: 12px;
  font-weight: 950;
}

.status-active {
  color: var(--success);
  background: var(--success-soft);
}

.status-locked,
.status-revoked {
  color: var(--danger);
  background: var(--danger-soft);
}

.access-ambulance {
  color: var(--primary-strong);
  background: var(--primary-soft);
}

.access-other {
  color: #078a91;
  background: #d9fbff;
}

:root[data-theme="dark"] .access-other {
  color: #58f0f6;
  background: #102f34;
}

.filter-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
}

.filter-card.collapsed {
  display: none;
}

.search-field {
  grid-column: 1 / -1;
}

.pagination-row {
  padding-top: 16px;
}

.pagination-row button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 18px;
  background: rgba(3, 10, 18, 0.58);
  backdrop-filter: blur(10px);
}

.modal-backdrop.open {
  display: flex;
}

.modal-card {
  position: relative;
  width: min(940px, 100%);
  max-height: min(860px, calc(100vh - 34px));
  overflow: auto;
  border-radius: 28px;
  border: 1px solid var(--line);
  background: var(--surface);
  box-shadow: var(--shadow);
  padding: 24px;
  padding-top: 68px;
}

.modal-close {
  position: absolute;
  top: 14px;
  right: 14px;
  width: 42px;
  height: 42px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface-soft);
  color: var(--text);
  font-size: 26px;
  line-height: 1;
}

.detail-grid {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  margin-top: 18px;
}

.detail-item {
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 12px;
  background: var(--surface-soft);
}

.detail-label {
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 950;
}

.detail-value {
  margin-top: 5px;
  overflow-wrap: anywhere;
  font-weight: 850;
}

.action-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 10px;
  margin-top: 16px;
  margin-bottom: 18px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--line);
}

.toast-region {
  position: fixed;
  left: 50%;
  bottom: max(18px, env(safe-area-inset-bottom));
  z-index: 60;
  display: grid;
  gap: 8px;
  transform: translateX(-50%);
  width: min(92vw, 440px);
}

.toast {
  border-radius: 16px;
  padding: 13px 14px;
  background: var(--text);
  color: var(--surface);
  box-shadow: var(--shadow);
  font-weight: 850;
}

.empty-state {
  padding: 28px;
  text-align: center;
  color: var(--muted);
  font-weight: 850;
}

@media (max-width: 1180px) {
  .stat-section-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .filter-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 860px) {
  body {
    background:
      linear-gradient(180deg, color-mix(in srgb, var(--primary) 13%, var(--bg)) 0, var(--bg) 260px),
      var(--bg);
  }

  .app-shell {
    display: block;
  }

  .sidebar {
    display: none;
  }

  .main-panel {
    padding: max(14px, env(safe-area-inset-top)) 10px max(24px, env(safe-area-inset-bottom));
  }

  .topbar {
    align-items: flex-start;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 12px;
    margin-bottom: 12px;
    border: 1px solid color-mix(in srgb, var(--line) 80%, transparent);
    border-radius: 24px;
    padding: 14px;
    background: color-mix(in srgb, var(--surface) 88%, transparent);
    box-shadow: var(--shadow);
    backdrop-filter: blur(16px);
  }

  .topbar h1 {
    font-size: clamp(23px, 6vw, 27px);
    white-space: nowrap;
  }

  .topbar .eyebrow {
    font-size: 10px;
    color: var(--muted);
    letter-spacing: 0.09em;
  }

  .topbar-logo {
    display: grid;
  }

  .topbar-title {
    min-height: 58px;
  }

  .topbar-actions {
    grid-column: 1 / -1;
    width: 100%;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
    max-height: 0;
    overflow: hidden;
    opacity: 0;
    pointer-events: none;
    transition: max-height 180ms ease, opacity 160ms ease;
  }

  .topbar-actions.show-api-tools {
    max-height: 120px;
    opacity: 1;
    pointer-events: auto;
  }

  .topbar-actions > * {
    min-width: 0;
  }

  .theme-control {
    position: absolute;
    top: 12px;
    right: 12px;
    display: block;
    width: 94px;
    z-index: 2;
    opacity: 1;
    pointer-events: auto;
  }

  .theme-control span {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
  }

  .theme-control select {
    min-width: 0;
    min-height: 36px;
    height: 36px;
    border-radius: 999px;
    padding: 0 27px 0 12px;
    font-size: 12px;
    font-weight: 950;
    background: var(--primary-soft);
    border-color: color-mix(in srgb, var(--primary) 34%, var(--line));
    color: var(--primary-strong);
  }

  .topbar-actions .ghost-button,
  .topbar-actions .danger-ghost-button {
    min-height: 44px;
    padding: 0 10px;
    font-size: 14px;
  }

  .mobile-tabs {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin: 10px 0 18px;
  }

  .mobile-tab {
    justify-content: center;
    color: var(--primary-strong);
    background: color-mix(in srgb, var(--surface) 92%, transparent);
    border-color: var(--line);
    box-shadow: 0 10px 24px rgba(0, 0, 0, 0.10);
  }

  .mobile-tab.active {
    background: var(--primary);
    color: white;
  }

  .filter-grid,
  .detail-grid {
    grid-template-columns: 1fr;
  }

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

  .action-grid button {
    min-height: 42px;
    padding: 0 10px;
    border-radius: 13px;
    font-size: 13px;
  }

  .stats-grid {
    gap: 13px;
  }

  .stat-section {
    gap: 7px;
  }

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

  .stat-card {
    min-height: 76px;
    padding: 11px;
    border-radius: 16px;
  }

  .stat-label {
    font-size: 10px;
    line-height: 1.15;
  }

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

  .section-header {
    align-items: end;
    margin: 20px 2px 12px;
  }

  .section-header h2 {
    font-size: 24px;
  }

  .section-header .primary-button.compact {
    min-width: 92px;
    min-height: 44px;
  }

  .section-actions {
    gap: 8px;
    flex-wrap: wrap;
  }

  .section-actions .compact {
    min-width: 78px;
    min-height: 42px;
    padding: 0 12px;
  }

  .panel-card,
  .filter-card {
    padding: 14px;
    border-radius: 22px;
    margin-top: 14px;
  }

  input,
  select {
    height: 46px;
    min-height: 46px;
    line-height: 46px;
    border-radius: 13px;
  }

  .data-table {
    min-width: 0;
  }

  .data-table thead {
    display: none;
  }

  .data-table,
  .data-table tbody,
  .data-table tr,
  .data-table td {
    display: block;
    width: 100%;
  }

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

  .data-table tbody {
    display: grid;
    gap: 10px;
  }

  .data-table tr {
    border: 1px solid var(--line);
    border-radius: 18px;
    background: var(--surface-soft);
    box-shadow: 0 12px 28px rgba(0, 0, 0, 0.10);
    border-bottom: 1px solid var(--line);
    padding: 12px;
  }

  .data-table td {
    display: grid;
    grid-template-columns: 94px minmax(0, 1fr);
    gap: 10px;
    align-items: start;
    border: 0;
    padding: 7px 0;
  }

  .data-table td::before {
    color: var(--muted);
    font-size: 11px;
    font-weight: 950;
    letter-spacing: 0.08em;
    text-transform: uppercase;
  }

  .recent-table td:nth-child(1)::before,
  .user-table td:nth-child(1)::before {
    content: "Name";
  }

  .recent-table td:nth-child(2)::before,
  .user-table td:nth-child(2)::before {
    content: "Status";
  }

  .recent-table td:nth-child(3)::before,
  .user-table td:nth-child(3)::before {
    content: "Access";
  }

  .recent-table td:nth-child(4)::before {
    content: "Platform";
  }

  .recent-table td:nth-child(5)::before,
  .user-table td:nth-child(6)::before {
    content: "Last Seen";
  }

  .user-table td:nth-child(4)::before {
    content: "Role";
  }

  .user-table td:nth-child(5)::before {
    content: "Device";
  }

  .person-cell {
    gap: 2px;
  }

  .status-badge,
  .access-badge {
    width: fit-content;
    max-width: 100%;
    white-space: normal;
    text-align: center;
  }

  .modal-card {
    border-radius: 22px;
    width: calc(100vw - 18px);
    max-height: calc(100vh - 18px);
    padding: 18px;
    padding-top: 66px;
  }

  .modal-header {
    align-items: flex-start;
    padding-right: 0;
  }

  .modal-header h2 {
    font-size: 24px;
  }

  .modal-close {
    top: 12px;
    right: 12px;
  }
}

@media (max-width: 520px) {
  .login-card,
  .panel-card,
  .filter-card {
    border-radius: 20px;
  }

  .topbar {
    border-radius: 22px;
  }

  .topbar-logo {
    width: 52px;
    height: 52px;
    border-radius: 16px;
  }

  .topbar-title {
    padding-right: 100px;
  }

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

  .stat-section-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .stat-card {
    min-height: 74px;
  }

  .data-table td {
    grid-template-columns: 82px minmax(0, 1fr);
  }
}

@media (max-width: 360px) {
  .stats-grid {
    grid-template-columns: 1fr;
  }

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