:root {
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #222221;
  --border: rgba(255, 255, 255, 0.10);
  --ink: #ffffff;
  --ink-secondary: #c3c2b7;
  --ink-muted: #898781;
  --grid: #2c2c2a;
  --accent: #3987e5;
  --accent-dark: #2a6fc4;

  --status-low: #3987e5;
  --status-good: #0ca30c;
  --status-warning: #fab219;
  --status-serious: #ec835a;
  --status-critical: #d03b3b;

  color-scheme: dark;
  --radius: 14px;
  --fab-size: 58px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

button {
  transition: transform 0.08s ease;
  font-family: inherit;
}
button:active { transform: scale(0.95); }

html, body {
  margin: 0;
  padding: 0;
  background: var(--page);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body {
  min-height: 100dvh;
  padding-bottom: calc(96px + env(safe-area-inset-bottom));
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(14px + env(safe-area-inset-top)) 18px 14px;
  background: rgba(13, 13, 13, 0.92);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}

.topbar h1 {
  font-size: 1.3rem;
  font-weight: 700;
  margin: 0;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: var(--surface-2);
  color: var(--ink);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.icon-btn:active { background: var(--border); }

.list {
  max-width: 560px;
  margin: 0 auto;
  padding: 14px 14px 8px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.empty-state {
  text-align: center;
  color: var(--ink-muted);
  padding: 60px 20px;
  font-size: 0.95rem;
  line-height: 1.5;
}

.reading-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
}
.reading-card:active { background: var(--surface-2); }

.reading-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  flex: none;
}

.reading-main {
  flex: 1;
  min-width: 0;
}

.reading-values {
  display: flex;
  align-items: baseline;
  gap: 12px;
  flex-wrap: nowrap;
  white-space: nowrap;
  line-height: 1.15;
}

.reading-bp {
  font-size: clamp(1.3rem, 6.4vw, 1.7rem);
  font-weight: 700;
}

.reading-pulse {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: clamp(1rem, 4.4vw, 1.2rem);
  font-weight: 600;
  color: var(--ink-secondary);
}
.reading-pulse svg { color: var(--ink-muted); flex: none; }

.reading-status {
  font-size: 0.82rem;
  font-weight: 600;
  margin-top: 2px;
}

.reading-meta {
  text-align: right;
  flex: none;
  color: var(--ink-secondary);
  font-size: 0.85rem;
  line-height: 1.4;
}

.reading-meta .count {
  color: var(--ink-muted);
  font-size: 0.78rem;
}

.fab {
  position: fixed;
  right: 20px;
  bottom: calc(22px + env(safe-area-inset-bottom));
  width: var(--fab-size);
  height: var(--fab-size);
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 10px 28px rgba(57, 135, 229, 0.4);
  cursor: pointer;
  z-index: 20;
}
.fab:active { background: var(--accent-dark); }

/* --- Sheet overlay (add/edit + chart) --- */
.sheet-overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: var(--page);
}

.sheet {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  max-width: 560px;
  margin: 0 auto;
}

.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(14px + env(safe-area-inset-top)) 16px 14px;
  border-bottom: 1px solid var(--border);
  flex: none;
}

.sheet-header h2 {
  font-size: 1.15rem;
  margin: 0;
}

.sheet-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  -webkit-overflow-scrolling: touch;
}

.sheet-footer {
  flex: none;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--page);
}

.btn-primary {
  width: 100%;
  border: none;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-size: 1.05rem;
  font-weight: 700;
  padding: 15px;
  cursor: pointer;
}
.btn-primary:active { background: var(--accent-dark); }

.field-row {
  display: flex;
  gap: 12px;
  margin-bottom: 18px;
}

.field {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.82rem;
  color: var(--ink-secondary);
}

.field input {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  color: var(--ink);
  font-size: 1rem;
  font-family: inherit;
  color-scheme: dark;
}
.field input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.measurements-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 6px;
}

.measurement-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.measurement-index {
  width: 14px;
  color: var(--ink-muted);
  font-size: 0.8rem;
  flex: none;
}

.measurement-input {
  flex: 1;
  min-width: 0;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 4px;
  color: var(--ink);
  font-size: 1.1rem;
  font-weight: 600;
  text-align: center;
  font-family: inherit;
}
.measurement-input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.measurement-input::placeholder { color: var(--ink-muted); font-weight: 400; font-size: 0.8rem; }

.measurement-sep {
  color: var(--ink-muted);
  font-weight: 700;
  flex: none;
}

.measurement-remove {
  flex: none;
  width: 28px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: var(--ink-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.measurement-remove:active { background: var(--surface-2); color: var(--status-critical); }

.link-btn {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  color: var(--accent);
  font-size: 0.95rem;
  font-weight: 600;
  padding: 10px 2px;
  cursor: pointer;
}

.link-btn-danger {
  color: var(--status-critical);
  margin-top: 10px;
}

/* --- Toast --- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(100px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(16px);
  max-width: calc(100vw - 48px);
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--ink);
  padding: 12px 18px;
  border-radius: 10px;
  font-size: 0.88rem;
  font-weight: 600;
  line-height: 1.35;
  text-align: center;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 60;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.toast-error { background: #4a1712; border-color: rgba(208, 59, 59, 0.4); }
.toast.toast-success { background: #0f3a13; border-color: rgba(12, 163, 12, 0.4); }

/* --- Chart --- */
.chart-block { margin-bottom: 8px; }
.chart-block h3 {
  font-size: 0.82rem;
  font-weight: 600;
  line-height: 20px;
  color: var(--ink-secondary);
  margin: 0 0 4px;
}

.chart-row { display: flex; }
.chart-yaxis { flex: none; display: block; }

.chart-scroll {
  position: relative;
  flex: 1;
  min-width: 0;
  overflow-x: auto;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}
.chart-scroll::-webkit-scrollbar { display: none; }
.chart-scroll svg { display: block; }

.chart-none {
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--ink-muted);
  font-size: 0.85rem;
  background: var(--surface);
  border-radius: 8px;
}

.chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 14px;
  margin-bottom: 10px;
  font-size: 0.76rem;
  color: var(--ink-secondary);
}
.chart-legend-item { display: flex; align-items: center; gap: 6px; }
.chart-legend-dot { width: 10px; height: 10px; border-radius: 3px; flex: none; }

