:root {
  --bg: #f4f7fb;
  --surface: #ffffff;
  --border: #dce3ee;
  --text: #1a2433;
  --muted: #5a697d;
  --primary: #1d4ed8;
  --primary-hover: #1e40af;
  --success: #15803d;
  --warning-bg: #fff7ed;
  --warning-border: #fb923c;
  --danger: #b91c1c;
  --danger-bg: #fef2f2;
  --danger-border: #ef4444;
  --radius: 12px;
  --shadow: 0 2px 10px rgba(15, 23, 42, 0.06);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

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

.topbar {
  background: #0f172a;
  color: #fff;
  padding: 16px 0;
  margin-bottom: 20px;
}

.topbar-inner {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
}

.brand-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: 10px;
  background: var(--primary);
  font-size: 24px;
  font-weight: 700;
}

.brand h1 {
  margin: 0;
  font-size: 1.25rem;
}

.subtitle {
  margin: 2px 0 0;
  font-size: 0.82rem;
  color: #cbd5e1;
}

.health-badges {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.badge {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.76rem;
  font-weight: 600;
}
.badge-ok { background: #dcfce7; color: #166534; }
.badge-warn { background: #fef3c7; color: #92400e; }
.badge-danger { background: #fee2e2; color: #991b1b; }
.badge-neutral { background: #334155; color: #e2e8f0; }

/* Safety Notice */
.safety-banner {
  display: flex;
  gap: 14px;
  align-items: flex-start;
  background: #fef2f2;
  border: 2px solid #ef4444;
  border-radius: var(--radius);
  padding: 14px 18px;
  margin-bottom: 20px;
  color: #7f1d1d;
  font-size: 0.92rem;
}
.safety-icon { font-size: 1.5rem; line-height: 1.2; }

/* Cards & Tabs */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  margin-bottom: 20px;
}

.tabs-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 12px;
  margin-bottom: 18px;
}

.tabs {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.model-picker {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.84rem;
  font-weight: 600;
  color: var(--muted);
}
.model-picker select {
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #f8fafc;
  color: var(--text);
  font-size: 0.84rem;
  font-weight: 600;
  cursor: pointer;
}

.tab {
  border: 1px solid transparent;
  background: transparent;
  padding: 8px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  color: var(--muted);
  font-size: 0.9rem;
}
.tab-link {
  display: inline-flex;
  align-items: center;
  text-decoration: none;
}
.tab.active {
  background: #eff6ff;
  color: var(--primary);
  border-color: #bfdbfe;
}

.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* Drop Zone */
.drop-zone {
  border: 2px dashed #94a3b8;
  border-radius: var(--radius);
  padding: 36px 20px;
  text-align: center;
  background: #f8fafc;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.drop-zone.dragover {
  border-color: var(--primary);
  background: #eff6ff;
}
.drop-icon { font-size: 2.2rem; margin-bottom: 8px; }

.link-btn {
  border: none;
  background: none;
  color: var(--primary);
  font-weight: 600;
  cursor: pointer;
  text-decoration: underline;
  padding: 0;
  font-size: inherit;
}

.preview-wrap {
  margin-top: 16px;
  text-align: center;
}
.preview-wrap img, #cameraSnapshot, #cameraVideo {
  max-width: 100%;
  max-height: 360px;
  border-radius: 10px;
  border: 1px solid var(--border);
  display: block;
  margin: 0 auto 12px;
}

.camera-box { text-align: center; }
.camera-controls, .preview-actions, .btn-row {
  display: flex;
  gap: 10px;
  justify-content: flex-start;
  flex-wrap: wrap;
  margin-top: 12px;
}
.camera-controls, .preview-actions { justify-content: center; }

textarea {
  width: 100%;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.92rem;
  resize: vertical;
}

.field-label { display: block; font-weight: 600; margin-bottom: 8px; font-size: 0.9rem; }
.hint { color: var(--muted); font-size: 0.85rem; margin: 6px 0; }

.training-page-heading,
.training-batch-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}
.training-crop-editor { margin-top: 16px; }
.training-crop-canvas-wrap {
  display: grid;
  place-items: center;
  height: 65vh;
  max-height: 560px;
  min-height: 240px;
  margin-bottom: 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #e2e8f0;
}
#trainingCropCanvas {
  display: block;
  max-width: 100%;
  max-height: 60vh;
  width: auto;
  height: auto;
  cursor: crosshair;
  touch-action: none;
}
.training-crop-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-top: 10px;
}
.training-page-heading h2,
.card > h2 { margin-top: 0; }
.training-batch-progress {
  padding: 8px 14px;
  border-radius: 999px;
  background: #eff6ff;
  color: #1e3a8a;
  font-weight: 700;
}
.training-sample-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 14px;
  margin: 18px 0;
}
.training-sample-card {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #f8fafc;
}
.training-sample-placeholder {
  display: grid;
  min-height: 190px;
  place-items: center;
  border: 2px dashed #cbd5e1;
  border-radius: 10px;
  color: var(--muted);
  font-weight: 600;
  background: #f8fafc;
}
.training-sample-card img {
  width: 100%;
  height: 110px;
  object-fit: contain;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 6px;
}
.training-sample-text {
  flex: 1;
  margin: 0;
  font-weight: 600;
  overflow-wrap: anywhere;
}
.training-progress-heading {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
  font-weight: 600;
}
.training-progress-bar {
  display: block;
  width: 100%;
  height: 18px;
  appearance: none;
  border: 0;
  border-radius: 999px;
  overflow: hidden;
  background: #dbeafe;
}
.training-progress-bar::-webkit-progress-bar {
  border-radius: 999px;
  background: #dbeafe;
}
.training-progress-bar::-webkit-progress-value {
  border-radius: 999px;
  background: linear-gradient(90deg, #1d4ed8, #3b82f6);
  transition: width 0.3s ease;
}
.training-progress-bar::-moz-progress-bar {
  border-radius: 999px;
  background: linear-gradient(90deg, #1d4ed8, #3b82f6);
}
.training-batch-actions .status-msg {
  flex: 1;
  margin: 0;
}

/* Buttons */
.btn {
  padding: 9px 16px;
  border-radius: 8px;
  border: 1px solid transparent;
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: #e2e8f0; color: var(--text); }
.btn-warning { background: #ffedd5; color: #9a3412; border-color: #fdba74; }
.btn-success { background: var(--success); color: #fff; }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn-sm { padding: 5px 10px; font-size: 0.8rem; }

/* Loading Progress Bar & Errors */
.loading-bar {
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: #eff6ff;
  border: 1px solid #93c5fd;
  padding: 14px 18px;
  border-radius: var(--radius);
  margin-bottom: 16px;
  color: #1e3a8a;
  font-weight: 600;
}
.loading-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}
.loading-title {
  display: flex;
  align-items: center;
  gap: 10px;
}
.loading-percent {
  font-size: 0.92rem;
  color: var(--primary);
  min-width: 42px;
  text-align: right;
}
.progress-track {
  width: 100%;
  height: 10px;
  background: #dbeafe;
  border-radius: 999px;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  width: 0%;
  border-radius: 999px;
  background: linear-gradient(90deg, #1d4ed8, #3b82f6, #1d4ed8);
  background-size: 200% 100%;
  transition: width 0.35s ease;
  animation: shimmer 1.6s linear infinite;
}
@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}
.progress-steps {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
  font-size: 0.78rem;
  font-weight: 600;
}
.p-step {
  color: #64748b;
  padding: 2px 8px;
  border-radius: 999px;
  background: #e0f2fe;
}
.p-step.active {
  background: var(--primary);
  color: #ffffff;
}
.p-step.done {
  background: #dcfce7;
  color: #166534;
}
.spinner {
  width: 18px; height: 18px;
  border: 3px solid #bfdbfe;
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }

.error-box {
  background: var(--danger-bg);
  border: 1px solid var(--danger-border);
  color: var(--danger);
  padding: 12px 16px;
  border-radius: var(--radius);
  margin-bottom: 16px;
  font-weight: 500;
}

.status-msg {
  margin-top: 12px;
  padding: 10px 14px;
  border-radius: 8px;
  background: #dcfce7;
  color: #166534;
  font-weight: 600;
  font-size: 0.9rem;
}

/* Results Table */
.results-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  flex-wrap: wrap;
  gap: 14px;
  margin-bottom: 16px;
}
.results-header h2 { margin: 0; font-size: 1.2rem; }

.meta-pills {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}
.meta-pill {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  display: flex;
  flex-direction: column;
  font-size: 0.75rem;
  color: var(--muted);
}
.inline-meta-input {
  border: none;
  background: transparent;
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--text);
  outline: none;
  min-width: 130px;
}

.table-wrap { overflow-x: auto; }
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}
th, td {
  border: 1px solid var(--border);
  padding: 10px;
  vertical-align: top;
  text-align: left;
}
th {
  background: #f1f5f9;
  font-weight: 700;
  white-space: nowrap;
}

.cell-input {
  width: 100%;
  min-width: 110px;
  padding: 6px 8px;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  font-size: 0.86rem;
  font-family: inherit;
  background: #fff;
}
.cell-input:focus {
  outline: 2px solid #93c5fd;
  border-color: var(--primary);
}

/* Red highlight for low-confidence, unreadable, or flagged fields */
.cell-low-conf {
  background: var(--danger-bg) !important;
}
.cell-low-conf .cell-input {
  border-color: var(--danger-border);
  color: #991b1b;
  font-weight: 600;
  background: #fff5f5;
}

.conf-tag {
  display: inline-block;
  margin-top: 4px;
  font-size: 0.72rem;
  padding: 2px 6px;
  border-radius: 4px;
  font-weight: 600;
}
.conf-high { background: #dcfce7; color: #166534; }
.conf-low { background: #fee2e2; color: #991b1b; }

.sub-info {
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: 4px;
}

.candidate-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 6px;
}
.chip {
  font-size: 0.74rem;
  padding: 2px 7px;
  border-radius: 999px;
  border: 1px solid #93c5fd;
  background: #eff6ff;
  color: #1d4ed8;
  cursor: pointer;
}
.chip:hover { background: #dbeafe; }

.warning-list {
  margin: 6px 0 0;
  padding-left: 16px;
  color: #b91c1c;
  font-size: 0.78rem;
  font-weight: 600;
}

.save-bar {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-top: 16px;
  flex-wrap: wrap;
}
.notes-input {
  flex: 1;
  min-width: 220px;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.9rem;
}

.json-panel {
  margin-top: 16px;
  background: #0f172a;
  color: #e2e8f0;
  border-radius: 10px;
  padding: 14px;
}
.json-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}
.json-panel pre {
  margin: 0;
  overflow-x: auto;
  font-size: 0.82rem;
  max-height: 360px;
}

/* Readable Prescription Text Card */
.rx-text-card {
  background: #fffdf7;
  border: 1px solid #d6d3c4;
  border-left: 5px solid var(--primary);
  border-radius: 10px;
  padding: 14px 16px;
  margin-bottom: 18px;
}
.rx-text-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 10px;
}
.rx-text-tabs {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}
.rx-tab {
  border: 1px solid var(--border);
  background: #ffffff;
  color: var(--muted);
  padding: 5px 11px;
  border-radius: 7px;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
}
.rx-tab.active {
  background: #eff6ff;
  color: var(--primary);
  border-color: #93c5fd;
}
.rx-pad-preview {
  margin: 0;
  padding: 14px 16px;
  background: #ffffff;
  border: 1px dashed #cbd5e1;
  border-radius: 8px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.92rem;
  line-height: 1.65;
  color: #0f172a;
  white-space: pre-wrap;
  word-break: break-word;
}

/* Visual Bounding-Box Overlay */
.overlay-card {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  margin-bottom: 18px;
}
.overlay-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 12px;
}
.toggle-label {
  font-size: 0.84rem;
  font-weight: 600;
  color: var(--text);
  cursor: pointer;
}
.overlay-body {
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(240px, 1fr);
  gap: 14px;
  align-items: start;
}
@media (max-width: 860px) {
  .overlay-body { grid-template-columns: 1fr; }
}
.overlay-canvas-wrap {
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
  text-align: center;
  overflow: auto;
  max-height: 480px;
}
#overlayCanvas {
  max-width: 100%;
  height: auto;
  cursor: pointer;
  border-radius: 4px;
}
.region-inspector {
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
  font-size: 0.85rem;
}
.inspector-row {
  margin-bottom: 8px;
  padding-bottom: 8px;
  border-bottom: 1px solid #eef2f7;
}
.inspector-row:last-child { border-bottom: none; margin-bottom: 0; padding-bottom: 0; }
.inspector-label { font-size: 0.74rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.03em; }
.inspector-val { font-weight: 600; color: var(--text); word-break: break-word; }

