/* CSS Reset and Base Styles */
@import url('https://fonts.googleapis.com/css2?family=Audiowide&display=swap');

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, #0c0c0c 0%, #1a1a1a 100%);
    color: #e0e0e0;
    overflow: hidden;
    height: 100vh;
    height: 100dvh; /* track the dynamic (visible) viewport on mobile */
}

/* App Layout */
#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh; /* matches the visible area so the sim floor isn't below the fold */
}

/* Header */
.header {
    background: rgba(20, 20, 20, 0.95);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid #333;
    padding: 10px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    z-index: 1000;
}

.logo {
    display: flex;
    align-items: center;
    gap: 10px;
}

.logo i {
    user-select: none;
    font-size: 30px;
    color: #00d4ff;
    animation: pulse 2s infinite;
}

.logo h1 {
    user-select: none;
    font-family: 'Audiowide', cursive;
    font-size: 36px;
    font-weight: 400;
    background: linear-gradient(45deg, #00d4ff, #0099cc);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.header-controls {
    display: flex;
    align-items: center;
    gap: 15px;
}

.performance-info {
    display: flex;
    gap: 15px;
    font-size: 12px;
    color: #888;
    align-items: flex-start;
}

.counts-display {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.count-row {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    min-width: 90px;
}

.count-label {
    font-weight: 600;
    text-shadow: 0 0 2px rgba(0,0,0,0.5);
}

/* Main Content */
.main-content {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* Ensure Toolbar is hidden if HTML persists */
.toolbar {
    display: none !important;
}

/* Canvas Container */
.canvas-container {
    flex: 1;
    position: relative;
    background: #090909;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

#simulationCanvas {
    border: 2px solid #333;
    border-radius: 12px;
    background: #000;
    cursor: crosshair;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
}

.canvas-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    pointer-events: none;
    z-index: 4; /* tool cursor stays above the GPU + overlay canvases */
}

/* Stacked 2D canvases for the WebGPU rendering path. The under-canvas holds
   widgets that sit BEHIND particles (grid, barriers, drains, ...); the
   overlay-canvas holds effects that sit IN FRONT (explosions, trails, tool
   feedback). Both are hidden unless GPU mode is active. */
.overlay-canvas,
.under-canvas {
    position: absolute;
    top: 0;
    left: 0;
    pointer-events: none;
    display: none;
}

/* GPU-mode z-stack: accents(1) < under(2) < particles(3) < overlay(4) < cursor(5) */
.canvas-container.gpu-mode .under-canvas {
    display: block;
    z-index: 2;
}

.canvas-container.gpu-mode .overlay-canvas {
    display: block;
    z-index: 4;
}

.canvas-container.gpu-mode .canvas-overlay {
    z-index: 5;
}

/* In GPU mode the chrome moves to the container so both stacked canvases share
   one content box (no per-canvas border offset to misalign the layers). The
   container's radial-gradient background shows through the transparent particles
   canvas. */
.canvas-container.gpu-mode {
    border: 2px solid #333;
    border-radius: 12px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
}

.canvas-container.gpu-mode #simulationCanvas {
    border: none;
    box-shadow: none;
    z-index: 3; /* particles sit above the under-canvas, below the overlay */
    /* Absolute top-left so the WebGPU particle layer shares the exact origin of
       the 2D under/overlay canvases. (Flex-centering would offset it by the
       container border, shifting particles ~2px up-left of the walls.) */
    position: absolute;
    top: 0;
    left: 0;
    /* Transparent so the container's radial-gradient background shows through
       the WebGPU particle layer. */
    background: transparent;
}

.tool-cursor {
    position: absolute;
    width: 20px;
    height: 20px;
    border: 2px solid #00d4ff;
    border-radius: 50%;
    transform: translate(-50%, -50%);
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.2s ease;
    box-shadow: 0 0 10px rgba(0, 212, 255, 0.5);
}

.tool-cursor.active {
    opacity: 1;
}

/* Controls Panel */
.controls-panel {
    width: 280px;
    background: rgba(25, 25, 25, 0.95);
    backdrop-filter: blur(10px);
    border-left: 1px solid #333;
    padding: 20px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 25px;
}

.panel-section h3 {
    color: #fff;
    margin-bottom: 15px;
    font-size: 14px;
    text-transform: uppercase;
    letter-spacing: 1px;
    border-bottom: 1px solid #444;
    padding-bottom: 8px;
}

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

.control-group label {
    display: block;
    margin-bottom: 6px;
    font-size: 13px;
    color: #ccc;
}

.control-group input[type="range"] {
    width: 100%;
    height: 6px;
    background: #333;
    border-radius: 3px;
    outline: none;
    margin-bottom: 5px;
}

.control-group input[type="range"]::-webkit-slider-thumb {
    appearance: none;
    width: 18px;
    height: 18px;
    background: linear-gradient(45deg, #00d4ff, #0099cc);
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 2px 6px rgba(0, 212, 255, 0.3);
}

.control-group input[type="range"]::-moz-range-thumb {
    width: 18px;
    height: 18px;
    background: linear-gradient(45deg, #00d4ff, #0099cc);
    border-radius: 50%;
    cursor: pointer;
    border: none;
    box-shadow: 0 2px 6px rgba(0, 212, 255, 0.3);
}

.value-display {
    font-size: 12px;
    color: #00d4ff;
    font-weight: 600;
}

/* Slider rows: put the value next to the label (top row), slider full-width
   below. Scoped to range groups so checkboxes/dropdowns keep their layout. */
.control-group:has(input[type="range"]) {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    row-gap: 4px;
}

.control-group:has(input[type="range"]) label {
    flex: 1;
    margin-bottom: 0;
}

.control-group:has(input[type="range"]) .value-display {
    order: 1;
}

.control-group:has(input[type="range"]) input[type="range"] {
    order: 2;
    flex-basis: 100%;
}

.control-group input[type="checkbox"] {
    margin-right: 8px;
    accent-color: #00d4ff;
}

.text-input {
    width: 100%;
    padding: 8px 12px;
    background: rgba(40, 40, 40, 0.8);
    border: 1px solid #555;
    border-radius: 6px;
    color: #e0e0e0;
    font-size: 14px;
    margin-bottom: 8px;
}

.text-input:focus {
    outline: none;
    border-color: #00d4ff;
    box-shadow: 0 0 0 2px rgba(0, 212, 255, 0.2);
}

.preset-select {
    width: 100%;
    padding: 8px 12px;
    background: rgba(40, 40, 40, 0.8);
    border: 1px solid #555;
    border-radius: 6px;
    color: #e0e0e0;
    font-size: 14px;
    margin-bottom: 8px;
}

.preset-select:focus {
    outline: none;
    border-color: #00d4ff;
}

/* Buttons */
.btn {
    padding: 8px 16px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 600;
    transition: all 0.3s ease;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.btn-primary {
    background: linear-gradient(45deg, #00d4ff, #0099cc);
    color: #000;
}

.btn-primary:hover {
    box-shadow: 0 4px 15px rgba(0, 212, 255, 0.4);
}

.btn-success {
    background: linear-gradient(45deg, #00ff88, #00cc66);
    color: #000;
}

.btn-success:hover {
    box-shadow: 0 4px 15px rgba(0, 255, 136, 0.4);
}

.btn-warning {
    background: linear-gradient(45deg, #ffaa00, #cc8800);
    color: #000;
}

.btn-warning:hover {
    box-shadow: 0 4px 15px rgba(255, 170, 0, 0.4);
}

.btn-danger {
    background: linear-gradient(45deg, #ff4444, #cc0000);
    color: #fff;
    font-size: 14px;
}

.btn-danger:hover {
    box-shadow: 0 4px 15px rgba(255, 68, 68, 0.4);
}

.tool-btn {
    padding: 8px;
    font-size: 12px;
}

/* Status Bar */
.status-bar {
    background: rgba(15, 15, 15, 0.95);
    backdrop-filter: blur(10px);
    border-top: 1px solid #333;
    padding: 8px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 12px;
    color: #888;
}

.status-left, .status-right {
    display: flex;
    gap: 20px;
}

/* Loading Screen */
.loading-screen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.9);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10000;
    transition: opacity 0.5s ease;
}

.loading-content {
    text-align: center;
    color: #e0e0e0;
}

.loading-icon {
    font-size: 48px;
    color: #00d4ff;
    margin-bottom: 20px;
    animation: spin 2s linear infinite;
}

.loading-content h2 {
    margin-bottom: 10px;
    font-size: 24px;
}

.loading-content p {
    color: #888;
    font-size: 14px;
}

/* Animations */
@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.7; }
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

@keyframes glow {
    0% { box-shadow: 0 0 10px rgba(255, 69, 0, 0.5); }
    100% { box-shadow: 0 0 20px rgba(255, 69, 0, 0.8); }
}

/* Responsive Design */
@media (max-width: 1200px) {
    .controls-panel {
        width: 240px;
    }
    
    #simulationCanvas {
        width: 100%;
        height: auto;
        max-width: 800px;
        max-height: 600px;
    }
}

/* ===== Mobile / touch layout (toggled by body.mobile-ui from JS, which also
   covers touch tablets wider than 768px) ===== */
/* backdrop-filter blur is very expensive on mobile GPUs; the backgrounds are
   already near-opaque, so drop it. */
body.mobile-ui .header,
body.mobile-ui .controls-panel,
body.mobile-ui #mobileBar { backdrop-filter: none; }

body.mobile-ui .header { padding: 8px 12px; }
body.mobile-ui .logo h1 { font-size: 22px; }
body.mobile-ui .logo i { font-size: 22px; }
body.mobile-ui .header-controls { gap: 8px; }
/* Override the 44px touch-target min-height for the compact header buttons. */
body.mobile-ui .header .btn { padding: 4px 10px; font-size: 12px; min-height: 30px; }
/* Stack FPS and the (total-only) count vertically; keep just the Particles row. */
body.mobile-ui .performance-info { font-size: 10px; gap: 2px; flex-direction: column; align-items: flex-end; }
body.mobile-ui .counts-display .count-row:nth-child(-n+3) { display: none; }
body.mobile-ui .counts-display .count-row { min-width: 0; gap: 6px; }

/* Canvas fills the area; reserve room for the fixed bottom tool bar. */
body.mobile-ui .main-content { padding-bottom: 60px; }

/* No browser gestures (scroll/zoom) when drawing on the canvas. */
body.mobile-ui .canvas-container canvas { touch-action: none; }

/* JS sizes the canvases to the container; drop the desktop max-dimension caps
   (max-width/height from the 1200px breakpoint) so the particle layer fills the
   full work area in portrait instead of being clamped to 600px tall. */
body.mobile-ui #simulationCanvas,
body.mobile-ui .under-canvas,
body.mobile-ui .overlay-canvas {
    max-width: none;
    max-height: none;
}

/* Controls panel becomes a slide-up bottom drawer. */
body.mobile-ui .controls-panel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    width: auto;
    max-height: 75vh;
    transform: translateY(100%);
    transition: transform 0.25s ease;
    z-index: 300;
    border-left: none;
    border-top: 1px solid #333;
    border-radius: 14px 14px 0 0;
    padding-bottom: 24px;
}
body.mobile-ui .controls-panel.panel-open {
    transform: translateY(0);
}

body.mobile-ui #mobileBar { display: flex; }

/* Mobile bottom tool bar (built in ui.js). Hidden on desktop. */
#mobileBar {
    display: none;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 350;
    align-items: center;
    gap: 8px;
    padding: 8px 10px;
    background: rgba(15, 15, 15, 0.92);
    backdrop-filter: blur(8px);
    border-top: 1px solid #333;
}

#mobileBar .mb-tools {
    display: flex;
    gap: 6px;
    overflow-x: auto;
    flex: 1;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    /* Contained pane: its bordered edge is the divider from the liquid chips. */
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid #3a3a3a;
    border-radius: 10px;
    padding: 5px 6px;
}
#mobileBar .mb-tools::-webkit-scrollbar { display: none; }

#mobileBar .mb-btn,
#mobileBar .mb-settings {
    flex: 0 0 auto;
    width: 44px;
    height: 44px;
    border-radius: 10px;
    border: 1px solid #444;
    background: #222;
    color: #ccc;
    font-size: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
}
/* The active tool expands into a labeled pill so you know what's selected. */
#mobileBar .mb-btn.active {
    width: auto;
    padding: 0 14px;
    gap: 8px;
    background: linear-gradient(45deg, #00d4ff, #0099cc);
    color: #00171f;
    border-color: #00d4ff;
}
#mobileBar .mb-btn-label {
    display: none;
    font-size: 13px;
    font-weight: 600;
    white-space: nowrap;
}
#mobileBar .mb-btn.active .mb-btn-label { display: inline; }
#mobileBar .mb-settings { color: #00d4ff; }

