:root {
  --bg: #f4f7f9;
  --surface: #ffffff;
  --text: #1a2b3c;
  --text-muted: #5a6f82;
  --border: #e2eaf0;
  --primary: #1d6f8a;
  --primary-hover: #155a70;
  --primary-soft: #e8f4f8;
  --danger: #c0392b;
  --danger-soft: #fdecea;
  --success: #2d8a5e;
  --success-soft: #e8f6ef;
  --radius: 14px;
  --shadow: 0 2px 12px rgba(26, 43, 60, 0.06);
  --font: "DM Sans", system-ui, -apple-system, sans-serif;

  --c-normal: #2d8a5e;
  --c-normal-bg: #e8f6ef;
  --c-elevated: #c9a227;
  --c-elevated-bg: #fbf6e4;
  --c-stage1: #d97706;
  --c-stage1-bg: #fef3e6;
  --c-stage2: #c2410c;
  --c-stage2-bg: #feefe8;
  --c-crisis: #b91c1c;
  --c-crisis-bg: #fde8e8;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
}

.app {
  max-width: 640px;
  margin: 0 auto;
  padding: 1.25rem 1rem 2.5rem;
}

/* Header */
.header {
  display: flex;
  align-items: center;
  gap: 0.875rem;
  margin-bottom: 1.25rem;
  padding: 0.25rem 0;
  flex-wrap: wrap;
}

.header-icon {
  flex-shrink: 0;
  width: 48px;
  height: 48px;
  border-radius: 12px;
  background: linear-gradient(145deg, #1d6f8a, #2a9aad);
  color: #fff;
  display: grid;
  place-items: center;
  box-shadow: 0 4px 14px rgba(29, 111, 138, 0.35);
}

.header-text {
  flex: 1;
  min-width: 0;
}

.header h1 {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.subtitle {
  margin: 0.15rem 0 0;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.user-bar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-left: auto;
}

.user-name {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
}

/* Nav tabs */
.nav-tabs {
  display: flex;
  gap: 0.35rem;
  margin-bottom: 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.3rem;
  box-shadow: var(--shadow);
}

.nav-tab {
  flex: 1;
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  padding: 0.55rem 0.5rem;
  border: none;
  border-radius: 9px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.nav-tab:hover {
  color: var(--text);
  background: var(--bg);
}

.nav-tab.active {
  background: var(--primary-soft);
  color: var(--primary);
}

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem 1.35rem;
  margin-bottom: 1rem;
}

.card h2 {
  margin: 0 0 1rem;
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: -0.01em;
}

/* Auth */
.auth-card {
  max-width: 400px;
  margin-left: auto;
  margin-right: auto;
}

.auth-tabs {
  display: flex;
  gap: 0.35rem;
  margin-bottom: 1.25rem;
  background: var(--bg);
  border-radius: 10px;
  padding: 0.25rem;
}

.auth-tab {
  flex: 1;
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  padding: 0.55rem;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
}

.auth-tab.active {
  background: var(--surface);
  color: var(--primary);
  box-shadow: var(--shadow);
}

.auth-form .field {
  margin-bottom: 0.85rem;
}

.auth-form .field input {
  width: 100%;
  padding: 0.7rem 0.85rem;
  font-size: 1rem;
  font-family: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1.5px solid var(--border);
  border-radius: 10px;
  outline: none;
}

.auth-form .field input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(29, 111, 138, 0.15);
  background: #fff;
}

.auth-form .field label {
  display: block;
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-muted);
  margin-bottom: 0.35rem;
}

.form-hint {
  margin: 0 0 0.75rem;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.form-hint code {
  font-size: 0.75rem;
  background: var(--bg);
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
}

/* Form fields */
.fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.875rem;
}

.field:nth-child(3) {
  grid-column: 1 / -1;
}

.field label {
  display: block;
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-muted);
  margin-bottom: 0.35rem;
}

.unit {
  font-weight: 400;
  opacity: 0.85;
}

.optional {
  font-weight: 400;
  font-style: italic;
  opacity: 0.7;
}

.field input[type="number"],
.field input[type="url"],
.field input[type="text"],
.field input[type="password"],
.field input[type="file"],
.field textarea,
.datetime-wrap input[type="datetime-local"] {
  width: 100%;
  padding: 0.7rem 0.85rem;
  font-size: 1.05rem;
  font-family: inherit;
  font-weight: 500;
  color: var(--text);
  background: var(--bg);
  border: 1.5px solid var(--border);
  border-radius: 10px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.field textarea {
  font-size: 0.9rem;
  font-weight: 400;
  resize: vertical;
  min-height: 100px;
}

.field input:focus,
.field textarea:focus,
.datetime-wrap input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(29, 111, 138, 0.15);
  background: #fff;
}

