/* MNH Discharge Records - design system
   One accent (teal), zinc neutrals, one radius rule
   (controls 8px, surfaces 12px), system font stack so the app
   works fully offline. Light theme only (permanent, no dark mode). */

:root {
  --font-sans: ui-sans-serif, -apple-system, "Segoe UI Variable Text", "Segoe UI",
    Roboto, "Helvetica Neue", Arial, sans-serif;

  --bg: #f7f7f6;
  --surface: #fefefe;
  --surface-sunken: #f1f1f0;
  --border: #e0e0de;
  --border-strong: #c8c8c5;
  --text: #1c1c1f;
  --text-secondary: #55555c;
  --text-faint: #7d7d85;

  --accent: #0f766e;
  --accent-hover: #0d6259;
  --accent-active: #0b524b;
  --accent-soft: #e4f2f0;
  --accent-soft-border: #b9dcd8;
  --accent-text-on: #f4fbfa;

  --danger: #b3372e;
  --danger-soft: #f9e9e7;
  --danger-soft-border: #ecc5c1;

  --warn-text: #8a5a00;
  --warn-soft: #faf1dd;
  --warn-soft-border: #ebd8ab;

  --radius-control: 8px;
  --radius-surface: 12px;

  --shadow-surface: 0 1px 2px rgb(28 28 31 / 0.04);
  --shadow-pop: 0 4px 16px rgb(28 28 31 / 0.10);

  --focus-ring: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.5;
}

/* ---------- App bar ---------- */

.appbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  height: 60px;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 20px;
}

.appbar-brand {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.appbar-brand .clinic-name {
  font-size: 16px;
  font-weight: 650;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.appbar-brand .clinic-sub {
  font-size: 12px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.appbar-spacer { flex: 1; }

.appbar-nav { display: flex; gap: 2px; }

@media (max-width: 560px) {
  .appbar-brand .clinic-sub { display: none; }
}

/* ---------- Patient profile ---------- */

.profile-meta {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 12px 16px;
}

.profile-meta .pm-label {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-secondary);
}

.profile-meta .pm-value { font-weight: 500; }

/* ---------- Layout ---------- */

.page {
  max-width: 1240px;
  margin: 0 auto;
  padding: 24px 20px 80px;
}

.editor-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 20px;
  align-items: start;
}

@media (min-width: 1100px) {
  .editor-layout.has-panel {
    grid-template-columns: minmax(0, 1fr) 340px;
  }
  .match-panel-wrap {
    position: sticky;
    top: 76px;
  }
}

/* ---------- Surfaces / sections ---------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-surface);
  box-shadow: var(--shadow-surface);
}

.section {
  padding: 18px 20px 20px;
}

.section + .section { border-top: 1px solid var(--border); }

.section-title {
  margin: 0 0 14px;
  font-size: 15px;
  font-weight: 650;
}

.section-hint {
  font-size: 13px;
  color: var(--text-secondary);
  margin: -10px 0 14px;
}

/* ---------- Form fields ---------- */

.field-grid {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  gap: 12px 14px;
}

.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }

.col-2 { grid-column: span 2; }
.col-3 { grid-column: span 3; }
.col-4 { grid-column: span 4; }
.col-6 { grid-column: span 6; }
.col-8 { grid-column: span 8; }
.col-12 { grid-column: span 12; }

@media (max-width: 760px) {
  .col-2, .col-3, .col-4 { grid-column: span 6; }
  .col-6, .col-8 { grid-column: span 12; }
}

label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
}

input[type="text"], input[type="date"], input[type="datetime-local"],
input[type="search"], input[type="tel"], textarea, select {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-control);
  padding: 8px 10px;
  width: 100%;
  min-width: 0;
}

textarea { resize: vertical; min-height: 64px; }

input:focus-visible, textarea:focus-visible, select:focus-visible,
button:focus-visible, a:focus-visible, [tabindex]:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

input::placeholder, textarea::placeholder { color: var(--text-faint); }

.field-error {
  font-size: 12.5px;
  color: var(--danger);
}

.input-invalid { border-color: var(--danger) !important; }

.vitals-input { font-variant-numeric: tabular-nums; }

/* Examination signs: checkbox pills (ticked = Yes, unticked = No) */

.check-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 10px;
  margin-bottom: 4px;
}

.check-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-control);
  padding: 7px 12px;
  font-size: 14px;
  font-weight: 550;
  color: var(--text);
  cursor: pointer;
  user-select: none;
  transition: background-color 120ms ease, border-color 120ms ease;
}

