:root {
    --ff: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    --bg: #f4f5f8;
    --surface: #ffffff;
    --surface-2: #eef0f4;
    --text: #1b1d22;
    --muted: #5d6470;
    --border: #d9dde4;
    --accent: #3b5bdb;
    --accent-ink: #ffffff;
    --accent-soft: #e6ebff;
    --done: #2b8a3e;
    --done-soft: #e3f5e7;
    --edge: #b4bbc7;
    --lock: #a85d00;
    --danger: #c92a2a;
    --shadow: 0 6px 24px rgba(20, 24, 36, 0.12);
    --radius: 10px;
    color-scheme: light;
}

[data-theme="dark"] {
    --bg: #0f1115;
    --surface: #181b21;
    --surface-2: #21252d;
    --text: #e6e8ec;
    --muted: #9aa2ae;
    --border: #2f343e;
    --accent: #7c93ff;
    --accent-ink: #0f1115;
    --accent-soft: #232b4a;
    --done: #51cf66;
    --done-soft: #17301d;
    --edge: #4a515e;
    --lock: #f0b44c;
    --danger: #ff6b6b;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
    margin: 0;
    font-family: var(--ff);
    font-size: 15px;
    background: var(--bg);
    color: var(--text);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

button { font: inherit; color: inherit; }
a { color: var(--accent); }
[hidden] { display: none !important; }

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

/* ---------- Top bar ---------- */
.topbar {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 10px 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    flex-wrap: wrap;
    position: relative;
    z-index: 20;
}

.brand { display: flex; flex-direction: column; line-height: 1.15; margin-right: 4px; }
.brand h1 { margin: 0; font-size: 17px; letter-spacing: -0.2px; }
.brand span { font-size: 12px; color: var(--muted); }

.progress { min-width: 150px; }
.progress-text { font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.progress-text strong { color: var(--text); font-size: 15px; }
.bar { height: 6px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: var(--done); border-radius: inherit; transition: width .3s ease; }

.seg {
    display: inline-flex;
    background: var(--surface-2);
    border-radius: 8px;
    padding: 3px;
    margin-left: auto;
}
.seg button {
    border: 0;
    background: transparent;
    padding: 6px 14px;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 600;
    font-size: 13.5px;
    color: var(--muted);
}
.seg button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }

.search { position: relative; }
.search input {
    width: 260px;
    padding: 8px 12px;
    padding-right: 40px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg);
    color: var(--text);
    font: inherit;
    font-size: 14px;
}
.search input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.search-count {
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 11.5px;
    font-weight: 600;
    background: var(--accent-soft);
    color: var(--accent);
    padding: 2px 7px;
    border-radius: 99px;
    pointer-events: none;
}

.icon-btn {
    width: 36px;
    height: 36px;
    display: inline-grid;
    place-items: center;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
    cursor: pointer;
    list-style: none;
}
.icon-btn::-webkit-details-marker { display: none; }
.icon-btn:hover { background: var(--surface-2); }
.icon-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.menu .icon-btn svg { fill: currentColor; stroke: none; }

.menu { position: relative; }
.menu-pop {
    position: absolute;
    right: 0;
    top: calc(100% + 6px);
    min-width: 210px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 6px;
    display: flex;
    flex-direction: column;
}
.menu-pop button, .menu-pop a {
    text-align: left;
    padding: 8px 10px;
    border: 0;
    background: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
    text-decoration: none;
    color: var(--text);
}
.menu-pop button:hover, .menu-pop a:hover { background: var(--surface-2); }
.menu-pop .danger { color: var(--danger); }
.menu-pop hr { border: 0; border-top: 1px solid var(--border); margin: 6px 4px; width: auto; }

.btn {
    border: 1px solid var(--border);
    background: var(--surface);
    padding: 7px 12px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 13.5px;
    font-weight: 500;
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:disabled { opacity: .45; cursor: default; }

/* ---------- Main / views ---------- */
main { flex: 1; position: relative; min-height: 0; }
.view { position: absolute; inset: 0; }

/* ---------- Flowchart ---------- */
#view-chart { background:
    radial-gradient(circle, var(--border) 1px, transparent 1px) 0 0 / 22px 22px,
    var(--bg); }

