/* nav.css — the shared design system: colour tokens, typography, the sidebar
   and the page primitives every screen is built from. Linked via
   <link rel="stylesheet" href="/nav.css"> and populated by nav.js. */

@import url("https://fonts.googleapis.com/css2?family=Public+Sans:wght@400;500;600;700&display=swap");

/* Single source of truth for the colour system — every page links this file
   instead of redefining its own :root, so the palette only ever changes here. */
:root {
  --font: "Public Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: ui-monospace, "SFMono-Regular", Consolas, monospace;

  --primary: #3D6B24;       /* deep leaf-green, closer to NEA's logo green */
  --primary-dark: #2F5319;  /* hover / pressed state for primary actions */
  --primary-light: #6B9950; /* borders / focus rings — a lighter step of primary */
  --accent: #EAF3E3;        /* pale tint for selected/active surfaces */
  --bg: #F6F7F4;
  --surface: #ffffff;
  --surface-muted: #FAFBF8; /* table heads, drop zones */
  --border: #E3E6DF;
  --border-strong: #D5DACF; /* form controls */
  --divider: #EEF0EB;
  --text: #1B2418;
  --text-body: #3F4A3A;
  --text-muted: #5D6758;    /* secondary copy — passes 4.5:1 on white */
  --text-light: #6B7466;    /* captions only */

  /* Status colours — success reuses the primary green per the NEA palette.
     "Needs support" is orange rather than red so it differs from the green
     in lightness as well as hue. */
  --green: #3D6B24;
  --green-bg: #EAF3E3;
  --green-border: #C9DDB8;
  --amber-bg: #FDF3EA;
  --amber-text: #93400A;
  --amber-border: #F0C9A6;
  --amber-mark: #D9731A;
  --info-bg: #E8EEF8;
  --info-text: #1F4A8A;
  --error-text: #B42318;
  --error-bg: #FBEAEA;
  --error-border: #E8B4AC;
  /* Aliases for pages that reference the red/error family by this name */
  --red: var(--error-text);
  --red-bg: var(--error-bg);
  --red-border: var(--error-border);

  --radius: 12px;
  --radius-sm: 8px;
  --shadow-pop: 0 24px 60px rgba(27, 36, 24, .22);
}

