/* ==========================================================================
   Lab Result Explainer — styles.css
   --------------------------------------------------------------------------
   Design direction: calm, clinical, professional. This is a healthcare tool
   used by clinic operators. No gradients, no animations, no decorative
   flourishes. Every visual choice serves legibility and trust.
   ========================================================================== */


/* ==========================================================================
   1. DESIGN TOKENS
   ========================================================================== */

:root {
  /* Typography */
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
               "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
               "Liberation Mono", monospace;

  --fs-xs:   0.75rem;   /* 12px */
  --fs-sm:   0.875rem;  /* 14px */
  --fs-base: 1rem;      /* 16px */
  --fs-lg:   1.125rem;  /* 18px */
  --fs-xl:   1.375rem;  /* 22px */
  --fs-2xl:  1.75rem;   /* 28px */

  --lh-tight:  1.25;
  --lh-normal: 1.5;
  --lh-loose:  1.7;

  /* Spacing scale (4px base) */
  --sp-1: 4px;
  --sp-2: 8px;
  --sp-3: 12px;
  --sp-4: 16px;
  --sp-5: 24px;
  --sp-6: 32px;
  --sp-7: 48px;

  /* Radii */
  --radius-sm: 4px;
  --radius:    8px;
  --radius-lg: 12px;

  /* Surfaces & text */
  --bg:            #f6f8fa;
  --surface:       #ffffff;
  --surface-alt:   #f0f3f7;
  --border:        #dfe3e8;
  --border-strong: #c3c9d1;
  --text:          #1a1d23;
  --text-muted:    #5b6470;
  --text-subtle:   #838d9c;

  /* Primary (clinical teal) */
  --primary:      #0f766e;
  --primary-hover:#0b5e57;
  --primary-fg:   #ffffff;
  --primary-soft: #e6f4f1;

  /* Danger (critical values, high flags) */
  --danger:        #b91c1c;
  --danger-bg:     #fef2f2;
  --danger-border: #fecaca;
  --danger-fg:     #7f1d1d;

  /* Warning (low flags, in-progress states) */
  --warn:        #b45309;
  --warn-bg:     #fffbeb;
  --warn-border: #fde68a;
  --warn-fg:     #78350f;

  /* Success (within-range flags, key set) */
  --success:        #15803d;
  --success-bg:     #f0fdf4;
  --success-border: #bbf7d0;

  /* Shadows */
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.05);
  --shadow:    0 1px 3px rgba(16, 24, 40, 0.08),
               0 1px 2px rgba(16, 24, 40, 0.04);
  --shadow-md: 0 4px 8px rgba(16, 24, 40, 0.08),
               0 2px 4px rgba(16, 24, 40, 0.04);

  /* Focus ring — used everywhere for keyboard accessibility */
  --focus-ring: 0 0 0 3px rgba(15, 118, 110, 0.25);
}


/* ==========================================================================
   2. RESET & BASE
   ========================================================================== */

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

/* Hidden by attribute wins over any display rule. Panels toggle via
   element.hidden = true/false in app.js. */
[hidden] {
  display: none !important;
}

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

body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: var(--fs-base);
  line-height: var(--lh-normal);
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

h1, h2, h3, h4, p, ul, ol {
  margin: 0;
}

a {
  color: var(--primary);
  text-decoration: underline;
  text-underline-offset: 2px;
}
a:hover { text-decoration: none; }

button { font: inherit; }

/* Screen-reader-only helper — used for the "Remove" column header */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}


/* ==========================================================================
   3. HEADER
   ========================================================================== */

.app-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: var(--sp-5) 0;
}

.header-inner {
  max-width: 960px;
  margin: 0 auto;
  padding: 0 var(--sp-5);
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: var(--sp-5);
  flex-wrap: wrap;
}

.header-title h1 {
  font-size: var(--fs-2xl);
  font-weight: 700;
  letter-spacing: -0.01em;
  line-height: var(--lh-tight);
  color: var(--text);
}

.tagline {
  font-size: var(--fs-sm);
  color: var(--text-muted);
  margin-top: var(--sp-2);
  max-width: 620px;
}

.tagline-em {
  display: block;
  color: var(--primary);
  font-weight: 600;
  font-size: var(--fs-xs);
  margin-top: var(--sp-1);
  letter-spacing: 0.01em;
}

/* ---- Status pills (license + key state) ---- */

.status-bar {
  display: flex;
  gap: var(--sp-2);
  flex-wrap: wrap;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  font-size: var(--fs-xs);
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 999px;
  letter-spacing: 0.02em;
  white-space: nowrap;
  border: 1px solid transparent;
}

.status-pill-warn {
  background: var(--warn-bg);
  color: var(--warn-fg);
  border-color: var(--warn-border);
}

