/* ============================================================================
 * CSE 135 HW4 — site-wide styles
 * One file, no framework, no build step. The palette is a set of role tokens
 * expressed as CSS custom properties, so the light and dark values are swapped
 * in a single place; the chart series colors are read back out of here by JS
 * (getComputedStyle), which means every color is defined exactly once, here.
 * ========================================================================== */

:root {
  color-scheme: light;

  --surface-0: #f4f4f2;      /* page background */
  --surface-1: #fcfcfb;      /* cards / chart surface */
  --surface-2: #eeeeec;      /* table headers, secondary blocks */
  --border:    #dcdcd8;
  --border-strong: #bfbfba;

  --text-primary:   #0b0b0b;
  --text-secondary: #52514e;
  --text-muted:     #77766f;

  --accent:      #2a78d6;
  --accent-ink:  #ffffff;

  /* Chart categorical colors (first three slots of the dataviz reference
     palette; verified across every pairing) */
  --series-1: #2a78d6;   /* blue */
  --series-2: #eb6834;   /* orange */
  --series-3: #1baf7a;   /* teal */
  --grid:     #e4e4e0;

  --good: #0f7a4d;
  --bad:  #b3261e;
  --warn: #8a5a00;

  --radius: 10px;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 14px rgba(0,0,0,.04);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
          "Hiragino Sans GB", "Microsoft YaHei", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --surface-0: #121211;
    --surface-1: #1a1a19;
    --surface-2: #242422;
    --border:    #32322f;
    --border-strong: #4a4a46;

    --text-primary:   #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted:     #97968c;

    --accent:     #3987e5;
    --accent-ink: #0b0b0b;

    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --grid:     #2c2c29;

    --good: #46b17c;
    --bad:  #e66767;
    --warn: #d0a03a;

    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 14px rgba(0,0,0,.3);
  }
}

/* -------------------------------------------------------- Base typography */

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--surface-0);
  color: var(--text-primary);
  font: 15px/1.6 var(--font);
  -webkit-font-smoothing: antialiased;
}

.wrap { width: min(1180px, 100% - 3rem); margin-inline: auto; }

h1 { font-size: 1.65rem; line-height: 1.25; margin: 0 0 .35rem; letter-spacing: -.01em; }
h2 { font-size: 1.1rem;  margin: 0 0 .25rem; letter-spacing: -.005em; }
p  { margin: 0 0 .6rem; }
code { font-family: var(--mono); font-size: .88em; }

a { color: var(--accent); text-decoration-thickness: 1px; text-underline-offset: 2px; }
a:hover { text-decoration-thickness: 2px; }

.muted  { color: var(--text-secondary); }
.small  { font-size: .85rem; }
.center { text-align: center; }
.num    { text-align: right; font-variant-numeric: tabular-nums; }

.skip-link {
  position: absolute; left: -9999px; top: 0; z-index: 100;
  background: var(--accent); color: var(--accent-ink); padding: .6rem 1rem;
}
.skip-link:focus { left: 0; }

:where(a, button, input, select, summary):focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px;
}

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

.site-header {
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20;
}
.header-inner {
  display: flex; align-items: center; gap: 1.5rem;
  min-height: 58px; flex-wrap: wrap;
}
.brand {
  display: inline-flex; align-items: center; gap: .5rem;
  font-weight: 650; color: var(--text-primary); text-decoration: none; letter-spacing: -.01em;
}
.brand-mark { color: var(--accent); font-size: 1.15rem; }
.brand strong { color: var(--accent); }

.site-nav { margin-right: auto; }
.nav-list { display: flex; gap: .35rem; list-style: none; margin: 0; padding: 0; }
.nav-list a {
  display: block; padding: .4rem .7rem; border-radius: 7px;
  color: var(--text-secondary); text-decoration: none; font-size: .92rem;
}
.nav-list a:hover { background: var(--surface-2); color: var(--text-primary); }
.nav-list a[aria-current="page"] { background: var(--surface-2); color: var(--text-primary); font-weight: 600; }
.lock { font-size: .8em; opacity: .7; }

