body {
    margin: 0;
    overflow: hidden; /* Hide scrollbars */
    /* Background properties are now controlled by script.js */
}

canvas {
    display: block;
}

#library-panel {
    position: absolute;
    top: 20px;
    left: 20px;
    background: rgba(40, 40, 40, 0.9);
    color: white;
    padding: 0;
    border-radius: 10px;
    font-family: sans-serif;
    width: 200px;
    box-shadow: 0 4px 8px rgba(0,0,0,0.3);
    overflow: hidden;
    z-index: 99;
}

#library-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 5px 15px;
    background: rgba(50, 50, 50, 0.9);
    cursor: pointer;
}

#library-panel h3 {
    margin: 0;
    font-size: 16px;
}

#toggle-library-panel-btn {
    background: none;
    border: 1px solid #777;
    color: white;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    cursor: pointer;
    line-height: 18px;
    padding: 0;
    font-size: 18px;
}

#library-body {
    padding: 15px;
    transition: all 0.3s ease;
    max-height: calc(100vh - 120px);
    overflow-y: auto;
}

#library-panel.minimized #library-body {
    padding: 0 15px;
    max-height: 0;
    opacity: 0;
    overflow: hidden;
}

#settings-panel {
    position: absolute;
    top: 20px;
    right: 20px;
    background: rgba(40, 40, 40, 0.9);
    color: white;
    padding: 0;
    border-radius: 10px;
    font-family: sans-serif;
    width: 200px;
    box-shadow: 0 4px 8px rgba(0,0,0,0.3);
    overflow: hidden;
}

#settings-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 5px 15px;
    background: rgba(50, 50, 50, 0.9);
    cursor: pointer;
}

#settings-panel h3 {
    margin: 0;
    font-size: 16px;
}

#toggle-settings-btn {
    background: none;
    border: 1px solid #777;
    color: white;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    cursor: pointer;
    line-height: 18px;
    padding: 0;
    font-size: 18px;
}

#settings-body {
    padding: 15px;
    transition: all 0.3s ease;
    max-height: calc(100vh - 120px);
    overflow-y: auto;
}

#settings-body h4 {
    margin: 15px 0 10px 0;
    text-align: center;
    font-size: 14px;
    font-weight: normal;
    color: #ccc;
    border-bottom: 1px solid #555;
    padding-bottom: 5px;
}

#settings-panel.minimized #settings-body {
    padding: 0 15px;
    max-height: 0;
    opacity: 0;
    overflow: hidden;
}

#color-settings-container {
    margin-bottom: 10px;
}

.setting {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
}

.setting-group {
    margin-bottom: 10px;
}

.setting-group label {
    display: block;
    font-size: 14px;
    margin-bottom: 5px;
}

.setting label {
    font-size: 14px;
}

.setting input[type="color"] {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    width: 40px;
    height: 25px;
    background-color: transparent;
    border: 1px solid #555;
    cursor: pointer;
    border-radius: 4px;
}

.setting input[type="color"]::-webkit-color-swatch {
    border-radius: 3px;
    border: none;
}

.setting input[type="color"]::-moz-color-swatch {
    border-radius: 3px;
    border: none;
}

input[type="range"] {
    width: 100%;
}

select {
    width: 100%;
    padding: 5px;
    background: #555;
    color: white;
    border: 1px solid #777;
    border-radius: 4px;
    font-family: sans-serif;
}

.control-btn {
    width: 100%;
    /* margin-top: 10px; */ /* Removed to be handled by setting-group */
    background-color: #555;
    color: white;
    border: none;
    padding: 8px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
}

.control-btn:hover {
    background-color: #777;
}

hr {
    border: none;
    border-top: 1px solid #555;
    margin: 15px 0;
}

.setting-controls {
    display: flex;
    justify-content: space-between;
    border-top: 1px solid #555;
    padding-top: 10px;
}

.setting-controls button {
    background-color: #555;
    color: white;
    border: none;
    padding: 5px 10px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
    width: 48%;
}

.setting-controls button:hover {
    background-color: #777;
}

#open-library-btn {
    background-color: #4a69bd;
}

#open-library-btn:hover {
    background-color: #5c7edc;
}

#open-wiki-btn {
    background: linear-gradient(145deg, #ffd700, #f0c400);
    color: #333;
    font-weight: bold;
    border: 1px solid #daa520;
    text-shadow: 0 1px 0 rgba(255,255,255,0.3);
}

