body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; /* Modern, clean font */
    margin: 0;
    padding: 0;
    background: linear-gradient(135deg, #e0f7fa, #c2e0f7); /* Soft gradient background */
    color: #333;
    display: flex;
    flex-direction: column;
    min-height: 100vh; /* Ensure footer stays at bottom */
    transition: background 0.3s ease, color 0.3s ease;
}

body.dark-mode {
    background: linear-gradient(135deg, #1a1a2e, #16213e);
    color: #f0f0f0;
}

header {
    background-color: #8e97f3; /* Slightly updated lavender */
    color: white;
    text-align: center;
    padding: 2.5em 0; /* Slightly more padding */
    margin-bottom: 20px; /* Space out from main content */
    box-shadow: 0 4px 8px rgba(0,0,0,0.05); /* Subtle shadow */
    position: relative;
    transition: background-color 0.3s ease;
}

body.dark-mode header {
    background-color: #303f9f;
    box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}

.theme-controls {
    position: absolute;
    top: 20px;
    right: 20px;
}

.theme-button {
    background: none;
    border: none;
    cursor: pointer;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: rgba(255, 255, 255, 0.2);
    transition: background-color 0.3s ease;
}

.theme-button:hover {
    background-color: rgba(255, 255, 255, 0.3);
}

.theme-button svg {
    fill: white;
}

body.dark-mode .theme-button svg path#lightIcon {
    display: none;
}

body.dark-mode .theme-button svg path#darkIcon {
    display: block;
}

body:not(.dark-mode) .theme-button svg path#lightIcon {
    display: block;
}

body:not(.dark-mode) .theme-button svg path#darkIcon {
    display: none;
}

header h1 {
    font-size: 2.5em; /* Slightly larger title */
    margin-bottom: 0.2em;
    text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1); /* Text shadow for depth */
}

header p {
    font-size: 1.1em;
    color: #f0f0f0; /* Lighter text for subheading */
}

main {
    padding: 20px;
    flex-grow: 1; /* Allow main to take up remaining space */
    display: flex;
    flex-direction: column;
    align-items: center; /* Center content horizontally */
}

#image-upload-section {
    display: flex;
    flex-direction: column;
    align-items: center;
    position: relative;
    /* Styles for the upload section */
    display: flex; /* Use flexbox for layout */
    flex-direction: column; /* Stack children vertically */
    align-items: center; /* Center items horizontally */
}

#image-container {
    position: relative; /* Needed for absolute positioning of overlays */
    width: 100%;
    max-width: 400px; /* Max width for the image preview area */
    margin-top: 15px;
    min-height: 200px; /* Minimum height */
    border: 1px dashed #ccc; /* Optional: border to show the container */
    background-color: #1a1a2e; /* Dark background for contrast with particles */
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden; /* Hide overflow from animations */
    border-radius: 8px; /* Match other rounded corners */
    cursor: crosshair; /* Indicate interactivity */
}

#image-preview {
    /* Styles for the preview div itself */
    display: flex; /* Use flex to center the image if needed */
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    opacity: 0.7; /* Slightly dim image to make particles stand out */
    transition: opacity 0.3s ease;
}
#image-container:hover #image-preview {
    opacity: 0.85; /* Bring image back slightly on hover */
}

#image-preview img {
    max-width: 100%;
    max-height: 400px; /* Limit image height */
    display: block; /* Remove extra space below image */
    object-fit: contain; /* Ensure image fits within bounds */
    pointer-events: none; /* Don't let image block mouse events for canvas */
}

#particleCanvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none; /* Canvas itself doesn't block mouse events for container */
    display: none; /* Hidden by default */
}

section {
    margin-bottom: 25px; /* Slightly more margin */
    background-color: white;
    padding: 25px; /* Slightly more padding */
    border-radius: 12px; /* More rounded corners */
    box-shadow: 0 6px 12px rgba(0,0,0,0.08); /* Enhanced shadow */
    width: 90%; /* Limit section width for better readability on larger screens */
    max-width: 800px; /* Maximum width of sections */
    transition: background-color 0.3s ease, box-shadow 0.3s ease;
    position: relative;
    overflow: hidden;
    backdrop-filter: blur(5px);
    border: 1px solid rgba(142, 151, 243, 0.2);
}

