/* Custom styles for grid and canvas */
body {
    font-family: 'Inter', sans-serif;
    margin: 0;
    overflow: hidden; /* Zapobiega przewijaniu całego body */
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background-color: #f0f2f5;
}
.container {
    display: flex;
    height: 100vh;
    width: 100vw;
    position: relative;
    background-color: transparent;
}
.canvas-area {
    flex-grow: 1; /* Płótno zajmuje całą dostępną przestrzeń */
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #ffffff; /* Tło płótna */
    overflow: hidden;
    position: relative; /* Potrzebne dla gridu i ikon */
    transition: margin-left 0.3s ease-out; /* Płynne przesuwanie przy otwieraniu/zamykaniu bocznego panelu */
}
#drawingCanvas {
    border: 1px solid #e0e0e0;
    background-color: #ffffff;
    cursor: crosshair;
    touch-action: none; /* Zapobiega domyślnemu przewijaniu na dotyku */
    width: 100%; /* Upewnij się, że canvas wypełnia swój kontener */
    height: 100%;
}
.canvas-grid {
    background-image:
        linear-gradient(to right, #f0f0f0 1px, transparent 1px),
        linear-gradient(to bottom, #f0f0f0 1px, transparent 1px);
    background-size: 20px 20px; /* Rozmiar komórek siatki */
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none; /* Pozwól na interakcję z canvas pod spodem */
}

/* --- Panel boczny narzędziowy --- */
.sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    background-color: #2d3748; /* Ciemny szary */
    color: #e2e8f0;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
    z-index: 10;
    transition: width 0.3s ease-out;
    overflow-y: auto;
    overflow-x: hidden; /* Ukryj poziome przewijanie */
    padding: 1rem 0; /* Padding tylko góra/dół */
}

/* Wąski stan paska bocznego (ikony) */
.sidebar.collapsed {
    width: 60px; /* Wąska szerokość */
}
.sidebar.collapsed .tool-section-content {
    display: none; /* Ukryj zawartość sekcji */
}
.sidebar.collapsed .tool-section-header {
    justify-content: center; /* Wyśrodkuj ikonę */
    padding: 0.75rem 0;
}
.sidebar.collapsed .tool-section-header span {
    display: none; /* Ukryj tekst nagłówka */
}
.sidebar.collapsed .tool-section-header i {
    margin-right: 0; /* Usuń margines z ikony */
    font-size: 1.5rem; /* Zwiększ rozmiar ikon */
}
.sidebar.collapsed #backToMenuBtn {
    display: none; /* Ukryj przycisk powrotu w trybie zwiniętym */
}

/* Rozwinięty stan paska bocznego */
.sidebar.expanded {
    width: 300px; /* Szeroka szerokość */
}
.sidebar.expanded .tool-section-content {
    display: block; /* Pokaż zawartość sekcji */
}
.sidebar.expanded .tool-section-header {
    justify-content: flex-start; /* Nagłówek wyrównany do lewej */
    padding: 0.75rem 1rem;
}
.sidebar.expanded .tool-section-header span {
    display: inline-block; /* Pokaż tekst nagłówka */
}
.sidebar.expanded .tool-section-header i {
    margin-right: 0.5rem; /* Dodaj margines do ikony */
    font-size: 1.125rem; /* Standardowy rozmiar ikon */
}
.sidebar.expanded #backToMenuBtn {
    display: flex; /* Pokaż przycisk powrotu w trybie rozwiniętym */
}