#open-wiki-btn:hover {
    background: linear-gradient(145deg, #ffe033, #f0c400);
    box-shadow: 0 0 8px rgba(255, 215, 0, 0.5);
}

#open-shapes-library-btn {
    width: 100%;
    /* margin-top: 10px; */ /* Removed */
    background-color: #57a773;
    color: white;
    border: none;
    padding: 8px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
}

#open-shapes-library-btn:hover {
    background-color: #63bf84;
}

/* Toggle Switch styles */
.switch {
    position: relative;
    display: inline-block;
    width: 40px;
    height: 22px;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #555;
    transition: .4s;
    border-radius: 22px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 16px;
    width: 16px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    transition: .4s;
    border-radius: 50%;
}

input:checked + .slider {
    background-color: #4a69bd;
}

input:checked + .slider:before {
    transform: translateX(18px);
}

/* Templates Library styles */
#templates-library {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
}

#templates-library.hidden {
    display: none;
}

#shapes-library {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 101; /* Above templates library if both open */
}

#shapes-library.hidden {
    display: none;
}

#templates-panel, #shapes-panel {
    background: #282828;
    color: white;
    padding: 20px;
    border-radius: 10px;
    width: 90%;
    font-family: sans-serif;
    box-shadow: 0 5px 15px rgba(0,0,0,0.5);
    max-height: 80vh;
    display: flex;
    flex-direction: column;
}

#templates-panel {
    max-width: 900px; /* Increased width */
}

#shapes-panel {
    max-width: 600px;
}

.search-container {
    display: flex;
    margin-bottom: 15px;
    position: relative;
}

#template-search {
    width: 100%;
    padding: 8px 30px 8px 10px;
    border-radius: 5px;
    border: 1px solid #555;
    background: #333;
    color: white;
    font-size: 14px;
}

#template-search:focus {
    outline: none;
    border-color: #4a69bd;
    box-shadow: 0 0 0 2px rgba(74, 105, 189, 0.3);
}

#clear-search-btn {
    position: absolute;
    right: 5px;
    top: 50%;
    transform: translateY(-50%);
    background: none;
    border: none;
    color: #aaa;
    font-size: 18px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    border-radius: 50%;
}

#clear-search-btn:hover {
    color: white;
    background: rgba(255, 255, 255, 0.1);
}

.templates-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #555;
    padding-bottom: 10px;
    margin-bottom: 15px;
}

.templates-header h3 {
    margin: 0;
}

#close-library-btn, #close-shapes-library-btn {
    background: none;
    border: none;
    color: white;
    font-size: 24px;
    cursor: pointer;
}

#templates-container {
    overflow-y: auto;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 15px;
    padding-right: 10px; /* Space for scrollbar */
}

#shapes-container {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    gap: 15px;
}

.template-item {
    background: #333;
    border-radius: 5px;
    padding: 10px;
    /* margin-bottom is now handled by grid gap */
}

.shape-item {
    background: #333;
    border-radius: 5px;
    padding: 15px;
    text-align: center;
    cursor: pointer;
    transition: background-color 0.2s;
}

.template-item h4,
.shape-item h4 {
    margin: 0 0 10px 0;
    font-size: 14px;
}

.color-swatches {
    display: flex;
    gap: 5px;
    margin-bottom: 10px;
    flex-wrap: wrap;
}

.swatch {
    width: 20px;
    height: 20px;
    border-radius: 3px;
}

.apply-template-btn {
    background-color: #4a69bd;
    color: white;
    border: none;
    padding: 5px 10px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
    float: right;
}

.apply-template-btn:hover {
    background-color: #5c7edc;
}

.delete-template-btn {
    background: none;
    border: none;
    color: #aaa;
    font-size: 20px;
    cursor: pointer;
    padding: 0 5px;
    line-height: 1;
}
.delete-template-btn:hover {
    color: #fff;
}

.shape-item:hover {
    background-color: #4a69bd;
}

.shape-item svg {
    width: 50px;
    height: 50px;
    fill: #fff;
    margin-bottom: 10px;
}

#bottom-toolbar {
    position: absolute;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(40, 40, 40, 0.9);
    padding: 8px;
    border-radius: 10px;
    display: flex;
    gap: 8px;
    box-shadow: 0 4px 8px rgba(0,0,0,0.3);
}

#bottom-toolbar.hidden {
    display: none;
}

