/* Global reset and box-sizing for consistent layout */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

/* Base styling for body with dark theme and full viewport height */
body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: #121212;
    color: #e0e0e0;
    height: 100vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

/* Simulation area with centered canvas and dark background */
.simulation-area {
    position: relative;
    flex: 1;
    background: #1a1a1a;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 0;
}

#physicsCanvas {
    width: 100%;
    height: 100%;
    cursor: none;
}

/* Title overlay for subtle branding or context */
.title-overlay {
    position: absolute;
    top: 20px;
    right: 20px;
    color: rgba(255, 255, 255, 0.5);
    font-size: 16px;
    font-weight: 300;
    letter-spacing: 1px;
    pointer-events: none;
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.8);
}

/* Cursor info display with translucent backdrop */
.cursor-info {
    position: absolute;
    top: 20px;
    left: 20px;
    background: rgba(0, 0, 0, 0.7);
    color: #e0e0e0;
    padding: 5px 12px;
    border-radius: 4px;
    font-size: 12px;
    backdrop-filter: blur(5px);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

/* Controls area with flexible layout and dark theme */
.controls-area {
    background: #252525;
    padding: 8px 12px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-top: 1px solid #333;
    min-height: 60px;
    flex-shrink: 0;
    flex-wrap: wrap;
}

/* Tool buttons container with consistent spacing */
.tool-buttons {
    display: flex;
    gap: 6px;
    flex-wrap: nowrap;
    margin-right: 10px;
}

/* Individual tool buttons with hover and active states */
.tool-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    background: #333;
    border: 1px solid #444;
    border-radius: 6px;
    cursor: pointer;
    color: #e0e0e0;
    transition: all 0.2s ease;
    flex-shrink: 0;
}

.tool-btn:hover {
    background: #3a3a3a;
    border-color: #4ecdc4;
}

.tool-btn.active {
    background: #4ecdc4;
    color: #121212;
    border-color: #4ecdc4;
}

.tool-btn .key {
    font-size: 9px;
    font-weight: bold;
    margin-bottom: 2px;
}

.tool-btn .icon {
    font-size: 14px;
    line-height: 1;
}

/* Action buttons container with consistent spacing */
.action-buttons {
    display: flex;
    gap: 8px;
    flex-wrap: nowrap;
    margin-left: 10px;
}

.action-buttons button {
    padding: 8px 15px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
    transition: all 0.2s ease;
    font-weight: 500;
    background: #333;
    color: #e0e0e0;
    border: 1px solid #444;
    white-space: nowrap;
    flex-shrink: 0;
}

.action-buttons button:hover {
    background: #3a3a3a;
}

#clearBtn:hover {
    background: #e74c3c;
    border-color: #e74c3c;
}

#demoBtn:hover {
    background: #27ae60;
    border-color: #27ae60;
}

#advancedBtn {
    font-size: 18px;
    line-height: 1;
    padding: 8px 12px;
}

/* Quick guide for keyboard shortcuts */
.quick-guide {
    display: flex;
    align-items: center;
    gap: 15px;
    font-size: 12px;
    color: #aaa;
    flex-wrap: nowrap;
    margin-left: auto;
    padding-left: 15px;
    border-left: 1px solid #333;
}

.quick-guide .key {
    background: rgba(255, 255, 255, 0.1);
    padding: 2px 6px;
    border-radius: 4px;
    font-weight: bold;
    margin-right: 5px;
}

/* Advanced menu with compact design and scrollable content */
.advanced-menu {
    position: fixed;
    bottom: 70px;
    right: 20px;
    width: 280px;
    max-height: 70vh;
    background: #2a2a2a;
    border-radius: 8px;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.5);
    border: 1px solid #333;
    display: none;
    z-index: 100;
    flex-direction: column;
    overflow-y: auto;
}

.advanced-menu.active {
    display: flex;
}

/* Menu header with title and close button */
.menu-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 15px;
    border-bottom: 1px solid #333;
}

.menu-header h3 {
    color: #4ecdc4;
    font-size: 16px;
    font-weight: 500;
    margin: 0;
    flex: 1;
}

.close-btn {
    background: none;
    border: none;
    color: #aaa;
    font-size: 18px;
    cursor: pointer;
    padding: 0;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: background 0.2s ease;
}

.close-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
}

/* Accordion menu content with scrollable sections */
.menu-content {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow-y: auto;
}

.menu-section {
    border-bottom: 1px solid #333;
}

.menu-section:last-child {
    border-bottom: none;
}

.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 12px;
    cursor: pointer;
    background: #333;
    transition: background 0.2s ease;
}

.section-header:hover {
    background: #3a3a3a;
}

