:root {
  --bg: #fafafa;
  --fg: #222;
  --accent: #2b6cb0;
  --red: #e53e3e;
  --yellow: #d69e2e;
  --green: #38a169;
  --border: #d6d6d6;
  --card-bg: #fff;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
body { margin: 0; background: var(--bg); color: var(--fg); }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.75rem 1.5rem; background: var(--card-bg); border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 1.25rem; margin: 0; }
.topbar a { color: var(--accent); text-decoration: none; }
.grid {
  display: grid; gap: 1rem; padding: 1rem;
  grid-template-columns: 2fr 1fr;
  grid-template-areas: "form sidebar" "latest sidebar";
}
.card {
  background: var(--card-bg); border: 1px solid var(--border); border-radius: 6px;
  padding: 1rem;
}
.sidebar { grid-area: sidebar; }
#latest-card { grid-area: latest; }
/* `section.card:first-of-type` (not bare `.card:first-of-type`) so the rule
   doesn't also match the `<aside class="card sidebar">`, which would put the
   sidebar on top of the form. */
section.card:first-of-type { grid-area: form; }

.tabs { display: flex; border-bottom: 1px solid var(--border); margin-bottom: 1rem; }
.tab { background: none; border: none; padding: 0.5rem 1rem; cursor: pointer; }
.tab.active { border-bottom: 2px solid var(--accent); color: var(--accent); }
.tab-pane { display: none; }
.tab-pane.active { display: block; }

