:root {
    --bg: #f8fafc;
    --dot-color: #cbd5e1;
    --panel-bg: rgba(255, 255, 255, 0.95);
    --border: #cbd5e1;
    --text: #0f172a;
    --text-muted: #64748b;
    --accent: #0284c7;
    --accent-hover: #0369a1;
    --danger: #ef4444;
    --node-bg: #ffffff;
    --node-border: #0284c7;
    --group-bg: rgba(2, 132, 199, 0.05);
    --group-border: #0284c7;
    --edge-line: #94a3b8;
    --flow-color: #2563eb;
    --label-bg: #ffffff;
    --label-border: #cbd5e1;
    --label-text: #1e293b;
    --btn-bg: #f1f5f9;
    --btn-hover: #e2e8f0;
    --banner-bg: #ffffff;
    --banner-border: #e2e8f0;
}

body.theme-dark {
    --bg: #0b1120;
    --dot-color: #1e293b;
    --panel-bg: rgba(15, 23, 42, 0.95);
    --border: #334155;
    --text: #f8fafc;
    --text-muted: #94a3b8;
    --accent: #38bdf8;
    --accent-hover: #0284c7;
    --danger: #f43f5e;
    --node-bg: #1e293b;
    --node-border: #0284c7;
    --group-bg: rgba(14, 165, 233, 0.04);
    --group-border: #0284c7;
    --edge-line: #475569;
    --flow-color: #22c55e;
    --label-bg: #0f172a;
    --label-border: #334155;
    --label-text: #cbd5e1;
    --btn-bg: #1e293b;
    --btn-hover: #334155;
    --banner-bg: #0f172a;
    --banner-border: #1e293b;
}

* { box-sizing: border-box; margin: 0; padding: 0; user-select: none; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    overflow: hidden;
    width: 100vw;
    height: 100vh;
    display: flex;
}

/* Вертикальный тулбар слева */
#toolbar {
    position: absolute;
    top: 12px;
    left: 12px;
    bottom: 12px;
    width: 130px;
    z-index: 60;
    background: var(--panel-bg);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 10px 8px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    box-shadow: 0 4px 15px rgba(0,0,0,0.08);
    overflow-y: auto;
}

.btn {
    background: var(--btn-bg);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 6px 8px;
    font-size: 11px;
    font-weight: 500;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: flex-start;
    gap: 6px;
    width: 100%;
    white-space: nowrap;
    position: relative;
}
.btn:hover { background: var(--btn-hover); }
.btn.active { background: var(--accent); color: #ffffff; border-color: var(--accent); font-weight: 600; }
.btn-danger { color: var(--danger); border-color: rgba(239,68,68,0.3); }
.btn-danger:hover { background: var(--danger); color: #fff; }
.separator-h { width: 100%; height: 1px; background: var(--border); margin: 3px 0; }

.has-tooltip { position: relative; }
.has-tooltip::after {
    content: attr(data-tooltip);
    position: absolute;
    left: calc(100% + 8px);
    top: 50%;
    transform: translateY(-50%);
    background: #0f172a;
    color: #f8fafc;
    padding: 5px 8px;
    font-size: 11px;
    border-radius: 5px;
    white-space: nowrap;
    pointer-events: none;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.15s ease;
    z-index: 100;
    box-shadow: 0 4px 12px rgba(0,0,0,0.25);
}
.has-tooltip:hover::after { opacity: 1; visibility: visible; }

/* Баннеры */
#banner-top {
    position: absolute;
    top: 12px;
    left: 154px;
    right: 184px;
    height: 60px;
    background: var(--banner-bg);
    border: 1px dashed var(--banner-border);
    border-radius: 8px;
    z-index: 40;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    font-size: 12px;
    font-weight: 500;
    box-shadow: 0 2px 10px rgba(0,0,0,0.04);
    letter-spacing: 0.3px;
}

#banner-right {
    position: absolute;
    top: 12px;
    right: 12px;
    width: 160px;
    bottom: 12px;
    background: var(--banner-bg);
    border: 1px dashed var(--banner-border);
    border-radius: 8px;
    z-index: 40;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    font-size: 12px;
    font-weight: 500;
    text-align: center;
    padding: 16px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.04);
    line-height: 1.4;
}

#canvas-container {
    flex: 1;
    height: 100%;
    position: relative;
    overflow: hidden;
    background-image: radial-gradient(circle, var(--dot-color) 1.5px, transparent 1.5px);
    background-size: 24px 24px;
    touch-action: none; /* Запрещает нативный зум/скролл страницы браузером */
}
#viewport {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    transform-origin: 0 0;
    overflow: visible;
}

