/* --- Общий стиль страницы --- */
body {
    background-color: #f8f9fa;
    background-image: linear-gradient(180deg, #f8f9fa 0%, #e9ecef 100%);
    padding: 20px;
    font-family: Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* Улучшения для touch-устройств */
@media (max-width: 768px) {
    body {
        padding: 10px;
    }
    
    /* Улучшенная область нажатия для кнопок */
    .btn {
        min-height: 44px; /* Минимальный размер для удобного нажатия на мобильных */
        touch-action: manipulation; /* Убирает задержку 300ms на touch-устройствах */
    }
    
    /* Улучшенная область нажатия для select и input */
    .form-select, .form-control {
        min-height: 44px;
        touch-action: manipulation;
    }
}

.container {
    background-color: transparent;
}

h2 { color: #343a40; font-weight: 700; }
h2 .header-sub { font-size: calc(1em - 8pt); font-weight: 700; margin-top: calc(1em - 2pt); }
.header-logo { max-width: 180px; height: auto; display: inline-block; }

.footer-logos { gap: 1rem; }

.footer-credit {
    font-size: 0.8rem;
    color: #6c757d;
    margin-top: 1.5rem;
    margin-bottom: 1rem;
}
.footer-credit a,
.footer-credit a:visited,
.footer-credit a:active,
.footer-credit a:focus,
.footer-credit a:focus-visible {
    color: inherit;
    text-decoration: none;
    outline: none;
}
@media (hover: hover) {
    .footer-credit a:hover {
        color: #0d6efd;
        text-decoration: underline;
    }
}
[data-bs-theme="dark"] .footer-credit { color: #adb5bd; }
@media (hover: hover) {
    [data-bs-theme="dark"] .footer-credit a:hover { color: #6ea8fe; }
}
.footer-logo {
    max-width: 45%;
    max-height: 80px;
    width: auto;
    height: auto;
    object-fit: contain;
}

/* Мобильные: главная строка заголовка на 1 pt меньше базового размера h2 */
@media (max-width: 575.98px) {
    h2 .header-main { font-size: calc(1em - 4pt); }
    h2 .header-sub { font-size: calc(1em - 9pt); }
    .header-logo { max-width: 144px; }
    .footer-credit { font-size: calc(0.8rem - 1pt); }
}
h4 { color: #495057; font-weight: 600; }

/* --- Таблица результатов --- */
.table thead { background-color: #495057; color: white; }
#resultTable td:first-child { position: sticky; left: 0; background-color: #fff; z-index: 2; }
#resultTable th:first-child { position: sticky; left: 0; top: 0; background-color: #495057; color: #fff; z-index: 5; }
#resultTable thead th { position: sticky; top: 0; background-color: #495057; color: #fff; z-index: 4; }
.table-responsive { overflow-x: auto; }
#resultTable { min-width: 800px; }

/* --- Мобильная (транспонированная) таблица результатов --- */
#resultTableMobile thead th { background-color: #495057; color: #fff; vertical-align: middle; }
#resultTableMobile tbody th {
    background-color: #f1f3f5;
    color: #212529;
    font-weight: 600;
    text-align: left;
    vertical-align: middle;
}

/* --- Кнопки --- */
.btn-primary { background-color: #495057; border: none; color: #fff; }
.btn-primary:hover,
.btn-primary:focus,
.btn-primary:active { background-color: #343a40 !important; border-color: #343a40 !important; color: #fff !important; }

/* --- Контейнер графика стоимости --- */
.chart-container {
    background: #f8f9fa;
    border: 2px solid #0d6efd;
    border-radius: 10px;
    padding: 20px;
    margin-top: 20px;
}

.chart-container h5,
.speed-animation-container h5,
.temp-stability-panel h5,
.emissions-panel h5,
.nozzle-note-panel h5 {
    color: #495057;
    font-weight: 600;
    text-align: center;
}

/* --- График --- */
#costChart { 
    display: block; 
    width: 100% !important; 
    max-width: 100%; 
    max-height: 400px;
    height: auto !important;
    margin-top: 0;
}

/* --- Блок эмиссий CO2 / NOx --- */
.emissions-panel {
    background: #f8f9fa;
    border: 2px solid #0d6efd;
    border-radius: 10px;
    padding: 20px;
    margin-top: 20px;
}

.emissions-source {
    color: #6c757d;
    font-size: 0.9rem;
    margin-bottom: 0.75rem;
}

.emissions-chart-wrap {
    position: relative;
    width: 100%;
    height: 280px;
    max-height: 400px;
}

#co2Chart,
#noxChart {
    display: block;
    width: 100% !important;
    max-width: 100%;
    height: 100% !important;
}

/* Адаптивность для мобильных устройств */
@media (max-width: 768px) {
    #costChart {
        max-height: 300px;
    }

    .emissions-chart-wrap {
        height: 240px;
        max-height: 300px;
    }
    
    /* Таблицы на мобильных */
    #resultTable {
        font-size: 12px;
    }
    
    #resultTable th {
        padding: 8px 4px;
        font-size: 11px;
    }
    
    #resultTable td {
        padding: 8px 4px;
        font-size: 11px;
    }

    /* Мобильная транспонированная таблица */
    #resultTableMobile {
        font-size: 12px;
        margin-bottom: 0;
    }

    #resultTableMobile th,
    #resultTableMobile td {
        padding: 8px 6px;
        font-size: 12px;
        vertical-align: middle;
    }

    #resultTableMobile tbody th {
        font-size: 11px;
        line-height: 1.2;
        white-space: normal;
    }
    
    /* Формы на мобильных */
    .form-label {
        font-size: 14px;
    }
    
    .form-control, .form-select {
        font-size: 16px; /* Предотвращает зум на iOS */
    }
    
    /* Кнопки на мобильных */
    .btn-lg {
        padding: 12px 24px;
        font-size: 16px;
    }
    
    /* Контейнеры на мобильных */
    .container {
        padding-left: 10px;
        padding-right: 10px;
    }
    
    .card {
        padding: 15px !important;
    }
}