.section-title {
    color: #4ecdc4;
    font-size: 13px;
    font-weight: 500;
}

.section-arrow {
    color: #aaa;
    font-size: 12px;
    transition: transform 0.2s ease;
}

.section-header.active .section-arrow {
    transform: rotate(90deg);
}

.section-content {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.3s ease;
    background: #2a2a2a;
}

.section-content.active {
    max-height: 280px;
    overflow-y: auto;
}

.section-content::-webkit-scrollbar,
.advanced-menu::-webkit-scrollbar {
    width: 6px;
}

.section-content::-webkit-scrollbar-track,
.advanced-menu::-webkit-scrollbar-track {
    background: #1a1a1a;
    border-radius: 3px;
}

.section-content::-webkit-scrollbar-thumb,
.advanced-menu::-webkit-scrollbar-thumb {
    background: #444;
    border-radius: 3px;
}

.section-content::-webkit-scrollbar-thumb:hover,
.advanced-menu::-webkit-scrollbar-thumb:hover {
    background: #555;
}

.section-content {
    scrollbar-width: thin;
    scrollbar-color: #444 #1a1a1a;
}

.section-inner {
    padding: 8px 12px;
}

/* Control groups for form inputs */
.control-group {
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin-bottom: 8px;
}

.control-group label {
    font-size: 11px;
    color: #bbb;
}

.control-group input[type="range"] {
    width: 100%;
    height: 4px;
    background: #444;
    border-radius: 2px;
    outline: none;
    -webkit-appearance: none;
}

.control-group input[type="range"]::-webkit-slider-thumb {
    width: 14px;
    height: 14px;
    background: #4ecdc4;
    border-radius: 50%;
    cursor: pointer;
    border: 2px solid #2a2a2a;
}

.control-group input[type="range"]::-moz-range-thumb {
    width: 14px;
    height: 14px;
    background: #4ecdc4;
    border-radius: 50%;
    cursor: pointer;
    border: 2px solid #2a2a2a;
}

.control-group span {
    font-size: 10px;
    color: #4ecdc4;
    align-self: flex-end;
}

/* Select input styling */
#pinningMode {
    background: #444;
    color: #e0e0e0;
    border: 1px solid #555;
    border-radius: 4px;
    padding: 4px 6px;
    font-size: 11px;
    width: 100%;
}

/* Apply button for form submissions */
.apply-btn {
    background: #4ecdc4;
    color: #121212;
    border: none;
    border-radius: 4px;
    padding: 6px 10px;
    font-size: 11px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s ease;
    margin-top: 6px;
}

.apply-btn:hover {
    background: #3dbdb4;
}

/* Info list for instructions */
.info-list {
    list-style: none;
    padding: 0;
    margin: 0;
}

.info-list li {
    margin-bottom: 4px;
    font-size: 10px;
    color: #ccc;
    line-height: 1.3;
}

.info-list li strong {
    color: #e0e0e0;
}

/* Save/load section styling */
.save-slots {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-bottom: 10px;
}

.slot-row {
    display: flex;
    gap: 6px;
}

.slot-btn, .load-btn {
    flex: 1;
    padding: 4px 6px;
    border: none;
    border-radius: 4px;
    font-size: 10px;
    cursor: pointer;
    transition: background 0.2s ease;
}

.slot-btn {
    background: #444;
    color: #e0e0e0;
}

.slot-btn:hover {
    background: #555;
}

.load-btn {
    background: #4ecdc4;
    color: #121212;
}

.load-btn:hover {
    background: #3dbdb4;
}

.file-operations {
    display: flex;
    gap: 6px;
    margin-bottom: 10px;
}

.file-btn {
    flex: 1;
    padding: 4px 6px;
    border: none;
    border-radius: 4px;
    font-size: 10px;
    background: #27ae60;
    color: white;
    cursor: pointer;
    transition: background 0.2s ease;
}

.file-btn:hover {
    background: #229954;
}

.presets {
    margin-top: 6px;
}

.presets h5 {
    color: #4ecdc4;
    font-size: 11px;
    margin: 0 0 4px 0;
}

.preset-btn {
    display: block;
    width: 100%;
    padding: 4px 6px;
    margin-bottom: 4px;
    border: none;
    border-radius: 4px;
    font-size: 10px;
    background: #9b59b6;
    color: white;
    cursor: pointer;
    transition: background 0.2s ease;
}

.preset-btn:hover {
    background: #8e44ad;
}

/* Visual effects section styling */
.visual-effects {
    margin-top: 0;
    padding-top: 0;
    border-top: none;
}

.visual-effects h5 {
    color: #4ecdc4;
    font-size: 11px;
    margin: 0 0 8px 0;
}

