:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #1c2430;
  --muted: #5d6878;
  --border: #d9dee5;
  --accent: #1f6f8b;
  --accent-text: #ffffff;
  --danger: #b42318;
  --due-bg: #fff7e0;
  --period-bg: #eef1f5;
  --ok: #1b7a3d;
  --warn: #9a6700;

  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12161c;
    --surface: #1a2029;
    --text: #e4e8ee;
    --muted: #9aa5b4;
    --border: #2f3846;
    --accent: #5fb3d0;
    --accent-text: #0d1217;
    --danger: #f97066;
    --due-bg: #3a3120;
    --period-bg: #222a35;
    --ok: #4ac27a;
    --warn: #e0b04a;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

a { color: var(--accent); }
h1 { font-size: 1.5rem; margin: 0 0 .25rem; display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
h2 { font-size: 1.15rem; margin: 2rem 0 .75rem; }
h3 { font-size: 1rem; margin: 1rem 0 .5rem; }

.container { max-width: 1200px; margin: 0 auto; padding: 1.5rem 16px 3rem; }

.topbar {
  display: flex; align-items: center; gap: 1.5rem; flex-wrap: wrap;
  padding: .75rem 16px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.topbar .brand { font-weight: 700; color: var(--text); text-decoration: none; }
.topbar nav { display: flex; gap: 1rem; }
.topbar nav a { text-decoration: none; color: var(--muted); }
.topbar nav a.is-current { color: var(--text); font-weight: 600; }
.topbar .account { margin-left: auto; display: flex; align-items: center; gap: 1rem; color: var(--muted); }
.topbar .account form { display: inline; }

.page-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 1.5rem; }
.card.narrow { max-width: 520px; margin: 0 auto; }

.muted { color: var(--muted); }
.small, .hint { font-size: .85rem; }
.hint { color: var(--muted); margin: .25rem 0 0; }
.error { color: var(--danger); }
.nowrap { white-space: nowrap; }
.empty { color: var(--muted); padding: 2rem 0; text-align: center; }

.flash { padding: .75rem 1rem; border-radius: 6px; margin-bottom: 1rem; border: 1px solid; }
.flash ul { margin: 0; padding-left: 1.25rem; }
.flash--notice { border-color: var(--ok); color: var(--ok); }
.flash--alert { border-color: var(--danger); color: var(--danger); }
.callout { padding: .75rem 1rem; border-radius: 6px; margin-bottom: 1rem; background: var(--due-bg); }
.callout p { margin: 0 0 .5rem; }
.callout label { font-weight: 600; }

.stack { display: flex; flex-direction: column; gap: 1rem; }
.field { display: flex; flex-direction: column; gap: .3rem; }
.field label { font-weight: 600; font-size: .9rem; }
.field.checkbox { flex-direction: row; align-items: center; }
.field.checkbox label { font-weight: normal; }
.readonly { margin: 0; font-weight: 600; }

input[type=text], input[type=email], input[type=password], input[type=search], input[type=date], select, textarea {
  font: inherit; color: inherit; background: var(--surface);
  border: 1px solid var(--border); border-radius: 6px; padding: .45rem .6rem; max-width: 100%;
}
input:focus-visible, select:focus-visible, textarea:focus-visible, .button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.actions { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }

.button {
  display: inline-block; font: inherit; font-weight: 600; text-decoration: none; cursor: pointer;
  background: var(--accent); color: var(--accent-text); border: 1px solid var(--accent);
  border-radius: 6px; padding: .45rem .9rem;
}
.button--secondary { background: transparent; color: var(--accent); }
.button--danger { background: transparent; color: var(--danger); border-color: var(--danger); }
.button--small { padding: .25rem .6rem; font-size: .85rem; }
.link-button { background: none; border: 0; padding: 0; font: inherit; color: var(--accent); cursor: pointer; text-decoration: underline; }

.filters { display: flex; gap: .5rem; flex-wrap: wrap; margin-bottom: 1rem; }

.table-scroll { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: .55rem .75rem; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table thead th { font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
.table tbody tr:last-child td { border-bottom: 0; }
.table.compact th, .table.compact td { padding: .3rem .6rem; }
.table tr.is-due td { background: var(--due-bg); }
.table tr.is-inactive td { color: var(--muted); }
.table .period-row th { background: var(--period-bg); font-size: .85rem; }
.table.audit td { font-size: .9rem; vertical-align: top; }
.changes { margin: 0; padding-left: 1rem; }

.visit-form { display: flex; gap: .4rem; flex-wrap: wrap; align-items: center; }

.preview { border: 1px dashed var(--border); border-radius: 6px; padding: .75rem 1rem; }
.preview p { margin: 0 0 .5rem; }

.badge {
  display: inline-block; font-size: .75rem; font-weight: 600; padding: .1rem .5rem; border-radius: 999px;
  border: 1px solid currentColor; color: var(--muted); margin-left: .35rem; vertical-align: middle; white-space: nowrap;
}
.badge--attended, .badge--completed, .badge--on-treatment, .badge--follow-up { color: var(--ok); }
.badge--missed, .badge--discontinued, .badge--screen-failed { color: var(--danger); }
.badge--scheduled, .badge--screening { color: var(--accent); }
.badge--due { color: var(--warn); }
h1 .badge { margin-left: 0; }

.pager { display: flex; gap: 1rem; margin-top: 1rem; }

.schedule-layout { display: grid; grid-template-columns: minmax(280px, 340px) 1fr; gap: 1.5rem; align-items: start; }
@media (max-width: 800px) { .schedule-layout { grid-template-columns: 1fr; } }

.calendar { padding: 1rem; }
.calendar__header { display: flex; justify-content: space-between; align-items: center; margin-bottom: .5rem; }
.calendar__nav { text-decoration: none; font-size: 1.3rem; padding: 0 .5rem; }
.calendar__grid { width: 100%; border-collapse: separate; border-spacing: 0 2px; table-layout: fixed; }
.calendar__grid th { font-size: .7rem; color: var(--muted); font-weight: 600; padding: .25rem 0; }
.calendar__grid td { text-align: center; padding: 0; }
.calendar__grid td a { display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 2.6rem; text-decoration: none; color: var(--text); border-radius: 4px; }
.calendar__grid td a:hover { background: var(--period-bg); }
.calendar__grid tr.is-selected td { background: var(--due-bg); }
.calendar__grid tr.is-selected td:first-child { border-radius: 6px 0 0 6px; }
.calendar__grid tr.is-selected td:last-child { border-radius: 0 6px 6px 0; }
.calendar__grid td.is-other-month a { color: var(--muted); opacity: .6; }
.calendar__grid td.is-today .calendar__day { font-weight: 700; color: var(--accent); text-decoration: underline; }
.calendar__week a { font-size: .75rem; color: var(--muted) !important; }
.calendar__count { font-size: .7rem; font-weight: 700; line-height: 1; padding: .1rem .35rem; border-radius: 999px; background: var(--accent); color: var(--accent-text); }

.week-summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: .75rem; margin-bottom: 1rem; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: .75rem 1rem; display: flex; flex-direction: column; }
.stat__value { font-size: 1.75rem; font-weight: 700; line-height: 1.1; }
.stat__label { color: var(--muted); font-size: .85rem; }

.day-block h2 { font-size: 1rem; margin: 1.25rem 0 .5rem; display: flex; gap: .5rem; align-items: baseline; flex-wrap: wrap; }
.day-block.is-today h2 { color: var(--accent); }
.day-count { margin-left: auto; font-size: .85rem; color: var(--muted); font-weight: normal; }
