/*
 * Hand-rolled application styles (no framework — SPEC: server-rendered Hotwire, "the UI bar is
 * better than an n8n form"). Served by Propshaft as-is. Brand navy follows the KLO document
 * branding (archive/scripts/branding.py BRAND_NAVY 34,49,96).
 *
 * 26/08/2026 UX pass (Kurt's smoke-test feedback): one visual system — top bar, page header,
 * cards, a numbered stepper for the live progress panels, status pills with a colour per
 * meaning, quiet data tables with whole-row links, grouped forms — kept deliberately plain.
 * Colour carries meaning consistently everywhere: navy = brand/in flight, amber = needs a
 * person, green = done, red = failed, grey = inactive/not captured.
 */

:root {
  --navy: #223160;
  --navy-dark: #1b1559;
  --navy-soft: #e8ecf7;
  --ink: #1f2430;
  --ink-soft: #3d4658;
  --muted: #667085;
  --line: #dde2ea;
  --line-strong: #c9d0dc;
  --paper: #f4f5f8;
  --white: #ffffff;
  --green: #1d7a46;
  --green-soft: #e6f4ea;
  --amber: #a05e03;
  --amber-soft: #fff4e0;
  --red: #b42318;
  --red-soft: #fdecea;
  --blue: #1f5fbf;
  --blue-soft: #e8f0fd;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 1px 2px rgba(20, 30, 60, 0.06), 0 1px 8px rgba(20, 30, 60, 0.04);
  --focus: 0 0 0 3px rgba(34, 49, 96, 0.25);
}

* { box-sizing: border-box; }

/* The hidden attribute must beat every display rule below (the notify opt-in button is
   toggled purely by that attribute). */
[hidden] { display: none !important; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  color: var(--ink);
  background: var(--paper);
  line-height: 1.5;
}

h1, h2, h3, h4 { color: var(--navy); line-height: 1.25; margin: 0 0 0.5rem; }
h1 { font-size: 1.6rem; font-weight: 700; letter-spacing: -0.01em; }
h2 { font-size: 1.15rem; font-weight: 700; }
h3 { font-size: 0.98rem; font-weight: 700; }
h4 { font-size: 0.92rem; font-weight: 700; color: var(--ink-soft); }
p { margin: 0 0 0.75rem; }
a { color: var(--navy); }
:focus-visible { outline: none; box-shadow: var(--focus); border-radius: var(--radius-sm); }

/* --- Top bar --- */

.site-header {
  display: flex;
  align-items: center;
  gap: 1.75rem;
  padding: 0 1.5rem;
  height: 3.25rem;
  background: var(--navy);
  color: var(--white);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 0.6rem;
  color: var(--white);
  font-weight: 700;
  text-decoration: none;
  white-space: nowrap;
}

.brand-mark {
  width: 1.5rem;
  height: 1.5rem;
  border-radius: 6px;
  background: var(--white);
  color: var(--navy);
  font-size: 0.7rem;
  font-weight: 800;
  display: inline-grid;
  place-items: center;
  letter-spacing: 0.02em;
}

.site-nav { display: flex; gap: 0.25rem; flex: 1; }

.site-nav a {
  color: rgba(255, 255, 255, 0.82);
  text-decoration: none;
  white-space: nowrap;
  padding: 0.4rem 0.7rem;
  border-radius: var(--radius-sm);
  font-weight: 500;
}

.site-nav a:hover { color: var(--white); background: rgba(255, 255, 255, 0.1); }
.site-nav a.active { color: var(--white); background: rgba(255, 255, 255, 0.16); }

.session-controls { display: flex; align-items: center; gap: 0.75rem; font-size: 0.88rem; }
.current-user { display: flex; align-items: center; gap: 0.45rem; opacity: 0.95; }
.role-chip {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 0.1rem 0.45rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.16);
}

.button-link {
  background: none;
  border: 1px solid rgba(255, 255, 255, 0.35);
  color: var(--white);
  border-radius: var(--radius-sm);
  cursor: pointer;
  padding: 0.3rem 0.65rem;
  font: inherit;
  font-size: 0.85rem;
}
.button-link:hover { background: rgba(255, 255, 255, 0.12); }

/* --- Layout --- */

.container { max-width: 68rem; margin: 0 auto; padding: 1.5rem 1.5rem 4rem; }

.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 1.25rem;
}

