/* ---------- Design tokens ---------- */
:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --border: #e2e6ec;
  --border-strong: #c9d0da;
  --ink: #1a2130;
  --ink-soft: #4b5768;
  --muted: #7c8798;
  --accent: #2f6feb;
  --accent-ink: #ffffff;
  --accent-soft: #eaf1fe;
  --accent-strong: #1d54c2;
  --warn-bg: #fff4e0;
  --warn-ink: #8a5a00;
  --warn-border: #f3d9a3;
  --err-bg: #fdecec;
  --err-ink: #ae1f1f;
  --err-border: #f4bcbc;
  --ok-bg: #e7f7ee;
  --ok-ink: #187a43;
  --ok-border: #b9e6cc;
  --senior-bg: #fdead0;
  --senior-ink: #9a5b00;
  --ipc-bg: #fdf3c4;
  --ipc-ink: #8a6d00;
  --dim: #eef1f5;
  --head-badge-bg: #e9e6fb;
  --head-badge-ink: #5a45b8;
  --pt-badge-bg: #e3f2fb;
  --pt-badge-ink: #1f6f9c;

  --radius-sm: 6px;
  --radius: 10px;
  --radius-lg: 14px;
  --shadow-sm: 0 1px 2px rgba(20, 26, 40, 0.06);
  --shadow: 0 2px 10px rgba(20, 26, 40, 0.07);
  --shadow-lg: 0 8px 28px rgba(20, 26, 40, 0.12);
  --space-1: 4px; --space-2: 8px; --space-3: 12px; --space-4: 16px;
  --space-5: 20px; --space-6: 28px; --space-7: 40px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --transition: 140ms ease;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1420; --surface: #161d2b; --surface-2: #1b2333;
    --border: #2a3446; --border-strong: #3a4658;
    --ink: #e9edf5; --ink-soft: #b6c0d1; --muted: #8894a8;
    --accent: #5b8def; --accent-ink: #08101f; --accent-soft: #1c2a44; --accent-strong: #7fa5f3;
    --warn-bg: #3a2c0d; --warn-ink: #f0b93f; --warn-border: #5c451a;
    --err-bg: #3a1616; --err-ink: #ff8b8b; --err-border: #5c2323;
    --ok-bg: #12331f; --ok-ink: #63d996; --ok-border: #1e4d30;
    --senior-bg: #40300f; --senior-ink: #f2b94a; --ipc-bg: #3a3610; --ipc-ink: #e8d24f;
    --dim: #1b2333; --head-badge-bg: #2a2650; --head-badge-ink: #b6a8f5;
    --pt-badge-bg: #12293a; --pt-badge-ink: #7fc4ea;
  }
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }
html { color-scheme: light dark; }
body {
  margin: 0; font-family: var(--font); background: var(--bg); color: var(--ink);
  font-size: 14px; line-height: 1.5; -webkit-font-smoothing: antialiased;
}
h1, h2, h3, h4 { margin: 0 0 var(--space-3); font-weight: 650; letter-spacing: -0.01em; }
h2 { font-size: 19px; } h3 { font-size: 15px; color: var(--ink-soft); }
p { margin: 0 0 var(--space-2); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.small { font-size: 12px; color: var(--muted); }
hr { border: none; border-top: 1px solid var(--border); margin: var(--space-4) 0; }

/* ---------- Header / nav ---------- */
header.top {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 22px; background: var(--surface);
  border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 20;
  box-shadow: var(--shadow-sm);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 15px; }
.brand .mark {
  width: 26px; height: 26px; border-radius: 8px; background: var(--accent);
  color: var(--accent-ink); display: inline-flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 800;
}
header.top nav.primary { display: flex; align-items: center; gap: 2px; margin-inline-start: 22px; }
header.top nav.primary a {
  padding: 7px 12px; border-radius: var(--radius-sm); color: var(--ink-soft);
  font-size: 13.5px; font-weight: 550; transition: background var(--transition), color var(--transition);
}
header.top nav.primary a:hover { background: var(--dim); text-decoration: none; color: var(--ink); }
header.top nav.primary a.active { background: var(--accent-soft); color: var(--accent-strong); }
.header-left { display: flex; align-items: center; }
.header-right { display: flex; align-items: center; gap: 14px; }
.user-chip { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--ink-soft); }
.avatar {
  width: 26px; height: 26px; border-radius: 50%; background: var(--dim); color: var(--ink-soft);
  display: inline-flex; align-items: center; justify-content: center; font-size: 11px; font-weight: 700;
}
.bell-link { position: relative; font-size: 15px; color: var(--ink-soft); padding: 6px; border-radius: 50%; }
.bell-link:hover { background: var(--dim); }
.bell-badge {
  position: absolute; top: 1px; right: 1px; background: var(--err-ink); color: #fff;
  font-size: 10px; line-height: 1; padding: 2px 4px; border-radius: 8px; font-weight: 700;
}