section::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 2px;
    background: linear-gradient(90deg, transparent, rgba(142, 151, 243, 0.5), transparent);
    z-index: 1;
}

body.dark-mode section {
    background-color: #252a41;
    box-shadow: 0 6px 12px rgba(0,0,0,0.2);
}

body.dark-mode section::before {
    background: linear-gradient(90deg, transparent, rgba(67, 56, 202, 0.5), transparent);
}

#particle-controls-section {
    margin-bottom: 25px;
    background-color: #f0f4f8; /* Light blue-grey background */
    padding: 25px;
    border-radius: 12px;
    box-shadow: 0 6px 12px rgba(0,0,0,0.08);
    width: 90%;
    max-width: 800px;
    display: flex; /* Use flexbox */
    flex-direction: column; /* Stack children vertically */
    align-items: center; /* Center children horizontally */
    transition: background-color 0.3s ease;
}

body.dark-mode #particle-controls-section {
    background-color: #1e2a4a;
}

.particle-type-selector {
    margin-bottom: 15px;
    width: 100%;
    max-width: 400px;
    display: flex;
    align-items: center;
    gap: 10px;
}

.particle-type-selector label {
    font-weight: bold;
    color: #555;
    min-width: 100px;
    flex-shrink: 0;
}

body.dark-mode .particle-type-selector label,
body.dark-mode .control-group label {
    color: #b0b0b0;
}

