:root {
  color-scheme: dark;
  --bg: #0b1220;
  --panel: #121b2e;
  --panel-soft: #18233a;
  --text: #edf2ff;
  --muted: #9fb0d0;
  --line: rgba(255, 255, 255, 0.08);
  --accent: #4fd1c5;
  --accent-soft: rgba(79, 209, 197, 0.15);
  --low: #f56565;
  --high: #ed8936;
  --in-range: #48bb78;
  --shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background:
    radial-gradient(circle at top left, rgba(79, 209, 197, 0.12), transparent 30%),
    radial-gradient(circle at top right, rgba(99, 102, 241, 0.12), transparent 25%),
    var(--bg);
  color: var(--text);
  min-height: 100vh;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
  -webkit-text-size-adjust: 100%;
}

.page {
  width: min(1100px, calc(100% - 2rem));
  margin: 0 auto;
  padding: 2rem 0 calc(3rem + env(safe-area-inset-bottom));
}

.header {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: end;
  margin-bottom: 1.5rem;
}

.eyebrow {
  margin: 0 0 0.35rem;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-size: 0.75rem;
  font-weight: 700;
}

h1,
h2 {
  margin: 0;
}

h1 {
  font-size: clamp(1.8rem, 4vw, 2.6rem);
}

.header-actions {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  flex-wrap: wrap;
}

.select-label {
  color: var(--muted);
  font-size: 0.85rem;
}

select,
button {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  border-radius: 999px;
  padding: 0.65rem 1rem;
  font: inherit;
  min-height: 44px;
  -webkit-tap-highlight-color: transparent;
}

button {
  background: linear-gradient(135deg, #38b2ac, #319795);
  border: none;
  cursor: pointer;
  font-weight: 600;
}

button:hover {
  filter: brightness(1.05);
}

button:active {
  filter: brightness(0.95);
}

.status-banner {
  margin-bottom: 1rem;
  padding: 1rem 1.2rem;
  border-radius: 16px;
  background: rgba(245, 101, 101, 0.12);
  border: 1px solid rgba(245, 101, 101, 0.35);
}

.status-banner.hidden,
.dashboard.hidden,
.loading.hidden {
  display: none;
}

.loading {
  display: grid;
  place-items: center;
  gap: 1rem;
  min-height: 40vh;
  color: var(--muted);
}

.spinner {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.12);
  border-top-color: var(--accent);
  animation: spin 0.9s linear infinite;
}

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

.dashboard {
  display: grid;
  gap: 1rem;
}

.hero-card,
.stat-card,
.sensor-card,
.chart-card,
.table-card {
  background: rgba(18, 27, 46, 0.92);
  border: 1px solid var(--line);
  border-radius: 24px;
  box-shadow: var(--shadow);
}

.hero-card {
  display: grid;
  grid-template-columns: 1.4fr 1fr;
  gap: 1.5rem;
  padding: 1.5rem;
}

.value-row {
  display: flex;
  align-items: baseline;
  gap: 0.75rem;
}

.current-value {
  font-size: clamp(3rem, 10vw, 5rem);
  line-height: 1;
  font-weight: 800;
}

.current-value.low,
.current-value.high,
.current-mmol.low,
.current-mmol.high,
.reading-value.low,
.reading-value.high {
  color: var(--low);
}

.current-unit,
.current-mmol,
.current-time {
  color: var(--muted);
}

.current-time.stale {
  color: var(--low);
  font-weight: 600;
}

.current-arrow {
  font-size: 2rem;
  color: var(--accent);
}

.hero-meta {
  display: grid;
  gap: 0.85rem;
}

.meta-item {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.85rem 1rem;
  border-radius: 16px;
  background: var(--panel-soft);
}

.meta-label {
  color: var(--muted);
}

.status-pill {
  padding: 0.2rem 0.65rem;
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 700;
}

.status-pill.low {
  background: rgba(245, 101, 101, 0.15);
  color: var(--low);
}

.status-pill.high {
  background: rgba(237, 137, 54, 0.15);
  color: var(--high);
}

.status-pill.in-range {
  background: rgba(72, 187, 120, 0.15);
  color: var(--in-range);
}

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

.stat-card {
  padding: 1.2rem;
}

.stat-label,
.stat-sub,
.section-head p,
.meta-label,
table th {
  color: var(--muted);
}