.field input::placeholder,
.field textarea::placeholder {
  color: #a0b0c0;
  font-weight: 400;
}

.field input[type="file"] {
  font-size: 0.85rem;
  padding: 0.55rem;
}

/* Past reading toggle */
.past-row {
  margin-top: 1.1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}

.toggle-label {
  display: inline-flex;
  align-items: center;
  gap: 0.65rem;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 500;
  user-select: none;
}

.toggle-label input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-ui {
  width: 40px;
  height: 22px;
  background: #c5d0da;
  border-radius: 11px;
  position: relative;
  transition: background 0.2s;
  flex-shrink: 0;
}

.toggle-ui::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 18px;
  height: 18px;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
  transition: transform 0.2s;
}

.toggle-label input:checked + .toggle-ui {
  background: var(--primary);
}

.toggle-label input:checked + .toggle-ui::after {
  transform: translateX(18px);
}

.toggle-label input:focus-visible + .toggle-ui {
  box-shadow: 0 0 0 3px rgba(29, 111, 138, 0.25);
}

.datetime-wrap {
  margin-top: 0.85rem;
}

.datetime-wrap label {
  display: block;
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-muted);
  margin-bottom: 0.35rem;
}

.form-error {
  margin: 0.85rem 0 0;
  padding: 0.65rem 0.85rem;
  background: var(--danger-soft);
  color: var(--danger);
  border-radius: 8px;
  font-size: 0.875rem;
  font-weight: 500;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
  text-decoration: none;
}

.btn:active {
  transform: scale(0.98);
}

.btn-primary {
  width: 100%;
  margin-top: 1.15rem;
  padding: 0.85rem 1.25rem;
  background: var(--primary);
  color: #fff;
}

.btn-primary:hover {
  background: var(--primary-hover);
}

.btn-primary:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  transform: none;
}

.btn-secondary {
  padding: 0.55rem 1rem;
  background: var(--primary-soft);
  color: var(--primary);
  margin-top: 0.5rem;
}

.btn-secondary:hover {
  background: #d5ebf2;
}

.btn-ghost {
  padding: 0.4rem 0.75rem;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.8rem;
  border: 1px solid var(--border);
}

.btn-ghost:hover {
  background: var(--bg);
  color: var(--text);
}

.btn-delete {
  padding: 0.4rem 0.55rem;
  background: transparent;
  color: var(--text-muted);
  border-radius: 8px;
  line-height: 0;
}

.btn-delete:hover {
  background: var(--danger-soft);
  color: var(--danger);
}

/* Import */
.import-intro {
  margin: -0.35rem 0 1rem;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.fhir-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 0.75rem 0 0.25rem;
}

.btn-disabled,
a.btn-disabled {
  opacity: 0.55;
  pointer-events: none;
  cursor: not-allowed;
}

.form-success {
  margin: 0.5rem 0;
  padding: 0.65rem 0.85rem;
  background: rgba(46, 125, 90, 0.12);
  color: #1b5e40;
  border-radius: 8px;
  font-size: 0.875rem;
}

.steps {
  margin: 1rem 0 0;
  padding-left: 1.25rem;
  font-size: 0.875rem;
  color: var(--text-muted);
}

.steps li {
  margin-bottom: 0.45rem;
}

.steps strong {
  color: var(--text);
}

.or-divider {
  text-align: center;
  font-size: 0.8rem;
  color: var(--text-muted);
  margin: 0.75rem 0;
}

.portal-actions {
  margin-bottom: 0.25rem;
}

.preview-table-wrap {
  overflow-x: auto;
  margin-bottom: 0.75rem;
  max-height: 320px;
  overflow-y: auto;
}

.preview-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

.preview-table th,
.preview-table td {
  text-align: left;
  padding: 0.45rem 0.5rem;
  border-bottom: 1px solid var(--border);
}

.preview-table th {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-weight: 600;
  position: sticky;
  top: 0;
  background: var(--surface);
}

.status-new {
  color: var(--success);
  font-weight: 600;
  font-size: 0.75rem;
}

