:root {
  --bg: #0b1625;
  --surface: #122338;
  --surface-2: #172b42;
  --surface-3: #1c334d;
  --text: #eef4fb;
  --muted: #a8b7c8;
  --line: #29425e;
  --cm-green: #29b7a9;
  --cm-green-2: #5eead4;
  --primary-soft: rgba(41, 183, 169, .16);
  --danger: #e6473b;
  --warning: #e0c92f;
  --success: #2fb86a;
  --shadow: 0 14px 36px rgba(0, 0, 0, .3);
  --radius: 14px;
  --font: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; background: var(--bg); color: var(--text); font-family: var(--font); }
body { min-height: 100dvh; overflow-x: hidden; }
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
a { color: var(--cm-green-2); }
h1, h2, h3 { line-height: 1.2; }
.muted { color: var(--muted); }

/* Shell */
.app-shell { min-height: 100dvh; display: grid; grid-template-columns: 248px minmax(0, 1fr); }
.app-nav { overflow: hidden; background: linear-gradient(180deg, #07111e, #10243a); border-right: 1px solid var(--line); padding: 20px 14px; display: flex; flex-direction: column; gap: 6px; }
.app-main { min-width: 0; padding: 26px 32px 60px; max-width: 1680px; }
.app-brand { display: flex; align-items: center; gap: 12px; padding: 8px 8px 22px; }
.brand-mark { width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; color: #06231f; font-weight: 900; font-size: 18px; background: linear-gradient(145deg, var(--cm-green), var(--cm-green-2)); box-shadow: 0 8px 18px rgba(41,183,169,.3); }
.brand-name { font-size: 16px; font-weight: 800; letter-spacing: .02em; }
.brand-sub { color: var(--muted); font-size: 11px; }
.nav-link { display: flex; align-items: center; gap: 10px; padding: 11px 12px; border-radius: 10px; color: var(--muted); text-decoration: none; font-size: 14px; font-weight: 600; transition: background .15s, color .15s; }
.nav-link:hover { background: var(--surface-2); color: var(--text); }
.nav-link.active { background: var(--primary-soft); color: var(--cm-green-2); }
.nav-footer { margin-top: auto; padding: 12px 8px; color: var(--muted); font-size: 11px; }
.app-topbar { display: flex; align-items: center; margin-bottom: 22px; gap: 12px; flex-wrap: wrap; }
.nav-toggle { display: none; align-items: center; justify-content: center; width: 40px; height: 40px; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--line); color: var(--text); font-size: 18px; flex-shrink: 0; }
.nav-toggle:active { background: var(--surface-3); }
.nav-close { display: none; margin-left: auto; background: transparent; border: none; color: var(--muted); font-size: 20px; padding: 4px 8px; }
.nav-backdrop { position: fixed; inset: 0; background: rgba(3,10,20,.6); opacity: 0; pointer-events: none; transition: opacity .2s ease; z-index: 1990; }
.nav-backdrop.show { opacity: 1; pointer-events: auto; }
.conn-pill { display: inline-flex; align-items: center; gap: 7px; font-size: 12px; font-weight: 700; padding: 6px 12px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--line); color: var(--muted); margin-left: auto; }
.conn-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--danger); }
.conn-pill.live .conn-dot { background: var(--success); box-shadow: 0 0 0 3px rgba(47,184,106,.25); }
.conn-pill.live { color: var(--success); }

/* Cards & layout */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-body { padding: 20px 22px; }
.card-header { padding: 18px 22px; border-bottom: 1px solid var(--line); display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.card-header h2 { margin: 0; font-size: 16px; }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.stack { display: flex; flex-direction: column; gap: 16px; min-width: 0; }
.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }

