/* Combat-GPT — shell stylesheet: design tokens, reset, header band, controls,
 * tables, banners, toasts, empty/loading states.
 *
 * Loaded on every page. Page-specific rules live in chat.css (trainee chat and
 * the source viewer) and console.css (instructor and admin pages).
 *
 * Design direction — "staff paper":
 *   off-white paper ground, a deep slate command band, one brass accent used
 *   only for primary actions and the instructor-verified badge, and micro-caps
 *   labels for structure. The signature device is the provenance gutter: the
 *   left rule on every answer card states the evidential standing of that
 *   answer (see chat.css). System fonts only; no web fonts, no CDN.
 */

/* ===================================================================== *
 * 1. Tokens
 * ===================================================================== */

:root {
  /* Command band */
  --color-header-bg: #16202b;
  --color-header-bg-alt: #1f2c3a;
  --color-header-text: #eceef1;
  --color-header-text-muted: #93a0ae;
  --color-header-rule: #2b3a4a;

  /* Paper */
  --color-bg: #f4f2ec;
  --color-surface: #fffdf9;
  --color-surface-alt: #ebe7dd;
  --color-surface-sunk: #f0ede4;
  --color-border: #d8d2c4;
  --color-border-strong: #b7ae9b;

  /* Ink */
  --color-text: #1a1f26;
  --color-text-muted: #5a616d;
  --color-text-faint: #838b98;

  /* The one accent: regimental brass. Primary actions + verified badge only. */
  --color-accent: #9a6614;
  --color-accent-strong: #7d5310;
  --color-accent-contrast: #fffdf7;
  --color-accent-wash: #f4e8d4;

  --color-danger: #9c2f2c;
  --color-danger-wash: #f7e4e2;
  --color-warning: #7f6110;
  --color-warning-wash: #f7eecb;
  --color-success: #2b6645;
  --color-success-wash: #e2efe7;
  --color-note: #2f4c63;
  --color-note-wash: #e8eef3;

  --font-system: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans",
    "Noto Sans Devanagari", Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace;

  --text-xs: 0.75rem;
  --text-sm: 0.8125rem;
  --text-base: 0.9375rem;
  --text-md: 1.0625rem;
  --text-lg: 1.1875rem;
  --text-xl: 1.5rem;

  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.5rem;
  --space-6: 2rem;
  --space-7: 3rem;

  --radius-sm: 3px;
  --radius-md: 6px;
  --radius-lg: 10px;

  --shadow-sm: 0 1px 2px rgba(22, 32, 43, 0.07);
  --shadow-md: 0 6px 20px rgba(22, 32, 43, 0.12);
  --shadow-panel: -6px 0 24px rgba(22, 32, 43, 0.13);

  --duration-fast: 110ms;
  --duration-normal: 220ms;
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);

  --header-height: 56px;
  --footer-height: 30px;
  --measure: 68ch;
}

/* ===================================================================== *
 * 2. Reset and base
 * ===================================================================== */

*, *::before, *::after { box-sizing: border-box; }

/* Components below set their own `display`, which would otherwise beat the
 * user-agent rule for [hidden]. Everything in this UI toggles visibility with
 * the hidden attribute, so it has to win. */
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--font-system);
  font-size: var(--text-base);
  line-height: 1.55;
  color: var(--color-text);
  background: var(--color-bg);
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4 { margin: 0; font-weight: 600; letter-spacing: -0.01em; line-height: 1.25; }
h1 { font-size: var(--text-xl); }
h2 { font-size: var(--text-lg); }
h3 { font-size: var(--text-md); }
p { margin: 0 0 var(--space-3); }
p:last-child { margin-bottom: 0; }

a { color: var(--color-accent-strong); }

button, input, textarea, select { font: inherit; color: inherit; }

table { border-collapse: collapse; width: 100%; }

img { max-width: 100%; }

:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
  border-radius: 2px;
}