body { font-family: var(--font); color: var(--text); background: var(--bg); }
button, input, select, textarea { font: inherit; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
/* Component display rules (buttons, chips, flex rows) must never override
   the hidden attribute scripts use to show and hide them. */
[hidden] { display: none !important; }
.visually-hidden {
  position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* Chromium's customizable-select mode exposes the opened option picker for
   styling while retaining native keyboard, form and change-event behaviour.
   Other browsers ignore this block and keep their normal native control. */
@supports (appearance: base-select) {
  select,
  ::picker(select) {
    appearance: base-select !important;
  }

  select {
    display: flex;
    align-items: center;
    padding-inline-end: 10px !important;
    background-image: none !important;
    transition: border-color .15s ease, box-shadow .15s ease, background-color .15s ease;
  }

  select::picker-icon {
    flex: 0 0 auto;
    margin-inline-start: auto;
    color: var(--text-muted);
    transform-origin: 50% 50%;
    transition: transform .15s ease;
  }

  select:open::picker-icon {
    transform: rotate(180deg);
  }

  ::picker(select) {
    max-height: min(320px, 50vh);
    margin-top: 5px;
    padding: 5px;
    overflow-y: auto;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--surface);
    box-shadow: 0 12px 30px rgba(27, 36, 24, .16);
  }

  select option {
    min-height: 36px;
    padding: 9px 11px;
    border-radius: 7px;
    background: var(--surface);
    color: var(--text);
  }

  select option:hover,
  select option:focus-visible {
    background: var(--accent);
    color: var(--primary-dark);
  }

  select option:checked {
    background: var(--primary);
    color: #fff;
    font-weight: 700;
  }

  select option:disabled {
    background: var(--bg);
    color: var(--text-light);
  }
}

/* ── Sidebar ──
   --nav-w is the sidebar's width on desktop; collapsing it to an icon rail
   (html.nav-collapsed, set by nav.js) gives every page more room. */
:root { --nav-w: 240px; }
@media (min-width: 861px) {
  html.nav-collapsed { --nav-w: 64px; }
}
.app-nav {
  width: var(--nav-w); flex-shrink: 0; box-sizing: border-box; background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 16px; padding: 18px 12px 14px;
  position: fixed; top: 0; left: 0; bottom: 0; z-index: 30; overflow-x: hidden;
  transition: transform .2s ease, width .2s ease, padding .2s ease;
  font-family: var(--font);
}
.app-nav-head { display: flex; align-items: center; justify-content: space-between; gap: 4px; }
.app-nav-brand {
  display: flex; align-items: center; gap: 10px; padding: 0 8px; min-width: 0;
  color: var(--text); text-decoration: none;
}
.app-nav-toggle {
  width: 36px; height: 36px; border-radius: var(--radius-sm); border: 0; background: transparent;
  color: var(--text-muted); cursor: pointer; display: flex; align-items: center; justify-content: center;
  flex-shrink: 0; transition: background .15s, color .15s;
}
.app-nav-toggle:hover { background: var(--bg); color: var(--text); }
.app-nav-toggle svg { width: 20px; height: 20px; }
.app-nav-title { font-size: 15px; font-weight: 700; line-height: 1.3; }

/* Knowledge-base switcher: one compact row under the brand, shaped like a nav item. */
.app-nav-kb { position: relative; display: flex; align-items: center; }
.app-nav-kb[hidden] { display: none; }
.app-nav-kb-icon { position: absolute; left: 12px; display: flex; color: var(--text-muted); pointer-events: none; }
.app-nav-kb-icon svg { width: 18px; height: 18px; }
.app-nav-kb-select {
  width: 100%; min-height: 38px; padding: 0 28px 0 42px; appearance: none; cursor: pointer;
  border: 1px solid transparent; border-radius: var(--radius-sm); background-color: var(--bg);
  color: var(--text); font: inherit; font-size: 14px; font-weight: 600; text-overflow: ellipsis;
  background-image: linear-gradient(45deg, transparent 50%, var(--text-muted) 50%), linear-gradient(135deg, var(--text-muted) 50%, transparent 50%);
  background-position: calc(100% - 15px) 50%, calc(100% - 11px) 50%;
  background-size: 4px 4px, 4px 4px; background-repeat: no-repeat;
  transition: border-color .15s;
}
.app-nav-kb-select:hover:not(:disabled) { border-color: var(--border-strong); }
.app-nav-kb-select:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }
.app-nav-kb-select:disabled { background-image: none; opacity: 1; cursor: default; }
.app-nav-kb-rail {
  display: none; width: 40px; height: 40px; margin: 0 auto; border: 0; border-radius: var(--radius-sm);
  background: transparent; color: var(--text-muted); cursor: pointer; align-items: center; justify-content: center;
}
.app-nav-kb-rail:hover { background: var(--bg); color: var(--text); }
.app-nav-kb-rail svg { width: 18px; height: 18px; }

/* Every item fits the full height; the list only scrolls on very short
   screens, and without a visible scrollbar. */
.app-nav-items { display: flex; flex-direction: column; gap: 14px; flex: 1; min-height: 0; overflow-y: auto; scrollbar-width: none; }
.app-nav-items::-webkit-scrollbar { display: none; }
.app-nav-group { display: flex; flex-direction: column; gap: 2px; }
.app-nav-group[hidden] { display: none; }
.app-nav-group-label {
  padding: 0 12px 4px; font-size: 11px; font-weight: 600; letter-spacing: .06em;
  text-transform: uppercase; color: var(--text-light);
}
.app-nav-item {
  display: flex; align-items: center; gap: 12px; min-height: 38px; padding: 0 12px;
  border-radius: var(--radius-sm); color: var(--text-body); text-decoration: none;
  font-size: 14px; font-weight: 500; transition: background .15s, color .15s;
}
.app-nav-item:hover { background: var(--bg); color: var(--text); }
.app-nav-item.active { background: var(--accent); color: var(--primary-dark); font-weight: 600; }
.app-nav-item svg { width: 18px; height: 18px; flex-shrink: 0; }