.status-pill-ok {
  background: var(--success-bg);
  color: var(--success);
  border-color: var(--success-border);
}


/* ==========================================================================
   4. MAIN LAYOUT & PANELS
   ========================================================================== */

.app-main {
  max-width: 960px;
  margin: 0 auto;
  padding: var(--sp-5);
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--sp-5);
  margin-bottom: var(--sp-5);
  box-shadow: var(--shadow-sm);
}

.panel > h2 {
  font-size: var(--fs-xl);
  font-weight: 600;
  letter-spacing: -0.005em;
  margin-bottom: var(--sp-4);
  color: var(--text);
}

.hint {
  font-size: var(--fs-sm);
  color: var(--text-muted);
  line-height: var(--lh-normal);
  margin-bottom: var(--sp-3);
}

/* ---- About details block ---- */

.about {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--sp-3) var(--sp-4);
  margin-bottom: var(--sp-5);
  font-size: var(--fs-sm);
  color: var(--text-muted);
  box-shadow: var(--shadow-sm);
}

.about summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--text);
  list-style: none;
  display: flex;
  align-items: center;
  gap: var(--sp-2);
}

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

.about summary::before {
  content: "▸";
  display: inline-block;
  transition: transform 0.15s ease;
  color: var(--text-muted);
  font-size: 0.9em;
}

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

.about p {
  margin-top: var(--sp-3);
  line-height: var(--lh-loose);
}


/* ==========================================================================
   5. FORM CONTROLS (inputs, textareas, selects)
   ========================================================================== */

input[type="text"],
input[type="password"],
textarea,
select {
  font-family: var(--font-sans);
  font-size: var(--fs-base);
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  width: 100%;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

input[type="text"]:focus,
input[type="password"]:focus,
textarea:focus,
select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: var(--focus-ring);
}

input::placeholder,
textarea::placeholder {
  color: var(--text-subtle);
}

/* Raw-data textareas use monospace so aligned lab columns remain readable */
textarea {
  font-family: var(--font-mono);
  font-size: var(--fs-sm);
  line-height: 1.6;
  resize: vertical;
  min-height: 120px;
}

/* License and API key fields are codes, so use monospace too */
#license-input,
#api-key-input {
  font-family: var(--font-mono);
  font-size: var(--fs-sm);
  letter-spacing: 0.02em;
}

input[type="file"] {
  font-size: var(--fs-sm);
  color: var(--text-muted);
  margin-bottom: var(--sp-3);
}


/* ==========================================================================
   6. BUTTONS
   ========================================================================== */

button {
  cursor: pointer;
  border-radius: var(--radius-sm);
  padding: 9px 16px;
  font-weight: 500;
  font-size: var(--fs-sm);
  border: 1px solid transparent;
  transition: background 0.15s ease, color 0.15s ease,
              border-color 0.15s ease, box-shadow 0.15s ease;
  line-height: 1.2;
  white-space: nowrap;
}

button:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.primary-btn {
  background: var(--primary);
  color: var(--primary-fg);
  border-color: var(--primary);
}

.primary-btn:hover:not(:disabled) {
  background: var(--primary-hover);
  border-color: var(--primary-hover);
}

.ghost-btn {
  background: transparent;
  color: var(--text);
  border-color: var(--border-strong);
}

.ghost-btn:hover:not(:disabled) {
  background: var(--surface-alt);
  border-color: var(--text-muted);
}

.button-row {
  display: flex;
  gap: var(--sp-2);
  flex-wrap: wrap;
  margin-top: var(--sp-3);
  align-items: center;
}


/* ==========================================================================
   7. SETUP SECTION
   ========================================================================== */

.setup-grid {
  display: grid;
  gap: var(--sp-4);
}

.setup-field > label {
  display: block;
  font-size: var(--fs-sm);
  font-weight: 600;
  color: var(--text);
  margin-bottom: var(--sp-2);
}

.input-row {
  display: flex;
  gap: var(--sp-2);
  align-items: stretch;
  flex-wrap: wrap;
}

.input-row input {
  flex: 1 1 240px;
  min-width: 0;
}


/* ==========================================================================
   8. INPUT SECTION — TABS
   ========================================================================== */

.tabs {
  display: flex;
  gap: 2px;
  border-bottom: 1px solid var(--border);
  margin-bottom: var(--sp-4);
  flex-wrap: wrap;
}

.tab-btn {
  background: transparent;
  border: none;
  border-radius: 0;
  padding: 10px 16px;
  font-weight: 500;
  font-size: var(--fs-sm);
  color: var(--text-muted);
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  transition: color 0.15s ease, border-color 0.15s ease, background 0.15s ease;
}

.tab-btn:hover {
  color: var(--text);
  background: var(--surface-alt);
}

.tab-btn.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  font-weight: 600;
}

.tab-btn:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
  border-radius: var(--radius-sm);
}

