/* FundingAlt Hub and FundingAlt Analytics: Funding Alternative brand (spec section 12).
   One file, no inline styles anywhere: the CSP forbids them. */

:root {
  --orange: #ea4628;       /* bars, rules, buttons, large headings */
  --orange-text: #c4381d;  /* small orange text (contrast) */
  --cream: #f7ead2;        /* panels */
  --charcoal: #404040;     /* text and top bar */
  --grey: #efeff0;         /* page background */
  --steel: #70a0b4;
  --steel-text: #3f6f82;   /* small steel text (contrast) */
  --sky: #9adbf5;
  --red: #d2232a;          /* errors, failed runs */
  --white: #ffffff;
  --line: #d4d4d7;
  --muted: #666668;

  --font: "Barlow", "Helvetica Neue", Arial, sans-serif;
  --radius: 4px;
}

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

html { font-size: 16px; }

body {
  margin: 0;
  background: var(--grey);
  color: var(--charcoal);
  font-family: var(--font);
  font-weight: 400;
  line-height: 1.45;
}

a { color: var(--orange-text); }
a:hover { text-decoration-thickness: 2px; }

:focus-visible {
  outline: 3px solid var(--charcoal);
  outline-offset: 2px;
}

/* ---- Headings: Black 900 italic caps for the page title, Medium below ---- */

h1, .page-title {
  margin: 0 0 1rem;
  color: var(--orange);
  font-size: 2rem;
  font-weight: 900;
  font-style: italic;
  text-transform: uppercase;
  line-height: 1.1;
  letter-spacing: 0.01em;
}

h2, h3 { font-weight: 500; line-height: 1.2; margin: 1.5rem 0 0.5rem; }
h2 { font-size: 1.4rem; }
h3 { font-size: 1.15rem; }

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

.topbar {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  min-height: 3.75rem;
  padding: 0.5rem 1.5rem;
  background: var(--charcoal);
  border-bottom: 3px solid var(--orange);
  color: var(--white);
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  color: var(--white);
  text-decoration: none;
}

.brand-logo { display: block; height: 2.25rem; width: auto; }

.brand-name {
  padding-left: 0.9rem;
  border-left: 1px solid rgba(255, 255, 255, 0.35);
  font-size: 1.2rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  white-space: nowrap;
}

.nav { display: flex; align-items: stretch; gap: 0.25rem; align-self: stretch; }

.nav a {
  display: flex;
  align-items: center;
  padding: 0 0.85rem;
  color: rgba(255, 255, 255, 0.88);
  font-weight: 500;
  text-decoration: none;
}
.nav a:hover { background: rgba(255, 255, 255, 0.1); color: var(--white); }
.nav a[aria-current="page"] {
  color: var(--white);
  box-shadow: inset 0 -4px 0 var(--orange);
}
.topbar :focus-visible { outline-color: var(--white); }

/* ---- User menu (a <details>: works without scripts) ---- */

.user-menu { position: relative; margin-left: auto; }

.user-menu summary {
  padding: 0.45rem 0.8rem;
  border-radius: var(--radius);
  cursor: pointer;
  font-weight: 500;
  list-style: none;
}
.user-menu summary::-webkit-details-marker { display: none; }
.user-menu summary::after { content: " \25BE"; }
.user-menu summary:hover,
.user-menu[open] summary { background: rgba(255, 255, 255, 0.12); }

.user-menu-panel {
  position: absolute;
  right: 0;
  z-index: 20;
  min-width: 13rem;
  margin-top: 0.4rem;
  padding: 0.5rem 0;
  background: var(--white);
  border: 1px solid var(--line);
  border-top: 3px solid var(--orange);
  border-radius: var(--radius);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.18);
  color: var(--charcoal);
}
.user-menu-panel a,
.user-menu-panel .link-button {
  display: block;
  width: 100%;
  padding: 0.5rem 1rem;
  color: var(--charcoal);
  text-align: left;
  text-decoration: none;
}
.user-menu-panel a:hover,
.user-menu-panel .link-button:hover { background: var(--grey); }
.user-menu-who { margin: 0; padding: 0.25rem 1rem 0.6rem; border-bottom: 1px solid var(--line); color: var(--muted); }
.user-menu-role { text-transform: capitalize; }
.user-menu-panel form { margin: 0; }

/* ---- Page ---- */

.page { max-width: 80rem; margin: 0 auto; padding: 1.75rem 1.5rem 3rem; }
.page-wide { max-width: none; padding: 0; }   /* the output viewer */