/* Группы */
.group-element {
    position: absolute;
    border: 2px dashed var(--group-color, var(--group-border));
    border-radius: 10px;
    cursor: move;
    background: var(--group-bg-custom, var(--group-bg));
}
.group-element.selected {
    box-shadow: 0 0 0 2px var(--group-color, var(--accent)) !important;
}
.group-element.collapsed {
    height: 36px !important;
    border-style: solid;
}
.group-header {
    padding: 6px 10px;
    border-radius: 8px 8px 0 0;
    font-size: 12px;
    font-weight: 600;
    color: var(--group-color, var(--accent));
    background: var(--group-header-bg, rgba(2, 132, 199, 0.15));
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.group-element.collapsed .group-header { border-radius: 8px; height: 100%; }
.collapse-btn {
    background: rgba(0,0,0,0.08);
    border: none;
    color: var(--text);
    font-size: 11px;
    padding: 2px 6px;
    border-radius: 4px;
    cursor: pointer;
}
body.theme-dark .collapse-btn { background: rgba(255,255,255,0.12); color: #fff; }

.resizer { position: absolute; background: transparent; z-index: 15; }
.resizer.r-e  { top: 6px; right: -4px; width: 8px; bottom: 6px; cursor: e-resize; }
.resizer.r-w  { top: 6px; left: -4px; width: 8px; bottom: 6px; cursor: w-resize; }
.resizer.r-s  { left: 6px; bottom: -4px; height: 8px; right: 6px; cursor: s-resize; }
.resizer.r-n  { left: 6px; top: -4px; height: 8px; right: 6px; cursor: n-resize; }
.resizer.r-se { right: -5px; bottom: -5px; width: 12px; height: 12px; cursor: se-resize; }
.resizer.r-sw { left: -5px; bottom: -5px; width: 12px; height: 12px; cursor: sw-resize; }
.resizer.r-ne { right: -5px; top: -5px; width: 12px; height: 12px; cursor: ne-resize; }
.resizer.r-nw { left: -5px; top: -5px; width: 12px; height: 12px; cursor: nw-resize; }
.resizer.corner::after {
    content: "";
    position: absolute;
    width: 6px;
    height: 6px;
    background: var(--group-color, var(--accent));
    border-radius: 1px;
}
.resizer.r-se::after { right: 2px; bottom: 2px; }
.resizer.r-sw::after { left: 2px; bottom: 2px; }
.resizer.r-ne::after { right: 2px; top: 2px; }
.resizer.r-nw::after { left: 2px; top: 2px; }

/* SVG-слой связей */
svg#edges-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    overflow: visible;
    pointer-events: none;
    z-index: 20;
}

.edge-line {
    fill: none;
    stroke: var(--edge-line);
    stroke-width: 2px;
    pointer-events: stroke;
    cursor: pointer;
}
.edge-line:hover, .edge-line.selected {
    stroke: var(--accent);
    stroke-width: 3px;
}

@keyframes flowAnim {
    from { stroke-dashoffset: 32; }
    to { stroke-dashoffset: 0; }
}
.edge-flow-particles {
    fill: none;
    stroke: var(--flow-color);
    stroke-width: 2.5px;
    stroke-linecap: round;
    stroke-dasharray: 6, 26;
    animation: flowAnim 1.4s linear infinite;
    pointer-events: none;
    will-change: stroke-dashoffset;
}
body.disable-flow .edge-flow-particles { display: none !important; }

.edge-label-bg {
    fill: var(--label-bg);
    stroke: var(--label-border);
    stroke-width: 1px;
    rx: 4px;
    cursor: pointer;
    pointer-events: all;
}
.edge-label-bg:hover { stroke: var(--accent); }
.edge-label-bg.aggregated { stroke: var(--accent); fill: var(--panel-bg); }
.edge-label-text {
    font-size: 11px;
    fill: var(--label-text);
    text-anchor: middle;
    dominant-baseline: central;
    font-family: monospace;
    pointer-events: none;
}

/* Ноды */
.node-element {
    position: absolute;
    background: var(--node-bg);
    border: 1px solid var(--node-border);
    border-radius: 6px;
    height: 28px;
    padding: 4px 8px;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    z-index: 30;
    cursor: move;
    box-shadow: 0 2px 6px rgba(0,0,0,0.06);
    white-space: nowrap;
}
.node-element:hover { border-color: var(--accent); }
.node-element.selected { border-color: #f59e0b; box-shadow: 0 0 0 2px #f59e0b; }
.node-element.connecting-source { border-color: #f59e0b; box-shadow: 0 0 0 2px #f59e0b; }
.node-element.malicious {
    border-color: #ef4444;
    background: rgba(239, 68, 68, 0.08);
}
.node-text { font-size: 11px; font-family: monospace; color: var(--text); font-weight: 500; }

/* Сайдбар */
#sidebar {
    position: absolute;
    top: 80px;
    right: 184px;
    width: 280px;
    background: var(--panel-bg);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 12px;
    z-index: 60;
    box-shadow: 0 8px 20px rgba(0,0,0,0.1);
    display: none;
}
.form-group { margin-bottom: 8px; }
.form-group label {
    display: block;
    font-size: 11px;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 2px;
    text-transform: uppercase;
}
.form-group input, .form-group select {
    width: 100%;
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text);
    padding: 5px 8px;
    border-radius: 6px;
    font-size: 12px;
    outline: none;
}
.form-group input[type="color"] {
    height: 32px;
    padding: 2px;
    cursor: pointer;
}
.form-group input:focus, .form-group select:focus { border-color: var(--accent); }

.sidebar-delete-btn {
    width: 100%;
    margin-top: 14px;
    padding: 7px;
    justify-content: center;
    font-size: 12px;
}

#modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.4);
    z-index: 100;
    align-items: center;
    justify-content: center;
}
#modal-overlay.active { display: flex; }
.modal-box {
    width: 520px;
    background: var(--panel-bg);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 16px;
    box-shadow: 0 20px 40px rgba(0,0,0,0.2);
}
.modal-box textarea {
    width: 100%;
    height: 160px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--accent);
    font-family: monospace;
    font-size: 12px;
    padding: 8px;
    margin: 10px 0;
    outline: none;
    resize: vertical;
}