.impersonation-banner {
  background: linear-gradient(90deg, #7a1fa2, #9b2fc9); color: #fff; text-align: center;
  padding: 8px; font-size: 13px; display: flex; align-items: center; justify-content: center; gap: 10px;
}

main { padding: 22px; max-width: 1440px; margin: 0 auto; }

/* ---------- Cards / surfaces ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg);
  padding: 18px 20px; margin-bottom: 18px; box-shadow: var(--shadow-sm);
}
.card-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
@media (max-width: 860px) { .grid-2 { grid-template-columns: 1fr; } main { padding: 14px; } }

/* ---------- Flash ---------- */
.flash {
  padding: 10px 14px; border-radius: var(--radius); margin-bottom: 10px; font-size: 13.5px;
  border: 1px solid transparent; display: flex; align-items: flex-start; gap: 8px;
}
.flash.ok { background: var(--ok-bg); color: var(--ok-ink); border-color: var(--ok-border); }
.flash.error { background: var(--err-bg); color: var(--err-ink); border-color: var(--err-border); }
.flash.warn { background: var(--warn-bg); color: var(--warn-ink); border-color: var(--warn-border); }

/* ---------- Buttons / forms ---------- */
input, select, textarea {
  font: inherit; padding: 8px 10px; border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--ink);
  transition: border-color var(--transition), box-shadow var(--transition);
}
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft);
}
label { font-size: 13px; color: var(--ink-soft); font-weight: 550; }
button, .btn {
  font: inherit; font-weight: 600; font-size: 13px; padding: 8px 14px; border-radius: var(--radius-sm);
  border: 1px solid transparent; background: var(--accent); color: var(--accent-ink); cursor: pointer;
  transition: background var(--transition), transform var(--transition), box-shadow var(--transition);
  display: inline-flex; align-items: center; gap: 6px;
}
button:hover, .btn:hover { background: var(--accent-strong); text-decoration: none; }
button:active { transform: translateY(1px); }
button.secondary, .btn.secondary {
  background: var(--surface); color: var(--ink); border-color: var(--border-strong);
}
button.secondary:hover { background: var(--dim); }
button.danger { background: var(--err-ink); }
button.danger:hover { background: #8f1919; }
button:disabled { opacity: .5; cursor: not-allowed; }
form.inline { display: inline; }

/* ---------- Tables ---------- */
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { border: 1px solid var(--border); padding: 7px 9px; text-align: center; }
th { background: var(--surface-2); font-weight: 650; color: var(--ink-soft); font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
tbody tr:hover { background: var(--surface-2); }
.table-scroll { overflow-x: auto; border-radius: var(--radius); }

/* ---------- Badges / pills ---------- */
.badge { display: inline-block; padding: 3px 9px; border-radius: 999px; font-size: 11px; font-weight: 700; letter-spacing: .02em; }
.badge.draft { background: var(--warn-bg); color: var(--warn-ink); }
.badge.published { background: var(--ok-bg); color: var(--ok-ink); }
.pill { font-size: 10.5px; padding: 2px 7px; border-radius: 999px; background: var(--dim); color: var(--ink-soft); font-weight: 650; }
.pill.head { background: var(--head-badge-bg); color: var(--head-badge-ink); }
.pill.pt { background: var(--pt-badge-bg); color: var(--pt-badge-ink); }

/* ---------- Schedule grid ---------- */
.week-block { margin-bottom: 18px; }
.week-header {
  display: flex; align-items: baseline; gap: 10px; font-weight: 700; font-size: 13.5px;
  margin-bottom: 8px; color: var(--ink-soft);
}
.sched-table { font-size: 12.5px; }
.sched-table th { position: sticky; top: 0; z-index: 2; }
.sched-table td.nurse-name { text-align: left; font-weight: 600; white-space: nowrap; background: var(--surface); position: sticky; left: 0; z-index: 1; }
.sched-table th.nurse-col { text-align: left; position: sticky; left: 0; z-index: 3; }
.sched-table td.headcount-row { background: var(--surface-2); color: var(--muted); font-weight: 600; }
.sched-table td.day-cell { min-width: 46px; cursor: pointer; position: relative; transition: background var(--transition); }
.sched-table td.day-cell:hover { background: var(--accent-soft); }
.sched-table td.day-cell.senior { background: var(--senior-bg); color: var(--senior-ink); font-weight: 700; }
.sched-table td.day-cell.ipc { background: var(--ipc-bg); color: var(--ipc-ink); font-weight: 700; }
.sched-table td.day-cell.out-of-month { color: var(--muted); background: var(--surface-2); opacity: .65; }
.sched-table td.day-cell.leave { font-style: italic; }
.sched-table .strike { text-decoration: line-through; color: var(--muted); font-size: 10px; display: block; }
.sched-table summary { list-style: none; cursor: pointer; padding: 2px; border-radius: 5px; }
.sched-table summary::-webkit-details-marker { display: none; }
.sched-table details[open] summary { background: var(--accent); color: var(--accent-ink); }
.sched-table details form {
  position: absolute; z-index: 30; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow-lg); padding: 12px; margin-top: 4px;
  text-align: left; min-width: 190px; font-size: 12.5px;
}
.sched-table details form label { display: block; margin: 6px 0 2px; }
.sched-table details form select { width: 100%; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; margin-top: 4px; }
.legend .swatch { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--ink-soft); }
.legend .dot { width: 11px; height: 11px; border-radius: 3px; display: inline-block; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.month-nav { display: flex; align-items: center; gap: 10px; font-size: 15px; }
.month-nav a { color: var(--ink-soft); font-weight: 600; }
.month-nav strong { font-size: 17px; }

/* ---------- Rank lists ---------- */
.rank-list { list-style: none; padding: 0; margin: 0; }
.rank-list li { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 8px 4px; border-bottom: 1px solid var(--border); font-size: 13.5px; }
.rank-list li:last-child { border-bottom: none; }

/* ---------- Auth pages ---------- */
.auth-wrap { min-height: calc(100vh - 0px); display: flex; align-items: center; justify-content: center; padding: 24px; }
.auth-card {
  width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-lg); padding: 32px 28px; box-shadow: var(--shadow-lg);
}
.auth-card .mark { width: 44px; height: 44px; border-radius: 12px; font-size: 19px; margin-bottom: 14px; }
.auth-card h2 { margin-bottom: 4px; }
.auth-card form p { margin-bottom: 14px; }
.auth-card input { width: 100%; margin-top: 4px; }
.auth-card button { width: 100%; justify-content: center; padding: 10px; margin-top: 6px; }