#chart {
    width: 100%;
    height: 100%;
    display: block;
    touch-action: none;
    cursor: grab;
    user-select: none;
    -webkit-user-select: none;
}
#chart.panning { cursor: grabbing; }

.edge { fill: none; stroke: var(--edge); stroke-width: 1.6; transition: opacity .15s, stroke .15s; }
.edge.on-path { stroke: var(--accent); stroke-width: 3.2; }

.dnode, .enode { transition: opacity .15s; }
.box { fill: var(--surface); stroke: var(--border); stroke-width: 1; }
.dnode .box { filter: drop-shadow(0 1px 1.5px rgba(0,0,0,.08)); }
.dnode.root .box { fill: var(--accent); stroke: none; }
.dnode.root .step { fill: var(--accent-ink); font-weight: 700; }
.step { font: 13px var(--ff); fill: var(--text); }
.step.lock { fill: var(--lock); font-weight: 600; }
.bullet { fill: var(--muted); }
.sep { stroke: var(--border); stroke-dasharray: 3 3; }
.dnode.on-path .box { stroke: var(--accent); stroke-width: 2; }
.dnode.hit .box { stroke: var(--accent); stroke-width: 2.5; fill: var(--accent-soft); }

.enode { cursor: pointer; outline: none; }
.enode .box { stroke-width: 1.5; filter: drop-shadow(0 1px 2px rgba(0,0,0,.1)); }
.enode .stripe { fill: var(--route); }
.enode:hover .box { stroke: var(--accent); }
.enode:focus-visible .box { stroke: var(--accent); stroke-width: 3; }
.enode .meta { font: 11px var(--ff); fill: var(--muted); }
.enode .etitle { font: 600 13.5px var(--ff); fill: var(--text); }
.enode.done .box { fill: var(--done-soft); stroke: var(--done); }
.enode.selected .box { stroke: var(--accent); stroke-width: 3; }

.check { cursor: pointer; }
.check .hit { fill: transparent; }
.check .ring { fill: var(--surface); stroke: var(--muted); stroke-width: 1.5; transition: fill .15s, stroke .15s; }
.check path { fill: none; stroke: transparent; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.check:hover .ring { stroke: var(--done); }
.check:hover path { stroke: var(--done); }
.enode.done .check .ring { fill: var(--done); stroke: var(--done); }
.enode.done .check path { stroke: var(--surface); }

#chart.far .enode .box { fill: var(--route); stroke: var(--route); }
#chart.far .enode.done .box { fill: var(--done); stroke: var(--done); }
#chart.far .enode text, #chart.far .enode .check, #chart.far .enode .stripe { visibility: hidden; }

#chart.has-path .dnode:not(.on-path),
#chart.has-path .enode:not(.on-path),
#chart.has-path .edge:not(.on-path) { opacity: .3; }

#chart.searching .dnode:not(.match),
#chart.searching .enode:not(.match),
#chart.searching .edge:not(.match) { opacity: .15; }

.legend, .chart-controls, .chart-hint {
    position: absolute;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}
.legend {
    left: 14px;
    bottom: 14px;
    display: flex;
    gap: 14px;
    padding: 8px 12px;
    font-size: 12px;
    color: var(--muted);
    flex-wrap: wrap;
    max-width: calc(100% - 260px);
}
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 14px; height: 10px; border-radius: 3px; border: 1px solid var(--border); background: var(--surface); }
.legend .lg-start { background: var(--accent); border-color: var(--accent); }
.legend .lg-ending { border-left: 4px solid hsl(200 62% 52%); }
.legend .lg-done { background: var(--done-soft); border-color: var(--done); }
.legend .lg-lock { background: var(--lock); border-color: var(--lock); border-radius: 50%; width: 10px; }

