/* Raft Failure Lab. Calm, readable, light-first. Dark mode via [data-theme=dark]. */
:root {
  --bg: #f6f6f4; --card: #ffffff; --ink: #1d1e20; --ink2: #45484f; --muted: #6d717a;
  --line: #e4e4e0; --line2: #d3d4d0; --field: #ffffff; --hover: #f1f1ee;
  --accent: #2f5bd3; --accent-ink: #ffffff; --accent-soft: #e9eefb;
  --leader: #1f8a5b; --candidate: #c98a14; --follower: #8e939c; --crashed: #b5b8be; --lane: #ececea;
  --bad: #c43d3d; --badbg: #fbeeed; --ok: #1f7a51; --okbg: #ebf5ef;
  --part: #7a5cc7; --degraded: #c86a1a; --msg: #2f5bd3;
  --mono: ui-monospace, "Cascadia Mono", Consolas, monospace;
  color-scheme: light;
}
[data-theme=dark] {
  --bg: #141516; --card: #1b1c1e; --ink: #e8e8e5; --ink2: #b9bbbf; --muted: #8b8f97;
  --line: #2a2c2f; --line2: #383a3e; --field: #202124; --hover: #232427;
  --accent: #7d9cf0; --accent-ink: #0f1420; --accent-soft: #232a3d;
  --leader: #45b583; --candidate: #e0a53a; --follower: #7b8089; --crashed: #4a4d52; --lane: #24262a;
  --bad: #ef6b6b; --badbg: #2e1c1c; --ok: #5cc493; --okbg: #182a21;
  --part: #a58cf0; --degraded: #e08a3e; --msg: #7d9cf0;
  color-scheme: dark;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; background: var(--bg); color: var(--ink); }
body { font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); }
b, strong { font-weight: 600; }
h1, h2 { margin: 0; letter-spacing: -0.01em; }

/* ---------- header ---------- */
.top { background: var(--card); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20; }
.top-in { max-width: 1180px; margin: 0 auto; padding: 0 20px; height: 56px; display: flex; align-items: center; gap: 28px; }
.brand { font-weight: 650; font-size: 16px; }
.tabs { display: flex; gap: 4px; height: 100%; }
.tabs button { border: 0; background: none; padding: 0 12px; cursor: pointer; color: var(--muted); border-bottom: 2px solid transparent; }
.tabs button:hover { color: var(--ink); }
.tabs button.on { color: var(--ink); border-bottom-color: var(--accent); font-weight: 600; }
#theme { margin-left: auto; }

main { max-width: 1180px; margin: 0 auto; padding: 28px 20px 60px; }
.intro { max-width: 760px; margin-bottom: 22px; }
.intro h1 { font-size: 26px; line-height: 1.25; font-weight: 650; }
.intro p { color: var(--ink2); margin: 8px 0 0; }

/* ---------- form controls ---------- */
.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field > span { font-size: 13px; font-weight: 600; color: var(--ink2); }
.field.grow { flex: 1 1 260px; }
.field.small { width: 110px; }
select, input[type=number], input[type=search] {
  height: 38px; padding: 0 10px; border: 1px solid var(--line2); border-radius: 8px; background: var(--field); min-width: 0; width: 100%;
}
select:focus, input:focus, button:focus-visible, summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.pair { display: flex; align-items: center; gap: 6px; }
.pair input { width: 80px; }
.pair i { font-style: normal; color: var(--muted); font-size: 13px; }
.btn { height: 38px; padding: 0 18px; border-radius: 8px; border: 1px solid var(--line2); background: var(--card); cursor: pointer; font-weight: 600; white-space: nowrap; }
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.primary:disabled { opacity: .6; cursor: progress; }
.btn.danger { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 45%, var(--line2)); background: var(--card); }
.textbtn { border: 0; background: none; color: var(--accent); cursor: pointer; padding: 0; font-size: 14px; text-decoration: none; }
.textbtn:hover { text-decoration: underline; }
.iconbtn { width: 34px; height: 34px; border-radius: 8px; border: 1px solid var(--line2); background: var(--card); cursor: pointer; font-size: 18px; line-height: 1; color: var(--ink2); }
.iconbtn:hover { background: var(--hover); }
.hint { color: var(--muted); font-size: 13px; margin: 0; }
.err { color: var(--bad); margin: 8px 0 0; }
.err:empty { display: none; }
.spacer { flex: 1; }

