:root {
  /* Neutrals (slate) */
  --bg: #f1f5f9;
  --card: #ffffff;
  --ink: #0f172a;
  --ink-soft: #334155;
  --muted: #64748b;
  --line: #e2e8f0;
  --line-strong: #cbd5e1;

  /* Brand / primary (navy — matched to the PBC Maintenance app) */
  --brand: #1a2f5a;
  --primary: #1a2f5a;
  --primary-hover: #14254a;
  --primary-soft: #eef1f7;
  --primary-tint: #f4f6fb;

  /* Status / semantic */
  --high: #dc2626;
  --high-bg: #fde8e8;
  --high-ring: #f7c4c4;
  --attn: #c2410c;
  --attn-bg: #ffedd5;
  --warn: #92671a;
  --warn-bg: #faf0d7;
  --ok: #1f6b46;
  --ok-bg: #dcefe3;
  --info: #1a2f5a;
  --info-bg: #e6eaf3;

  /* Shape & depth */
  --radius: 14px;
  --radius-sm: 10px;
  --radius-pill: 999px;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.08), 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-md: 0 4px 12px rgba(15, 23, 42, 0.10), 0 2px 4px rgba(15, 23, 42, 0.06);
  --shadow-lg: 0 12px 32px rgba(15, 23, 42, 0.18);

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
          Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji";

  /* Spacing scale */
  --sp-1: 4px; --sp-2: 8px; --sp-3: 12px; --sp-4: 16px; --sp-5: 20px; --sp-6: 24px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--ink);
  font-size: 16px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

h1, h2, h3 { line-height: 1.25; letter-spacing: -0.01em; }
h1 { font-size: 1.4rem; margin: 6px 0 var(--sp-4); }
h2 { font-size: 1.1rem; margin: var(--sp-5) 0 var(--sp-3); }

a { color: var(--primary); text-decoration: none; }
a:hover { color: var(--primary-hover); text-decoration: underline; }

/* ─── Top bar (white header hosts the navy brand) ─── */
header.topbar {
  position: sticky; top: 0; z-index: 20;
  background: #fff;
  color: var(--brand);
  display: flex; align-items: center; gap: var(--sp-3);
  padding: 10px 16px;
  padding-top: max(10px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
  box-shadow: var(--shadow-sm);
}
header.topbar .brand {
  display: inline-flex; align-items: center;
  text-decoration: none; margin-right: auto;
}
header.topbar .brand:hover { text-decoration: none; opacity: .9; }
.brand-logo { height: 34px; width: auto; display: block; }
header.topbar nav { display: flex; gap: 4px; margin-left: auto; flex-wrap: wrap; align-items: center; }
header.topbar nav a {
  color: var(--ink-soft); background: transparent; border: 0;
  padding: 9px 13px; border-radius: var(--radius-sm); font-size: .92rem; font-weight: 600;
  cursor: pointer; min-height: 40px; display: inline-flex; align-items: center; gap: 5px;
  transition: background .15s ease, color .15s ease; text-decoration: none;
}
header.topbar nav a:hover { background: var(--primary-soft); color: var(--brand); text-decoration: none; }
header.topbar nav a.active { background: var(--primary-soft); color: var(--brand); }

/* Hamburger button: hidden on desktop, revealed at the mobile breakpoint. */
.nav-toggle {
  display: none; margin-left: auto; color: var(--brand);
  background: transparent; border: 0; cursor: pointer;
  padding: 8px; border-radius: var(--radius-sm); min-height: 40px; align-items: center;
}
.nav-toggle:hover { background: var(--primary-soft); }
.ic { display: inline-block; vertical-align: -0.16em; flex: 0 0 auto; }

main {
  max-width: 1000px;
  margin: 0 auto;
  padding: var(--sp-5) var(--sp-4);
}

/* ─── Cards ─── */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: var(--sp-5);
  margin-bottom: var(--sp-4);
}
.card h2:first-child { margin-top: 0; }

.row { display: flex; gap: var(--sp-3); flex-wrap: wrap; }
.row > * { flex: 1 1 180px; }

