/* Prontuario: papel frio, tinta azul-escura, campos de formulario. */
:root {
  --ink: #1d2733;
  --ink-soft: #4b5a68;
  --ink-faint: #6f7d89;
  --paper: #ffffff;
  --wall: #e6ecee;
  --wall-deep: #d6dfe2;
  --rule: #b6c3c9;
  --rule-soft: #d3dce0;
  --teal: #1f5f74;
  --teal-deep: #164757;
  --teal-wash: #e1edf1;
  --red: #a52a22;
  --red-wash: #f8e5e3;
  --amber: #7a4f00;
  --amber-wash: #fff2cc;
  --green: #2d6a3a;
  --focus: #e0a100;
  --ui: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --nav-w: 208px;
  --plist-w: 248px;
  --top-h: 48px;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font: 15px/1.45 var(--ui);
  color: var(--ink);
  background: var(--wall);
}
a { color: var(--teal); }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 1px; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- botoes e campos ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  min-height: 34px; padding: 5px 12px;
  border: 1px solid var(--rule); border-radius: 4px;
  background: var(--paper); color: var(--ink);
  cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover { border-color: var(--ink-faint); background: #f6f9fa; }
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--teal); border-color: var(--teal); color: #fff; font-weight: 600; }
.btn-primary:hover { background: var(--teal-deep); border-color: var(--teal-deep); }
.btn-danger { color: var(--red); border-color: #d9a5a0; }
.btn-danger:hover { background: var(--red-wash); border-color: var(--red); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost:hover { background: var(--wall-deep); border-color: transparent; }
.btn-sm { min-height: 28px; padding: 2px 9px; font-size: 13.5px; }

.input, select.input, textarea.input {
  width: 100%; min-height: 34px; padding: 5px 9px;
  border: 1px solid var(--rule); border-radius: 3px; background: var(--paper);
}
.input:focus { border-color: var(--teal); outline: 2px solid var(--teal-wash); }
label.field { display: block; }
label.field > span { display: block; font-size: 13px; color: var(--ink-soft); margin-bottom: 3px; }

/* ---------- estrutura ---------- */
.topbar {
  position: sticky; top: 0; z-index: 30;
  height: var(--top-h);
  display: flex; align-items: center; gap: 10px;
  padding: 0 12px;
  background: var(--teal-deep); color: #fff;
}
.topbar .btn-ghost { color: #fff; }
.topbar .btn-ghost:hover { background: rgba(255,255,255,.12); }
.brand { font-weight: 650; letter-spacing: .01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand small { font-weight: 400; opacity: .75; margin-left: 6px; }
.topbar .spacer { flex: 1; }
.topbar .who { font-size: 13.5px; opacity: .85; }

.shell { display: flex; min-height: calc(100% - var(--top-h)); }
.nav {
  width: var(--nav-w); flex: 0 0 var(--nav-w);
  background: var(--wall-deep);
  border-right: 1px solid var(--rule);
  padding: 10px 8px;
}
.nav a {
  display: block; padding: 8px 10px; margin-bottom: 2px;
  border-radius: 3px; color: var(--ink); text-decoration: none;
}
.nav a:hover { background: rgba(255,255,255,.55); }
.nav a[aria-current="page"] { background: var(--paper); font-weight: 600; box-shadow: inset 3px 0 0 var(--teal); }
.nav .nav-sep { border-top: 1px solid var(--rule); margin: 10px 4px; }
body.nav-collapsed .nav { display: none; }

.main { flex: 1; min-width: 0; padding: 20px 24px 40px; }
.page-head { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px; margin-bottom: 14px; }
.page-head h1 { font-size: 22px; margin: 0; font-weight: 650; }
.page-head .count { color: var(--ink-faint); font-size: 14px; }
.page-head .spacer { flex: 1; }

.banner {
  padding: 9px 12px; margin-bottom: 14px; border-radius: 3px;
  border: 1px solid; font-size: 14px;
}
.banner-warn { background: var(--amber-wash); border-color: #e3c56a; color: var(--amber); }
.banner-info { background: var(--teal-wash); border-color: #a9c7d1; color: var(--teal-deep); }
.banner-danger { background: var(--red-wash); border-color: #dfaaa4; color: var(--red); }
.banner .btn { margin-left: 8px; }

.footnote { margin-top: 28px; font-size: 13px; color: var(--ink-faint); max-width: 70ch; }

/* ---------- lista de pacientes ---------- */
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 12px; align-items: end; }
.toolbar .search { flex: 1 1 280px; max-width: 460px; }
.toolbar .filter { flex: 0 1 220px; }

.table-wrap { background: var(--paper); border: 1px solid var(--rule); border-radius: 4px; overflow-x: auto; }
table.grid { width: 100%; border-collapse: collapse; font-size: 14.5px; }
.grid th {
  text-align: left; font-weight: 600; font-size: 13px; color: var(--ink-soft);
  padding: 9px 10px; border-bottom: 2px solid var(--rule); background: #f3f6f7;
  white-space: nowrap;
}
.grid td { padding: 4px 10px; border-bottom: 1px solid var(--rule-soft); height: 42px; vertical-align: middle; }
.grid tbody tr:last-child td { border-bottom: 0; }
.grid tbody tr.row-link { cursor: pointer; }
.grid tbody tr.row-link:hover td { background: #f2f7f9; }
.grid .c-leito { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; width: 80px; }
.grid .c-nome a { color: var(--ink); font-weight: 550; text-decoration: none; }
.grid .c-nome a:hover { text-decoration: underline; color: var(--teal); }
.grid .c-num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.grid .c-edit { width: 15%; min-width: 130px; }
.grid .c-actions { white-space: nowrap; text-align: right; }
.cell-input {
  width: 100%; padding: 4px 6px; border: 1px solid transparent; border-radius: 3px;
  background: transparent;
}
.cell-input:hover { border-color: var(--rule-soft); background: #fff; }
.cell-input:focus { border-color: var(--teal); background: #fff; outline: 2px solid var(--teal-wash); }
.cell-input.saved { border-color: #8fbf98; background: #eef7f0; transition: background .6s; }
.cell-input.error { border-color: var(--red); background: var(--red-wash); }
.empty { padding: 36px 16px; text-align: center; color: var(--ink-soft); }
.empty .btn { margin-top: 10px; }
.muted { color: var(--ink-faint); }

/* ---------- dialogos ---------- */
dialog {
  border: 1px solid var(--rule); border-radius: 5px; padding: 0;
  width: min(560px, calc(100vw - 32px));
  color: var(--ink); background: var(--paper);
  box-shadow: 0 18px 50px rgba(20, 40, 55, .28);
}
dialog::backdrop { background: rgba(29, 39, 51, .45); }
dialog form, .dlg-body { padding: 18px 20px; }
dialog h2 { margin: 0 0 14px; font-size: 18px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 14px; }
.form-grid .span2 { grid-column: span 2; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.form-error { color: var(--red); font-size: 14px; margin-top: 10px; }
dialog.wide { width: min(1100px, calc(100vw - 32px)); }

/* ---------- pagina do paciente ---------- */
.p-shell { display: flex; height: calc(100vh - var(--top-h)); }
.plist {
  width: var(--plist-w); flex: 0 0 var(--plist-w);
  background: #f3f6f7; border-right: 1px solid var(--rule);
  display: flex; flex-direction: column; min-height: 0;
}
.plist-head { padding: 10px 10px 8px; border-bottom: 1px solid var(--rule-soft); }
.plist-head .input { min-height: 30px; font-size: 14px; }
.plist ul { list-style: none; margin: 0; padding: 4px 0; overflow-y: auto; flex: 1; }
.plist li a {
  display: grid; grid-template-columns: 52px 1fr; gap: 6px; align-items: baseline;
  padding: 6px 12px; text-decoration: none; color: var(--ink);
  border-left: 3px solid transparent;
}
.plist li a:hover { background: var(--teal-wash); }
.plist li a[aria-current="page"] { background: var(--paper); border-left-color: var(--teal); font-weight: 650; }
.plist .pl-leito { font-weight: 700; font-variant-numeric: tabular-nums; color: var(--teal-deep); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.plist .pl-nome { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
body.plist-collapsed .plist { display: none; }

.p-main { flex: 1; min-width: 0; display: flex; flex-direction: column; overflow: hidden; }
.p-scroll { flex: 1; overflow-y: auto; padding: 16px 22px 18px; display: flex; flex-direction: column; min-height: 0; }

/* Cabecalho como etiqueta de prontuario */
.chart-head {
  display: grid;
  grid-template-columns: auto 1fr;
  background: var(--paper); border: 1px solid var(--rule); border-radius: 4px;
  margin-bottom: 12px;
}
.bed-tag {
  display: flex; flex-direction: column; justify-content: center; align-items: center;
  min-width: 104px; padding: 8px 14px;
  background: var(--teal); color: #fff; border-radius: 3px 0 0 3px;
}
.bed-tag .lbl { font-size: 12px; opacity: .8; }
.bed-tag .val { font-size: 30px; line-height: 1.05; font-weight: 750; font-variant-numeric: tabular-nums; letter-spacing: -.01em; max-width: 160px; overflow-wrap: anywhere; text-align: center; }
.chart-fields { display: grid; grid-template-columns: 2.4fr .7fr 1fr 1.3fr 1.3fr 1.3fr; }
.chart-fields .cf { padding: 6px 12px 7px; border-left: 1px solid var(--rule-soft); min-width: 0; }
.chart-fields .cf:first-child { border-left: 0; }
.cf .lbl { display: block; font-size: 12px; color: var(--ink-faint); }
.cf .val { display: block; font-size: 15.5px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cf.cf-nome .val { font-size: 18px; }
.cf .val.blank { color: var(--ink-faint); font-weight: 400; }

.ed-toolbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px;
  margin-bottom: 8px;
}
.ed-toolbar .spacer { flex: 1; }
.save-state { font-size: 13.5px; color: var(--ink-soft); display: inline-flex; align-items: center; gap: 6px; }
.save-state::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--green); }
.save-state[data-state="dirty"]::before { background: var(--focus); }
.save-state[data-state="saving"]::before { background: var(--teal); }
.save-state[data-state="error"] { color: var(--red); font-weight: 600; }
.save-state[data-state="error"]::before { background: var(--red); }

.editor-sheet {
  flex: 1; min-height: 360px; display: flex;
  background: var(--paper); border: 1px solid var(--rule); border-radius: 3px;
  box-shadow: 0 1px 0 var(--rule-soft);
}
.editor-sheet:focus-within { border-color: var(--teal); }
textarea.editor {
  flex: 1; width: 100%; resize: none; border: 0; outline: 0;
  padding: 18px 22px; background: transparent;
  font: 15px/1.6 var(--mono); color: var(--ink);
  white-space: pre-wrap; overflow-wrap: anywhere; tab-size: 2;
}
textarea.editor[readonly] { background: #f7f9fa; color: var(--ink-soft); }
.ed-hint { font-size: 12.5px; color: var(--ink-faint); margin-top: 6px; }

.notice {
  font-size: 13.5px; padding: 6px 10px; margin-bottom: 8px; border-radius: 3px;
  background: var(--amber-wash); color: var(--amber); border: 1px solid #e3c56a;
}
.notice .btn { margin-left: 6px; }

.compare { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.compare h3 { margin: 0 0 6px; font-size: 14px; color: var(--ink-soft); font-weight: 600; }
.compare pre {
  margin: 0; height: 56vh; overflow: auto; padding: 12px; border: 1px solid var(--rule);
  border-radius: 3px; background: #fbfcfc; font: 13.5px/1.5 var(--mono);
  white-space: pre-wrap; overflow-wrap: anywhere;
}

/* ---------- orientacoes ---------- */
.doc-sheet {
  background: var(--paper); border: 1px solid var(--rule); border-radius: 4px;
  padding: 22px 28px; max-width: 860px;
}
.doc-sheet pre {
  margin: 0; white-space: pre-wrap; overflow-wrap: anywhere;
  font: 15px/1.65 var(--ui);
}
.doc-sheet textarea.editor { min-height: 60vh; padding: 0; font: 15px/1.6 var(--mono); }
.doc-meta { font-size: 13px; color: var(--ink-faint); margin-top: 8px; }

/* ---------- configuracoes ---------- */
.settings { display: grid; gap: 16px; max-width: 820px; }
.panel { background: var(--paper); border: 1px solid var(--rule); border-radius: 4px; padding: 16px 18px; }
.panel h2 { font-size: 17px; margin: 0 0 4px; }
.panel p { margin: 0 0 12px; color: var(--ink-soft); font-size: 14px; }
.panel .row { display: flex; flex-wrap: wrap; gap: 10px; align-items: end; }
.panel .row > label { flex: 1 1 180px; }
.backup-list { font-size: 13.5px; margin: 10px 0 0; padding-left: 18px; color: var(--ink-soft); font-variant-numeric: tabular-nums; }

/* ---------- login ---------- */
.login-page { display: grid; place-items: center; min-height: 100%; padding: 16px; }
.login-card {
  width: min(380px, 100%); background: var(--paper);
  border: 1px solid var(--rule); border-top: 6px solid var(--teal); border-radius: 4px;
  padding: 22px 24px;
}
.login-card h1 { margin: 0 0 2px; font-size: 21px; }
.login-card p { margin: 0 0 18px; color: var(--ink-soft); font-size: 14px; }
.login-card label.field { margin-bottom: 12px; }
.login-card .btn-primary { width: 100%; justify-content: center; margin-top: 4px; min-height: 40px; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 9px 16px; border-radius: 4px;
  font-size: 14px; z-index: 100; box-shadow: 0 6px 20px rgba(0,0,0,.25);
  max-width: calc(100vw - 32px);
}
.toast.error { background: var(--red); }

/* ---------- impressao ---------- */
#print-area { display: none; }
@media print {
  body { background: #fff; }
  body.printing > *:not(#print-area) { display: none !important; }
  body.printing #print-area { display: block; }
  #print-area h1 { font: 700 15pt/1.3 var(--ui); margin: 0 0 4pt; }
  #print-area .meta { font: 10pt/1.4 var(--ui); margin-bottom: 10pt; border-bottom: 1px solid #000; padding-bottom: 6pt; white-space: pre-wrap; }
  #print-area pre { font: 10.5pt/1.5 var(--mono); white-space: pre-wrap; overflow-wrap: anywhere; margin: 0; }
}

/* ---------- responsivo ---------- */
@media (max-width: 1100px) {
  .chart-fields { grid-template-columns: repeat(3, 1fr); }
  .chart-fields .cf:nth-child(4) { border-left: 0; }
  .chart-fields .cf:nth-child(n+4) { border-top: 1px solid var(--rule-soft); }
}
@media (max-width: 760px) {
  :root { --nav-w: 220px; }
  .main { padding: 14px 16px 32px; }
  .nav { position: fixed; top: var(--top-h); bottom: 0; left: 0; z-index: 25; box-shadow: 4px 0 18px rgba(0,0,0,.18); }
  body:not(.nav-open) .nav { display: none; }
  body.nav-open .nav { display: block; }
  .topbar .who { display: none; }
  .brand small { display: none; }
  .p-shell { height: auto; min-height: calc(100vh - var(--top-h)); }
  .plist { position: fixed; top: var(--top-h); bottom: 0; left: 0; z-index: 24; box-shadow: 4px 0 18px rgba(0,0,0,.18); }
  body:not(.plist-open) .plist { display: none; }
  body.plist-open .plist { display: flex; }
  .p-main { overflow: visible; }
  .p-scroll { padding: 12px 16px 18px; overflow: visible; }
  .chart-head { grid-template-columns: 1fr; }
  .bed-tag { flex-direction: row; gap: 10px; justify-content: flex-start; border-radius: 3px 3px 0 0; padding: 6px 12px; }
  .bed-tag .val { font-size: 22px; }
  .chart-fields { grid-template-columns: 1fr 1fr; }
  .chart-fields .cf { border-left: 0; border-top: 1px solid var(--rule-soft); }
  .chart-fields .cf-nome { grid-column: span 2; border-top: 0; }
  .editor-sheet { min-height: 60vh; }
  textarea.editor { padding: 14px; font-size: 14px; }
  .form-grid { grid-template-columns: 1fr; }
  .form-grid .span2 { grid-column: auto; }
  .compare { grid-template-columns: 1fr; }
  .compare pre { height: 30vh; }
  .doc-sheet { padding: 16px; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