.header-user { display: flex; align-items: center; gap: .75rem; }
.who { font-size: .9rem; color: var(--text-secondary); display: inline-flex; align-items: center; gap: .4rem; }
.logout-form { margin: 0; }

.badge {
  font-size: .72rem; padding: .1rem .4rem; border-radius: 999px;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text-secondary);
}
.badge-admin { background: color-mix(in srgb, var(--accent) 15%, transparent); border-color: var(--accent); color: var(--accent); }

/* --------------------------------------------------------------- Buttons */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .35rem;
  padding: .45rem .85rem; border-radius: 7px;
  border: 1px solid var(--border-strong); background: var(--surface-1);
  color: var(--text-primary); font: inherit; font-size: .9rem; line-height: 1.2;
  cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn[disabled] { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn-primary:hover { filter: brightness(1.08); background: var(--accent); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--text-secondary); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { border-color: var(--bad); color: var(--bad); }
.btn-danger:hover { background: color-mix(in srgb, var(--bad) 12%, transparent); }
.btn-sm { padding: .25rem .55rem; font-size: .82rem; }
.btn-block { width: 100%; padding: .6rem; }

/* ------------------------------------------------------ Page head / cards */

.page-head {
  display: flex; justify-content: space-between; align-items: flex-end;
  gap: 1.5rem; flex-wrap: wrap; margin: 2rem 0 1.25rem;
}
.crumb { font-size: .85rem; color: var(--text-muted); margin-bottom: .3rem; }
.lede { font-size: 1.02rem; max-width: 62ch; }

.card {
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 1.1rem 1.2rem 1.2rem; margin-bottom: 1.25rem;
}
.card-head {
  display: flex; justify-content: space-between; align-items: flex-start;
  gap: 1rem; flex-wrap: wrap; margin-bottom: .9rem;
}
.card-head p { margin: 0; font-size: .88rem; max-width: 68ch; }

.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 1.25rem; align-items: start; }
@media (max-width: 900px) { .two-col { grid-template-columns: 1fr; } }

/* ------------------------------------------------------------- KPI cards */

.kpi-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1rem; margin-bottom: 1.25rem; }
@media (max-width: 780px) { .kpi-row { grid-template-columns: repeat(2, 1fr); } }

