/* 3d_style.css */
:root {
    --primary-color: #3498db;
    --secondary-color: #2c3e50;
    --accent-color: #1abc9c;
    --dark-bg: #1a1a1a;
    --light-bg: #f8f9fa;
    --text-light: #ffffff;
    --text-dark: #333333;
    --shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    --shadow-hover: 0 6px 20px rgba(0, 0, 0, 0.2);
    --border-radius: 12px;
    --border-radius-sm: 8px;
    --transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);

    --safe-area-top: env(safe-area-inset-top, 0px);
    --safe-area-bottom: env(safe-area-inset-bottom, 0px);
    --safe-area-left: env(safe-area-inset-left, 0px);
    --safe-area-right: env(safe-area-inset-right, 0px);
}

* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

body {
    margin: 0;
    padding: 0;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    overflow: hidden;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    height: 100vh;
    height: 100dvh;
    padding: var(--safe-area-top) var(--safe-area-right) var(--safe-area-bottom) var(--safe-area-left);
}

#main-container {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;
    box-shadow: inset 0 0 100px rgba(0, 0, 0, 0.1);
}

/* SIDEBAR MOBILE */
#sidebar {
    width: 100%;
    background: linear-gradient(135deg, rgba(44, 62, 80, 0.85) 0%, rgba(52, 73, 94, 0.8) 100%);
    color: var(--text-light);
    padding: calc(10px + var(--safe-area-top)) 12px 10px;
    box-sizing: border-box;
    overflow-x: auto;
    height: 75px;
    min-height: 75px;
    z-index: 100;
    position: relative;
    box-shadow: var(--shadow);
    border-bottom: 1px solid rgba(255, 255, 255, 0.15);
    backdrop-filter: blur(15px);
    contain: layout style paint; /* Ottimizzazione performance */
}

#model-list {
    display: flex;
    gap: 6px;
    padding: 4px 0;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    -ms-overflow-style: none;
    transform: translateZ(0); /* Force GPU layer */
    will-change: transform;
    contain: content; /* Ottimizzazione performance */
}

#model-list::-webkit-scrollbar {
    display: none;
}

.model-item {
    flex: 0 0 auto;
    padding: 10px 14px;
    background: rgba(255, 255, 255, 0.08);
    border-radius: var(--border-radius-sm);
    cursor: pointer;
    white-space: nowrap;
    font-size: 14px;
    font-weight: 500;
    transition: var(--transition);
    border: 1px solid rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(10px);
    position: relative;
    overflow: hidden;
    min-width: 90px;
    text-align: center;
    user-select: none;
    transform: translateZ(0); /* Force GPU layer */
    will-change: transform, background-color; /* Ottimizzazione performance */
}

.model-item:active {
    transform: scale(0.95);
    transition: transform 0.1s;
}

.model-item.active {
    background: rgba(26, 188, 156, 0.7);
    color: white;
    box-shadow: 0 4px 15px rgba(26, 188, 156, 0.4);
    border: 1px solid rgba(255, 255, 255, 0.3);
}

#viewer-wrapper {
    flex: 1;
    position: relative;
    width: 100%;
    height: calc(100% - 75px);
    background: var(--dark-bg);
}

#viewer {
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, #2c3e50 0%, #3498db 100%);
}

/* INFO BOX MOBILE  */
#model-info {
    position: absolute;
    bottom: 15px;
    left: 12px;
    right: auto;
    background: rgba(0, 0, 0, 0.7);
    color: white;
    padding: 10px 14px;
    border-radius: 10px;
    z-index: 99;
    font-size: 14px;
    backdrop-filter: blur(15px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
    font-weight: 500;
    text-align: left;
    line-height: 1.4;
    max-width: 170px;
    margin: 0;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
    display: flex;
    flex-direction: column;
    gap: 3px;
}

#model-info strong {
    color: var(--accent-color);
    font-weight: 700;
    font-size: 14px;
}

#model-info span {
    font-weight: 400;
    font-size: 12px;
    opacity: 0.9;
    line-height: 1.3;
}