.chart-controls {
    right: 14px;
    bottom: 14px;
    display: flex;
    overflow: hidden;
}
.chart-controls button {
    border: 0;
    border-right: 1px solid var(--border);
    background: none;
    min-width: 38px;
    height: 36px;
    cursor: pointer;
    font-size: 15px;
    font-variant-numeric: tabular-nums;
}
.chart-controls button:last-child { border-right: 0; padding: 0 12px; font-size: 13px; font-weight: 600; }
.chart-controls #zoom-level { font-size: 12.5px; min-width: 56px; }
.chart-controls button:hover { background: var(--surface-2); }

.chart-hint {
    top: 12px;
    left: 50%;
    transform: translateX(-50%);
    margin: 0;
    padding: 6px 12px;
    font-size: 12px;
    color: var(--muted);
    white-space: nowrap;
    pointer-events: none;
    box-shadow: none;
    opacity: .92;
}

/* ---------- Checklist ---------- */
#view-list { overflow-y: auto; }
.list-wrap { max-width: 860px; margin: 0 auto; padding: 18px 16px 40px; }
.list-toolbar { display: flex; gap: 8px; align-items: center; margin-bottom: 14px; flex-wrap: wrap; }
.toggle { display: inline-flex; align-items: center; gap: 6px; font-size: 14px; margin-left: auto; cursor: pointer; }
.toggle input { accent-color: var(--accent); width: 16px; height: 16px; }

.route {
    background: var(--surface);
    border: 1px solid var(--border);
    border-left: 4px solid var(--route);
    border-radius: var(--radius);
    margin-bottom: 10px;
    overflow: hidden;
    scroll-margin-top: 12px;
}
.route summary {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 14px;
    cursor: pointer;
    list-style: none;
    font-weight: 600;
}
.route summary::-webkit-details-marker { display: none; }
.route summary::after {
    content: "";
    width: 8px;
    height: 8px;
    border-right: 2px solid var(--muted);
    border-bottom: 2px solid var(--muted);
    transform: rotate(-45deg);
    transition: transform .15s;
    flex: none;
}
.route[open] summary::after { transform: rotate(45deg); }
.route summary:hover { background: var(--surface-2); }
.swatch { display: none; }
.rname { flex: 1; min-width: 0; }
.rprog { display: inline-flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--muted); font-weight: 500; font-variant-numeric: tabular-nums; }
.rbar { width: 70px; height: 5px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.rbar i { display: block; height: 100%; width: 0; background: var(--done); transition: width .3s; }
.route.complete .rname::after { content: " ✓"; color: var(--done); }

.elist { list-style: none; margin: 0; padding: 4px 8px 10px; border-top: 1px solid var(--border); }
.erow { display: flex; align-items: center; gap: 4px; border-radius: 8px; padding: 2px 6px; scroll-margin: 60px; }
.erow:hover { background: var(--surface-2); }
.erow.active { background: var(--accent-soft); }
.erow input { width: 18px; height: 18px; accent-color: var(--done); flex: none; cursor: pointer; margin: 0 6px 0 2px; }
.etitle-btn {
    flex: 1;
    display: flex;
    gap: 10px;
    align-items: baseline;
    text-align: left;
    border: 0;
    background: none;
    padding: 8px 4px;
    cursor: pointer;
    font-size: 14.5px;
}
.num { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 12.5px; font-weight: 600; }
.erow.done .etitle-text { color: var(--muted); text-decoration: line-through; text-decoration-color: var(--done); }

.empty { text-align: center; color: var(--muted); padding: 30px 0; }
.list-footer { margin-top: 24px; font-size: 13px; color: var(--muted); text-align: center; }

/* ---------- Details panel ---------- */
.detail {
    position: absolute;
    top: 12px;
    right: 12px;
    bottom: 12px;
    width: 380px;
    max-width: calc(100% - 24px);
    background: var(--surface);
    border: 1px solid var(--border);
    border-top: 4px solid var(--route, var(--accent));
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 14px 18px 16px;
    overflow-y: auto;
    z-index: 10;
    display: flex;
    flex-direction: column;
    animation: slide-in .18s ease;
}
@keyframes slide-in { from { transform: translateX(16px); opacity: 0; } }

.detail-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.detail-meta { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding-top: 6px; }
.chip {
    font-size: 12px;
    font-weight: 600;
    padding: 3px 9px;
    border-radius: 99px;
    background: var(--surface-2);
    border-left: 4px solid var(--route, var(--accent));
}
.detail h2 { font-size: 21px; margin: 10px 0 12px; line-height: 1.25; }
.detail h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 18px 0 8px; }