.kpi {
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: var(--radius); padding: .9rem 1rem; display: flex; flex-direction: column; gap: .1rem;
}
.kpi-label { font-size: .8rem; color: var(--text-secondary); text-transform: uppercase; letter-spacing: .04em; }
.kpi-value { font-size: 1.85rem; font-weight: 650; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.kpi-sub   { font-size: .78rem; color: var(--text-muted); }
.kpi-alert .kpi-value { color: var(--bad); }

.admin-cta {
  display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap;
  background: color-mix(in srgb, var(--accent) 8%, var(--surface-1));
  border: 1px solid color-mix(in srgb, var(--accent) 35%, var(--border));
  border-radius: var(--radius); padding: .8rem 1.1rem; margin-bottom: 1.25rem;
}

/* ---------------------------------------------------------------- Charts */

.chart-box { position: relative; height: 320px; }        /* with Chart.js maintainAspectRatio:false the
                                                            container has to pin the height, or the canvas collapses to 0 */
.chart-box.tall { height: 380px; }
canvas { max-width: 100%; }
@media (max-width: 640px) { .chart-box { height: 260px; } }

.data-fallback { margin-top: .8rem; border-top: 1px dashed var(--border); padding-top: .6rem; }
.data-fallback summary { cursor: pointer; font-size: .85rem; color: var(--text-secondary); }
.data-fallback[open] summary { margin-bottom: .6rem; }

/* ---------------------------------------------------------------- Tables */

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

.data-table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.data-table th, .data-table td { padding: .48rem .6rem; border-bottom: 1px solid var(--border); text-align: left; }
.data-table thead th {
  background: var(--surface-2); font-size: .78rem; text-transform: uppercase;
  letter-spacing: .04em; color: var(--text-secondary); font-weight: 600;
  position: sticky; top: 0;
}
.data-table th.num, .data-table td.num { text-align: right; }
.data-table th.center, .data-table td.center { text-align: center; }
.data-table tbody tr:hover { background: var(--surface-2); }
.data-table .rank { color: var(--text-muted); font-variant-numeric: tabular-nums; }
.data-table .empty { text-align: center; color: var(--text-muted); padding: 1.4rem; }
.sortable th[data-sort] { cursor: pointer; user-select: none; }
.sortable th[data-sort]::after { content: " ↕"; opacity: .35; }
.sortable th[aria-sort="ascending"]::after  { content: " ↑"; opacity: 1; }
.sortable th[aria-sort="descending"]::after { content: " ↓"; opacity: 1; }

.pill { font-size: .75rem; padding: .12rem .5rem; border-radius: 999px; border: 1px solid; }
.pill-good { color: var(--good); border-color: color-mix(in srgb, var(--good) 45%, transparent); }
.pill-bad  { color: var(--bad);  border-color: color-mix(in srgb, var(--bad) 45%, transparent); }

.crud-grid input[type="text"], .crud-grid input[type="email"],
.crud-grid input[type="password"], .crud-grid input:not([type]) {
  padding: .28rem .4rem; font: inherit; font-size: .85rem;
  border: 1px solid var(--border-strong); border-radius: 5px;
  background: var(--surface-1); color: var(--text-primary); max-width: 100%;
}
.crud-grid .hashcell code { color: var(--text-muted); display: block; font-size: .75rem; margin-bottom: .2rem; }
.crud-grid .actions { display: flex; gap: .3rem; justify-content: center; }
.row-dirty { background: color-mix(in srgb, var(--warn) 10%, transparent) !important; }

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

.form .field { margin-bottom: .9rem; }
.form label { display: block; font-size: .87rem; font-weight: 600; margin-bottom: .28rem; }
.form input[type="text"], .form input[type="email"], .form input[type="password"],
.form input[type="date"], .form input:not([type]) {
  width: 100%; padding: .5rem .6rem; font: inherit;
  border: 1px solid var(--border-strong); border-radius: 7px;
  background: var(--surface-1); color: var(--text-primary);
}
.form input[aria-invalid="true"] { border-color: var(--bad); }
.field-hint { font-size: .78rem; color: var(--text-muted); margin: .25rem 0 0; }
.field-err  { font-size: .8rem;  color: var(--bad);        margin: .25rem 0 0; }
.field-check label { font-weight: 400; display: inline-flex; gap: .4rem; align-items: center; }

.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 0 1.2rem; }
.form-grid .field-submit { grid-column: 1 / -1; }
@media (max-width: 700px) { .form-grid { grid-template-columns: 1fr; } }

.pw-wrap { position: relative; display: flex; gap: .4rem; }
.pw-wrap input { flex: 1; }

.range-form { display: flex; align-items: center; gap: .45rem; flex-wrap: wrap; font-size: .88rem; }
.range-form label { color: var(--text-secondary); }
.range-form input[type="date"] {
  padding: .32rem .45rem; font: inherit; font-size: .85rem;
  border: 1px solid var(--border-strong); border-radius: 6px;
  background: var(--surface-1); color: var(--text-primary);
}
.preset-links { display: flex; gap: .6rem; font-size: .82rem; margin-left: .3rem; }
.inline-form { display: inline-flex; gap: .5rem; align-items: center; margin: 0; }

/* ----------------------------------------------------------- Login page */