.particle-type-selector select {
    flex-grow: 1;
    padding: 10px 15px;
    border-radius: 20px;
    border: 1px solid #ccc;
    background: linear-gradient(to bottom, #ffffff, #f5f5f5);
    font-family: inherit;
    cursor: pointer;
    outline: none;
    transition: all 0.3s ease;
    box-shadow: 0 2px 5px rgba(0,0,0,0.05);
    -webkit-appearance: none;
    appearance: none;
    background-image: url("data:image/svg+xml;charset=US-ASCII,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' width='24' height='24'%3E%3Cpath fill='%238e97f3' d='M7 10l5 5 5-5z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 10px center;
}

body.dark-mode .particle-type-selector select {
    background: linear-gradient(to bottom, #32385c, #262b4a);
    border-color: #454d6e;
    color: #f0f0f0;
    box-shadow: 0 2px 5px rgba(0,0,0,0.2);
    background-image: url("data:image/svg+xml;charset=US-ASCII,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' width='24' height='24'%3E%3Cpath fill='%23f0f0f0' d='M7 10l5 5 5-5z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 10px center;
}

.particle-type-selector select:hover {
    border-color: #8e97f3;
    box-shadow: 0 3px 7px rgba(0,0,0,0.1);
}

.particle-type-selector select:focus {
    border-color: #8e97f3;
    box-shadow: 0 0 0 3px rgba(142, 151, 243, 0.3);
}

body.dark-mode .particle-type-selector select:hover {
    border-color: #3f51b5;
    box-shadow: 0 3px 7px rgba(0,0,0,0.3);
}

body.dark-mode .particle-type-selector select:focus {
    border-color: #3f51b5;
    box-shadow: 0 0 0 3px rgba(63, 81, 181, 0.3);
}

.presets-container {
    display: flex;
    flex-wrap: wrap;
    gap: 12px; /* Slightly more gap */
    justify-content: center;
    margin-top: 10px;
}

.preset-button {
    background: linear-gradient(135deg, #2ed5c4, #3f51b5);
    color: white;
    border: none;
    padding: 12px 20px;
    border-radius: 25px;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
    font-size: 0.95em;
    position: relative;
    overflow: hidden;
}

.preset-button::after {
    content: '';
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: rgba(255, 255, 255, 0.1);
    transform: rotate(45deg);
    opacity: 0;
    transition: opacity 0.3s;
}

.preset-button:hover {
    background: linear-gradient(135deg, #1abc9c, #303f9f);
    transform: translateY(-2px);
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2);
}

.preset-button:hover::after {
    opacity: 1;
    animation: shimmer 1.5s infinite;
}

.preset-button.active {
    background: linear-gradient(135deg, #ff6b6b, #ff8e8e);
    transform: scale(1.05);
    box-shadow: 0 4px 10px rgba(255, 107, 107, 0.4);
}

@keyframes shimmer {
    0% { transform: translate(-50%, -50%) rotate(45deg); }
    100% { transform: translate(150%, 150%) rotate(45deg); }
}

#scanButton {
    margin: 20px auto 10px;
    position: relative;
    background: linear-gradient(135deg, #1e40af, #3730a3, #4f46e5);
    color: white;
    border: none;
    padding: 15px 30px;
    border-radius: 30px;
    cursor: pointer;
    font-size: 1.2em;
    transition: all 0.3s ease;
    box-shadow: 0 0 15px rgba(79, 70, 229, 0.5), 0 0 30px rgba(79, 70, 229, 0.3);
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    width: 80%;
    max-width: 320px;
    z-index: 1;
}

#scanButton:hover {
    transform: translateY(-3px);
    background: linear-gradient(135deg, #2563eb, #4338ca, #6366f1);
    box-shadow: 0 0 20px rgba(99, 102, 241, 0.6), 0 0 40px rgba(79, 70, 229, 0.4);
}

#scanButton:active {
    transform: translateY(0);
    box-shadow: 0 0 10px rgba(79, 70, 229, 0.4);
}

.scan-icon {
    width: 22px;
    height: 22px;
    border: 2px solid white;
    border-radius: 50%;
    position: relative;
    display: inline-block;
    margin-right: 5px;
}

.scan-icon::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 70%;
    height: 2px;
    background-color: white;
    transform: translate(-50%, -50%);
}

.scan-icon::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 2px;
    height: 70%;
    background-color: white;
    transform: translate(-50%, -50%);
}

.scan-text {
    position: relative;
    z-index: 2;
    font-weight: bold;
    letter-spacing: 0.5px;
}

.scan-pulse {
    position: absolute;
    top: -20%;
    left: -10%;
    right: -10%;
    bottom: -20%;
    background: radial-gradient(circle, rgba(99, 102, 241, 0.8) 0%, rgba(79, 70, 229, 0) 70%);
    opacity: 0;
    z-index: 0;
    animation: pulse-scan 3s infinite;
}

@keyframes pulse-scan {
    0% { opacity: 0; transform: scale(0.8); }
    50% { opacity: 0.5; }
    100% { opacity: 0; transform: scale(1.2); }
}

body.dark-mode #scanButton {
    background: linear-gradient(135deg, #1e3a8a, #312e81, #4338ca);
    box-shadow: 0 0 15px rgba(67, 56, 202, 0.5), 0 0 30px rgba(67, 56, 202, 0.3);
}

body.dark-mode #scanButton:hover {
    background: linear-gradient(135deg, #1d4ed8, #3730a3, #4f46e5);
    box-shadow: 0 0 20px rgba(79, 70, 229, 0.6), 0 0 40px rgba(67, 56, 202, 0.4);
}

#activateParticlesButton {
    background: linear-gradient(135deg, #55efc4, #00b894);
    color: white;
    border: none;
    padding: 12px 20px;
    border-radius: 25px;
    cursor: pointer;
    font-size: 1em;
    transition: all 0.3s ease;
    box-shadow: 0 4px 8px rgba(0,0,0,0.15);
    min-width: 180px;
    text-align: center;
    position: relative;
    overflow: hidden;
}

#activateParticlesButton::after {
    content: '';
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: rgba(255, 255, 255, 0.1);
    transform: rotate(45deg);
    opacity: 0;
    transition: opacity 0.3s;
}

#activateParticlesButton:hover {
    background: linear-gradient(135deg, #00b894, #20a87f);
    transform: translateY(-2px);
    box-shadow: 0 6px 12px rgba(0,0,0,0.2);
}

#activateParticlesButton:hover::after {
    opacity: 1;
    animation: shimmer 1.5s infinite;
}

#activateParticlesButton:disabled {
    background: linear-gradient(135deg, #d3d0e7, #b8b5c9);
    color: #888;
    cursor: default;
    transform: none;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

#activateParticlesButton:disabled:hover {
    background: linear-gradient(135deg, #d3d0e7, #b8b5c9);
    transform: none;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

#activateParticlesButton:disabled::after {
    display: none;
}

#results-section {
    background-color: #f8f9fa; /* Lighter background for results */
    border: 1px solid #eee;
    border-radius: 12px;
    display: flex; /* Use flexbox */
    flex-direction: column; /* Stack children vertically */
    align-items: center; /* Center children horizontally */
    transition: background-color 0.3s ease, border-color 0.3s ease;
}

body.dark-mode #results-section {
    background-color: #2a3050;
    border-color: #454d6e;
}