@media (max-height: 800px) {
    #costChart {
        max-height: 250px;
    }
}

@media (max-height: 600px) {
    #costChart {
        max-height: 200px;
    }
}

/* --- Лейблы --- */
.label { color: black; font-size: 1rem; display: inline-block; transition: all 0.5s ease; }
.label-active { color: blue !important; font-size: 1.15rem !important; }

/* --- Счётчик посещений --- */
.counter {
    position: fixed;
    bottom: 15px;
    right: 20px;
    background: rgba(255, 255, 255, 0.9);
    border: 1px solid #ccc;
    border-radius: 10px;
    padding: 8px 14px;
    font-size: 14px;
    color: #333;
    box-shadow: 0 3px 10px rgba(0,0,0,0.2);
    z-index: 9999;
}
.counter span { font-weight: bold; color: #007bff; transition: transform 0.3s ease; }

/* --- Контейнер анимации скорости резки / блок ресурса дюзы --- */
.speed-animation-container,
.nozzle-note-panel {
    background: #f8f9fa;
    border: 2px solid #0d6efd;
    border-radius: 10px;
    padding: 20px;
    margin-top: 20px;
}

.speed-track-container {
    display: flex;
    flex-direction: column;
    gap: 25px;
    margin-top: 20px;
    overflow: visible;
}

.speed-line-wrapper {
    display: grid;
    grid-template-columns: 120px 1fr 100px;
    align-items: center;
    gap: 15px;
}

.speed-line-label {
    font-weight: 600;
    font-size: 14px;
    color: #333;
    text-align: right;
}

.speed-track {
    position: relative;
    height: 50px;
    background-color: #e9ecef;
    background-image: linear-gradient(to right, #e9ecef 0%, #dee2e6 100%);
    border: 2px solid #ced4da;
    border-radius: 25px;
    overflow: visible;
    box-shadow: inset 0 2px 4px rgba(0,0,0,0.1);
    padding-right: 20px;
    box-sizing: border-box;
}

.speed-line {
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    height: 6px;
    width: 0;
    transition: width linear;
    z-index: 2;
    border-radius: 3px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

.speed-line-indicator {
    position: absolute;
    right: -10px;
    top: 50%;
    transform: translateY(-50%);
    width: 20px;
    height: 20px;
    border-radius: 50%;
    border: 2px solid #fff;
    box-shadow: 0 2px 6px rgba(0,0,0,0.3);
    z-index: 3;
    transition: all 0.2s ease;
}

/* Цвета для каждого газа (синхронизированы с графиком стоимости) */
#line-acetylene {
    background: linear-gradient(to right, #495057, #343a40);
}

#line-acetylene .speed-line-indicator {
    background: #495057;
}

#line-propane {
    background: linear-gradient(to right, #adb5bd, #868e96);
}

#line-propane .speed-line-indicator {
    background: #adb5bd;
}

#line-hydrogen {
    background: linear-gradient(to right, #28A745, #1E7E34);
}

#line-hydrogen .speed-line-indicator {
    background: #28A745;
}

/* Темная тема: ацетилен подсвечиваем чуть светлее для контраста с тёмным фоном */
[data-bs-theme="dark"] #line-acetylene {
    background: linear-gradient(to right, #6c757d, #495057);
}
[data-bs-theme="dark"] #line-acetylene .speed-line-indicator {
    background: #6c757d;
}

.speed-result {
    font-weight: 600;
    font-size: 14px;
    color: #198754;
    text-align: left;
    min-width: 80px;
    transition: opacity 0.3s ease;
}

/* Цвет чисел ацетилена и пропана = цвет подписей газов (.speed-line-label).
   Водород — остаётся зелёным по правилу .speed-result выше. */
#result-acetylene,
#result-propane { color: #333; }

