:root {
    --bg: #f4f5f1;
    --surface: #ffffff;
    --surface-2: #f8f9f6;
    --text: #1b211c;
    --muted: #5f6861;
    --border: #dde2da;
    --accent: #2f7d32;
    --accent-strong: #256628;
    --accent-soft: #e5f1e5;
    --warn-bg: #fff6d9;
    --warn-border: #ecc55b;
    --warn-text: #5e4500;
    --error-bg: #fdeaea;
    --error-border: #e9a3a3;
    --error-text: #7a1c1c;
    --tissue: rgb(40 110 255 / .45);
    --hole: rgb(255 190 0 / .8);
    --ignored: rgb(120 120 120 / .7);
    --contour: #ff2060;
    --shadow: 0 1px 2px rgb(0 0 0 / .05), 0 4px 16px rgb(0 0 0 / .04);
    --radius: 12px;
    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #111412;
        --surface: #191d1a;
        --surface-2: #1f2420;
        --text: #e5eae5;
        --muted: #9aa49c;
        --border: #2c332e;
        --accent: #6cc070;
        --accent-strong: #86d08a;
        --accent-soft: #1f3321;
        --warn-bg: #2d2611;
        --warn-border: #7a6120;
        --warn-text: #f1d890;
        --error-bg: #3a1b1b;
        --error-border: #7c3434;
        --error-text: #f5b8b8;
        --shadow: none;
        color-scheme: dark;
    }
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

h1, h2 { margin: 0; line-height: 1.25; }
h2 { font-size: 1rem; font-weight: 650; }
a { color: var(--accent); }
code, .mono { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: .92em; }
.muted { color: var(--muted); }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- каркас ---------- */

.topbar {
    display: flex;
    align-items: baseline;
    gap: 12px;
    flex-wrap: wrap;
    padding: 14px 24px;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
}

.brand {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    color: var(--text);
    text-decoration: none;
    font-weight: 700;
    font-size: 1.1rem;
}

.brand-mark { width: 22px; height: 22px; fill: var(--accent); align-self: center; }
.topbar-note { color: var(--muted); font-size: .9rem; }

.page {
    max-width: 1440px;
    margin: 0 auto;
    padding: 20px 24px 48px;
    display: grid;
    gap: 16px;
}

.layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 360px;
    gap: 16px;
    align-items: start;
}

.side { display: grid; gap: 12px; min-width: 0; }

.panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 16px;
    min-width: 0;
}

.panel-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
    margin-bottom: 12px;
}

/* ---------- загрузка ---------- */

.intro { display: grid; gap: 16px; }

.dropzone {
    display: grid;
    justify-items: center;
    gap: 6px;
    width: 100%;
    padding: 32px 16px;
    border: 2px dashed var(--border);
    border-radius: var(--radius);
    background: var(--surface-2);
    color: var(--text);
    font: inherit;
    cursor: pointer;
    transition: border-color .15s, background .15s;
}

.dropzone img { max-width: 100%; height: auto; margin-bottom: 8px; transition: transform .2s; }
.dropzone strong { font-size: 1.05rem; }
.dropzone:hover, .dropzone:focus-visible, .dropzone.is-over { border-color: var(--accent); background: var(--accent-soft); outline: none; }
.dropzone:hover img, .dropzone.is-over img { transform: rotate(-4deg) scale(1.03); }

.file-chip {
    margin: 0;
    padding: 8px 12px;
    border-radius: 8px;
    background: var(--accent-soft);
}

.tips { margin: 0; padding-left: 20px; color: var(--muted); display: grid; gap: 4px; }
.tips b { color: var(--text); }

/* ---------- итоги ---------- */

.summary { display: grid; gap: 14px; }

