/* Attendance panel — mobile-first.
   Base rules describe the phone layout: a bottom tab bar, stacked cards and
   tables that render as labelled record cards. Everything from 768px up is an
   additive upgrade, and the 1024px tier is the real desktop design: a fixed
   sidebar, dense data tables and multi-column dashboards.

   Tokens are the validated data-viz palette (light + dark both explicitly
   selected, never an automatic flip). */
:root {
  color-scheme: light;
  --page:           #f9f9f7;
  --surface:        #fcfcfb;
  --surface-2:      #f1f1ee;
  --text:           #0b0b0b;
  --text-2:         #52514e;
  --muted:          #898781;
  --grid:           #e1e0d9;
  --axis:           #c3c2b7;
  --border:         rgba(11, 11, 11, 0.10);
  --series-1:       #2a78d6;
  --series-1-soft:  #cde2fb;
  --good:           #0ca30c;
  --warning:        #fab219;
  --serious:        #ec835a;
  --critical:       #d03b3b;
  --shadow:         0 1px 2px rgba(11,11,11,.06), 0 8px 24px rgba(11,11,11,.05);
  --radius:         12px;
  --font: system-ui, -apple-system, "Segoe UI", sans-serif;
  /* Height of the mobile tab bar, including the iOS home-indicator inset. */
  --tabbar-h: calc(58px + env(safe-area-inset-bottom, 0px));
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232322;
    --text: #ffffff; --text-2: #c3c2b7; --muted: #898781;
    --grid: #2c2c2a; --axis: #383835; --border: rgba(255,255,255,0.10);
    --series-1: #3987e5; --series-1-soft: #184f95;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.35);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232322;
  --text: #ffffff; --text-2: #c3c2b7; --muted: #898781;
  --grid: #2c2c2a; --axis: #383835; --border: rgba(255,255,255,0.10);
  --series-1: #3987e5; --series-1-soft: #184f95;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.35);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--page); color: var(--text);
  font: 14px/1.5 var(--font); -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}
a { color: var(--series-1); }
button, input, select { font: inherit; color: inherit; }
/* A standalone PWA has no browser chrome to pull on, so kill the rubber band. */
@media (display-mode: standalone) { body { overscroll-behavior: none; } }

/* ------------------------------------------------------------ login */
.login-wrap {
  min-height: 100vh; min-height: 100dvh; display: grid; place-items: center;
  padding: 20px calc(16px + env(safe-area-inset-left, 0px));
}
.login-card {
  width: 100%; max-width: 380px; background: var(--surface); padding: 24px;
  border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
}
.login-card h1 { margin: 0 0 4px; font-size: 20px; }
.login-card p.sub { margin: 0 0 24px; color: var(--muted); font-size: 13px; }
@media (min-width: 480px) { .login-card { padding: 32px; } }

/* Brand lockup — used by both the login card and the desktop sidebar. */
.brand { display: flex; align-items: center; gap: 10px; padding: 0 8px 18px; }
.brand-mark {
  width: 30px; height: 30px; border-radius: 8px; background: var(--series-1);
  display: grid; place-items: center; color: #fff; font-weight: 700; font-size: 13px;
  flex-shrink: 0;
}
.brand-name { font-weight: 650; font-size: 14px; line-height: 1.2; }
.brand-sub { font-size: 11px; color: var(--muted); }

.field { margin-bottom: 14px; }
.field label { display: block; font-size: 12px; color: var(--text-2); margin-bottom: 6px; }
.field input, .field select {
  width: 100%; padding: 11px; background: var(--page);
  border: 1px solid var(--border); border-radius: 8px;
  /* 16px keeps iOS Safari from zooming the viewport on focus. */
  font-size: 16px;
}
.field input:focus, .field select:focus {
  outline: 2px solid var(--series-1); outline-offset: -1px; border-color: transparent;
}
@media (min-width: 768px) {
  .field input, .field select { padding: 9px 11px; font-size: 14px; }
}
.alert {
  padding: 10px 12px; border-radius: 8px; font-size: 13px; margin-bottom: 16px;
  background: color-mix(in srgb, var(--critical) 12%, transparent);
  color: var(--critical); border: 1px solid color-mix(in srgb, var(--critical) 35%, transparent);
}