[data-bs-theme="dark"] #result-acetylene,
[data-bs-theme="dark"] #result-propane { color: var(--app-text); }

.speed-result.updating {
    opacity: 0.5;
}

/* Адаптивность */
@media (max-width: 768px) {
    .speed-animation-container,
    .nozzle-note-panel {
        padding: 15px;
    }
    
    .speed-line-wrapper {
        grid-template-columns: 90px 1fr 70px;
        gap: 8px;
        overflow: visible;
    }
    
    .speed-line-label {
        font-size: 12px;
        text-align: left !important;
    }
    
    .speed-result {
        font-size: 11px;
        min-width: 60px;
        margin-left: 30px; /* Сдвигаем надписи вправо на 30px (было 20px), чтобы график не наезжал */
    }
    
    .speed-track {
        padding-right: 15px;
        margin-left: -25px; /* Расширение влево на 25px */
        margin-right: -35px; /* Расширение вправо на 35px (увеличено для предотвращения выхода линии) */
        width: calc(100% + 60px); /* 25px + 35px = 60px */
    }
    
    .speed-line-indicator {
        width: 18px;
        height: 18px;
        right: -8px;
        border-width: 2px;
    }
}

/* === CSS-переменные тем === */
:root,
[data-bs-theme="light"] {
    --app-bg: #ffffff;
    --app-text: #212529;
    --app-card-bg: #f8f9fa;
    --app-border: #dee2e6;
    --app-track-bg: #e9ecef;
    --app-counter-bg: rgba(0, 0, 0, 0.05);
}

[data-bs-theme="dark"] {
    --app-bg: #1a1d21;
    --app-text: #e9ecef;
    --app-card-bg: #2b3035;
    --app-border: #495057;
    --app-track-bg: #343a40;
    --app-counter-bg: rgba(255, 255, 255, 0.08);
}

body {
    background-color: var(--app-bg);
    color: var(--app-text);
    transition: background-color 0.2s ease, color 0.2s ease;
}

[data-bs-theme="dark"] .bg-light {
    background-color: var(--app-card-bg) !important;
    color: var(--app-text);
    border-color: var(--app-border) !important;
}

[data-bs-theme="dark"] .speed-track {
    background-image: none;
    background-color: var(--app-track-bg);
    border-color: var(--app-border);
}
[data-bs-theme="dark"] .counter {
    background-color: var(--app-counter-bg);
    color: var(--app-text);
}

/* === Шапка управления === */
.container { position: relative; }

/* Кнопка переключения темы всегда стоит в одном ряду с языками
   (и на десктопе, и на мобильных). */

/* === Языковой переключатель (ряд кнопок) === */
.lang-switch {
    margin-top: -0.25rem;
}

.lang-switch .lang-btn {
    min-width: 48px;
    font-weight: 600;
    letter-spacing: 0.5px;
    text-transform: uppercase;
    padding: 0.35rem 0.75rem;
}

.lang-switch .lang-btn.active {
    background-color: #495057;
    border-color: #495057;
    color: #fff;
    box-shadow: 0 2px 6px rgba(73, 80, 87, 0.35);
}

[data-bs-theme="dark"] .lang-switch .lang-btn.active {
    background-color: #adb5bd;
    border-color: #adb5bd;
    color: #1a1d21;
    box-shadow: 0 2px 6px rgba(173, 181, 189, 0.25);
}

/* Портрет телефона: все языки + тема в один ряд. */
@media (max-width: 575.98px) and (orientation: portrait) {
    .lang-switch {
        flex-wrap: nowrap !important;
        width: 100%;
        gap: 0.12rem !important;
    }
    .lang-switch .lang-btn,
    .lang-switch .theme-toggle {
        flex: 1 1 0;
        min-width: 0;
        min-height: 32px;
        padding: 0.2rem 0.06rem;
        font-size: 0.62rem;
        letter-spacing: 0;
        line-height: 1.1;
    }
    .lang-switch .theme-toggle {
        margin-left: 0;
    }
    .lang-switch #themeIcon {
        font-size: 0.72rem;
    }
}

@media (max-width: 360px) and (orientation: portrait) {
    .lang-switch {
        gap: 0.08rem !important;
    }
    .lang-switch .lang-btn,
    .lang-switch .theme-toggle {
        padding: 0.18rem 0.04rem;
        font-size: 0.58rem;
        min-height: 30px;
    }
}

/* === Dark theme: расширенное покрытие === */
[data-bs-theme="dark"] body {
    background-image: none;
    background-color: var(--app-bg);
    color: var(--app-text);
}

[data-bs-theme="dark"] #resultTable td:first-child {
    background-color: var(--app-card-bg);
    color: var(--app-text);
}
[data-bs-theme="dark"] #resultTable th:first-child,
[data-bs-theme="dark"] #resultTable thead th {
    background-color: #343a40;
    color: var(--app-text);
}

[data-bs-theme="dark"] .table thead {
    background-color: #343a40;
    color: var(--app-text);
}