.tab-panel {
  padding-top: var(--sp-1);
}


/* ==========================================================================
   9. REVIEW TABLE
   ========================================================================== */

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  margin-bottom: var(--sp-2);
}

#review-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--fs-sm);
  min-width: 640px;
}

#review-table thead th {
  background: var(--surface-alt);
  text-align: left;
  padding: 10px 12px;
  font-size: var(--fs-xs);
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

#review-table tbody td {
  padding: 4px 6px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

#review-table tbody tr:last-child td {
  border-bottom: none;
}

#review-table tbody tr:hover {
  background: var(--surface-alt);
}

/* Editable cells look like inline text until focused */
#review-table tbody td input,
#review-table tbody td select {
  border: 1px solid transparent;
  background: transparent;
  padding: 7px 8px;
  font-size: var(--fs-sm);
  border-radius: var(--radius-sm);
  transition: border-color 0.1s ease, background 0.1s ease;
}

#review-table tbody td input:hover,
#review-table tbody td select:hover {
  border-color: var(--border);
  background: var(--surface);
}

#review-table tbody td input:focus,
#review-table tbody td select:focus {
  border-color: var(--primary);
  background: var(--surface);
  box-shadow: 0 0 0 2px rgba(15, 118, 110, 0.15);
  outline: none;
}

/* Value / low / high columns: right-aligned monospace numerics */
#review-table td.cell-number input {
  font-family: var(--font-mono);
  text-align: right;
}

/* Remove-row button */
#review-table td.cell-remove {
  width: 36px;
  text-align: center;
  padding: 4px;
}

#review-table td.cell-remove button {
  width: 28px;
  height: 28px;
  padding: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-subtle);
  border: 1px solid transparent;
  font-size: 16px;
  line-height: 1;
}

#review-table td.cell-remove button:hover {
  color: var(--danger);
  background: var(--danger-bg);
  border-color: var(--danger-border);
}


/* ==========================================================================
   10. CRITICAL VALUE BANNER
   ========================================================================== */

.critical-banner {
  background: var(--danger-bg);
  border: 1px solid var(--danger-border);
  border-left: 4px solid var(--danger);
  color: var(--danger-fg);
  padding: var(--sp-4) var(--sp-4);
  border-radius: var(--radius);
  margin-bottom: var(--sp-5);
  font-weight: 500;
  line-height: var(--lh-normal);
}

.critical-banner strong {
  display: block;
  font-weight: 700;
  margin-bottom: var(--sp-2);
  font-size: var(--fs-base);
}

.critical-banner ul {
  margin: var(--sp-2) 0 0;
  padding-left: var(--sp-5);
}

.critical-banner li {
  margin-bottom: var(--sp-1);
}


/* ==========================================================================
   11. OUTPUT — PANEL SUMMARY & CARDS
   ========================================================================== */

.panel-summary {
  background: var(--primary-soft);
  color: var(--text);
  padding: var(--sp-3) var(--sp-4);
  border-radius: var(--radius);
  border-left: 3px solid var(--primary);
  font-size: var(--fs-base);
  line-height: var(--lh-loose);
  margin-bottom: var(--sp-4);
}

#cards-container {
  display: flex;
  flex-direction: column;
  gap: var(--sp-3);
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--sp-4);
  box-shadow: var(--shadow-sm);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: var(--sp-3);
  margin-bottom: var(--sp-2);
}

.card-title {
  font-size: var(--fs-lg);
  font-weight: 600;
  color: var(--text);
  line-height: var(--lh-tight);
}

/* Flag pill — set via data-flag attribute from app.js */
.flag {
  display: inline-block;
  font-size: var(--fs-xs);
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 999px;
  letter-spacing: 0.02em;
  white-space: nowrap;
  text-transform: none;
  line-height: 1.4;
}

.flag[data-flag="H"] {
  background: var(--danger-bg);
  color: var(--danger);
  border: 1px solid var(--danger-border);
}

.flag[data-flag="L"] {
  background: var(--warn-bg);
  color: var(--warn-fg);
  border: 1px solid var(--warn-border);
}

.flag[data-flag="N"] {
  background: var(--success-bg);
  color: var(--success);
  border: 1px solid var(--success-border);
}

/* Editable body */
.card-text {
  padding: var(--sp-2) var(--sp-3);
  margin: var(--sp-2) calc(var(--sp-3) * -1);
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  line-height: var(--lh-loose);
  color: var(--text);
  transition: border-color 0.15s ease, background 0.15s ease;
  min-height: 1.7em;
}

.card-text:hover {
  border-color: var(--border);
  background: var(--surface-alt);
  cursor: text;
}

.card-text:focus {
  outline: none;
  border-color: var(--primary);
  background: var(--surface);
  box-shadow: var(--focus-ring);
}

