@layer reset, base, components, report, utilities, responsive, print;

/* ==========================================================================
   RESET
   ========================================================================== */
@layer reset {
  *, *::before, *::after { box-sizing: border-box; }
  html { color-scheme: light; scroll-behavior: smooth; -webkit-text-size-adjust: 100%; }
  body, h1, h2, h3, p, dl, dd, figure { margin: 0; }
  button, input, select, textarea { font: inherit; color: inherit; }
  button { cursor: pointer; background: none; border: 0; }
  img, svg, canvas { display: block; max-width: 100%; }
  table { border-collapse: collapse; }
  [hidden] { display: none !important; }
}

/* ==========================================================================
   BASE — design tokens, typography, form controls
   ========================================================================== */
@layer base {
  :root {
    /* Ink */
    --ink-900: #0d2438;
    --ink-800: #14395a;
    --ink-700: #2c4d66;
    --ink-600: #40607a;
    --ink-500: #4d6373;
    --ink-400: #5f7686;

    /* Lines & surfaces */
    --line: #dae5ed;
    --line-strong: #bfd1de;
    --canvas: #eff5fa;
    --surface: #ffffff;
    --surface-2: #f7fbfd;
    --surface-3: #eef6fa;

    /* Accents */
    --blue-800: #10557f;
    --blue-700: #176b9b;
    --blue-600: #1a7fbd;
    --blue-500: #2f9ad6;
    --blue-100: #ddf0fb;
    --blue-050: #eef8fd;

    --teal-700: #0b6f62;
    --teal-100: #dbf3ec;
    --amber-700: #8a5c07;
    --amber-100: #fdefd2;
    --violet-700: #63489f;
    --violet-100: #ece5fa;

    --danger-700: #a33232;
    --danger-050: #fdf0ef;

    --focus: #0f6ea3;

    /* SEO focus keyword "A1C Chart" — 5.28:1 on white, 3.00:1 against --ink-900 */
    --keyword: #0b6fb8;

    /* Poppins with a full fallback stack so the app never breaks offline. */
    --font-sans: "Poppins", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;

    /* Elevation */
    --shadow-xl: 0 24px 60px -22px rgba(11, 45, 72, .28), 0 4px 14px -6px rgba(11, 45, 72, .10);
    --shadow-md: 0 12px 30px -14px rgba(11, 45, 72, .24);
    --shadow-sm: 0 4px 12px -6px rgba(11, 45, 72, .20);

    --r-xl: 24px;
    --r-lg: 18px;
    --r-md: 12px;
    --r-sm: 10px;

    --ease: cubic-bezier(.22, .61, .36, 1);
  }

  body {
    min-width: 320px;
    min-height: 100vh;
    margin: 0;
    background-color: var(--canvas);
    color: var(--ink-900);
    font-family: var(--font-sans);
    font-size: 16px;
    line-height: 1.55;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
  }

  h1, h2, h3, strong { color: var(--ink-900); }
  h1 {
    max-width: 18ch;
    font-size: clamp(2.15rem, 1.1rem + 3.6vw, 3.9rem);
    line-height: 1.04;
    letter-spacing: -.035em;
    font-weight: 800;
  }
  h2 { font-size: clamp(1.2rem, 1.05rem + .5vw, 1.5rem); line-height: 1.25; letter-spacing: -.018em; font-weight: 700; }
  p { color: var(--ink-500); }

  ::selection { background: var(--blue-100); color: var(--ink-900); }

  button, input, select { min-height: 46px; }

  :focus-visible {
    outline: 3px solid var(--focus);
    outline-offset: 2px;
    border-radius: 4px;
  }

  input, select {
    width: 100%;
    border: 1px solid var(--line-strong);
    border-radius: var(--r-sm);
    background: var(--blue-050);          /* on brand tint rather than plain white */
    color: var(--ink-900);
    padding: 11px 13px;
    transition: border-color .18s var(--ease), box-shadow .18s var(--ease), background .18s var(--ease);
  }
  option { background: #ffffff; color: var(--ink-900); }
  input:hover:not(:disabled), select:hover:not(:disabled) { border-color: #8fb1c6; background-color: #e7f4fc; }
  input:focus, select:focus { border-color: var(--focus); background-color: #ffffff; box-shadow: 0 0 0 3px rgba(15, 110, 163, .16); outline: none; }
  input:focus-visible, select:focus-visible { outline: 3px solid var(--focus); outline-offset: 1px; }
  input:disabled, select:disabled { background-color: #eef3f7; color: #6b7d8a; cursor: not-allowed; }
  input[aria-invalid="true"], select[aria-invalid="true"] { border-color: var(--danger-700); box-shadow: 0 0 0 3px rgba(163, 50, 50, .12); }

  select {
    appearance: none;
    padding-right: 42px;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8'%3E%3Cpath d='M1 1.5 6 6.5l5-5' fill='none' stroke='%234d6373' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 14px center;
    background-size: 12px 8px;
  }

  input[type="date"] { min-width: 0; }
  input[type="date"]::-webkit-calendar-picker-indicator { cursor: pointer; opacity: .65; }
  input[type="date"]::-webkit-calendar-picker-indicator:hover { opacity: 1; }

  ::placeholder { color: #6b7d8a; opacity: 1; }
}

/* ==========================================================================
   COMPONENTS — application interface
   ========================================================================== */
@layer components {
  .skip-link {
    position: fixed; z-index: 200; top: 12px; left: 12px;
    transform: translateY(-180%);
    padding: 11px 16px; border-radius: var(--r-sm);
    background: var(--ink-900); color: #fff; font-weight: 700; text-decoration: none;
    transition: transform .18s var(--ease);
  }
  .skip-link:focus-visible { transform: translateY(0); }

  .app-shell {
    width: min(1240px, 100% - 2.5rem);
    margin-inline: auto;
    padding-block: clamp(28px, 5vw, 72px);
  }

  .noscript-note {
    margin-bottom: 22px;
    padding: 16px 18px;
    border: 1px solid #e3bcbc; border-left: 4px solid var(--danger-700);
    border-radius: var(--r-md);
    background: var(--danger-050);
    color: var(--ink-800); font-size: .92rem; line-height: 1.55;
  }

  /* ---------- Intro ---------- */
  .intro {
    display: grid;
    grid-template-columns: minmax(0, 1.55fr) minmax(280px, .6fr);
    gap: clamp(20px, 3vw, 44px);
    align-items: end;
    margin-bottom: clamp(24px, 3vw, 36px);
  }
  .eyebrow {
    display: inline-flex; align-items: center; gap: 7px;
    margin-bottom: 10px;
    color: var(--blue-700); font-size: .78rem; font-weight: 800;
    text-transform: uppercase; letter-spacing: .11em;
  }
  .brand-keyword { color: var(--keyword); }

  .intro-lead { max-width: 62ch; margin-top: 18px; font-size: clamp(1rem, .96rem + .2vw, 1.1rem); color: var(--ink-500); }
  .intro-keyword-link,
  .intro-keyword-link:hover { color: var(--keyword); font-weight: 700; text-decoration: none; }

  .privacy-pill {
    display: flex; gap: 14px; align-items: center;
    padding: 16px 18px;
    border: 1px solid #c6e2e6; border-radius: var(--r-lg);
    background: rgba(255, 255, 255, .82);
    box-shadow: var(--shadow-sm);
  }
  .privacy-pill svg { flex: 0 0 32px; width: 32px; fill: var(--teal-700); }
  .privacy-pill span { display: grid; gap: 2px; color: var(--ink-500); font-size: .85rem; line-height: 1.42; }
  .privacy-pill strong { font-size: .95rem; }

  /* ---------- Cards ---------- */
  .calculator-card, .options-card, .report-actions, .medical-note {
    border: 1px solid var(--line);
    border-radius: var(--r-xl);
    background: var(--surface);
    box-shadow: var(--shadow-xl);
  }
  .calculator-card { overflow: hidden; }

  /* ---------- Tabs ---------- */
  .mode-tabs {
    display: grid; grid-template-columns: 1fr 1fr; gap: 8px;
    padding: 9px;
    border-bottom: 1px solid var(--line);
    background: linear-gradient(180deg, #edf4f9, #e7f0f6);
  }
  .mode-tab {
    display: flex; align-items: center; gap: 14px;
    min-height: 70px; padding: 12px 18px;
    border: 1px solid transparent; border-radius: 15px;
    color: var(--ink-500); text-align: left;
    transition: background .2s var(--ease), color .2s var(--ease), box-shadow .2s var(--ease), transform .2s var(--ease);
  }
  .mode-tab:hover:not(.is-active) { background: rgba(255, 255, 255, .7); color: var(--ink-800); }
  .mode-tab.is-active {
    border-color: var(--line); background: #fff; color: var(--ink-900);
    box-shadow: 0 8px 20px -12px rgba(11, 45, 72, .5);
  }
  .tab-text { display: grid; font-weight: 700; font-size: .98rem; }
  .mode-tab small { margin-top: 2px; color: var(--ink-500); font-size: .8rem; font-weight: 500; }
  .tab-number {
    display: grid; place-items: center;
    flex: 0 0 38px; width: 38px; height: 38px;
    border-radius: 11px; background: #dceaf2; color: var(--blue-800);
    font-size: .78rem; font-weight: 800;
    transition: background .2s var(--ease), color .2s var(--ease);
  }
  .mode-tab.is-active .tab-number { background: var(--blue-100); color: var(--blue-700); }

  .mode-panel { padding: clamp(16px, 3vw, 40px); }

  .panel-grid {
    display: grid;
    grid-template-columns: minmax(0, .85fr) minmax(0, 1.15fr);
    gap: clamp(16px, 2vw, 26px);
    align-items: stretch;
  }

  .form-card, .result-empty, .result-card, .history-entry-card, .records-card, .chart-card {
    border: 1px solid var(--line);
    border-radius: var(--r-lg);
    background: var(--surface-2);
  }
  .form-card, .result-card, .history-entry-card, .records-card, .chart-card { padding: clamp(18px, 2.4vw, 28px); }

  /* ---------- Section headings ---------- */
  .section-heading { display: flex; gap: 14px; align-items: flex-start; margin-bottom: 22px; }
  .section-heading p:not(.eyebrow) { margin-top: 5px; font-size: .93rem; }
  .heading-icon {
    display: grid; place-items: center;
    flex: 0 0 44px; height: 44px;
    border-radius: 13px; background: var(--blue-100);
  }
  .heading-icon svg { width: 22px; fill: var(--blue-700); }
  .row-heading { justify-content: space-between; align-items: center; }

  /* ---------- Fields ---------- */
  .field-group { min-width: 0; margin-bottom: 18px; }
  .field-group label { display: block; margin-bottom: 7px; color: var(--ink-800); font-size: .9rem; font-weight: 700; }
  .field-hint, .field-error, .field-warning {
    min-height: 1.2em; margin: 6px 1px 0;
    font-size: .8rem; line-height: 1.35;
  }
  .field-hint { color: var(--ink-500); }
  .field-error { color: var(--danger-700); font-weight: 600; }
  .field-warning { color: var(--amber-700); font-weight: 600; }
  .field-error:empty, .field-warning:empty { min-height: 0; margin-top: 0; }
  .optional { color: var(--ink-400); font-size: .76rem; font-weight: 500; }
  .required { color: var(--danger-700); }

  .input-unit { position: relative; display: flex; align-items: center; }
  .input-unit input { padding-right: 78px; }
  .input-unit > span {
    position: absolute; right: 13px;
    max-width: 64px;
    color: var(--blue-700); font-size: .8rem; font-weight: 800;
    pointer-events: none;
  }

  /* ---------- Buttons ---------- */
  .button {
    display: inline-flex; justify-content: center; align-items: center; gap: 10px;
    min-height: 48px; padding: 11px 20px;
    border: 1px solid transparent; border-radius: var(--r-sm);
    font-size: .95rem; font-weight: 700; text-decoration: none;
    transition: transform .16s var(--ease), box-shadow .16s var(--ease), border-color .16s var(--ease), background .16s var(--ease), color .16s var(--ease);
  }
  .button-icon { font-size: 1.05em; line-height: 1; }
  .button:hover:not(:disabled) { transform: translateY(-1px); }
  .button:active:not(:disabled) { transform: translateY(0); }
  .button:disabled { opacity: .55; cursor: not-allowed; box-shadow: none; }

  .button-primary {
    background: linear-gradient(180deg, #1c7cae, var(--blue-700));
    color: #fff;
    box-shadow: 0 10px 22px -12px rgba(23, 107, 155, .9);
  }
  .button-primary:hover:not(:disabled) { background: linear-gradient(180deg, #17709f, var(--blue-800)); box-shadow: 0 14px 26px -12px rgba(23, 107, 155, .95); }
  .button-secondary { border-color: var(--line-strong); background: #fff; color: var(--ink-800); }
  .button-secondary:hover:not(:disabled) { border-color: #7fa8c1; background: var(--surface-2); }
  .button-quiet, .button-danger-quiet { min-height: 44px; padding: 8px 14px; border-color: var(--line-strong); background: #fff; color: var(--blue-700); font-size: .88rem; }
  .button-quiet:hover:not(:disabled) { background: var(--blue-050); }
  .button-danger-quiet { color: var(--danger-700); }
  .button-danger-quiet:hover:not(:disabled) { background: var(--danger-050); border-color: #e2b5b5; }
  .button-danger { background: var(--danger-700); color: #fff; }
  .button-danger:hover:not(:disabled) { background: #8d2a2a; }
  .button-wide { width: 100%; }
  .button-row { display: flex; flex-wrap: wrap; gap: 10px; }

  .formula-note {
    display: flex; gap: 12px; align-items: flex-start;
    margin-top: 20px; padding: 14px 15px;
    border: 1px solid #d5e7f0; border-radius: var(--r-md);
    background: var(--blue-050);
  }
  .formula-note > span {
    display: grid; place-items: center;
    flex: 0 0 23px; height: 23px; margin-top: 1px;
    border: 1.5px solid #8ab9d1; border-radius: 50%;
    color: var(--blue-700); font-size: .78rem; font-weight: 800;
  }
  .formula-note p { color: var(--ink-500); font-size: .82rem; line-height: 1.5; }
  .formula-note strong { display: block; margin-bottom: 2px; font-size: .86rem; }

  /* ---------- Single-mode result ---------- */
  .result-empty { display: grid; place-content: center; min-height: 480px; padding: 32px; text-align: center; }
  .result-empty h2 { margin-top: 22px; }
  .result-empty > p { max-width: 46ch; margin: 10px auto 0; font-size: .92rem; }
  .empty-visual {
    width: min(330px, 100%); margin-inline: auto;
    padding: 8px;
    border: 1px solid #d3e5ee; border-radius: var(--r-lg);
    background: linear-gradient(180deg, #ffffff, #f0f8fc);
    box-shadow: inset 0 1px 0 #fff;
  }
  .empty-visual svg { width: 100%; height: auto; }
  .ev-grid line { stroke: #dcebf3; stroke-width: 1; stroke-dasharray: 4 5; }
  .ev-axis { stroke: #c2d8e5; stroke-width: 1.5; }
  .ev-area { fill: url(#emptyVisualArea); stroke: none; }
  .ev-line { fill: none; stroke: var(--blue-600); stroke-width: 3.5; stroke-linecap: round; stroke-linejoin: round; }
  .ev-dot circle { fill: #fff; stroke: var(--teal-700); stroke-width: 3; }
  .ev-pill rect { fill: #fff; stroke: #d3e6f0; stroke-width: 1; }
  .ev-pill-text text {
    fill: var(--blue-700);
    font-family: var(--font-sans); font-size: 11px; font-weight: 700;
    text-anchor: middle;
  }

  /* Reference-scale gauge shown with a single converted result */
  .result-gauge { position: relative; z-index: 1; margin: 0 0 18px; }
  .result-gauge figcaption { margin-bottom: 8px; color: var(--ink-500); font-size: .79rem; font-weight: 600; }
  .gauge-wrap {
    width: 100%; height: 126px;
    border: 1px solid #d7e7ee; border-radius: var(--r-md);
    background: #fff;
    overflow: hidden;
  }
  #resultGauge { width: 100%; height: 100%; }

  .result-card {
    position: relative;
    background: linear-gradient(150deg, #f4fbfe 0%, #ecf6f8 100%);
    overflow: hidden;
  }
  .result-card::after {
    position: absolute; content: "";
    right: -60px; top: -74px; width: 190px; height: 190px;
    border: 34px solid rgba(26, 127, 189, .07); border-radius: 50%;
    pointer-events: none;
  }
  .result-card-top { position: relative; z-index: 1; display: flex; justify-content: space-between; gap: 14px; align-items: flex-start; }
  .result-kicker { margin-bottom: 5px; color: var(--blue-700); font-size: .75rem; font-weight: 800; text-transform: uppercase; letter-spacing: .1em; }

  .category-badge {
    display: inline-flex; align-items: center;
    max-width: 230px; min-height: 34px; padding: 7px 13px;
    border-radius: 999px; background: var(--teal-100); color: var(--teal-700);
    font-size: .76rem; font-weight: 800; line-height: 1.3;
  }
  .category-badge.range-mid { background: var(--amber-100); color: var(--amber-700); }
  .category-badge.range-upper { background: var(--violet-100); color: var(--violet-700); }

  .main-result { position: relative; z-index: 1; display: flex; flex-wrap: wrap; align-items: baseline; gap: 9px; margin: 30px 0 22px; }
  .main-result > span:first-child {
    color: var(--ink-900);
    font-size: clamp(3.1rem, 1.6rem + 5.6vw, 5.4rem);
    font-weight: 800; line-height: .85; letter-spacing: -.05em;
    font-variant-numeric: tabular-nums;
  }
  .main-result > span:nth-child(2) { color: var(--blue-700); font-size: 1.5rem; font-weight: 800; }
  .main-result small { flex-basis: 100%; margin-top: 6px; color: var(--ink-500); font-size: .85rem; }

  .metric-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 9px; margin-bottom: 18px; }
  .metric-grid > div { padding: 13px; border: 1px solid #d7e7ee; border-radius: var(--r-md); background: rgba(255, 255, 255, .8); }
  .metric-grid dt { color: var(--ink-500); font-size: .73rem; font-weight: 800; text-transform: uppercase; letter-spacing: .05em; }
  .metric-grid dt span { text-transform: none; letter-spacing: 0; }
  .metric-grid dd { margin-top: 4px; font-size: 1.18rem; font-weight: 800; font-variant-numeric: tabular-nums; }

  .interpretation { margin-bottom: 10px; color: var(--ink-800); font-size: .92rem; font-weight: 600; }
  .reference-note, .table-reference-note {
    padding: 10px 13px;
    border-left: 3px solid var(--blue-500); border-radius: 0 var(--r-sm) var(--r-sm) 0;
    background: rgba(255, 255, 255, .72);
    color: var(--ink-500); font-size: .8rem; line-height: 1.5;
  }
  .result-meta {
    display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px;
    margin: 18px 0; padding-top: 16px; border-top: 1px solid var(--line);
  }
  .result-meta > span { display: grid; color: var(--ink-500); font-size: .84rem; }
  .result-meta strong { font-size: .73rem; text-transform: uppercase; letter-spacing: .05em; }

  /* ---------- History ---------- */
  .history-entry-card, .records-card, .chart-card { background: #fff; }
  .records-card, .chart-card { margin-top: 20px; }

  .history-form {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
    gap: 14px 16px;
    align-items: start;
  }
  .history-form .field-group { margin: 0; }
  .history-form .form-action { grid-column: 1 / -1; justify-self: start; min-width: 230px; margin-top: 4px; }

  .records-toolbar { display: flex; justify-content: space-between; gap: 18px; align-items: flex-end; margin-bottom: 16px; }
  .toolbar-controls { display: flex; gap: 10px; align-items: center; }
  .toolbar-controls label { font-size: .84rem; font-weight: 700; white-space: nowrap; }
  .toolbar-controls select { width: auto; min-width: 155px; min-height: 44px; padding-block: 9px; }
  .table-reference-note { margin-bottom: 14px; }

  .table-scroll {
    position: relative;
    max-height: 520px;
    border: 1px solid var(--line); border-radius: var(--r-md);
    overflow: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }
  table { width: 100%; border-spacing: 0; font-size: .84rem; }
  caption { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; clip-path: inset(50%); overflow: hidden; white-space: nowrap; }
  th {
    position: sticky; z-index: 1; top: 0;
    padding: 12px 11px;
    border-bottom: 1px solid var(--line-strong);
    background: #e9f1f6; color: var(--ink-800);
    font-size: .73rem; font-weight: 800; text-align: left;
    text-transform: uppercase; letter-spacing: .04em; white-space: nowrap;
  }
  td { padding: 12px 11px; border-bottom: 1px solid #e6eef2; color: var(--ink-900); vertical-align: middle; }
  tbody tr:nth-child(even) { background: #f7fbfc; }
  tbody tr:hover { background: var(--blue-050); }
  tbody tr:last-child td { border-bottom: 0; }
  td.numeric { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
  td.note-cell { min-width: 150px; max-width: 240px; overflow-wrap: anywhere; color: var(--ink-600); }
  td.category-cell { min-width: 165px; }

  .table-category {
    display: inline-flex; padding: 5px 9px;
    border-radius: 999px; background: var(--teal-100); color: var(--teal-700);
    font-size: .72rem; font-weight: 700; line-height: 1.25;
  }
  .table-category.range-mid { background: var(--amber-100); color: var(--amber-700); }
  .table-category.range-upper { background: var(--violet-100); color: var(--violet-700); }

  .trend-label { display: inline-flex; gap: 4px; align-items: center; white-space: nowrap; font-weight: 700; font-variant-numeric: tabular-nums; }
  .trend-up { color: var(--violet-700); }
  .trend-down { color: var(--teal-700); }
  .trend-same { color: var(--ink-500); }

  .table-actions { display: flex; gap: 6px; }
  .icon-button {
    display: inline-grid; place-items: center;
    min-width: 42px; min-height: 42px; padding: 6px 11px;
    border: 1px solid var(--line-strong); border-radius: 9px;
    background: #fff; color: var(--blue-700);
    font-size: .78rem; font-weight: 700;
    transition: background .16s var(--ease), border-color .16s var(--ease);
  }
  .icon-button:hover { border-color: #7fa8c1; background: var(--blue-050); }
  .icon-button.delete { color: var(--danger-700); }
  .icon-button.delete:hover { border-color: #e2b5b5; background: var(--danger-050); }

  .table-empty, .chart-empty { display: grid; place-items: center; text-align: center; }
  .table-empty {
    min-height: 200px; padding: 26px;
    border: 1px dashed var(--line-strong); border-radius: var(--r-md);
    background: var(--surface-2);
  }
  .table-empty > span {
    display: grid; place-items: center;
    width: 46px; height: 46px; margin-bottom: 12px;
    border: 1px dashed #93b3c5; border-radius: 50%;
    color: var(--blue-700); font-size: 1.4rem;
  }
  .table-empty p { margin-top: 5px; font-size: .86rem; }

  /* ---------- Chart ---------- */
  .metric-control { display: inline-flex; padding: 4px; border: 1px solid var(--line); border-radius: var(--r-md); background: #edf4f8; }
  .metric-button {
    min-height: 40px; padding: 7px 15px;
    border-radius: 8px; background: transparent; color: var(--ink-500);
    font-size: .82rem; font-weight: 700;
    transition: background .18s var(--ease), color .18s var(--ease), box-shadow .18s var(--ease);
  }
  .metric-button:hover:not(.is-active) { color: var(--ink-800); }
  .metric-button.is-active { background: #fff; color: var(--blue-700); box-shadow: var(--shadow-sm); }

  .chart-empty {
    min-height: 280px; padding: 30px;
    border: 1px dashed var(--line-strong); border-radius: var(--r-md);
    background: var(--surface-2);
  }
  .chart-empty > div {
    display: grid; place-items: center;
    width: 54px; height: 54px; margin-bottom: 12px;
    border-radius: 15px; background: var(--blue-100); color: var(--blue-700);
    font-size: 1.9rem;
  }
  .chart-empty p { margin-top: 5px; font-size: .86rem; }

  .chart-legend {
    display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px 18px;
    margin: 0 2px 12px;
    color: var(--ink-500); font-size: .79rem;
  }
  .legend-item { display: inline-flex; gap: 7px; align-items: center; white-space: nowrap; }
  .legend-line { width: 20px; height: 3px; border-radius: 999px; background: var(--blue-600); }
  .legend-target { width: 20px; height: 0; border-top: 2px dashed var(--teal-700); }
  .legend-band { width: 14px; height: 12px; border-radius: 4px; }
  .legend-band.band-below { background: #cdeee3; }
  .legend-band.band-mid { background: #fbe6c0; }
  .legend-band.band-upper { background: #e3d9f7; }

  .canvas-wrap {
    position: relative;
    width: 100%;
    height: clamp(280px, 34vw, 400px);
    border: 1px solid var(--line); border-radius: var(--r-md);
    background: #fff;
    overflow: hidden;
  }
  #trendChart { width: 100%; height: 100%; touch-action: pan-y; }
  #trendChart:focus-visible { outline: 3px solid var(--focus); outline-offset: -3px; }

  .chart-tooltip {
    position: absolute; z-index: 2;
    min-width: 132px; max-width: 210px; padding: 9px 12px;
    border-radius: var(--r-sm);
    background: rgba(11, 36, 56, .96);
    color: #fff; font-size: .78rem; line-height: 1.45;
    box-shadow: var(--shadow-md);
    pointer-events: none;
    transform: translate(-50%, calc(-100% - 14px));
  }
  .chart-tooltip strong { display: block; color: #fff; font-size: .92rem; }
  .chart-tooltip span { color: #c6dced; }
  .chart-tooltip em { display: block; margin-top: 3px; color: #a9c9de; font-style: normal; font-size: .74rem; }

  .axis-note { margin-top: 9px; color: var(--ink-500); font-size: .76rem; text-align: right; }
  .chart-summary {
    margin-top: 14px; padding: 13px 15px;
    border: 1px solid var(--line); border-radius: var(--r-sm);
    background: var(--surface-2); color: var(--ink-600);
    font-size: .83rem; line-height: 1.55;
  }

  /* ---------- Options ---------- */
  .options-card { margin-top: 22px; padding: clamp(20px, 3vw, 34px); }
  .local-chip {
    display: inline-flex; gap: 8px; align-items: center;
    padding: 8px 13px; border-radius: 999px;
    background: var(--teal-100); color: var(--teal-700);
    font-size: .76rem; font-weight: 800; white-space: nowrap;
  }
  .chip-dot { font-size: .62rem; line-height: 1; }
  .options-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 14px 18px; }
  .options-grid .field-group { margin-bottom: 4px; }
  .option-checks { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 14px; margin-top: 18px; }
  .check-control {
    display: flex; gap: 13px; align-items: flex-start;
    min-height: 74px; padding: 14px;
    border: 1px solid var(--line); border-radius: var(--r-md);
    background: var(--surface-2); cursor: pointer;
    transition: border-color .16s var(--ease), background .16s var(--ease);
  }
  .check-control:hover { border-color: var(--line-strong); background: var(--blue-050); }
  .check-control input { flex: 0 0 20px; width: 20px; min-height: 20px; height: 20px; margin: 3px 0 0; accent-color: var(--blue-700); cursor: pointer; }
  .check-control > span { display: grid; gap: 2px; }
  .check-control strong { font-size: .89rem; }
  .check-control small { color: var(--ink-500); font-size: .8rem; line-height: 1.4; }

  .privacy-row { display: flex; justify-content: space-between; gap: 18px; align-items: center; margin-top: 18px; padding-top: 18px; border-top: 1px solid var(--line); }
  .privacy-row p { font-size: .82rem; }

  /* ---------- Report actions ---------- */
  .report-actions {
    display: grid; grid-template-columns: minmax(0, 1fr) auto;
    gap: 18px 24px; align-items: center;
    margin-top: 22px; padding: clamp(20px, 3vw, 32px);
  }
  .report-actions .button-row { justify-content: flex-end; }
  .report-actions p { font-size: .92rem; }
  .action-message { grid-column: 1 / -1; color: var(--danger-700); font-size: .84rem; font-weight: 600; text-align: right; }
  .action-message:empty { display: none; }
  .text-button {
    grid-column: 1 / -1; justify-self: end;
    min-height: 40px; padding: 0 4px;
    color: var(--blue-700); font-size: .88rem; font-weight: 700;
    text-decoration: underline; text-underline-offset: 3px;
  }

  /* ---------- Medical note ---------- */
  .medical-note {
    display: flex; gap: 18px;
    margin-top: 22px; padding: clamp(20px, 3vw, 32px);
    border-color: #c4dee4; background: #ecf6f8; box-shadow: none;
  }
  .medical-icon {
    display: grid; place-items: center;
    flex: 0 0 44px; height: 44px;
    border-radius: 13px; background: var(--teal-700); color: #fff; font-size: 1.3rem;
  }
  .medical-note h2 { margin-bottom: 8px; font-size: 1.14rem; }
  .medical-note p { font-size: .87rem; line-height: 1.6; }
  .medical-note .factor-note { margin-top: 10px; color: var(--ink-600); font-size: .82rem; }

  /* ---------- Dialog ---------- */
  .confirm-dialog {
    width: min(440px, calc(100% - 28px));
    max-height: calc(100dvh - 40px);
    padding: 0; border: 0; border-radius: var(--r-lg);
    background: #fff; color: var(--ink-900);
    box-shadow: 0 30px 80px -20px rgba(11, 36, 56, .55);
  }
  .confirm-dialog::backdrop { background: rgba(11, 36, 56, .5); backdrop-filter: blur(2px); }
  .confirm-dialog form { display: grid; justify-items: center; padding: 28px; text-align: center; }
  .dialog-icon {
    display: grid; place-items: center;
    width: 50px; height: 50px; margin-bottom: 14px;
    border-radius: 50%; background: var(--amber-100); color: var(--amber-700);
    font-size: 1.4rem; font-weight: 800;
  }
  .confirm-dialog p { margin: 10px 0 22px; font-size: .92rem; }
  .confirm-dialog .button-row { width: 100%; }
  .confirm-dialog .button { flex: 1 1 140px; }
}

/* ==========================================================================
   REPORT — single source of truth for BOTH Print and PDF output
   Rendered at 703px (= A4 portrait width minus 12mm margins) so the printed
   page and the generated PDF are pixel-identical.
   ========================================================================== */
@layer report {
  .print-host { display: none; }
  body.pdf-render .print-host {
    position: absolute; top: 0; left: -20000px;
    display: block; width: 703px;
    pointer-events: none;
  }

  .print-report {
    width: 703px;
    background: #ffffff;
    color: #14293d;
    font-family: var(--font-sans);
    font-size: 11px;
    line-height: 1.45;
  }
  .print-report h2 { color: #0d2438; }
  .print-report p { color: #40607a; }

  .print-head {
    display: flex; justify-content: space-between; gap: 16px; align-items: flex-end;
    padding-bottom: 10px;
    border-bottom: 3px solid #176b9b;
  }
  .print-brand { display: flex; gap: 11px; align-items: center; }
  .report-logo-mark {
    display: grid; place-items: center;
    flex: 0 0 44px; width: 44px; height: 44px;
    border-radius: 11px; background: #ddf0fb;
  }
  .report-logo-mark svg { width: 34px; height: 34px; }
  .report-logo-mark svg path:first-child { fill: #176b9b; }
  .report-logo-mark svg path:last-child { fill: none; stroke: #ffffff; stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
  .print-brand-name { display: grid; }
  .print-brand-name strong { font-size: 20px; line-height: 1.15; letter-spacing: -.02em; }
  .print-brand-name .brand-keyword { color: #0b6fb8; font-size: inherit; }  /* literal, html2canvas-safe */
  /* Direct child only — the keyword <span> lives inside <strong> and must keep the 20px brand size. */
  .print-brand-name > span { color: #5f7686; font-size: 10px; }
  .print-brand-meta { display: grid; text-align: right; }
  .print-brand-meta strong { color: #176b9b; font-size: 14px; line-height: 1.2; }
  .print-brand-meta span { color: #5f7686; font-size: 10px; }

  .print-identity {
    margin-top: 10px; padding: 7px 10px;
    border-left: 3px solid #0b6f62; border-radius: 0 5px 5px 0;
    background: #ecf6f8; font-weight: 700; font-size: 11px;
  }
  .print-empty {
    margin-top: 14px; padding: 14px;
    border: 1px dashed #bfd1de; border-radius: 6px;
    background: #f7fbfd; color: #40607a; text-align: center;
  }

  .print-summary, .print-chart-section, .print-table-section { margin-top: 14px; }
  .print-section-title { display: flex; gap: 10px; align-items: baseline; margin-bottom: 7px; }
  .print-section-title p { color: #176b9b; font-size: 9px; font-weight: 800; text-transform: uppercase; letter-spacing: .09em; }
  .print-section-title h2 { font-size: 14px; font-weight: 700; }

  .print-summary dl { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
  .print-summary dl > div {
    padding: 9px 10px;
    border: 1px solid #dae5ed; border-top: 3px solid #1a7fbd; border-radius: 6px;
    background: #f7fbfd;
  }
  .print-summary dt { color: #5f7686; font-size: 9px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; }
  .print-summary dd { margin: 3px 0 0; color: #0d2438; font-size: 19px; font-weight: 700; line-height: 1.1; }
  .print-gauge {
    width: 100%; height: auto; margin-top: 9px;
    border: 1px solid #dae5ed; border-radius: 6px;
  }
  .print-category {
    margin-top: 9px; padding: 9px 11px;
    border-left: 4px solid #0b6f62; border-radius: 0 6px 6px 0;
    background: #dbf3ec; color: #12384a; font-size: 10.5px;
  }
  .print-category.range-mid { border-left-color: #8a5c07; background: #fdefd2; }
  .print-category.range-upper { border-left-color: #63489f; background: #ece5fa; }

  .print-chart-section img {
    width: 100%; height: auto;
    border: 1px solid #dae5ed; border-radius: 6px;
  }
  .print-chart-section > p { margin-top: 5px; color: #40607a; font-size: 9.5px; line-height: 1.45; }

  /* table-layout:auto lets every column take exactly the width its content
     needs; the label/note column is the only one allowed to wrap, so it
     absorbs whatever space is left over. */
  .print-table-section table { width: 100%; table-layout: auto; border-collapse: collapse; }
  .print-table-section th,
  .print-table-section td {
    padding: 7px 9px;
    border: 1px solid #dae5ed;
    font-size: 9px;            /* header and body deliberately identical */
    line-height: 1.5;
    white-space: nowrap;       /* no mid-word breaks anywhere but the note */
    text-align: center;        /* every heading sits exactly over its values */
    vertical-align: middle;
  }
  .print-table-section th {
    position: static;
    border-color: #bfd1de; background: #e9f1f6; color: #14395a;
    font-weight: 700;
    text-transform: uppercase; letter-spacing: .03em;
  }
  .print-table-section td { color: #14293d; }
  .print-table-section tbody tr:nth-child(even) td { background: #f7fbfd; }
  /* Centred, but digits still line up column-wise. */
  .print-table-section .num { font-variant-numeric: tabular-nums; }
  /* The one flexible column. Wraps only when a note is genuinely too long. */
  #printNoteHeader,
  .print-table-section .note { white-space: normal; min-width: 92px; overflow-wrap: anywhere; }
  .print-chip {
    display: inline-block; padding: 2px 6px;
    border-radius: 999px; background: #dbf3ec; color: #0b6f62;
    font-size: 8px; font-weight: 700; line-height: 1.35;
  }
  .print-chip.range-mid { background: #fdefd2; color: #8a5c07; }
  .print-chip.range-upper { background: #ece5fa; color: #63489f; }
  .print-trend { font-weight: 700; white-space: nowrap; }
  .print-trend.trend-up { color: #63489f; }
  .print-trend.trend-down { color: #0b6f62; }
  .print-trend.trend-same { color: #5f7686; }

  .print-method, .print-disclaimer {
    margin-top: 11px; padding: 9px 11px;
    border: 1px solid #dae5ed; border-radius: 6px;
  }
  .print-method { display: grid; grid-template-columns: 118px 1fr; gap: 8px; background: #f7fbfd; font-size: 9.5px; }
  .print-method strong { color: #176b9b; }
  .print-disclaimer { border-color: #c4dee4; background: #ecf6f8; font-size: 9px; }
  .print-disclaimer strong { color: #0b6f62; }
  .print-disclaimer p { margin-top: 4px; color: #40607a; line-height: 1.5; }
  .print-report > *:last-child { margin-bottom: 0; }
}

/* ==========================================================================
   UTILITIES
   ========================================================================== */
@layer utilities {
  .sr-only {
    position: absolute !important;
    width: 1px !important; height: 1px !important;
    padding: 0 !important; margin: -1px !important;
    overflow: hidden !important; clip-path: inset(50%) !important;
    white-space: nowrap !important; border: 0 !important;
  }
  .is-loading { position: relative; color: transparent !important; pointer-events: none; }
  .is-loading > * { visibility: hidden; }
  .is-loading::after {
    position: absolute; content: "";
    top: 50%; left: 50%;
    width: 19px; height: 19px; margin: -10px 0 0 -10px;
    border: 2px solid rgba(255, 255, 255, .45); border-top-color: #fff; border-radius: 50%;
    animation: spin .7s linear infinite;
  }
  @keyframes spin { to { transform: rotate(360deg); } }
}

/* ==========================================================================
   RESPONSIVE
   ========================================================================== */
@layer responsive {
  @media (max-width: 1180px) {
    .intro { grid-template-columns: minmax(0, 1.4fr) minmax(260px, .7fr); }
  }

  @media (max-width: 980px) {
    .app-shell { width: min(100% - 2rem, 820px); }
    .intro { grid-template-columns: 1fr; align-items: start; gap: 22px; }
    .privacy-pill { max-width: 460px; }
    .panel-grid { grid-template-columns: 1fr; }
    .result-empty { min-height: 340px; }
    .report-actions { grid-template-columns: 1fr; }
    .report-actions .button-row { justify-content: flex-start; }
    .action-message { text-align: left; }
    .text-button { justify-self: start; }
  }

  @media (max-width: 760px) {
    .app-shell { width: min(100% - 1.5rem, 680px); }
    .records-toolbar { flex-direction: column; align-items: stretch; gap: 12px; }
    .toolbar-controls { flex-wrap: wrap; }
    .toolbar-controls select { flex: 1 1 150px; }
    .toolbar-controls .button { flex: 0 0 auto; }
    .metric-control { width: 100%; }
    .metric-button { flex: 1; }
    .chart-legend { justify-content: flex-start; }
    .privacy-row { flex-direction: column; align-items: flex-start; }
    .medical-note { flex-direction: column; }
  }

  @media (max-width: 560px) {
    .app-shell { width: calc(100% - 1.25rem); padding-block: 22px; }
    h1 { max-width: none; }
    .mode-tabs { grid-template-columns: 1fr; }
    .mode-tab { min-height: 62px; padding: 10px 14px; }
    .mode-panel { padding: 14px; }
    .form-card, .result-card, .history-entry-card, .records-card, .chart-card { padding: 16px 14px; }
    .section-heading { margin-bottom: 18px; }
    .result-card-top { flex-direction: column; align-items: flex-start; }
    .category-badge { max-width: 100%; }
    .metric-grid { grid-template-columns: 1fr; }
    .history-form { grid-template-columns: 1fr; }
    .history-form .form-action { min-width: 0; width: 100%; }
    .row-heading { flex-direction: column; align-items: flex-start; gap: 12px; }
    .button-row > .button { flex: 1 1 100%; }
    .report-actions .button-row { width: 100%; }
    .canvas-wrap { height: 300px; }
    .axis-note { text-align: left; }
    .local-chip { align-self: flex-start; }
  }

  @media (max-width: 400px) {
    .app-shell { width: calc(100% - .9rem); }
    .privacy-pill { align-items: flex-start; }
    .tab-number { display: none; }
    .mode-panel { padding: 10px; }
    .calculator-card, .options-card, .report-actions, .medical-note { border-radius: var(--r-lg); }
    .form-card, .result-card, .history-entry-card, .records-card, .chart-card { border-radius: var(--r-md); }
    .toolbar-controls .button { width: 100%; }
    .canvas-wrap { height: 268px; }
  }

  @media (prefers-reduced-motion: reduce) {
    html { scroll-behavior: auto; }
    *, *::before, *::after {
      animation-duration: .01ms !important;
      animation-iteration-count: 1 !important;
      transition-duration: .01ms !important;
      scroll-behavior: auto !important;
    }
    .button:hover:not(:disabled) { transform: none; }
  }
}

/* ==========================================================================
   PRINT — page setup only; the report itself uses the shared @layer report
   ========================================================================== */
/* margin:0 leaves the browser no page-margin box, which suppresses its own
   header/footer (date, document title, file URL, page number). The 12mm inset
   is restored as padding on #printHost inside @media print below. */
@page { size: A4 portrait; margin: 0; }

@layer print {
  @media print {
    *, *::before, *::after {
      -webkit-print-color-adjust: exact !important;
      print-color-adjust: exact !important;
      box-shadow: none !important;
      text-shadow: none !important;
    }
    html, body {
      min-width: 0 !important;
      min-height: 0 !important;
      height: auto !important;
      margin: 0 !important;
      padding: 0 !important;
      background: #ffffff !important;
      background-image: none !important;
    }
    body > *:not(#printHost) { display: none !important; }

    #printHost {
      position: static !important;
      display: block !important;
      width: 100% !important;
      left: auto !important;
      top: auto !important;
      padding: 12mm !important;   /* replaces the @page margin we zeroed out */
    }
    .print-report { width: 100% !important; }

    /* Keep small blocks whole, let the long table flow across pages. */
    .print-head, .print-summary, .print-chart-section, .print-method, .print-disclaimer, .print-identity {
      break-inside: avoid; page-break-inside: avoid;
    }
    .print-table-section { break-inside: auto; page-break-inside: auto; }
    .print-table-section thead { display: table-header-group; }
    .print-table-section tr { break-inside: avoid; page-break-inside: avoid; }
    .print-chart-section img { max-height: 74mm; object-fit: contain; }

    /* No trailing blank page: nothing may push past the last block. */
    .print-report > *:last-child { margin-bottom: 0 !important; page-break-after: avoid; break-after: avoid; }
  }
}