/* ------------------------------------------------------------ shell
   Phone: one column, nav lives in the bottom tab bar, content clears it. */
.shell { display: block; min-height: 100vh; min-height: 100dvh; }
.sidebar { display: none; }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 8px;
  padding: 10px calc(14px + env(safe-area-inset-left, 0px))
           10px calc(14px + env(safe-area-inset-right, 0px));
  border-bottom: 1px solid var(--border); background: var(--surface);
  position: sticky; top: 0; z-index: 10;
}
.topbar h1 { margin: 0; font-size: 16px; font-weight: 600; }
.topbar .grow { flex: 1; }
/* The clock and the desktop action buttons are noise on a phone. */
.topbar #clock, .topbar .desk-only { display: none; }

.content {
  padding: 14px calc(14px + env(safe-area-inset-left, 0px))
           calc(var(--tabbar-h) + 24px) calc(14px + env(safe-area-inset-right, 0px));
}

/* ------------------------------------------------------------ tab bar (mobile) */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: flex; background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  box-shadow: 0 -1px 3px rgba(0,0,0,.05);
}
.tabbar button {
  flex: 1 1 0; min-width: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 2px;
  background: none; border: 0; cursor: pointer; padding: 8px 2px 7px;
  color: var(--muted); font-size: 10px; line-height: 1.25;
  -webkit-tap-highlight-color: transparent;
}
.tabbar button .ico { font-size: 17px; line-height: 1; }
.tabbar button span:last-child {
  max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.tabbar button.active { color: var(--series-1); font-weight: 600; }
.tabbar button:active { background: var(--surface-2); }

/* Sheet used for the overflow items that do not fit the tab bar. */
.sheet-back {
  position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 60;
  display: flex; align-items: flex-end;
}
.sheet {
  width: 100%; background: var(--surface); border-radius: 16px 16px 0 0;
  padding: 8px 8px calc(8px + env(safe-area-inset-bottom, 0px));
  box-shadow: var(--shadow);
}
.sheet .grabber {
  width: 36px; height: 4px; border-radius: 2px; background: var(--grid);
  margin: 6px auto 10px;
}
.sheet button, .sheet a {
  display: flex; align-items: center; gap: 12px; width: 100%;
  padding: 14px 14px; border: 0; background: none; border-radius: 10px;
  color: var(--text); font-size: 15px; text-align: left; cursor: pointer;
  text-decoration: none;
}
.sheet button:active, .sheet a:active { background: var(--surface-2); }

/* ------------------------------------------------------------ atoms */
.btn {
  padding: 9px 13px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); cursor: pointer; font-size: 13px; white-space: nowrap;
  min-height: 40px; -webkit-tap-highlight-color: transparent;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--series-1); border-color: var(--series-1); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--critical); border-color: color-mix(in srgb, var(--critical) 40%, transparent); }
.btn.sm { padding: 6px 10px; font-size: 12px; min-height: 34px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.icon-only { padding: 8px 10px; }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
}
.card-head {
  padding: 13px 14px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
}
.card-head h2 { margin: 0; font-size: 14px; font-weight: 600; }
.card-head .grow { flex: 1; }
.card-body { padding: 14px; }
@media (min-width: 768px) {
  .card-head { padding: 14px 16px; }
  .card-body { padding: 16px; }
}

.badge {
  display: inline-flex; align-items: center; gap: 5px; padding: 2px 8px;
  border-radius: 999px; font-size: 11.5px; font-weight: 550;
  border: 1px solid transparent;
  /* Long device errors land in badges; on a phone they must wrap, not clip. */
  white-space: normal; text-align: left; max-width: 100%; overflow-wrap: anywhere;
}
@media (min-width: 768px) { .badge { white-space: nowrap; } }
.badge .dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.good { color: var(--good); background: color-mix(in srgb, var(--good) 12%, transparent); }
.badge.warn { color: var(--text); background: color-mix(in srgb, var(--warning) 22%, transparent); }
.badge.serious { color: var(--text); background: color-mix(in srgb, var(--serious) 24%, transparent); }
.badge.crit { color: var(--critical); background: color-mix(in srgb, var(--critical) 12%, transparent); }
.badge.neutral { color: var(--text-2); background: var(--surface-2); }
.badge.info { color: var(--series-1); background: color-mix(in srgb, var(--series-1) 12%, transparent); }

