:root {
  color-scheme: light dark;
  --bg: #f4f5f7;
  --panel: #ffffff;
  --text: #1a1d23;
  --muted: #667085;
  --border: #d9dce1;
  --accent: #2563eb;
  --danger: #dc2626;
  --ok: #16a34a;
  --warn: #d97706;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --panel: #1e2126;
    --text: #eef0f3;
    --muted: #9aa3b2;
    --border: #33373f;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.layout { display: flex; min-height: 100vh; align-items: stretch; }

.sidebar {
  width: 220px;
  flex: 0 0 220px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 1rem 0;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow: hidden;
}

.sidebar .brand {
  color: var(--text);
  text-decoration: none;
  font-weight: 700;
  font-size: 1.1rem;
  padding: 0 1.2rem 1rem;
  display: block;
}

.sidebar nav { display: flex; flex-direction: column; gap: 0.15rem; padding: 0 0.6rem; overflow-y: auto; flex: 1 1 auto; min-height: 0; }
.sidebar nav a {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  color: var(--muted);
  text-decoration: none;
  font-weight: 400;
  padding: 0.5rem 0.6rem;
  border-radius: 8px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.sidebar nav a:hover { color: var(--text); background: var(--bg); }
.sidebar nav a.active { color: var(--accent); background: var(--bg); font-weight: 600; }
.sidebar nav a .nav-icon { flex: 0 0 auto; width: 18px; height: 18px; }
.sidebar nav a.active .nav-icon { color: var(--accent); }

.sidebar .nav-group-label {
  padding: 0.6rem 0.6rem 0.3rem;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar .nav-divider { border-top: 1px solid var(--border); margin: 0.6rem 0.6rem; }

.sidebar-footer { margin-top: auto; padding: 0.5rem 0.6rem 0; border-top: 1px solid var(--border); }
.sidebar-footer a {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  color: var(--muted);
  text-decoration: none;
  font-size: 0.85rem;
  padding: 0.5rem 0.6rem;
  border-radius: 8px;
}
.sidebar-footer a:hover { color: var(--text); background: var(--bg); }
.sidebar-footer a .nav-icon { flex: 0 0 auto; width: 18px; height: 18px; }
.sidebar-version { padding: 0.4rem 0.6rem 0.2rem; font-size: 0.75rem; color: var(--muted); }

.main-area { flex: 1 1 auto; min-width: 0; }

.mobile-topbar { display: none; }
.sidebar-backdrop { display: none; }

@media (max-width: 800px) {
  .mobile-topbar {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 3rem;
    padding: 0 0.75rem;
    background: var(--panel);
    border-bottom: 1px solid var(--border);
    z-index: 30;
  }
  .mobile-topbar-brand { font-weight: 700; }
  .sidebar-toggle {
    background: none;
    border: 1px solid var(--border);
    border-radius: 6px;
    width: 2.25rem;
    height: 2.25rem;
    padding: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.2rem;
    cursor: pointer;
  }
  .sidebar-toggle span { display: block; width: 1.1rem; height: 2px; background: var(--text); }

  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 35;
  }
  .sidebar-backdrop[hidden] { display: none; }

  .layout { flex-direction: column; padding-top: 3rem; }
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    width: 240px;
    max-width: 80vw;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    z-index: 40;
  }
  .sidebar.open { transform: translateX(0); }
}

main { max-width: 1100px; margin: 0 auto; padding: 1.5rem; }

.help-widget { position: fixed; top: 0.6rem; right: 1rem; z-index: 45; }
.help-toggle {
  width: 2.1rem;
  height: 2.1rem;
  padding: 0;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  border: none;
  font-weight: 700;
  font-size: 0.95rem;
  cursor: pointer;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
}
.help-toggle:hover { filter: brightness(1.1); }
.help-panel {
  position: absolute;
  top: 2.6rem;
  right: 0;
  width: 320px;
  max-width: calc(100vw - 2rem);
  max-height: 70vh;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
  padding: 0.75rem 0.9rem;
}
.help-panel[hidden] { display: none; }
.help-panel-title { font-weight: 700; margin-bottom: 0.4rem; }
.help-panel-body { font-size: 0.85rem; color: var(--text); line-height: 1.45; }
.help-panel-body p { margin: 0 0 0.6rem; }
.help-panel-body p:last-child { margin-bottom: 0; }
.help-panel-body ul { margin: 0 0 0.6rem; padding-left: 1.1rem; }
.help-panel-body ul:last-child { margin-bottom: 0; }
.help-panel-body li { margin-bottom: 0.25rem; }

@media (max-width: 800px) {
  .help-widget { top: 0.45rem; right: 0.75rem; }
  .help-toggle { width: 1.9rem; height: 1.9rem; font-size: 0.85rem; }
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1.25rem;
  margin-bottom: 1.25rem;
}

h1, h2, h3 { margin-top: 0; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 0.5rem 0.6rem; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-weight: 600; font-size: 0.85rem; text-transform: uppercase; }

tr.row-link { cursor: pointer; }
tr.row-link:hover { background: var(--panel-alt, rgba(127, 127, 127, 0.08)); }
tr.row-link:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

th.sortable-col { cursor: pointer; user-select: none; white-space: nowrap; }
th.sortable-col:hover { color: var(--text); }
th.sortable-col .sort-arrow { font-size: 0.75em; }

form.inline { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: end; }
label { display: block; font-size: 0.85rem; color: var(--muted); margin-bottom: 0.2rem; }
input, select, textarea {
  padding: 0.45rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.95rem;
}
textarea { width: 100%; min-height: 4rem; }

button, .btn {
  padding: 0.5rem 0.9rem;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  text-decoration: none;
  display: inline-block;
}
button.secondary, .btn.secondary { background: transparent; color: var(--accent); }
button.danger, .btn.danger { background: var(--danger); border-color: var(--danger); }

.field-row { display: flex; gap: 1rem; flex-wrap: wrap; margin-bottom: 0.75rem; }
.field-row > div { flex: 1; min-width: 180px; }
.field-row input, .field-row select, .field-row textarea { width: 100%; box-sizing: border-box; }

.details-toggle > summary {
  cursor: pointer;
  list-style: none;
  display: inline-block;
  padding: 0.5rem 0.9rem;
  border: 1px solid var(--accent);
  border-radius: 6px;
  color: var(--accent);
  font-size: 0.9rem;
}
.details-toggle > summary::-webkit-details-marker { display: none; }
.details-toggle[open] > summary { margin-bottom: 0.75rem; }

.error { color: var(--danger); margin-bottom: 0.75rem; }
.muted { color: var(--muted); font-size: 0.85rem; }

.board { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 0.75rem; }
.board-column { background: var(--panel); border: 1px solid var(--border); border-top: 4px solid var(--status-column-color, var(--border)); border-radius: 8px; padding: 0.75rem; min-height: 200px; transition: background 0.1s, border-color 0.1s; }
.board-column.drag-over { background: var(--bg); border-color: var(--accent, #2563eb); }
.board-column-drop { min-height: 40px; }
.board-column h3 { font-size: 0.85rem; text-transform: uppercase; color: var(--muted); }
.board-column[data-status="available"] { --status-column-color: var(--status-available-fg, #26682a); }
.board-column[data-status="staging"] { --status-column-color: var(--status-staging-fg, #0e4481); }
.board-column[data-status="in_field"] { --status-column-color: var(--status-in_field-fg, #8b5704); }
.board-column[data-status="temporarily_out_of_service"] { --status-column-color: var(--status-temporarily_out_of_service-fg, #8f4100); }
.board-column[data-status="out_of_service"] { --status-column-color: var(--status-out_of_service-fg, #771818); }
.board-column[data-status="checked_out"] { --status-column-color: var(--status-checked_out-fg, #474747); }
.card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.5rem 0.6rem;
  margin-bottom: 0.5rem;
  cursor: grab;
}
.card:hover { border-color: var(--accent, #2563eb); }
.card.dragging { opacity: 0.4; }
.card .label { font-weight: 600; }
.card .callsign { color: var(--muted); font-size: 0.8rem; }

.board-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: 1rem; }
.board-toolbar p { margin: 0; }
#boardCompactToggle { flex-shrink: 0; min-height: 40px; }
#boardCompactToggle[aria-pressed="true"] { background: var(--accent); color: #fff; }
#statusBoard .card { padding: 0.75rem; margin-bottom: 0.625rem; border-left: 4px solid var(--status-column-color, var(--border)); }
.board-card-heading { display: flex; align-items: baseline; justify-content: space-between; flex-wrap: wrap; gap: 0.375rem 0.75rem; line-height: 1.4; }
.board-card-heading .label { overflow-wrap: anywhere; }
.board-card-heading .callsign { margin-left: auto; }
#statusBoard .board-status-select { display: block; width: 100%; min-height: 44px; margin: 0.625rem 0 0; padding: 0.5rem 0.625rem; font: inherit; cursor: pointer; }
#statusBoard .board-status-select[hidden] { display: none; }
#statusBoard.is-compact .card { min-height: 52px; display: flex; align-items: center; }
#statusBoard.is-compact .board-card-heading { width: 100%; }
#statusBoard .card:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.status-menu-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: transparent;
}

.status-menu {
  position: absolute;
  z-index: 50;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.15);
  padding: 0.25rem;
  min-width: 160px;
  display: flex;
  flex-direction: column;
}
.status-menu-option {
  background: none;
  border: none;
  text-align: left;
  padding: 0.4rem 0.6rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.85rem;
  color: inherit;
}
.status-menu-option:hover { background: var(--bg); }
.status-menu[hidden], .status-menu-backdrop[hidden] { display: none; }

.badge { display: inline-block; padding: 0.1rem 0.5rem; border-radius: 999px; font-size: 0.75rem; background: var(--border); }
.badge.in_field { background: var(--status-in_field-fg, #8b5704); color: #fff; opacity: 0.85; }
.badge.checked_out { background: var(--status-checked_out-fg, #474747); color: #fff; opacity: 0.85; }
.badge.temporarily_out_of_service { background: var(--status-temporarily_out_of_service-fg, #8f4100); color: #fff; opacity: 0.85; }
.badge.staging { background: var(--status-staging-fg, #0e4481); color: #fff; opacity: 0.85; }
.badge.available { background: var(--status-available-fg, #26682a); color: #fff; opacity: 0.85; }
.badge.out_of_service { background: var(--status-out_of_service-fg, #771818); color: #fff; opacity: 0.85; }
.badge.in_command { background: #6A1B9A; color: #fff; }

/* License expiration status -- reuses the exact status-board colors so
   expired/expiring/valid reads consistently with in_field/staging/etc. */
.badge.valid { background: #dcfce7; color: #166534; }
.badge.expiring_soon { background: #fef3c7; color: #92400e; }
.badge.expired { background: #fee2e2; color: #991b1b; }
.badge.unknown { background: var(--border); }

/* Command Feed project-request decisions -- reuses the same color language
   as the status board (green=good, amber=needs attention, red=blocked). */
.badge.pending { background: var(--border); }
.badge.approved { background: #dcfce7; color: #166534; }
.badge.denied { background: #fee2e2; color: #991b1b; }
.badge.sent_back { background: #fef3c7; color: #92400e; }

.checklist { display: flex; flex-direction: column; gap: 0.2rem; max-height: 12rem; overflow-y: auto; border: 1px solid var(--border); border-radius: 6px; padding: 0.4rem 0.6rem; }
.checklist-item { font-size: 0.9rem; }
.checklist-item.disabled { color: var(--muted); }

.tag { display: inline-block; padding: 0.05rem 0.4rem; margin-right: 0.2rem; border-radius: 4px; font-size: 0.7rem; background: var(--border); color: var(--muted); }

.info-toggle summary { cursor: pointer; list-style: none; }
.info-toggle summary::-webkit-details-marker { display: none; }
.info-icon { display: inline-flex; }

/* Family-tree org chart (templates/org_chart.html) -- classic nested-list
   connector-line technique, no JS/library needed. */
.org-tree-scroll { overflow-x: auto; padding: 0.5rem 0 1rem; }
.span-limit-form { align-items: flex-end; }

ul.org-tree, .org-tree ul {
  display: flex;
  padding-top: 1rem;
  position: relative;
  list-style: none;
  margin: 0;
}
ul.org-tree {
  justify-content: center;
  /* If root-level positions overflow the scroll container, plain `center`
     can push the leading siblings out past scrollLeft: 0, making them
     unreachable by scrolling. `safe center` falls back to start-alignment
     in that case so every sibling stays scrollable. Unsupported browsers
     simply ignore this line and keep the plain center above. */
  justify-content: safe center;
  padding-top: 0;
}
.org-tree li {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 1rem 0.5rem 0;
  position: relative;
}
.org-tree li:only-child { padding-top: 0; }

/* Connector from a node down to its children row, centered under the
   parent card (li's align-items: center keeps the card centered over its
   children row, so 50% here lands on the card's own center). */
.org-tree li ul::before {
  content: "";
  position: absolute;
  top: 0;
  left: 50%;
  border-left: 1px solid var(--border);
  width: 0;
  height: 1rem;
}
/* Each child gets two half-width horizontal segments -- ::before reaches
   left to the previous sibling's stub, ::after reaches right to the next
   one -- plus its own vertical stub down to the center of ITS OWN card
   (border-left on ::after, or border-right on ::before for the last
   child). Percentages resolve against each li's own box, so this lines up
   correctly even when sibling cards render at different widths. */
.org-tree li li::before,
.org-tree li li::after {
  content: "";
  position: absolute;
  top: 0;
  right: 50%;
  width: 50%;
  height: 1rem;
  border-top: 1px solid var(--border);
}
.org-tree li li::after {
  right: auto;
  left: 50%;
  border-left: 1px solid var(--border);
}
.org-tree li li:first-child::before { border: none; }
.org-tree li li:last-child::after { border: none; }
.org-tree li li:last-child::before { border-right: 1px solid var(--border); }
.org-tree li li:only-child::before,
.org-tree li li:only-child::after { display: none; }

.org-children.collapsed { display: none; }
.org-tree-controls { display: flex; gap: 0.5rem; margin-bottom: 0.5rem; }

.org-node {
  display: inline-flex;
  flex-direction: column;
  gap: 0.25rem;
  min-width: 11rem;
  max-width: 16rem;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.5rem 0.65rem;
  text-align: left;
}
.org-node-flagged { border-color: var(--danger); }
.org-node-header { display: flex; align-items: center; gap: 0.35rem; }
.org-toggle {
  flex: 0 0 auto;
  width: 1.3rem;
  height: 1.3rem;
  line-height: 1;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg);
  color: var(--muted);
  cursor: pointer;
  font-size: 0.7rem;
}
.org-node-title { font-weight: 600; font-size: 0.9rem; }
.org-node-person { font-size: 0.85rem; }
.org-node-count { font-size: 0.75rem; color: var(--muted); }
.org-node-manage { font-size: 0.85rem; margin-top: 0.15rem; }
.org-node-manage summary { cursor: pointer; color: var(--accent); }
.org-node-manage > div { margin-top: 0.4rem; }
.org-node-manage label { display: block; font-size: 0.75rem; color: var(--muted); margin-bottom: 0.1rem; }
.org-node-add-position { display: flex; flex-direction: column; gap: 0.3rem; }
.org-node-add-position input, .org-node-add-position select {
  width: 100%; box-sizing: border-box; font-size: 0.8rem; padding: 0.3rem 0.45rem;
}
.org-node-add-position button { align-self: flex-start; font-size: 0.8rem; padding: 0.3rem 0.6rem; }
.info-icon svg rect { fill: var(--muted); }
.info-icon.valid svg rect { fill: #22c55e; }
.info-icon.expiring_soon svg rect { fill: #f59e0b; }
.info-icon.expired svg rect { fill: #ef4444; }
.info-icon.unknown svg rect { fill: var(--muted); }
.info-panel { display: grid; grid-template-columns: 1fr 1fr; gap: 0.3rem 0.8rem; margin-top: 0.4rem; padding: 0.4rem 0.5rem; border: 1px solid var(--border); border-radius: 6px; font-size: 0.75rem; }
.info-label { font-weight: 600; color: var(--muted); font-size: 0.7rem; }

.tab-bar { display: flex; gap: 0.4rem; flex-wrap: wrap; margin-top: 0.75rem; }
.tab-btn { background: var(--panel); border: 1px solid var(--border); border-radius: 6px; padding: 0.4rem 0.8rem; cursor: pointer; }
.tab-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.tab-count { display: inline-block; margin-left: 0.3rem; padding: 0 0.4rem; border-radius: 999px; background: var(--border); font-size: 0.7rem; }

.headcount { display: flex; gap: 1.5rem; margin-bottom: 1rem; }
.headcount .stat { text-align: center; }
.headcount .stat .n { font-size: 2rem; font-weight: 700; }
.headcount .stat .l { color: var(--muted); font-size: 0.85rem; }

.incident-list a { display: block; padding: 0.5rem 0; }
