:root {
  --bg: #f4f6f8;
  --card: #ffffff;
  --border: #dfe3e8;
  --text: #1f2937;
  --muted: #6b7280;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --danger: #dc2626;
  --good: #16a34a;
  --moderate: #d97706;
  --bad: #dc2626;
  font-family: -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  margin-left: 220px;
}
body.impersonating { margin-top: 40px; }

/* ----- Side navigation ----- */
.sidenav {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 220px;
  background: #111827;
  color: #fff;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  z-index: 100;
}
body.impersonating .sidenav { top: 40px; }
.sidenav-brand { font-weight: 700; padding: 16px 16px 8px; }
.sidenav-links { display: flex; flex-direction: column; gap: 2px; padding: 4px 10px; flex: 1; }
.sidenav-links a {
  color: #cbd5e1;
  text-decoration: none;
  padding: 10px 12px;
  border-radius: 6px;
  font-size: 13px;
}
.sidenav-links a:hover { background: #1f2937; }
.sidenav-links a.active { background: var(--primary); color: #fff; }

.nav-dropdown { display: flex; flex-direction: column; }
.nav-dropdown-trigger {
  color: #cbd5e1;
  background: transparent;
  border: none;
  text-decoration: none;
  padding: 10px 12px;
  border-radius: 6px;
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  text-align: left;
}
.nav-dropdown-trigger:hover { background: #1f2937; }
.nav-dropdown-trigger.active { background: var(--primary); color: #fff; }
.nav-dropdown-menu {
  display: none;
  flex-direction: column;
  padding: 0 0 0 14px;
}
.nav-dropdown-menu.open { display: flex; }
.nav-dropdown-menu a {
  color: #cbd5e1;
  text-decoration: none;
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 13px;
  white-space: nowrap;
}
.nav-dropdown-menu a:hover { background: #1f2937; }
.nav-dropdown-menu a.active { background: var(--primary); color: #fff; }

.sidenav-user {
  font-size: 13px;
  color: #cbd5e1;
  padding: 12px 16px 16px;
  border-top: 1px solid #1f2937;
}
.sidenav-user button { background: transparent; border: 1px solid #374151; color: #cbd5e1; border-radius: 6px; padding: 4px 8px; cursor: pointer; }

.impersonation-banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  background: var(--moderate);
  color: #fff;
  padding: 0 16px;
  font-size: 13px;
  text-align: center;
  z-index: 200;
}
.impersonation-banner button {
  background: rgba(255, 255, 255, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.6);
  color: #fff;
  border-radius: 6px;
  padding: 4px 10px;
  cursor: pointer;
  font-weight: 600;
}
.impersonation-banner button:hover { background: rgba(255, 255, 255, 0.3); }

.nav-toggle {
  display: none;
  position: fixed;
  top: 10px;
  left: 10px;
  background: #111827;
  border: 1px solid #374151;
  color: #cbd5e1;
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  z-index: 150;
}
body.impersonating .nav-toggle { top: 50px; }

.account-panel {
  position: absolute;
  bottom: 100%;
  left: 0;
  margin-bottom: 8px;
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px;
  /* Matches the sidebar's own content width exactly, rather than a fixed
     px value — the sidenav scrolls vertically, which (per the overflow
     spec) forces it to also clip horizontally, so anything wider than its
     content box would get cut off instead of overflowing visibly. */
  width: 100%;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
  z-index: 50;
  text-align: left;
}
.account-panel label { margin-top: 6px; }

.page { max-width: 1100px; margin: 24px auto; padding: 0 16px; }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 20px;
}

h1 { font-size: 20px; margin: 0 0 4px; }
h2 { font-size: 16px; margin: 0 0 12px; }
.subtitle { color: var(--muted); margin: 0 0 20px; }

table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-weight: 600; background: #fafbfc; }
tr.locked-row { background: #f8fafc; }
tr.open-row { background: #fffbeb; }

input, select, textarea {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 13px;
  font-family: inherit;
  width: 100%;
}
label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 3px; margin-top: 10px; }

button {
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 8px 14px;
  font-size: 13px;
  cursor: pointer;
}
button:hover { background: var(--primary-dark); }
button.secondary { background: #fff; color: var(--text); border: 1px solid var(--border); }
button.secondary:hover { background: #f3f4f6; }
button.danger { background: var(--danger); }
button:disabled { opacity: 0.5; cursor: not-allowed; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 600; }
.badge.locked { background: #e5e7eb; color: #374151; }
.badge.open { background: #fef3c7; color: #92400e; }
.badge.approved { background: #dcfce7; color: #166534; }
.badge.good { background: #dcfce7; color: #166534; }
.badge.moderate { background: #fef3c7; color: #92400e; }
.badge.bad { background: #fee2e2; color: #991b1b; }

.error { color: var(--danger); font-size: 13px; margin-top: 8px; }
.success { color: var(--good); font-size: 13px; margin-top: 8px; }
.hint { color: var(--muted); font-size: 12px; }

.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 14px; }
.stat { background: #fafbfc; border: 1px solid var(--border); border-radius: 8px; padding: 12px; }
.stat .value { font-size: 22px; font-weight: 700; }
.stat .label { color: var(--muted); font-size: 12px; }

.toolbar { display: flex; align-items: center; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; }
.toolbar > * { flex-shrink: 0; }
.toolbar input[type=month] { width: auto; }

/* Groups a filter's label with its control (select / filter-dropdown) so
   the two never get separated onto different lines — .toolbar's children
   wrap independently, so without this a label can end up on one row and
   its control on the next. The group itself is one flex item, and wraps as
   a whole once the row runs out of room. */
.toolbar-group { display: inline-flex; align-items: center; gap: 6px; flex-shrink: 0; }
.toolbar-group > label { margin: 0; }

/* Compact multi-select filter: a small "Label: All / N selected" button
   that opens a checkbox list on click, instead of a native <select multiple>
   rendering as a tall, always-open listbox. See common.js createFilterDropdown. */
.filter-dropdown { position: relative; display: inline-block; }
/* A per-column filter (e.g. inside a table header cell) fills its column,
   same as the plain text/select filters next to it, instead of sizing to
   its own (variable-length) label text. */
.filter-dropdown-compact { display: block; width: 100%; }
.filter-dropdown-compact .filter-dropdown-trigger { width: 100%; text-align: left; }
.filter-dropdown-trigger {
  background: #fff;
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
}
.filter-dropdown-trigger:hover { background: #f3f4f6; }
.filter-dropdown-trigger.has-selection { border-color: var(--primary); color: var(--primary); font-weight: 600; }
.filter-dropdown-menu {
  display: none;
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px;
  min-width: 180px;
  max-height: 260px;
  overflow-y: auto;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
  z-index: 60;
}
.filter-dropdown-menu.open { display: block; }
.filter-dropdown-clear {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  border-bottom: 1px solid var(--border);
  color: var(--muted);
  font-size: 12px;
  padding: 4px 6px 8px;
  margin-bottom: 4px;
  cursor: pointer;
}
.filter-dropdown-clear:hover { color: var(--text); }
.filter-dropdown-options label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  padding: 5px 6px;
  border-radius: 5px;
  cursor: pointer;
  white-space: nowrap;
}
.filter-dropdown-options label:hover { background: #f3f4f6; }
.filter-dropdown-options input[type=checkbox] { width: auto; margin: 0; }

.login-wrap { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 16px; }
.login-card { width: min(340px, 100%); }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
.form-grid.wide { grid-template-columns: 1fr; }

/* Any table gets wrapped in this (see common.js) so a wide table scrolls
   inside its own box instead of blowing out the page on a narrow screen. */
.table-scroll { overflow-x: auto; overflow-y: auto; max-height: 480px; -webkit-overflow-scrolling: touch; }
.table-scroll > table { min-width: 100%; }
.table-scroll th { position: sticky; top: 0; z-index: 1; }

/* Freezes the header row(s) AND the first column together, for a wide
   sheet-style table (e.g. the Internal Management Report). The header's
   second row (the per-column filter inputs) gets its sticky `top` set from
   JS as --freeze-row2-top, since the first row's height depends on its
   content and can't be hard-coded. Every sticky cell needs an opaque
   background — otherwise rows/columns scrolling underneath show through. */
.freeze-table thead tr:nth-child(2) th {
  position: sticky;
  top: var(--freeze-row2-top, 37px);
  z-index: 2;
}
.freeze-table thead th:first-child,
.freeze-table tbody td:first-child {
  position: sticky;
  left: 0;
}
.freeze-table thead th:first-child { background: #fafbfc; }
.freeze-table tbody td:first-child { background: var(--card); z-index: 1; }
/* The two corner cells (row+column both frozen) must paint above every
   other sticky cell, in both scroll directions at once. */
.freeze-table thead tr:first-child th:first-child,
.freeze-table thead tr:nth-child(2) th:first-child {
  z-index: 4;
}

/* ----- Tablet and phone ----- */
/* Below this width the sidebar becomes an off-canvas drawer — hidden by
   default, slid in from the left via the hamburger toggle, overlaying the
   page instead of pushing it (which is why body's margin-left resets to 0
   here: there's no permanent reserved column any more). */
@media (max-width: 1024px) {
  body { margin-left: 0; }
  .sidenav {
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: 4px 0 16px rgba(0, 0, 0, 0.3);
  }
  .sidenav.open { transform: translateX(0); }
  .nav-toggle { display: block; }
}

/* ----- Phone ----- */
@media (max-width: 640px) {
  .page { margin: 14px auto; padding: 0 10px; }
  .card { padding: 14px; }
  h1 { font-size: 18px; }
  h2 { font-size: 15px; }

  .form-grid { grid-template-columns: 1fr; }

  .toolbar { flex-direction: column; align-items: stretch; }
  .toolbar > * { width: 100%; }

  /* 16px stops iOS Safari auto-zooming the page on focus; also gives
     every control a taller, easier-to-tap hit area. */
  input, select, textarea { font-size: 16px; padding: 10px; }
  button { padding: 10px 16px; font-size: 14px; }

  table { font-size: 12px; }
  th, td { padding: 6px 8px; }
}