/* Pre-OCR Rotate & Crop Canvas */
.prep-canvas-wrap {
  background: #0f172a;
  border-radius: 10px;
  padding: 10px;
  display: inline-block;
  max-width: 100%;
}
#prepCanvas {
  max-width: 100%;
  max-height: 420px;
  height: auto;
  cursor: crosshair;
  display: block;
  margin: 0 auto;
  border-radius: 4px;
}
.prep-toolbar {
  display: flex;
  justify-content: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 10px;
}
.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Clinical Safety & Drug Interaction Alerts */
.clinical-alerts {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 18px;
}
.alert-item {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 11px 14px;
  border-radius: 9px;
  font-size: 0.87rem;
  font-weight: 600;
}
.alert-high {
  background: #fef2f2;
  border: 1.5px solid #ef4444;
  color: #991b1b;
}
.alert-moderate {
  background: #fff7ed;
  border: 1.5px solid #fb923c;
  color: #9a3412;
}

/* Patient Daily Medication Schedule */
.schedule-card {
  margin-top: 18px;
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
}
.schedule-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
}
.schedule-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(195px, 1fr));
  gap: 10px;
}
.schedule-col {
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
}
.schedule-col h4 {
  margin: 0 0 8px;
  font-size: 0.86rem;
  color: var(--primary);
  border-bottom: 1px solid #eef2f7;
  padding-bottom: 6px;
}
.sched-item {
  font-size: 0.8rem;
  padding: 6px 0;
  border-bottom: 1px dashed #e2e8f0;
}
.sched-item:last-child { border-bottom: none; }
.sched-item strong { display: block; color: var(--text); }
.sched-item span { color: var(--muted); font-size: 0.75rem; }