#mobileBar .mb-liquids {
    display: flex;
    gap: 6px;
    flex: 0 0 auto;
    /* Separation comes from the tool pane's edge + the bar gap, not a chip-side line. */
    margin-left: 2px;
}
#mobileBar .mb-liquid {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    border: 2px solid transparent;
    cursor: pointer;
}
#mobileBar .mb-liquid.active {
    border-color: #fff;
}

/* Close (✕) button shown at the top of the drawer on mobile. */
#drawerClose {
    display: none;
}
body.mobile-ui #drawerClose {
    display: block;
    position: sticky;
    top: 0;
    margin-left: auto;
    background: none;
    border: none;
    color: #aaa;
    font-size: 22px;
    cursor: pointer;
}

/* Scrollbar Styling */
::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: rgba(40, 40, 40, 0.5);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb {
    background: linear-gradient(45deg, #00d4ff, #0099cc);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: linear-gradient(45deg, #0099cc, #007799);
}

/* Canvas Effects */

#simulationCanvas {
    position: relative;
    z-index: 2;
}

/* Tool-specific cursor styles */
.canvas-container.tool-pour #simulationCanvas { cursor: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><circle cx="12" cy="12" r="10" fill="none" stroke="%2300d4ff" stroke-width="2"/><circle cx="12" cy="12" r="3" fill="%2300d4ff"/></svg>') 12 12, crosshair; }