form label { display: block; margin: 0.75rem 0; }
fieldset { border: 1px solid var(--border); padding: 0.5rem 1rem; }
input[type=text], input[name=patient_id], select, textarea {
  width: 100%; padding: 0.4rem; border: 1px solid var(--border); border-radius: 4px;
  font-family: inherit; font-size: 0.95rem;
}
textarea { font-family: ui-monospace, "Consolas", monospace; }
.dropzone {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 1rem; border: 2px dashed var(--border); border-radius: 6px;
  background: #fbfbfb; cursor: pointer; min-height: 4rem;
}
.dropzone.dragover { background: #eef6ff; border-color: var(--accent); }
.dropzone ul { list-style: none; padding: 0; margin: 0.5rem 0 0; }
.actions { display: flex; gap: 0.5rem; align-items: center; margin-top: 1rem; }
button.primary {
  background: var(--accent); color: #fff; border: none; padding: 0.5rem 1.25rem;
  border-radius: 4px; cursor: pointer; font-size: 0.95rem;
}
button.primary[disabled] { opacity: 0.6; cursor: wait; }
.status { color: #555; font-size: 0.85rem; }

.meta { color: #555; font-size: 0.85rem; margin-bottom: 0.25rem; }
.premed-line { color: var(--yellow); margin-bottom: 0.5rem; font-weight: 600; }
.premed-line.red { color: var(--red); }
.premed-line.green { display: none; }
.caries-line { font-size: 0.85rem; margin-bottom: 0.5rem; color: #555; }
.caries-line:empty { display: none; }
.caries-chip {
  display: inline-block; padding: 0 0.4rem; margin-right: 0.4rem;
  font-size: 0.72rem; font-weight: 700; letter-spacing: 0.02em;
  color: #fff; border-radius: 3px; vertical-align: 1px;
}
/* Collapsible caries block: the level stays visible, the audit trail is one
   click away. The markdown itself keeps every line, so copy/download/print
   and the PDF export always carry the full evidence. */
.caries-collapse { margin: 0.5rem 0; }
.caries-collapse > summary {
  cursor: pointer; list-style: none; padding: 0.35rem 0.5rem;
  background: #f7f6f4; border: 1px solid var(--line, #e5e2dc); border-radius: 5px;
}
.caries-collapse > summary::-webkit-details-marker { display: none; }
.caries-collapse > summary::before { content: "▸ "; color: #888; }
.caries-collapse[open] > summary::before { content: "▾ "; }
.caries-collapse > summary:hover { background: #f0eeea; }
.caries-collapse > ul { margin-top: 0.4rem; }
/* Three-column caries balance: what raises risk, what lowers it, what is unknown. */
.caries-table { width: 100%; border-collapse: collapse; margin-top: 0.5rem; font-size: 0.82rem; }
.caries-table th {
  text-align: left; padding: 0.3rem 0.45rem; background: #f2f1ee;
  border: 1px solid var(--line, #e5e2dc); font-size: 0.76rem; letter-spacing: 0.02em;
}
.caries-table td {
  vertical-align: top; padding: 0.35rem 0.45rem; width: 33.33%;
  border: 1px solid var(--line, #e5e2dc);
}
.caries-table th:first-child { color: var(--red, #b3261e); }
.caries-table th:nth-child(2) { color: var(--green, #2e7d32); }
.cf { margin-bottom: 0.15rem; }
.cf-label { font-weight: 600; }
.cf-badge {
  display: inline-block; margin-right: 0.3rem; padding: 0 0.25rem;
  font-size: 0.63rem; font-weight: 700; letter-spacing: 0.03em;
  color: #fff; background: var(--red, #b3261e); border-radius: 2px; vertical-align: 1px;
}
.cf-ev { color: #555; margin-top: 0.1rem; }
.cf.unmet { opacity: 0.65; }
.cf.unmet .cf-ev { color: var(--red, #b3261e); }
.cf-empty { color: #bbb; }
.caries-foot { font-size: 0.78rem; color: #666; margin: 0.45rem 0 0; }

.caries-ref {
  margin-left: 0.5rem; font-size: 0.78rem; font-weight: 600;
  color: var(--accent, #1769aa); cursor: pointer; text-decoration: underline;
}

/* Reference popup */
.modal {
  position: fixed; inset: 0; z-index: 50; display: flex;
  align-items: center; justify-content: center; padding: 1.5rem;
  background: rgba(0, 0, 0, 0.45);
}
.modal[hidden] { display: none; }
.modal-card {
  position: relative; max-width: 40rem; max-height: 85vh; overflow-y: auto;
  background: #fff; border-radius: 8px; padding: 1.4rem 1.6rem;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.25);
}
.modal-card h3 { margin: 0 0 0.5rem; }
.modal-card h4 { margin: 1rem 0 0.3rem; font-size: 0.95rem; }
.modal-close {
  position: absolute; top: 0.5rem; right: 0.6rem;
  background: none; border: none; font-size: 1.4rem; line-height: 1; cursor: pointer; color: #666;
}
.ref-list { margin: 0.2rem 0 0 1.1rem; padding: 0; font-size: 0.9rem; line-height: 1.5; }
.ref-list li { margin: 0.25rem 0; }
.ref-links { display: flex; flex-wrap: wrap; gap: 0.5rem; margin: 1rem 0 0.5rem; }
.ref-btn {
  display: inline-block; padding: 0.4rem 0.7rem; font-size: 0.85rem;
  border: 1px solid var(--line, #e5e2dc); border-radius: 5px;
  background: #f7f6f4; color: inherit; text-decoration: none;
}
.ref-btn:hover { background: #ebe9e5; }

.caries-chip.low { background: var(--green); }
.caries-chip.moderate { background: var(--yellow); }
.caries-chip.high, .caries-chip.extreme { background: var(--red); }
.markdown h1, .markdown h2, .markdown h3 { margin-top: 1rem; }
.markdown { line-height: 1.5; }

#today-list { list-style: none; padding: 0; margin: 0; }
#today-list li {
  padding: 0.4rem 0.5rem; border-bottom: 1px solid #eee;
  display: flex; align-items: center; gap: 0.5rem; cursor: pointer;
}
.dot { width: 0.7rem; height: 0.7rem; border-radius: 50%; flex-shrink: 0; }
.dot.red { background: var(--red); }
.dot.yellow { background: var(--yellow); }
.dot.green { background: var(--green); }

#today-list .del-btn {
  margin-left: auto;
  background: none; border: none; cursor: pointer;
  color: #aaa; font-size: 1.1rem; line-height: 1;
  padding: 0.1rem 0.4rem; border-radius: 3px;
  opacity: 0;
  transition: opacity 0.15s, color 0.15s, background 0.15s;
}
#today-list li:hover .del-btn { opacity: 1; }
#today-list .del-btn:hover { color: var(--red); background: #fee; }

.hint { color: #666; font-size: 0.82rem; margin: 0.4rem 0 0.5rem; }
.hint code { background: #f0f0f0; padding: 0.05rem 0.3rem; border-radius: 3px; }

/* Working-date picker embedded in card headings */
.date-input {
  width: auto !important; display: inline-block;
  font-size: 0.95rem !important; font-weight: 600;
  padding: 0.1rem 0.3rem !important;
  border: 1px solid var(--border); border-radius: 4px;
  color: var(--accent); background: #fff;
  font-family: inherit;
}

/* Per-patient category rows in batch dropzone lists */
.file-row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 0.75rem; width: 100%; padding: 0.2rem 0;
}
.file-row span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cat-select {
  width: auto !important; min-width: 7.5rem;
  padding: 0.15rem 0.3rem !important; font-size: 0.85rem !important;
  border: 1px solid var(--border); border-radius: 4px; background: #fff;
  flex-shrink: 0;
}

/* Spinner for in-flight batch operations */
.spinner {
  display: inline-block;
  width: 0.85rem; height: 0.85rem;
  border: 2px solid #ddd;
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  vertical-align: middle;
  margin-right: 0.5rem;
}
@keyframes spin { to { transform: rotate(360deg); } }

#batch-progress { list-style: none; padding: 0; margin: 1rem 0 0; }
#batch-progress li {
  padding: 0.4rem 0.6rem; border-left: 3px solid transparent;
  background: #fafafa; margin-bottom: 0.25rem; border-radius: 3px;
  font-family: ui-monospace, "Consolas", monospace; font-size: 0.9rem;
}
#batch-progress li.batch-pending {
  background: #eef6ff; border-left-color: var(--accent); color: #2b6cb0;
}
#batch-progress li.batch-done {
  background: #f0fdf4; border-left-color: var(--green); color: #2f855a;
  font-weight: 600;
}

/* Disabled submit button gets a wait cursor and dimmer look */
button[disabled] { opacity: 0.7; cursor: wait; }

/* Markdown article styling */
.markdown { line-height: 1.55; }
.markdown h1 { font-size: 1.5rem; margin: 1.2rem 0 0.6rem; }
.markdown h2 { font-size: 1.25rem; margin: 1rem 0 0.5rem; padding-bottom: 0.2rem; border-bottom: 1px solid var(--border); }
.markdown h3 { font-size: 1.05rem; margin: 0.9rem 0 0.4rem; color: #444; }
.markdown h4 { font-size: 0.95rem; margin: 0.7rem 0 0.3rem; color: #555; }
.markdown hr { border: none; border-top: 1px solid var(--border); margin: 1.2rem 0; }
.markdown ul, .markdown ol { margin: 0.4rem 0 0.6rem 1.4rem; padding: 0; }
.markdown li { margin: 0.15rem 0; }
.markdown p  { margin: 0.4rem 0; }
.markdown code { background: #f3f3f3; padding: 0.05rem 0.3rem; border-radius: 3px; font-size: 0.9em; }
.markdown strong { color: #1a1a1a; }

/* Print: hide UI chrome; show only the focused content card */
@media print {
  body { background: #fff; }
  .topbar, .sidebar, .tabs, form, #banner, .actions,
  #latest-card .meta, #latest-premed.green {
    display: none !important;
  }
  /* A collapsed <details> must never hide evidence on paper. */
  .caries-collapse > *:not(summary) { display: block !important; }
  .caries-collapse > summary::before { content: "" !important; }
  .caries-ref, .modal { display: none !important; }
  .grid { display: block !important; padding: 0 !important; }
  .card { border: none !important; box-shadow: none !important; padding: 0 !important; margin: 0 !important; }
  /* Page-specific: only the latest-card on single page, only digest-card on meeting */
  body.print-summary #latest-card { display: block !important; }
  body.print-summary .card:not(#latest-card) { display: none !important; }
  body.print-meeting #digest-card { display: block !important; }
  body.print-meeting .card:not(#digest-card) { display: none !important; }
  /* Batch print: show only the assembled multi-patient area */
  body.print-batch #batch-print-area { display: block !important; }
  body.print-batch .card { display: none !important; }
  .page-break { page-break-before: always; }
  .markdown h2 { border: none; }
  a[href]::after { content: ""; }  /* don't append URLs */
}

.banner {
  position: fixed; bottom: 1rem; left: 50%; transform: translateX(-50%);
  background: var(--red); color: #fff; padding: 0.5rem 1.25rem; border-radius: 4px;
  z-index: 10;
}

/* Schedule-column badge in patient lists */
.col-badge {
  display: inline-block; padding: 0 0.35rem; margin-left: 0.25rem;
  font-size: 0.72rem; font-weight: 700; letter-spacing: 0.02em;
  color: #fff; background: var(--accent); border-radius: 3px;
  vertical-align: 1px;
}
.row-selects { display: inline-flex; gap: 0.4rem; flex-shrink: 0; }
.col-select {
  width: auto !important; min-width: 6.5rem;
  padding: 0.15rem 0.3rem !important; font-size: 0.85rem !important;
  border: 1px solid var(--border); border-radius: 4px; background: #fff;
  flex-shrink: 0;
}