/* ---------- Misc ---------- */
.stat-row { display: flex; gap: 16px; flex-wrap: wrap; }
.stat { background: var(--surface-2); border-radius: var(--radius); padding: 12px 16px; min-width: 130px; }
.stat .num { font-size: 20px; font-weight: 750; }
.stat .label { font-size: 11.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.empty-state { text-align: center; padding: 30px 10px; color: var(--muted); }

/* ---------- Mobile menu (CSS-only disclosure) ---------- */
.nav-toggle { display: none; }
.nav-toggle > summary {
  list-style: none; cursor: pointer; padding: 8px 10px; border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong); background: var(--surface); font-size: 16px; line-height: 1;
}
.nav-toggle > summary::-webkit-details-marker { display: none; }

/* Agenda (mobile-only personal view of the schedule) */
.agenda { display: none; }
.agenda-day {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 11px 13px; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); margin-bottom: 7px; box-shadow: var(--shadow-sm);
}
.agenda-day.is-off { opacity: .58; }
.agenda-day.is-senior { border-inline-start: 4px solid var(--senior-ink); }
.agenda-day.is-ipc { border-inline-start: 4px solid var(--ipc-ink); }
.agenda-day .when { font-weight: 650; }
.agenda-day .when .small { font-weight: 400; display: block; }
.agenda-day .what { text-align: end; font-weight: 700; font-size: 15px; }
.agenda-day .what .small { font-weight: 500; display: block; }
.agenda-today { outline: 2px solid var(--accent); }

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  main { padding: 14px; }
  .card { padding: 15px 16px; border-radius: var(--radius); }
  .grid-2 { grid-template-columns: 1fr; gap: 14px; }
}

