/* ==========================================================================
   Manila Voting Weather 2025
   Enterprise shell: fixed left rail, boxed panels, white surface, navy ink,
   azure reserved for data marks and links. Headlines in Source Serif 4,
   everything else in Inter. Light and dark are separately tuned palettes,
   both validated against their own surfaces.
   ========================================================================== */

:root {
  color-scheme: light;

  /* surfaces and ink */
  --surface-1:      #ffffff;   /* panel / chart surface */
  --plane:          #f4f6f9;   /* page plane behind panels */
  --rail:           #ffffff;   /* sidebar surface */
  --text-primary:   #16213c;   /* navy ink */
  --text-secondary: #4a5468;
  --text-muted:     #69748c;
  --gridline:       #e5e9f0;
  --baseline:       #c9d0dc;
  --border:         #e2e6ee;
  --border-strong:  #c9d0dc;

  /* categorical slots 1-2, validated all-pairs on white */
  --series-1:       #2a78d6;
  --series-2:       #eb6834;

  /* ordinal blue ramp, validated --ordinal on white */
  --ord-1:          #184f95;
  --ord-2:          #2a78d6;
  --ord-3:          #5598e7;
  --ord-4:          #86b6ef;
  --ord-5:          #b7d3f6;

  /* de-emphasised context marks */
  --mark-quiet:     #cfd6e0;

  /* status */
  --status-good:     #0ca30c;
  --status-warning:  #b97a00;
  --status-critical: #c43333;
  --success-text:    #006300;

  --navy:           #16213c;
  --navy-2:         #101830;
  --link:           #1c5cab;

  --radius:    10px;
  --radius-sm:  7px;
  --rail-w:    236px;
  --maxw:      1240px;
  --measure:   70ch;

  --font-sans:  "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-serif: "Source Serif 4", Charter, Georgia, serif;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --surface-1:      #121826;
    --plane:          #0b101b;
    --rail:           #0e1420;
    --text-primary:   #f2f5fa;
    --text-secondary: #b4bdcc;
    --text-muted:     #7e8898;
    --gridline:       #222c3f;
    --baseline:       #313d55;
    --border:         #1f2940;
    --border-strong:  #313d55;
    --series-1:       #3987e5;
    --series-2:       #d95926;
    --ord-1:          #cde2fb;
    --ord-2:          #86b6ef;
    --ord-3:          #3987e5;
    --ord-4:          #184f95;
    --ord-5:          #104281;
    --mark-quiet:     #3b475e;
    --status-warning: #d99a1f;
    --success-text:   #0ca30c;
    --navy:           #0e1420;
    --navy-2:         #0a0f1a;
    --link:           #6da7ec;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-1:      #121826;
  --plane:          #0b101b;
  --rail:           #0e1420;
  --text-primary:   #f2f5fa;
  --text-secondary: #b4bdcc;
  --text-muted:     #7e8898;
  --gridline:       #222c3f;
  --baseline:       #313d55;
  --border:         #1f2940;
  --border-strong:  #313d55;
  --series-1:       #3987e5;
  --series-2:       #d95926;
  --ord-1:          #cde2fb;
  --ord-2:          #86b6ef;
  --ord-3:          #3987e5;
  --ord-4:          #184f95;
  --ord-5:          #104281;
  --mark-quiet:     #3b475e;
  --status-warning: #d99a1f;
  --success-text:   #0ca30c;
  --navy:           #0e1420;
  --navy-2:         #0a0f1a;
  --link:           #6da7ec;
}

/* --------------------------------------------------------------- reset -- */

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.6;
  color: var(--text-primary);
  background: var(--plane);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

h1, h2 {
  font-family: var(--font-serif);
  font-weight: 640;
  line-height: 1.15;
  letter-spacing: -0.01em;
  margin: 0 0 .45em;
}
h1 { font-size: clamp(1.75rem, 3.4vw, 2.45rem); }
h2 { font-size: clamp(1.3rem, 2.4vw, 1.7rem); }
h3, h4 { font-family: var(--font-sans); font-weight: 620; line-height: 1.3; margin: 0 0 .4em; letter-spacing: -0.008em; }
h3 { font-size: 1rem; }
h4 { font-size: .84rem; }
p  { margin: 0 0 1em; }
a  { color: var(--link); text-decoration-thickness: 1px; text-underline-offset: 3px; }
a:hover { color: var(--text-primary); }
img { max-width: 100%; height: auto; display: block; }
code, kbd { font-family: ui-monospace, "Cascadia Code", "SF Mono", Menlo, monospace; font-size: .86em; }