/* Live Autocomplete Dropdown */
.autocomplete-menu {
  position: absolute;
  z-index: 1000;
  background: #ffffff;
  border: 1px solid #93c5fd;
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.14);
  max-height: 240px;
  overflow-y: auto;
  min-width: 260px;
}
.ac-item {
  padding: 8px 12px;
  cursor: pointer;
  border-bottom: 1px solid #f1f5f9;
  font-size: 0.83rem;
}
.ac-item:last-child { border-bottom: none; }
.ac-item:hover { background: #eff6ff; }
.ac-item strong { color: var(--primary); display: block; }
.ac-item small { color: var(--muted); }

/* History & Corrections Tab */
.history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}
.history-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}
@media (max-width: 820px) {
  .history-grid { grid-template-columns: 1fr; }
}
.sub-heading {
  margin: 0 0 8px;
  font-size: 0.92rem;
  color: var(--text);
}
.history-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 340px;
  overflow-y: auto;
}
.history-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  background: #f8fafc;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  font-size: 0.83rem;
}

/* Patient Safety Context Bar */
.patient-profile-bar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  margin-bottom: 16px;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--muted);
}
.patient-profile-bar .profile-title {
  color: var(--text);
  font-weight: 700;
}
.patient-profile-bar label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.patient-profile-bar input[type="number"],
.patient-profile-bar input[type="text"] {
  padding: 5px 8px;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  font-size: 0.82rem;
  background: #fff;
  color: var(--text);
}
.patient-profile-bar input[type="number"] { width: 76px; }
.patient-profile-bar .profile-wide input { min-width: 190px; }
.patient-profile-bar .profile-chk {
  cursor: pointer;
  color: var(--text);
}
.counseling-tip {
  margin-top: 5px;
  padding: 4px 7px;
  background: #eff6ff;
  border-left: 3px solid var(--primary);
  border-radius: 4px;
  font-size: 0.75rem;
  color: #1e3a8a;
}

/* Print / PDF Stylesheet */
@media print {
  body { background: #ffffff; }
  .no-print, .autocomplete-menu { display: none !important; }
  .card { box-shadow: none; border: 1px solid #cbd5e1; padding: 12px; }
  .cell-input { border: none; padding: 0; font-weight: 600; background: transparent !important; }
}

.hidden { display: none !important; }