.app-header :focus-visible { outline-color: #d7ab63; }

.visually-hidden {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: absolute;
  left: var(--space-3);
  top: -3rem;
  z-index: 60;
  background: var(--color-surface);
  color: var(--color-text);
  padding: var(--space-2) var(--space-4);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-sm);
  text-decoration: none;
  transition: top var(--duration-fast) var(--ease-out);
}
.skip-link:focus { top: var(--space-2); }

/* Micro-caps label — the recurring structural device across the whole UI. */
.label-caps {
  font-size: var(--text-xs);
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--color-text-muted);
}

.helper-text {
  font-size: var(--text-xs);
  line-height: 1.5;
  color: var(--color-text-muted);
  margin: 2px 0 0;
}

/* Inline form/validation message. A failed request explains itself next to the
 * control that failed rather than in a toast the reader has to catch. */
.error-text {
  font-size: var(--text-xs);
  line-height: 1.5;
  color: var(--color-danger);
  margin: var(--space-2) 0 0;
}
.error-text:empty { display: none; }

.section-note { margin-bottom: var(--space-4); }
.push-end { margin-left: auto; }

.mono { font-family: var(--font-mono); font-size: 0.9em; }

/* ===================================================================== *
 * 3. Header band
 * ===================================================================== */

.app-header {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: stretch;
  gap: var(--space-5);
  height: var(--header-height);
  padding: 0 var(--space-5);
  background: var(--color-header-bg);
  border-bottom: 1px solid var(--color-header-rule);
  color: var(--color-header-text);
}

.app-header__brand {
  display: flex;
  flex-direction: column;
  justify-content: center;
  font-size: var(--text-md);
  font-weight: 650;
  letter-spacing: -0.01em;
  white-space: nowrap;
}
.app-header__brand small {
  display: block;
  font-size: var(--text-xs);
  font-weight: 400;
  letter-spacing: 0.02em;
  color: var(--color-header-text-muted);
}

.app-nav { display: flex; align-items: stretch; gap: var(--space-1); min-width: 0; overflow-x: auto; }

.app-nav__link {
  display: flex;
  align-items: center;
  padding: 0 var(--space-3);
  font-size: var(--text-sm);
  color: var(--color-header-text-muted);
  text-decoration: none;
  white-space: nowrap;
  border-bottom: 2px solid transparent;
  transition: color var(--duration-fast) var(--ease-out);
}
.app-nav__link:hover { color: var(--color-header-text); }
.app-nav__link--active {
  color: var(--color-header-text);
  border-bottom-color: var(--color-accent);
  font-weight: 600;
}

.app-header__spacer { flex: 1; }

.app-header__meta {
  display: flex;
  align-items: center;
  gap: var(--space-4);
  margin-left: auto;
  min-width: 0;
}

.profile-chip {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-1) var(--space-3);
  border: 1px solid var(--color-header-rule);
  border-radius: 999px;
  background: var(--color-header-bg-alt);
  font-size: var(--text-xs);
  color: var(--color-header-text-muted);
  white-space: nowrap;
}
.profile-chip b { color: var(--color-header-text); font-weight: 600; }

.app-header__user {
  display: flex;
  flex-direction: column;
  justify-content: center;
  line-height: 1.3;
  font-size: var(--text-sm);
  min-width: 0;
}
.app-header__user-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.app-header__user-role {
  font-size: var(--text-xs);
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--color-header-text-muted);
}

.logout-form { margin: 0; display: flex; align-items: center; }

.app-main { min-height: calc(100vh - var(--header-height) - var(--footer-height)); }

.app-footer {
  display: flex;
  align-items: center;
  gap: var(--space-4);
  height: var(--footer-height);
  padding: 0 var(--space-5);
  font-size: var(--text-xs);
  color: var(--color-text-faint);
  border-top: 1px solid var(--color-border);
  background: var(--color-surface-sunk);
}