:where(a, button, summary, [tabindex]):focus-visible {
  outline: 2px solid var(--series-1);
  outline-offset: 2px;
  border-radius: 3px;
}

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
[hidden] { display: none !important; }

/* ------------------------------------------------------------ app shell -- */

.app { display: flex; min-height: 100vh; }

.rail {
  position: fixed; inset: 0 auto 0 0; z-index: 60;
  width: var(--rail-w);
  background: var(--rail);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  padding: 1.1rem .9rem 1rem;
  overflow-y: auto;
}

.rail-brand {
  display: flex; align-items: center; gap: .6rem;
  padding: .2rem .45rem 1rem;
  text-decoration: none; color: var(--text-primary);
}
.rail-brand__mark {
  width: 34px; height: 34px; flex: none;
  display: grid; place-items: center;
}
.rail-brand__mark picture { display: block; width: 100%; }
.rail-brand__mark img { width: 100%; height: auto; display: block; }
.rail-brand__name { font-size: .84rem; font-weight: 650; line-height: 1.25; letter-spacing: -0.01em; }
.rail-brand__sub  { font-size: .68rem; font-weight: 480; color: var(--text-muted); }

.rail-label {
  font-size: .64rem; font-weight: 660; letter-spacing: .1em; text-transform: uppercase;
  color: var(--text-muted); padding: .85rem .45rem .35rem;
}

.rail-nav { display: flex; flex-direction: column; gap: 2px; }
.rail-nav a {
  display: flex; align-items: center; gap: .55rem;
  padding: .44rem .55rem; border-radius: var(--radius-sm);
  font-size: .84rem; font-weight: 520; text-decoration: none;
  color: var(--text-secondary);
  border-left: 2px solid transparent;
}
.rail-nav a svg { flex: none; opacity: .75; }
.rail-nav a:hover { background: color-mix(in srgb, var(--text-primary) 5%, transparent); color: var(--text-primary); }
.rail-nav a[aria-current="page"] {
  background: color-mix(in srgb, var(--series-1) 10%, transparent);
  border-left-color: var(--series-1);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  color: var(--text-primary); font-weight: 600;
}
.rail-nav a[aria-current="page"] svg { opacity: 1; color: var(--series-1); }

.rail-sub { display: flex; flex-direction: column; gap: 1px; margin: .15rem 0 .3rem; }
.rail-sub a {
  padding: .3rem .55rem .3rem 2.15rem;
  font-size: .78rem; color: var(--text-muted); text-decoration: none;
  border-radius: var(--radius-sm);
}
.rail-sub a:hover { color: var(--text-primary); background: color-mix(in srgb, var(--text-primary) 4%, transparent); }

.rail-foot {
  margin-top: auto; padding: .9rem .45rem 0;
  border-top: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between; gap: .6rem;
}
.rail-foot__meta { font-size: .68rem; color: var(--text-muted); line-height: 1.45; }

.theme-toggle {
  display: inline-grid; place-items: center;
  width: 32px; height: 32px; flex: none;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface-1); color: var(--text-secondary);
  cursor: pointer; padding: 0;
}
.theme-toggle:hover { color: var(--text-primary); border-color: var(--border-strong); }
.theme-toggle svg { display: block; }

/* main column */
.main {
  flex: 1; min-width: 0;
  margin-left: var(--rail-w);
  display: flex; flex-direction: column; min-height: 100vh;
}
.content {
  width: 100%; max-width: var(--maxw);
  margin-inline: auto;
  padding: clamp(1.1rem, 2.6vw, 2rem) clamp(1rem, 3vw, 2.4rem) 3rem;
  flex: 1;
}

/* mobile top bar replaces the rail */
.topbar { display: none; }

@media (max-width: 1023px) {
  .rail { display: none; }
  .main { margin-left: 0; }
  .topbar {
    display: flex; align-items: center; gap: .75rem;
    position: sticky; top: 0; z-index: 60;
    background: color-mix(in srgb, var(--surface-1) 92%, transparent);
    backdrop-filter: saturate(160%) blur(10px);
    border-bottom: 1px solid var(--border);
    padding: .55rem clamp(1rem, 3vw, 1.4rem);
  }
  .topbar .rail-brand { padding: 0; flex: none; }
  .topbar .rail-brand__sub { display: none; }
  .topbar .theme-toggle { margin-left: auto; }
}