.muted { color: var(--muted); }

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

/* ---- Panels and messages ---- */

.panel {
  padding: 1rem 1.25rem;
  background: var(--cream);
  border-radius: var(--radius);
  border-left: 5px solid var(--orange);
}

.card {
  padding: 1rem 1.25rem;
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.flash {
  margin: 0 0 1rem;
  padding: 0.7rem 1rem;
  background: var(--white);
  border: 1px solid var(--line);
  border-left: 5px solid var(--steel);
  border-radius: var(--radius);
}
.flash.error { border-left-color: var(--red); color: var(--red); font-weight: 500; }
.flash.warning { border-left-color: var(--orange); background: var(--cream); }

/* ---- Buttons ----
   White on #ea4628 is 3.9:1, which passes only for large bold text, hence the
   18px bold label. */

.btn, button.btn {
  display: inline-block;
  padding: 0.45rem 1.1rem;
  background: var(--orange);
  border: 2px solid var(--orange);
  border-radius: var(--radius);
  color: var(--white);
  cursor: pointer;
  font-family: var(--font);
  font-size: 1.17rem;
  font-weight: 700;
  line-height: 1.2;
  text-decoration: none;
}
.btn:hover { background: var(--orange-text); border-color: var(--orange-text); }
.btn-secondary { background: var(--white); border-color: var(--charcoal); color: var(--charcoal); }
.btn-secondary:hover { background: var(--charcoal); border-color: var(--charcoal); color: var(--white); }
.btn-danger { background: var(--red); border-color: var(--red); }
.btn-danger:hover { background: #a91b21; border-color: #a91b21; }
.btn[disabled], .btn[aria-disabled="true"] { opacity: 0.5; cursor: not-allowed; }

.link-button {
  padding: 0;
  background: none;
  border: 0;
  color: inherit;
  cursor: pointer;
  font: inherit;
}

/* ---- Forms ---- */

label { display: block; margin: 0 0 0.25rem; font-weight: 500; }

input[type="text"], input[type="email"], input[type="password"], input[type="number"],
input[type="search"], input[type="date"], input[type="time"], select, textarea {
  width: 100%;
  max-width: 28rem;
  padding: 0.5rem 0.65rem;
  background: var(--white);
  border: 1px solid #a9a9ad;
  border-radius: var(--radius);
  color: var(--charcoal);
  font: inherit;
}
input:focus, select:focus, textarea:focus { border-color: var(--charcoal); }
.field { margin: 0 0 1rem; }
input[type="file"] { display: block; width: 100%; max-width: 28rem; padding: 0.5rem 0; font: inherit; }
.hint { margin: 0.25rem 0 0; color: var(--muted); }
.field-error { margin: 0.25rem 0 0; color: var(--red); font-weight: 500; }

/* ---- Sign-in, 2FA and account pages ---- */

.auth { max-width: 32rem; margin: 1.5rem auto; }
.auth h1 { font-size: 1.8rem; }
.auth form { margin: 1rem 0; }

code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.95em;
}
.secret { word-break: break-all; }

.steps { padding-left: 1.25rem; }
.steps li { margin: 0 0 0.75rem; }
.qr-box { margin: 0.75rem 0; }
svg.qr { width: 14rem; height: auto; }   /* the QR has a viewBox, no fixed size */

.recovery { margin: 1rem 0; }
.recovery-codes {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, max-content));
  gap: 0.35rem 2.5rem;
  margin: 0.75rem 0 0;
  padding-left: 1.75rem;
  font-size: 1.05rem;
}

.account { display: grid; gap: 1.25rem; max-width: 40rem; }
.account h2 { margin-top: 0; }

/* ---- Status badges (run states) ---- */

