:root {
    --bg: #f5f7fb;
    --card: #ffffff;
    --text: #212529;
    --muted: #6c757d;
    --border: #e9ecef;
    --primary: #0d6efd;
    --primary-soft: #e7f1ff;
    --nav-hover: #f1f3f5;
    --shadow: 0 4px 18px rgba(0,0,0,0.04);
    --topbar-shadow: 0 2px 12px rgba(0,0,0,.04);
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

a { color: var(--primary); }

.topbar {
    position: sticky;
    top: 0;
    z-index: 100;
    background: #fff;
    border-bottom: 1px solid var(--border);
    box-shadow: var(--topbar-shadow);
}

.topbar-inner {
    max-width: 1120px;
    margin: 0 auto;
    min-height: 64px;
    padding: 0 16px;
    display: flex;
    align-items: center;
    gap: 20px;
}

.brand {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    text-decoration: none;
    color: var(--text);
    font-weight: 700;
    font-size: 20px;
    flex-shrink: 0;
}

.brand:hover { opacity: .9; }

.brand-logo {
    display: block;
    height: 40px;
    width: auto;
    max-width: none;
    object-fit: contain;
}

.brand-mark {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    object-fit: cover;
}

.weather-bar {
    position: absolute;
    left: 8px;
    top: 50%;
    transform: translateY(-50%);
    z-index: 2;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    max-width: min(260px, calc(28vw - 12px));
    min-height: 34px;
    padding: 4px 10px;
    border-radius: 999px;
    background: var(--primary-soft);
    color: var(--primary);
    text-decoration: none;
    font-size: 12px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
}
.weather-bar:hover { color: #0a58ca; background: #d6e6ff; }
.weather-bar[hidden] { display: none !important; }
.wb-city {
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 9ch;
}
.wb-sep { opacity: .55; }
.wb-vals {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    color: #345;
    font-weight: 600;
}
.wb-td { color: var(--primary); }

.nav {
    display: flex;
    align-items: center;
    gap: 8px;
    flex: 1;
    flex-wrap: wrap;
}

.nav-link {
    display: inline-flex;
    align-items: center;
    min-height: 38px;
    padding: 0 14px;
    border-radius: 10px;
    color: #495057;
    text-decoration: none;
    font-weight: 500;
    transition: all .15s ease;
}

.nav-link:hover {
    background: var(--nav-hover);
    color: var(--text);
}

.nav-link.active {
    background: var(--primary-soft);
    color: var(--primary);
    font-weight: 600;
}

.btn-login {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    margin-left: auto;
    min-height: 38px;
    padding: 0 16px;
    border-radius: 10px;
    background: var(--primary);
    color: #fff;
    text-decoration: none;
    font-weight: 600;
    font-size: 14px;
    white-space: nowrap;
}
.btn-login:hover {
    background: #0b5ed7;
    color: #fff;
}

.main {
    flex: 1;
    max-width: 1120px;
    width: 100%;
    margin: 0 auto;
    padding: 28px 16px 48px;
}

.page-card {
    background: var(--card);
    border-radius: 12px;
    padding: 24px;
    box-shadow: var(--shadow);
}

.page-card + .page-card { margin-top: 16px; }

h1 {
    margin: 0 0 8px;
    font-size: 28px;
    font-weight: 700;
}

h2 {
    margin: 0 0 12px;
    font-size: 20px;
    font-weight: 700;
}

.lead {
    margin: 0 0 24px;
    color: var(--muted);
    font-size: 15px;
    line-height: 1.5;
}

.muted { color: var(--muted); font-size: 13px; }

.grid-cards {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
    gap: 14px;
}

.tile {
    display: block;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 18px;
    text-decoration: none;
    color: inherit;
    box-shadow: var(--shadow);
    transition: border-color .15s, box-shadow .15s;
}

.tile:hover {
    border-color: #cfe2ff;
    box-shadow: 0 8px 24px rgba(13,110,253,.08);
}

.tile-title {
    font-weight: 700;
    font-size: 16px;
    margin-bottom: 6px;
}

.tile-desc {
    color: var(--muted);
    font-size: 13px;
    line-height: 1.4;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 40px;
    padding: 0 16px;
    border-radius: 10px;
    border: 1px solid transparent;
    font-weight: 600;
    font-size: 14px;
    text-decoration: none;
    cursor: pointer;
    font-family: inherit;
}

.btn-primary {
    background: var(--primary);
    color: #fff;
}

.btn-primary:hover { filter: brightness(.95); color: #fff; }

.btn-outline {
    background: #fff;
    border-color: #dee2e6;
    color: #495057;
}

.btn-outline:hover {
    background: var(--nav-hover);
    color: var(--text);
}

.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 14px;
    margin-bottom: 18px;
}

.field label {
    display: block;
    font-size: 13px;
    font-weight: 600;
    margin-bottom: 6px;
    color: #495057;
}

.field input,
.field select {
    width: 100%;
    height: 42px;
    padding: 0 12px;
    border: 1px solid #dee2e6;
    border-radius: 10px;
    font-size: 15px;
    background: #fff;
    font-family: inherit;
}

.field input:focus,
.field select:focus {
    outline: none;
    border-color: #86b7fe;
    box-shadow: 0 0 0 3px rgba(13,110,253,.15);
}

.result-box {
    background: var(--primary-soft);
    border: 1px solid #cfe2ff;
    border-radius: 12px;
    padding: 18px;
    margin-top: 8px;
}

.result-value {
    font-size: 32px;
    font-weight: 700;
    color: var(--primary);
    line-height: 1.1;
}

.result-unit {
    font-size: 16px;
    font-weight: 600;
    color: #495057;
    margin-left: 6px;
}

.result-hint {
    margin-top: 10px;
    color: #495057;
    font-size: 14px;
    line-height: 1.45;
}

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

.footer {
    border-top: 1px solid var(--border);
    background: #fff;
    padding: 16px;
    text-align: center;
    color: var(--muted);
    font-size: 13px;
}

.formula {
    background: #f8f9fa;
    border-radius: 10px;
    padding: 12px 14px;
    font-size: 13px;
    color: #495057;
    margin-top: 16px;
    line-height: 1.5;
}

.cat-title {
    margin: 28px 0 12px;
    font-size: 15px;
    font-weight: 700;
    color: #495057;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.cat-title:first-of-type { margin-top: 8px; }

.back-link {
    display: inline-block;
    margin-bottom: 12px;
    font-size: 13px;
    font-weight: 600;
    text-decoration: none;
}

.back-link:hover { text-decoration: underline; }

.result-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: 12px;
    margin-top: 8px;
}

.result-item {
    background: #fff;
    border: 1px solid #cfe2ff;
    border-radius: 10px;
    padding: 12px;
}

.result-item .lbl {
    font-size: 12px;
    color: #6c757d;
    margin-bottom: 4px;
}

.result-item .val {
    font-size: 20px;
    font-weight: 700;
    color: #0d6efd;
}

.table-mini {
    width: 100%;
    border-collapse: collapse;
    margin-top: 12px;
    font-size: 13px;
}

.table-mini th,
.table-mini td {
    border: 1px solid #e9ecef;
    padding: 8px 10px;
    text-align: left;
    vertical-align: top;
}

.table-mini th { background: #f8f9fa; }

/* Справочники: выровненные колонки */
.ref-table-wrap {
    overflow-x: auto;
    margin-top: 12px;
    -webkit-overflow-scrolling: touch;
}
.table-ref {
    table-layout: fixed;
    width: 100%;
    min-width: 720px;
    margin-top: 0;
}
.table-ref col.c-name { width: 20%; }
.table-ref col.c-num { width: 10%; }
.table-ref col.c-num-wide { width: 14%; }
.table-ref col.c-note { width: 38%; }
.table-ref .src {
    display: block;
    margin-top: 4px;
    font-size: 11px;
    color: #0d6efd;
    font-weight: 600;
    line-height: 1.3;
}
body.theme-dark .table-ref .src { color: #6ea8fe; }
.table-ref th {
    text-align: center;
    vertical-align: middle;
    line-height: 1.25;
    font-size: 12px;
}
.table-ref th.c-name,
.table-ref td.c-name { text-align: left; }
.table-ref td.c-num {
    text-align: center;
    white-space: nowrap;
    font-variant-numeric: tabular-nums;
}
.table-ref td.c-note {
    text-align: left;
    color: #495057;
    font-size: 12px;
    line-height: 1.35;
}
.table-ref td.c-empty { color: #adb5bd; text-align: center; }

.ref-toc {
    display: flex;
    flex-wrap: wrap;
    gap: 8px 12px;
    margin: 12px 0 8px;
    font-size: 13px;
}
.ref-toc a {
    text-decoration: none;
    padding: 4px 10px;
    background: var(--primary-soft);
    border-radius: 999px;
    font-weight: 600;
}
.ref-toc a:hover { text-decoration: underline; }
.cat-title { scroll-margin-top: 80px; }

body.theme-dark .table-ref td.c-note { color: #9aa4b2; }
body.theme-dark .table-ref td.c-empty { color: #6e7681; }

.result-warning {
    display: none;
    margin-top: 10px;
    padding: 10px 12px;
    border-radius: 10px;
    background: #fdecea;
    border: 1px solid #f5c2c7;
    color: #842029;
    font-size: 13px;
    line-height: 1.45;
}

.result-warning .warn-line + .warn-line {
    margin-top: 6px;
}

.source-box {
    margin: 12px 0 18px;
    padding: 12px 14px;
    background: #e7f1ff;
    border: 1px solid #cfe2ff;
    border-radius: 10px;
    font-size: 13px;
    color: #084298;
    line-height: 1.45;
}
.ref-disclaimer {
    margin-top: 0;
    margin-bottom: 8px;
}
body.theme-dark .source-box {
    background: #1a2740;
    border-color: #2a3a55;
    color: #9ec5fe;
}

.norm-ref {
    margin-top: 10px;
    font-size: 12px;
    color: #6c757d;
    line-height: 1.4;
}

.accuracy-badge {
    display: inline-block;
    margin-top: 8px;
    padding: 4px 10px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 700;
    background: #fff3cd;
    color: #856404;
    border: 1px solid #ffe69c;
}

.actions .btn + .btn { margin-left: 0; }

.details-toggle {
    margin: 8px 0 14px;
    font-size: 13px;
    font-weight: 600;
    color: #0d6efd;
    cursor: pointer;
    user-select: none;
}

.details-box {
    display: none;
    margin-bottom: 14px;
}

.details-box.open { display: block; }

.history-note {
    font-size: 12px;
    color: #6c757d;
    margin-top: 8px;
}

/* Горизонтальный скролл для широких таблиц (мобильные) */
.table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
    margin-top: 12px;
}
.table-wrap > .table-mini { margin-top: 0; }

@media print {
    .topbar, .footer, .back-link, .actions, .bottom-nav, nav,
    .form-grid, .lead, .formula, .details-toggle, .details-box,
    .history-note, #clearListBtn, .del-item { display: none !important; }
    .main { max-width: none; padding: 0; }
    .page-card { box-shadow: none; border: 0; padding: 0; }
    .result-box { break-inside: avoid; display: block !important; }
    body { background: #fff; }
}

body.theme-dark {
    --bg: #12151a;
    --card: #1c2128;
    --text: #e6edf3;
    --muted: #9aa4b2;
    --border: #2f3742;
    --primary: #4c8dff;
    --primary-soft: #1a2740;
    --nav-hover: #252b34;
    --shadow: 0 4px 18px rgba(0,0,0,0.25);
    --topbar-shadow: 0 2px 12px rgba(0,0,0,.35);
}
body.theme-dark .topbar,
body.theme-dark .footer { background: #161b22; }
body.theme-dark .field input,
body.theme-dark .field select { background: #161b22; color: var(--text); border-color: #2f3742; }
body.theme-dark .formula { background: #161b22; color: #9aa4b2; }
body.theme-dark .result-item { background: #161b22; border-color: #2a3a55; }
body.theme-dark .result-warning { background: #3a1d21; border-color: #6e2b32; color: #f1aeb5; }
body.theme-dark .accuracy-badge { background: #3a2e12; color: #ffda6a; border-color: #664d03; }
body.theme-dark .btn-outline { background: #161b22; border-color: #2f3742; color: #c9d1d9; }

/* ——— Мобильная вёрстка (планшеты / телефоны) ——— */
@media (max-width: 720px) {
    html { overflow-x: clip; }

    .topbar-inner {
        gap: 8px;
        padding: 8px 12px;
        min-height: 52px;
        flex-wrap: nowrap;
        align-items: center;
    }
    .brand { font-size: 17px; gap: 8px; }
    .brand-logo {
        height: 32px;
    }
    .brand-mark { width: 32px; height: 32px; }
    .brand span { max-width: 9ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

    .weather-bar {
        left: 4px;
        max-width: min(150px, 38vw);
        padding: 3px 8px;
        font-size: 11px;
        gap: 4px;
        pointer-events: auto;
    }
    .wb-city { max-width: 5ch; }
    .wb-rh { display: none; }
    .wb-sep { display: none; }

    .nav {
        flex: 1;
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        gap: 4px;
        padding-bottom: 2px;
        scrollbar-width: none;
        mask-image: linear-gradient(90deg, #000 90%, transparent);
    }
    .nav::-webkit-scrollbar { display: none; }
    .nav-link {
        flex-shrink: 0;
        padding: 0 10px;
        font-size: 13px;
        min-height: 34px;
    }
    .btn-login {
        min-height: 34px;
        padding: 0 12px;
        font-size: 13px;
    }

    .main {
        padding: 16px 12px 40px;
        overflow-x: clip;
        max-width: 100%;
    }

    .page-card {
        padding: 16px;
        border-radius: 10px;
        max-width: 100%;
        overflow-x: clip;
    }

    h1 { font-size: 22px; line-height: 1.25; overflow-wrap: anywhere; }
    h2 { font-size: 18px; }
    .lead { font-size: 14px; margin-bottom: 16px; overflow-wrap: anywhere; }
    .back-link { display: inline-block; margin-bottom: 10px; }

    .form-grid {
        grid-template-columns: 1fr;
        gap: 12px;
        margin-bottom: 14px;
    }

    .field label {
        font-size: 13px;
        line-height: 1.35;
        overflow-wrap: anywhere;
    }

    /* ≥16px — без автозума iOS при фокусе */
    .field input,
    .field select,
    .field textarea {
        width: 100%;
        max-width: 100%;
        height: 44px;
        font-size: 16px;
        border-radius: 10px;
    }

    .cat-title {
        margin: 20px 0 10px;
        font-size: 13px;
        letter-spacing: 0.03em;
        line-height: 1.35;
    }
    .cat-title .muted {
        display: block;
        margin-top: 4px;
        text-transform: none;
        letter-spacing: 0;
        font-weight: 500;
        font-size: 12px;
    }

    .actions {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 8px;
        margin-top: 4px;
    }
    .actions .btn {
        width: 100%;
        min-height: 44px;
        margin: 0;
    }
    /* 3-я кнопка на всю ширину при нечётном числе */
    .actions .btn:last-child:nth-child(odd) {
        grid-column: 1 / -1;
    }

    .result-box {
        padding: 14px;
        margin-top: 10px;
        max-width: 100%;
    }
    .result-value { font-size: 26px; word-break: break-word; }
    .result-unit { display: inline-block; margin-top: 2px; }
    .result-hint { font-size: 13px; overflow-wrap: anywhere; }

    .result-grid {
        grid-template-columns: 1fr 1fr;
        gap: 8px;
    }
    .result-item { padding: 10px; min-width: 0; }
    .result-item .lbl { font-size: 11px; line-height: 1.3; overflow-wrap: anywhere; }
    .result-item .val {
        font-size: 16px;
        line-height: 1.25;
        word-break: break-word;
    }
    .result-group-title {
        font-size: 11px !important;
        margin-top: 8px !important;
        overflow-wrap: anywhere;
    }

    .grid-cards {
        grid-template-columns: 1fr;
        gap: 10px;
    }
    .tile { padding: 14px; }

    .formula {
        font-size: 12px;
        padding: 10px 12px;
        overflow-wrap: anywhere;
    }

    /* Горизонтальный скролл только у широких таблиц */
    .table-wrap,
    .ref-table-wrap,
    .zeta-ref,
    #listBox,
    .result-box:has(.table-mini) {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        max-width: 100%;
        overscroll-behavior-x: contain;
    }
    .table-mini { font-size: 12px; width: 100%; }
    #listBox .table-mini,
    #histTable,
    .table-wrap .table-mini {
        min-width: 520px;
        width: max-content;
        max-width: none;
    }
    .zeta-ref .table-mini,
    #zetaTable {
        min-width: 260px;
        width: max-content;
        max-width: none;
    }
    .table-mini th,
    .table-mini td {
        padding: 6px 8px;
    }
    #listBox .table-mini th,
    #listBox .table-mini td,
    #histTable th,
    #histTable td {
        white-space: nowrap;
    }
    .zeta-ref .table-mini td:first-child {
        white-space: normal;
        min-width: 140px;
    }

    .zeta-ref {
        padding: 8px 10px;
        margin: 10px 0 4px;
    }
    .zeta-ref summary {
        font-size: 13px;
        line-height: 1.35;
        padding: 4px 0;
    }
    .zeta-ref .add-z {
        min-height: 36px;
        min-width: 36px;
        padding: 4px 10px !important;
        font-size: 14px !important;
    }

    .history-note { font-size: 11px; line-height: 1.4; }

    canvas,
    .chart-wrap,
    .psychro-chart-wrap,
    .psychro-chart,
    #chartBox,
    #tDChart {
        max-width: 100% !important;
        height: auto !important;
    }

    body.theme-dark .zeta-ref { background: #161b22; }
}

@media (max-width: 420px) {
    .actions { grid-template-columns: 1fr; }
    .actions .btn:last-child:nth-child(odd) { grid-column: auto; }
    .result-grid { grid-template-columns: 1fr; }
    .page-card { padding: 14px 12px; }
    .main { padding: 12px 10px 36px; }
    .brand span { display: none; }
    .brand-logo { height: 28px; }
    .result-item .val { font-size: 18px; }
    #listBox .table-mini,
    #histTable,
    .table-wrap .table-mini { min-width: 460px; }
}