/* hamburger button; lives in the topbar, so mobile only */
.nav-burger {
  display: inline-grid; place-items: center;
  width: 32px; height: 32px; flex: none;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface-1); color: var(--text-secondary);
  cursor: pointer; padding: 0;
}
.nav-burger:hover { color: var(--text-primary); border-color: var(--border-strong); }

/* slide-in drawer, built from the rail's own content */
.drawer-backdrop {
  position: fixed; inset: 0; z-index: 70;
  background: rgba(9, 14, 24, .48);
  opacity: 0; pointer-events: none; transition: opacity .18s ease;
}
.drawer-backdrop[data-open="1"] { opacity: 1; pointer-events: auto; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 80;
  width: min(300px, 84vw);
  background: var(--rail); border-left: 1px solid var(--border);
  transform: translateX(105%); transition: transform .22s ease;
  display: flex; flex-direction: column;
  padding: 1.1rem .9rem 1rem; overflow-y: auto;
}
.drawer[data-open="1"] { transform: none; box-shadow: -12px 0 32px rgba(8, 12, 22, .18); }
@media (min-width: 1024px) { .drawer, .drawer-backdrop { display: none; } }

/* ----------------------------------------------------------- page head -- */

.pagehead { padding: .4rem 0 1.3rem; }
.pagehead__kicker {
  font-size: .68rem; font-weight: 660; letter-spacing: .11em; text-transform: uppercase;
  color: var(--series-1); margin: 0 0 .5rem;
}
.pagehead .lede {
  font-size: .98rem; color: var(--text-secondary); max-width: var(--measure); margin: 0;
}
.pagehead__meta {
  display: flex; flex-wrap: wrap; gap: .4rem; margin-top: .95rem;
  font-size: .72rem; color: var(--text-secondary);
}
.pagehead__meta span {
  padding: .22rem .6rem; border-radius: 999px;
  background: var(--surface-1); border: 1px solid var(--border);
}
.pagehead__actions { display: flex; flex-wrap: wrap; gap: .55rem; margin-top: 1.15rem; }