/* Toggle switch for visual effects */
.toggle-group {
    display: flex;
    align-items: center;
    margin-bottom: 6px;
}

.toggle-switch {
    position: relative;
    display: inline-block;
    width: 32px;
    height: 16px;
    margin-right: 8px;
}

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

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

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

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

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

.toggle-label {
    font-size: 10px;
    color: #e0e0e0;
    cursor: pointer;
    user-select: none;
}

#trailLengthGroup {
    margin-top: 6px;
    opacity: 0.5;
    pointer-events: none;
    transition: opacity 0.3s ease;
}

#trailLengthGroup.enabled {
    opacity: 1;
    pointer-events: auto;
}

/* Shape generator controls */
.shape-controls {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

#shapeType {
    background: #444;
    color: #e0e0e0;
    border: 1px solid #555;
    border-radius: 4px;
    padding: 4px 6px;
    font-size: 11px;
    width: 100%;
}

#ropeControls,
#boxControls,
#boxHeightControls,
#circleControls,
#bridgeControls,
#gridControls,
#gridHeightControls,
#gridSpacingControls {
    display: none;
}

#ropeControls.active,
#boxControls.active,
#boxHeightControls.active,
#circleControls.active,
#bridgeControls.active,
#gridControls.active,
#gridHeightControls.active,
#gridSpacingControls.active {
    display: block;
}

/* Header controls for menu pinning */
.header-controls {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.menu-pin-btn {
    background: none;
    border: none;
    color: #aaa;
    font-size: 14px;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 4px;
    transition: all 0.2s ease;
}

.menu-pin-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
}

.menu-pin-btn.pinned {
    background: #e74c3c;
    color: white;
}

/* Advanced physics section */
.advanced-physics {
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px solid #444;
}

.advanced-physics h5 {
    color: #4ecdc4;
    font-size: 11px;
    margin: 0 0 8px 0;
}

/* Collision visualization */
.collision-highlight {
    box-shadow: 0 0 10px rgba(255, 0, 0, 0.8);
    animation: collision-pulse 0.3s ease-out;
}

@keyframes collision-pulse {
    0% { transform: scale(1); }
    50% { transform: scale(1.2); }
    100% { transform: scale(1); }
}

#collision-content .control-group {
    margin-bottom: 15px;
}

#collision-content .toggle-group {
    margin-bottom: 20px;
}

/* Spring-specific styling */
.tool-btn[data-tool="spring"] .icon {
    font-size: 16px;
}

#stiffness-warning {
    color: #e74c3c;
    font-size: 12px;
    margin-top: 5px;
    text-align: center;
    transition: opacity 0.3s ease;
}

#springs-content .info-text {
    margin-top: 15px;
    padding: 10px;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 4px;
    font-size: 12px;
    line-height: 1.4;
}

#springs-content .info-text p {
    margin: 5px 0;
    color: #bbb;
}

/* Undo/redo button styling */
.action-btn {
    position: relative;
    transition: all 0.2s ease;
}

.action-btn.disabled {
    opacity: 0.5;
    cursor: not-allowed;
    background: #333;
}

.action-btn.disabled:hover {
    background: #333;
    transform: none;
}

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

/* Selection tool styling */
.tool-btn[data-tool="selection"] .icon {
    font-size: 16px;
}

.selection-highlight {
    box-shadow: 0 0 10px rgba(52, 152, 219, 0.8);
}

.selection-box {
    stroke: rgba(52, 152, 219, 0.8);
    stroke-width: 2;
    stroke-dasharray: 5, 5;
    fill: none;
}

.selection-center {
    stroke: rgba(52, 152, 219, 0.6);
    stroke-width: 1;
    stroke-dasharray: 3, 3;
    fill: none;
}

/* Responsive design for smaller screens */
@media (max-width: 1200px) {
    .controls-area {
        height: auto;
        min-height: 60px;
        padding: 8px;
        justify-content: center;
        gap: 15px;
    }
    
    .tool-buttons, .action-buttons, .quick-guide {
        margin: 0;
        flex: 0 0 auto;
    }
    
    .tool-buttons { order: 1; }
    .action-buttons { order: 2; justify-content: center; }
    .quick-guide { order: 3; padding: 0; border-left: none; }
    
    .controls-area:has(.action-buttons:not(:only-child)) {
        justify-content: space-around;
    }
}

@media (max-width: 480px) {
    .controls-area { padding: 6px; }
    .tool-buttons { gap: 4px; }
    .tool-btn { width: 32px; height: 32px; }
    .action-buttons { gap: 6px; }
    .quick-guide { gap: 8px; font-size: 10px; }
}