[data-bs-theme="dark"] #resultTableMobile thead th {
    background-color: #343a40;
    color: var(--app-text);
}
[data-bs-theme="dark"] #resultTableMobile tbody th {
    background-color: var(--app-card-bg);
    color: var(--app-text);
}

[data-bs-theme="dark"] .btn-primary {
    background-color: #6c757d;
    border-color: #6c757d;
    color: #fff;
}
[data-bs-theme="dark"] .btn-primary:hover,
[data-bs-theme="dark"] .btn-primary:focus,
[data-bs-theme="dark"] .btn-primary:active {
    background-color: #868e96 !important;
    border-color: #868e96 !important;
    color: #fff !important;
}

[data-bs-theme="dark"] .label,
[data-bs-theme="dark"] .speed-line-label,
[data-bs-theme="dark"] .form-label {
    color: var(--app-text);
}

/* Перекрываем Bootstrap-утилиту .text-dark на лейблах в тёмной теме,
   иначе текст над полями (Толщина листа, Длина реза, Цены и т.д.)
   сливается с тёмным фоном. */
[data-bs-theme="dark"] .form-label.text-dark,
[data-bs-theme="dark"] label.text-dark,
[data-bs-theme="dark"] .text-dark {
    color: var(--app-text) !important;
}

[data-bs-theme="dark"] .card,
[data-bs-theme="dark"] .chart-container,
[data-bs-theme="dark"] .speed-animation-container,
[data-bs-theme="dark"] .emissions-panel,
[data-bs-theme="dark"] .nozzle-note-panel {
    background-color: var(--app-card-bg);
    border-color: var(--app-border);
    color: var(--app-text);
}

[data-bs-theme="dark"] .chart-container h5,
[data-bs-theme="dark"] .speed-animation-container h5,
[data-bs-theme="dark"] .temp-stability-panel h5,
[data-bs-theme="dark"] .emissions-panel h5,
[data-bs-theme="dark"] .nozzle-note-panel h5 {
    color: #ced4da;
}

[data-bs-theme="dark"] .emissions-source {
    color: #adb5bd;
}

[data-bs-theme="dark"] h2 { color: #e9ecef; }
[data-bs-theme="dark"] h4 { color: #ced4da; }

[data-bs-theme="dark"] .form-control,
[data-bs-theme="dark"] .form-select {
    background-color: var(--app-card-bg);
    color: var(--app-text);
    border-color: var(--app-border);
}

[data-bs-theme="dark"] .form-control::placeholder {
    color: #adb5bd;
}

[data-bs-theme="dark"] .btn-outline-secondary {
    color: var(--app-text);
    border-color: var(--app-border);
}
[data-bs-theme="dark"] .btn-outline-secondary:hover {
    background-color: var(--app-card-bg);
    color: var(--app-text);
}

[data-bs-theme="dark"] .table {
    --bs-table-bg: var(--app-card-bg);
    --bs-table-color: var(--app-text);
    --bs-table-striped-bg: rgba(255, 255, 255, 0.04);
    --bs-table-border-color: var(--app-border);
}

/* === Плавный переход тем для крупных контейнеров === */
.card,
.chart-container,
.speed-animation-container,
.emissions-panel,
.nozzle-note-panel {
    transition: background-color 0.2s ease, color 0.2s ease, border-color 0.2s ease;
}

/* === Temperature stability block === */
@property --flame-scale-y {
    syntax: '<number>';
    inherits: false;
    initial-value: 1;
}
@property --flame-opacity {
    syntax: '<number>';
    inherits: false;
    initial-value: 1;
}
@property --flame-brightness {
    syntax: '<number>';
    inherits: false;
    initial-value: 1;
}
@property --flame-saturate {
    syntax: '<number>';
    inherits: false;
    initial-value: 1;
}
@property --flame-hue {
    syntax: '<angle>';
    inherits: false;
    initial-value: 0deg;
}
@property --flame-tx {
    syntax: '<length>';
    inherits: false;
    initial-value: 0px;
}
@property --flame-scale-x {
    syntax: '<number>';
    inherits: false;
    initial-value: 1;
}

@keyframes flame-flicker {
    0%, 100% {
        --flame-flicker-y: 1;
        --flame-flicker-brightness: 1;
        --flame-flicker-saturate: 1;
    }
    35% {
        --flame-flicker-y: 0.95;
        --flame-flicker-brightness: 1.04;
        --flame-flicker-saturate: 1.06;
    }
    70% {
        --flame-flicker-y: 1.03;
        --flame-flicker-brightness: 0.98;
        --flame-flicker-saturate: 0.97;
    }
}

/* Ацетилен при T < −5 °C: размах длины −10%…+5% (яркость/насыщенность как в базовом) */
@keyframes flame-flicker-acetylene-cold {
    0%, 100% {
        --flame-flicker-y: 1;
        --flame-flicker-brightness: 1;
        --flame-flicker-saturate: 1;
    }
    35% {
        --flame-flicker-y: 0.90;
        --flame-flicker-brightness: 1.04;
        --flame-flicker-saturate: 1.06;
    }
    70% {
        --flame-flicker-y: 1.05;
        --flame-flicker-brightness: 0.98;
        --flame-flicker-saturate: 0.97;
    }
}

#temp-stability {
    max-width: 100%;
    min-height: 220px;
}

.temp-stability-panel {
    background: #f8f9fa;
    border: 2px solid #0d6efd;
    border-radius: 10px;
    padding: 20px;
    margin-top: 20px;
    transition: background-color 0.2s ease, color 0.2s ease, border-color 0.2s ease;
}

.temp-slider-wrap {
    width: 100%;
    min-height: 44px;
    margin-bottom: 1rem;
    touch-action: manipulation;
}

.temp-slider {
    width: 100%;
    min-height: 44px;
    accent-color: #495057;
    -webkit-appearance: none;
    appearance: none;
    background: transparent;
    cursor: pointer;
    touch-action: manipulation;
}

.temp-slider::-webkit-slider-runnable-track {
    height: 8px;
    border-radius: 4px;
    border: 1px solid rgba(0, 0, 0, 0.12);
    /* −10 слева → +15 справа: тёмно-синий → светло-синий (0 °C ≈ 40%) → ярко-жёлтый */
    background: linear-gradient(
        to right,
        #1a4a7a 0%,
        #9ed0ef 40%,
        #fff3a8 72%,
        #ffe033 100%
    );
}

.temp-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: #495057;
    border: 2px solid #fff;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.25);
    margin-top: -18px;
}