.btn {
  display: inline-flex; align-items: center; gap: .45rem;
  padding: .5rem .95rem; border-radius: var(--radius-sm);
  font-size: .82rem; font-weight: 600; text-decoration: none;
  border: 1px solid transparent; cursor: pointer;
  transition: background-color .14s, border-color .14s, color .14s;
}
.btn--solid { background: var(--navy); color: #fff; }
.btn--solid:hover { background: var(--series-1); color: #fff; }
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) .btn--solid { background: var(--series-1); }
  :root:where(:not([data-theme="light"])) .btn--solid:hover { background: #5598e7; }
}
:root[data-theme="dark"] .btn--solid { background: var(--series-1); }
:root[data-theme="dark"] .btn--solid:hover { background: #5598e7; }
.btn--outline { background: var(--surface-1); color: var(--text-primary); border-color: var(--border-strong); }
.btn--outline:hover { border-color: var(--series-1); color: var(--link); }

.section { padding: 1.1rem 0 1.6rem; scroll-margin-top: 70px; }
.section > h2 { margin-bottom: .25rem; }
.section__sub { font-size: .88rem; color: var(--text-secondary); max-width: var(--measure); margin: 0 0 1.1rem; }

.rule { border: 0; border-top: 1px solid var(--border); margin: 1.4rem 0; }

/* ---------------------------------------------------------- stat tiles -- */

.stats {
  display: grid; gap: .7rem;
  grid-template-columns: repeat(auto-fit, minmax(185px, 1fr));
  margin-bottom: .9rem;
}
.stat {
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: var(--radius); padding: .85rem 1rem .9rem;
}
.stat__label {
  font-size: .68rem; font-weight: 620; letter-spacing: .07em; text-transform: uppercase;
  color: var(--text-muted); margin: 0 0 .35rem;
}
.stat__value {
  font-size: clamp(1.5rem, 2.6vw, 1.95rem); font-weight: 650;
  letter-spacing: -0.02em; line-height: 1.05; color: var(--text-primary);
}
.stat__note { font-size: .72rem; color: var(--text-secondary); margin: .4rem 0 0; line-height: 1.45; }

/* -------------------------------------------------------------- panels -- */

.card {
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.1rem 1.05rem;
}
.card__head { display: flex; align-items: flex-start; gap: .9rem; flex-wrap: wrap; margin-bottom: .4rem; }
.card__title { font-size: .92rem; font-weight: 630; margin: 0; }
.card__sub { font-size: .77rem; color: var(--text-secondary); margin: .2rem 0 0; max-width: 60ch; }
.card__foot {
  font-size: .72rem; color: var(--text-muted); line-height: 1.5;
  margin: .85rem 0 0; padding-top: .7rem; border-top: 1px solid var(--border);
}
.card__foot a { color: var(--link); }

.grid { display: grid; gap: .8rem; align-items: start; }
.grid--2 { grid-template-columns: repeat(auto-fit, minmax(330px, 1fr)); }
.grid--3 { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }

/* -------------------------------------------------------- chart chrome -- */

.chart { width: 100%; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart text { font-family: var(--font-sans); }

.chart-tools { display: flex; align-items: center; gap: .35rem; margin-left: auto; flex: none; }

.chip {
  font: inherit; font-size: .72rem; font-weight: 570;
  padding: .26rem .62rem; border-radius: var(--radius-sm); cursor: pointer;
  border: 1px solid var(--border); background: var(--surface-1); color: var(--text-secondary);
}
.chip:hover { border-color: var(--border-strong); color: var(--text-primary); }
.chip[aria-pressed="true"] {
  background: var(--navy); color: #fff; border-color: var(--navy);
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) .chip[aria-pressed="true"] {
    background: var(--series-1); border-color: var(--series-1);
  }
}
:root[data-theme="dark"] .chip[aria-pressed="true"] {
  background: var(--series-1); border-color: var(--series-1);
}

.legend { display: flex; flex-wrap: wrap; gap: .3rem .9rem; margin: .5rem 0 .7rem; }
.legend__item {
  display: inline-flex; align-items: center; gap: .38rem;
  font-size: .75rem; color: var(--text-secondary);
}
.legend__swatch { width: 10px; height: 10px; border-radius: 3px; flex: none; }

.filterbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: .45rem .7rem;
  padding: .55rem .8rem; margin-bottom: .9rem;
  background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius-sm);
}
.filterbar__label { font-size: .72rem; font-weight: 620; color: var(--text-secondary); }
.filterbar__group { display: flex; gap: .3rem; flex-wrap: wrap; }