.badge {
  display: inline-block;
  padding: 0.1rem 0.6rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--white);
  font-size: 0.9rem;
  font-weight: 600;
  white-space: nowrap;
}
.badge.succeeded { border-color: var(--steel); color: var(--steel-text); }
.badge.failed { background: var(--red); border-color: var(--red); color: var(--white); }
.badge.timed_out { border-color: var(--red); color: var(--red); }
.badge.running { background: var(--sky); border-color: var(--sky); }
.badge.queued { background: var(--cream); border-color: var(--cream); }
.badge.cancelled { background: #dcdcde; border-color: #dcdcde; }

/* ---- Tables: charcoal header band with an orange underline, figures centred ---- */

/* position: relative: the grids' visually hidden labels (.sr-only, absolutely positioned)
   belong to this box, so they scroll with the table instead of widening the page on a phone. */
.table-wrap { overflow-x: auto; position: relative; }

table.data {
  width: 100%;
  border-collapse: collapse;
  background: var(--white);
}
table.data th, table.data td { padding: 0.6rem 0.85rem; text-align: left; vertical-align: top; }
table.data thead th {
  background: var(--charcoal);
  border-bottom: 3px solid var(--orange);
  color: var(--white);
  font-weight: 600;
}
table.data tbody td { border-bottom: 1px solid var(--line); }
table.data tbody tr:nth-child(even) { background: #f8f8f9; }
table.data tbody tr:hover { background: var(--cream); }
table.data tfoot td { border-top: 2px solid var(--charcoal); font-weight: 600; }
table.data .num { text-align: center; font-variant-numeric: tabular-nums; }

/* Sortable columns (faga.js adds aria-sort and the arrows). Opt a column out
   with data-nosort. */
table.sortable thead th:not([data-nosort]) { cursor: pointer; user-select: none; white-space: nowrap; }
table.sortable thead th:not([data-nosort]):hover { background: #505050; }
table.sortable thead th[aria-sort="ascending"]::after { content: " \25B2"; font-size: 0.7em; }
table.sortable thead th[aria-sort="descending"]::after { content: " \25BC"; font-size: 0.7em; }
table.sortable thead th:focus-visible { outline-color: var(--white); outline-offset: -3px; }

/* ---- Scripts: the "Code from GitHub" header, search, groups, one script ---- */

:root {
  --amber: #b0812f;
  --amber-bg: #fbf1d9;
}

.sync-banner {
  margin: 0 0 1.25rem;
  padding: 0.7rem 1rem;
  background: var(--white);
  border: 1px solid var(--line);
  border-left: 5px solid var(--steel);
  border-radius: var(--radius);
}
.sync-banner p { margin: 0; }
.sync-banner .sync-line { font-weight: 500; }
.sync-banner .sync-note,
.sync-banner .sync-error { margin-top: 0.25rem; }
.sync-banner.sync-stale,
.sync-banner.sync-none { background: var(--amber-bg); border-left-color: var(--amber); }
.sync-banner.sync-failed { border-left-color: var(--red); }
.sync-banner.sync-failed .sync-error { color: var(--red); font-weight: 500; }

.search-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 0.6rem; margin: 0 0 1.25rem; }
.search-bar input[type="search"] { flex: 1 1 16rem; max-width: 28rem; }
.search-bar .btn { font-size: 1rem; padding: 0.4rem 1rem; }

.script-group h2 { margin-top: 1.75rem; }
.script-group .count { font-size: 1rem; font-weight: 400; }
.file-name { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.85rem; }

.badge.rstudio { background: var(--amber-bg); border-color: var(--amber); color: #6e4f12; }
.rstudio-panel { margin: 0 0 1.25rem; }
.rstudio-panel p { margin: 0.5rem 0 0; }

.crumb { margin: 0 0 0.5rem; }
.script-meta { margin: -0.5rem 0 1rem; }
.header-card, .run-form { margin: 0 0 1.25rem; }
.header-card h2, .run-form h2 { margin-top: 0; }
.run-form { max-width: 40rem; }
.req { color: var(--orange-text); font-weight: 400; }
pre.header-text {
  margin: 0;
  max-height: 28rem;
  overflow: auto;
  white-space: pre-wrap;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.9rem;
  line-height: 1.4;
}

/* ---- Runs: the run page, the list, the queue ---- */

.btn-small { font-size: 1rem; padding: 0.3rem 0.85rem; }
.inline-form { display: inline; margin: 0; }
.actions { white-space: nowrap; }

.run-status, .run-inputs, .run-outputs { margin: 0 0 1.25rem; }
.run-inputs h2, .run-outputs h2, .run-log-section h2 { margin-top: 0; }
.run-log-section h2 { margin-top: 1.5rem; }
.run-head { display: flex; flex-wrap: wrap; align-items: center; gap: 0.75rem; margin: 0 0 0.9rem; }
.run-head .badge { padding: 0.2rem 0.85rem; font-size: 1.05rem; }

.facts {
  display: grid;
  grid-template-columns: max-content minmax(0, 1fr);
  gap: 0.35rem 1.5rem;
  margin: 0;
}
.facts dt { color: var(--muted); font-weight: 500; }
.facts dd { margin: 0; overflow-wrap: anywhere; }
.run-error { margin: 0.9rem 0 0; }

pre.run-log {
  min-height: 5rem;
  max-height: 34rem;
  margin: 0 0 1.25rem;
  padding: 0.75rem 1rem;
  overflow: auto;
  background: var(--charcoal);
  border-radius: var(--radius);
  color: var(--white);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.85rem;
  line-height: 1.4;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.filters { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 0.75rem 1rem; margin: 0 0 1.25rem; }
.filter label { font-size: 0.9rem; }
.filters select, .filters input[type="date"] { width: auto; min-width: 9rem; max-width: 16rem; }
.filter-buttons { display: flex; align-items: center; gap: 0.9rem; }
.pager { display: flex; gap: 1.5rem; margin: 1rem 0 0; }

.queue-section h2 { margin-top: 1.5rem; }
.queue-section .count { font-size: 1rem; font-weight: 400; }

/* ---- Outputs: the list, by date, the viewer ---- */

.subnav { display: flex; flex-wrap: wrap; gap: 0.4rem 1.25rem; margin: 0 0 1rem; }   /* wraps on a phone */
.subnav [aria-current="page"] { font-weight: 600; box-shadow: inset 0 -3px 0 var(--orange); }
.list-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 0.5rem 1rem; margin: 0 0 0.75rem; }
.file-cell { overflow-wrap: anywhere; }
.sha { overflow-wrap: anywhere; font-size: 0.85rem; }

/* The viewer: a slim bar, then a frame that fills the rest of the window. */
.viewer-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.4rem 1.25rem;
  padding: 0.4rem 1rem;
  background: var(--white);
  border-bottom: 1px solid var(--line);
}
.viewer-name { flex: 1 1 14rem; min-width: 0; overflow-wrap: anywhere; }
.viewer-links { display: flex; flex-wrap: wrap; align-items: center; gap: 0.4rem 1rem; }
.viewer-frame {
  display: block;
  width: 100%;
  height: calc(100vh - 7.3rem);
  min-height: 28rem;
  border: 0;
  background: var(--white);
}
.download-card { max-width: 40rem; margin: 1.5rem; }
.download-card .card-title {
  margin: 0 0 1rem;
  color: var(--charcoal);
  font-size: 1.4rem;
  font-style: normal;
  font-weight: 600;
  text-transform: none;
  overflow-wrap: anywhere;
}

/* ---- Admin: users, CIMS login, toolkit sync, system, audit ---- */

.admin-sections { display: grid; gap: 1.25rem; max-width: 40rem; }
.admin-sections h2, .admin-section h2, .invite-result h2, .sync-result h2 { margin-top: 0; }
.upload-card { max-width: 40rem; }
.upload-card h2 { margin-top: 0; }
.flash-title { margin: 0 0 0.25rem; font-weight: 700; }
.problems { margin: 0; padding-left: 1.25rem; }
.admin-section { margin: 0 0 1.5rem; }
.admin-section .count { font-size: 1rem; font-weight: 400; }
.inline-fields { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 0 1rem; }
.inline-fields .field { margin-bottom: 0.5rem; }
.inline-fields input[type="email"] { min-width: 18rem; }
.invite-result { margin: 0 0 1.25rem; border-left: 5px solid var(--orange); }
.invite-link { padding: 0.6rem 0.85rem; background: var(--cream); border-radius: var(--radius); overflow-wrap: anywhere; }
.invite-link code { user-select: all; }
select.select-small { width: auto; max-width: 9rem; padding: 0.25rem 0.5rem; }
button.btn.btn-compact { font-size: 1rem; padding: 0.3rem 0.85rem; }   /* beats button.btn's 18px */
td.actions form { margin: 0 0.35rem 0.25rem 0; }
.nowrap { white-space: nowrap; }
.stored-line { overflow-wrap: anywhere; }
.name-list { margin: 0 0 0.75rem; padding-left: 1.25rem; }
.sync-result { margin: 0 0 1.25rem; }
.sync-result h3 { margin-top: 1rem; }
.sync-result .count { font-size: 1rem; font-weight: 400; }

/* System status: colour is never the only signal, each state also has a word. */
.system-table th.check-label { background: none; border-bottom: 1px solid var(--line); color: var(--charcoal); font-weight: 600; white-space: nowrap; }
.badge.status-ok { border-color: var(--steel); color: var(--steel-text); }
.badge.status-warn { background: var(--amber-bg); border-color: var(--amber); color: #6e4f12; }
.badge.status-bad { background: var(--red); border-color: var(--red); color: var(--white); }
.badge.status-unknown { background: #dcdcde; border-color: #dcdcde; }

/* ---- Dashboard: tabs, the bar above the report, the frame; the admin grid ---- */

.dash-head { padding: 1.25rem 1.5rem 0; }
.dash-head h1 { margin-bottom: 0.75rem; }

.tabs { display: flex; flex-wrap: wrap; gap: 0.25rem; border-bottom: 3px solid var(--orange); }
.tabs a {
  padding: 0.55rem 1.1rem;
  background: var(--white);
  border: 1px solid var(--line);
  border-bottom: 0;
  border-radius: var(--radius) var(--radius) 0 0;
  color: var(--charcoal);
  font-weight: 500;
  text-decoration: none;
}
.tabs a:hover { background: var(--cream); }
.tabs a[aria-current="page"] { background: var(--charcoal); border-color: var(--charcoal); color: var(--white); }

.dash-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem 1.25rem;
  padding: 0.6rem 1.5rem;
  background: var(--white);
  border-bottom: 1px solid var(--line);
}
.dash-info { flex: 1 1 18rem; min-width: 0; }
.dash-description { margin: 0 0 0.15rem; }
.dash-updated { margin: 0; }
.dash-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem 0.75rem; }
.dash-flash { margin: 0.75rem 1.5rem 0; }
.refresh-status {
  margin: 0;
  padding: 0.55rem 1.5rem;
  background: var(--cream);
  border-left: 5px solid var(--orange);
  font-weight: 500;
}
.dash-empty { margin: 1.5rem; }
.dash-frame {
  display: block;
  width: 100%;
  height: calc(100vh - 13rem);
  min-height: 28rem;
  border: 0;
  background: var(--white);
}

/* The order arrows: a white button (button.btn would otherwise paint them orange). */
button.btn.btn-move {
  padding: 0.25rem 0.6rem;
  background: var(--white);
  border-color: var(--charcoal);
  color: var(--charcoal);
  font-size: 1rem;
}
button.btn.btn-move:hover:not([disabled]) { background: var(--charcoal); border-color: var(--charcoal); color: var(--white); }

input[type="checkbox"] { width: 1.1rem; height: 1.1rem; accent-color: var(--orange); }
.check { display: flex; align-items: center; gap: 0.5rem; margin: 0 0 0.5rem; font-weight: 400; }
textarea { min-height: 4rem; resize: vertical; }
table.access-grid thead th.num { text-align: center; }

/* ---- The hub: a card per app (they stack on a phone) ---- */

.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr)); gap: 1.25rem; }
.tile { display: flex; flex-direction: column; border-top: 5px solid var(--orange); }
.tile h2 { margin-top: 0; }
.tile p { margin: 0 0 0.6rem; }
.tile-open { margin-top: auto; padding-top: 0.4rem; }
table.access-grid select.select-small { max-width: 8.5rem; }
/* Long names and emails wrap, so the choices next to them come into view on a phone. */
table.access-grid .person-cell { min-width: 9rem; overflow-wrap: anywhere; }