.card-text p {
  margin: 0 0 var(--sp-2);
}

.card-text p:last-child {
  margin-bottom: 0;
}

.card-question {
  font-style: italic;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
  padding-top: var(--sp-3);
  margin-top: var(--sp-3) !important;
}


/* ==========================================================================
   12. REVIEW GATE
   ========================================================================== */

.review-gate {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--sp-4);
  margin: var(--sp-5) 0 var(--sp-3);
}

.review-gate-label {
  display: flex;
  gap: var(--sp-3);
  align-items: flex-start;
  cursor: pointer;
  font-weight: 500;
  color: var(--text);
  line-height: var(--lh-normal);
}

.review-gate-label input[type="checkbox"] {
  margin-top: 3px;
  width: 18px;
  height: 18px;
  accent-color: var(--primary);
  cursor: pointer;
  flex-shrink: 0;
}

.review-gate-label input[type="checkbox"]:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
  border-radius: var(--radius-sm);
}

.export-buttons {
  margin-top: var(--sp-4);
  padding-top: var(--sp-4);
  border-top: 1px solid var(--border);
}


/* ==========================================================================
   13. STATUS AREA
   ========================================================================== */

.status-area {
  min-height: 24px;
  padding: var(--sp-2) 0;
  font-size: var(--fs-sm);
  color: var(--text-muted);
  text-align: center;
  line-height: var(--lh-normal);
}

.status-area.error {
  color: var(--danger);
  font-weight: 500;
}

.status-area.success {
  color: var(--success);
  font-weight: 500;
}


/* ==========================================================================
   14. FOOTER
   ========================================================================== */

.app-footer {
  max-width: 960px;
  margin: 0 auto;
  padding: var(--sp-5) var(--sp-5) var(--sp-6);
  font-size: var(--fs-xs);
  color: var(--text-subtle);
  line-height: var(--lh-loose);
  text-align: center;
  border-top: 1px solid var(--border);
}

.app-footer p {
  margin-bottom: var(--sp-2);
}

.footer-small {
  color: var(--text-subtle);
  font-size: var(--fs-xs);
  letter-spacing: 0.02em;
}


/* ==========================================================================
   15. RESPONSIVE
   ========================================================================== */

@media (max-width: 640px) {
  .app-main { padding: var(--sp-4) var(--sp-3); }
  .header-inner { padding: 0 var(--sp-3); }
  .panel { padding: var(--sp-4); border-radius: var(--radius); }
  .app-footer { padding: var(--sp-4) var(--sp-3); }

  .header-title h1 { font-size: var(--fs-xl); }

  .status-bar { width: 100%; }

  .input-row { flex-direction: column; align-items: stretch; }
  .input-row button { width: 100%; }

  .button-row { flex-direction: column; align-items: stretch; }
  .button-row button { width: 100%; }

  .card-header { flex-wrap: wrap; }
}


/* ==========================================================================
   16. PRINT — the printout is the patient-facing artifact
   ========================================================================== */

@media print {
  @page { margin: 2cm; }

  body {
    background: #ffffff;
    color: #000000;
    font-size: 12pt;
    font-family: Georgia, "Times New Roman", serif;
  }

  /* Hide everything that isn't the patient-facing output */
  .app-header,
  .app-footer,
  .about,
  #setup-section,
  #input-section,
  #review-section,
  .review-gate,
  .export-buttons,
  .status-area,
  .hint,
  .tab-btn,
  .button-row {
    display: none !important;
  }

  .app-main { padding: 0; max-width: none; }

  .panel {
    border: none;
    box-shadow: none;
    padding: 0;
    margin: 0;
    background: #ffffff;
  }

  #output-section { display: block !important; }

  .panel > h2 {
    font-size: 16pt;
    margin-bottom: 8pt;
    font-family: Georgia, serif;
  }

  .panel-summary {
    background: #f5f5f5;
    border-left: 3px solid #000;
    color: #000;
    font-family: Georgia, serif;
  }

  #cards-container {
    display: block;
  }

  .card {
    border: 1px solid #999;
    box-shadow: none;
    background: #ffffff;
    margin-bottom: 12pt;
    break-inside: avoid;
    page-break-inside: avoid;
  }

  .card-text,
  .card-text * {
    background: transparent !important;
    border: none !important;
    box-shadow: none !important;
    color: #000 !important;
    padding-left: 0 !important;
    margin-left: 0 !important;
  }

  .card-question {
    border-top: 1px solid #ccc !important;
    padding-top: 6pt !important;
    color: #333 !important;
    font-style: italic;
  }

  .card-title { color: #000; }

  .flag {
    border: 1px solid #333 !important;
    background: #fff !important;
    color: #000 !important;
  }

  .critical-banner {
    border: 2px solid #000;
    background: #fff;
    color: #000;
    break-inside: avoid;
  }
}