#help-overlay {
    position: absolute;
    bottom: 12px;
    left: 154px;
    background: var(--panel-bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 4px 10px;
    font-size: 11px;
    color: var(--text-muted);
    z-index: 40;
    pointer-events: none;
}

/* ==========================================
       АДАПТИВНОСТЬ ДЛЯ МОБИЛЬНЫХ УСТРОЙСТВ
       ========================================== */
/* Дефолтный стиль зум-группы */
.zoom-group {
    display: flex;
    gap: 4px;
    width: 100%;
}

@media (max-width: 768px) {
    #help-overlay, #banner-top, #banner-right {
        display: none !important;
    }

    /* Вертикальный тулбар в виде узкой плашки слева */
    #toolbar {
        top: 8px;
        left: 8px;
        bottom: 8px;
        width: 46px;
        padding: 6px 4px;
        gap: 6px;
        align-items: center;
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
        z-index: 50;
    }

    /* Квадратные тач-кнопки по 36x36px */
    .btn {
        width: 36px;
        height: 36px;
        min-width: 36px;
        padding: 0;
        justify-content: center;
        font-size: 15px;
    }

    /* Скрываем текстовые лейблы */
    .btn-text {
        display: none !important;
    }

    /* Кнопки зума выстраиваем тоже вертикально одна под другой */
    .zoom-group {
        flex-direction: column;
        gap: 4px;
        width: 100%;
        align-items: center;
    }
    .zoom-group .btn {
        width: 36px;
        height: 30px;
        font-size: 11px;
    }

    /* Разделители на мобилке */
    .separator-h {
        width: 80%;
        margin: 2px 0;
    }

    /* Тултипы на мобилке скрываем от залипания */
    .has-tooltip:hover::after {
        display: none !important;
    }

    /* Зоны захвата для изменения размера групп пальцами */
    .resizer.r-e  { right: -10px; width: 20px; }
    .resizer.r-w  { left: -10px; width: 20px; }
    .resizer.r-s  { bottom: -10px; height: 20px; }
    .resizer.r-n  { top: -10px; height: 20px; }
    .resizer.corner { width: 20px; height: 20px; }

    /* Сайдбар параметров выезжает снизу (Bottom Sheet) */
    #sidebar {
        top: auto;
        left: 8px;
        right: 8px;
        bottom: 8px;
        width: auto;
        max-height: 55vh;
        overflow-y: auto;
        border-radius: 14px;
        box-shadow: 0 -4px 25px rgba(0, 0, 0, 0.25);
        padding: 14px;
        z-index: 90;
    }

    .form-group input, .form-group select {
        font-size: 14px; /* Защита от автозума Safari */
        padding: 7px 10px;
    }

    /* Модалка импорта CSV */
    .modal-box {
        width: calc(100vw - 24px) !important;
        max-height: 80vh;
        padding: 14px;
    }
}