.stat-card strong {
  display: block;
  margin-top: 0.35rem;
  font-size: 1.8rem;
}

.chart-card,
.table-card {
  padding: 1.2rem 1.2rem 1.5rem;
}

.sensor-card {
  padding: 1.2rem 1.5rem 1.4rem;
}

.sensor-progress-wrap {
  display: grid;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.sensor-progress {
  height: 10px;
  border-radius: 999px;
  background: var(--panel-soft);
  overflow: hidden;
}

.sensor-progress-bar {
  height: 100%;
  width: 0;
  border-radius: inherit;
  background: linear-gradient(90deg, #38b2ac, #4fd1c5);
  transition: width 0.4s ease;
}

.sensor-progress-bar.grace {
  background: linear-gradient(90deg, #ed8936, #f6ad55);
}

.sensor-progress-bar.expired {
  background: linear-gradient(90deg, #c53030, #f56565);
}

.sensor-remaining {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.sensor-reset-btn {
  justify-self: start;
  margin-top: 0.25rem;
}

.sensor-meta {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.85rem;
}

.sensor-note {
  margin: 0.85rem 0 0;
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.45;
}

.sensor-note.hidden {
  display: none;
}

.sensor-controls {
  margin-top: 1.25rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--line);
  display: grid;
  gap: 1rem;
}

.sensor-form {
  display: grid;
  gap: 0.85rem;
}

.sensor-form-row {
  display: grid;
  gap: 0.45rem;
}

.sensor-form-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.65rem;
}

.sensor-btn {
  appearance: none;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel-soft);
  color: var(--text);
  padding: 0.7rem 1rem;
  font: inherit;
  cursor: pointer;
}

.sensor-btn:hover {
  border-color: rgba(79, 209, 197, 0.45);
}

.sensor-btn-primary {
  background: linear-gradient(135deg, #38b2ac, #4fd1c5);
  border-color: transparent;
  color: #0b1220;
  font-weight: 700;
}

.sensor-btn-muted {
  color: var(--muted);
}

.sensor-btn:disabled {
  opacity: 0.6;
  cursor: wait;
}

.chart-wrap {
  position: relative;
  height: 320px;
}

.chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  margin-top: 0.75rem;
  font-size: 0.85rem;
  color: #9fb0d0;
}

.chart-legend-item::before {
  content: "";
  display: inline-block;
  width: 1.5rem;
  height: 0;
  margin-right: 0.4rem;
  vertical-align: middle;
  border-top: 2px solid currentColor;
}

.chart-legend-normal::before {
  color: #4fd1c5;
}

.chart-legend-gap::before {
  color: #f56565;
  border-top-style: dashed;
}

.section-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 1rem;
}

.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th,
td {
  padding: 0.85rem 0.5rem;
  text-align: left;
  border-bottom: 1px solid var(--line);
}

tbody tr:hover {
  background: rgba(255, 255, 255, 0.03);
}

@media (max-width: 900px) {
  .hero-card {
    grid-template-columns: 1fr;
  }

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

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

  .header-actions {
    width: 100%;
  }
}

@media (max-width: 640px) {
  .page {
    width: calc(100% - 1.25rem);
    padding: 1.25rem 0 calc(2rem + env(safe-area-inset-bottom));
  }

  .header {
    margin-bottom: 1rem;
    gap: 1.25rem;
  }

  .header-actions {
    flex-direction: column;
    align-items: stretch;
    gap: 0.5rem;
  }

  .select-label {
    font-size: 0.8rem;
  }

  select,
  button {
    width: 100%;
    text-align: center;
  }

  .hero-card,
  .stat-card,
  .sensor-card,
  .chart-card,
  .table-card {
    border-radius: 18px;
  }

  .hero-card {
    padding: 1.15rem;
    gap: 1.15rem;
  }

  .value-row {
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  .current-arrow {
    font-size: 1.6rem;
  }

  .hero-meta {
    gap: 0.65rem;
  }

  .meta-item {
    padding: 0.75rem 0.85rem;
    font-size: 0.9rem;
  }

  #current-trend {
    text-align: right;
    max-width: 58%;
    line-height: 1.35;
  }

  .stat-card {
    padding: 1rem;
  }

  .stat-card strong {
    font-size: 1.45rem;
  }

  .chart-card,
  .table-card {
    padding: 1rem 1rem 1.15rem;
  }

  .sensor-card {
    padding: 1rem;
  }

  .sensor-meta {
    grid-template-columns: 1fr;
  }

  .chart-wrap {
    height: 220px;
  }

  .section-head {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.2rem;
    margin-bottom: 0.85rem;
  }

  .section-head p {
    font-size: 0.85rem;
  }

  .table-wrap {
    overflow-x: visible;
  }

  .readings-table thead {
    display: none;
  }

  .readings-table tbody tr {
    display: block;
    padding: 0.85rem 0;
    border-bottom: 1px solid var(--line);
  }

  .readings-table tbody tr:last-child {
    border-bottom: none;
  }

  .readings-table tbody tr:hover {
    background: transparent;
  }

  .readings-table td {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 1rem;
    padding: 0.35rem 0;
    border-bottom: none;
    font-size: 0.9rem;
  }

  .readings-table td::before {
    content: attr(data-label);
    color: var(--muted);
    font-size: 0.8rem;
    flex-shrink: 0;
  }

  .readings-table td.reading-primary {
    display: block;
    padding: 0 0 0.5rem;
    font-size: 1rem;
    font-weight: 600;
  }

  .readings-table td.reading-primary::before {
    display: none;
  }

  .readings-table .reading-value {
    font-size: 1.05rem;
  }
}

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

  h1 {
    font-size: 1.65rem;
  }

  .current-value {
    font-size: clamp(2.5rem, 14vw, 3.5rem);
  }
}

