/* =========================================================
   MedParse — stylesheet
   All CSS lives here per CSP (style-src 'self', no inline style).
   System font stack only — no webfonts, fast on slow connections.
   ========================================================= */

:root {
  --bg: #f7f9fb;
  --surface: #ffffff;
  --border: #dfe6ee;
  --text: #16232f;
  --muted: #5b6b7c;

  --primary: #1f6feb;
  --primary-dark: #154fa8;
  --primary-tint: #eaf1ff;

  --success: #1a8a4a;
  --success-tint: #e7f5ec;

  --warning: #a15c07;
  --warning-tint: #fbf0dd;

  --danger: #c0392b;
  --danger-tint: #fbeae7;

  --processing: #6f5bd6;
  --processing-tint: #eeebfb;

  --radius-sm: 8px;
  --radius-md: 10px;
  --radius-lg: 12px;
  --font: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 17px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

h1, h2, h3 { margin: 0 0 12px; font-weight: 700; text-wrap: pretty; }
h1 { font-size: 28px; }
h2 { font-size: 22px; }
h3 { font-size: 18px; }

a { color: var(--primary); text-decoration: none; font-weight: 600; }
a:hover { color: var(--primary-dark); text-decoration: underline; }

button, input, select, textarea { font-family: inherit; font-size: inherit; }

.container {
  max-width: 1100px;
  margin: 0 auto;
  padding: 0 20px;
}

.page { padding-top: 28px; padding-bottom: 60px; }

/* ---------- nav ---------- */
.navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  gap: 16px;
}
.navbar__brand { font-size: 18px; font-weight: 700; color: var(--primary-dark); }
.navbar__brand:hover { text-decoration: none; }
.navbar__right { display: flex; align-items: center; gap: 16px; }
.navbar__action { font-size: 14px; color: var(--muted); font-weight: 600; }
.navbar__action:hover { color: var(--text); }
.navbar__who { font-size: 14px; color: var(--muted); }

/* ---------- generic card / form ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 24px;
}

.auth-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px 16px;
}
.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 32px;
}
.auth-card__brand { font-size: 20px; font-weight: 700; color: var(--primary-dark); margin-bottom: 24px; }
.auth-card__footer { text-align: center; margin-top: 16px; font-size: 14px; color: var(--muted); }

.field { margin-bottom: 16px; }
.field label { display: block; font-size: 14px; font-weight: 600; margin-bottom: 6px; }
.field input[type="text"],
.field input[type="email"],
.field input[type="password"],
.field input[type="file"] {
  width: 100%;
  padding: 12px 14px;
  font-size: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
}
.field input:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 22px;
  font-size: 15px;
  font-weight: 600;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  cursor: pointer;
}
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn--primary { background: var(--primary); color: #fff; }
.btn--primary:hover:not(:disabled) { background: var(--primary-dark); }
.btn--block { width: 100%; padding: 14px; font-size: 16px; }
.btn--success { background: var(--success); color: #fff; }
.btn--success:hover:not(:disabled) { background: #146b3a; }
.btn--outline-danger { background: var(--surface); color: var(--danger); border-color: var(--danger); }
.btn--outline-danger:hover:not(:disabled) { background: var(--danger-tint); }

a.btn:hover { text-decoration: none; }

/* ---------- notices ---------- */
.notice {
  display: flex;
  gap: 14px;
  border-radius: var(--radius-md);
  padding: 18px;
}
.notice__icon {
  width: 22px; height: 22px; border-radius: 50%;
  border: 2.5px solid currentColor;
  flex-shrink: 0; margin-top: 1px;
}
.notice__title { font-weight: 700; margin-bottom: 4px; }
.notice--info { background: var(--primary-tint); color: var(--primary-dark); }
.notice--warning { background: var(--warning-tint); color: var(--warning); }
.notice--danger { background: var(--danger-tint); color: var(--danger); }
.notice--success { background: var(--success-tint); color: var(--success); }
.notice p { margin: 0; color: var(--text); font-size: 15px; }

.notice + .notice,
.notice + form,
.notice + .card { margin-top: 16px; }