.temp-slider::-moz-range-track {
    height: 8px;
    border-radius: 4px;
    border: 1px solid rgba(0, 0, 0, 0.12);
    background: linear-gradient(
        to right,
        #1a4a7a 0%,
        #9ed0ef 40%,
        #fff3a8 72%,
        #ffe033 100%
    );
}

.temp-slider::-moz-range-thumb {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: #495057;
    border: 2px solid #fff;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.25);
    cursor: pointer;
}

.temp-slider-scale {
    display: flex;
    justify-content: space-between;
    font-size: 0.85rem;
    color: #6c757d;
    margin-bottom: 0.25rem;
}

.temp-slider-value {
    display: block;
    text-align: center;
    font-weight: 600;
    font-size: 1rem;
    color: #495057;
    margin-top: 0.35rem;
}

.temp-flames-area {
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: flex-end;
    gap: 0.5rem;
    min-height: 100px;
    padding: 0.25rem 0;
    overflow: hidden;
}

.temp-flame-col {
    display: flex;
    flex-direction: column;
    align-items: center;
    flex: 1 1 0;
    min-width: 0;
    max-width: 120px;
}

.temp-flame-label {
    font-weight: 600;
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    margin-bottom: 0.35rem;
    color: #333;
}

.temp-flame-stage {
    position: relative;
    height: 64px;
    width: calc(144px * var(--flame-thickness, 1));
    overflow: hidden;
    flex-shrink: 0;
    --flame-thickness: 2.1; /* водород */
    --flame-length: 1;
    --flame-l: 1;
    --flame-glow: 1;
    --flame-red: 0;
    --flame-glow-g: calc(120 - 60 * var(--flame-red));
    filter: drop-shadow(0 0 6px rgba(255, var(--flame-glow-g), 0, calc(0.20 + 0.15 * var(--flame-glow))));
    transition: filter 0.45s ease;
}

.temp-flame-col[data-gas="propane"] .temp-flame-stage,
.temp-flame-col[data-gas="acetylene"] .temp-flame-stage {
    --flame-thickness: 1.785; /* 2.1 × 0.85 */
}

/* Якорь сопла: left/top = 0, без обрезки — длина через scale на img */
.temp-flame-clip {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: visible;
    pointer-events: none;
}

.temp-flame-img {
    width: calc(64px * var(--flame-thickness, 1));
    height: 64px;
    max-width: none;
    max-height: none;
    object-fit: contain;
    object-position: bottom left;
    display: block;
}

.temp-flame-rotator {
    display: contents;
}

.temp-flame-shape {
    --flame-flicker-y: 1;
    --flame-flicker-brightness: 1;
    --flame-flicker-saturate: 1;
    transform: translateX(var(--flame-tx, 0px))
        scale(var(--flame-scale-x, 1),
            calc(var(--flame-scale-y, 1) * var(--flame-flicker-y, 1)));
    opacity: var(--flame-opacity, 1);
    filter: brightness(calc(var(--flame-brightness, 1) * var(--flame-flicker-brightness, 1)))
        saturate(calc(var(--flame-saturate, 1) * var(--flame-flicker-saturate, 1)))
        hue-rotate(var(--flame-hue, 0deg));
    transform-origin: bottom left;
    transition: --flame-scale-x 0.45s ease, --flame-scale-y 0.45s ease,
        --flame-tx 0.45s ease, --flame-opacity 0.45s ease,
        --flame-brightness 0.45s ease, --flame-saturate 0.45s ease,
        --flame-hue 0.45s ease;
    will-change: transform, opacity, filter;
    /* 0.196s → 0.098s: частота мерцания ×2 */
    animation: flame-flicker 0.098s ease-in-out infinite;
}