.setup { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 14px; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 16px; }
.scen-desc { color: var(--muted); font-size: 14px; margin: 8px 2px 0; }

/* ---------- cards ---------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: 12px; margin: 0 0 16px; min-width: 0; }
.card-h { display: flex; align-items: baseline; gap: 8px 20px; flex-wrap: wrap; padding: 14px 18px 0; }
.card-h h2 { font-size: 16px; font-weight: 650; }
.card-sub { color: var(--muted); font-size: 13px; }
.pad { padding: 6px 18px 0; }
.pair-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px; }
@media (max-width: 900px) { .pair-grid { grid-template-columns: 1fr; } }
figure.card { margin: 0 0 16px; }

/* ---------- verdict ---------- */
.verdict { margin: 16px 0; display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 14px; }
.verdict h2 { font-size: 17px; font-weight: 650; color: var(--ok); }
.verdict.bad h2 { color: var(--bad); }
.verdict p { margin: 0; color: var(--muted); font-size: 14px; }
.verdict ul { flex-basis: 100%; list-style: none; padding: 0; margin: 4px 0 0; display: flex; flex-wrap: wrap; gap: 4px 22px; font-size: 14px; }
.verdict li::before { content: "✓"; color: var(--ok); font-weight: 700; margin-right: 7px; }
.verdict li.fail::before { content: "✕"; color: var(--bad); }
.verdict li.fail { color: var(--bad); }
.verdict li.fail button { margin-left: 6px; }

/* ---------- stage: cluster, story and player in one card ---------- */
.stage-grid { display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); border-bottom: 1px solid var(--line); }
.stage-left { border-right: 1px solid var(--line); padding: 12px 8px 4px; }
.stage-note { margin: 0 10px; font-size: 13px; color: var(--muted); }
.stage-right { padding: 16px 18px 8px; min-width: 0; }
.stage-right h2 { font-size: 15px; font-weight: 650; }
@media (max-width: 900px) {
  .stage-grid { grid-template-columns: 1fr; }
  .stage-left { border-right: 0; border-bottom: 1px solid var(--line); }
}
.key { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 16px; font-size: 13px; color: var(--muted); margin: 0; padding: 4px 18px 14px; }
.key.inline { padding: 0; }
.key .hint { margin-left: auto; }
.key.inline .hint { margin-left: 0; }
.sw { display: inline-block; width: 14px; height: 8px; border-radius: 2px; margin-right: 6px; vertical-align: 1px; }
.sw.leader { background: var(--leader); } .sw.candidate { background: var(--candidate); }
.sw.crashed { background: repeating-linear-gradient(135deg, var(--crashed) 0 2px, transparent 2px 4px); }
.sw.part { background: color-mix(in srgb, var(--part) 60%, transparent); }
.sw.bad { background: var(--bad); width: 3px; }
.cellkey { display: inline-block; width: 11px; height: 11px; border-radius: 2px; border: 2px solid var(--accent); background: var(--accent); margin-right: 6px; vertical-align: -1px; }
.cellkey.open { background: transparent; }
.cellkey.ring { box-shadow: 0 0 0 2px var(--card), 0 0 0 4px var(--bad); }