.check-pill:hover { background: var(--surface-sunken); }

.check-pill:has(input:checked) {
  background: var(--accent-soft);
  border-color: var(--accent-soft-border);
  color: var(--accent);
}

.check-pill input {
  accent-color: var(--accent);
  width: 15px;
  height: 15px;
  margin: 0;
  cursor: pointer;
}

.check-pill:has(input:focus-visible) { box-shadow: var(--focus-ring); }
.check-pill input:focus-visible { outline: none; box-shadow: none; }

/* ---------- Buttons ---------- */

button {
  font: inherit;
  cursor: pointer;
  border-radius: var(--radius-control);
  border: 1px solid transparent;
  padding: 8px 14px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  white-space: nowrap;
  transition: background-color 120ms ease, border-color 120ms ease,
    transform 80ms ease;
}

button:active { transform: translateY(1px); }

button:disabled { opacity: 0.55; cursor: not-allowed; }

.btn-primary {
  background: var(--accent);
  color: var(--accent-text-on);
}
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn-primary:active:not(:disabled) { background: var(--accent-active); }

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border-color: var(--border-strong);
}
.btn-secondary:hover:not(:disabled) { background: var(--surface-sunken); }

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  padding: 6px 10px;
}
.btn-ghost:hover:not(:disabled) { background: var(--surface-sunken); color: var(--text); }

.btn-danger-ghost {
  background: transparent;
  color: var(--danger);
  padding: 6px 10px;
}
.btn-danger-ghost:hover:not(:disabled) { background: var(--danger-soft); }

.btn-icon {
  padding: 7px;
  line-height: 0;
}

button svg, a svg { flex: none; }

/* Icon spans populated by renderIcons() default to the body line-height
   (1.5), which pads the SVG asymmetrically and throws off vertical
   centering inside flex buttons. Zero it, matching what iconEl() already
   sets inline for icons built the other way. */
[data-icon] { line-height: 0; }

/* ---------- Status chips ---------- */

.chip {
  display: inline-flex;
  align-items: center;
  font-size: 12px;
  font-weight: 650;
  padding: 2px 9px;
  border-radius: 999px;
  border: 1px solid;
}

.chip-draft {
  color: var(--warn-text);
  background: var(--warn-soft);
  border-color: var(--warn-soft-border);
}

/* ---------- Record list ---------- */

.list-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.list-header h1 {
  font-size: 21px;
  margin: 0;
  font-weight: 700;
}

.search-box { position: relative; width: min(380px, 100%); }

.search-box input { padding-left: 34px; }

.search-box .search-icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-faint);
  line-height: 0;
  pointer-events: none;
}

.record-row {
  display: grid;
  grid-template-columns: minmax(0, 1.2fr) minmax(0, 2fr) auto auto;
  gap: 6px 16px;
  align-items: center;
  padding: 13px 20px;
  border: 0;
  border-top: 1px solid var(--border);
  background: transparent;
  width: 100%;
  text-align: left;
  border-radius: 0;
  font-weight: 500;
  transition: background-color 100ms ease;
}

.record-row:first-child { border-top: 0; }
.record-row:hover { background: var(--surface-sunken); }

.record-row .r-name { font-weight: 650; }
.record-row .r-meta { color: var(--text-secondary); font-size: 13px; }
.record-row .r-dx {
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.record-row .r-date {
  color: var(--text-faint);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

@media (max-width: 700px) {
  .record-row { grid-template-columns: minmax(0, 1fr) auto; }
  .record-row .r-dx { grid-column: 1 / -1; }
}

/* ---------- Treatment table ---------- */

.tx-table { display: flex; flex-direction: column; gap: 8px; }

.tx-row {
  display: grid;
  grid-template-columns: minmax(0, 2.2fr) minmax(0, 1fr) minmax(0, 0.9fr)
    minmax(0, 1.1fr) minmax(0, 1.1fr) minmax(0, 1.6fr) auto;
  gap: 8px;
  align-items: center;
}

.tx-head {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-faint);
  padding: 0 2px;
}

/* Route/frequency are text inputs with a `list` attribute (editable
   dropdowns via <datalist>: pick a preset or type your own). Chromium
   draws its own list-button indicator on top of these on :hover/:focus
   regardless of `appearance: none`, stacking with our custom chevron -
   every declaration below is needed to actually suppress it in every
   state, not just the default one. */
.tx-row input[list] {
  appearance: none;
  -webkit-appearance: none;
  padding-right: 26px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath fill='%237d7d85' d='M4 6l4 4 4-4z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  background-size: 10px 10px;
}

.tx-row input[list]::-webkit-calendar-picker-indicator,
.tx-row input[list]:hover::-webkit-calendar-picker-indicator,
.tx-row input[list]:focus::-webkit-calendar-picker-indicator,
.tx-row input[list]::-webkit-list-button,
.tx-row input[list]:hover::-webkit-list-button,
.tx-row input[list]:focus::-webkit-list-button {
  display: none !important;
  opacity: 0 !important;
  pointer-events: none !important;
  width: 0 !important;
  height: 0 !important;
}

@media (max-width: 900px) {
  .tx-row {
    grid-template-columns: repeat(2, minmax(0, 1fr)) auto;
    border: 1px solid var(--border);
    border-radius: var(--radius-control);
    padding: 10px;
  }
  .tx-row.tx-header-row { display: none; }
  .tx-row input:first-child { grid-column: 1 / -2; }
}

/* ---------- Patient picker ---------- */

.patient-picker { position: relative; }

.patient-suggestions {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  z-index: 15;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-control);
  box-shadow: var(--shadow-pop);
  max-height: 280px;
  overflow-y: auto;
}