/* ------------------------------------------------------------ stat tiles
   Two up on the narrowest phone — one big number per row wastes the screen. */
.tiles { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.tile {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 13px; box-shadow: var(--shadow);
}
.tile .label { font-size: 11.5px; color: var(--muted); margin-bottom: 4px; }
.tile .value { font-size: 22px; font-weight: 620; line-height: 1.15; letter-spacing: -.01em; }
.tile .foot { font-size: 11px; color: var(--muted); margin-top: 4px; }
@media (min-width: 768px) {
  .tiles { grid-template-columns: repeat(auto-fit, minmax(168px, 1fr)); gap: 12px; }
  .tile { padding: 15px 16px; }
  .tile .value { font-size: 26px; }
  .tile .label { font-size: 12px; }
}

/* ------------------------------------------------------------ chart */
.chart-wrap { position: relative; }
.chart-wrap svg { display: block; width: 100%; height: auto; overflow: visible; }
.bar { fill: var(--series-1); transition: opacity .12s; }
.bar-hit { fill: transparent; cursor: pointer; }
.bar-hit:hover + .bar, .bar.hot { opacity: .78; }
.axis-label { fill: var(--muted); font-size: 11px; font-family: var(--font); }
.axis-value { fill: var(--text-2); font-size: 11px; font-family: var(--font);
  font-variant-numeric: tabular-nums; }
.gridline { stroke: var(--grid); stroke-width: 1; }
.baseline { stroke: var(--axis); stroke-width: 1; }
.tooltip {
  position: absolute; pointer-events: none; z-index: 20; opacity: 0; transform: translate(-50%, -100%);
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: var(--shadow); padding: 7px 10px; font-size: 12px; white-space: nowrap;
  transition: opacity .1s;
}
.tooltip.on { opacity: 1; }
.tooltip .tt-date { color: var(--muted); font-size: 11px; margin-bottom: 2px; }
.tooltip .tt-row { display: flex; align-items: center; gap: 6px; }
.tooltip .swatch { width: 8px; height: 8px; border-radius: 2px; background: var(--series-1); }
.tooltip b { font-variant-numeric: tabular-nums; }

/* ------------------------------------------------------------ tables
   Phone: each row becomes a card of label/value pairs. app.js stamps every
   cell with data-label from its column header, which is what ::before shows. */
.table-scroll { overflow-x: visible; }
table.data { width: 100%; border-collapse: collapse; font-size: 13px; }
table.data thead { display: none; }
table.data, table.data tbody, table.data tr, table.data td { display: block; width: 100%; }
table.data tr {
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface);
  margin-bottom: 10px; padding: 4px 0; overflow: hidden;
}
table.data td {
  display: flex; align-items: baseline; justify-content: space-between; gap: 14px;
  padding: 7px 13px; border: 0; white-space: normal; text-align: right;
  min-width: 0; overflow-wrap: anywhere;
}
table.data td::before {
  content: attr(data-label); flex: 0 0 auto; text-align: left;
  color: var(--muted); font-size: 11px; text-transform: uppercase;
  letter-spacing: .04em; font-weight: 550;
}
/* First cell reads as the card's heading. */
table.data td:first-child {
  font-weight: 600; font-size: 14.5px;
  border-bottom: 1px solid var(--grid); padding: 9px 13px; margin-bottom: 3px;
}
table.data td.num { font-variant-numeric: tabular-nums; }
table.data td:empty { display: none; }
/* Action buttons, and the shift inputs, sit on their own line below the label
   rather than being squeezed beside it. */
table.data td.actions,
table.data td.shift-cell { justify-content: flex-start; flex-wrap: wrap; gap: 8px; padding-top: 9px; }
table.data td.actions::before,
table.data td.shift-cell::before { flex-basis: 100%; margin-bottom: 4px; }
table.data td.shift-cell input { flex: 1 1 auto; min-width: 0; }
table.data .empty {
  display: block; text-align: center; color: var(--muted); padding: 30px 12px;
}
table.data .empty::before { content: none; }
table.data tr:has(.empty) { border-style: dashed; }