/* ---------- cluster ---------- */
#cluster { display: block; width: 100%; height: auto; max-height: 380px; }
.link { stroke: var(--line2); stroke-width: 1; }
.link.cut { stroke: var(--bad); stroke-dasharray: 3 5; opacity: .6; }
.node { cursor: pointer; }
.node circle.body { stroke-width: 2; transition: fill .25s, stroke .25s; }
.node.follower circle.body { fill: var(--card); stroke: var(--follower); }
.node.candidate circle.body { fill: color-mix(in srgb, var(--candidate) 16%, var(--card)); stroke: var(--candidate); }
.node.leader circle.body { fill: color-mix(in srgb, var(--leader) 16%, var(--card)); stroke: var(--leader); stroke-width: 3; }
.node.crashed circle.body { fill: var(--lane); stroke: var(--crashed); stroke-dasharray: 3 3; }
.node .grp { fill: none; stroke-width: 2; stroke-dasharray: 1 3; opacity: 0; }
.node .sel { fill: none; stroke: var(--accent); stroke-width: 2; opacity: 0; }
.node.selected .sel { opacity: 1; }
.node text { text-anchor: middle; pointer-events: none; fill: var(--ink); }
.node .name { font-weight: 650; font-size: 15px; }
.node .role { font-size: 11px; fill: var(--ink2); }
.node.crashed .name, .node.crashed .role { fill: var(--muted); }
.legend { display: flex; flex-wrap: wrap; gap: 4px 16px; padding: 0 18px 14px; font-size: 13px; color: var(--muted); margin: 0; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; vertical-align: -1px; border: 2px solid; }
.dot.leader { border-color: var(--leader); background: color-mix(in srgb, var(--leader) 16%, var(--card)); }
.dot.candidate { border-color: var(--candidate); background: color-mix(in srgb, var(--candidate) 16%, var(--card)); }
.dot.follower { border-color: var(--follower); background: var(--card); }
.dot.crashed { border-color: var(--crashed); background: var(--lane); border-style: dashed; }
.dot.msg { width: 7px; height: 7px; border: 0; background: var(--msg); }
.legend .x { color: var(--bad); margin-right: 5px; }

/* ---------- story ---------- */
.story { list-style: none; margin: 0; padding: 6px 0 0; }
.story li { display: grid; grid-template-columns: 64px minmax(0, 1fr); gap: 10px; padding: 7px 0; border-bottom: 1px solid var(--line); cursor: pointer; }
.story li:last-child { border-bottom: 0; }
.story li:hover .what { text-decoration: underline; text-decoration-color: var(--line2); }
.story .when { color: var(--muted); font: 13px/1.6 var(--mono); text-align: right; }
.story .what { color: var(--ink2); }
.story li.now .what { color: var(--ink); font-weight: 500; }
.story li.fault .what::before, .story li.elect .what::before, .story li.bad .what::before, .story li.script .what::before {
  content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 8px; vertical-align: 1px;
}
.story li.fault .what::before { background: var(--part); }
.story li.elect .what::before { background: var(--leader); }
.story li.script .what::before { background: var(--muted); }
.story li.bad { background: var(--badbg); margin: 0 -18px; padding-left: 18px; padding-right: 18px; }
.story li:nth-child(n+2) { opacity: .8; }
.story li.bad .what { color: var(--bad); font-weight: 500; }
.story li.bad .what::before { background: var(--bad); }
.story .empty { display: block; color: var(--muted); padding: 24px 0; border: 0; cursor: default; text-align: center; }

/* ---------- player ---------- */
.controls { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; padding: 14px 18px 8px; }
.play { width: 42px; height: 42px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink); cursor: pointer; font-size: 15px; }
.clock { font-size: 14px; color: var(--muted); min-width: 140px; }
.clock b { font: 600 20px var(--mono); color: var(--ink); }
.stepper { display: flex; gap: 4px; }
#speed { width: auto; height: 34px; }
#lanes { display: block; width: 100%; height: 170px; cursor: pointer; touch-action: none; }