.stat-main { display: grid; gap: 2px; }
.stat-label { color: var(--muted); font-size: .85rem; }
.stat-value { font-size: 2rem; font-weight: 700; line-height: 1.15; font-variant-numeric: tabular-nums; color: var(--accent-strong); }
.stat-value small { font-size: 1rem; font-weight: 600; color: var(--muted); }
.stat-sub { font-variant-numeric: tabular-nums; }
.stat-sub > span { color: var(--muted); white-space: nowrap; }

.facts { margin: 0; display: grid; gap: 6px; border-top: 1px solid var(--border); padding-top: 12px; }
.facts > div { display: grid; grid-template-columns: 11em minmax(0, 1fr); gap: 8px; }
.facts dt { color: var(--muted); font-size: .88rem; }
.facts dd { margin: 0; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.facts .note { display: block; color: var(--muted); font-size: .82rem; }

/* ---------- сообщения ---------- */

.callout {
    padding: 10px 12px;
    border-radius: 10px;
    border: 1px solid var(--warn-border);
    background: var(--warn-bg);
    color: var(--warn-text);
    font-size: .92rem;
}

.callout-error { border-color: var(--error-border); background: var(--error-bg); color: var(--error-text); }

/* ---------- параметры ---------- */

.settings { display: grid; gap: 12px; }
.settings fieldset { margin: 0; padding: 0; border: 0; display: grid; gap: 6px; }
.settings fieldset.attention { padding: 8px; margin: -8px; border-radius: 10px; background: var(--warn-bg); }
.settings legend { padding: 0; margin-bottom: 4px; font-weight: 600; font-size: .9rem; }
.radio, .check { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.radio input, .check input { accent-color: var(--accent); width: 16px; height: 16px; margin: 0; }

.inline-input { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.inline-input input {
    width: 7.5em;
    padding: 6px 8px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface-2);
    color: var(--text);
    font: inherit;
    font-variant-numeric: tabular-nums;
}
.inline-input input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.inline-input input:disabled { opacity: .5; }

.hint { margin: 0; color: var(--muted); font-size: .84rem; }

.actions { display: flex; gap: 8px; flex-wrap: wrap; }

.button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 8px 14px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface-2);
    color: var(--text);
    font: inherit;
    font-weight: 600;
    text-decoration: none;
    cursor: pointer;
}
.button:hover { border-color: var(--accent); }
.button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.button.primary:hover { background: var(--accent-strong); }
@media (prefers-color-scheme: dark) { .button.primary { color: #0d1a0e; } }

/* ---------- просмотр ---------- */

.viewer-panel { display: grid; gap: 10px; }

.viewer-toolbar { display: flex; align-items: center; gap: 8px 16px; flex-wrap: wrap; font-size: .9rem; }
.toggle, .range { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.toggle input { accent-color: var(--accent); }
.range input { width: 110px; accent-color: var(--accent); }

.zoom { display: inline-flex; align-items: center; gap: 4px; margin-left: auto; font-variant-numeric: tabular-nums; }
.zoom span { min-width: 4.2em; text-align: center; }
.icon-button, .link-button {
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--text);
    border-radius: 8px;
    font: inherit;
    cursor: pointer;
}
.icon-button { width: 30px; height: 30px; font-size: 1.1rem; line-height: 1; }
.link-button { padding: 4px 10px; }

.viewport {
    overflow: auto;
    max-height: 78vh;
    border-radius: 8px;
    background: repeating-conic-gradient(var(--surface-2) 0 25%, var(--bg) 0 50%) 0 0 / 16px 16px;
    border: 1px solid var(--border);
}

.viewer { position: relative; width: calc(var(--zoom, 1) * 100%); margin: 0 auto; }
.viewer .layer { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.viewer img.layer:first-child, .viewer canvas.base { position: relative; }
.viewer .layer.is-hidden { display: none; }
.viewer.hide-overlay [data-layer="overlay"], .viewer.hide-labels [data-layer="labels"] { display: none; }

.labels { overflow: visible; pointer-events: none; }
.labels .leaf { pointer-events: auto; cursor: pointer; }
.labels rect { fill: transparent; stroke: transparent; stroke-width: 2px; vector-effect: non-scaling-stroke; }
.labels text {
    font: 700 var(--fs, 24px) system-ui, sans-serif;
    fill: #fff;
    stroke: rgb(20 20 20 / .75);
    stroke-width: calc(var(--fs, 24px) * .16);
    paint-order: stroke;
    text-anchor: middle;
    dominant-baseline: central;
}
.labels .leaf-cut rect { stroke: #ffb000; stroke-dasharray: 6 4; }
.labels .leaf.is-active rect { stroke: #ffea00; fill: rgb(255 234 0 / .12); }
.labels .leaf.is-active text { fill: #ffea00; }

.legend { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 4px 16px; font-size: .85rem; }
.legend li { display: inline-flex; align-items: center; gap: 6px; }
.swatch { width: 14px; height: 14px; border-radius: 4px; display: inline-block; }
.swatch-tissue { background: var(--tissue); }
.swatch-hole { background: var(--hole); }
.swatch-ignored { background: var(--ignored); }
.swatch-contour { background: transparent; border: 2px solid var(--contour); }

/* ---------- таблица ---------- */

.table-wrap { overflow-x: auto; }
.leaves { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.leaves th, .leaves td { padding: 7px 10px; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; }
.leaves thead th { font-size: .82rem; color: var(--muted); font-weight: 600; position: sticky; top: 0; background: var(--surface); }
.leaves .num { text-align: right; }
.leaves .strong { font-weight: 650; }
.leaves tbody tr { cursor: pointer; }
.leaves tbody tr:hover, .leaves tbody tr.is-active, .leaves tbody tr:focus-visible { background: var(--accent-soft); outline: none; }
.leaves tfoot th, .leaves tfoot td { font-weight: 700; border-bottom: 0; border-top: 2px solid var(--border); }
.leaves td:last-child { color: var(--warn-text); font-size: .85rem; }

/* ---------- методика ---------- */

.method summary { cursor: pointer; font-weight: 650; }
.method-body { max-width: 75ch; }
.method-body ul { padding-left: 20px; display: grid; gap: 6px; }
.formula { font-size: 1.1rem; font-family: Cambria, "Times New Roman", serif; font-style: italic; margin: 12px 0 4px; }

/* ---------- ожидание ---------- */

.busy {
    position: fixed;
    inset: 0;
    display: grid;
    place-items: center;
    background: rgb(0 0 0 / .25);
    z-index: 10;
}
.busy[hidden] { display: none; }
.busy-card { display: flex; align-items: center; gap: 12px; padding: 16px 20px; border-radius: var(--radius); background: var(--surface); box-shadow: 0 8px 32px rgb(0 0 0 / .2); }
.spinner { width: 20px; height: 20px; border-radius: 50%; border: 3px solid var(--accent-soft); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- узкие экраны ---------- */

@media (max-width: 960px) {
    .layout { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 560px) {
    .page { padding: 12px 16px 32px; }
    .topbar { padding: 12px 16px; }
    .facts > div { grid-template-columns: minmax(0, 1fr); gap: 0; }
    .zoom { margin-left: 0; }
    .stat-value { font-size: 1.7rem; }
}

/* ---------- Blazor ---------- */

.dropzone { position: relative; }
.file-overlay { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; cursor: pointer; }
.dropzone:focus-within { border-color: var(--accent); background: var(--accent-soft); }
label.button { position: relative; }
.labels.is-hidden { display: none; }

.app-loading { min-height: 60vh; display: flex; align-items: center; justify-content: center; gap: 12px; color: var(--muted); }

#blazor-error-ui {
    display: none;
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 20;
    padding: 12px 16px;
    background: var(--error-bg);
    color: var(--error-text);
    border-top: 1px solid var(--error-border);
}
#blazor-error-ui .dismiss { float: right; cursor: pointer; font-size: 1.2rem; line-height: 1; }