.temp-flame-col[data-gas="propane"] .temp-flame-shape {
    animation-delay: 0s;
    filter: brightness(calc(var(--flame-brightness, 1) * var(--flame-flicker-brightness, 1)))
        saturate(calc(var(--flame-saturate, 1) * var(--flame-flicker-saturate, 1) * 1.15))
        sepia(calc(0.32 + 0.5 * var(--flame-yellow, 0)))
        hue-rotate(calc(var(--flame-hue, 0deg) + 22deg * var(--flame-yellow, 0)));
}

.temp-flame-col[data-gas="propane"] .temp-flame-stage {
    filter: drop-shadow(0 0 8px rgba(255, 175, 25, calc(0.16 + 0.4 * var(--flame-yellow, 0))))
        drop-shadow(0 0 5px rgba(255, 200, 50, calc(0.1 + 0.28 * var(--flame-yellow, 0))));
}

.temp-flame-col[data-gas="acetylene"].flame-flicker-cold .temp-flame-shape {
    animation-name: flame-flicker-acetylene-cold;
}

.temp-flame-col[data-gas="acetylene"] .temp-flame-shape {
    animation-delay: -0.0285s;
    filter: brightness(calc(var(--flame-brightness, 1) * var(--flame-flicker-brightness, 1)))
        saturate(calc(var(--flame-saturate, 1) * var(--flame-flicker-saturate, 1)))
        sepia(calc(0.15 + 0.72 * var(--flame-red, 0)))
        hue-rotate(calc(var(--flame-hue, 0deg) - 32deg * var(--flame-red, 0)));
}

.temp-flame-col[data-gas="acetylene"] .temp-flame-stage {
    --flame-glow-g: calc(155 - 135 * var(--flame-red, 0));
    filter: drop-shadow(0 0 8px rgba(255, calc(50 + 130 * var(--flame-red, 0)), 20, calc(0.22 + 0.38 * var(--flame-glow))))
        drop-shadow(0 0 5px rgba(255, 45, 35, calc(0.12 + 0.5 * var(--flame-red, 0))));
}

.temp-flame-col[data-gas="hydrogen"] .temp-flame-shape {
    animation-delay: -0.057s;
}

#temp-stability.temp-flames-paused .temp-flame-shape {
    animation-play-state: paused;
}

@media (prefers-reduced-motion: reduce) {
    .temp-flame-shape {
        animation: none;
        --flame-flicker-y: 1;
        --flame-flicker-brightness: 1;
        --flame-flicker-saturate: 1;
    }
}

@media (max-width: 575.98px) {
    #temp-stability {
        min-height: 0;
    }

    .temp-stability-panel {
        padding: 15px;
        overflow-x: hidden;
        overflow-y: hidden;
    }

    .temp-flames-area {
        gap: 0.25rem;
        min-height: 0;
        overflow: hidden;
    }

    .temp-flame-label {
        font-size: 0.62rem;
        letter-spacing: 0.01em;
        margin-bottom: 0.15rem;
        text-align: center;
        line-height: 1.15;
        padding: 0 0.1rem;
        max-width: 100%;
        overflow-wrap: anywhere;
    }

    .temp-flame-col {
        max-width: none;
        gap: 0;
    }

    .temp-flame-col[data-gas="propane"] .temp-flame-stage {
        --flame-thickness: 0.492; /* 0.579 × 0.85 */
    }

    .temp-flame-col[data-gas="acetylene"] .temp-flame-stage {
        --flame-thickness: 0.547; /* 0.643 × 0.85 */
    }

    .temp-flame-stage {
        --flame-thickness: 0.514; /* водород, бывший 0.643 × 0.8 */
        display: flex;
        justify-content: flex-start;
        align-items: flex-end;
        margin-top: 0.2rem;
        height: 200px;
        width: 100%;
        max-width: 100%;
        overflow: hidden;
        filter: drop-shadow(0 0 4px rgba(255, var(--flame-glow-g), 0, calc(0.18 + 0.12 * var(--flame-glow))));
    }

    .temp-flame-clip {
        position: relative;
        left: auto;
        top: auto;
        width: 100%;
        height: 100%;
        display: flex;
        justify-content: flex-start;
        align-items: flex-end;
        overflow: hidden;
    }

    .temp-flame-img {
        /* Ширина арта 128px; толщину задаёт JS через scaleX — не умножать повторно. */
        width: 128px;
        max-width: none;
        height: 200px;
        margin-left: calc((100% - 128px * var(--flame-thickness, 1)) / 2);
        object-position: bottom left;
    }

    .temp-flame-shape {
        transform-origin: bottom left;
    }
}

