body, html {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    background-color: #1a1a1a;
    overflow: hidden;
    font-family: 'Courier New', monospace;
    color: #eee;
}

canvas {
    display: block;
    width: 100%;
    height: 100%;
    image-rendering: pixelated; /* Сохраняет четкость при зуме */
}

#controls {
    position: absolute;
    top: 80px; /* Сдвинул ниже, чтобы не перекрывать верхние кнопки */
    bottom: auto;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 0, 0, 0.7);
    padding: 10px 20px;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    backdrop-filter: blur(4px);
    border: 1px solid #333;
    z-index: 50; /* Чтобы быть поверх Canvas */
    width: 90%; /* Stretch width */
    max-width: 1400px; /* But keep it reasonable on ultra-wide screens */
    box-sizing: border-box;
}

#coordinates {
    position: absolute;
    bottom: 20px;
    right: 20px;
    top: auto;
    background: rgba(0, 0, 0, 0.7);
    color: #eee;
    padding: 8px 12px;
    border-radius: 4px;
    border: 1px solid #333;
    font-size: 14px;
    pointer-events: none;
    z-index: 50;
    backdrop-filter: blur(4px);
    font-family: monospace;
}

.palette {
    display: flex;
    gap: 10px;
    flex-wrap: wrap; /* Для мобильных */
    justify-content: center;
    width: 100%;
}

.secondary-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 20px;
    width: 100%;
    flex-wrap: wrap;
    padding-top: 4px;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.tools-group {
    display: flex;
    gap: 10px;
}

.divider {
    width: 1px;
    height: 24px;
    background: rgba(255, 255, 255, 0.2);
}

.tool {
    background: #333;
    border: 1px solid #555;
    /* Убираем border-color при обычном состоянии, чтобы bottom border выделялся */
    border-bottom-width: 4px; 
    color: #ddd;
    padding: 6px 16px; /* Чуть меньше паддинг, чтобы компенсировать бордер */
    cursor: pointer;
    border-radius: 4px;
    font-family: inherit;
    font-size: 14px;
    transition: all 0.2s;
    position: relative;
    overflow: hidden;
}

.tool .hotkey {
    position: absolute;
    bottom: 2px;
    right: 4px;
    font-size: 10px;
    font-weight: bold;
    opacity: 0.6;
    text-transform: uppercase;
    pointer-events: none;
    color: inherit;
}

.tool:hover {
    background: #444;
    transform: translateY(-2px);
}

.tool.active {
    background: #555; /* Neutral background, relying on border color */
    color: white;
    transform: translateY(1px);
    box-shadow: inset 0 2px 4px rgba(0,0,0,0.3);
}