/* CONTROL PANEL MOBILE */
.control-panel {
    position: absolute;
    bottom: 15px;
    top: auto;
    right: 12px;
    left: 12px;
    z-index: 100;
    display: flex;
    flex-direction: column;
    gap: 15px;
    background: rgba(0, 0, 0, 0.75);
    padding: 20px;
    border-radius: var(--border-radius);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: var(--shadow-hover);
    backdrop-filter: blur(20px);
    border: 1px solid rgba(255, 255, 255, 0.08);
    opacity: 0;
    visibility: hidden;
    transform: translateY(calc(100% + 100px));
}

.control-panel:not(.collapsed) {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.control-panel.collapsed {
    opacity: 0;
    visibility: hidden;
    transform: translateY(calc(100% + 100px));
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
    align-items: center;
    width: 100%;
}

.control-group > div {
    color: var(--text-light);
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 5px;
    text-align: center;
    width: 100%;
}

.control-group-row {
    display: flex;
    gap: 10px;
    justify-content: center;
    width: 100%;
}

/* PULSANTI CONTROLLO */
.control-btn {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.08);
    color: var(--text-light);
    border: none;
    font-size: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: var(--transition);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    user-select: none;
    touch-action: manipulation;
    flex-shrink: 0;
}

.control-btn:active {
    transform: scale(0.92);
    background: rgba(255, 255, 255, 0.15);
}

.control-btn.active {
    background: rgba(26, 188, 156, 0.6);
    box-shadow: 0 0 20px rgba(26, 188, 156, 0.5);
}

#bg-color {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    cursor: pointer;
    border: 2px solid rgba(255, 255, 255, 0.2);
    background: transparent;
    overflow: hidden;
    transition: var(--transition);
    touch-action: manipulation;
    flex-shrink: 0;
}

#bg-color:active {
    transform: scale(0.95);
}

/* SLIDER ESPLOSIONE */
.explosion-slider {
    -webkit-appearance: none;
    appearance: none;
    width: 120px;
    height: 6px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 3px;
    outline: none;
    margin: 0 8px;
    flex: 1;
    max-width: 140px;
}

.explosion-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--accent-color);
    cursor: pointer;
    border: 2px solid white;
    transition: var(--transition);
}

.explosion-slider::-webkit-slider-thumb:active {
    transform: scale(1.2);
}

.explosion-slider::-moz-range-thumb {
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--accent-color);
    cursor: pointer;
    border: 2px solid white;
}

#toggle-panel {
    position: fixed;
    right: 15px;
    bottom: 15px;
    bottom: calc(15px + var(--safe-area-bottom));
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: linear-gradient(135deg, rgba(52, 152, 219, 0.8) 0%, rgba(26, 188, 156, 0.8) 100%);
    color: white;
    border: none;
    font-size: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 101;
    box-shadow: var(--shadow-hover);
    touch-action: manipulation;
    transition: var(--transition);
    border: 1px solid rgba(255, 255, 255, 0.2);
    user-select: none;
    backdrop-filter: blur(10px);
}

#toggle-panel:active {
    transform: scale(0.90);
}

.back-btn {
    position: absolute;
    top: 15px;
    top: calc(15px + var(--safe-area-top));
    left: 12px;
    z-index: 102;
    background: rgba(0, 0, 0, 0.6);
    color: white;
    border: none;
    padding: 10px 16px;
    border-radius: 25px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 14px;
    font-weight: 500;
    transition: var(--transition);
    backdrop-filter: blur(15px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    user-select: none;
    touch-action: manipulation;
    min-width: 120px;
    text-align: center;
    justify-content: center;
}

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

/* LOADER */
#loader-container {
    display: none;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(0, 0, 0, 0.85);
    color: white;
    padding: 25px 20px;
    border-radius: var(--border-radius);
    z-index: 1000;
    width: 260px;
    max-width: 85vw;
    backdrop-filter: blur(20px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    box-shadow: var(--shadow-hover);
}

#loader {
    width: 45px;
    height: 45px;
    border: 3px solid rgba(255, 255, 255, 0.2);
    border-top: 3px solid var(--accent-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto 12px;
}

#progress-text {
    font-size: 14px;
    margin-bottom: 10px;
    font-weight: 500;
}

#progress-bar-container {
    width: 100%;
    height: 5px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 3px;
    margin: 12px auto;
    overflow: hidden;
}

#progress-bar {
    width: 0%;
    height: 100%;
    background: linear-gradient(90deg, var(--primary-color), var(--accent-color));
    border-radius: 3px;
    transition: width 0.3s;
}