.auth-card {
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 2rem; max-width: 420px; margin: 4rem auto;
}
.auth-card.center { text-align: center; }
.auth-card h1 { margin-bottom: .5rem; }
.big-check {
  width: 56px; height: 56px; margin: 0 auto 1rem; border-radius: 50%;
  display: grid; place-items: center; font-size: 1.8rem; font-weight: 700;
  background: color-mix(in srgb, var(--good) 15%, transparent); color: var(--good);
}
.big-warn { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }
.hint { margin-top: 1.4rem; font-size: .85rem; }
.hint summary { cursor: pointer; color: var(--text-secondary); }
.hint ul { margin: .6rem 0; padding-left: 1.2rem; }

/* -------------------------------------------------------- flash / verdict */

.flash-stack { margin: 1rem 0 0; display: grid; gap: .5rem; }
.flash {
  padding: .6rem .85rem; border-radius: 8px; font-size: .9rem;
  border: 1px solid var(--border); background: var(--surface-2);
}
.flash-ok   { border-color: color-mix(in srgb, var(--good) 45%, transparent); background: color-mix(in srgb, var(--good) 10%, var(--surface-1)); }
.flash-err  { border-color: color-mix(in srgb, var(--bad)  45%, transparent); background: color-mix(in srgb, var(--bad)  10%, var(--surface-1)); }
.flash-warn { border-color: color-mix(in srgb, var(--warn) 45%, transparent); background: color-mix(in srgb, var(--warn) 10%, var(--surface-1)); }

.verdict {
  background: var(--surface-1); border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  border-radius: var(--radius); padding: 1.1rem 1.2rem; margin-bottom: 1.25rem;
}
.verdict h2 { margin-bottom: .5rem; }
.verdict p:last-child { margin-bottom: 0; }

.danger-card { border-color: var(--bad); border-left: 4px solid var(--bad); }

.notes { font-size: .9rem; color: var(--text-secondary); margin: 1.5rem 0 3rem; }
.notes ul { padding-left: 1.2rem; }
.notes li { margin-bottom: .35rem; }

/* ---------------------------------------------------------------- Footer */

.site-footer {
  border-top: 1px solid var(--border); margin-top: 3rem; padding: 1.2rem 0 2rem;
  font-size: .82rem; color: var(--text-muted);
}
.site-footer p { margin: 0; }
.mock-note { color: var(--warn); margin-top: .3rem; }

/* ------------------------------------------------- Email code sign-in */

/* The 'use a different address' button is a form, not a link, because it
   changes server-side state (it clears the pending address out of the
   session) and a GET that mutates state is a GET a browser prefetcher will
   fire on its own. Styled to sit flush under the primary form. */
.form-inline {
  margin-top: .5rem;
  display: flex;
  justify-content: center;
}
.form-inline .btn { padding-inline: 0; }

/* A one-time code is read digit by digit off another screen, so it gets a
   monospaced face and real letter spacing — proportional digits at body size
   are where 6 and 8 get misread. */
#code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 1.35rem;
  letter-spacing: .35em;
  text-align: center;
}

/* ----------------------------------------------------------------- Print */