.auth-page .page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding-top: 1.5rem;
}

.auth-shell {
  width: min(420px, calc(100% - 1.5rem));
}

.auth-card,
.settings-card {
  background: rgba(18, 27, 46, 0.92);
  border: 1px solid var(--line);
  border-radius: 24px;
  box-shadow: var(--shadow);
  padding: 1.5rem;
}

.auth-lead {
  margin: 0.75rem 0 1.25rem;
  color: var(--muted);
  line-height: 1.5;
}

.auth-form,
.settings-form {
  display: grid;
  gap: 0.85rem;
}

.field-label {
  color: var(--muted);
  font-size: 0.85rem;
}

.text-input {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--panel-soft);
  color: var(--text);
  border-radius: 14px;
  padding: 0.8rem 0.95rem;
  font: inherit;
  min-height: 44px;
}

.text-input:focus {
  outline: 2px solid rgba(79, 209, 197, 0.35);
  border-color: rgba(79, 209, 197, 0.45);
}

.auth-submit,
.settings-form button {
  width: 100%;
  margin-top: 0.35rem;
}

.button-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  border-radius: 999px;
  padding: 0.65rem 1rem;
  font: inherit;
  min-height: 44px;
  font-weight: 600;
  text-decoration: none;
}

.button-link:hover {
  filter: brightness(1.05);
}

.settings-status {
  margin: 0.85rem 0 0;
  font-size: 0.9rem;
}

.settings-status.success {
  color: var(--in-range);
}

.settings-status.error {
  color: var(--low);
}

.settings-status.hidden {
  display: none;
}

.config-page {
  display: grid;
  gap: 1rem;
}

.config-lead {
  margin: 0 0 1rem;
  color: var(--muted);
  line-height: 1.5;
}

.config-list {
  display: grid;
  gap: 0.75rem;
  margin-bottom: 0.85rem;
}

.config-list-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 0.75rem;
  align-items: center;
}

.config-list-field {
  min-width: 0;
}

.config-secondary-btn,
.config-primary-btn,
.config-remove-btn {
  width: auto;
  border-radius: 14px;
  padding: 0.7rem 1rem;
  font: inherit;
  cursor: pointer;
}

.config-secondary-btn {
  background: var(--panel-soft);
  border: 1px solid var(--line);
  color: var(--text);
  margin-bottom: 0.75rem;
  margin-right: 0.75rem;
}

.config-primary-btn {
  width: 100%;
  margin-top: 1rem;
}

.config-remove-btn {
  background: rgba(245, 101, 101, 0.12);
  border: 1px solid rgba(245, 101, 101, 0.35);
  color: var(--low);
  white-space: nowrap;
}

.config-checkbox {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.45;
}

.config-checkbox input {
  margin-top: 0.2rem;
}

@media (max-width: 640px) {
  .auth-card,
  .settings-card {
    border-radius: 18px;
    padding: 1.15rem;
  }
}