/* ===================================================================== *
 * 4. Buttons
 * ===================================================================== */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-4);
  font-size: var(--text-sm);
  font-weight: 550;
  line-height: 1.2;
  color: var(--color-text);
  background: var(--color-surface);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-md);
  cursor: pointer;
  white-space: nowrap;
  transition: background var(--duration-fast) var(--ease-out),
              border-color var(--duration-fast) var(--ease-out);
}
.btn:hover { background: var(--color-surface-alt); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

.btn--primary {
  background: var(--color-accent);
  border-color: var(--color-accent-strong);
  color: var(--color-accent-contrast);
}
.btn--primary:hover { background: var(--color-accent-strong); }
.btn--primary:disabled:hover { background: var(--color-accent); }

.btn--ghost { background: transparent; border-color: transparent; color: var(--color-text-muted); }
.btn--ghost:hover { background: var(--color-surface-alt); color: var(--color-text); }
.app-header .btn--ghost { color: var(--color-header-text-muted); }
.app-header .btn--ghost:hover { color: var(--color-header-text); background: rgba(255, 255, 255, 0.09); }

.btn--danger { border-color: var(--color-danger); color: var(--color-danger); background: var(--color-surface); }
.btn--danger:hover { background: var(--color-danger-wash); }

.btn--sm { padding: var(--space-1) var(--space-3); font-size: var(--text-xs); }
.btn--block { width: 100%; }

/* ===================================================================== *
 * 5. Forms and segmented controls
 * ===================================================================== */

.field { margin-bottom: var(--space-4); display: flex; flex-direction: column; gap: var(--space-1); }
.field > label {
  font-size: var(--text-xs);
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--color-text-muted);
}

.input, textarea.input, select.input {
  width: 100%;
  padding: var(--space-2) var(--space-3);
  background: var(--color-surface);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-md);
  font-size: var(--text-base);
  transition: border-color var(--duration-fast) var(--ease-out);
}
.input:focus { border-color: var(--color-accent); }
.input:disabled { background: var(--color-surface-sunk); color: var(--color-text-faint); cursor: not-allowed; }
textarea.input { resize: vertical; line-height: 1.5; }
select.input { width: auto; min-width: 8rem; }

.segmented {
  display: inline-flex;
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-md);
  overflow: hidden;
  background: var(--color-surface);
}
.segmented__option {
  appearance: none;
  padding: var(--space-2) var(--space-3);
  font-size: var(--text-sm);
  font-weight: 550;
  color: var(--color-text-muted);
  background: transparent;
  border: none;
  border-right: 1px solid var(--color-border);
  cursor: pointer;
  transition: background var(--duration-fast) var(--ease-out), color var(--duration-fast) var(--ease-out);
}
.segmented__option:last-child { border-right: none; }
.segmented__option:hover { background: var(--color-surface-alt); color: var(--color-text); }
.segmented__option[aria-pressed="true"] {
  background: var(--color-header-bg);
  color: var(--color-header-text);
}
.segmented:has(.segmented__option:disabled) { opacity: 0.55; }
.segmented__option:disabled { cursor: not-allowed; }

.checkbox-row {
  display: flex;
  align-items: flex-start;
  gap: var(--space-2);
  font-size: var(--text-sm);
  cursor: pointer;
}
.checkbox-row input { margin-top: 0.25em; flex: none; }

.checkbox-pill {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-xs);
  background: var(--color-surface-alt);
  border: 1px solid var(--color-border);
  border-radius: 999px;
  padding: var(--space-1) var(--space-3);
  cursor: pointer;
}
.checkbox-pill:has(input:checked) {
  background: var(--color-accent-wash);
  border-color: var(--color-accent);
  color: var(--color-accent-strong);
}

/* ===================================================================== *
 * 6. Banners, toasts, dialogs
 * ===================================================================== */

.banner {
  display: flex;
  align-items: baseline;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-5);
  font-size: var(--text-sm);
  border-bottom: 1px solid var(--color-border);
}
.banner--warning { background: var(--color-warning-wash); color: var(--color-warning); }
.banner__label {
  font-size: var(--text-xs);
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  white-space: nowrap;
}

#toast-region {
  position: fixed;
  right: var(--space-5);
  bottom: var(--space-5);
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  max-width: 380px;
}