/* ERROR MESSAGE */
.error-message {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(231, 76, 60, 0.9);
    color: white;
    padding: 20px 16px;
    border-radius: var(--border-radius);
    z-index: 1000;
    display: none;
    text-align: center;
    width: 280px;
    max-width: 85vw;
    backdrop-filter: blur(20px);
    border: 1px solid rgba(255, 255, 255, 0.2);
    box-shadow: var(--shadow-hover);
}

.error-message button {
    background: rgba(255, 255, 255, 0.9);
    color: #c0392b;
    border: none;
    padding: 8px 16px;
    border-radius: var(--border-radius-sm);
    cursor: pointer;
    margin-top: 12px;
    font-weight: 600;
    transition: var(--transition);
    font-size: 14px;
    width: 100%;
}

.error-message button:active {
    transform: scale(0.98);
}

/* TABELLA MISURE - IN ALTO A DESTRA PER MOBILE */
#dimensions-table {
    position: absolute;
    top: 20px;
    left: auto;
    right: 12px;
    background: rgba(255, 255, 255, 0.08);
    color: #000000;
    padding: 12px 18px;
    border-radius: var(--border-radius);
    z-index: 99;
    font-size: 14px;
    backdrop-filter: blur(15px) saturate(180%);
    border: 1px solid rgba(255, 255, 255, 0.15);
    box-shadow:
        0 4px 12px rgba(0, 0, 0, 0.15),
        inset 0 1px 0 rgba(255, 255, 255, 0.1);
    display: none;
    min-width: 200px;
    max-width: 80vw;
    transition: all 0.3s ease;
    opacity: 0;
    transform: translateY(-30px);
}

#dimensions-table.visible {
    display: block;
    opacity: 1;
    transform: translateY(0);
}

#dimensions-table table {
    width: 100%;
    border-collapse: collapse;
}

#dimensions-table th {
    text-align: left;
    padding: 6px 12px 6px 0;
    font-weight: 700;
    color: #000000;
    font-size: 13px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.2);
    text-shadow: 0 1px 2px rgba(255, 255, 255, 0.8);
    letter-spacing: 0.3px;
}

#dimensions-table td {
    padding: 5px 12px 5px 0;
    border-bottom: 1px solid rgba(0, 0, 0, 0.15);
    font-size: 13px;
    font-weight: 600;
    text-shadow: 0 1px 2px rgba(255, 255, 255, 0.8);
    color: #000000;
}

#dimensions-table tr:last-child td {
    border-bottom: none;
}

#dimensions-table .dim-value {
    text-align: right;
    font-weight: 700;
    color: #000000;
    text-shadow: 0 1px 2px rgba(255, 255, 255, 0.8);
}

#dimensions-table .dim-unit {
    font-size: 11px;
    opacity: 0.8;
    margin-left: 3px;
    font-weight: 600;
    text-shadow: 0 1px 1px rgba(255, 255, 255, 0.8);
    color: #333333;
}