.toolbar-btn {
    background-color: #555;
    color: white;
    border: 1px solid #777;
    padding: 8px 20px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
    transition: background-color 0.2s;
}

.toolbar-btn:hover {
    background-color: #666;
}

.toolbar-btn.active {
    background-color: #4a69bd;
    border-color: #5c7edc;
}

#delete-mode-btn.active {
    background-color: #e74c3c;
    border-color: #c0392b;
}

#explode-mode-btn.active {
    background-color: #f39c12;
    border-color: #e67e22;
}

#attract-mode-btn.active {
    background-color: #9b59b6;
    border-color: #8e44ad;
}

#weld-mode-btn.active {
    background-color: #27ae60;
    border-color: #2ecc71;
}

.selected-for-weld {
    box-shadow: 0 0 20px #27ae60 !important;
}

/* Statistics styling */
.stats-container {
    display: flex;
    flex-direction: column;
    gap: 5px;
    max-height: 200px;
    overflow-y: auto;
    margin-bottom: 10px;
    padding-right: 5px;
}

.stat-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 14px;
}

.stat-value {
    font-weight: bold;
    color: #4a69bd;
}

/* Media Queries for Mobile Friendliness */
@media (max-width: 768px) {
    #settings-panel, #library-panel {
        width: 250px; /* A bit wider for mobile */
    }

    #bottom-toolbar {
        padding: 5px;
        gap: 5px;
    }

    .toolbar-btn {
        padding: 8px 12px;
        font-size: 12px;
    }

    #templates-panel, #shapes-panel {
        width: 95%;
        max-height: 90vh;
    }

    #templates-container {
        grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    }

    #shapes-container {
        grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
    }
}

@media (max-width: 480px) {
    #settings-panel, #library-panel {
        top: 10px;
        max-height: 85vh;
    }
    #settings-panel {
        right: 10px;
        width: calc(100vw - 20px);
    }
    #library-panel {
        left: 10px;
        width: calc(100vw - 20px);
    }

    #bottom-toolbar {
        bottom: 10px;
    }
}

#play-time-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 5px 0;
}

#play-time-display {
    font-weight: bold;
    color: #4a69bd;
}

.template-category-header {
    grid-column: 1 / -1;
    text-align: center;
    border-bottom: 1px solid #555;
    padding-bottom: 10px;
    margin: 20px 0 10px 0;
    color: #fff;
    background: linear-gradient(90deg, rgba(70, 70, 70, 0), rgba(70, 70, 70, 0.5), rgba(70, 70, 70, 0));
    text-shadow: 0 0 8px rgba(120, 120, 255, 0.6);
    font-size: 16px;
    letter-spacing: 1px;
    position: relative;
}

.template-category-header:first-child {
    margin-top: 0;
}

#templates-panel {
    max-width: 900px; /* Increased width */
    max-height: 85vh; /* Increased height to show more templates */
}

#templates-container {
    max-height: calc(85vh - 80px); /* Adjust to fit within the panel */
    overflow-y: auto;
}

/* Inspection Panel styles */
#inspection-panel {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    background: rgba(40, 40, 40, 0.95);
    color: white;
    padding: 0;
    border-radius: 10px;
    font-family: sans-serif;
    width: 280px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.5);
    z-index: 100;
}

#inspection-panel.hidden {
    display: none;
}

.panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 15px;
    background: rgba(50, 50, 50, 0.9);
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
}

.panel-header h3 {
    margin: 0;
    font-size: 16px;
}

#close-inspection-btn {
    background: none;
    border: none;
    color: white;
    font-size: 24px;
    cursor: pointer;
}

.panel-body {
    padding: 15px;
    max-height: 400px;
    overflow-y: auto;
}

.info-row {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
    border-bottom: 1px solid #555;
    padding-bottom: 5px;
}

.info-row:last-child {
    border-bottom: none;
}

.info-label {
    font-weight: bold;
    color: #aaa;
}

.info-value {
    text-align: right;
    word-break: break-all;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 5px;
}

.color-preview {
    display: inline-block;
    width: 15px;
    height: 15px;
    border-radius: 3px;
    margin-left: 5px;
    vertical-align: middle;
    border: 1px solid #777;
}

#inspect-mode-btn.active {
    background-color: #9b59b6;
    border-color: #8e44ad;
}

@media (max-width: 480px) {
    #inspection-panel {
        width: 90%;
        max-width: 300px;
    }
}