#essence-recommendations {
    padding: 20px;
    font-size: 1em;
    line-height: 1.6;
    width: 100%; /* Ensure it takes full width */
    box-sizing: border-box; /* Include padding in width */
}

#essence-recommendations ul {
    list-style-type: none; /* Remove default bullet points */
    padding-left: 0; /* Reset default padding */
}

#essence-recommendations li {
    margin-bottom: 10px;
    padding-bottom: 10px;
    border-bottom: 1px dashed #ddd; /* Dashed line between recommendations */
}

#essence-recommendations li:last-child {
    border-bottom: none; /* No border for the last item */
}

.action-controls {
    display: flex;
    flex-wrap: wrap; /* Allow controls to wrap on smaller screens */
    gap: 20px; /* Space between control groups */
    justify-content: center; /* Center control groups */
    width: 90%;
    max-width: 600px; /* Max width for controls container */
    margin-top: 20px;
    padding: 20px;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 15px;
    box-shadow: 0 10px 20px rgba(0,0,0,0.05), 0 6px 6px rgba(0,0,0,0.07);
    backdrop-filter: blur(5px);
    border: 1px solid rgba(142, 151, 243, 0.2);
}

body.dark-mode .action-controls {
    background: rgba(38, 43, 74, 0.9);
    box-shadow: 0 10px 20px rgba(0,0,0,0.15), 0 6px 6px rgba(0,0,0,0.2);
    border: 1px solid rgba(63, 81, 181, 0.2);
}

.control-group {
    display: flex;
    align-items: center; /* Vertically align label, input, and value */
    gap: 8px; /* Space within each control group */
    font-size: 0.9em;
}

.control-group label {
    font-weight: bold;
    color: #555;
    min-width: 130px;
}

.control-group input[type="range"] {
    cursor: pointer;
    width: 130px; /* Wider range slider */
    height: 8px;
    -webkit-appearance: none;
    appearance: none;
    background: linear-gradient(to right, #8e97f3 0%, #55efc4 100%);
    border-radius: 10px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1) inset;
    outline: none;
    transition: all 0.3s ease;
}

.control-group input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 0 10px rgba(142, 151, 243, 0.5);
    cursor: pointer;
    border: 2px solid #8e97f3;
    transition: all 0.2s ease;
}

.control-group input[type="range"]::-moz-range-thumb {
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 0 10px rgba(142, 151, 243, 0.5);
    cursor: pointer;
    border: 2px solid #8e97f3;
    transition: all 0.2s ease;
}

.control-group input[type="range"]:hover::-webkit-slider-thumb {
    box-shadow: 0 0 15px rgba(142, 151, 243, 0.8);
    transform: scale(1.1);
}

.control-group input[type="range"]:hover::-moz-range-thumb {
    box-shadow: 0 0 15px rgba(142, 151, 243, 0.8);
    transform: scale(1.1);
}

.control-group input[type="range"]:active::-webkit-slider-thumb {
    transform: scale(1.2);
}

.control-group input[type="range"]:active::-moz-range-thumb {
    transform: scale(1.2);
}