.app-nav-footer {
  display: flex; align-items: center; gap: 10px; padding: 12px 8px 0;
  border-top: 1px solid var(--divider);
}
.app-nav-avatar {
  width: 32px; height: 32px; border-radius: 50%; background: var(--accent); color: var(--primary-dark);
  display: flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 700;
  flex-shrink: 0;
}
.app-nav-user { display: flex; flex-direction: column; flex: 1; min-width: 0; }
.app-nav-username {
  font-size: 13px; font-weight: 600; color: var(--text);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.app-nav-role { font-size: 12px; color: var(--text-muted); }
.app-nav-logout {
  width: 36px; height: 36px; border-radius: var(--radius-sm); border: 0; background: transparent;
  color: var(--text-muted); cursor: pointer; display: flex; align-items: center; justify-content: center;
  flex-shrink: 0; transition: background .15s, color .15s;
}
.app-nav-logout:hover { background: var(--bg); color: var(--text); }
.app-nav-logout:disabled { cursor: progress; opacity: .6; }
.app-nav-logout svg { width: 18px; height: 18px; }

/* ── Mobile top bar + backdrop (hidden on desktop) ── */
.app-topbar {
  display: none; align-items: center; gap: 12px; height: 56px; padding: 0 16px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20; flex-shrink: 0;
  align-self: stretch; width: 100%; box-sizing: border-box;
}
.app-topbar-burger {
  width: 40px; height: 40px; border-radius: var(--radius-sm); border: 1px solid var(--border-strong);
  background: var(--surface); color: var(--text); cursor: pointer;
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.app-topbar-burger svg { width: 20px; height: 20px; }
.app-topbar-title { color: var(--text); font-size: 15px; font-weight: 700; }
.app-nav-backdrop {
  display: none; position: fixed; inset: 0; background: rgba(27,36,24,.4); z-index: 25;
  opacity: 0; transition: opacity .2s ease;
}
.app-nav-backdrop.open { display: block; opacity: 1; }

/* The drawer only exists below the breakpoint. Without this, a drawer left
   open while the window is widened leaves a full-screen backdrop swallowing
   every click on the desktop layout. */
@media (min-width: 861px) {
  .app-nav-backdrop, .app-nav-backdrop.open { display: none; }
}

/* Stop the page behind the drawer from scrolling under the user's finger. */
body.nav-open { overflow: hidden; }

/* ── Main content area ── */
.app-main { margin-left: var(--nav-w); min-height: 100vh; min-height: 100dvh; transition: margin-left .2s ease; }

/* Collapsed sidebar: icons only. Labels stay in the page, visually hidden,
   so screen readers still announce each link. */
@media (min-width: 861px) {
  html.nav-collapsed .app-nav { padding: 18px 10px 14px; }
  html.nav-collapsed .app-nav-head { justify-content: center; }
  html.nav-collapsed .app-nav-brand { display: none; }
  html.nav-collapsed .app-nav-items { gap: 8px; }
  html.nav-collapsed .app-nav-group-label { height: 1px; margin: 0 6px 6px; padding: 0; overflow: hidden; color: transparent; background: var(--divider); }
  html.nav-collapsed .app-nav-item { justify-content: center; gap: 0; padding: 0; }
  html.nav-collapsed .app-nav-item span,
  html.nav-collapsed .app-nav-user { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
  html.nav-collapsed .app-nav-footer { flex-direction: column; gap: 8px; padding: 12px 0 0; }
  html.nav-collapsed .app-nav-kb-icon,
  html.nav-collapsed .app-nav-kb-select { display: none; }
  html.nav-collapsed .app-nav-kb-rail { display: flex; }
}
/* No slide animation when a page first loads with the sidebar collapsed. */
html.nav-no-anim .app-nav, html.nav-no-anim .app-main { transition: none !important; }

/* ── Shared page primitives ──
   Every non-chat page uses the same frame: a title, one line of subtitle,
   an optional filter row, then content in white cards. */
.ui-page { max-width: 1240px; margin: 0 auto; padding: 28px 32px 48px; box-sizing: border-box; }
.ui-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 20px; margin-bottom: 20px; flex-wrap: wrap; }
.ui-title { margin: 0; font-size: 26px; font-weight: 700; letter-spacing: -.01em; line-height: 1.2; color: var(--text); }
.ui-sub { margin: 4px 0 0; font-size: 14px; line-height: 1.5; color: var(--text-muted); max-width: 680px; }
.ui-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.ui-card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; padding: 18px 20px 12px; }
.ui-card-title { margin: 0; font-size: 16px; font-weight: 700; color: var(--text); }
.ui-card-sub { display: block; margin-top: 2px; font-size: 13px; color: var(--text-muted); }

.ui-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; box-sizing: border-box;
  min-height: 40px; padding: 0 16px; border-radius: 10px; border: 1px solid var(--border-strong);
  background: var(--surface); color: var(--text); font-size: 14px; font-weight: 600; line-height: 1.2;
  text-decoration: none; cursor: pointer; transition: background .15s, border-color .15s, color .15s;
}
.ui-btn:hover:not(:disabled) { border-color: var(--primary-light); background: var(--bg); }
.ui-btn:disabled { cursor: not-allowed; opacity: .5; }
.ui-btn svg { width: 16px; height: 16px; flex-shrink: 0; }
.ui-btn-primary { border-color: var(--primary); background: var(--primary); color: #fff; }
.ui-btn-primary:hover:not(:disabled) { border-color: var(--primary-dark); background: var(--primary-dark); color: #fff; }
.ui-btn-danger { color: var(--error-text); border-color: var(--error-border); }
.ui-btn-danger:hover:not(:disabled) { background: var(--error-bg); border-color: var(--error-text); }
.ui-btn-solid-danger { border-color: var(--error-text); background: var(--error-text); color: #fff; }
.ui-btn-solid-danger:hover:not(:disabled) { background: #8E1B12; border-color: #8E1B12; color: #fff; }
.ui-btn-quiet { border-color: transparent; background: transparent; color: var(--text-body); }
.ui-btn-lg { min-height: 44px; padding: 0 20px; font-size: 15px; }

.ui-segmented { display: inline-flex; padding: 3px; gap: 2px; background: #E9ECE5; border-radius: 10px; }
.ui-segmented > button, .ui-segmented > a {
  min-height: 34px; padding: 0 12px; border: 0; border-radius: var(--radius-sm); background: transparent;
  color: var(--text-muted); font-size: 13px; font-weight: 600; cursor: pointer; text-decoration: none;
  display: inline-flex; align-items: center;
}
.ui-segmented > [aria-pressed="true"], .ui-segmented > [aria-current="page"] {
  background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(27,36,24,.12);
}

/* Page buttons above a long list (uiPagedList in nav.js). */
.ui-pager { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-bottom: 12px; }
.ui-pager-btn {
  min-width: 34px; min-height: 34px; padding: 0 10px; border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm); background: var(--surface); color: var(--text);
  font: inherit; font-size: 13px; font-weight: 600; cursor: pointer;
}
.ui-pager-btn:hover:not(:disabled) { border-color: var(--primary-light); color: var(--primary); }
.ui-pager-btn:disabled { cursor: not-allowed; opacity: .45; }
.ui-pager-btn[aria-current="page"], .ui-pager-btn[aria-pressed="true"] {
  border-color: var(--primary); background: var(--primary); color: #fff;
}
.ui-pager-all { margin-left: auto; }

.ui-chip {
  display: inline-flex; align-items: center; gap: 4px; padding: 2px 10px; border-radius: 999px;
  font-size: 12px; font-weight: 600; white-space: nowrap; background: var(--divider); color: var(--text-body);
}
.ui-chip.is-good { background: var(--green-bg); color: var(--primary-dark); }
.ui-chip.is-warn { background: var(--amber-bg); color: var(--amber-text); }
.ui-chip.is-info { background: var(--info-bg); color: var(--info-text); }

.ui-field { display: flex; flex-direction: column; gap: 6px; }
.ui-label { font-size: 14px; font-weight: 600; color: var(--text); }
.ui-hint { margin: 0; font-size: 13px; line-height: 1.45; color: var(--text-muted); }
.ui-input, .ui-select, .ui-textarea {
  min-height: 44px; padding: 0 12px; border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text); font-size: 15px; box-sizing: border-box;
}
.ui-textarea { padding: 10px 12px; line-height: 1.5; resize: vertical; }
.ui-input:focus, .ui-select:focus, .ui-textarea:focus {
  outline: none; border-color: var(--primary-light); box-shadow: 0 0 0 3px rgba(107,153,80,.18);
}

/* Confirmation dialog opened by uiConfirm() in nav.js. */
.ui-dialog {
  /* margin: auto centres the modal even on pages that reset margins to 0. */
  width: min(420px, calc(100% - 32px)); margin: auto; padding: 24px; border: 0; border-radius: 14px;
  background: var(--surface); color: var(--text); font-family: var(--font); box-shadow: var(--shadow-pop);
}
.ui-dialog::backdrop { background: rgba(27, 36, 24, .36); }
.ui-dialog-title { margin: 0 0 8px; font-size: 18px; font-weight: 700; line-height: 1.3; }
.ui-dialog-text { margin: 0; font-size: 14px; line-height: 1.5; color: var(--text-body); }
.ui-dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }

/* Status message shown by uiToast() in nav.js. Centred over the content
   area, not the whole window, when the sidebar is showing. */
.ui-toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom, 0px)); z-index: 60;
  width: max-content; max-width: min(480px, calc(100% - 32px)); box-sizing: border-box;
  padding: 11px 18px; border-radius: 10px; background: var(--text); color: #fff;
  font-family: var(--font); font-size: 14px; font-weight: 500; line-height: 1.4; text-align: center;
  box-shadow: 0 8px 24px rgba(27, 36, 24, .22);
  opacity: 0; transform: translate(-50%, 8px); pointer-events: none;
  transition: opacity .2s, transform .2s;
}
.ui-toast.show { opacity: 1; transform: translate(-50%, 0); }
.ui-toast.is-error { background: var(--error-text); }
@media (min-width: 861px) {
  .ui-toast { left: calc(50% + var(--nav-w) / 2); max-width: min(480px, calc(100% - var(--nav-w) - 32px)); }
}