.canvas-container.tool-barrier #simulationCanvas { cursor: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><rect x="2" y="10" width="20" height="4" fill="%23fff" stroke="%23333" stroke-width="1"/></svg>') 12 12, crosshair; }

.canvas-container.tool-eraser #simulationCanvas { cursor: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><rect x="6" y="8" width="12" height="8" fill="%23ff4444" stroke="%23fff" stroke-width="1" rx="2"/></svg>') 12 12, crosshair; }

.canvas-container.tool-stirring #simulationCanvas { cursor: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><circle cx="12" cy="12" r="8" fill="none" stroke="%2300ff88" stroke-width="2"/><path d="M12 4 L16 8 L12 12 L8 8 Z" fill="%2300ff88"/></svg>') 12 12, crosshair; }

.canvas-container.tool-attractor #simulationCanvas { cursor: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><circle cx="12" cy="12" r="8" fill="none" stroke="%23ff44ff" stroke-width="2"/><circle cx="12" cy="12" r="2" fill="%23ff44ff"/></svg>') 12 12, crosshair; }

.canvas-container.tool-bomb #simulationCanvas { cursor: crosshair; }

/* Utility Classes */
.hidden {
    display: none !important;
}

.fade-in {
    animation: fadeIn 0.5s ease;
}

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