/* Sekcje narzędzi w sidebarze */
.tool-section {
    margin-bottom: 0.5rem;
    border-bottom: 1px solid #4a5568;
    padding-bottom: 0.5rem;
}
.tool-section:last-child {
    border-bottom: none;
    margin-bottom: 0;
    padding-bottom: 0;
}
.tool-section-header {
    display: flex;
    align-items: center;
    padding: 0.75rem 1rem;
    cursor: pointer;
    background-color: #3b4252;
    transition: background-color 0.2s;
    font-weight: bold;
    color: #cbd5e0;
    border-radius: 0.4rem;
    margin: 0 0.5rem; /* Marginesy boczne dla nagłówków sekcji */
}
.tool-section-header:hover {
    background-color: #4a5568;
}
.tool-section-header i {
    margin-right: 0.5rem;
}
.tool-section-content {
    padding: 0.5rem 1rem;
}
.tool-section-content h3 {
    font-size: 1rem;
    font-weight: bold;
    margin-bottom: 0.5rem;
    color: #cbd5e0;
}
.tool-button {
    background-color: #4a5568;
    color: white;
    padding: 0.6rem 0.8rem;
    border-radius: 0.4rem;
    cursor: pointer;
    transition: background-color 0.2s;
    width: 100%;
    text-align: left;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.9rem;
    margin-bottom: 0.4rem;
}
.tool-button:last-child {
    margin-bottom: 0;
}
.tool-button:hover {
    background-color: #6a768d;
}
.tool-button.active {
    background-color: #3b82f6;
}

/* Właściwości linii, ikon, ESP, skryptów */
.line-properties label, .esp-config label, .esp-control-section label, .icon-properties label, .script-management label {
    display: block;
    margin-top: 0.6rem;
    margin-bottom: 0.2rem;
    color: #a0aec0;
    font-size: 0.8rem;
}
.line-properties input[type="color"],
.line-properties input[type="number"],
.esp-config input[type="text"],
.esp-config input[type="number"],
.esp-config textarea,
.icon-properties select,
.script-management select,
.script-management textarea,
.icon-properties input[type="text"] {
    width: 100%;
    padding: 0.4rem;
    border-radius: 0.2rem;
    border: 1px solid #4a5568;
    background-color: #2d3748;
    color: #e2e8f0;
    font-size: 0.8rem;
}
.line-properties input[type="color"] {
    height: 35px;
    padding: 0;
}
.line-type-options {
    display: flex;
    gap: 0.8rem;
    margin-top: 0.4rem;
    font-size: 0.8rem;
}
.line-type-options input {
    margin-right: 0.2rem;
}