.btn-done { align-self: flex-start; font-weight: 600; }
.btn-done.is-done { background: var(--done); border-color: var(--done); color: var(--surface); }
.btn-done.is-done:hover { background: var(--done); filter: brightness(1.08); }

.steps { margin: 0; padding: 0; list-style: none; counter-reset: step; }
.steps li {
    counter-increment: step;
    position: relative;
    padding: 6px 0 6px 34px;
    font-size: 14px;
    line-height: 1.4;
}
.steps li::before {
    content: counter(step);
    position: absolute;
    left: 0;
    top: 5px;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: var(--surface-2);
    color: var(--muted);
    font-size: 11.5px;
    font-weight: 700;
    display: grid;
    place-items: center;
}
.steps li:not(:last-child)::after {
    content: "";
    position: absolute;
    left: 10.5px;
    top: 28px;
    bottom: -4px;
    width: 1px;
    background: var(--border);
}
.steps li.branch::before { background: var(--accent-soft); color: var(--accent); }
.steps li.lock { color: var(--lock); font-weight: 600; }
.steps li.lock::before { content: "🔒"; background: transparent; font-size: 13px; }
.steps li:last-child { font-weight: 600; }
.alts { display: block; font-size: 12.5px; color: var(--muted); font-weight: 400; margin-top: 2px; }

.elink {
    border: 0;
    background: var(--accent-soft);
    color: var(--accent);
    font-weight: 600;
    padding: 0 5px;
    border-radius: 5px;
    cursor: pointer;
    font-size: inherit;
}
.elink.done { background: var(--done-soft); color: var(--done); }

.shared {
    font-size: 13px;
    color: var(--muted);
    background: var(--surface-2);
    border-radius: 8px;
    padding: 8px 10px;
    margin: 12px 0 0;
}

.detail-nav {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    margin-top: auto;
    padding-top: 18px;
}
.detail-nav .btn { flex: 1; white-space: nowrap; }
#detail-locate { flex: 2; }

/* ---------- Toast ---------- */
.toast {
    position: fixed;
    left: 50%;
    bottom: 24px;
    transform: translateX(-50%);
    background: var(--text);
    color: var(--bg);
    padding: 10px 16px;
    border-radius: 8px;
    font-size: 14px;
    box-shadow: var(--shadow);
    z-index: 50;
}

/* ---------- Small screens ---------- */
@media (max-width: 900px) {
    .chart-hint { display: none; }
    .legend { display: none; }
}

@media (max-width: 760px) {
    .topbar { gap: 10px; padding: 8px 10px; }
    .brand span { display: none; }
    .progress { order: 5; flex: 1 1 100%; min-width: 0; display: flex; align-items: center; gap: 10px; }
    .progress-text { margin: 0; white-space: nowrap; }
    .progress .bar { flex: 1; }
    .seg { margin-left: auto; }
    .seg button { padding: 6px 10px; }
    .search { order: 6; flex: 1 1 0; min-width: 0; }
    .search input { width: 100%; }
    #theme-toggle, .menu { order: 7; }

    .detail {
        top: auto;
        left: 0;
        right: 0;
        bottom: 0;
        width: auto;
        max-width: none;
        max-height: 62%;
        border-radius: 14px 14px 0 0;
        animation-name: slide-up;
    }
    @keyframes slide-up { from { transform: translateY(24px); opacity: 0; } }

    .chart-controls { bottom: 10px; right: 10px; }
}

@media (prefers-reduced-motion: reduce) {
    * { transition: none !important; animation: none !important; }
}