/* ---------- logs ---------- */
.logmatrix { padding: 14px 18px 16px; display: grid; gap: 7px; }
.lrow { display: grid; grid-template-columns: 120px minmax(0, 1fr); gap: 12px; align-items: center; cursor: pointer; }
.lhead { font-size: 13px; color: var(--ink2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lhead b { color: var(--ink); margin-right: 4px; }
.lrow.selected .lhead b { color: var(--accent); }
.cells { display: flex; gap: 3px; height: 20px; min-width: 0; overflow: hidden; }
.cell { flex: 0 0 auto; border-radius: 3px; border: 2px solid; }
.cell.unc { background: transparent !important; }
.cell.conflict { box-shadow: 0 0 0 2px var(--card), 0 0 0 4px var(--bad); }
.cell.more { border: 0; color: var(--muted); font-size: 11px; display: grid; place-items: center; padding: 0 4px; }
.lindex { display: grid; grid-template-columns: 120px minmax(0, 1fr); gap: 12px; font-size: 11px; color: var(--muted); }
.lindex div { display: flex; justify-content: space-between; }
.bad-text { color: var(--bad); }

/* ---------- collapsible sections ---------- */
details.more > summary { padding: 14px 18px; cursor: pointer; font-weight: 650; list-style: none; display: flex; gap: 10px; align-items: baseline; }
details.more > summary::-webkit-details-marker { display: none; }
details.more > summary::before { content: "▸"; color: var(--muted); font-size: 12px; transition: transform .15s; }
details.more[open] > summary::before { transform: rotate(90deg); }
details.more > summary .hint { font-weight: 400; }
table.stats { border-collapse: collapse; width: 100%; font-size: 14px; margin-bottom: 14px; }
table.stats td { padding: 9px 18px; border-top: 1px solid var(--line); vertical-align: top; }
table.stats td:first-child { font-weight: 600; width: 200px; }
table.stats td.num { font-family: var(--mono); width: 110px; text-align: right; }
table.stats td:last-child { color: var(--muted); }
.evtools { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; padding: 0 18px 10px; }
.evtools input { width: 220px; height: 32px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { border: 1px solid var(--line2); background: var(--card); border-radius: 99px; padding: 3px 11px; font-size: 13px; cursor: pointer; color: var(--muted); }
.chip.on { background: var(--accent-soft); border-color: transparent; color: var(--ink); }
.evlist { height: 320px; overflow: auto; font: 12.5px/1.6 var(--mono); border-top: 1px solid var(--line); }
.ev { display: grid; grid-template-columns: 70px 34px 150px minmax(0, 1fr); gap: 10px; padding: 1px 18px; cursor: pointer; }
.ev:hover { background: var(--hover); }
.ev.cur { background: var(--accent-soft); }
.ev .t { color: var(--muted); text-align: right; }
.ev .d { color: var(--ink2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ev.vrow { display: block; background: var(--badbg); color: var(--bad); font-weight: 600; white-space: normal; }
.evlist .empty { color: var(--muted); padding: 30px; text-align: center; font-family: system-ui, sans-serif; }

.foot { display: flex; flex-wrap: wrap; gap: 8px 20px; align-items: center; color: var(--muted); font-size: 13px; margin: 8px 2px 0; }

/* ---------- compare ---------- */
.cmp-form { padding: 16px 18px; }
fieldset { border: 0; padding: 0; margin: 0; }
legend { font-size: 13px; font-weight: 600; color: var(--ink2); margin-bottom: 8px; padding: 0; }
.checks { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 6px 16px; }
.checks label { display: flex; gap: 8px; align-items: center; cursor: pointer; font-size: 14px; }
.checks input { width: 16px; height: 16px; accent-color: var(--accent); }
.cmp-row { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 14px; margin-top: 16px; }
.cmp-row .hint { align-self: center; }
.progress { height: 3px; margin-top: 14px; background: var(--line); border-radius: 3px; overflow: hidden; }
.progress i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .3s linear; }
.summary { padding: 18px; }
.summary h2 { font-size: 18px; font-weight: 650; }
.summary ul { margin: 10px 0 0; padding-left: 20px; color: var(--ink2); }
.summary li { margin: 4px 0; }
.charts { display: grid; grid-template-columns: repeat(auto-fit, minmax(330px, 1fr)); gap: 16px; }
.chart { padding: 14px 18px 10px; margin: 0; }
.chart h3 { margin: 0; font-size: 15px; }
.chart p { margin: 2px 0 8px; font-size: 13px; color: var(--muted); }
.chart svg { width: 100%; height: auto; display: block; }
.chart text { fill: var(--muted); font-size: 10.5px; }
.chart .grid { stroke: var(--line); }
.serA { fill: var(--accent); } .serB { fill: var(--candidate); }
.key { display: flex; gap: 18px; font-size: 13px; color: var(--ink2); margin: 0 0 12px; }
.key i { display: inline-block; width: 11px; height: 11px; border-radius: 2px; margin-right: 6px; vertical-align: -1px; }
.tablewrap { overflow-x: auto; }
table.cmp { border-collapse: collapse; width: 100%; font-size: 13.5px; }
table.cmp th, table.cmp td { padding: 8px 12px; border-top: 1px solid var(--line); text-align: right; white-space: nowrap; }
table.cmp th { font-weight: 600; color: var(--muted); font-size: 12.5px; cursor: help; border-top: 0; }
table.cmp td { font-family: var(--mono); }
table.cmp th:nth-child(-n+2), table.cmp td:nth-child(-n+2) { text-align: left; font-family: inherit; }
table.cmp td.better { color: var(--ok); font-weight: 700; }
table.cmp td.bad { color: var(--bad); font-weight: 700; }

/* ---------- findings ---------- */
.find-grid { display: grid; grid-template-columns: 230px minmax(0, 1fr); gap: 24px; align-items: start; }
.toc { position: sticky; top: 76px; display: grid; gap: 2px; font-size: 13.5px; }
.toc b { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; padding: 0 10px 6px; }
.toc a { color: var(--ink2); text-decoration: none; padding: 5px 10px; border-radius: 6px; }
.toc a:hover { background: var(--card); color: var(--ink); }
.md { padding: 8px 32px 28px; line-height: 1.7; }
.md h1 { font-size: 24px; margin: 18px 0 4px; }
.md h2 { font-size: 17px; margin: 30px 0 8px; padding-top: 20px; border-top: 1px solid var(--line); scroll-margin-top: 76px; }
.md h2 .meta { display: block; font-size: 12.5px; color: var(--muted); font-weight: 400; margin-bottom: 2px; }
.md code { font-family: var(--mono); font-size: 13px; background: var(--hover); padding: 1px 5px; border-radius: 4px; }
.md ul { padding-left: 22px; } .md li { margin: 5px 0; }
@media (max-width: 900px) { .find-grid { grid-template-columns: 1fr; } .toc { position: static; } }

/* ---------- overlays ---------- */
.tip { position: fixed; z-index: 50; pointer-events: none; background: var(--ink); color: var(--bg); border-radius: 7px; padding: 6px 10px; font-size: 13px; line-height: 1.45; max-width: 360px; white-space: pre-line; }
.toast { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); z-index: 60; background: var(--ink); color: var(--bg); border-radius: 8px; padding: 9px 16px; font-size: 14px; }
.boot { position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; background: color-mix(in srgb, var(--bg) 88%, transparent); }
.boot-card { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 28px 32px; text-align: center; max-width: 360px; }
.boot-card b { display: block; margin-top: 14px; }
.boot-card p { color: var(--muted); margin: 6px 0 0; font-size: 14px; }
.spinner { width: 30px; height: 30px; margin: 0 auto; border-radius: 50%; border: 3px solid var(--line2); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- small screens ---------- */
@media (max-width: 700px) {
  .top-in { height: auto; flex-wrap: wrap; gap: 0 14px; padding: 10px 14px 0; }
  .brand { font-size: 15px; white-space: nowrap; }
  #theme { white-space: nowrap; }
  .tabs { order: 3; width: 100%; height: 42px; }
  .tabs button { flex: 1; padding: 0 8px; }
  main { padding: 20px 14px 50px; }
  .intro h1 { font-size: 21px; }
  .field.small { width: calc(50% - 7px); }
  .setup .btn.primary { width: 100%; }
  .lrow, .lindex { grid-template-columns: 84px minmax(0, 1fr); }
  .ev { grid-template-columns: 60px 30px 110px minmax(0, 1fr); }
  table.stats td:first-child { width: auto; }
  table.stats td:last-child { display: none; }
  .md { padding: 4px 16px 18px; }
}