/* ---- htmx (its own indicator styles are switched off for the CSP) ---- */

.htmx-indicator { opacity: 0; transition: opacity 150ms ease-in; }
.htmx-request .htmx-indicator, .htmx-request.htmx-indicator { opacity: 1; }

/* ---- Small screens ---- */

@media (max-width: 720px) {
  .topbar { flex-wrap: wrap; gap: 0.5rem 1rem; padding: 0.5rem 1rem; }
  .nav { order: 3; width: 100%; overflow-x: auto; align-self: auto; }
  .nav a { padding: 0.6rem 0.85rem; }
  .page { padding: 1.25rem 1rem 2.5rem; }
  h1, .page-title { font-size: 1.6rem; }
  .dash-head { padding: 1rem 1rem 0; }
  .dash-bar { padding: 0.6rem 1rem; }
  .refresh-status { padding: 0.55rem 1rem; }
  .dash-flash { margin: 0.75rem 1rem 0; }
  .dash-empty { margin: 1rem; }
}

/* The full-width pages (the dashboard, the viewer) keep no page padding on a phone either:
   the frame fills the screen and their own bars carry the side padding. Two classes, so it
   wins over the .page padding above whatever the order. */
.page.page-wide { padding: 0; }

/* A phone (375 px): the logo and the app's name share one line with room to spare. */
@media (max-width: 420px) {
  .brand { gap: 0.6rem; }
  .brand-logo { height: 1.8rem; }
  .brand-name { padding-left: 0.6rem; font-size: 1.05rem; }
}