/* Two kinds of table stay tabular on a phone rather than becoming cards:
   one with no header row is a key/value list (the ADMS setup panel, read at the
   terminal), and `.compact` marks a short-celled table — the department list —
   where a card per row would be three lines of scrolling for two values. */
table.data:not(:has(thead)), table.data.compact { display: table; }
table.data:not(:has(thead)) tbody, table.data.compact tbody { display: table-row-group; }
table.data.compact thead { display: table-header-group; }
table.data.compact th {
  display: table-cell; text-align: left; font-weight: 550; font-size: 11px;
  text-transform: uppercase; letter-spacing: .04em; color: var(--muted);
  padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap;
}
table.data:not(:has(thead)) tr, table.data.compact tr {
  display: table-row; border: 0; border-radius: 0; margin: 0; padding: 0; background: none;
}
table.data:not(:has(thead)) td, table.data.compact td {
  display: table-cell; text-align: left; font-weight: 400; font-size: 13px;
  padding: 8px 10px; margin: 0; border-bottom: 1px solid var(--grid);
}
table.data:not(:has(thead)) td::before, table.data.compact td::before { content: none; }
table.data:not(:has(thead)) td:first-child { color: var(--text-2); width: 45%; }
table.data.compact td:first-child { font-weight: 500; font-size: 13px; }
table.data.compact td.actions { display: table-cell; text-align: right; }

.mono { font-variant-numeric: tabular-nums; }
.sub { color: var(--muted); font-size: 12px; }

@media (min-width: 768px) {
  /* Back to a real data table: denser, scannable, horizontally scrollable. */
  .table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  table.data { display: table; }
  table.data thead { display: table-header-group; }
  table.data tbody { display: table-row-group; }
  table.data tr {
    display: table-row; border: 0; border-radius: 0; margin: 0; padding: 0;
    background: none;
  }
  table.data th {
    display: table-cell; text-align: left; font-weight: 550; font-size: 11.5px;
    text-transform: uppercase; letter-spacing: .04em; color: var(--muted);
    padding: 9px 12px; border-bottom: 1px solid var(--border);
    white-space: nowrap; background: var(--surface);
  }
  table.data td {
    display: table-cell; padding: 9px 12px; border: 0;
    border-bottom: 1px solid var(--grid); white-space: nowrap;
    text-align: left; font-weight: 400; font-size: 13px; margin: 0;
  }
  table.data td::before { content: none; }
  table.data td:first-child { font-weight: 400; font-size: 13px; margin: 0; }
  table.data tbody tr:hover { background: var(--surface-2); }
  table.data td.actions { white-space: nowrap; }
  /* Desktop: back to one tight row of controls. */
  table.data td.shift-cell { display: table-cell; white-space: nowrap; }
  table.data td.shift-cell input { flex: none; }
  table.data .empty { display: table-cell; text-align: center; padding: 34px 12px; }
}

/* ------------------------------------------------------------ filters */
.filters {
  display: grid; grid-template-columns: 1fr 1fr; gap: 10px; align-items: end;
  padding: 13px 14px; border-bottom: 1px solid var(--border);
}
.filters .field { margin: 0; min-width: 0; }
.filters .field label { font-size: 11px; }
.filters input, .filters select { width: 100%; min-width: 0; }
/* Only the date pair pairs up; everything else gets the full width, so long
   option text ("All departments") is never clipped inside a half-width select. */
.filters .field, .filters .row { grid-column: 1 / -1; }
.filters .field:has(input[type="date"]) { grid-column: span 1; }
.filters .row .btn { flex: 1 1 auto; }
@media (min-width: 768px) {
  .filters { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; padding: 14px 16px; }
  .filters input, .filters select { padding: 7px 10px; min-width: 130px; width: auto; }
  .filters .field, .filters .row { grid-column: auto; }
  .filters .row .btn { flex: 0 0 auto; }
}

.pager {
  display: flex; align-items: center; gap: 8px; padding: 12px 14px;
  border-top: 1px solid var(--border); flex-wrap: wrap;
}
.pager .grow { flex: 1; }
.pager #rCount { flex-basis: 100%; }
@media (min-width: 768px) {
  .pager { flex-wrap: nowrap; padding: 12px 16px; gap: 10px; }
  .pager #rCount { flex-basis: auto; }
}

