/* ── Reset & Base ────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f0f2f5;
  --card: #ffffff;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --danger: #ef4444;
  --success: #16a34a;
  --warn: #f59e0b;
  --text: #1e293b;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06);
}

body {
  font-family: 'Poppins', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
}

/* ── Header ──────────────────────────────────────── */
.header-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 1100px;
  margin: 0 auto;
  gap: 1rem;
}
.header-title { flex: 1; }
header {
  background: #e5fbc5;
  color: var(--text);
  text-align: left;
  padding: 1.5rem 1.5rem 1.25rem;
  border-bottom: 3px solid #1a6e3a;
  box-shadow: 0 2px 8px rgba(0,0,0,.06);
}
header h1 { margin: 0; line-height: 1; }
.app-logo { height: 80px; width: auto; flex-shrink: 0; }
.subtitle { color: var(--text-muted); font-size: .95rem; margin-top: .4rem; }

/* ── Member Selector ─────────────────────────────── */
.member-selector {
  display: flex;
  align-items: center;
  gap: .5rem;
  flex-shrink: 0;
}
.member-selector select {
  padding: .45rem .75rem;
  border: 1px solid #0f4a04;
  border-radius: 6px;
  font-size: .88rem;
  font-weight: 500;
  background: #135905;
  color: #fff;
  cursor: pointer;
  min-width: 140px;
  appearance: auto;
}
.member-selector select:focus {
  outline: none;
  border-color: #1a6e3a;
  box-shadow: 0 0 0 2px rgba(19,89,5,.2);
}
.member-selector select option {
  background: #135905;
  color: #fff;
}
.btn-add-member, .btn-delete-member, .btn-default-member {
  width: 34px;
  height: 34px;
  border: 1px solid #0f4a04;
  border-radius: 6px;
  background: #135905;
  color: #fff;
  font-size: 1.3rem;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all .15s;
  line-height: 1;
}
.btn-add-member:hover {
  background: #1a6e3a;
  border-color: #1a6e3a;
}
.btn-delete-member:hover {
  background: #7f1d1d;
  border-color: #7f1d1d;
}
.btn-default-member:hover {
  background: #1a6e3a;
  border-color: #1a6e3a;
  color: #fbbf24;
}
.btn-default-member.is-default {
  background: #1a6e3a;
  border-color: #1a6e3a;
  color: #fbbf24;
}