/* Tablet / desktop: vertical stack, horizontal flames from the left */
@media (min-width: 576px) {
    .temp-flames-area {
        flex-direction: column;
        align-items: stretch;
        justify-content: flex-start;
        gap: 1rem;
        min-height: auto;
        width: calc(100% + 40px);
        margin-left: -20px;
        margin-right: -20px;
        padding: 0.5rem 20px;
        overflow: hidden;
    }

    .temp-flame-col {
        flex: 0 0 auto;
        flex-direction: column;
        align-items: stretch;
        justify-content: flex-start;
        width: 100%;
        max-width: none;
        gap: 0.15rem;
        height: auto;
    }

    .temp-flame-col[data-gas="acetylene"] { order: 1; }
    .temp-flame-col[data-gas="propane"]   { order: 2; }
    .temp-flame-col[data-gas="hydrogen"]  { order: 3; }

    .temp-flame-label {
        order: 1;
        flex-shrink: 0;
        font-size: 0.75rem;
        letter-spacing: 0.04em;
        margin-bottom: 0;
        text-align: left;
        line-height: 1.1;
    }

    .temp-flame-stage {
        order: 2;
        position: relative;
        display: block;
        width: 100%;
        flex: none;
        height: calc(48px * var(--flame-thickness, 1));
        overflow: hidden;
        filter: drop-shadow(0 0 4px rgba(255, var(--flame-glow-g), 0, calc(0.18 + 0.12 * var(--flame-glow))));
    }

    .temp-flame-clip {
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        overflow: visible;
    }

    .temp-flame-rotator {
        display: block;
        position: absolute;
        left: 0;
        top: 0;
        width: calc(72px * var(--flame-thickness, 1));
        height: 48px;
        overflow: visible;
        transform-origin: top left;
        transition: left 0.45s ease;
    }

    .temp-flame-img {
        position: absolute;
        left: 0;
        bottom: 0;
        top: auto;
        display: block;
        width: 72px;
        height: 48px;
        margin: 0;
        max-width: none;
        max-height: none;
        object-fit: contain;
        object-position: bottom left;
    }

    .temp-flame-shape {
        transform-origin: 22.5px 100%;
    }
}

[data-bs-theme="dark"] .temp-stability-panel {
    background-color: var(--app-card-bg);
    border-color: var(--app-border);
    color: var(--app-text);
}
[data-bs-theme="dark"] .temp-slider-scale { color: #adb5bd; }

[data-bs-theme="dark"] .temp-slider::-webkit-slider-runnable-track,
[data-bs-theme="dark"] .temp-slider::-moz-range-track {
    border-color: rgba(255, 255, 255, 0.15);
    background: linear-gradient(
        to right,
        #0f3058 0%,
        #6eb5e0 40%,
        #ffe680 72%,
        #ffd633 100%
    );
}
[data-bs-theme="dark"] .temp-flame-label { color: var(--app-text); }
[data-bs-theme="dark"] .temp-slider-value { color: var(--app-text); }

/* Light theme: пламя заметнее на белом фоне (краснее и темнее, без изменения JS) */
[data-bs-theme="light"] #temp-stability {
    --flame-light-hue: -16deg;
    --flame-light-saturate-mul: 1.22;
    --flame-light-brightness-mul: 0.78;
}

[data-bs-theme="light"] #temp-stability .temp-flame-shape {
    filter: brightness(calc(var(--flame-brightness, 1) * var(--flame-flicker-brightness, 1) * var(--flame-light-brightness-mul)))
        saturate(calc(var(--flame-saturate, 1) * var(--flame-flicker-saturate, 1) * var(--flame-light-saturate-mul)))
        hue-rotate(calc(var(--flame-hue, 0deg) + var(--flame-light-hue)));
}

[data-bs-theme="light"] #temp-stability .temp-flame-col[data-gas="propane"] .temp-flame-shape {
    --flame-light-hue: 48deg;
    --flame-light-saturate-mul: 1.85;
    filter: brightness(calc(var(--flame-brightness, 1) * var(--flame-flicker-brightness, 1) * var(--flame-light-brightness-mul)))
        sepia(calc(0.42 + 0.5 * var(--flame-yellow, 0)))
        saturate(calc(var(--flame-saturate, 1) * var(--flame-flicker-saturate, 1) * var(--flame-light-saturate-mul)))
        hue-rotate(calc(var(--flame-hue, 0deg) + var(--flame-light-hue) + 16deg * var(--flame-yellow, 0)));
}