.status-dupe {
  color: var(--text-muted);
  font-size: 0.75rem;
}

.import-errors {
  list-style: none;
  margin: 0 0 0.75rem;
  padding: 0.65rem 0.85rem;
  background: var(--danger-soft);
  color: var(--danger);
  border-radius: 8px;
  font-size: 0.8rem;
}

.import-errors li {
  margin-bottom: 0.25rem;
}

/* Chart */
.chart-hint {
  margin: -0.5rem 0 0.75rem;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.chart-wrap {
  position: relative;
  width: 100%;
  min-height: 140px;
}

#trend-chart {
  display: block;
  width: 100%;
  height: auto;
  max-height: 200px;
}

.chart-empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  margin: 0;
  font-size: 0.875rem;
  color: var(--text-muted);
  text-align: center;
  padding: 1rem;
}

.chart-empty[hidden],
#trend-chart[hidden] {
  display: none !important;
}

.legend {
  display: flex;
  gap: 1.25rem;
  margin-top: 0.75rem;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}

.swatch {
  width: 12px;
  height: 3px;
  border-radius: 2px;
}

.swatch.systolic {
  background: #1d6f8a;
}

.swatch.diastolic {
  background: #5b9eb8;
}

/* History */
.history-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}

.history-header h2 {
  margin: 0;
}

.count {
  font-size: 0.8rem;
  color: var(--text-muted);
  font-weight: 500;
}

.history-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.history-item {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  padding: 0.9rem 0;
  border-bottom: 1px solid var(--border);
}

.history-item:last-child {
  border-bottom: none;
  padding-bottom: 0;
}

.history-item:first-child {
  padding-top: 0.25rem;
}

.reading-values {
  flex: 1;
  min-width: 0;
}

.bp-line {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.35rem 0.65rem;
}

.bp-nums {
  font-size: 1.2rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}

.bp-slash {
  color: var(--text-muted);
  font-weight: 500;
}

.pulse {
  font-size: 0.85rem;
  color: var(--text-muted);
  font-weight: 500;
}

.meta {
  margin-top: 0.3rem;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem 0.65rem;
}

.timestamp {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.history-empty {
  margin: 0.5rem 0 0;
  font-size: 0.9rem;
  color: var(--text-muted);
  text-align: center;
  padding: 1.5rem 0.5rem;
}

.history-empty[hidden] {
  display: none;
}

/* Badges */
.badge {
  display: inline-block;
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  padding: 0.2rem 0.5rem;
  border-radius: 6px;
  white-space: nowrap;
}

.badge-normal {
  color: var(--c-normal);
  background: var(--c-normal-bg);
}

.badge-elevated {
  color: #9a7b12;
  background: var(--c-elevated-bg);
}

.badge-stage1 {
  color: var(--c-stage1);
  background: var(--c-stage1-bg);
}

.badge-stage2 {
  color: var(--c-stage2);
  background: var(--c-stage2-bg);
}

.badge-crisis {
  color: var(--c-crisis);
  background: var(--c-crisis-bg);
}

/* Footer */
.footer {
  margin-top: 0.5rem;
  padding: 0 0.25rem;
}

.categories-info {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.categories-info summary {
  cursor: pointer;
  font-weight: 500;
  color: var(--primary);
  list-style: none;
}

.categories-info summary::-webkit-details-marker {
  display: none;
}

.categories-info summary::before {
  content: "▸ ";
  display: inline-block;
  transition: transform 0.15s;
}

.categories-info[open] summary::before {
  transform: rotate(90deg);
}

.cat-list {
  list-style: none;
  margin: 0.75rem 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.cat-list li {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.disclaimer {
  margin: 0.85rem 0 0;
  font-size: 0.75rem;
  line-height: 1.45;
  opacity: 0.85;
}

[hidden] {
  display: none !important;
}

/* Mobile tweaks */
@media (max-width: 420px) {
  .fields {
    grid-template-columns: 1fr;
  }

  .field:nth-child(3) {
    grid-column: auto;
  }

  .header h1 {
    font-size: 1.2rem;
  }

  .bp-nums {
    font-size: 1.1rem;
  }

  .user-bar {
    width: 100%;
    margin-left: 0;
    justify-content: flex-end;
  }
}

@media (min-width: 480px) {
  .fields {
    grid-template-columns: 1fr 1fr 1fr;
  }

  .field:nth-child(3) {
    grid-column: auto;
  }
}