/* Material Colors */
.tool[data-type="sand"] { border-bottom-color: #edc9af; }
.tool[data-type="water"] { border-bottom-color: #4a90e2; }
.tool[data-type="algae"] { border-bottom-color: #2e7d32; }
.tool[data-type="vine"] { border-bottom-color: #8bc34a; }
.tool[data-type="gravel"] { border-bottom-color: #795548; }
.tool[data-type="source"] { border-bottom-color: #00bcd4; }
.tool[data-type="void"] { border-bottom-color: #673ab7; }
.tool[data-type="membrane"] { border-bottom-color: #e91e63; }
.tool[data-type="fire"] { border-bottom-color: #ff5722; }
.tool[data-type="steel"] { border-bottom-color: #607d8b; }
.tool[data-type="stone"] { border-bottom-color: #787878; }
.tool[data-type="empty"] { border-bottom-color: #cc3333; }
.tool[data-type="oil"] { border-bottom-color: #8d6e63; }
.tool[data-type="source_oil"] { border-bottom-color: #5d4037; }
.tool[data-type="campfire"] { border-bottom-color: #d84315; }
.tool[data-type="ichor"] { border-bottom-color: #00ff7f; }

/* Remove old specific active styles to unify look */
/* .tool[data-type="algae"].active ... removed */
/* .tool[data-type="vine"].active ... removed */

.settings {
    display: flex;
    gap: 10px;
    padding: 4px;
}

/* --- Settings Groups --- */
.settings-group {
    background: #2a2a2a;
    border: 1px solid #444;
    border-radius: 6px;
    padding: 12px;
    margin-bottom: 16px;
}

.settings-group h3 {
    margin: 0 0 10px 0;
    font-size: 14px;
    color: #888;
    text-transform: uppercase;
    letter-spacing: 1px;
    border-bottom: 1px solid #444;
    padding-bottom: 6px;
}

.sub-info {
    font-size: 11px;
    color: #666;
    margin-top: 2px;
    font-style: italic;
}

/* --- Settings Button --- */
#settings-btn {
    position: absolute;
    top: 20px;
    right: 20px;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.7);
    border: 1px solid #555;
    color: #eee;
    font-size: 20px;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
    transition: transform 0.2s, background 0.2s;
}

#top-controls {
    position: absolute;
    top: 20px;
    left: 20px;
    display: flex;
    gap: 8px;
    z-index: 100;
    align-items: center;
}

.speed-controls {
    display: flex;
    align-items: center;
    gap: 8px;
    background: rgba(0, 0, 0, 0.7);
    padding: 0 12px;
    height: 40px;
    border-radius: 20px;
    border: 1px solid #555;
    color: #eee;
}

.speed-icon {
    font-size: 16px;
}

#sim-speed {
    width: 80px;
    cursor: pointer;
}

.divider-vertical {
    width: 1px;
    height: 24px;
    background: rgba(255, 255, 255, 0.2);
    margin: 0 4px;
}

.icon-btn {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.7);
    border: 1px solid #555;
    color: #eee;
    font-size: 20px;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: transform 0.2s, background 0.2s;
}

.icon-btn:hover {
    background: #444;
    transform: scale(1.1);
}

.icon-btn:active {
    transform: scale(0.95);
}

#settings-btn:hover {
    background: #444;
    transform: rotate(45deg);
}

/* --- Modal --- */
.modal {
    display: none; /* Hidden by default */
    position: fixed; 
    z-index: 200; 
    left: 0;
    top: 0;
    width: 100%; 
    height: 100%; 
    overflow: auto; 
    background-color: rgba(0,0,0,0.5); 
    backdrop-filter: blur(2px);
}

.modal.show {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px; /* Отступ от краев экрана */
    box-sizing: border-box;
}

.modal-content {
    background-color: #222;
    border: 1px solid #555;
    width: 90%;
    max-width: 800px; /* Увеличили ширину для десктопа */
    max-height: 85vh; /* Ограничение высоты */
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.5);
    display: flex;
    flex-direction: column;
    color: #eee;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px;
    border-bottom: 1px solid #444;
    flex-shrink: 0; /* Заголовок не сжимается */
}

.modal-header h2 {
    margin: 0;
    font-size: 18px;
}

.modal-body {
    padding: 20px;
    overflow-y: auto; /* Скролл только внутри тела */
}

/* Desktop Layout for Settings */
@media (min-width: 768px) {
    .modal-body {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 16px;
        align-items: start;
    }
    
    .settings-group {
        margin-bottom: 0;
        height: 100%; /* Одинаковая высота карточек */
        box-sizing: border-box;
    }
}

.close-btn {
    color: #aaa;
    background: none;
    border: none;
    font-size: 28px;
    font-weight: bold;
    cursor: pointer;
}

.close-btn:hover,
.close-btn:focus {
    color: #fff;
    text-decoration: none;
    cursor: pointer;
}

.modal-body {
    /* Удаляем старый паддинг, он теперь в классе выше */
}

.setting-item {
    margin-bottom: 12px;
}

.setting-item label {
    display: block;
    margin-bottom: 5px;
    font-size: 14px;
    color: #ccc;
}

.setting-item input[type="number"] {
    width: 100%;
    padding: 8px;
    background: #333;
    border: 1px solid #555;
    border-radius: 4px;
    color: #eee;
    font-family: inherit;
    box-sizing: border-box;
}

.setting-item input[type="number"]:focus {
    outline: none;
    border-color: #4a90e2;
}

.checkbox-label {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 14px;
    cursor: pointer;
    user-select: none;
}

.checkbox-label input {
    cursor: pointer;
}


.tools-panel {
    display: flex;
    gap: 10px;
    padding: 6px;
    background: #2a2a2a;
    border-radius: 6px;
    border: 1px solid #444;
}

.tool-action {
    background: #333;
    border: 1px solid #555;
    border-bottom-width: 4px;
    color: #ddd;
    padding: 6px 12px;
    cursor: pointer;
    border-radius: 4px;
    font-family: inherit;
    font-size: 14px;
    transition: all 0.2s;
    position: relative;
    overflow: hidden;
}

.tool-action:hover {
    background: #444;
    transform: translateY(-2px);
}

.tool-action.active {
    background: #555;
    color: white;
    transform: translateY(1px);
    box-shadow: inset 0 2px 4px rgba(0,0,0,0.3);
    border-bottom-color: #eee;
}

.tool-action[data-tool="eraser"] { border-bottom-color: #cc3333; }
.tool-action[data-tool="ruler"] { border-bottom-color: #ffeb3b; }

.tool-action .hotkey {
    position: absolute;
    bottom: 2px;
    right: 4px;
    font-size: 10px;
    font-weight: bold;
    opacity: 0.6;
    pointer-events: none;
    color: inherit;
}

.brush-settings {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
    color: #ccc;
    margin-top: 0;
}

.brush-settings input[type=range] {
    width: 80px;
    cursor: pointer;
}

.info {
    font-size: 12px;
    color: #888;
}

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

.action-btn {
    flex: 1;
    background: #333;
    border: 1px solid #555;
    color: #ddd;
    padding: 8px 12px;
    cursor: pointer;
    border-radius: 4px;
    font-family: inherit;
    font-size: 13px;
    text-align: center;
    transition: all 0.2s;
    user-select: none;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 36px;
    box-sizing: border-box;
    text-decoration: none; /* For label */
}

.action-btn:hover {
    background: #444;
}

.action-btn.danger {
    background: #7f2e2e;
    border-color: #5c1b1b;
    color: #ffcccc;
}

.action-btn.danger:hover {
    background: #993333;
}

/* --- New Refactored Settings CSS --- */

details {
    background: #252525;
    border: 1px solid #333;
    border-radius: 4px;
    margin-bottom: 8px;
    overflow: hidden;
}

details summary {
    padding: 8px 12px;
    cursor: pointer;
    background: #2a2a2a;
    font-size: 13px;
    font-weight: bold;
    color: #ddd;
    user-select: none;
    list-style: none; /* Hide default triangle */
    display: flex;
    align-items: center;
    justify-content: space-between;
}

details summary::after {
    content: '+';
    font-weight: bold;
    color: #888;
}

details[open] summary::after {
    content: '-';
}

details summary:hover {
    background: #333;
}

.indent-group {
    padding: 10px;
    border-top: 1px solid #333;
    background: #1f1f1f;
}

.setting-header-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;
    background: #252525;
    padding: 8px;
    border-radius: 4px;
}

.setting-header-row h4 {
    margin: 0;
    font-size: 14px;
    font-weight: normal;
    color: #eee;
}

.conditional-settings {
    border-left: 2px solid #444;
    padding-left: 12px;
    margin-left: 4px;
    margin-bottom: 12px;
    display: none; /* Hidden by default if unchecked, controlled by JS */
}

.conditional-settings.active {
    display: block;
    animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(-5px); }
    to { opacity: 1; transform: translateY(0); }
}

/* Switch Toggle */
.switch {
    position: relative;
    display: inline-block;
    width: 36px;
    height: 20px;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #444;
    transition: .4s;
    border-radius: 20px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 14px;
    width: 14px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    transition: .4s;
    border-radius: 50%;
}

input:checked + .slider {
    background-color: #4a90e2;
}

input:focus + .slider {
    box-shadow: 0 0 1px #4a90e2;
}

input:checked + .slider:before {
    transform: translateX(16px);
}

/* Pipette Tooltip */
#pipette-tooltip {
    position: fixed;
    background: rgba(0, 0, 0, 0.8);
    color: white;
    padding: 5px 10px;
    border-radius: 4px;
    font-size: 14px;
    pointer-events: none;
    z-index: 1000;
    display: none;
    border: 1px solid #555;
    white-space: nowrap;
}