/* Forms */
.form-group { margin-bottom: 14px; }
.form-label { display: block; font-size: 12px; font-weight: 700; color: var(--muted); margin-bottom: 6px; text-transform: uppercase; letter-spacing: .04em; }
.input { width: 100%; background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; color: var(--text); }
.input:focus { outline: 2px solid var(--cm-green); outline-offset: 1px; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 10px 18px; border-radius: 10px; border: 1px solid transparent; font-weight: 700; font-size: 14px; transition: transform .1s, filter .15s; }
.btn:active { transform: translateY(1px); }
.btn-primary { background: linear-gradient(135deg, var(--cm-green), var(--cm-green-2)); color: #06231f; }
.btn-primary:hover { filter: brightness(1.06); }
.btn-secondary { background: var(--surface-2); border-color: var(--line); color: var(--text); }
.btn-secondary:hover { background: var(--surface-3); }
.btn-danger { background: rgba(230,71,59,.14); color: #ff9a8f; border-color: rgba(230,71,59,.4); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-block { width: 100%; }

.badge { display: inline-flex; align-items: center; padding: 3px 10px; border-radius: 999px; font-size: 11px; font-weight: 800; letter-spacing: .03em; text-transform: uppercase; }
.badge-draft { background: var(--surface-3); color: var(--muted); }
.badge-generated { background: rgba(224,201,47,.16); color: var(--warning); }
.badge-processed { background: rgba(47,184,106,.16); color: var(--success); }

/* Results table (generated preview) */
.results-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.results-table th, .results-table td { padding: 9px 10px; text-align: left; border-bottom: 1px solid var(--line); }
.results-table th { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .04em; }

/* Responsive tables: on tablets a scrollable wrapper keeps every table
   usable without redesigning each view; on phones the JS in
   shared/responsive-tables.js swaps the table for a stacked card list. */
.table-responsive { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table-responsive table { min-width: 480px; }
.table-cards { display: none; flex-direction: column; gap: 10px; }
.table-card { background: var(--surface-2); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; }
.table-card-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.table-card-row { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; padding: 6px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
.table-card-row:last-child { border-bottom: none; }
.table-card-label { color: var(--muted); font-size: 10.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; flex-shrink: 0; }
.table-card-value { text-align: right; font-weight: 600; word-break: break-word; }
.table-card-message { text-align: center; color: var(--muted); font-size: 13px; padding: 16px; }
@media (max-width: 640px) {
  .table-responsive.has-cards { display: none; }
  .table-cards.active { display: flex; }
}
.party-swatch { width: 12px; height: 12px; border-radius: 3px; display: inline-block; margin-right: 8px; vertical-align: -1px; }
.totals-strip { display: flex; gap: 18px; flex-wrap: wrap; margin-top: 10px; }
.totals-strip .stat { background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 10px 16px; }
.totals-strip .stat .n { font-size: 18px; font-weight: 800; }
.totals-strip .stat .l { font-size: 11px; color: var(--muted); text-transform: uppercase; }

/* Upload / dropzone */
.dropzone { border: 2px dashed var(--line); border-radius: var(--radius); padding: 34px 20px; text-align: center; color: var(--muted); transition: border-color .15s, background .15s; cursor: pointer; }
.dropzone.drag { border-color: var(--cm-green); background: var(--primary-soft); color: var(--text); }
.dropzone input { display: none; }

/* Validation alerts */
.alert { border-radius: 12px; padding: 16px 18px; border: 1px solid; margin-top: 16px; }
.alert h3 { margin: 0 0 6px; font-size: 15px; }
.alert p { margin: 0; font-size: 13px; line-height: 1.55; }
.alert-success { background: rgba(47,184,106,.1); border-color: rgba(47,184,106,.35); color: #a9f0c6; }
.alert-error { background: rgba(230,71,59,.1); border-color: rgba(230,71,59,.35); color: #ffb4ab; }
.alert-warning { background: rgba(224,201,47,.1); border-color: rgba(224,201,47,.35); color: #f2e59a; }

/* PDF viewer */
.pdf-frame { width: 100%; height: 560px; border: 1px solid var(--line); border-radius: 10px; background: #fff; }

/* Ranking bars */
.bar-row { margin-bottom: 14px; }
.bar-row .bar-label { display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 6px; }
.bar-track { height: 14px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.bar-fill { height: 100%; border-radius: 999px; transition: width .5s ease; }

/* Ranking cards */
.rank-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 14px; }
.rank-card { background: var(--surface-2); border: 1px solid var(--line); border-radius: 12px; padding: 16px; position: relative; overflow: hidden; transition: transform .2s; }
.rank-card::before { content: ''; position: absolute; left: 0; top: 0; bottom: 0; width: 6px; background: var(--party-color, var(--cm-green)); }
.rank-card .medal { font-size: 12px; color: var(--muted); font-weight: 800; }
.rank-card .party { font-weight: 800; margin: 6px 0 10px; display: flex; align-items: center; gap: 8px; }
.rank-card .votes { font-size: 24px; font-weight: 900; font-variant-numeric: tabular-nums; }
.rank-card .pct { color: var(--muted); font-size: 13px; margin-top: 2px; }

/* Guide */
.modal-backdrop { position: fixed; inset: 0; background: rgba(3,10,20,.65); display: flex; align-items: center; justify-content: center; padding: 24px; z-index: 2050; }
.modal { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); max-width: 720px; width: 100%; max-height: 84vh; overflow: auto; box-shadow: var(--shadow); }
.modal-wide { max-width: 960px; }
.modal-header { padding: 18px 22px; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; align-items: center; position: sticky; top: 0; background: var(--surface); }
.modal-body { padding: 20px 22px; }
.scenario { border: 1px solid var(--line); border-radius: 10px; padding: 14px 16px; margin-bottom: 12px; }
.scenario h4 { margin: 0 0 8px; font-size: 14px; color: var(--cm-green-2); }
.scenario ol { margin: 0; padding-left: 18px; font-size: 13px; color: var(--muted); line-height: 1.7; }
.scenario .expect { margin-top: 8px; font-size: 12px; font-weight: 700; }
.icon-btn { background: transparent; border: none; color: var(--muted); font-size: 20px; }

/* Toasts */
#toast-root { position: fixed; right: 20px; bottom: 20px; display: flex; flex-direction: column; gap: 10px; z-index: 2060; }
.toast { background: var(--surface); border: 1px solid var(--line); border-left: 4px solid var(--cm-green); border-radius: 10px; padding: 12px 16px; box-shadow: var(--shadow); font-size: 13px; min-width: 240px; animation: toast-in .18s ease; }
.toast.error { border-left-color: var(--danger); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

.btn-sm { padding: 6px 12px; font-size: 12px; }
.pagination { display: flex; align-items: center; justify-content: flex-end; gap: 12px; margin-top: 12px; }
.pagination-status { font-size: 12px; color: var(--muted); }

/* Page grid: main content + simulated ops sidebar */
.page-grid { display: grid; grid-template-columns: minmax(0, 3.6fr) minmax(260px, 1fr); gap: 20px; align-items: start; }
.page-aside { position: sticky; top: 18px; display: flex; flex-direction: column; gap: 16px; }
.widget-card .card-header h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .03em; }
.widget-badge { font-size: 10px; padding: 2px 8px; }

/* Telemetry widget */
.telemetry-row { display: grid; grid-template-columns: 64px 1fr 52px; align-items: center; gap: 10px; font-size: 12px; color: var(--muted); }
.telemetry-track { height: 8px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.telemetry-fill { height: 100%; border-radius: 999px; background: linear-gradient(90deg, var(--cm-green), var(--cm-green-2)); transition: width .6s ease; }
.telemetry-fill.warn { background: linear-gradient(90deg, var(--warning), #f2b90a); }
.telemetry-fill.hot { background: linear-gradient(90deg, var(--danger), #ff8a7a); }
.container-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.container-chip { display: inline-flex; align-items: center; gap: 6px; font-size: 11px; font-weight: 700; padding: 4px 9px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--line); color: var(--muted); }
.container-chip .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--success); box-shadow: 0 0 0 2px rgba(47,184,106,.2); }
.container-chip.enter { animation: chip-in .35s ease; }
@keyframes chip-in { from { opacity: 0; transform: scale(.7); } to { opacity: 1; transform: scale(1); } }

/* Storage latency sparkline */
.spark-line { fill: none; stroke: var(--cm-green-2); stroke-width: 2; }
.spark-fill { fill: rgba(41,183,169,.14); stroke: none; }

/* Live hash chain */
.chain-list { display: flex; flex-direction: column; gap: 8px; max-height: 300px; overflow: hidden; }
.chain-block { background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; animation: block-in .3s ease; }
.chain-block-top { display: flex; justify-content: space-between; font-size: 11px; margin-bottom: 4px; }
.chain-block-top strong { color: var(--cm-green-2); }
@keyframes block-in { from { opacity: 0; transform: translateY(-6px); } to { opacity: 1; transform: translateY(0); } }

/* Network / voting centers */
.center-row { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); }
.center-row .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--line); flex-shrink: 0; transition: background .3s, box-shadow .3s; }
.center-row .dot.active { background: var(--success); box-shadow: 0 0 0 3px rgba(47,184,106,.2); }
.center-row .name { flex: 1; color: var(--text); }

/* Worker nodes */
.node-row { display: flex; align-items: center; gap: 10px; font-size: 12px; }
.node-row .name { flex: 1; color: var(--text); font-weight: 600; }
.node-badge { font-size: 9px; padding: 2px 7px; border-radius: 999px; font-weight: 800; letter-spacing: .03em; }
.node-badge.running { background: rgba(47,184,106,.16); color: var(--success); }
.node-badge.scaling { background: rgba(224,201,47,.16); color: var(--warning); }
.node-mini-track { width: 50px; height: 5px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.node-mini-fill { height: 100%; background: var(--cm-green-2); transition: width .5s ease; }
.node-row.enter { animation: chip-in .3s ease; }

@media (max-width: 880px) {
  .page-grid { grid-template-columns: minmax(0, 1fr); }
  .page-aside { position: static; }
}

/* PDF viewer modal (frontend/vendor/pdfjs renders each page onto a
   <canvas> instead of relying on the browser's own PDF viewer inside an
   <iframe>, which is unreliable on mobile). */
body.document-viewer-open { overflow: hidden; }
.mobile-pdf-stage { display: block; max-height: min(68vh, 720px); overflow: auto; padding: 8px; background: #d8dde5; border: 1px solid var(--line); border-radius: 10px; overscroll-behavior: contain; }

/* The PDF viewer modal should only ever scroll inside .mobile-pdf-stage —
   letting the outer .modal scroll too (its normal default) produced two
   nested scrollbars stacked right next to each other, which looked broken. */
.pdf-viewer-modal .modal { display: flex; flex-direction: column; max-height: 84vh; overflow: hidden; }
.pdf-viewer-modal .modal-header { flex: 0 0 auto; }
.pdf-viewer-modal .modal-body { display: flex; flex: 1; min-height: 0; flex-direction: column; overflow: hidden; }
.pdf-viewer-modal .mobile-pdf-stage { flex: 1; min-height: 0; max-height: none; }
.pdf-viewer-modal .row { flex: 0 0 auto; margin-top: 14px; }
.mobile-pdf-loading { display: flex; min-height: 220px; align-items: center; justify-content: center; gap: 10px; color: #243447; }
.mobile-pdf-page { display: flex; position: relative; justify-content: center; margin: 0 auto 10px; }
.mobile-pdf-page canvas { display: block; max-width: 100%; height: auto !important; background: #fff; box-shadow: 0 2px 8px rgba(0,0,0,.2); }
.mobile-pdf-page-label { position: absolute; top: 6px; right: 6px; z-index: 1; padding: 3px 7px; border-radius: 999px; background: rgba(15,30,46,.82); color: #fff; font-size: 10px; }
.button-spinner { display: inline-block; width: 14px; height: 14px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; vertical-align: -2px; animation: pdf-spin .7s linear infinite; }
@keyframes pdf-spin { to { transform: rotate(360deg); } }

@media (max-width: 640px) {
  /* On phones the PDF viewer takes over the whole screen — same as the
     other rapid prototypes — so there's maximum room to read pages. */
  .pdf-viewer-modal { position: fixed !important; inset: 0 !important; width: 100vw !important; height: 100dvh !important; max-width: none !important; margin: 0 !important; padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left)) !important; align-items: stretch !important; }
  .pdf-viewer-modal .modal { width: 100% !important; height: 100% !important; max-height: none !important; border-radius: 14px; }
}
table.audit-table th, table.audit-table td { padding: 7px 8px; border-bottom: 1px solid var(--line); text-align: left; }
code.hash { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 11px; color: var(--muted); word-break: break-all; }

/* Mobile public page */
.mobile-shell { min-height: 100dvh; padding: 22px 16px 60px; max-width: 480px; margin: 0 auto; }
.mobile-header { text-align: center; margin-bottom: 18px; }
.mobile-header .eyebrow { color: var(--cm-green-2); font-size: 12px; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; }
.mobile-header h1 { font-size: 20px; margin: 6px 0 0; }
.mobile-updated { text-align: center; color: var(--muted); font-size: 12px; margin-top: 16px; }

@media (max-width: 880px) {
  html, body { height: auto; overflow: visible; }
  .app-shell { grid-template-columns: minmax(0, 1fr); height: auto; }
  .nav-toggle { display: inline-flex; }
  .nav-close { display: block; }
  /* Off-canvas drawer: the sidebar is taken out of the grid flow and slides
     in from the left instead of being squeezed into a horizontal strip. */
  .app-nav {
    position: fixed; top: 0; left: 0; bottom: 0; z-index: 2000; width: min(84vw, 300px);
    height: 100dvh; flex-direction: column; overflow-y: auto; overflow-x: hidden;
    padding: 18px 14px; transform: translateX(-100%); transition: transform .25s ease;
    box-shadow: 0 0 40px rgba(0,0,0,.5);
  }
  .app-nav.open { transform: translateX(0); }
  .app-brand { display: flex; }
  .nav-footer { display: block; }
  .app-main { height: auto; overflow-y: visible; padding: 18px 16px 48px; }
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
  .modal-backdrop { padding: 12px; }
  .modal { max-height: 90vh; }
  .pdf-frame { height: 50vh; }
  .card-header { flex-wrap: wrap; }
  .card-header h2 { flex: 1 1 100%; }
  .card-header .row { width: 100%; }
}