.patient-suggestions button {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  border: 0;
  border-radius: 0;
  background: transparent;
  padding: 10px 12px;
  font-weight: 500;
}

.patient-suggestions button:hover { background: var(--surface-sunken); }

.patient-suggestions .ps-meta { color: var(--text-faint); font-size: 13px; }

.patient-locked {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--accent-soft);
  border: 1px solid var(--accent-soft-border);
  border-radius: var(--radius-control);
  padding: 10px 14px;
}

.patient-locked .pl-name { font-weight: 650; }
.patient-locked .pl-meta { color: var(--text-secondary); font-size: 13px; }

/* ---------- Match panel (similar cases) ---------- */

.match-panel { overflow: hidden; }

.match-panel .section { padding: 16px 18px; }

.match-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 0 0 4px;
  font-size: 15px;
  font-weight: 650;
}

.match-sub { font-size: 13px; color: var(--text-secondary); margin: 0 0 12px; }

.match-item {
  border: 1px solid var(--border);
  border-radius: var(--radius-control);
  padding: 12px;
  margin-bottom: 10px;
}

.match-item .m-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 10px;
  margin-bottom: 4px;
}

.match-item .m-name { font-weight: 650; font-size: 14px; }

.match-item .m-score {
  font-size: 12px;
  color: var(--accent);
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  flex: none;
}

.match-item .m-dx {
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 8px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.match-item .m-tx {
  font-size: 12.5px;
  color: var(--text-faint);
  margin-bottom: 10px;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.match-item .m-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.match-item .m-actions button { font-size: 13px; padding: 5px 10px; }

/* ---------- Editor toolbar ---------- */

.editor-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.editor-toolbar h1 {
  font-size: 20px;
  margin: 0;
  font-weight: 700;
  flex: 1;
  min-width: 200px;
}

.save-status {
  font-size: 13px;
  color: var(--text-secondary);
  min-width: 90px;
}

/* ---------- Empty / loading / toast ---------- */

.empty-state {
  text-align: center;
  padding: 56px 24px;
  color: var(--text-secondary);
}

.empty-state .e-title {
  font-size: 16px;
  font-weight: 650;
  color: var(--text);
  margin-bottom: 6px;
}

.skeleton-row {
  height: 52px;
  border-top: 1px solid var(--border);
  position: relative;
  overflow: hidden;
}

.skeleton-row::after {
  content: "";
  position: absolute;
  inset: 14px 20px;
  border-radius: 6px;
  background: linear-gradient(90deg, var(--surface-sunken) 25%,
    var(--border) 50%, var(--surface-sunken) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s ease infinite;
}

@keyframes shimmer {
  from { background-position: 200% 0; }
  to { background-position: -200% 0; }
}

@media (prefers-reduced-motion: reduce) {
  .skeleton-row::after { animation: none; }
  button { transition: none; }
}

.toast-host {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
}

.toast {
  background: var(--text);
  color: var(--bg);
  padding: 9px 16px;
  border-radius: var(--radius-control);
  font-size: 14px;
  font-weight: 550;
  box-shadow: var(--shadow-pop);
}

.toast.toast-error { background: var(--danger); color: #fdf6f5; }

/* ---------- Utility ---------- */

.muted { color: var(--text-secondary); }
.hidden { display: none !important; }

.divider-label {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text-faint);
  font-size: 13px;
  font-weight: 600;
  margin: 14px 0 10px;
}

.divider-label::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--border);
}

/* ---------- Login ---------- */

.login-overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgb(28 28 31 / 0.38);
  backdrop-filter: blur(3px);
  overflow-y: auto;
}