/* High DPI Display Support */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    #simulationCanvas {
        image-rendering: -webkit-optimize-contrast;
        image-rendering: crisp-edges;
    }
}

/* Mobile-specific optimizations */
@media (max-width: 480px) {
    .header {
        padding: 10px;
    }
    
    .logo h1 {
        font-size: 18px;
    }
    
    .performance-info {
        font-size: 10px;
        gap: 10px;
    }
    
    .btn {
        padding: 6px 12px;
        font-size: 12px;
    }
    
    .tool-btn {
        padding: 8px;
        font-size: 12px;
    }
    
    .control-group label {
        font-size: 12px;
    }
    
    .status-bar {
        font-size: 10px;
        padding: 6px 15px;
    }
    
    #simulationCanvas {
        max-height: 300px;
    }
}

/* Touch-specific styles */
@media (hover: none) and (pointer: coarse) {
    .tool-btn, .btn {
        min-height: 44px; /* Minimum touch target size */
    }
    
    .control-group input[type="range"] {
        height: 8px;
    }
    
    .control-group input[type="range"]::-webkit-slider-thumb {
        width: 22px;
        height: 22px;
    }
    
    .tool-cursor {
        display: none; /* Hide cursor on touch devices */
    }
}

/* Context Menu */
.context-menu {
    position: fixed;
    background: rgba(25, 25, 25, 0.95);
    backdrop-filter: blur(10px);
    border: 1px solid #444;
    border-radius: 8px;
    padding: 8px;
    min-width: 180px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
    z-index: 10000;
    opacity: 0;
    transform: scale(0.95);
    transition: opacity 0.2s ease, transform 0.2s ease;
    pointer-events: none; /* Initially disabled */
}

