/* Reset & Base */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg-primary: #1a1a2e;
    --bg-secondary: #16213e;
    --bg-tertiary: #0f0f1a;
    --accent: #e94560;
    --accent-hover: #ff6b6b;
    --text-primary: #eee;
    --text-secondary: #aaa;
    --border: #333;
    --success: #4ade80;
    --warning: #fbbf24;
    --danger: #ef4444;
}

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    overflow: hidden;
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

/* Layout */
body {
    display: flex;
}

body.fullscreen #sidebar {
    display: none;
}

body.fullscreen #viewer-container {
    width: 100%;
}

body.fullscreen .btn-exit-fullscreen {
    display: flex;
}

/* Exit Fullscreen Button */
.btn-exit-fullscreen {
    display: none;
    position: absolute;
    top: 16px;
    right: 16px;
    width: 44px;
    height: 44px;
    border: 1px solid var(--border);
    background: rgba(22, 33, 62, 0.9);
    color: var(--text-secondary);
    border-radius: 8px;
    cursor: pointer;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    z-index: 60;
}

.btn-exit-fullscreen:hover {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}

#sidebar {
    width: 300px;
    min-width: 300px;
    height: 100vh;
    background: var(--bg-secondary);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    border-right: 1px solid var(--border);
    z-index: 10;
}

#viewer-container {
    flex: 1;
    position: relative;
    overflow: hidden;
    touch-action: none;
}

#viewer {
    width: 100%;
    height: 100%;
    display: block;
    touch-action: none;
}

/* Sidebar Header */
.sidebar-header {
    padding: 12px 16px;
    background: var(--bg-tertiary);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.sidebar-header h1 {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--accent);
    letter-spacing: 0.5px;
}

.btn-icon {
    width: 36px;
    height: 36px;
    border: 1px solid var(--border);
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.btn-icon:hover, .btn-icon.active {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}

/* Panels */
.panel {
    padding: 12px 14px;
    border-bottom: 1px solid var(--border);
}

.panel h2 {
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-secondary);
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    gap: 6px;
}

.hint {
    font-size: 0.65rem;
    font-weight: 400;
    text-transform: none;
    letter-spacing: normal;
    opacity: 0.6;
}

.panel-settings {
    margin-top: auto;
}

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

.settings-row label {
    font-size: 0.75rem;
    color: var(--text-secondary);
}

.settings-row input[type="color"] {
    width: 32px;
    height: 28px;
    border: 1px solid var(--border);
    border-radius: 4px;
    cursor: pointer;
    background: transparent;
    padding: 2px;
}

/* Dropzone */
.dropzone {
    border: 2px dashed var(--border);
    border-radius: 8px;
    padding: 16px;
    text-align: center;
    transition: all 0.2s ease;
    cursor: pointer;
}

.dropzone:hover,
.dropzone.dragover {
    border-color: var(--accent);
    background: rgba(233, 69, 96, 0.1);
}

.dropzone-content svg {
    margin-bottom: 8px;
    color: var(--text-secondary);
}

.dropzone-content p {
    margin-bottom: 8px;
    color: var(--text-secondary);
    font-size: 0.8rem;
}

.file-buttons {
    display: flex;
    gap: 8px;
    justify-content: center;
}

.file-buttons .btn {
    flex: 1;
    padding: 8px 12px;
    font-size: 0.75rem;
}

/* Folder Files */
.folder-files {
    margin-top: 10px;
    background: var(--bg-tertiary);
    border-radius: 8px;
    overflow: hidden;
}

.folder-files.hidden {
    display: none;
}

.folder-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 12px;
    background: var(--bg-primary);
    border-bottom: 1px solid var(--border);
}