/* Ikony urządzeń do przeciągania */
.device-icons-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.8rem;
    margin-top: 0.8rem;
}
.device-icon {
    background-color: #4a5568;
    padding: 0.6rem;
    border-radius: 0.4rem;
    text-align: center;
    cursor: grab;
    transition: background-color 0.2s;
    font-size: 1.8rem;
    color: #e2e8f0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}
.device-icon:hover {
    background-color: #6a768d;
}
.device-icon.dragging {
    opacity: 0.7;
    cursor: grabbing;
}
.device-icon svg {
    width: 35px;
    height: 35px;
    stroke: currentColor;
    stroke-width: 2;
    fill: none;
}

/* Osadzone ikony na canvas */
.placed-icon {
    position: absolute;
    cursor: grab;
    user-select: none;
    touch-action: none;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 5px;
    transition: color 0.2s ease, transform 0.2s ease;
    width: 50px;
    height: 50px;
}
.placed-icon svg {
    width: 100%;
    height: 100%;
    stroke-width: 2;
    fill: none;
}
.placed-icon svg {
    stroke: #3b82f6;
}
.placed-icon.active svg {
    stroke: #ef4444;
}
.placed-icon.connected svg {
    stroke: #10b981;
}
.placed-icon.connection-error svg {
    stroke: #f59e0b;
}
.placed-icon.active.connected svg, .placed-icon.active.connection-error svg {
    stroke: #ef4444;
}
.placed-icon.connection-error.connected svg {
    stroke: #f59e0b;
}

.placed-icon.selected {
    outline: 2px dashed #f59e0b;
    outline-offset: 3px;
    border-radius: 0.5rem;
}
.placed-icon .icon-text {
    position: absolute;
    font-size: 0.75rem;
    color: #4a5568;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100px;
    pointer-events: none;
    background-color: rgba(255, 255, 255, 0.8);
    padding: 2px 5px;
    border-radius: 3px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    transform: translate(-50%, -50%);
}
.placed-icon.selected .icon-text {
    color: #f59e0b;
}
.placed-icon .icon-name {
    font-weight: bold;
    display: block;
}
.placed-icon .icon-desc {
    font-size: 0.65rem;
    display: block;
}

.placed-icon.direction-up { transform: translate(-50%, -50%) rotate(0deg); }
.placed-icon.direction-right { transform: translate(-50%, -50%) rotate(90deg); }
.placed-icon.direction-down { transform: translate(-50%, -50%) rotate(180deg); }
.placed-icon.direction-left { transform: translate(-50%, -50%) rotate(270deg); }

.placed-icon.text-pos-top .icon-text { top: -20px; left: 50%; }
.placed-icon.text-pos-bottom .icon-text { top: calc(100% + 10px); left: 50%; }
.placed-icon.text-pos-left .icon-text { top: 50%; left: -10px; transform: translate(calc(-100% - 5px), -50%); }
.placed-icon.text-pos-right .icon-text { top: 50%; left: calc(100% + 10px); transform: translate(5px, -50%); }

/* Edytor konfiguracji ESP */
.esp-list {
    max-height: 150px;
    overflow-y: auto;
    border: 1px solid #4a5568;
    border-radius: 0.25rem;
    margin-top: 0.8rem;
    background-color: #1a202c;
}
.esp-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.4rem 0.6rem;
    border-bottom: 1px solid #4a5568;
    cursor: pointer;
    transition: background-color 0.2s ease;
    font-size: 0.8rem;
}
.esp-item:last-child {
    border-bottom: none;
}
.esp-item:hover {
    background-color: #4a5568;
}
.esp-item.selected {
    background-color: #3b82f6;
    color: white;
}
.esp-item-name {
    flex-grow: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.esp-item-actions button {
    background-color: transparent;
    border: none;
    color: #a0aec0;
    cursor: pointer;
    font-size: 0.8rem;
    margin-left: 0.4rem;
    transition: color 0.2s ease;
    padding: 0.2rem;
    border-radius: 0.2rem;
}
.esp-item-actions button:hover {
    color: white;
    background-color: #555;
}
.esp-item-actions button.delete-btn {
    color: #ef4444;
}
.esp-item-actions button.delete-btn:hover {
    color: #f87171;
    background-color: #555;
}
.esp-add-btn {
    background-color: #10b981;
    margin-top: 0.8rem;
}
.esp-add-btn:hover {
    background-color: #059669;
}

/* Edytor tekstowy nazwy modułu */
.esp-name-editor {
    margin-top: 0.8rem;
}
.esp-name-editor input, .esp-name-editor textarea {
    width: 100%;
    padding: 0.4rem;
    border-radius: 0.2rem;
    border: 1px solid #4a5568;
    background-color: #2d3748;
    color: #e2e8f0;
    font-size: 0.8rem;
}
.esp-name-editor input:disabled, .esp-name-editor textarea:disabled {
    background-color: #1a202c;
    color: #a0aec0;
    cursor: not-allowed;
}
.esp-name-editor textarea {
    min-height: 50px;
    resize: vertical;
    margin-top: 0.4rem;
}

/* Komunikacja ESP */
.esp-control-section button {
    background-color: #3b82f6;
    margin-top: 0.6rem;
}
.esp-control-section button:hover {
    background-color: #2563eb;
}
.esp-response-panel {
    background-color: #1a202c;
    padding: 0.6rem;
    border-radius: 0.2rem;
    margin-top: 0.8rem;
    font-size: 0.7rem;
    color: #a0aec0;
    min-height: 40px;
    max-height: 120px;
    overflow-y: auto;
    white-space: pre-wrap;
    word-break: break-all;
    border: 1px solid #4a5568;
}
.esp-response-panel.error {
    color: #ef4444;
}
.esp-response-panel.success {
    color: #10b981;
}

/* Script Management Section */
.script-management .tab-buttons {
    display: flex;
    margin-bottom: 0.8rem;
    border-bottom: 1px solid #4a5568;
}
.script-management .tab-button {
    flex: 1;
    padding: 0.6rem 0.4rem;
    background-color: #4a5568;
    color: #e2e8f0;
    border: none;
    border-radius: 0.2rem 0.2rem 0 0;
    cursor: pointer;
    transition: background-color 0.2s;
    font-size: 0.8rem;
    text-align: center;
}
.script-management .tab-button:hover {
    background-color: #6a768d;
}
.script-management .tab-button.active {
    background-color: #2d3748;
    border-bottom: 2px solid #3b82f6;
    color: white;
}
.script-management .tab-content textarea {
    min-height: 120px;
    resize: vertical;
    font-family: 'Cascadia Code', 'Fira Code', monospace;
    font-size: 0.75rem;
}
.script-management .action-buttons {
    display: flex;
    gap: 0.4rem;
    margin-top: 0.6rem;
}
.script-management .action-buttons button {
    flex: 1;
    background-color: #10b981;
    color: white;
    padding: 0.4rem;
    border-radius: 0.2rem;
    cursor: pointer;
    transition: background-color 0.2s;
    font-size: 0.8rem;
}
.script-management .action-buttons button:hover {
    background-color: #059669;
}
.script-management .action-buttons button.clear-btn {
    background-color: #ef4444;
}
.script-management .action-buttons button.clear-btn:hover {
    background-color: #dc2626;
}

/* Hamburger menu dla mobilnych */
.hamburger-menu-icon {
    position: fixed;
    top: 1rem;
    right: 1rem;
    z-index: 1000; /* Najwyższy z-index */
    background-color: #3b82f6;
    color: white;
    padding: 0.75rem 1rem;
    border-radius: 0.5rem;
    cursor: pointer;
    display: none; /* Domyślnie ukryty na desktopie */
    box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}

.mobile-menu-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background-color: rgba(0, 0, 0, 0.7); /* Półprzezroczyste tło */
    z-index: 999;
    display: flex;
    justify-content: center;
    align-items: center;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease-out, visibility 0.3s ease-out;
}
.mobile-menu-overlay.open {
    opacity: 1;
    visibility: visible;
}
.mobile-menu-content {
    background-color: #2d3748;
    color: #e2e8f0;
    padding: 1.5rem;
    border-radius: 0.5rem;
    width: 90%;
    max-width: 500px;
    max-height: 90vh;
    overflow-y: auto;
    position: relative;
}
.mobile-menu-close-btn {
    position: absolute;
    top: 0.75rem;
    right: 0.75rem;
    background: none;
    border: none;
    color: white;
    font-size: 1.5rem;
    cursor: pointer;
}

/* Media queries dla responsywności */
@media (min-width: 769px) {
    /* Desktop styles */
    .sidebar {
        width: 60px; /* Wąski pasek boczny */
    }
    .sidebar:hover {
        width: 300px; /* Rozwinięty pasek boczny na hover */
    }
    .canvas-area {
        margin-left: 60px; /* Przesuń płótno na prawo od wąskiego paska */
    }
    .sidebar:hover + .canvas-area {
        margin-left: 300px; /* Przesuń płótno na prawo od rozwiniętego paska */
    }
    .hamburger-menu-icon {
        display: none;
    }
    .mobile-menu-overlay {
        display: none;
    }
}

@media (max-width: 768px) {
    /* Mobile styles */
    .sidebar {
        display: none; /* Ukryj pasek boczny desktopowy na mobilnych */
    }
    .canvas-area {
        margin-left: 0; /* Brak marginesu na mobilnych */
    }
    .hamburger-menu-icon {
        display: block; /* Pokaż hamburger na mobilnych */
    }
    .mobile-menu-overlay.open {
        display: flex;
    }
}