.login-card {
  margin: auto;            /* centred when it fits, scrollable when it does not */
  width: min(400px, 100%);
  padding: 28px 28px 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: var(--shadow-pop);
}

.login-brand { display: flex; flex-direction: column; margin-bottom: 4px; }
.login-brand .clinic-name { font-size: 15px; font-weight: 650; color: var(--accent); }
.login-brand .clinic-sub { font-size: 12.5px; color: var(--text-secondary); }

.login-card h1 { margin: 0; font-size: 22px; font-weight: 700; }

.login-resume {
  font-size: 13px;
  background: var(--warn-soft);
  border: 1px solid var(--warn-soft-border);
  color: var(--warn-text);
  border-radius: var(--radius-control);
  padding: 8px 10px;
  margin: 0;
}

.login-card input[type="password"] {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-control);
  padding: 8px 10px;
  width: 100%;
}

.login-submit { justify-content: center; margin-top: 6px; }

/* ---------- App bar: user menu ---------- */

.user-menu { display: flex; align-items: center; gap: 2px; }

.user-chip { gap: 8px; max-width: 260px; }
.user-chip #user-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text);
}

.chip-role {
  color: var(--text-secondary);
  background: var(--surface-sunken);
  border-color: var(--border);
  text-transform: capitalize;
}
.chip-role.is-admin {
  color: var(--accent);
  background: var(--accent-soft);
  border-color: var(--accent-soft-border);
}

@media (max-width: 760px) {
  .logout-label { display: none; }
  .user-chip .chip-role { display: none; }
  .user-chip { max-width: 140px; }
}

/* ---------- Lists: tools, deleted rows ---------- */

.list-tools { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.record-row.is-deleted .r-name,
.record-row.is-deleted .r-dx { color: var(--text-faint); text-decoration: line-through; }

.chip-deleted {
  color: var(--danger);
  background: var(--danger-soft);
  border-color: var(--danger-soft-border);
}

.chip-ok {
  color: var(--accent);
  background: var(--accent-soft);
  border-color: var(--accent-soft-border);
}

.chip-muted {
  color: var(--text-secondary);
  background: var(--surface-sunken);
  border-color: var(--border);
}

.record-row .r-actions { display: flex; gap: 6px; }
.record-row .r-actions button { font-size: 13px; padding: 4px 10px; }

/* ---------- Banners ---------- */

.banner {
  border-radius: var(--radius-control);
  padding: 10px 14px;
  margin-bottom: 14px;
  font-size: 14px;
  font-weight: 550;
}

.banner-warn {
  background: var(--warn-soft);
  border: 1px solid var(--warn-soft-border);
  color: var(--warn-text);
}

/* ---------- Tables (users, audit) ---------- */

.table-card { overflow: hidden; }
.table-scroll { overflow-x: auto; }

table.data {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

table.data th, table.data td {
  text-align: left;
  padding: 10px 14px;
  border-top: 1px solid var(--border);
  vertical-align: top;
  white-space: nowrap;
}

table.data th {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-faint);
  border-top: 0;
  background: var(--surface-sunken);
}

table.data td.wrap { white-space: normal; min-width: 220px; }
table.data td.mono {
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
  font-size: 12.5px;
  color: var(--text-secondary);
}
table.data td .row-actions { display: flex; gap: 4px; flex-wrap: wrap; }
table.data td .row-actions button { font-size: 13px; padding: 4px 9px; }

.table-footer { display: flex; justify-content: center; padding: 10px; }
.table-footer:empty { display: none; }

.audit-filters { align-items: end; }

/* ---------- Dialogs ---------- */

dialog.dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius-surface);
  padding: 0;
  width: min(440px, calc(100vw - 32px));
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-pop);
}

dialog.dialog::backdrop {
  background: rgb(28 28 31 / 0.38);
  backdrop-filter: blur(2px);
}

dialog.dialog form {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 22px 24px 20px;
}

dialog.dialog h2 { margin: 0; font-size: 18px; font-weight: 700; }

dialog.dialog input[type="password"] {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-control);
  padding: 8px 10px;
  width: 100%;
}

.field-hint { font-size: 12.5px; color: var(--text-faint); }

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 6px;
}

.btn-danger {
  background: var(--danger);
  color: #fdf6f5;
}
.btn-danger:hover:not(:disabled) { background: #9a2f27; }