.folder-header span {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-secondary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.btn-close {
    width: 24px;
    height: 24px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 1.2rem;
    line-height: 1;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.btn-close:hover {
    background: var(--danger);
    color: white;
}

.file-list {
    max-height: 150px;
    overflow-y: auto;
}

.file-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    cursor: pointer;
    transition: background 0.1s;
    border-bottom: 1px solid var(--border);
}

.file-item:last-child {
    border-bottom: none;
}

.file-item:hover {
    background: rgba(233, 69, 96, 0.15);
}

.file-item svg {
    color: var(--accent);
    flex-shrink: 0;
}

.file-item span {
    font-size: 0.75rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Model Dimensions */
.model-dimensions {
    background: var(--bg-tertiary);
    border-radius: 6px;
    padding: 10px 12px;
}

.dim-row {
    display: flex;
    justify-content: space-between;
    padding: 4px 0;
    font-size: 0.8rem;
}

.dim-label {
    color: var(--text-secondary);
}

.dim-value {
    font-weight: 600;
    color: var(--success);
    font-family: 'SF Mono', Monaco, 'Courier New', monospace;
}

/* Buttons */
.btn {
    padding: 10px 14px;
    border: none;
    border-radius: 6px;
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.15s ease;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
}

.btn-primary {
    background: var(--accent);
    color: white;
}

.btn-primary:hover {
    background: var(--accent-hover);
    transform: scale(1.02);
}

.btn-secondary {
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border);
}

.btn-secondary:hover {
    background: var(--border);
}

.btn-small {
    padding: 7px 10px;
    font-size: 0.7rem;
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    border: 1px solid var(--border);
}

.btn-small:hover {
    background: var(--border);
    color: var(--text-primary);
}

.btn-danger {
    background: var(--danger);
    color: white;
    border: none;
}

.btn-danger:hover {
    background: #dc2626;
}

.btn-large {
    padding: 14px 20px;
    font-size: 0.9rem;
}

.btn-measure {
    width: 100%;
}

.btn-full {
    width: 100%;
}

.hidden {
    display: none !important;
}

/* Quick Views */
.quick-views {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
}

.btn-view {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    padding: 10px 6px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.15s;
}

.btn-view:hover, .btn-view:active {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}

.btn-view span {
    font-size: 0.6rem;
    font-weight: 500;
    text-transform: uppercase;
}

.btn-view-iso {
    background: rgba(233, 69, 96, 0.15);
    border-color: var(--accent);
    color: var(--accent);
}

/* Parts Tree */
.parts-tree {
    max-height: 150px;
    overflow-y: auto;
    margin-bottom: 8px;
}

.parts-tree .placeholder {
    font-size: 0.75rem;
    color: var(--text-secondary);
    font-style: italic;
}

.part-item {
    display: flex;
    align-items: center;
    padding: 8px;
    border-radius: 6px;
    cursor: pointer;
    gap: 8px;
    margin-bottom: 2px;
    transition: all 0.1s ease;
    border: 1px solid transparent;
}

.part-item:hover {
    background: var(--bg-tertiary);
}

.part-item.selected {
    background: rgba(233, 69, 96, 0.2);
    border-color: var(--accent);
}

.part-item.hidden-part {
    opacity: 0.35;
}

.part-item .visibility-btn {
    width: 28px;
    height: 28px;
    min-width: 28px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: all 0.1s ease;
}

.part-item .visibility-btn:hover {
    background: var(--bg-primary);
    color: var(--text-primary);
}

.part-item .part-name {
    flex: 1;
    font-size: 0.75rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.part-item .part-color {
    width: 12px;
    height: 12px;
    border-radius: 3px;
    border: 1px solid var(--border);
}

.part-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 8px;
}

/* Measurements */
.measure-controls {
    display: flex;
    gap: 8px;
    margin-bottom: 10px;
}

.measure-hint {
    font-size: 0.8rem;
    color: var(--warning);
    background: rgba(251, 191, 36, 0.15);
    padding: 8px 12px;
    border-radius: 6px;
    margin-bottom: 10px;
    text-align: center;
    font-weight: 500;
}

.measurements-list {
    max-height: 100px;
    overflow-y: auto;
}

.measurement-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px;
    background: var(--bg-tertiary);
    border-radius: 6px;
    margin-bottom: 6px;
    border-left: 3px solid var(--success);
}

.measurement-item .label {
    font-size: 0.7rem;
    color: var(--text-secondary);
}

.measurement-item .value {
    font-weight: 700;
    font-size: 1rem;
    color: var(--success);
    font-family: 'SF Mono', Monaco, 'Courier New', monospace;
}