.grid-2 { display: grid; grid-template-columns: 1fr; gap: 12px; }
.stack { display: grid; gap: 12px; }
/* Grid items default to min-width:auto, so one unbreakable string — a raw
   fingerprint template in the push log, say — would widen every sibling card
   and push the whole page sideways. */
.stack > *, .grid-2 > * { min-width: 0; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
@media (min-width: 768px) { .stack, .grid-2 { gap: 14px; } }
@media (min-width: 1024px) { .grid-2 { grid-template-columns: 1.6fr 1fr; } }

.live-dot {
  width: 8px; height: 8px; border-radius: 50%; background: var(--critical);
  animation: pulse 1.6s ease-in-out infinite;
}
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .3; } }
@media (prefers-reduced-motion: reduce) {
  .live-dot { animation: none; }
  * { transition-duration: .01ms !important; }
}

/* ------------------------------------------------------------ modal
   Bottom sheet on a phone, centred dialog once there is room. */
.modal-back {
  position: fixed; inset: 0; background: rgba(0,0,0,.45); display: flex;
  align-items: flex-end; justify-content: center; z-index: 50;
}
.modal {
  background: var(--surface); border-radius: 16px 16px 0 0; border: 1px solid var(--border);
  width: 100%; box-shadow: var(--shadow); max-height: 92vh; max-height: 92dvh;
  overflow-y: auto; padding-bottom: env(safe-area-inset-bottom, 0px);
}
@media (min-width: 640px) {
  .modal-back { align-items: center; padding: 20px; }
  .modal { max-width: 460px; border-radius: var(--radius); max-height: 90vh; }
}

.toast {
  position: fixed; z-index: 100;
  left: 12px; right: 12px; bottom: calc(var(--tabbar-h) + 12px);
  background: var(--surface); border: 1px solid var(--border);
  border-left: 3px solid var(--series-1);
  border-radius: 8px; padding: 11px 14px; box-shadow: var(--shadow); font-size: 13px;
}
.toast.err { border-left-color: var(--critical); }
.toast.ok { border-left-color: var(--good); }
@media (min-width: 768px) {
  .toast { left: auto; right: 20px; bottom: 20px; max-width: 380px; }
}

/* Offline banner, driven by the online/offline events in app.js. */
.offline-bar {
  position: fixed; left: 0; right: 0; bottom: var(--tabbar-h); z-index: 40;
  background: var(--warning); color: #0b0b0b; text-align: center;
  padding: 7px 12px; font-size: 12.5px; font-weight: 550;
}
@media (min-width: 1024px) { .offline-bar { bottom: 0; } }

[hidden] { display: none !important; }

/* ------------------------------------------------------------ desktop
   From 1024px the sidebar returns and the tab bar retires. This is the
   density-first layout: fixed nav rail, full tables, wide dashboard. */
@media (min-width: 1024px) {
  .shell { display: grid; grid-template-columns: 232px 1fr; }
  .sidebar {
    display: flex; background: var(--surface); border-right: 1px solid var(--border);
    padding: 20px 14px; flex-direction: column; gap: 4px;
    position: sticky; top: 0; height: 100vh; overflow-y: auto;
  }
  .nav-item {
    display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 8px;
    background: none; border: 0; width: 100%; text-align: left; cursor: pointer;
    color: var(--text-2); font-size: 13.5px;
  }
  .nav-item:hover { background: var(--surface-2); color: var(--text); }
  .nav-item.active { background: var(--series-1); color: #fff; font-weight: 550; }
  .nav-item .ico { width: 16px; text-align: center; }
  .nav-spacer { flex: 1; }

  .tabbar { display: none; }
  .topbar { padding: 14px 24px; gap: 12px; }
  .topbar h1 { font-size: 17px; }
  .topbar #clock { display: inline; }
  .topbar .desk-only { display: inline-flex; }
  .topbar .mob-only { display: none; }
  .content { padding: 22px 24px 60px; }
  :root { --tabbar-h: 0px; }
}