.toast {
  padding: var(--space-4);
  background: var(--color-header-bg);
  color: var(--color-header-text);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  font-size: var(--text-sm);
}
.toast__title { font-weight: 650; margin-bottom: var(--space-1); }
.toast__message { color: var(--color-header-text-muted); }
.toast__dismiss {
  margin-top: var(--space-3);
  background: transparent;
  border: 1px solid var(--color-header-rule);
  color: var(--color-header-text-muted);
  border-radius: var(--radius-sm);
  padding: var(--space-1) var(--space-3);
  font-size: var(--text-xs);
  cursor: pointer;
}
.toast__dismiss:hover { color: var(--color-header-text); }

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 55;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-5);
  background: rgba(22, 32, 43, 0.45);
}

.modal {
  width: 100%;
  max-width: 460px;
  max-height: 90vh;
  overflow-y: auto;
  background: var(--color-surface);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  padding: var(--space-5);
}
.modal__title { margin-bottom: var(--space-2); }
.modal__intro { font-size: var(--text-sm); color: var(--color-text-muted); margin-bottom: var(--space-4); }
.modal__section { margin-bottom: var(--space-4); }
.modal__actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-3);
  margin-top: var(--space-5);
}
.modal__quote {
  background: var(--color-surface-sunk);
  border-left: 2px solid var(--color-border-strong);
  padding: var(--space-3);
  font-size: var(--text-sm);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  margin-bottom: var(--space-4);
}

/* ===================================================================== *
 * 7. Tables, pills, tags
 * ===================================================================== */

.data-table-wrap {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  overflow-x: auto;
}
.data-table { min-width: 640px; }
.data-table th {
  position: sticky;
  top: 0;
  text-align: left;
  padding: var(--space-3) var(--space-4);
  font-size: var(--text-xs);
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--color-text-muted);
  background: var(--color-surface-sunk);
  border-bottom: 1px solid var(--color-border);
  white-space: nowrap;
}
.data-table td {
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--color-border);
  font-size: var(--text-sm);
  vertical-align: top;
}
.data-table tr:last-child td { border-bottom: none; }
.data-table tbody tr:hover { background: var(--color-surface-sunk); }

.sortable-th { padding: 0; }
.sortable-th button {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  width: 100%;
  padding: var(--space-3) var(--space-4);
  background: none;
  border: none;
  font: inherit;
  color: inherit;
  text-transform: inherit;
  letter-spacing: inherit;
  cursor: pointer;
  text-align: left;
}
.sortable-th button:hover { color: var(--color-text); }
.sortable-th__arrow { color: var(--color-text-faint); font-size: 0.8em; }
.sortable-th[aria-sort="ascending"] .sortable-th__arrow,
.sortable-th[aria-sort="descending"] .sortable-th__arrow { color: var(--color-accent); }

.count-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 24px;
  padding: 1px var(--space-2);
  border-radius: 999px;
  background: var(--color-danger-wash);
  color: var(--color-danger);
  font-size: var(--text-xs);
  font-weight: 650;
}
.count-pill--zero { background: var(--color-surface-alt); color: var(--color-text-faint); }

.title-tag {
  display: inline-block;
  background: var(--color-surface-alt);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 1px var(--space-2);
  font-size: var(--text-xs);
  margin: 0 var(--space-1) var(--space-1) 0;
}

.status-dot {
  display: inline-block;
  width: 8px; height: 8px;
  border-radius: 50%;
  margin-right: var(--space-2);
  background: var(--color-text-faint);
}
.status-dot--ok { background: var(--color-success); }
.status-dot--bad { background: var(--color-danger); }

.state-pill {
  display: inline-flex;
  align-items: center;
  padding: 1px var(--space-2);
  border-radius: var(--radius-sm);
  font-size: var(--text-xs);
  font-weight: 600;
  border: 1px solid var(--color-border);
  background: var(--color-surface-alt);
  color: var(--color-text-muted);
}
.state-pill--ok { background: var(--color-success-wash); color: var(--color-success); border-color: transparent; }
.state-pill--busy { background: var(--color-warning-wash); color: var(--color-warning); border-color: transparent; }
.state-pill--bad { background: var(--color-danger-wash); color: var(--color-danger); border-color: transparent; }