[data-bs-theme="light"] #temp-stability .temp-flame-col[data-gas="propane"] .temp-flame-stage {
    --flame-glow-g: calc(195 - 55 * var(--flame-yellow, 0));
    filter: drop-shadow(0 0 10px rgba(255, 165, 20, calc(0.32 + 0.38 * var(--flame-yellow, 0))))
        drop-shadow(0 0 6px rgba(255, 210, 45, calc(0.2 + 0.3 * var(--flame-glow))))
        drop-shadow(0 0 5px rgba(255, var(--flame-glow-g), 0, calc(0.18 + 0.12 * var(--flame-glow))));
}

/* Светлая тема: ниже −5 °C ацетилен явно красный.
   --flame-red-boost разгоняет JS-переменную --flame-red так, чтобы при −5 °C (red=0.5)
   она уже достигала 1, а ниже — оставалась 1. Это даёт чёткий красный, а выше нуля
   поведение остаётся прежним. */
[data-bs-theme="light"] #temp-stability .temp-flame-col[data-gas="acetylene"] {
    --flame-red-boost: clamp(0, calc(var(--flame-red, 0) * 2), 1);
}

[data-bs-theme="light"] #temp-stability .temp-flame-col[data-gas="acetylene"] .temp-flame-shape {
    --flame-light-hue: -58deg;
    --flame-light-saturate-mul: calc(1.65 + 0.95 * var(--flame-red-boost, 0));
    --flame-light-brightness-mul: calc(0.78 + 0.18 * var(--flame-red-boost, 0));
    filter: brightness(calc(var(--flame-brightness, 1) * var(--flame-flicker-brightness, 1) * var(--flame-light-brightness-mul)))
        sepia(calc(0.25 + 0.75 * var(--flame-red-boost, 0)))
        saturate(calc(var(--flame-saturate, 1) * var(--flame-flicker-saturate, 1) * var(--flame-light-saturate-mul)))
        hue-rotate(calc(
            (var(--flame-hue, 0deg) + var(--flame-light-hue)) * (1 - var(--flame-red-boost, 0))
            + -38deg * var(--flame-red-boost, 0)
        ));
}

[data-bs-theme="light"] #temp-stability .temp-flame-col[data-gas="acetylene"] .temp-flame-stage {
    --flame-glow-g: calc(40 - 35 * var(--flame-red-boost, 0));
    filter: drop-shadow(0 0 10px rgba(255, calc(60 - 50 * var(--flame-red-boost, 0)), 25, calc(0.38 + 0.32 * var(--flame-glow))))
        drop-shadow(0 0 6px rgba(220, calc(45 - 35 * var(--flame-red-boost, 0)), 35, calc(0.2 + 0.55 * var(--flame-red-boost, 0))));
}

[data-bs-theme="light"] #temp-stability .temp-flame-col[data-gas="hydrogen"] .temp-flame-shape {
    --flame-light-brightness-mul: 0.94;
    --flame-light-saturate-mul: 1.4;
    --flame-light-hue: 14deg;
    filter: brightness(calc(var(--flame-brightness, 1) * var(--flame-flicker-brightness, 1) * var(--flame-light-brightness-mul)))
        saturate(calc(var(--flame-saturate, 1) * var(--flame-flicker-saturate, 1) * var(--flame-light-saturate-mul)))
        hue-rotate(calc(var(--flame-hue, 0deg) + var(--flame-light-hue)))
        contrast(1.15);
}

[data-bs-theme="light"] #temp-stability .temp-flame-col[data-gas="hydrogen"] .temp-flame-stage {
    --flame-glow-g: calc(130 - 50 * var(--flame-red));
    filter: drop-shadow(0 0 8px rgba(90, 170, 255, calc(0.38 + 0.18 * var(--flame-glow))))
        drop-shadow(0 0 5px rgba(255, var(--flame-glow-g), 0, calc(0.24 + 0.14 * var(--flame-glow))));
}

[data-bs-theme="light"] #temp-stability .temp-flame-stage {
    --flame-glow-g: calc(95 - 55 * var(--flame-red));
    filter: drop-shadow(0 0 7px rgba(255, var(--flame-glow-g), 0, calc(0.30 + 0.20 * var(--flame-glow))));
}

@media (max-width: 575.98px) {
    [data-bs-theme="light"] #temp-stability .temp-flame-stage {
        filter: drop-shadow(0 0 6px rgba(255, var(--flame-glow-g), 0, calc(0.28 + 0.18 * var(--flame-glow))));
    }

    [data-bs-theme="light"] #temp-stability .temp-flame-col[data-gas="hydrogen"] .temp-flame-stage {
        filter: drop-shadow(0 0 8px rgba(90, 170, 255, calc(0.42 + 0.2 * var(--flame-glow))))
            drop-shadow(0 0 5px rgba(255, var(--flame-glow-g), 0, calc(0.26 + 0.14 * var(--flame-glow))));
    }
}

@media (min-width: 576px) {
    [data-bs-theme="light"] #temp-stability .temp-flame-stage {
        filter: drop-shadow(0 0 6px rgba(255, var(--flame-glow-g), 0, calc(0.28 + 0.18 * var(--flame-glow))));
    }
}