/* ---------- upload dropzone ---------- */
.upload-drop {
  border: 2px dashed var(--border);
  border-radius: var(--radius-md);
  padding: 28px 20px;
  text-align: center;
  margin-bottom: 28px;
}
.upload-drop__title { font-size: 16px; font-weight: 600; margin-bottom: 6px; }
.upload-drop__hint { font-size: 14px; color: var(--muted); margin-bottom: 16px; }

/* Native file inputs cannot be restyled, so the real input is visually
   hidden and its <label> acts as the button. Kept focusable for keyboards. */
.upload-drop input[type="file"] {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}
.upload-drop input[type="file"]:focus-visible + label {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}
.upload-drop__file-label { max-width: 100%; overflow: hidden; text-overflow: ellipsis; }

/* ---------- uploads list ---------- */
.upload-list__title { font-size: 15px; font-weight: 700; margin-bottom: 12px; }
.upload-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 0;
  border-top: 1px solid var(--border);
  gap: 12px;
}
.upload-row__date { font-size: 15px; font-weight: 600; }
.upload-row__file { font-size: 13px; color: var(--muted); }
.upload-row__actions { display: flex; align-items: center; gap: 12px; flex-shrink: 0; }

/* ---------- status pills (class names are load-bearing — do not rename) ---------- */
.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 700;
  padding: 5px 12px;
  border-radius: 100px;
  white-space: nowrap;
}
.pill::before { content: ""; width: 8px; height: 8px; flex-shrink: 0; }
.pill.pending { color: var(--warning); background: var(--warning-tint); }
.pill.pending::before { border-radius: 50%; border: 2px solid currentColor; }
.pill.approved { color: var(--primary); background: var(--primary-tint); }
.pill.approved::before { border-radius: 50%; background: currentColor; }
.pill.processing { color: var(--processing); background: var(--processing-tint); }
.pill.processing::before { border-radius: 50%; background: currentColor; animation: pill-pulse 1.4s ease-in-out infinite; }
.pill.done { color: var(--success); background: var(--success-tint); }
.pill.done::before { border-radius: 50%; background: currentColor; }
.pill.failed { color: var(--danger); background: var(--danger-tint); }
.pill.failed::before { border-radius: 2px; background: currentColor; }
@keyframes pill-pulse { 0%, 100% { opacity: 1; } 50% { opacity: .35; } }

/* low-confidence badge (used inline on a field, not a table pill) */
.pill.low {
  color: var(--warning);
  background: var(--warning-tint);
}
.pill.low::before {
  width: 0; height: 0; border-radius: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-bottom: 8px solid currentColor;
}

@media (prefers-reduced-motion: reduce) {
  .pill.processing::before { animation: none; }
}

/* ---------- upload preloader overlay ---------- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(22, 35, 47, 0.94);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}
.overlay.on { display: flex; }
.preloader { display: flex; flex-direction: column; align-items: center; gap: 28px; color: #fff; text-align: center; }
.preloader__rings { position: relative; width: 140px; height: 140px; }
.ring {
  position: absolute;
  border-radius: 50%;
  border: 4px solid transparent;
}
.ring:nth-child(1) {
  inset: 0;
  border-top-color: var(--primary);
  border-right-color: var(--primary);
  animation: spin-cw 2.4s linear infinite;
}
.ring:nth-child(2) {
  inset: 14px;
  border-bottom-color: #7fb0ff;
  border-left-color: #7fb0ff;
  animation: spin-ccw 3.1s linear infinite;
}
.cross {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  animation: cross-pulse 1.8s ease-in-out infinite;
}
.cross__shape { position: relative; width: 36px; height: 36px; }
.cross__shape::before,
.cross__shape::after { content: ""; position: absolute; background: #fff; border-radius: 2px; }
.cross__shape::before { left: 14px; top: 0; width: 8px; height: 36px; }
.cross__shape::after { top: 14px; left: 0; width: 36px; height: 8px; }
.ecg { display: block; }
.ecg path {
  fill: none;
  stroke: #7fb0ff;
  stroke-width: 2.5;
  stroke-dasharray: 400;
  stroke-dashoffset: 400;
  animation: ecg-draw 2s linear infinite;
}
.preloader__title { font-size: 17px; font-weight: 600; margin-bottom: 6px; }
.preloader__sub { font-size: 14px; color: #b7c3d0; }

@keyframes spin-cw { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }
@keyframes spin-ccw { from { transform: rotate(0deg); } to { transform: rotate(-360deg); } }
@keyframes cross-pulse { 0%, 100% { transform: scale(1); opacity: 1; } 50% { transform: scale(1.08); opacity: .85; } }
@keyframes ecg-draw { from { stroke-dashoffset: 400; } to { stroke-dashoffset: 0; } }

@media (prefers-reduced-motion: reduce) {
  .ring, .cross, .ecg path { animation: none !important; }
}

/* ---------- prescription result ---------- */
.disclaimer {
  background: var(--primary-tint);
  border: 1px solid #c7dbfb;
  border-radius: var(--radius-md);
  padding: 16px 20px;
  font-size: 14px;
  color: var(--primary-dark);
  margin-bottom: 20px;
}