@media print {
  .site-header, .site-footer, .range-form, .admin-cta, .btn { display: none !important; }
  .card { break-inside: avoid; box-shadow: none; }
  body { background: #fff; }
}

/* ============================================================================
 * HW5 — three-level access, report categories, comments, exports
 * Same rules as the rest of this file: role tokens only, no new hard-coded
 * colours, and nothing that depends on JavaScript having run.
 * ========================================================================== */

/* ── Role badges ───────────────────────────────────────────────────────── */
.badge-analyst  { background: var(--series-3); color: var(--accent-ink); }
.badge-sections { background: var(--surface-2); color: var(--text-secondary);
                  font-weight: 400; letter-spacing: 0; }

/* ── Charts: the static SVG and the canvas occupy the same box ──────────── */
.chart-static  { width: 100%; color: var(--text-secondary); }
.svg-chart     { width: 100%; height: auto; display: block; }
.chart-data    { margin: .5rem 0 0; }
.chart-data > summary {
  cursor: pointer; color: var(--text-secondary); font-size: .9rem;
  padding: .35rem 0;
}
.chart-data[open] > summary { margin-bottom: .5rem; }

/* ── KPI tiles ─────────────────────────────────────────────────────────── */
.kpi-note  { margin: .15rem 0 0; font-size: .78rem; color: var(--text-muted); }
.kpi-warn  { border-color: var(--bad); }
.kpi-warn .kpi-value { color: var(--bad); }

/* ── Saved report index ────────────────────────────────────────────────── */
.report-list { list-style: none; margin: 0; padding: 0; }
.report-list > li {
  padding: .85rem 0; border-top: 1px solid var(--border);
}
.report-list > li:first-child { border-top: 0; }
.report-list p { margin: .2rem 0 0; }
.report-link { font-size: 1.05rem; text-decoration: none; }
.report-link:hover strong { text-decoration: underline; }

/* ── Analyst comments ──────────────────────────────────────────────────── */
.comment-form { margin-bottom: 1.25rem; }
.comment-form textarea {
  width: 100%; font: inherit; padding: .6rem .7rem; border-radius: var(--radius);
  border: 1px solid var(--border-strong); background: var(--surface-1);
  color: var(--text-primary); resize: vertical;
}
.comment-list { list-style: none; margin: 0; padding: 0; }
.comment {
  border-left: 3px solid var(--accent);
  padding: .1rem 0 .1rem .8rem;
  margin: 0 0 1.1rem;
}
.comment-meta {
  margin: 0 0 .25rem; display: flex; gap: .6rem; align-items: baseline;
  font-size: .85rem; color: var(--text-muted); flex-wrap: wrap;
}
/* Comment bodies are plain text run through h() and nl2br(); pre-wrap keeps the
   author's own line breaks without letting any markup through. */
.comment-body { margin: 0; white-space: pre-wrap; }

/* ── Publish / export actions ──────────────────────────────────────────── */
.action-grid {
  display: grid; gap: 1.25rem;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
}
.action-grid h3 { margin: 0 0 .35rem; font-size: 1rem; }
.stack > * + * { margin-top: .5rem; }
.stack input[type="text"] {
  width: 100%; font: inherit; padding: .45rem .55rem;
  border: 1px solid var(--border-strong); border-radius: 6px;
  background: var(--surface-1); color: var(--text-primary);
}
.btn-secondary { background: var(--surface-2); border-color: var(--border-strong); }
.btn-small, .btn-sm { font-size: .8rem; padding: .2rem .5rem; }
.inline { display: inline; }

/* ── Section grants in the users grid ──────────────────────────────────── */
.sections-cell { white-space: nowrap; }
.chk {
  display: inline-flex; align-items: center; gap: .25rem;
  margin-right: .55rem; font-size: .82rem; color: var(--text-secondary);
}
.label-like { display: block; font-weight: 600; margin-bottom: .3rem; }

.copy-target {
  display: inline-block; word-break: break-all;
  background: var(--surface-2); padding: .35rem .5rem; border-radius: 6px;
}
.empty { color: var(--text-muted); padding: .75rem 0; }

/* ── Print ─────────────────────────────────────────────────────────────── */
/* Kept for the browser's own Print to PDF. It is not how the export works --
   that is generated server-side -- but somebody will reach for Cmd-P anyway,
   and the page should not print its navigation and its buttons. */
@media print {
  .site-header, .site-footer, .range-form, .action-grid,
  .comment-form, .chart-data > summary { display: none !important; }
  .chart-static { display: block !important; }
  canvas { display: none !important; }
  .card { break-inside: avoid; border: 1px solid #ccc; }
}
.sections-cell .chk { margin-right: .35rem; }
.sections-cell abbr { text-decoration: none; border-bottom: 1px dotted var(--border-strong); }
.crud-grid td, .crud-grid th { padding-left: .45rem; padding-right: .45rem; }