.context-menu.active {
    opacity: 1;
    transform: scale(1);
    pointer-events: auto;
}

.context-menu-header {
    padding: 8px 12px;
    font-size: 12px;
    text-transform: uppercase;
    color: #888;
    border-bottom: 1px solid #444;
    margin-bottom: 8px;
    font-weight: 600;
}

.context-menu-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 4px 12px;
    color: #e0e0e0;
    cursor: pointer;
    border-radius: 6px;
    transition: all 0.2s ease;
    font-size: 14px;
    position: relative;
}

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

/* Tooltip Styles */
.tool-tooltip {
    display: none;
    position: absolute;
    left: 100%;
    top: 50%;
    transform: translateY(-50%);
    background: rgba(15, 15, 15, 0.95);
    color: #ccc;
    padding: 6px 10px;
    border-radius: 4px;
    font-size: 11px;
    white-space: nowrap;
    border: 1px solid #444;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
    margin-left: 10px;
    z-index: 10002;
    pointer-events: none;
    opacity: 0;
    animation: tooltipFadeIn 0.2s forwards;
}

.tool-tooltip::before {
    content: '';
    position: absolute;
    top: 50%;
    right: 100%;
    margin-top: -4px;
    border-width: 4px;
    border-style: solid;
    border-color: transparent #444 transparent transparent;
}

.context-menu-item:hover .tool-tooltip {
    display: block;
}

@keyframes tooltipFadeIn {
    from { opacity: 0; transform: translateY(-50%) translateX(-5px); }
    to { opacity: 1; transform: translateY(-50%) translateX(0); }
}

.context-menu-item.active {
    background: rgba(0, 212, 255, 0.15);
    color: #00d4ff;
}

.context-menu-item i {
    width: 20px;
    text-align: center;
    font-size: 14px;
}

.liquid-preview {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    margin-right: 10px;
    border: 1px solid rgba(255, 255, 255, 0.3);
    box-shadow: 0 0 4px rgba(0, 0, 0, 0.3);
}

.liquid-preview.water {
    background: #4da6ff;
    box-shadow: 0 0 6px rgba(77, 166, 255, 0.6);
}

.liquid-preview.oil {
    background: #ffcc00;
    box-shadow: 0 0 6px rgba(255, 204, 0, 0.6);
}

.liquid-preview.lava {
    background: #ff4500;
    box-shadow: 0 0 6px rgba(255, 69, 0, 0.6);
}

/* Submenu Styles */
.context-submenu {
    display: none;
    position: absolute;
    left: 100%;
    top: -5px; /* Align with top of parent item */
    margin-left: 5px;
    background: rgba(25, 25, 25, 0.95);
    backdrop-filter: blur(10px);
    border: 1px solid #444;
    border-radius: 8px;
    padding: 4px;
    min-width: 160px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
    z-index: 10001;
}

.context-submenu::before {
    content: '';
    position: absolute;
    top: 0;
    bottom: 0;
    left: -10px;
    width: 15px;
    background: transparent;
}

.context-menu-item:hover > .context-submenu {
    display: block;
}

.context-menu-item.has-submenu {
    padding-right: 25px;
}

.context-menu-item.has-submenu::after {
    content: '\f054';
    font-family: 'Font Awesome 6 Free';
    font-weight: 900;
    position: absolute;
    right: 8px;
    font-size: 10px;
    color: #888;
}

.context-menu-item:hover.has-submenu::after {
    color: #fff;
}