/* STILI PER DESKTOP */
@media (min-width: 901px) {
    #main-container {
        flex-direction: row;
    }

    #sidebar {
        width: 280px;
        height: 100%;
        overflow-y: auto;
        padding: 25px 20px;
        height: 100vh;
        background: linear-gradient(135deg, rgba(44, 62, 80, 0.9) 0%, rgba(52, 73, 94, 0.85) 100%);
        backdrop-filter: blur(15px);
    }

    #model-list {
        display: block;
        overflow-x: visible;
        gap: 10px;
    }

    #viewer-wrapper {
        height: 100%;
    }

    .model-item {
        display: block;
        margin-bottom: 10px;
        width: auto;
        white-space: normal;
        padding: 14px 16px;
        min-width: auto;
        text-align: left;
        font-size: 14px;
        background: rgba(255, 255, 255, 0.08);
    }

    .model-item:hover {
        background: rgba(255, 255, 255, 0.12);
        transform: translateY(-2px);
    }

    .model-item.active {
        background: rgba(26, 188, 156, 0.7);
    }

    /* MODEL INFO DESKTOP */
    #model-info {
        left: 20px;
        right: auto;
        bottom: 20px;
        max-width: 300px;
        text-align: left;
        padding: 14px 18px;
        background: rgba(0, 0, 0, 0.7);
        backdrop-filter: blur(20px);
        font-size: 15px;
    }

    #model-info strong {
        font-size: 15px;
    }

    #model-info span {
        font-size: 13px;
    }

    /* CONTROL PANEL DESKTOP */
    .control-panel {
        position: absolute;
        right: 20px;
        top: 20px;
        bottom: auto;
        left: auto;
        flex-direction: column;
        flex-wrap: nowrap;
        width: auto;
        transform: translateX(calc(100% + 30px));
        padding: 15px;
        gap: 12px;
        background: rgba(0, 0, 0, 0.7);
        backdrop-filter: blur(20px);
        border: 1px solid rgba(255, 255, 255, 0.08);
    }

    .control-panel:not(.collapsed) {
        transform: translateX(0);
        opacity: 1;
        visibility: visible;
    }

    .control-panel.collapsed {
        transform: translateX(calc(100% + 30px));
        opacity: 0;
        visibility: hidden;
    }

    .control-group {
        min-width: auto;
        flex: none;
        width: auto;
    }

    .control-btn {
        width: 44px;
        height: 44px;
        font-size: 18px;
        background: rgba(255, 255, 255, 0.08);
    }

    .control-btn:hover {
        background: rgba(255, 255, 255, 0.15);
        transform: scale(1.1);
    }

    .control-btn.active {
        background: rgba(26, 188, 156, 0.6);
    }

    #bg-color {
        width: 44px;
        height: 44px;
    }

    #bg-color:hover {
        transform: scale(1.1);
        border-color: rgba(255, 255, 255, 0.4);
    }

    .explosion-slider {
        width: 120px;
    }

    #toggle-panel {
        position: absolute;
        top: 20px;
        right: 20px;
        bottom: auto;
        width: 50px;
        height: 50px;
        font-size: 20px;
        background: linear-gradient(135deg, rgba(52, 152, 219, 0.8) 0%, rgba(26, 188, 156, 0.8) 100%);
        backdrop-filter: blur(10px);
    }

    #toggle-panel:hover {
        transform: scale(1.1) rotate(90deg);
    }

    /* BACK BUTTON DESKTOP */
    .back-btn {
        left: 20px;
        min-width: auto;
        font-size: 15px;
        background: rgba(0, 0, 0, 0.6);
        backdrop-filter: blur(15px);
    }

    .back-btn:hover {
        transform: translateY(-2px);
        background: rgba(0, 0, 0, 0.7);
    }

    /* TABELLA MISURE DESKTOP - POSIZIONE CENTRALE */
    #dimensions-table {
        top: 25px;
        left: 50%;
        right: auto;
        transform: translateX(-50%) translateY(-30px);
        background: rgba(255, 255, 255, 0.06);
        min-width: 180px;
        padding: 10px 16px;
        backdrop-filter: blur(12px) saturate(180%);
    }

    #dimensions-table.visible {
        transform: translateX(-50%) translateY(0);
    }
}

/* OTTIMIZZAZIONE PER TABLET */
@media (min-width: 601px) and (max-width: 900px) {
    .control-panel {
        left: 50%;
        transform: translateX(-50%) translateY(calc(100% + 100px));
        width: 90%;
        max-width: 500px;
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: space-around;
        gap: 12px;
        background: rgba(0, 0, 0, 0.75);
        backdrop-filter: blur(20px);
    }

    .control-panel:not(.collapsed) {
        transform: translateX(-50%) translateY(0);
        opacity: 1;
        visibility: visible;
    }

    .control-panel.collapsed {
        transform: translateX(-50%) translateY(calc(100% + 100px));
        opacity: 0;
        visibility: hidden;
    }

    .control-group {
        width: auto;
        min-width: 80px;
    }

    .model-item {
        min-width: 100px;
        padding: 12px 18px;
        font-size: 15px;
        background: rgba(255, 255, 255, 0.08);
    }

    #sidebar {
        height: 75px;
        min-height: 75px;
        background: linear-gradient(135deg, rgba(44, 62, 80, 0.85) 0%, rgba(52, 73, 94, 0.8) 100%);
    }

    #viewer-wrapper {
        height: calc(100% - 75px);
    }

    /* MODEL INFO TABLET */
    #model-info {
        bottom: 15px;
        left: 15px;
        max-width: 200px;
        padding: 10px 14px;
        font-size: 14px;
        background: rgba(0, 0, 0, 0.7);
    }

    #model-info strong {
        font-size: 14px;
    }

    #model-info span {
        font-size: 12px;
    }

    /* TABELLA MISURE TABLET - POSIZIONE CENTRALE */
    #dimensions-table {
        top: 15px;
        left: 50%;
        right: auto;
        transform: translateX(-50%) translateY(-30px);
        background: rgba(255, 255, 255, 0.07);
        min-width: 180px;
        backdrop-filter: blur(12px) saturate(180%);
    }

    #dimensions-table.visible {
        transform: translateX(-50%) translateY(0);
    }
}

