: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);
  --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);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(circle at top left, rgba(13, 140, 255, 0.14), transparent 34rem),
    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 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.14em;
  text-transform: uppercase;
}

.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;
  min-height: 46px;
  padding: 0 13px;
  background: var(--surface);
  color: var(--text);
  outline: none;
}

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;
}

.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: repeat(5, minmax(0, 1fr));
  gap: 14px;
}

.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-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;
}

.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;
}

.modal-close {
  position: absolute;
  top: 16px;
  right: 16px;
  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: 18px;
}

.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) {
  .stats-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

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

@media (max-width: 860px) {
  .app-shell {
    display: block;
  }

  .sidebar {
    display: none;
  }

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

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

  .topbar-actions {
    width: 100%;
    justify-content: stretch;
  }

  .topbar-actions > * {
    flex: 1 1 150px;
  }

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

  .mobile-tab {
    justify-content: center;
    color: var(--primary-strong);
    background: var(--surface);
    border-color: var(--line);
  }

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

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

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

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

  .user-table thead {
    display: none;
  }

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

  .user-table tr {
    border-bottom: 1px solid var(--line);
    padding: 12px;
  }

  .user-table td {
    border: 0;
    padding: 6px 0;
  }

  .modal-card {
    border-radius: 22px;
    padding: 20px;
  }
}

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

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

  .theme-control {
    display: grid;
    align-items: stretch;
  }
}