/* ── Tabs ────────────────────────────────────────── */
.tabs {
  display: flex;
  justify-content: center;
  gap: .5rem;
  padding: 1rem 1rem 0;
  max-width: 1100px;
  margin: 0 auto;
}
.tab {
  padding: .55rem 1.3rem;
  border: none;
  background: #135905;
  border-radius: var(--radius) var(--radius) 0 0;
  cursor: pointer;
  font-weight: 500;
  color: rgba(255,255,255,.75);
  font-size: .9rem;
  transition: all .15s;
  border-bottom: 3px solid transparent;
}
.tab:hover { color: #fff; background: #1a6e3a; }
.tab.active { color: #fff; border-bottom-color: #fbbf24; background: #1a6e3a; }

/* ── Cards & Layout ──────────────────────────────── */
main { max-width: 1100px; margin: 0 auto; padding: 0 1rem 2rem; }
.tab-content { display: none; }
.tab-content.active { display: block; }
.card {
  background: #f2fde4;
  border-radius: 0 0 var(--radius) var(--radius);
  padding: 1.5rem;
  box-shadow: var(--shadow);
}

/* ── Upload Zone ─────────────────────────────────── */
.upload-hint { font-size: .88rem; color: var(--text-muted); margin-bottom: 1rem; }

.drop-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 2.5rem 1.5rem;
  text-align: center;
  transition: all .2s;
  cursor: pointer;
}
.drop-zone:hover, .drop-zone.drag-over {
  border-color: #135905;
  background: #e5fbc5;
}
.drop-zone-icon { font-size: 3rem; margin-bottom: .5rem; }
.drop-zone-text { font-size: 1rem; font-weight: 500; color: var(--text); }
.drop-zone-or { font-size: .82rem; color: var(--text-muted); margin: .5rem 0; }
.btn-upload {
  display: inline-block;
  cursor: pointer;
  padding: .55rem 1.5rem;
  border-radius: 6px;
  font-size: .9rem;
}

/* ── Extraction Status ───────────────────────────── */
.extract-status {
  display: flex;
  align-items: center;
  gap: .75rem;
  margin-top: 1.25rem;
  padding: .75rem 1rem;
  background: #e5fbc5;
  border-radius: 8px;
  font-size: .9rem;
  color: #135905;
}
.spinner {
  width: 20px; height: 20px;
  border: 3px solid #c6e8a0;
  border-top-color: #135905;
  border-radius: 50%;
  animation: spin .6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Preview Section ─────────────────────────────── */
.preview-section { margin-top: 1.5rem; }
.preview-section h3 { font-size: 1.05rem; color: var(--text); margin-bottom: .75rem; text-transform: none; letter-spacing: 0; }
.preview-meta { display: flex; gap: 1.5rem; flex-wrap: wrap; margin-bottom: 1rem; }
.preview-meta .form-row { flex: 1; min-width: 200px; }
.preview-hint { font-size: .82rem; color: var(--text-muted); margin-bottom: .75rem; }
.val-high-preview { color: var(--danger); font-weight: 700; }

.preview-grid { display: grid; grid-template-columns: 1fr; gap: 0; }
.preview-group-header {
  background: #e5f5d5;
  font-weight: 600;
  color: #135905;
  font-size: .82rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  padding: .5rem .7rem;
  margin-top: .5rem;
  border-radius: 4px;
}
.preview-row {
  display: grid;
  grid-template-columns: 1fr 100px 120px;
  gap: .5rem;
  align-items: center;
  padding: .35rem .7rem;
  border-bottom: 1px solid var(--border);
  font-size: .85rem;
}
.preview-row:hover { background: #e9f8d6; }
.preview-marker { font-weight: 500; }
.preview-marker .unit { color: var(--text-muted); font-weight: 400; font-size: .75rem; }
.preview-marker .range { color: var(--text-muted); font-weight: 400; font-size: .72rem; opacity: .7; display: block; }
.preview-value input {
  width: 85px;
  padding: .3rem .5rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: .85rem;
  text-align: center;
}
.preview-value input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 2px rgba(37,99,235,.12); }
.preview-value input.has-value { border-color: #135905; background: #f2fde4; }
.preview-status { font-size: .78rem; font-weight: 500; }
.preview-status.normal { color: var(--success); }
.preview-status.high { color: var(--danger); font-weight: 700; }
.preview-status.low { color: var(--warn); }
.preview-status.empty { color: #cbd5e1; }

/* ── Raw Text Debug ──────────────────────────────── */
.raw-text-toggle { font-size: .82rem; color: var(--text-muted); cursor: pointer; }
.raw-text-box {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: .75rem;
  font-size: .75rem;
  max-height: 300px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-word;
  margin-top: .5rem;
}

/* ── Form ────────────────────────────────────────── */
h2 { font-size: 1.25rem; margin-bottom: 1rem; }
h3 { font-size: .95rem; color: var(--primary); margin: 1.25rem 0 .5rem; text-transform: uppercase; letter-spacing: .04em; }
.form-row { margin-bottom: .75rem; }
.form-row label { display: block; font-size: .82rem; font-weight: 500; margin-bottom: .2rem; color: var(--text-muted); }
.form-row input[type="date"], .form-row input[type="text"] {
  width: 100%;
  max-width: 350px;
  padding: .45rem .6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: .88rem;
}
.form-row input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,.12); }

.form-actions { display: flex; gap: .75rem; margin-top: 1.5rem; }
.btn-primary {
  padding: .6rem 1.5rem;
  background: #135905;
  color: #fff;
  border: none;
  border-radius: 6px;
  font-weight: 600;
  font-size: .9rem;
  cursor: pointer;
  transition: background .15s;
}
.btn-primary:hover { background: #1a6e3a; }
.btn-secondary {
  padding: .6rem 1.5rem;
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-weight: 500;
  font-size: .9rem;
  cursor: pointer;
  transition: all .15s;
}
.btn-secondary:hover { border-color: var(--text-muted); color: var(--text); }
.btn-sm { padding: .35rem 1rem; font-size: .82rem; }

/* ── Table ───────────────────────────────────────── */
.table-header { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: .5rem; }
.table-wrap { overflow-x: auto; margin-top: .75rem; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: .85rem; }
th, td { padding: .55rem .7rem; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { background: #e5f5d5; font-weight: 600; position: sticky; top: 0; color: var(--text-muted); font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; }
td:first-child, th:first-child { position: sticky; left: 0; background: #f2fde4; z-index: 1; font-weight: 500; }
th:first-child { background: #e5f5d5; z-index: 2; }
tr:hover td { background: #e9f8d6; }
tr:hover td:first-child { background: #ddf3c8; }

.val-normal { color: var(--success); font-weight: 600; }
.val-high { color: var(--danger); font-weight: 700; }
.val-low { color: var(--warn); font-weight: 600; }
.val-empty { color: #cbd5e1; font-weight: 400; }

/* Bio Ref Interval column */
.col-ref { color: var(--text-muted); font-size: .78rem; font-weight: 400; white-space: nowrap; }
th.col-ref-head { white-space: nowrap; }

/* % change annotation under value */
.pct-change {
  display: block;
  font-size: .7rem;
  font-weight: 500;
  margin-top: 1px;
  line-height: 1.1;
}
.pct-change.pct-up { color: var(--danger); }
.pct-change.pct-down { color: var(--success); }
.pct-change.pct-same { color: var(--text-muted); }

/* Overall trend column */
.trend-up { color: var(--danger); font-weight: 600; font-size: .82rem; }
.trend-down { color: var(--success); font-weight: 600; font-size: .82rem; }
.trend-same { color: var(--text-muted); font-size: .82rem; }

.hint { font-size: .8rem; color: var(--text-muted); margin-top: .25rem; }
.legend-normal { color: var(--success); font-weight: 600; }
.legend-high { color: var(--danger); font-weight: 700; }
.legend-low { color: var(--warn); font-weight: 600; }
.no-data { text-align: center; padding: 2rem; color: var(--text-muted); }

/* Delete / edit buttons in table header */
.date-header { display: flex; align-items: center; gap: .35rem; }
.btn-delete-entry {
  background: none;
  border: none;
  color: var(--danger);
  cursor: pointer;
  font-size: .85rem;
  padding: 0 .2rem;
  opacity: .6;
  transition: opacity .15s;
}
.btn-delete-entry:hover { opacity: 1; }

/* Inline editing in table */
.table-editing td { padding: .3rem .4rem; }
.table-editing .cell-edit-input {
  width: 70px;
  padding: .25rem .4rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: .82rem;
  text-align: center;
  background: #fefffe;
  transition: border-color .15s;
}
.table-editing .cell-edit-input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(37,99,235,.12);
}
.table-editing .cell-edit-input.changed {
  border-color: var(--primary);
  background: #eff6ff;
}
.edit-mode-banner {
  background: #e5fbc5;
  border: 1px solid #a3d977;
  border-radius: 6px;
  padding: .5rem 1rem;
  margin-bottom: .75rem;
  font-size: .82rem;
  color: #135905;
  display: none;
  align-items: center;
  gap: .5rem;
}
.edit-mode-banner.visible { display: flex; }
.edit-count { font-weight: 600; }

/* ── Insights ────────────────────────────────────── */
.insight-section { margin-bottom: 1.5rem; }
.insight-section-title {
  font-size: .95rem;
  font-weight: 600;
  margin-bottom: .75rem;
  padding-bottom: .4rem;
  border-bottom: 2px solid var(--border);
  text-transform: none;
  letter-spacing: 0;
}
.insight-focus .insight-section-title { color: var(--danger); border-bottom-color: var(--danger); }
.insight-improved .insight-section-title { color: var(--success); border-bottom-color: var(--success); }

.insight-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: .75rem;
}

.insight-item {
  border-radius: 8px;
  padding: .85rem 1rem;
  border-left: 4px solid transparent;
  transition: box-shadow .15s;
}
.insight-item:hover { box-shadow: var(--shadow); }
.insight-item-high { background: #fef2f2; border-left-color: var(--danger); }
.insight-item-low { background: #fffbeb; border-left-color: var(--warn); }
.insight-item-good { background: #f0fdf4; border-left-color: var(--success); }

.insight-item-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: .3rem;
}
.insight-label { font-weight: 600; font-size: .9rem; color: var(--text); }
.insight-group { font-size: .72rem; color: var(--text-muted); background: #f1f5f9; padding: .1rem .45rem; border-radius: 3px; }
.insight-value { font-size: 1.15rem; font-weight: 700; margin-bottom: .15rem; }
.insight-item-high .insight-value { color: var(--danger); }
.insight-item-low .insight-value { color: var(--warn); }
.insight-item-good .insight-value { color: var(--success); }

.insight-detail { font-size: .78rem; color: var(--text-muted); margin-bottom: .3rem; }
.insight-meta { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.insight-trend {
  font-size: .72rem;
  font-weight: 600;
  padding: .15rem .5rem;
  border-radius: 10px;
}
.trend-good { background: #dcfce7; color: #166534; }
.trend-bad { background: #fee2e2; color: #991b1b; }
.insight-change { font-size: .72rem; color: var(--text-muted); }

.insight-all-normal {
  text-align: center;
  padding: 2rem 1rem;
  color: var(--success);
  font-weight: 600;
  font-size: 1rem;
}
.insight-normal-summary {
  text-align: center;
  padding: .75rem;
  margin-top: .5rem;
  background: #f0fdf4;
  border-radius: 8px;
  font-size: .85rem;
  color: #166534;
}
.insight-normal-count { font-weight: 700; }
.insight-hint {
  text-align: center;
  font-size: .82rem;
  color: var(--text-muted);
  margin-top: 1rem;
  font-style: italic;
}

/* ── Footer ──────────────────────────────────────── */
footer { text-align: center; padding: 1rem; font-size: .78rem; color: var(--text-muted); }
footer a { color: var(--text-muted); text-decoration: none; opacity: .7; transition: opacity .15s; }
footer a:hover { opacity: 1; text-decoration: underline; }

/* ── Toast ───────────────────────────────────────── */
.toast {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: #1e293b;
  color: #fff;
  padding: .6rem 1.5rem;
  border-radius: 8px;
  font-size: .88rem;
  opacity: 0;
  transition: all .3s ease;
  z-index: 1000;
  pointer-events: none;
}
.toast.show { transform: translateX(-50%) translateY(0); opacity: 1; }

/* ── Responsive ──────────────────────────────────── */

/* ── Tablet (max 768px) ─────────────────────────── */
@media (max-width: 768px) {
  header { padding: 1.25rem 1rem 1rem; }
  header h1 { font-size: 1.5rem; }
  .app-logo { height: 60px; }
  .subtitle { font-size: .85rem; }

  .header-top {
    flex-wrap: wrap;
    gap: .75rem;
  }
  .header-title { min-width: 0; }
  .member-selector { width: auto; }
  .member-selector select { min-width: 120px; font-size: .82rem; }

  main { padding: 0 .75rem 1.5rem; }
  .card { padding: 1.25rem; }

  .tabs { gap: .35rem; padding: .75rem .75rem 0; }
  .tab { padding: .5rem 1rem; font-size: .84rem; }

  .drop-zone { padding: 2rem 1rem; }
  .drop-zone-icon { font-size: 2.5rem; }

  .preview-row { grid-template-columns: 1fr 90px 100px; gap: .4rem; }
  .preview-meta { gap: 1rem; }

  .table-header { gap: .4rem; }
  table { font-size: .82rem; }
  th, td { padding: .45rem .55rem; }

  .insight-grid { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }

  h2 { font-size: 1.15rem; }
}

/* ── Mobile (max 480px) ─────────────────────────── */
@media (max-width: 480px) {
  header { padding: 1rem .75rem .85rem; }
  header h1 { font-size: 1.3rem; justify-content: center; }
  .app-logo { height: 48px; }
  .subtitle { font-size: .78rem; margin-top: .15rem; }

  .header-top {
    flex-direction: column;
    align-items: stretch;
    gap: .6rem;
  }
  .header-title { text-align: center; }
  .member-selector {
    justify-content: center;
    gap: .4rem;
  }
  .member-selector select {
    min-width: 0;
    flex: 1;
    max-width: 180px;
    font-size: .8rem;
    padding: .4rem .55rem;
  }
  .btn-add-member, .btn-delete-member, .btn-default-member {
    width: 30px;
    height: 30px;
    font-size: 1.1rem;
  }

  main { padding: 0 .5rem 1.25rem; }
  .card { padding: .85rem; }

  .tabs { gap: .2rem; padding: .5rem .5rem 0; }
  .tab {
    padding: .45rem .6rem;
    font-size: .78rem;
    flex: 1;
    text-align: center;
  }

  .drop-zone { padding: 1.5rem .75rem; }
  .drop-zone-icon { font-size: 2rem; }
  .drop-zone-text { font-size: .88rem; }
  .btn-upload { padding: .45rem 1.2rem; font-size: .82rem; }

  .upload-hint { font-size: .8rem; }

  .preview-section h3 { font-size: .95rem; }
  .preview-hint { font-size: .76rem; }
  .preview-meta { flex-direction: column; gap: .6rem; }
  .preview-meta .form-row { min-width: 0; }
  .preview-row {
    grid-template-columns: 1fr 70px 80px;
    gap: .3rem;
    padding: .3rem .5rem;
    font-size: .78rem;
  }
  .preview-value input { width: 65px; font-size: .78rem; padding: .25rem .35rem; }
  .preview-group-header { font-size: .75rem; padding: .4rem .5rem; }

  .form-actions { flex-direction: column; gap: .5rem; }
  .form-actions .btn-primary,
  .form-actions .btn-secondary { width: 100%; text-align: center; }

  .form-row input[type="date"],
  .form-row input[type="text"] { max-width: 100%; }

  h2 { font-size: 1.05rem; margin-bottom: .75rem; }

  .table-header {
    flex-direction: column;
    align-items: stretch;
    gap: .5rem;
  }
  .table-actions {
    display: flex;
    gap: .4rem;
    flex-wrap: wrap;
  }
  .table-actions .btn-sm { flex: 1; text-align: center; min-width: 0; }
  .table-wrap { margin-top: .5rem; -webkit-overflow-scrolling: touch; }
  table { font-size: .75rem; }
  th, td { padding: .4rem .45rem; }
  .col-ref { font-size: .7rem; }
  .pct-change { font-size: .62rem; }
  .hint { font-size: .72rem; }

  .edit-mode-banner { font-size: .75rem; padding: .4rem .75rem; }
  .table-editing .cell-edit-input { width: 55px; font-size: .75rem; }

  .insight-grid { grid-template-columns: 1fr; gap: .6rem; }
  .insight-item { padding: .7rem .85rem; }
  .insight-label { font-size: .84rem; }
  .insight-value { font-size: 1.05rem; }
  .insight-detail { font-size: .72rem; }
  .insight-section-title { font-size: .88rem; }
  .insight-normal-summary { font-size: .8rem; padding: .6rem; }

  .extract-status { font-size: .82rem; padding: .6rem .75rem; }

  footer { font-size: .7rem; padding: .75rem .5rem; }

  .toast { font-size: .8rem; padding: .5rem 1.2rem; bottom: 1rem; }
}

/* ── Small mobile (max 360px) ───────────────────── */
@media (max-width: 360px) {
  header h1 { font-size: 1.15rem; }
  .app-logo { height: 40px; }
  .subtitle { font-size: .72rem; }

  .tab { padding: .4rem .45rem; font-size: .72rem; }

  .preview-row { grid-template-columns: 1fr 60px 65px; font-size: .72rem; }
  .preview-value input { width: 55px; font-size: .72rem; }

  table { font-size: .7rem; }
  th, td { padding: .35rem .35rem; }

  .insight-item { padding: .6rem .7rem; }
  .insight-label { font-size: .78rem; }
  .insight-value { font-size: .95rem; }
}

/* ── Large screens (min 1200px) ─────────────────── */
@media (min-width: 1200px) {
  .header-top, main, .tabs { max-width: 1200px; }
  .insight-grid { grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
}