.page-header h1 { margin-bottom: 0.15rem; }
.page-title-row { display: flex; align-items: center; gap: 0.6rem; flex-wrap: wrap; }
.lede { color: var(--muted); margin: 0; max-width: 52rem; }
.page-actions { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }

.page-subheader {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 0.75rem;
}
.page-subheader h2, .page-subheader h3 { margin: 0; }

.review-actions, .row-actions { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }

.back-link {
  display: inline-block;
  color: var(--muted);
  text-decoration: none;
  font-size: 0.88rem;
  margin-bottom: 0.5rem;
}
.back-link:hover { color: var(--navy); text-decoration: underline; }

/* --- Cards --- */

.card,
.progress-panel,
.review-panel,
.feedback-panel,
.classification-confirm-panel,
.classification-panel,
.compliance-panel,
.draft-preview,
.submission-meta,
.user-meta,
.auth-card,
.edit-section,
.form-section {
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.progress-panel,
.review-panel,
.feedback-panel,
.classification-confirm-panel,
.classification-panel,
.compliance-panel,
.draft-preview { padding: 1.1rem 1.35rem; margin: 0 0 1rem; }

.classification-panel, .compliance-panel, .draft-preview { box-shadow: none; }

/* --- Flash + banners --- */

.flash, .banner {
  display: flex;
  align-items: flex-start;
  gap: 0.6rem;
  padding: 0.65rem 0.95rem;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  margin: 0 0 1rem;
  font-size: 0.93rem;
}
.flash::before, .banner::before { font-weight: 700; flex: none; }
.flash-notice { background: var(--green-soft); color: var(--green); border-color: #b7e0c4; }
.flash-notice::before { content: "✓"; }
.flash-alert { background: var(--red-soft); color: var(--red); border-color: #f6c6c1; }
.flash-alert::before { content: "!"; }

.banner-test { background: var(--amber-soft); border-color: #ecd39d; color: var(--amber); }
.banner-test::before { content: "⚑"; }
.banner-info { background: var(--blue-soft); border-color: #c3d0ef; color: var(--navy); }
.banner-info::before { content: "i"; font-family: Georgia, serif; }
.banner-warning { background: var(--amber-soft); border-color: #ecd39d; color: var(--amber); }
.banner-warning::before { content: "!"; }
.banner ul { margin: 0.4rem 0 0; }

/* --- Buttons + forms --- */

.button-primary,
.button-secondary,
.button-ghost,
.button-danger,
input[type="submit"] {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  min-height: 2.25rem;
  padding: 0.4rem 0.95rem;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  font: inherit;
  font-size: 0.93rem;
  font-weight: 600;
  line-height: 1.2;
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.12s ease, border-color 0.12s ease;
}

.button-primary, input[type="submit"] { background: var(--navy); color: var(--white); }
.button-primary:hover, input[type="submit"]:hover { background: var(--navy-dark); }
.button-secondary { background: var(--white); color: var(--navy); border-color: var(--line-strong); }
.button-secondary:hover { border-color: var(--navy); background: var(--navy-soft); }
.button-ghost { background: transparent; color: var(--navy); border-color: transparent; padding-inline: 0.6rem; }
.button-ghost:hover { background: var(--navy-soft); }
.button-danger { background: var(--white); color: var(--red); border-color: #f0b7b1; }
.button-danger:hover { background: var(--red-soft); border-color: var(--red); }
.button-small { min-height: 1.9rem; padding: 0.2rem 0.65rem; font-size: 0.85rem; }

form.button_to { display: inline; margin: 0; }

.actions { margin: 1.25rem 0 0; display: flex; gap: 0.6rem; align-items: center; flex-wrap: wrap; }

.form-section {
  padding: 1rem 1.25rem 0.5rem;
  margin: 0 0 1rem;
  border: 1px solid var(--line);
  min-width: 0;
}
.form-section legend, .edit-section legend {
  font-weight: 700;
  color: var(--navy);
  padding: 0 0.4rem;
  font-size: 0.98rem;
}
.form-section > .hint:first-of-type { margin-top: -0.25rem; }

.field { margin-bottom: 1rem; }
.field label, .field .label { display: block; font-weight: 600; margin-bottom: 0.3rem; font-size: 0.93rem; }
.field-checkbox { display: grid; grid-template-columns: auto 1fr; gap: 0.15rem 0.6rem; align-items: center; }
.field-checkbox input[type="checkbox"] { width: 1.05rem; height: 1.05rem; margin: 0; accent-color: var(--navy); }
.field-checkbox label { display: inline; margin: 0; }
.field-checkbox .hint { grid-column: 2; margin: 0; }
.field-inline { display: flex; gap: 1rem; flex-wrap: wrap; }
.field-inline > .field { flex: 1 1 14rem; }

input[type="text"],
input[type="email"],
input[type="password"],
input[type="date"],
input[type="file"],
select,
textarea {
  width: 100%;
  max-width: 34rem;
  padding: 0.5rem 0.65rem;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  font: inherit;
  background: var(--white);
  color: var(--ink);
}
input[type="file"] { padding: 0.4rem; }
select { appearance: auto; }
textarea { max-width: 100%; min-height: 4.5rem; resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--navy); outline: none; box-shadow: var(--focus); }
::placeholder { color: #98a2b3; }

.hint { color: var(--muted); font-size: 0.85rem; margin: 0.3rem 0 0; line-height: 1.45; }
.sub-label { font-weight: 600; margin: 0.5rem 0 0.3rem; font-size: 0.93rem; }

.form-errors {
  background: var(--red-soft);
  border: 1px solid #f6c6c1;
  color: var(--red);
  border-radius: var(--radius-sm);
  padding: 0.65rem 0.95rem;
  margin-bottom: 1rem;
}
.form-errors p { margin: 0 0 0.3rem; font-weight: 600; }
.form-errors ul { margin: 0; padding-left: 1.2rem; }

/* --- Auth --- */

.auth-card { max-width: 24rem; margin: 5rem auto; padding: 1.75rem; }
.auth-brand { display: flex; align-items: center; gap: 0.6rem; margin-bottom: 1rem; }
.auth-brand .brand-mark { background: var(--navy); color: var(--white); width: 2rem; height: 2rem; font-size: 0.85rem; }
.auth-card h1 { font-size: 1.3rem; margin: 0; }
.auth-lede, .auth-help { color: var(--muted); font-size: 0.9rem; }
.auth-card input[type="submit"] { width: 100%; }

/* --- Filter tabs --- */

.filter-tabs {
  display: flex;
  gap: 0.25rem;
  flex-wrap: wrap;
  margin: 0 0 0.9rem;
  padding: 0;
  list-style: none;
}
.filter-tabs a {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.35rem 0.75rem;
  border-radius: 999px;
  text-decoration: none;
  color: var(--ink-soft);
  font-size: 0.9rem;
  font-weight: 500;
  border: 1px solid transparent;
}
.filter-tabs a:hover { background: var(--white); border-color: var(--line); }
.filter-tabs a.active { background: var(--navy); color: var(--white); }
.filter-tabs .count {
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0 0.4rem;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.07);
}
.filter-tabs a.active .count { background: rgba(255, 255, 255, 0.2); }

/* --- Data tables --- */

.table-wrap { overflow-x: auto; }

.submissions-table, .section-table, .edit-rows, .users-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.submissions-table th, .section-table th, .edit-rows th, .users-table th {
  text-align: left;
  background: #f8f9fc;
  color: var(--muted);
  padding: 0.55rem 0.85rem;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  border-bottom: 1px solid var(--line);
}

.submissions-table td, .section-table td, .edit-rows td, .users-table td {
  padding: 0.65rem 0.85rem;
  border-top: 1px solid var(--line);
  font-size: 0.93rem;
  vertical-align: middle;
}
.submissions-table tbody tr:first-child td, .users-table tbody tr:first-child td,
.section-table tbody tr:first-child td, .edit-rows tbody tr:first-child td { border-top: 0; }

.section-table, .edit-rows { box-shadow: none; border-radius: var(--radius-sm); margin: 0.4rem 0; }
.section-table td, .edit-rows td { font-size: 0.9rem; vertical-align: top; }

.row-link { cursor: pointer; }
.row-link:hover td { background: var(--navy-soft); }
.row-link:focus-visible { box-shadow: inset 0 0 0 2px var(--navy); }

.cell-primary { font-weight: 600; color: var(--navy); }
.cell-muted { color: var(--muted); }
.cell-nowrap { white-space: nowrap; }

.empty-state {
  text-align: center;
  color: var(--muted);
  padding: 2.5rem 1rem;
  background: var(--white);
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius);
}

/* --- Status pills --- */

.badge {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.15rem 0.6rem;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
  line-height: 1.5;
  background: var(--navy-soft);
  color: var(--navy);
  white-space: nowrap;
  text-decoration: none;
}
.badge::before {
  content: "";
  width: 0.45rem;
  height: 0.45rem;
  border-radius: 50%;
  background: currentColor;
  flex: none;
}
.badge-plain::before, .badge-test::before, .rag-red::before, .rag-amber::before, .rag-green::before { display: none; }

/* In-flight submission and report states share the navy pill with a pulsing dot. */
.state-uploaded::before, .state-extracting::before, .state-cleansing::before,
.state-structuring::before, .state-sanitising::before, .state-rendering::before,
.state-approved::before, .state-filing::before, .state-classifying::before,
.state-drafting::before, .state-mapping::before, .state-compliance_checking::before {
  animation: pulse 1.4s ease-in-out infinite;
}

.badge-test { background: var(--amber-soft); color: var(--amber); margin-left: 0.4rem; }
.badge-inactive { background: #f0f1f4; color: var(--muted); }

.state-completed, .state-ready { background: var(--green-soft); color: var(--green); }
.state-failed { background: var(--red-soft); color: var(--red); }
.state-in_review, .state-awaiting_classification { background: var(--amber-soft); color: var(--amber); }

.rag-red { background: var(--red-soft); color: var(--red); }
.rag-amber { background: var(--amber-soft); color: var(--amber); }
.rag-green { background: var(--green-soft); color: var(--green); }

@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.25; } }

/* --- Progress panel (stepper) --- */

.progress-state {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
  margin: 0 0 0.75rem;
}
.progress-state .badge { font-size: 0.85rem; padding: 0.25rem 0.75rem; }

.progress-steps {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem 0;
  list-style: none;
  padding: 0;
  margin: 0.25rem 0 0;
  counter-reset: step;
}

.progress-steps .step {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.82rem;
  color: var(--muted);
  padding-right: 0.9rem;
  position: relative;
}
.progress-steps .step::before {
  counter-increment: step;
  content: counter(step);
  width: 1.35rem;
  height: 1.35rem;
  border-radius: 50%;
  border: 1.5px solid var(--line-strong);
  display: inline-grid;
  place-items: center;
  font-size: 0.7rem;
  font-weight: 700;
  background: var(--white);
  flex: none;
}
.progress-steps .step:not(:last-child)::after {
  content: "";
  width: 0.9rem;
  height: 1.5px;
  background: var(--line-strong);
  margin-left: 0.5rem;
}
.progress-steps .step-done { color: var(--green); }
.progress-steps .step-done::before { content: "✓"; background: var(--green-soft); border-color: #b7e0c4; color: var(--green); }
.progress-steps .step-done::after { background: #b7e0c4; }
.progress-steps .step-current { color: var(--navy); font-weight: 700; }
.progress-steps .step-current::before { background: var(--navy); border-color: var(--navy); color: var(--white); animation: pulse 1.4s ease-in-out infinite; }

.notify-enable { margin-top: 0.75rem; font-size: 0.85rem; }
.notify-enable::before { content: "🔔"; }

.review-cue {
  margin: 0.9rem 0 0;
  padding: 0.6rem 0.85rem;
  background: var(--amber-soft);
  border: 1px solid #ecd39d;
  border-radius: var(--radius-sm);
  color: var(--amber);
}
.review-cue a { color: inherit; font-weight: 600; }

.error-panel {
  background: var(--red-soft);
  border: 1px solid #f6c6c1;
  border-radius: var(--radius-sm);
  padding: 0.85rem 1rem;
  margin-top: 0.5rem;
}
.error-panel p { margin-bottom: 0.5rem; }
.error-panel .button-danger { margin-right: 0.5rem; margin-top: 0.25rem; }

.error-message {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.82rem;
  white-space: pre-wrap;
  word-break: break-word;
}

/* --- Meta grids --- */

.submission-meta, .user-meta {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr));
  gap: 0.7rem 1.25rem;
  padding: 1rem 1.25rem;
  margin: 0 0 1rem;
  box-shadow: none;
}
.submission-meta > div, .user-meta > div { min-width: 0; }
.submission-meta dt, .user-meta dt {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  font-weight: 600;
  margin-bottom: 0.1rem;
}
.submission-meta dd, .user-meta dd { margin: 0; font-size: 0.95rem; overflow-wrap: anywhere; }

/* Older inline dl markup (classification panels) still renders as a two-column list. */
.classification-panel .submission-meta,
.classification-confirm-panel .submission-meta {
  grid-template-columns: max-content 1fr;
  gap: 0.35rem 1rem;
  padding: 0.75rem 1rem;
  margin: 0.5rem 0 0.75rem;
  background: #fafbfd;
}
.classification-panel .submission-meta dt,
.classification-confirm-panel .submission-meta dt { text-transform: none; letter-spacing: 0; font-size: 0.9rem; }

/* --- File note review --- */

.filing-status { margin: 0 0 0.75rem; }
.filing-status .hint { margin: 0; }

.missing-sections {
  background: var(--amber-soft);
  border: 1px solid #ecd39d;
  border-radius: var(--radius-sm);
  padding: 0.75rem 1rem;
  margin: 0.5rem 0 1rem;
}
.missing-sections h3 { margin: 0 0 0.25rem; color: var(--amber); }
.missing-sections ul { margin: 0.25rem 0 0; padding-left: 1.2rem; }

.note-index {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  margin: 0 0 1rem;
  padding: 0;
  list-style: none;
}
.note-index a {
  display: inline-block;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  background: var(--paper);
  border: 1px solid var(--line);
  color: var(--ink-soft);
  text-decoration: none;
  font-size: 0.78rem;
}
.note-index a:hover { border-color: var(--navy); color: var(--navy); }
.note-index a.is-empty { color: var(--muted); border-style: dashed; }

.note-section { border-top: 1px solid var(--line); padding: 0.85rem 0; scroll-margin-top: 4rem; }
.note-section:first-child { border-top: 0; }
.note-section h3 { margin: 0 0 0.35rem; display: flex; align-items: center; gap: 0.5rem; }
.note-section p:last-child { margin-bottom: 0; }
.section-prose { white-space: normal; }

.not-captured { color: var(--muted); font-style: italic; }

.section-fields { display: grid; grid-template-columns: max-content 1fr; gap: 0.3rem 1rem; margin: 0; }
.section-fields dt { font-weight: 600; color: var(--muted); font-size: 0.9rem; }
.section-fields dd { margin: 0; }

.section-list { margin: 0.25rem 0; padding-left: 1.25rem; }

/* --- File note editing --- */

.edit-section { padding: 0.9rem 1.25rem 0.5rem; margin-bottom: 1rem; box-shadow: none; }
.edit-rows input[type="text"] { max-width: none; padding: 0.35rem 0.5rem; }
.sticky-actions {
  position: sticky;
  bottom: 0;
  background: rgba(244, 245, 248, 0.92);
  backdrop-filter: blur(4px);
  padding: 0.75rem 0;
  margin-top: 0.5rem;
  border-top: 1px solid var(--line);
}

/* --- Reports --- */

.reports-panel { margin: 0 0 1rem; }
.report-version { color: var(--muted); font-size: 0.85rem; font-weight: 500; margin-left: 0.4rem; }
.classification-form .field { max-width: 28rem; }

.compliance-summary { display: flex; align-items: center; gap: 0.6rem; flex-wrap: wrap; }
.compliance-summary-text { color: var(--ink-soft); }
.compliance-findings .finding-fail.finding-red td:nth-child(2) { color: var(--red); font-weight: 600; }
.compliance-findings .finding-fail.finding-amber td:nth-child(2) { color: var(--amber); font-weight: 600; }
.compliance-findings .finding-fail.finding-green td:nth-child(2) { color: var(--green); font-weight: 600; }
.compliance-findings .finding-pass td:nth-child(2) { color: var(--green); font-weight: 600; }

.draft-field { border-top: 1px solid var(--line); padding: 0.75rem 0; scroll-margin-top: 4rem; }
.draft-field:first-of-type { border-top: 0; }
.draft-field h4 { margin: 0 0 0.35rem; }

.placeholder {
  background: var(--amber-soft);
  color: var(--amber);
  padding: 0.05rem 0.3rem;
  border-radius: 3px;
  font-weight: 600;
}

/* --- Responsive --- */

@media (max-width: 720px) {
  .site-header { gap: 0.75rem; padding: 0 0.9rem; height: auto; min-height: 3.25rem; flex-wrap: wrap; }
  .site-nav { order: 3; flex-basis: 100%; padding-bottom: 0.4rem; overflow-x: auto; }
  .container { padding: 1rem 0.9rem 3rem; }
  .submissions-table td, .users-table td { padding: 0.5rem 0.6rem; font-size: 0.88rem; }
  .current-user .name { display: none; }
}