.control-group span {
    min-width: 36px; /* Ensure space for value display */
    text-align: center;
    color: #fff;
    background: #8e97f3;
    font-weight: bold;
    padding: 4px 8px;
    border-radius: 12px;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

body.dark-mode .control-group input[type="range"] {
    background: linear-gradient(to right, #3f51b5 0%, #1abc9c 100%);
    box-shadow: 0 1px 3px rgba(0,0,0,0.3) inset;
}

body.dark-mode .control-group span {
    background: #3f51b5;
    box-shadow: 0 2px 5px rgba(0,0,0,0.3);
}

textarea {
    width: 100%;
    padding: 12px;
    margin-top: 12px;
    border: 1px solid #ddd;
    border-radius: 8px;
    box-sizing: border-box;
    resize: vertical;
    font-size: 1em;
    font-family: inherit; /* Inherit font for textarea */
    transition: background-color 0.3s ease, border-color 0.3s ease;
}

body.dark-mode textarea {
    background-color: #32385c;
    border-color: #454d6e;
    color: #f0f0f0;
}

body.dark-mode textarea::placeholder {
    color: #a0a0a0;
}

.action-buttons {
    display: flex;
    gap: 15px; /* Space between buttons */
    margin-top: 25px; /* Space above the buttons */
    margin-bottom: 20px; /* Space below the buttons */
    justify-content: center; /* Center buttons horizontally */
}

footer {
    text-align: center;
    padding: 1.5em 0; /* More padding */
    background-color: #e0e0e0; /* Lighter footer background */
    color: #666;
    margin-top: 40px; /* Spacing above footer */
    box-shadow: 0 -2px 5px rgba(0,0,0,0.05); /* Subtle top shadow */
    transition: background-color 0.3s ease, color 0.3s ease;
}

body.dark-mode footer {
    background-color: #1a1a2e;
    color: #a0a0a0;
    box-shadow: 0 -2px 5px rgba(0,0,0,0.2);
}

.pulsate {
    animation: pulsate 2s infinite;
}

@keyframes pulsate {
    0% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.05); opacity: 0.8; }
    100% { transform: scale(1); opacity: 1; }
}

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

.pulse-feedback {
    animation: pulse-feedback 0.5s ease-in-out;
}

@keyframes shake-error {
    0%, 100% { transform: translateX(0); }
    10%, 30%, 50%, 70%, 90% { transform: translateX(-5px); }
    20%, 40%, 60%, 80% { transform: translateX(5px); }
}

.shake-error {
    animation: shake-error 0.6s cubic-bezier(0.36, 0.07, 0.19, 0.97) both;
}

.control-group input[type="range"]:focus {
    box-shadow: 0 0 0 3px rgba(142, 151, 243, 0.3);
}

body.dark-mode .control-group input[type="range"]:focus {
    box-shadow: 0 0 0 3px rgba(63, 81, 181, 0.3);
}

/* Mobile responsiveness */
@media (max-width: 600px) {
    body {
        font-size: 16px;
    }
    header h1 {
        font-size: 2.2em;
    }
    section {
        padding: 15px;
        width: 95%; /* Sections take up more width on smaller screens */
        margin-left: auto;
        margin-right: auto;
    }
    #image-container {
        max-width: 100%; /* Allow image container to fill width */
        min-height: 150px;
        background-color: #1a1a2e; /* Ensure dark background on mobile too */
    }
    #image-preview img {
        max-height: 300px; /* Adjust max height for smaller screens */
    }

    .presets-container {
        flex-direction: column;
        align-items: stretch; /* Buttons stretch to full width */
    }
    .preset-button {
        width: 100%; /* Full width buttons on mobile */
        margin-bottom: 8px;
        border-radius: 8px; /* Less rounded on mobile for better touch area */
        font-size: 1em; /* Adjust font size on mobile */
    }
    #scanButton {
        width: 90%;
        padding: 12px 20px;
        font-size: 1em;
    }
    .action-controls {
        flex-direction: column; /* Stack controls vertically */
        align-items: flex-start; /* Align controls to the left */
        width: 95%;
        gap: 15px; /* Adjust gap */
    }
    .control-group {
        width: 100%; /* Make control groups take full width */
        justify-content: space-between; /* Space out elements */
    }
    .control-group label {
        flex-shrink: 0; /* Prevent label from shrinking */
    }
    .control-group input[type="range"] {
        flex-grow: 1; /* Allow range slider to take available space */
        width: auto; /* Override fixed width */
        margin: 0 5px;
    }
    .action-buttons {
        flex-direction: column; /* Stack buttons vertically */
        align-items: center; /* Center buttons */
        width: 90%; /* Limit button width */
    }
    #activateParticlesButton {
        width: 100%; /* Make buttons full width */
        font-size: 1em;
    }
    header {
        padding: 2em 0; /* Adjust header padding on mobile if needed */
    }
}