.rx-layout { display: flex; flex-direction: column; gap: 24px; }
@media (min-width: 860px) {
  .rx-layout { flex-direction: row; align-items: flex-start; }
  .rx-layout__side { flex: 0 0 300px; }
  .rx-layout__main { flex: 1; min-width: 0; }
}

.rx-scan { width: 100%; aspect-ratio: 3 / 4; object-fit: cover; border-radius: var(--radius-lg); border: 1px solid var(--border); display: block; }

.rx-meta { margin-top: 16px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-md); padding: 18px; }
.rx-meta__grid { display: grid; grid-template-columns: auto 1fr; gap: 8px 14px; font-size: 14px; }
.rx-meta__label { color: var(--muted); }
.rx-meta__value { font-weight: 600; }
.rx-meta__section-title { margin-top: 16px; font-size: 13px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }

.tag-list { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.tag { background: #eef2f6; padding: 6px 12px; border-radius: 100px; font-size: 13px; font-weight: 600; }

.unread-box { margin-top: 16px; background: var(--warning-tint); border: 1px solid #eed7ad; border-radius: var(--radius-md); padding: 16px 18px; }
.unread-box__title { font-size: 13px; font-weight: 700; color: var(--warning); margin-bottom: 6px; }
.unread-box p { margin: 0; font-size: 14px; }

.med-list { display: flex; flex-direction: column; gap: 16px; }
.med-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 20px; }
.med-card__head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 14px; }
.med-card__id { display: flex; gap: 14px; }
.med-thumb { width: 72px; height: 72px; flex-shrink: 0; border-radius: var(--radius-sm); object-fit: cover; background: var(--bg); border: 1px solid var(--border); }
.med-thumb--placeholder { display: flex; align-items: center; justify-content: center; font-size: 11px; color: var(--muted); text-align: center; }
.med-name { font-size: 18px; font-weight: 700; }
.med-sub { font-size: 14px; color: var(--muted); }

.timing-row { display: flex; gap: 10px; background: var(--bg); border-radius: var(--radius-md); padding: 14px; margin-bottom: 14px; }
.timing-item { flex: 1; text-align: center; }
.timing-icon { width: 34px; height: 34px; margin: 0 auto 6px; border-radius: 50%; }
.timing-icon--morning { background: #e0a000; }
.timing-icon--afternoon { background: linear-gradient(180deg, #e0a000 50%, #c7d0d9 50%); }
.timing-icon--evening { background: #5b6b7c; position: relative; overflow: hidden; }
.timing-icon--evening::after { content: ""; position: absolute; inset: 0; left: 40%; border-radius: 50%; background: var(--bg); }
.timing-label { font-size: 11px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.timing-count { font-size: 16px; font-weight: 700; }

/* Raw schedule fallback: shown instead of the morning/afternoon/evening grid
   when the prescription's wording could not be resolved into dose counts.
   Printing "0" for an unparsed schedule would be actively misleading. */
.timing-raw { background: var(--bg); border-radius: var(--radius-md); padding: 14px; margin-bottom: 14px; }
.timing-raw__label { font-size: 11px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; margin-bottom: 8px; }
.timing-raw .tag-list { margin-top: 0; }
.timing-raw .tag { background: var(--surface); border: 1px solid var(--border); }

.med-details { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: 14px; margin-bottom: 12px; }
.med-details__label { color: var(--muted); }
.med-instructions { background: var(--primary-tint); border-radius: var(--radius-sm); padding: 12px 14px; font-size: 14px; color: var(--primary-dark); }

/* ---------- admin ---------- */
.admin-list { border: 1px solid var(--border); border-radius: var(--radius-md); overflow: hidden; }
.admin-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-top: 1px solid var(--border);
  gap: 12px;
}
.admin-list .admin-row:first-child { border-top: none; }
.admin-row__name { font-weight: 600; }
.admin-row__meta { font-size: 13px; color: var(--muted); }
.admin-section-title { font-size: 15px; font-weight: 700; margin: 28px 0 12px; }
.admin-section-title:first-child { margin-top: 0; }

/* ---------- admin settings ---------- */
.settings-card { max-width: 560px; }
.key-field { margin-bottom: 22px; padding-bottom: 22px; border-bottom: 1px solid var(--border); }
.key-field:last-of-type { border-bottom: 0; }
.key-field label { display: block; font-size: 14px; font-weight: 600; margin-bottom: 6px; }
.key-field input[type="password"],
.key-field input[readonly] {
  width: 100%;
  padding: 12px 14px;
  font-size: 15px;
  font-family: ui-monospace, monospace;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  margin-bottom: 8px;
}
.key-field__current { font-size: 13px; color: var(--muted); margin: 0 0 8px; }
.key-field__hint { font-size: 13px; color: var(--muted); margin: 0 0 8px; }
.key-clear { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); font-weight: 400; }
.key-clear input { width: auto; margin: 0; }

/* ---------- prescription: failed parse ---------- */
.rx-failed {
  max-width: 480px;
  margin: 40px auto;
  text-align: center;
}
.rx-failed__icon {
  width: 56px; height: 56px;
  margin: 0 auto 20px;
  border-radius: 50%;
  border: 3px solid var(--danger);
  position: relative;
}
.rx-failed__icon::before,
.rx-failed__icon::after {
  content: "";
  position: absolute;
  top: 50%; left: 50%;
  width: 3px; height: 24px;
  background: var(--danger);
  border-radius: 2px;
}
.rx-failed__icon::before { transform: translate(-50%, -50%) rotate(45deg); }
.rx-failed__icon::after { transform: translate(-50%, -50%) rotate(-45deg); }
.rx-failed h2 { margin-bottom: 10px; }
.rx-failed p { color: var(--muted); font-size: 15px; margin: 0 0 20px; }
.rx-failed__reasons {
  text-align: left;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 18px 20px 18px 38px;
  margin-bottom: 24px;
  font-size: 14px;
}
.rx-failed__reasons li { margin-bottom: 6px; }
.rx-failed__reasons li:last-child { margin-bottom: 0; }
.rx-failed__scan {
  width: 120px;
  aspect-ratio: 3 / 4;
  object-fit: cover;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  margin: 0 auto 24px;
  display: block;
  opacity: .8;
}
.rx-failed__actions { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; }
.rx-failed__detail { font-size: 13px; color: var(--muted); margin-top: 16px; }

/* ---------- empty state ---------- */
.empty-block {
  max-width: 420px;
  margin: 40px auto;
  text-align: center;
}
.empty-block__icon {
  width: 48px; height: 48px;
  margin: 0 auto 16px;
  border-radius: 50%;
  border: 2.5px dashed var(--border);
}
.empty-block h3 { margin-bottom: 8px; }
.empty-block p { color: var(--muted); font-size: 14px; margin: 0; }

/* ---------- footer ---------- */
.foot {
  max-width: 1100px;
  margin: 0 auto;
  padding: 24px 20px 48px;
  color: var(--muted);
  font-size: 13px;
}

/* ---------- utility ---------- */
.empty-state { font-size: 14px; color: var(--muted); padding: 12px 0; }