@media (max-width: 780px) {
  header.top { padding: 10px 14px; flex-wrap: wrap; gap: 10px; }
  .header-left { flex: 1 1 auto; justify-content: space-between; width: 100%; flex-wrap: wrap; row-gap: 8px; }
  .nav-toggle { display: block; }
  header.top nav.primary {
    display: none; margin: 0; width: 100%; flex-direction: column; align-items: stretch; gap: 2px;
  }
  .nav-toggle[open] + nav.primary { display: flex; }
  header.top nav.primary a { padding: 11px 12px; font-size: 15px; }
  .header-right { width: 100%; justify-content: space-between; gap: 8px; }
  .user-chip { font-size: 12.5px; }
  .impersonation-banner { flex-direction: column; gap: 6px; font-size: 12.5px; }

  /* touch targets */
  button, .btn { padding: 10px 14px; font-size: 14px; min-height: 42px; }
  input, select { padding: 10px; font-size: 16px; /* 16px avoids iOS zoom-on-focus */ min-height: 42px; }
  label { font-size: 13.5px; }
  .toolbar { width: 100%; }
  .toolbar > form, .toolbar > .btn, .toolbar > details { flex: 1 1 auto; }
  .toolbar button, .toolbar .btn { width: 100%; justify-content: center; }

  /* forms stack instead of squeezing */
  form[style*="flex"] { flex-direction: column; align-items: stretch !important; }
  form[style*="flex"] label { display: block; }
  form[style*="flex"] input, form[style*="flex"] select { width: 100%; }

  .card-header { flex-direction: column; align-items: flex-start; }
  .month-nav { width: 100%; justify-content: space-between; }

  /* schedule table: tighter, still scrollable, name column stays pinned */
  .sched-table { font-size: 12px; }
  .sched-table th, .sched-table td { padding: 5px 4px; }
  .sched-table td.day-cell { min-width: 42px; height: 42px; }
  .sched-table td.nurse-name, .sched-table th.nurse-col {
    max-width: 116px; overflow: hidden; text-overflow: ellipsis; font-size: 11.5px;
    box-shadow: 2px 0 4px rgba(0,0,0,.06);
  }
  .table-scroll { -webkit-overflow-scrolling: touch; }
  .week-header { font-size: 12.5px; }

  /* the cell editor becomes a bottom sheet so it can never be clipped off-screen */
  .sched-table details form {
    position: fixed; inset-inline: 0; bottom: 0; top: auto; margin: 0; width: 100%;
    min-width: 0; border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    border-bottom: none; padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
    box-shadow: 0 -8px 30px rgba(0,0,0,.28); z-index: 50; font-size: 14px;
  }
  .sched-table details form select { min-height: 44px; }
  .sched-table details form button { flex: 1 1 auto; }

  .stat-row { gap: 10px; }
  .stat { flex: 1 1 calc(50% - 10px); min-width: 0; }
  .rank-list li { padding: 11px 4px; }
  table { font-size: 12.5px; }
}

@media (max-width: 560px) {
  .auth-card { padding: 24px 20px; }
  .stat { flex: 1 1 100%; }
  .agenda { display: block; }        /* personal agenda replaces the grid for nurses */
  .agenda + .grid-view { display: none; }
  .user-chip span:last-child { max-width: 130px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
}