.chart-tooltip {
  position: absolute;
  pointer-events: none;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 0.78rem;
  white-space: nowrap;
  transform: translate(-50%, -110%);
  opacity: 0;
  transition: opacity 0.1s ease;
  z-index: 2;
}
.chart-tooltip.show { opacity: 1; }

.chart-empty {
  text-align: center;
  color: var(--ink-muted);
  padding: 40px 20px;
  font-size: 0.92rem;
  line-height: 1.5;
}

/* --- Chart filters --- */
.chart-body {
  display: flex;
  flex-direction: column;
  overflow: visible;
  padding-bottom: 8px;
}
.charts-wrap {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

.chart-filters {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
}

.cf-select, .cf-btn {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 10px;
  color: var(--ink);
  font-size: 0.85rem;
  font-family: inherit;
  color-scheme: dark;
}
.cf-select { flex: 1; min-width: 0; }
.cf-select:focus, .cf-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: -1px; }

.cf-dd { flex: 1; min-width: 0; position: relative; }
.cf-btn {
  text-align: left;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding-right: 24px;
  position: relative;
}
.cf-btn::after {
  content: "";
  position: absolute;
  right: 10px;
  top: 50%;
  width: 6px;
  height: 6px;
  border-right: 2px solid var(--ink-muted);
  border-bottom: 2px solid var(--ink-muted);
  transform: translateY(-70%) rotate(45deg);
}

.cf-pop {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  min-width: 190px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 6px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.55);
  z-index: 10;
}

.cf-opt {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  border-radius: 8px;
  font-size: 0.92rem;
  cursor: pointer;
}
.cf-opt:active { background: var(--border); }
.cf-opt input { width: 18px; height: 18px; accent-color: var(--accent); margin: 0; }
.cf-opt-name { flex: 1; }
.cf-opt-range { color: var(--ink-muted); font-size: 0.8rem; }

.cf-custom {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  color: var(--ink-muted);
}
.cf-custom input {
  flex: 1;
  min-width: 0;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
  color: var(--ink);
  font-size: 0.85rem;
  font-family: inherit;
  color-scheme: dark;
}

/* --- Pill (took medication) --- */
:root { --pill: #9085e9; }

.pill-icon { display: block; overflow: visible; }
.pill-icon .pill-body { fill: none; stroke: var(--ink-muted); stroke-width: 1.8; }
.pill-icon .pill-half { fill: none; }
.pill-icon .pill-line { stroke: var(--ink-muted); stroke-width: 1.8; }
.pill-icon.on .pill-body { fill: var(--pill); stroke: var(--pill); }
.pill-icon.on .pill-half { fill: rgba(255, 255, 255, 0.4); }
.pill-icon.on .pill-line { stroke: var(--page); }

.reading-status-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 2px;
}
.reading-status-row .reading-status { margin-top: 0; }

.pill-btn {
  flex: none;
  width: 36px;
  height: 36px;
  margin: -10px -8px -10px 0;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.pill-btn:active { background: var(--surface-2); }

.field-pill { flex: none; width: 64px; }
.pill-toggle {
  height: 42px;
  width: 100%;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.pill-toggle.on { border-color: var(--pill); }

.chart-legend-item .pill-icon { flex: none; }

/* --- Pinch zoom / long-press --- */
.chart-scroll { touch-action: pan-x; }
.chart-scroll svg { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }

.mpop-backdrop {
  position: fixed;
  inset: 0;
  z-index: 55;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.mpop-card {
  width: 100%;
  max-width: 560px;
  max-height: 70dvh;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px 18px 0 0;
  padding: 16px 16px calc(18px + env(safe-area-inset-bottom));
}
.mpop-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.mpop-title { font-size: 1.05rem; font-weight: 700; }
.mpop-sub { margin-top: 4px; color: var(--ink-secondary); font-size: 0.9rem; display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.mpop-sub b { color: var(--ink); }
.mpop-sub svg, .mpop-pulse svg { color: var(--ink-muted); flex: none; }
.mpop-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 11px 4px;
  border-top: 1px solid var(--border);
  font-size: 1.2rem;
  font-weight: 600;
}
.mpop-n { width: 22px; color: var(--ink-muted); font-size: 0.85rem; font-weight: 400; }
.mpop-sep { color: var(--ink-muted); }
.mpop-pulse { margin-left: auto; display: flex; align-items: center; gap: 4px; color: var(--ink-secondary); font-size: 1.05rem; }
.mpop-hl { color: var(--accent); }