.ui-empty {
  margin: 0; padding: 24px 16px; border: 1px dashed var(--border-strong); border-radius: 10px;
  color: var(--text-muted); text-align: center; font-size: 14px; line-height: 1.5;
}

@media (max-height: 720px) {
  .app-nav { gap: 12px; padding-top: 14px; }
  .app-nav-items { gap: 10px; }
  .app-nav-item { min-height: 34px; }
}

@media (prefers-reduced-motion: reduce) {
  .app-nav, .app-nav-backdrop, .app-nav-item, .app-nav-logout, .app-nav-toggle, .app-main, .ui-toast {
    transition-duration: .01ms;
  }
}

@media (max-width: 860px) {
  .app-nav { transform: translateX(-100%); }
  .app-nav.open { transform: translateX(0); box-shadow: 4px 0 24px rgba(0,0,0,.18); }
  .app-main { margin-left: 0; }
  .app-nav-toggle { display: none; }
  .app-topbar { display: flex; }
  .ui-page { padding: 20px 16px 40px; }
}

/* Touch screens: every control is at least 44px tall so a thumb can hit it
   reliably. Comes last so it also beats the short-screen sidebar compaction. */
@media (max-width: 860px), (pointer: coarse) {
  .app-nav-item, .app-nav-kb-select { min-height: 44px; }
  .app-nav-logout, .app-topbar-burger { width: 44px; height: 44px; }
  .ui-btn { min-height: 44px; }
  /* Page buttons and form fields, whatever size each page gives them.
     Icon-only buttons also need a 44px width; pages set that themselves.
     The :not() clauses exclude what they name and also raise specificity
     above two-class page rules such as ".filters .ui-select". */
  .app-main button:not(.ui-segmented > *), .app-main select:not([multiple]),
  .app-main input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="file"]) {
    min-height: 44px;
  }
  .app-nav-brand { min-height: 44px; }
  /* Segment buttons keep their look; the tappable area reaches the edges
     of the 3px track, making it 44px tall. */
  .ui-segmented > button, .ui-segmented > a { position: relative; min-height: 38px; }
  .ui-segmented > button::after, .ui-segmented > a::after { content: ""; position: absolute; inset: -3px 0; }
}