.measurement-item .delete-btn {
    width: 28px;
    height: 28px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 1.2rem;
    line-height: 1;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.measurement-item .delete-btn:hover {
    color: var(--accent);
    background: rgba(233, 69, 96, 0.1);
}

#btnClearMeasures {
    margin-top: 8px;
}

/* Loading Overlay */
.overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.9);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
}

.overlay.hidden {
    display: none;
}

.loader {
    text-align: center;
}

.spinner {
    width: 50px;
    height: 50px;
    border: 4px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin: 0 auto 16px;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.loader p {
    color: var(--text-secondary);
    font-size: 0.9rem;
}

/* Part Tooltip */
.part-tooltip {
    position: absolute;
    top: 16px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--bg-secondary);
    border: 1px solid var(--accent);
    border-radius: 10px;
    padding: 12px 16px;
    z-index: 50;
    box-shadow: 0 8px 32px rgba(0,0,0,0.5);
    display: flex;
    flex-direction: column;
    gap: 10px;
    min-width: 200px;
}

.part-tooltip.hidden {
    display: none;
}

.part-tooltip > span {
    font-size: 0.9rem;
    font-weight: 600;
    text-align: center;
    max-width: 250px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.tooltip-actions {
    display: flex;
    gap: 6px;
}

.tooltip-btn {
    flex: 1;
    padding: 8px 12px;
    background: var(--danger);
    color: white;
    border: none;
    border-radius: 6px;
    font-size: 0.75rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.15s;
}

.tooltip-btn:hover {
    transform: scale(1.03);
}

.tooltip-btn-secondary {
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    color: var(--text-primary);
}

.tooltip-btn-secondary:hover {
    background: var(--accent);
    border-color: var(--accent);
}

/* Info Bar */
.info-bar {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 10px 16px;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    justify-content: space-between;
    font-size: 0.7rem;
    color: var(--text-secondary);
    font-family: 'SF Mono', Monaco, 'Courier New', monospace;
}

/* Measurement Mode Cursor */
.measure-mode #viewer {
    cursor: crosshair;
}

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

::-webkit-scrollbar-track {
    background: var(--bg-tertiary);
}

::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-secondary);
}

/* Mobile / Tablet Responsive */
@media (max-width: 1024px) {
    body {
        flex-direction: column;
    }

    #sidebar {
        width: 100%;
        min-width: 100%;
        height: auto;
        max-height: 40vh;
        order: 2;
        border-right: none;
        border-top: 1px solid var(--border);
    }

    #viewer-container {
        order: 1;
        height: 60vh;
    }

    .sidebar-header {
        padding: 10px 14px;
    }

    .panel {
        padding: 10px 14px;
    }

    .quick-views {
        grid-template-columns: repeat(4, 1fr);
    }

    .btn-view {
        padding: 12px 8px;
    }

    .btn-view svg {
        width: 20px;
        height: 20px;
    }

    .parts-tree {
        max-height: 80px;
    }

    .measurements-list {
        max-height: 60px;
    }

    .btn-large {
        padding: 16px 20px;
        font-size: 1rem;
    }

    .part-tooltip {
        top: 10px;
        min-width: 240px;
    }

    body.fullscreen #sidebar {
        display: none;
    }

    body.fullscreen #viewer-container {
        height: 100vh;
    }
}

/* Very small screens */
@media (max-width: 480px) {
    .quick-views {
        grid-template-columns: repeat(4, 1fr);
        gap: 4px;
    }

    .btn-view span {
        font-size: 0.55rem;
    }

    .dropzone {
        padding: 12px;
    }

    .dropzone-content svg {
        width: 32px;
        height: 32px;
    }
}

/* Touch feedback */
@media (hover: none) {
    .btn:active {
        transform: scale(0.96);
    }

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

    .part-item:active {
        background: var(--bg-tertiary);
    }
}

/* Landscape tablet optimization */
@media (min-width: 768px) and (max-width: 1024px) and (orientation: landscape) {
    body {
        flex-direction: row;
    }

    #sidebar {
        width: 280px;
        min-width: 280px;
        height: 100vh;
        max-height: 100vh;
        order: 1;
        border-right: 1px solid var(--border);
        border-top: none;
    }

    #viewer-container {
        order: 2;
        height: 100vh;
    }
}