/* ─── Forms ─── */
label {
  display: block; font-size: .8rem; color: var(--ink-soft); margin: var(--sp-3) 0 6px;
  font-weight: 600; letter-spacing: .005em;
}
input, select, textarea {
  width: 100%; padding: 11px 13px; font-size: 16px;
  border: 1px solid var(--line-strong); border-radius: var(--radius-sm);
  background: #fff; color: var(--ink); font-family: inherit;
  transition: border-color .15s ease, box-shadow .15s ease;
  min-height: 44px;
}
input::placeholder, textarea::placeholder { color: #94a3b8; }
input:hover, select:hover, textarea:hover { border-color: #94a3b8; }
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(26, 47, 90, .16);
}
select {
  appearance: none; -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center; padding-right: 38px;
}

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  background: var(--primary); color: #fff; border: 0;
  padding: 11px 18px; border-radius: var(--radius-sm); font-size: 1rem; font-weight: 600;
  cursor: pointer; text-align: center; min-height: 44px; line-height: 1.2;
  box-shadow: var(--shadow-sm); text-decoration: none;
  transition: background .15s ease, box-shadow .15s ease, transform .05s ease;
  font-family: inherit;
}
.btn:hover { background: var(--primary-hover); text-decoration: none; box-shadow: var(--shadow); color: #fff; }
.btn:active { transform: translateY(1px); }
.btn:focus-visible { outline: none; box-shadow: 0 0 0 3px rgba(26, 47, 90, .30); }
.btn.secondary { background: #fff; color: var(--ink-soft); border: 1px solid var(--line-strong); box-shadow: var(--shadow-sm); }
.btn.secondary:hover { background: #f8fafc; border-color: #94a3b8; color: var(--ink); }
.btn.small { padding: 8px 13px; font-size: .85rem; min-height: 38px; }
.btn.block { width: 100%; }
.btn.danger { background: var(--high); }
.btn.danger:hover { background: #b91c1c; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-row { display: flex; gap: var(--sp-3); margin-top: var(--sp-4); flex-wrap: wrap; }

/* Logout button in the nav */
.nav-logout {
  color: var(--muted); background: transparent; border: 0; cursor: pointer;
  padding: 9px 13px; border-radius: var(--radius-sm); font-size: .92rem; font-weight: 600;
  min-height: 40px; font-family: inherit;
}
.nav-logout:hover { background: var(--high-bg); color: var(--high); }

/* Login screen */
.login-wrap {
  min-height: 100vh; min-height: 100dvh; display: flex; align-items: center; justify-content: center;
  padding: var(--sp-5); background: linear-gradient(170deg, #fff 0%, var(--primary-tint) 55%, #e6eaf3 100%);
}
.login-box { width: 100%; max-width: 380px; }
.login-logo { text-align: center; margin-bottom: var(--sp-5); }
.login-logo img { width: 100%; max-width: 240px; height: auto; }
.login-logo p { margin: 12px 0 0; color: var(--muted); font-weight: 600; }
.login-box .card { box-shadow: var(--shadow-lg); }

/* Collapsible inline form (meter update / add schedule) */
.link-summary { cursor: pointer; color: var(--primary); font-weight: 600; font-size: .9rem; }
.link-summary:hover { text-decoration: underline; }

/* Maintenance schedule rows */
.mnt-row {
  display: flex; justify-content: space-between; align-items: center; gap: var(--sp-3);
  flex-wrap: wrap; padding: 10px 0; border-top: 1px solid var(--line);
}
.mnt-row:first-of-type { border-top: 0; }
.mnt-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.badge.mnt-overdue { background: var(--high-bg); color: var(--high); border-color: var(--high-ring); text-transform: none; letter-spacing: 0; }
.badge.mnt-soon { background: var(--warn-bg); color: var(--warn); border-color: #ecdcae; text-transform: none; letter-spacing: 0; }
.badge.mnt-ok { background: var(--ok-bg); color: var(--ok); border-color: #b6dac5; text-transform: none; letter-spacing: 0; }
.badge.mnt-none { background: #f1f5f9; color: var(--muted); border-color: var(--line); text-transform: none; letter-spacing: 0; }

/* Status timeline */
.tl-row { display: flex; gap: var(--sp-3); align-items: flex-start; padding: 10px 0; border-top: 1px solid var(--line); }
.tl-row:first-of-type { border-top: 0; }
.tl-row > :first-child { flex: 0 0 auto; padding-top: 2px; }

/* ─── Tables ─── */
/* Wrapper keeps a wide table's overflow inside the card instead of stretching the
   whole page (which would push the sticky header — and its hamburger — off-screen
   on narrow viewports). */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--line); font-size: 0.92rem; }
th { color: var(--muted); font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.04em; font-weight: 700; }
tbody tr:last-child td { border-bottom: 0; }
td a { color: var(--primary); text-decoration: none; font-weight: 600; }
td a:hover { text-decoration: underline; }
tfoot th { color: var(--ink); font-size: .82rem; text-transform: none; letter-spacing: 0; }

/* ─── Badges ─── */
.badge {
  display: inline-flex; align-items: center; padding: 4px 11px; border-radius: var(--radius-pill);
  font-size: .72rem; font-weight: 700; white-space: nowrap; line-height: 1.4;
  letter-spacing: .02em; text-transform: uppercase; border: 1px solid transparent;
}
.badge.active { background: var(--ok-bg); color: var(--ok); border-color: #b6dac5; }
.badge.down { background: var(--warn-bg); color: var(--warn); border-color: #ecdcae; }
.badge.sold { background: var(--high-bg); color: var(--high); border-color: var(--high-ring); }
.badge.archived { background: #f1f5f9; color: var(--muted); border-color: var(--line-strong); }
.badge.idle { background: var(--warn-bg); color: var(--warn); border-color: #ecdcae; text-transform: none; letter-spacing: 0; }

/* Dashboard stat card (idle-equipment count — click to filter) */
.amber { color: var(--warn); }
.stat-card { display: flex; align-items: center; gap: 16px; cursor: pointer; }
.stat-card .big-amount { line-height: 1; }
.stat-card:hover { border-color: var(--warn); box-shadow: var(--shadow-md); }
.stat-card:focus-visible { outline: 2px solid var(--warn); outline-offset: 2px; }
.stat-card.active { border-color: var(--warn); box-shadow: 0 0 0 2px var(--warn-bg), var(--shadow-sm); }

.muted { color: var(--muted); }
.small { font-size: .85rem; }
.check { display: inline-flex; align-items: center; gap: 7px; font-size: .85rem; color: var(--ink-soft); font-weight: 600; cursor: pointer; margin: 0; }
.hidden { display: none !important; }
/* Financial/admin-only UI is hidden until the session is confirmed admin. */
body:not(.role-admin) .admin-only { display: none !important; }
.right { text-align: right; }
.pos { color: var(--ok); }
.neg { color: var(--high); }

/* Summary cards (lifetime ledger running totals) */
.summary-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: var(--sp-3); margin-bottom: var(--sp-4); }
.summary-cards .card { margin-bottom: 0; }
.sum-label { font-size: .72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin-bottom: 4px; }

/* Ledger entry-type badges */
.badge.led-purchase { background: var(--info-bg); color: var(--info); border-color: #ccd5e6; text-transform: none; letter-spacing: 0; }
.badge.led-maintenance { background: var(--warn-bg); color: var(--warn); border-color: #ecdcae; text-transform: none; letter-spacing: 0; }
.badge.led-expense { background: var(--attn-bg); color: var(--attn); border-color: #fed7aa; text-transform: none; letter-spacing: 0; }
.badge.led-usage { background: var(--ok-bg); color: var(--ok); border-color: #b6dac5; text-transform: none; letter-spacing: 0; }

/* Utilization fleet bars */
.util-row { display: grid; grid-template-columns: 1.4fr 2fr auto auto; gap: var(--sp-4); align-items: center; padding: 12px 0; border-top: 1px solid var(--line); }
.util-row:first-of-type { border-top: 0; }
.util-name { font-weight: 600; min-width: 0; }
.util-bars { display: flex; flex-direction: column; gap: 6px; }
.util-bar { position: relative; background: #f1f5f9; border-radius: var(--radius-sm); height: 22px; overflow: hidden; }
.util-fill { position: absolute; inset: 0 auto 0 0; border-radius: var(--radius-sm); }
.util-fill.invested { background: var(--warn); opacity: .85; }
.util-fill.billed { background: var(--brand); }
.util-bar span { position: relative; z-index: 1; display: inline-flex; align-items: center; height: 100%; padding: 0 8px; font-size: .78rem; font-weight: 600; color: var(--ink); white-space: nowrap; }
.util-down { text-align: center; white-space: nowrap; }
.util-down-num { font-weight: 800; font-size: 1.05rem; font-variant-numeric: tabular-nums; }
.util-net { font-weight: 800; font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
@media (max-width: 620px) {
  .util-row { grid-template-columns: 1fr auto; }
  .util-bars { grid-column: 1 / -1; order: 3; }
  .util-down { text-align: left; }
}

/* Segmented toggle (e.g. date-range vs engine-hours billing) */
.tabs {
  display: inline-flex; gap: 4px; margin: 4px 0 10px; padding: 4px;
  background: #e2e8f0; border-radius: var(--radius-sm);
}
.tab {
  flex: 0 0 auto; text-align: center; padding: 8px 16px; border-radius: 7px;
  cursor: pointer; background: transparent; color: var(--ink-soft);
  font-weight: 600; font-size: .9rem; border: 0; font-family: inherit; min-height: 38px;
  transition: background .15s ease, color .15s ease;
}
.tab.active { background: #fff; color: var(--brand); box-shadow: var(--shadow-sm); }

/* Grouped report tables (billing export) */
.group-row td {
  background: var(--primary-soft); color: var(--brand);
  font-weight: 700; font-size: .82rem; text-transform: uppercase; letter-spacing: .02em;
}
.subtotal-row td { font-weight: 700; border-top: 1px solid var(--line-strong); }
.subtotal-row td.right { color: var(--brand); }

/* ─── Print view (billing export) — clean layout, no nav/controls ─── */
.print-only { display: none; }
@media print {
  header.topbar, #controls, #actions, .btn { display: none !important; }
  body { background: #fff; color: #000; }
  main { max-width: 100%; padding: 0; margin: 0; }
  h1 { margin: 0 0 4px; }
  .card { box-shadow: none; border: 0; border-radius: 0; padding: 0; margin: 0 0 18px; }
  .print-only { display: block; }
  .table-wrap { overflow: visible; }
  table { font-size: 11px; }
  th { color: #000; }
  thead { display: table-header-group; }
  tr { page-break-inside: avoid; }
  .group-row td, .subtotal-row td.right { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
.mono { font-variant-numeric: tabular-nums; }
.big-amount { font-size: 1.7rem; font-weight: 800; color: var(--brand); letter-spacing: -0.02em; }

.kv { display: grid; grid-template-columns: 150px 1fr; gap: 8px 16px; font-size: .95rem; }
.kv dt { color: var(--muted); font-weight: 600; }
.kv dd { margin: 0; }

.notice { padding: 11px 14px; border-radius: var(--radius-sm); font-size: 0.9rem; margin-bottom: var(--sp-3); }
.notice.error { background: var(--high-bg); color: var(--high); }
.notice.ok { background: var(--ok-bg); color: var(--ok); }
.empty { color: var(--muted); padding: 20px 4px; text-align: center; }

.toolbar { display: flex; gap: var(--sp-3); align-items: flex-end; flex-wrap: wrap; margin-bottom: var(--sp-4); }
.toolbar > * { flex: 0 1 auto; }
.toolbar label { margin: 0 0 6px; }

/* ─── Searchable job code combobox ─── */
.combo { position: relative; }
.combo-menu {
  position: absolute; top: calc(100% + 4px); left: 0; right: 0; z-index: 40;
  background: #fff; border: 1px solid var(--line); border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md); padding: 6px; margin: 0;
  max-height: 260px; overflow-y: auto;
  display: none; flex-direction: column; gap: 2px;
}
.combo-menu.open { display: flex; }
.combo-opt {
  display: block; width: 100%; text-align: left; background: transparent; border: 0;
  padding: 9px 11px; border-radius: 8px; cursor: pointer; font-family: inherit;
  font-size: .92rem; color: var(--ink); min-height: 0;
}
.combo-opt:hover, .combo-opt.active { background: var(--primary-soft); color: var(--brand); }
.combo-opt .combo-code { font-weight: 700; font-variant-numeric: tabular-nums; }
.combo-opt .combo-name { color: var(--muted); margin-left: 6px; }
.combo-opt.active .combo-name, .combo-opt:hover .combo-name { color: var(--brand); }
.combo-empty { padding: 9px 11px; color: var(--muted); font-size: .88rem; }

@media (max-width: 560px) {
  main { padding: var(--sp-4) var(--sp-3); }
  header.topbar { padding: 10px 12px; gap: var(--sp-2); }
  header.topbar .brand { font-size: 1.05rem; }
  .card { padding: var(--sp-4); }
  .kv { grid-template-columns: 1fr; gap: 2px; }
  .kv dt { margin-top: var(--sp-2); }
  th.hide-sm, td.hide-sm { display: none; }
}

/* ─── Mobile nav: collapse the top bar into a ☰ hamburger menu ─── */
@media (max-width: 680px) {
  .nav-toggle { display: inline-flex; }
  header.topbar nav {
    display: none;
    position: absolute; top: 100%; left: 0; right: 0;
    flex-direction: column; align-items: stretch; gap: 2px;
    margin-left: 0; flex-wrap: nowrap;
    background: #fff;
    border-bottom: 1px solid var(--line);
    box-shadow: 0 10px 24px rgba(26, 47, 90, .14);
    padding: 8px;
    z-index: 30;
  }
  header.topbar nav.open { display: flex; }
  header.topbar nav a { width: 100%; justify-content: flex-start; min-height: 44px; }
}