/* ===================================================================== *
 * 8. Empty, loading and skeleton states
 * ===================================================================== */

.empty-state {
  padding: var(--space-6) var(--space-5);
  text-align: center;
  color: var(--color-text-muted);
  font-size: var(--text-sm);
}
.empty-state__title { font-weight: 600; color: var(--color-text); margin-bottom: var(--space-1); }
.empty-state__hint { font-size: var(--text-xs); color: var(--color-text-faint); }

.loading-state {
  padding: var(--space-5);
  color: var(--color-text-faint);
  font-size: var(--text-sm);
}

.skeleton {
  display: block;
  height: 0.75rem;
  margin-bottom: var(--space-2);
  border-radius: var(--radius-sm);
  background: linear-gradient(90deg, var(--color-surface-alt) 25%, var(--color-surface-sunk) 50%, var(--color-surface-alt) 75%);
  background-size: 400% 100%;
  animation: skeleton-sweep 1.4s ease-in-out infinite;
}
.skeleton--wide { width: 100%; }
.skeleton--half { width: 55%; }
.skeleton--short { width: 30%; }
.skeleton--tall { height: 2.25rem; }
.skeleton-block { padding: var(--space-4); }

@keyframes skeleton-sweep {
  from { background-position: 100% 0; }
  to { background-position: 0 0; }
}

/* ===================================================================== *
 * 9. Login
 * ===================================================================== */

.auth-shell {
  min-height: calc(100vh - var(--header-height) - var(--footer-height));
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-6);
}

.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: var(--space-6);
}
.auth-card h1 { font-size: var(--text-lg); margin-bottom: var(--space-1); }
.auth-card__subtitle {
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  margin-bottom: var(--space-5);
}

.form-error {
  background: var(--color-danger-wash);
  color: var(--color-danger);
  border-radius: var(--radius-sm);
  padding: var(--space-3);
  font-size: var(--text-sm);
  margin-bottom: var(--space-4);
}

.demo-accounts {
  margin-top: var(--space-5);
  padding-top: var(--space-4);
  border-top: 1px solid var(--color-border);
}
.demo-accounts__list {
  list-style: none;
  margin: var(--space-2) 0 0;
  padding: 0;
  font-size: var(--text-sm);
}
.demo-accounts__list li {
  display: flex;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-1) 0;
}
.demo-accounts__list b { font-family: var(--font-mono); font-size: 0.9em; font-weight: 600; }
.demo-accounts__list span { color: var(--color-text-muted); }

/* ===================================================================== *
 * 10. Generic page shell (instructor + admin)
 * ===================================================================== */

.page-shell { padding: var(--space-6) var(--space-5); max-width: 1180px; margin: 0 auto; }
.page-shell--wide { max-width: 1440px; }

.page-shell__header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--space-4);
  flex-wrap: wrap;
  margin-bottom: var(--space-5);
  padding-bottom: var(--space-4);
  border-bottom: 1px solid var(--color-border);
}
.page-shell__subtitle { color: var(--color-text-muted); font-size: var(--text-sm); margin-top: var(--space-1); }

.section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  flex-wrap: wrap;
  margin: var(--space-6) 0 var(--space-3);
}
.section-heading:first-child { margin-top: 0; }
.section-heading__actions { display: flex; align-items: center; gap: var(--space-3); }

/* ===================================================================== *
 * 11. Motion preference
 * ===================================================================== */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}

/* ===================================================================== *
 * 12. Narrow screens (usable at 1024 px, degrades below)
 * ===================================================================== */

@media (max-width: 1024px) {
  .app-header { padding: 0 var(--space-4); gap: var(--space-3); }
  .app-header__brand small { display: none; }
  .profile-chip { display: none; }
  .page-shell { padding: var(--space-5) var(--space-4); }
}

@media (max-width: 720px) {
  .app-header__user { display: none; }
  #toast-region { left: var(--space-4); right: var(--space-4); max-width: none; }
}