.viz-tip {
  position: fixed; z-index: 200; pointer-events: none; opacity: 0;
  transition: opacity .1s ease;
  background: var(--navy); color: #f2f5fa;
  border: 1px solid rgba(255,255,255,.08);
  border-radius: 7px; padding: .48rem .65rem;
  font-size: .74rem; line-height: 1.45; max-width: 270px;
  box-shadow: 0 8px 24px rgba(10, 16, 32, .35);
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) .viz-tip { background: #1c2740; }
}
:root[data-theme="dark"] .viz-tip { background: #1c2740; }
.viz-tip[data-show="1"] { opacity: 1; }
.viz-tip b { font-weight: 640; }
.viz-tip .viz-tip__v { font-variant-numeric: tabular-nums; }
.viz-tip .viz-tip__k { opacity: .72; }

.table-wrap { overflow-x: auto; margin-top: .3rem; }
table.data { border-collapse: collapse; width: 100%; font-size: .78rem; }
table.data caption { text-align: left; font-size: .73rem; color: var(--text-muted); padding-bottom: .5rem; }
table.data th, table.data td {
  text-align: left; padding: .42rem .65rem; border-bottom: 1px solid var(--gridline); white-space: nowrap;
}
table.data th {
  font-weight: 630; color: var(--text-muted); font-size: .68rem;
  text-transform: uppercase; letter-spacing: .05em;
}
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data tbody tr:hover { background: color-mix(in srgb, var(--series-1) 5%, transparent); }

/* --------------------------------------------------------------- prose -- */

.paper { max-width: 76ch; }
.paper h2 { margin-top: 2.1rem; padding-top: 1.5rem; border-top: 1px solid var(--border); }
.paper h2:first-of-type { border-top: 0; padding-top: 0; margin-top: 0; }
.paper h3 { margin-top: 1.5rem; }
.paper ul, .paper ol { padding-left: 1.25rem; margin: 0 0 1em; }
.paper li { margin-bottom: .4em; }
.paper figure { margin: 1.5rem 0; }
.paper figcaption { font-size: .74rem; color: var(--text-muted); margin-top: .55rem; }

.abstract {
  background: var(--surface-1); border: 1px solid var(--border);
  border-left: 3px solid var(--series-1);
  border-radius: 0 var(--radius) var(--radius) 0;
  padding: 1rem 1.25rem; margin: 1.3rem 0 1.8rem;
  font-size: .9rem;
}
.abstract > :last-child { margin-bottom: 0; }

.callout {
  border: 1px solid var(--border);
  border-left: 3px solid var(--series-1);
  background: var(--surface-1);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  padding: .85rem 1.1rem; margin: 1.3rem 0; font-size: .87rem;
}
.callout > :last-child { margin-bottom: 0; }
.callout__title {
  font-weight: 660; font-size: .7rem; letter-spacing: .07em; text-transform: uppercase;
  color: var(--series-1); margin: 0 0 .3rem;
}
.callout--warn { border-left-color: var(--status-warning); }
.callout--warn .callout__title { color: var(--status-warning); }

.refs { font-size: .84rem; }
.refs li { padding-left: 1.6rem; text-indent: -1.6rem; margin-bottom: .7em; line-height: 1.55; }
.refs a { word-break: break-word; }

.toc {
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: var(--radius); padding: .9rem 1.15rem; margin-bottom: 1.8rem;
}
.toc h2 {
  font-family: var(--font-sans) !important; font-size: .68rem !important;
  text-transform: uppercase; letter-spacing: .1em;
  color: var(--text-muted); margin: 0 0 .5rem !important; padding: 0 !important; border: 0 !important;
}
.toc ol { margin: 0; padding-left: 1.1rem; font-size: .82rem; columns: 2; column-gap: 2rem; }
@media (max-width: 640px) { .toc ol { columns: 1; } }
.toc li { margin-bottom: .28em; break-inside: avoid; }
.toc a { text-decoration: none; color: var(--text-secondary); }
.toc a:hover { color: var(--link); text-decoration: underline; }

.finding { display: flex; gap: .95rem; padding: 1rem 0; border-bottom: 1px solid var(--border); }
.finding:last-child { border-bottom: 0; }
.finding__n {
  flex: none; width: 28px; height: 28px; border-radius: var(--radius-sm);
  display: grid; place-items: center; font-size: .78rem; font-weight: 680;
  background: color-mix(in srgb, var(--series-1) 12%, var(--surface-1));
  color: var(--link); border: 1px solid color-mix(in srgb, var(--series-1) 25%, transparent);
}
.finding__body h3 { margin-bottom: .25rem; }
.finding__body p { font-size: .88rem; color: var(--text-secondary); }
.finding__body p:last-child { margin-bottom: 0; }

.audit td { white-space: normal; min-width: 9ch; }
.audit td.flag { color: var(--status-critical); font-weight: 600; white-space: nowrap; }
.audit td.ok { color: var(--success-text); font-weight: 600; white-space: nowrap; }

/* ---------------------------------------------------------- pdf viewer -- */

.pdf-frame {
  width: 100%; height: min(80vh, 880px);
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface-1);
}
.pdf-mobile { display: none; }
@media (max-width: 767px) {
  .pdf-desktop { display: none; }
  .pdf-mobile { display: block; }
}
.pdf-pages { display: grid; gap: .9rem; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.pdf-page {
  margin: 0; border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden; background: var(--surface-1);
}
.pdf-page img { width: 100%; }
.pdf-page figcaption {
  padding: .6rem .85rem; font-size: .75rem; color: var(--text-secondary);
  border-top: 1px solid var(--border);
}

/* -------------------------------------------------------------- footer -- */

.foot {
  border-top: 1px solid var(--border);
  padding: 1.1rem clamp(1rem, 3vw, 2.4rem) 1.4rem;
  font-size: .74rem; color: var(--text-muted);
  display: flex; flex-wrap: wrap; gap: .5rem 1.6rem; align-items: center;
  max-width: var(--maxw); margin-inline: auto; width: 100%;
}
.foot a { color: var(--text-secondary); }
.foot a:hover { color: var(--link); }

/* --------------------------------------------------------------- print -- */

@media print {
  .rail, .topbar, .chart-tools, .theme-toggle, .pagehead__actions, .foot { display: none !important; }
  .main { margin-left: 0; }
  body { background: #fff; color: #000; }
  .card, .stat { border: 1px solid #ccc; break-inside: avoid; }
}
