/* ---------- Application shell ---------- */
.app { display: grid; grid-template-columns: var(--rail-width) minmax(0, 1fr); min-height: 100vh; min-height: 100dvh; }
.main-col { min-width: 0; display: flex; flex-direction: column; }

.rail-col { background: var(--color-surface); border-right: 1px solid var(--color-divider); min-width: 0; }
.rail {
  position: sticky; top: 0; height: 100vh; height: 100dvh; overflow-y: auto;
  display: flex; flex-direction: column; gap: var(--space-1);
  padding: var(--space-4) var(--space-3) var(--space-3);
}
.brand { display: flex; align-items: center; gap: 10px; padding: 6px var(--space-2) var(--space-4); text-decoration: none; color: var(--color-text); }
.brand__mark { width: 30px; height: 30px; border-radius: 7px; background: var(--color-primary); color: #fff; display: grid; place-items: center;
  font-size: 13px; font-weight: var(--weight-bold); letter-spacing: 0.02em; box-shadow: inset 0 -3px 0 var(--color-accent); }
.brand__name { font-size: 16px; line-height: 20px; font-weight: var(--weight-bold); letter-spacing: -0.005em; }
.nav-group { margin-top: var(--space-3); }
.nav-group__label { padding: 0 var(--space-3) 6px; font-size: 12px; line-height: 16px; letter-spacing: 0.08em; text-transform: uppercase;
  font-weight: var(--weight-semibold); color: var(--color-text-secondary); }
