:root {
  color-scheme: light;
  --navy: #2E5266;
  --navy-dark: #1E384A;
  --ink: #1E384A;
  --teal: #6E8898;
  --coral: #D64550;
  --bg: #F7F9FA;
  --card: #FFFFFF;
  --text: #1B2B34;
  --muted: #5A6B75;
  --border: #D8E1E5;
  --sys-color: #D64550;
  --dia-color: #2E5266;
  /* Outline colours on photos: bright enough to see on any display */
  --box-sys: #FF4D5A;
  --box-dia: #3D8BFF;
  --box-pulse: #22C55E;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  font-size: 20px;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

.hidden { display: none !important; }

.app-header {
  background: var(--navy);
  color: white;
  padding: 20px;
  text-align: center;
}
.app-header h1 { margin: 0; font-size: 1.4rem; font-weight: 600; }

.screen {
  max-width: 600px;
  margin: 0 auto;
  padding: 8px 20px 100px;
}

/* Back button and page title on one line. */
.screen-head {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
  min-height: 48px;
}
.screen-head h2 {
  margin: 0;
  font-size: 1.25rem;
  line-height: 1.25;
  min-width: 0;
}

.big-button {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  font-size: 1.15rem;
  font-weight: 600;
  padding: 22px 20px;
  margin-bottom: 16px;
  border-radius: 14px;
  border: none;
  cursor: pointer;
  text-align: left;
}
.big-button .icon { font-size: 1.6rem; }
.big-button.primary { background: var(--navy); color: white; }
.big-button.primary:active { background: var(--navy-dark); }
.big-button.secondary { background: var(--card); color: var(--ink); border: 2px solid var(--border); }
.big-button.secondary:active { background: var(--bg); }

.back-button {
  background: none;
  border: none;
  color: var(--ink);
  font-size: 1.1rem;
  font-weight: 600;
  padding: 10px 12px 10px 0;
  margin: 0;
  cursor: pointer;
  white-space: nowrap;
  flex: none;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px;
  margin-top: 24px;
}
.card h2 { margin-top: 0; font-size: 1rem; color: var(--muted); text-transform: none; }

.reading-display { font-size: 1.6rem; font-weight: 700; color: var(--ink); line-height: 1.5; }
.reading-display small { font-size: 1rem; font-weight: 400; color: var(--muted); }
.muted { color: var(--muted); font-size: 0.95rem; font-weight: 400; margin-top: 6px; }

.field-label {
  display: block;
  font-weight: 600;
  margin: 20px 0 8px;
  color: var(--ink);
}
.big-input {
  width: 100%;
  font-size: 1.2rem;
  padding: 14px 16px;
  border: 2px solid var(--border);
  border-radius: 10px;
  background: var(--card);
  color: var(--text);
}
.big-input:focus { outline: 3px solid var(--teal); border-color: var(--navy); }

.hint { color: var(--muted); font-size: 0.95rem; margin-top: 8px; }
.hint.warning { color: var(--warn, #7A5300); font-weight: 600; }
#btn-enter-manually { margin-top: 12px; }

.export-row { display: flex; gap: 10px; margin-bottom: 16px; }
.small-button {
  flex: 1;
  padding: 12px;
  border-radius: 10px;
  border: 2px solid var(--border);
  background: var(--card);
  color: var(--ink);
  font-weight: 600;
  cursor: pointer;
}

.history-item .row-actions { display: flex; gap: 4px; }
.history-item .edit-btn,
.history-item .delete-btn {
  background: none;
  border: none;
  font-size: 1.3rem;
  cursor: pointer;
  padding: 6px 10px;
}

#preview-img {
  width: 100%;
  border-radius: 14px;
  margin: 16px 0;
  border: 1px solid var(--border);
}

/* ---------- Photo chosen: fit photo, date and buttons on one phone screen ----------
   The page becomes exactly one screen tall and the photo takes whatever
   height is left. The whole photo is scaled to fit (object-fit: contain),
   never cropped: before it's read we don't know where SYS, DIA and pulse
   are, so any crop could cut them off. svh = the height with Safari's
   toolbars showing. */
body:has(#screen-capture.previewing:not(.hidden)) {
  height: 100vh;
  height: 100svh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
body:has(#screen-capture.previewing:not(.hidden)) .settings-fab { display: none; }
/* Settings is opened from the home screen; elsewhere the floating button
   would only cover content (e.g. the graph summary). */
body:has(#screen-home.hidden) .settings-fab { display: none; }
#screen-capture.previewing {
  width: 100%;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding-top: 4px;
  padding-bottom: 12px;
}
#screen-capture.previewing .screen-head { margin-bottom: 4px; }
#screen-capture.previewing #preview-wrap {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
#screen-capture.previewing #preview-img {
  flex: 1 1 0;
  min-height: 140px;
  object-fit: contain;
  background: #000;
  margin: 4px 0 0;
}
#screen-capture.previewing .field-label { margin: 12px 0 6px; }
#screen-capture.previewing .hint { font-size: 0.85rem; margin: 6px 0 10px; }
#screen-capture.previewing .big-button { padding: 16px 20px; margin-bottom: 10px; }
#screen-capture.previewing .big-button:last-of-type { margin-bottom: 0; }

.status {
  margin-top: 20px;
  padding: 16px;
  border-radius: 10px;
  background: var(--card);
  border: 1px solid var(--border);
  font-weight: 600;
  text-align: center;
}

.banner {
  background: #FFF4E5;
  border: 1px solid #F0C36D;
  color: #7A5300;
  padding: 14px 16px;
  border-radius: 10px;
  margin-top: 20px;
  font-weight: 600;
}

.history-item {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px 18px;
  margin-bottom: 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.history-item .numbers { font-size: 1.3rem; font-weight: 700; color: var(--ink); }
.history-item .date { color: var(--muted); font-size: 0.9rem; margin-top: 4px; }
.history-item .queued-tag {
  background: #FFF4E5; color: #7A5300; font-size: 0.8rem; font-weight: 700;
  padding: 4px 10px; border-radius: 20px;
}

/* ---------- Graph screen ---------- */
/* Segmented control: one row of equal buttons in a grey tray. */
.segmented {
  display: flex;
  gap: 4px;
  padding: 4px;
  margin-bottom: 10px;
  border-radius: 12px;
  background: var(--border);
}
.seg-btn {
  flex: 1 1 0;
  min-width: 0;
  min-height: 44px;
  padding: 8px 4px;
  border: none;
  border-radius: 9px;
  background: transparent;
  color: var(--ink);
  font-size: 0.8rem;
  font-weight: 600;
  line-height: 1.15;
  cursor: pointer;
}
.seg-btn.active { background: var(--card); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.15); }
.seg-btn:focus-visible { outline: 3px solid var(--teal); outline-offset: 1px; }

/* Two range inputs stacked on one thin line under the chart's dates:
   only the triangle handles take touches, so each drags on its own.
   Lined up with the plot area (the chart's left/right padding: 42px/16px). */
.dual-range { position: relative; height: 30px; margin: -6px 16px 0 42px; }
.dual-track {
  position: absolute; left: 0; right: 0; top: 7px; height: 2px;
  border-radius: 1px; background: #E3E9EC;
}
.dual-fill { position: absolute; top: 0; bottom: 0; border-radius: 1px; background: #6E8898; }
.dual-range input[type=range] {
  position: absolute; left: -14px; top: -6px; width: calc(100% + 28px); height: 36px;
  margin: 0; background: none; pointer-events: none;
  -webkit-appearance: none; appearance: none;
}
.dual-range input[type=range]::-webkit-slider-runnable-track { background: none; height: 36px; }
.dual-range input[type=range]::-moz-range-track { background: none; }
/* The handle is a 28px touch area showing a small upward triangle. */
.dual-range input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none; pointer-events: auto;
  width: 28px; height: 36px; margin-top: 0; border: none; cursor: grab;
  background: no-repeat center 12px / 14px 12px
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 14 12'%3E%3Cpath d='M7 0 14 12H0z' fill='%232E5266'/%3E%3C/svg%3E");
}
.dual-range input[type=range]::-moz-range-thumb {
  pointer-events: auto; width: 28px; height: 36px; border: none; border-radius: 0; cursor: grab;
  background: no-repeat center 12px / 14px 12px
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 14 12'%3E%3Cpath d='M7 0 14 12H0z' fill='%232E5266'/%3E%3C/svg%3E");
}
.dual-range input[type=range]:focus-visible::-webkit-slider-thumb { outline: 2px solid var(--teal); outline-offset: -4px; }

.chart-card {
  background: #FFFFFF;
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 10px 6px 12px;
}
#chart-canvas { display: block; width: 100%; touch-action: pan-y; cursor: crosshair; }
#chart-canvas:focus-visible { outline: 3px solid var(--teal); outline-offset: 2px; border-radius: 6px; }
.chart-hint { margin: 6px 8px 0; text-align: center; font-size: 0.78rem; color: #5A6B75; }

.legend { display: flex; gap: 24px; justify-content: center; margin-top: 8px; font-weight: 600; font-size: 0.9rem; color: #1B2B34; }

.summary-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px 18px;
  margin-top: 14px;
}
.summary-head { display: flex; justify-content: space-between; color: var(--muted); font-size: 0.85rem; font-weight: 600; }
.summary-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px 12px; margin-top: 14px; text-align: center; }
.sum-value { font-size: 1.6rem; font-weight: 700; color: var(--text); font-variant-numeric: tabular-nums; }
.sum-label { color: var(--muted); font-size: 0.85rem; font-weight: 600; }
.sum-date { color: var(--muted); font-size: 0.8rem; }
.dot { display: inline-block; width: 12px; height: 12px; border-radius: 50%; margin-right: 6px; }
.dot.sys { background: var(--sys-color); }
.dot.dia { background: var(--dia-color); }
.dot.pulse { background: var(--box-pulse); }

/* ---------- Photo review (confirm) screen ---------- */
.screen.wide { max-width: 980px; }
.review { display: grid; grid-template-columns: 1fr; gap: 20px; margin-top: 12px; }
@media (min-width: 720px) {
  .review { grid-template-columns: minmax(0, 3fr) minmax(240px, 2fr); align-items: start; }
}
.review-photo { position: relative; overflow: hidden; border-radius: 14px; background: #000; }
.review-canvas { position: relative; }
.review-photo.cropped .review-canvas { position: absolute; }
.review-canvas img { display: block; width: 100%; }
#btn-toggle-zoom { margin-top: 10px; }
.review-boxes { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.review-boxes .box rect { fill: none; stroke-width: 3; vector-effect: non-scaling-stroke; transition: opacity .15s; }
.review-boxes .box text { font-weight: 700; paint-order: stroke; stroke: rgba(0, 0, 0, .75); font-family: inherit; }
.review-boxes.has-active .box { opacity: .35; }
.review-boxes .box.active { opacity: 1; }
.review-boxes .box.active rect { stroke-width: 5; }
.box-sys rect { stroke: var(--box-sys); }   .box-sys text { fill: var(--box-sys); }
.box-dia rect { stroke: var(--box-dia); }   .box-dia text { fill: var(--box-dia); }
.box-pulse rect { stroke: var(--box-pulse); } .box-pulse text { fill: var(--box-pulse); }

.review-fields { position: relative; }
.review-field { padding-left: 12px; border-left: 5px solid var(--border); left: 0; right: 0; }
.review-fields:not(.aligned) .review-field + .review-field { margin-top: 4px; }
.review-field .field-label { margin-top: 0; }
.review-fields:not(.aligned) .review-field .field-label { margin-top: 12px; }
.field-sys { border-left-color: var(--box-sys); }
.field-dia { border-left-color: var(--box-dia); }
.field-pulse { border-left-color: var(--box-pulse); }
.unread-note { display: none; margin: 6px 0 0; font-size: .9rem; font-weight: 600; color: var(--warn, #7A5300); }
.review-field.unread .unread-note { display: block; }
.review-field.unread input { border-style: dashed; border-color: #C98A00; }

.safety-card {
  background: var(--card);
  border: 2px solid var(--teal);
  border-radius: 14px;
  padding: 18px 18px 6px;
  margin-top: 8px;
}
.safety-card h2 { margin: 0 0 6px; font-size: 1.1rem; }
.safety-card p { margin: 0 0 14px; line-height: 1.45; }
.safety-card .big-button { padding: 16px 20px; margin-bottom: 12px; }

.app-version { margin-top: 28px; text-align: center; font-size: 0.75rem; color: var(--muted); }

.settings-fab {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 54px;
  height: 54px;
  border-radius: 50%;
  border: none;
  background: var(--navy);
  color: white;
  font-size: 1.4rem;
  box-shadow: 0 4px 10px rgba(0,0,0,0.2);
  z-index: 10;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #14202A; --card: #1C2B36; --text: #EAF1F5; --muted: #9FB2BC; --border: #2C3E4A;
    --ink: #A9CBE0;
    --warn: #F2C063;
  }
  body { background: var(--bg); color: var(--text); }
}