/* ORIENTAMENTO LANDSCAPE SU MOBILE */
@media (max-height: 500px) and (orientation: landscape) {
    #sidebar {
        height: 55px;
        min-height: 55px;
        padding: 6px 12px;
        background: linear-gradient(135deg, rgba(44, 62, 80, 0.85) 0%, rgba(52, 73, 94, 0.8) 100%);
    }

    #viewer-wrapper {
        height: calc(100% - 55px);
    }

    .model-item {
        padding: 6px 12px;
        min-width: 80px;
        font-size: 12px;
        background: rgba(255, 255, 255, 0.08);
    }

    .control-panel {
        bottom: 10px;
        padding: 15px;
        gap: 10px;
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: space-around;
        background: rgba(0, 0, 0, 0.75);
    }

    .control-group {
        width: auto;
        min-width: 70px;
    }

    .control-btn {
        width: 40px;
        height: 40px;
        font-size: 16px;
        background: rgba(255, 255, 255, 0.08);
    }

    #bg-color {
        width: 40px;
        height: 40px;
    }

    .explosion-slider {
        width: 90px;
        margin: 0 5px;
    }

    /* MODEL INFO LANDSCAPE */
    #model-info {
        bottom: 8px;
        left: 8px;
        max-width: 140px;
        padding: 6px 10px;
        font-size: 12px;
        background: rgba(0, 0, 0, 0.7);
    }

    #model-info strong {
        font-size: 12px;
    }

    #model-info span {
        font-size: 10px;
    }

    .back-btn {
        top: 8px;
        left: 8px;
        padding: 6px 12px;
        font-size: 12px;
        min-width: 100px;
        background: rgba(0, 0, 0, 0.6);
    }

    #toggle-panel {
        width: 45px;
        height: 45px;
        font-size: 20px;
        bottom: 10px;
        background: linear-gradient(135deg, rgba(52, 152, 219, 0.8) 0%, rgba(26, 188, 156, 0.8) 100%);
    }

    /* TABELLA MISURE LANDSCAPE - ALLINEATA A DESTRA */
    #dimensions-table {
        top: 10px;
        left: auto;
        right: 8px;
        transform: translateY(-30px);
        padding: 8px 12px;
        min-width: 160px;
        background: rgba(255, 255, 255, 0.06);
        backdrop-filter: blur(10px) saturate(180%);
    }

    #dimensions-table.visible {
        transform: translateY(0);
    }
}

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

@keyframes fadeIn {
    from { opacity: 0; transform: translate(-50%, -50%) scale(0.9); }
    to { opacity: 1; transform: translate(-50%, -50%) scale(1); }
}

.control-panel, #sidebar, #model-info, .back-btn {
    animation: fadeIn 0.4s ease-out;
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* EFFETTI VISIVI */
.model-item::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.15), transparent);
    transition: left 0.5s;
}

.model-item:active::before {
    left: 100%;
}

/* EFFETTO GLOW PER ELEMENTI ATTIVI */
.model-item.active, .control-btn.active {
    position: relative;
}

.model-item.active::after, .control-btn.active::after {
    content: '';
    position: absolute;
    top: -2px;
    left: -2px;
    right: -2px;
    bottom: -2px;
    border-radius: inherit;
    background: linear-gradient(45deg, var(--accent-color), var(--primary-color), var(--accent-color));
    z-index: -1;
    animation: glow 2s linear infinite;
    background-size: 200% 200%;
    opacity: 0.7;
}

@keyframes glow {
    0% { background-position: 0% 50%; }
    50% { background-position: 100% 50%; }
    100% { background-position: 0% 50%; }
}