.nav-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.nav-item {
  position: relative; display: flex; align-items: center; gap: 10px; min-height: 40px; padding: 8px var(--space-3);
  border-radius: var(--radius-control); color: var(--color-text); text-decoration: none; font-size: 15px; line-height: 20px; font-weight: var(--weight-medium);
}
.nav-item svg { width: 19px; height: 19px; color: var(--color-text-secondary); }
.nav-item:hover { background: var(--color-surface-sunken); color: var(--color-text); }
.nav-item[aria-current="page"] { background: var(--color-primary-soft); color: var(--color-primary); font-weight: var(--weight-semibold); }
.nav-item[aria-current="page"] svg { color: var(--color-primary); }
.nav-item[aria-current="page"]::before { content: ""; position: absolute; left: -12px; top: 8px; bottom: 8px; width: 3px; border-radius: 0 3px 3px 0; background: var(--color-primary); }
.nav-item__text { min-width: 0; }
.nav-item__sub { display: block; font-size: 12.5px; line-height: 16px; color: var(--color-text-secondary); font-weight: var(--weight-regular); }
.nav-item[aria-current="page"] .nav-item__sub { color: #3E5F87; }
.nav-item__badge { margin-left: auto; }
.rail__bottom { margin-top: auto; padding-top: var(--space-4); display: grid; gap: 2px; }
.account-button { display: flex; align-items: center; gap: 10px; width: 100%; min-height: 52px; padding: 8px var(--space-2); margin-top: var(--space-2);
  border: 1px solid var(--color-divider); border-radius: var(--radius-surface); background: var(--color-surface); text-align: left; }
.account-button:hover { background: var(--color-surface-muted); }
.account-button__name { display: block; font-size: 14px; line-height: 18px; font-weight: var(--weight-semibold); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.account-button__role { display: block; font-size: 12.5px; line-height: 16px; color: var(--color-text-secondary); }
.account-button > svg { width: 16px; height: 16px; margin-left: auto; color: var(--color-text-secondary); }
.account-button__text { min-width: 0; }

.topbar {
  position: sticky; top: 0; z-index: 20; height: var(--topbar-height);
  display: flex; align-items: center; gap: var(--space-3); padding: 0 var(--space-8);
  background: rgba(246, 248, 251, 0.94); backdrop-filter: saturate(1.2) blur(6px);
  border-bottom: 1px solid var(--color-divider);
}
.topbar__menu { display: none; }
.topbar__title { display: none; font-weight: var(--weight-semibold); min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.breadcrumb { display: flex; align-items: center; gap: 6px; font-size: 14px; line-height: 20px; color: var(--color-text-secondary); min-width: 0; flex-wrap: wrap; }
.breadcrumb a { display: inline-flex; align-items: center; min-height: 28px; color: var(--color-text-secondary); text-decoration: none; }
.breadcrumb a:hover { color: var(--color-primary); text-decoration: underline; }
.breadcrumb svg { width: 14px; height: 14px; }
.breadcrumb [aria-current="page"] { color: var(--color-text); font-weight: var(--weight-medium); }
.topbar__end { margin-left: auto; display: flex; align-items: center; gap: var(--space-2); }
.topbar__date { font-size: 13px; color: var(--color-text-secondary); white-space: nowrap; }
.pending-pill { display: inline-flex; align-items: center; gap: 6px; min-height: 36px; padding: 0 12px; border-radius: var(--radius-pill);
  border: 1px solid var(--color-warning-border); background: var(--color-warning-fill); color: var(--color-warning); font-size: 14px; font-weight: var(--weight-semibold); }
.pending-pill svg { width: 16px; height: 16px; }

.page { padding: var(--space-6) var(--space-8) var(--space-12); min-width: 0; }
.page--narrow { max-width: calc(var(--editor-width) + 2 * var(--space-8)); }
.page--center { margin-left: auto; margin-right: auto; }

.page-header { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--space-4) var(--space-6); flex-wrap: wrap; margin-bottom: var(--space-5); }
.page-header__text { min-width: 0; flex: 1 1 360px; }
.page-header__title { font-size: var(--text-title); line-height: var(--leading-title); font-weight: var(--weight-semibold); letter-spacing: -0.012em; overflow-wrap: break-word; }
.page-header__desc { margin-top: 4px; font-size: 15px; line-height: 22px; color: var(--color-text-secondary); max-width: 72ch; }
.page-header__actions { display: flex; flex-wrap: wrap; gap: var(--space-3); align-items: center; }
.page-header__meta { margin-top: var(--space-2); display: flex; flex-wrap: wrap; gap: var(--space-2) var(--space-3); align-items: center; }
.file-line { display: flex; align-items: center; gap: 6px; font-size: 14px; line-height: 20px; color: var(--color-text-secondary); min-width: 0; }
.file-line svg { width: 16px; height: 16px; }
.file-line strong { color: var(--color-text); font-weight: var(--weight-semibold); overflow-wrap: break-word; }
.back-link { display: inline-flex; align-items: center; gap: 6px; margin-bottom: var(--space-3); min-height: 32px; font-size: 14px; font-weight: var(--weight-semibold); text-decoration: none; }
.back-link svg { width: 16px; height: 16px; }

.stack > * + * { margin-top: var(--space-5); }
.stack-sm > * + * { margin-top: var(--space-3); }
.stack-lg > * + * { margin-top: var(--space-8); }

/* ---------- Search + list workspace ---------- */
.search-panel { display: grid; gap: var(--space-3); }
.search-panel__row { display: flex; gap: var(--space-3); align-items: flex-end; flex-wrap: wrap; }
.search-panel__row .field { flex: 1 1 320px; }
.filters { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: var(--space-3) var(--space-4); align-items: end; }
.filters__actions { display: flex; gap: var(--space-2); }
.list-meta { display: flex; justify-content: space-between; align-items: center; gap: var(--space-3); flex-wrap: wrap; margin-bottom: var(--space-3); font-size: 14px; color: var(--color-text-secondary); }

/* Selection bar (Cards: selected person → Replace card) */
.selection-bar { margin-top: 16px; position: sticky; bottom: var(--space-4); z-index: 5; display: flex; align-items: center; gap: var(--space-4); flex-wrap: wrap; padding: var(--space-3) var(--space-4) var(--space-3) var(--space-5);
  border: 1px solid var(--color-info-border); background: var(--color-surface); border-radius: var(--radius-surface); box-shadow: inset 4px 0 0 var(--color-primary); }
.selection-bar__text { flex: 1 1 280px; min-width: 0; }

/* ---------- Focused editor ---------- */
.editor { max-width: var(--editor-width); }
.editor-section { padding: var(--space-6); }
.editor-section + .editor-section { border-top: 1px solid var(--color-divider); }
.editor-section__head { display: flex; justify-content: space-between; align-items: flex-start; gap: var(--space-4); margin-bottom: var(--space-4); }
.editor-footer { position: sticky; bottom: 0; display: flex; gap: var(--space-3); justify-content: space-between; align-items: center; flex-wrap: wrap;
  padding: var(--space-4) var(--space-6); border-top: 1px solid var(--color-divider); background: rgba(255,255,255,.97); border-radius: 0 0 var(--radius-surface) var(--radius-surface); }
.editor-footer__note { font-size: 14px; color: var(--color-text-secondary); min-width: 0; flex: 1 1 240px; }

.row-editor { display: grid; gap: var(--space-3); }
.row-editor__row { display: grid; gap: var(--space-3); align-items: end; padding: var(--space-3) var(--space-4); border: 1px solid var(--color-divider); border-radius: var(--radius-control); background: var(--color-surface-muted); }
.row-editor__row.is-locked { background: var(--color-surface-sunken); }
.row-editor__head { display: flex; justify-content: space-between; align-items: center; gap: var(--space-3); flex-wrap: wrap; }

/* ---------- Detail pages ---------- */
.detail-layout { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: var(--space-6); align-items: start; }
.detail-layout__aside { position: sticky; top: calc(var(--topbar-height) + var(--space-5)); display: grid; gap: var(--space-4); }

/* Compact information band at the top of detail pages */
.info-band { display: grid; grid-template-columns: minmax(240px, 1fr) minmax(0, 2fr); gap: var(--space-5) var(--space-8); align-items: start; }
.info-band .kv { grid-template-columns: minmax(110px, max-content) minmax(0, 1fr); }
@media (max-width: 900px) { .info-band { grid-template-columns: 1fr; } }

/* ---------- Two-column task (caregivers) ---------- */
.task-columns { display: grid; grid-template-columns: minmax(0, 5fr) minmax(0, 7fr); gap: var(--space-6); align-items: start; }

/* ---------- Attendance ---------- */
.reason-fields { min-width: 200px; }
@media (max-width: 767px) { .reason-fields { min-width: 0; } }
.context-bar { display: grid; grid-template-columns: 220px minmax(0, 1fr); gap: var(--space-4) var(--space-6); align-items: end; padding: var(--space-4) var(--space-5); }
.child-state { display: grid; gap: 2px; font-size: 14px; line-height: 20px; }
.state-line { display: flex; gap: 6px; align-items: flex-start; }
.state-line > svg { margin-top: 3px; }
.state-line svg { width: 15px; height: 15px; color: var(--color-text-secondary); }
.freshness { font-size: 12.5px; line-height: 16px; color: var(--color-text-secondary); display: inline-flex; gap: 4px; align-items: center; }
.freshness svg { width: 13px; height: 13px; }
.mode-switch { display: inline-flex; border: 1px solid var(--color-border-control); border-radius: var(--radius-control); overflow: hidden; background: var(--color-surface); }
.mode-switch button { min-height: 44px; padding: 0 var(--space-4); white-space: nowrap; border: 0; background: transparent; font-size: 15px; font-weight: var(--weight-semibold); color: var(--color-text-secondary); }
.mode-switch button + button { border-left: 1px solid var(--color-border-control); }
.mode-switch button[aria-pressed="true"] { background: var(--color-primary); color: #fff; }

.lookup-row { display: grid; grid-template-columns: minmax(190px, 220px) repeat(3, minmax(0, 1fr)) auto; gap: var(--space-3) var(--space-4); align-items: end; }
@media (max-width: 1100px) { .lookup-row { grid-template-columns: repeat(2, minmax(0, 1fr)); } .lookup-row .filters__actions { grid-column: 1 / -1; } }
@media (max-width: 767px) { .lookup-row { grid-template-columns: 1fr; } }

/* ---------- Reports ---------- */
.metric-row { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 0; }
.metric { padding: var(--space-4) var(--space-5); border-left: 1px solid var(--color-divider); }
.metric:first-child { border-left: 0; }
.metric__label { font-size: 13px; line-height: 18px; color: var(--color-text-secondary); font-weight: var(--weight-semibold); }
.metric__value { margin-top: 4px; font-size: 28px; line-height: 34px; font-weight: var(--weight-semibold); font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }
.metric__note { margin-top: 2px; font-size: 13px; line-height: 18px; color: var(--color-text-secondary); }
.chart-surface { padding: var(--space-4) var(--space-5) var(--space-3); }
.chart-surface__head { display: flex; justify-content: space-between; align-items: flex-start; gap: var(--space-3); flex-wrap: wrap; margin-bottom: var(--space-2); }
.chart { width: 100%; height: 300px; }
[data-recent-family] { white-space: normal; overflow-wrap: anywhere; text-align: left; max-width: 100%; }
.chart--tall { height: 340px; }
.chart-grid { display: grid; grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); gap: var(--space-5); }

/* ---------- Sign-in ---------- */
.signin { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: var(--space-6) var(--space-4); }
.signin__card { width: min(440px, 100%); padding: var(--space-8); }
.signin__brand { display: flex; align-items: center; gap: 12px; margin-bottom: var(--space-6); }
.signin__title { font-size: 24px; line-height: 30px; font-weight: var(--weight-semibold); }
.signin__foot { margin-top: var(--space-6); font-size: 13px; line-height: 18px; color: var(--color-text-secondary); }

.account-menu { min-width: 0; position: fixed; z-index: 40; left: calc(var(--rail-width) + 8px); bottom: 12px; width: 300px; }
@media (max-width: 1199px) { .account-menu { left: auto; right: 12px; bottom: auto; top: calc(var(--topbar-height) + 4px); } }

/* ---------- Drawer (mobile navigation) ---------- */
.drawer { position: fixed; z-index: 51; inset: 0 auto 0 0; width: min(300px, 86vw); background: var(--color-surface); box-shadow: var(--shadow-overlay);
  display: flex; flex-direction: column; padding: var(--space-3); overflow-y: auto; }
.drawer__head { display: flex; align-items: center; justify-content: space-between; padding-bottom: var(--space-2); }

/* ---------- Responsive ---------- */
@media (max-width: 1279px) {
  .detail-layout { grid-template-columns: minmax(0, 1fr) 300px; }
  .page { padding-left: var(--space-6); padding-right: var(--space-6); }
  .topbar { padding: 0 var(--space-6); }
}
@media (max-width: 1100px) {
  .row-editor__row { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; }
  .detail-layout { grid-template-columns: 1fr; }
  .detail-layout__aside { position: static; }
  .chart-grid { grid-template-columns: 1fr; }
  .task-columns { grid-template-columns: 1fr; }
}
@media (max-width: 1199px) {
  .app { grid-template-columns: 1fr; }
  .app > .rail-col { display: none; }
  .topbar { padding: 0 var(--space-4) 0 var(--space-2); }
  .topbar__menu { display: inline-flex; }
  .topbar .breadcrumb { display: none; }
  .topbar__title { display: block; }
  .topbar__date { display: none; }
  .page { padding: var(--space-5) var(--space-5) var(--space-10); }
  .metric-row { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .metric:nth-child(3) { border-left: 0; }
  .metric:nth-child(n+3) { border-top: 1px solid var(--color-divider); }
  .context-bar { grid-template-columns: 1fr; }
}
@media (max-width: 767px) {
  .page { padding: var(--space-4) var(--space-4) var(--space-8); }
  .page-header { margin-bottom: var(--space-4); }
  .page-header__title { font-size: 24px; line-height: 30px; }
  .page-header__actions { width: 100%; }
  .page-header__actions .btn { flex: 1 1 auto; }
  .field-grid, .field-grid--3 { grid-template-columns: 1fr; }
  .field-grid .span-2 { grid-column: auto; }
  .kv, .kv--compact { grid-template-columns: 1fr; gap: 2px; }
  .kv dd + dt { margin-top: var(--space-2); }
  .consequences { grid-template-columns: 1fr; }
  .row-editor__row { grid-template-columns: 1fr !important; }
  .editor-section { padding: var(--space-4); }
  .editor-footer { padding: var(--space-3) var(--space-4); }
  .editor-footer .btn-row { width: 100%; }
  .editor-footer .btn-row .btn { flex: 1 1 auto; }
  .action-bar { position: sticky; bottom: 0; margin: var(--space-4) calc(-1 * var(--space-4)) 0; border-radius: 0; border-left: 0; border-right: 0;
    padding: var(--space-3) var(--space-4) calc(var(--space-3) + env(safe-area-inset-bottom)); }
  .action-bar .btn { flex: 1 1 auto; }
  .action-bar .btn-row { width: 100%; }
  .mode-switch { display: grid; grid-template-columns: auto auto 1fr; width: 100%; }
  .mode-switch button { padding: 4px 10px; font-size: 14px; white-space: normal; line-height: 18px; }
  .stepper { gap: 6px; flex-wrap: nowrap; }
  .stepper__label { display: none; }
  .stepper li[aria-current="step"] .stepper__label { display: inline; }
  .stepper__sep { width: 12px; }
  .toolbar__label { flex-basis: 100%; }
  .page-header__actions:has(.stepper) { width: auto; }
  .surface--pad { padding: var(--space-4); }
  .dialog { top: auto; bottom: 0; left: 0; transform: none; width: 100vw; max-height: 92dvh; border-radius: 14px 14px 0 0; }
  .dialog__footer { flex-direction: column-reverse; gap: var(--space-2); padding: var(--space-3) var(--space-5) calc(var(--space-4) + env(safe-area-inset-bottom)); border-top-color: var(--color-divider); }
  .dialog__footer .btn { width: 100%; }
  .sidepanel { width: 100vw; }
  .search-row { flex-direction: column; align-items: stretch; }
  .metric__value { font-size: 24px; line-height: 30px; }
  .chart { height: 260px; }
  .signin__card { padding: var(--space-6) var(--space-5); }
  .result { grid-template-columns: 22px minmax(0, 1fr); padding: var(--space-4); }
  .result__dismiss { display: none; }
  .selection-bar { bottom: 0; margin-left: calc(-1 * var(--space-4)); margin-right: calc(-1 * var(--space-4)); border-radius: 0; border-left: 0; border-right: 0;
    box-shadow: 0 -4px 16px rgba(23,36,59,.08); padding-bottom: calc(var(--space-3) + env(safe-area-inset-bottom)); }
  .search-panel__row .btn { flex: 1 1 auto; }
  .selection-bar .btn { flex: 1 1 auto; }
}
@media (max-width: 359px) {
  .table th { white-space: normal; }
  .table th, .table td { padding-left: var(--space-2); padding-right: var(--space-2); }
  .page { padding-left: var(--space-3); padding-right: var(--space-3); }
  .action-bar, .selection-bar { margin-left: calc(-1 * var(--space-3)); margin-right: calc(-1 * var(--space-3)); }
  .table--stack td { grid-template-columns: 92px minmax(0, 1fr); }
}

/* Touch: every interactive target reaches 44 px. Last in the cascade on purpose. */
@media (pointer: coarse) {
  .btn--compact, .chip, .back-link, .row-toggle { min-height: var(--control-height); }
  .row-toggle { min-width: var(--control-height); }
  .segmented { padding: 2px; }
  .segmented button { min-height: var(--control-height); }
  .btn--icon.btn--compact { width: var(--control-height); }
  .check, .nav-item, .pending-pill { min-height: var(--control-height); }
  .check { align-items: center; }
  .calendar__day { height: var(--control-height); }
  .calendar { width: 328px; }
}

.record-editor { display: grid; gap: 16px; padding: 16px; border: 1px solid var(--color-divider); border-radius: var(--radius-control); background: var(--color-surface-muted); }
.record-editor .field-grid { align-items: end; }
.page-header__actions > .csv-upload ~ .btn { align-self: flex-start; }

/* Native modal containers keep the shared visual system and browser focus trap. */
dialog.dialog { margin: 0; padding: 0; border: 1px solid var(--color-divider); }
dialog::backdrop { background: rgba(16,28,46,.42); }
.date-dialog { padding: 0; border: 1px solid var(--color-divider); border-radius: var(--radius-surface); max-width: calc(100vw - 24px); background: var(--color-surface); color: var(--color-text); box-shadow: var(--shadow-overlay); }
.date-dialog .calendar { width: min(380px, calc(100vw - 24px)); }
.date-dialog .calendar__head { flex-wrap: wrap; }
.nav-dialog { margin: 0; padding: 0; border: 0; max-width: none; max-height: none; width: min(300px,86vw); height: 100dvh; background: var(--color-surface); }
.nav-dialog .drawer { position: static; width: 100%; height: 100%; }
.page-header__title:focus { box-shadow: none; }
.btn[aria-busy="true"]::after { content: ""; width: 14px; height: 14px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: portal-spin .8s linear infinite; }
@keyframes portal-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .btn[aria-busy="true"]::after { animation: none; } }
.chart .modebar { display: flex; flex-wrap: wrap; justify-content: flex-end; width: max-content; max-width: 100%; gap: 2px; background: var(--color-surface); border: 1px solid var(--color-divider); border-radius: var(--radius-control); padding: 2px; }
.chart .modebar-group { display: inline-flex; align-items: center; padding: 0; float: none; }
.chart .modebar-group:empty { display: none; }
.chart .modebar-btn { display: inline-flex; align-items: center; justify-content: center; width: 28px; height: 28px; padding: 6px; border-radius: var(--radius-control); }
.chart .modebar-btn:hover, .chart .modebar-btn.active { background: var(--color-primary-soft); }
.chart .modebar-btn:focus-visible { box-shadow: var(--focus-ring); }
.plotly-notifier .notifier-note { background: var(--color-text); color: var(--color-text-inverse); border-radius: var(--radius-control); font-family: var(--font-sans); }
