:root {
    --bg-color: #0f1115;
    --panel-bg: rgba(20, 22, 27, 0.9);
    --text-primary: #f8f9fa;
    --text-secondary: #94a3b8;
    --accent-color: #6366f1;
    --accent-hover: #4f46e5;
    --border-color: rgba(255, 255, 255, 0.1);
    --glass-bg: rgba(30, 41, 59, 0.5);
    --glass-border: rgba(255, 255, 255, 0.15);
    --success-color: #10b981;
    --error-color: #ef4444;
}

* { 
    margin: 0; 
    padding: 0; 
    box-sizing: border-box; 
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

body {
    font-family: 'Outfit', sans-serif;
    background-color: var(--bg-color);
    color: var(--text-primary);
    overflow: hidden;
    display: flex;
    height: 100vh;
}

#canvas-container {
    flex-grow: 1;
    position: relative;
    z-index: 1;
    touch-action: none;
    width: 100%;
    height: 100%;
}
#canvas-container canvas {
    width: 100% !important;
    height: 100% !important;
    display: block;
}

#ui-container {
    width: 320px;
    height: 100vh;
    background: var(--panel-bg);
    backdrop-filter: blur(25px);
    -webkit-backdrop-filter: blur(25px);
    border-right: 1px solid var(--border-color);
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 24px;
    z-index: 2000;
    box-shadow: 4px 0 24px rgba(0,0,0,0.5);
    transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    overflow-y: auto;
}

/* Custom Panel Scrollbar */
#ui-container::-webkit-scrollbar {
    width: 5px;
}
#ui-container::-webkit-scrollbar-track {
    background: transparent;
}
#ui-container::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.15);
    border-radius: 3px;
}
#ui-container::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.3);
}

.drawer-handle {
    display: none;
    width: 40px;
    height: 5px;
    background: rgba(255, 255, 255, 0.25);
    border-radius: 3px;
    margin: -8px auto 8px auto;
    flex-shrink: 0;
}

h1 {
    font-size: 1.5rem;
    font-weight: 600;
    background: linear-gradient(135deg, #f8f9fa, #94a3b8);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.control-label {
    font-size: 0.8rem;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 1px;
    font-weight: 600;
}

.button-group {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}

button {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 10px 16px;
    border-radius: 8px;
    font-family: inherit;
    font-size: 0.9rem;
    cursor: pointer;
    transition: all 0.2s ease;
}

button:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(255, 255, 255, 0.3);
}

button:active {
    transform: scale(0.98);
}

button.active {
    background: var(--accent-color);
    border-color: var(--accent-color);
    box-shadow: 0 0 12px rgba(99, 102, 241, 0.4);
}

button:disabled {
    opacity: 0.3;
    cursor: not-allowed;
    transform: none !important;
}

.status-panel {
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    border-radius: 8px;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.status-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.9rem;
}

.status-val {
    font-weight: 600;
    font-size: 1.1rem;
}

.status-message {
    font-size: 0.8rem;
    line-height: 1.4;
    color: var(--text-secondary);
    min-height: 2.8em;
    display: flex;
    align-items: center;
}

.status-message.success {
    color: var(--success-color);
    font-weight: 600;
}

.status-message.error {
    color: var(--error-color);
}

input[type="range"] {
    -webkit-appearance: none;
    appearance: none;
    background: rgba(255,255,255,0.1);
    height: 6px;
    border-radius: 3px;
    outline: none;
    cursor: pointer;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--accent-color);
    box-shadow: 0 0 8px rgba(99, 102, 241, 0.5);
    transition: transform 0.1s ease;
}

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

input[type="range"]:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}



/* Overlay UI */
#overlay-ui {
    position: absolute;
    top: 24px;
    left: 344px; /* Avoid overlapping behind the 320px sidebar panel */
    z-index: 2500;
    display: flex;
    gap: 12px;
    transition: opacity 0.3s ease, visibility 0.3s ease;
}

.portal-link-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--panel-bg);
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: 0 4px 12px rgba(0,0,0,0.25);
    text-decoration: none;
}

.portal-link-btn:hover {
    color: var(--text-primary);
    border-color: rgba(255, 255, 255, 0.3);
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(0,0,0,0.35);
}

/* Responsive */
@media (max-width: 768px) {
    body {
        flex-direction: column;
    }
    #ui-container {
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 60vh; /* Adjust drawer height to fit contents while maintaining 3D area */
        border-right: none;
        border-top: 1px solid var(--border-color);
        /* Expose 110px default so the app title "展開図カッター" is 100% visible */
        transform: translateY(calc(100% - 110px)); 
        border-top-left-radius: 20px;
        border-top-right-radius: 20px;
        padding: 12px 20px 24px 20px;
        gap: 16px;
        box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.6);
    }
    #ui-container h1 {
        font-size: 1.3rem;
        margin-bottom: 4px;
    }
    .control-group {
        gap: 6px;
    }
    .math-card {
        padding: 8px 12px;
        margin-top: 4px;
    }
    .math-formula {
        padding: 4px 6px;
        font-size: 0.72rem;
    }
    .instruction-note {
        font-size: 0.75rem;
        line-height: 1.3;
    }
    #ui-container.open {
        transform: translateY(0);
    }
    .drawer-handle {
        display: block; /* Visible handle for pulling up */
    }
    #canvas-container {
        height: 100svh;
    }
    #overlay-ui {
        top: auto !important;
        bottom: 126px; /* Place floating HUD right above the 110px default drawer title area */
        left: 16px;
        right: auto;
    }
    .hud-status {
        font-size: 0.9rem;
        padding: 5px 10px;
    }
}

/* Math Info Column Card */
.math-card {
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 12px;
}

.math-formula {
    background: rgba(99, 102, 241, 0.1);
    border-left: 3px solid var(--accent-color);
    padding: 6px 8px;
    font-family: 'Courier New', Courier, monospace;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-primary);
    border-radius: 0 4px 4px 0;
}

/* Mini 3D Preview (Always Closed) CSS */
#mini-canvas-container {
    position: absolute;
    top: 24px;
    right: 24px;
    width: 150px;
    height: 150px;
    background: rgba(15, 17, 21, 0.7);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    z-index: 2500;
    overflow: hidden;
    box-shadow: 0 8px 24px rgba(0,0,0,0.45);
    pointer-events: none; /* Let drag events pass through to main canvas */
    transition: opacity 0.3s ease, visibility 0.3s ease;
}

.mini-label {
    position: absolute;
    top: 8px;
    left: 10px;
    font-size: 0.65rem;
    color: var(--text-secondary);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    z-index: 1010;
}

/* Responsive adjustment for mini-canvas on mobile */
@media (max-width: 768px) {
    #mini-canvas-container {
        top: auto;
        bottom: 126px;
        right: 16px;
        width: 100px;
        height: 100px;
        border-radius: 12px;
    }
    .mini-label {
        font-size: 0.5rem;
        top: 4px;
        left: 6px;
    }
}

/* --- HUD & Tooltip Styling --- */

/* Welcome Tooltip (Center floating modal) */
.welcome-tooltip {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    background: rgba(15, 17, 21, 0.85);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    padding: 20px;
    width: calc(100% - 32px);
    max-width: 320px;
    color: var(--text-primary);
    text-align: center;
    font-size: 0.85rem;
    line-height: 1.5;
    z-index: 5000;
    box-shadow: 0 12px 36px rgba(0,0,0,0.6);
    transition: opacity 0.4s ease, transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    pointer-events: auto;
}

.tooltip-close-btn {
    position: absolute;
    top: 8px;
    right: 12px;
    background: transparent;
    border: none;
    color: var(--text-secondary);
    font-size: 1.4rem;
    cursor: pointer;
    line-height: 1;
    padding: 0;
    width: auto;
    height: auto;
    opacity: 0.7;
    transition: opacity 0.2s;
}

.tooltip-close-btn:hover {
    opacity: 1.0;
    background: transparent;
    border-color: transparent;
}

/* Left-Top HUD Status */
.hud-status {
    background: rgba(15, 17, 21, 0.7);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 6px 14px;
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--text-primary);
    box-shadow: 0 4px 16px rgba(0,0,0,0.3);
    display: flex;
    align-items: center;
    justify-content: center;
}

/* HUD Top Control Bar */
#hud-bar-container {
    position: absolute;
    top: 24px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 1000;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    pointer-events: none;
}

#hud-bar {
    background: rgba(20, 22, 27, 0.8);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border: 1px solid var(--border-color);
    border-radius: 30px;
    padding: 4px 10px;
    display: flex;
    align-items: center;
    gap: 2px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.45);
    pointer-events: auto;
}

.hud-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1px;
    padding: 4px 10px;
    background: transparent;
    border: none;
    border-radius: 20px;
    color: var(--text-secondary);
    min-width: 42px;
    height: 42px;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

.hud-btn:hover:not(:disabled) {
    background: rgba(255, 255, 255, 0.08);
    color: var(--text-primary);
    border-color: transparent;
}

.hud-btn:active:not(:disabled) {
    transform: scale(0.93);
}

.hud-btn.active {
    background: rgba(99, 102, 241, 0.15) !important;
    border-color: transparent !important;
    color: #38bdf8 !important;
    box-shadow: none !important;
}

.hud-btn:disabled {
    opacity: 0.2;
    cursor: not-allowed;
}

.hud-svg {
    width: 20px;
    height: 20px;
}

.hud-btn-label {
    font-size: 0.58rem;
    font-weight: 600;
    color: inherit;
    line-height: 1;
}

.hud-divider {
    width: 1px;
    height: 18px;
    background: rgba(255, 255, 255, 0.15);
    margin: 0 4px;
}

/* Sliding slider under HUD bar */
.hud-slider-wrapper {
    background: rgba(20, 22, 27, 0.8);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border: 1px solid var(--border-color);
    border-radius: 14px;
    padding: 8px 14px;
    width: 240px;
    display: flex;
    align-items: center;
    gap: 8px;
    box-shadow: 0 6px 20px rgba(0,0,0,0.3);
    transition: all 0.35s cubic-bezier(0.4, 0, 0.2, 1);
    opacity: 0;
    transform: translateY(-12px);
    pointer-events: none;
}

.hud-slider-wrapper input[type="range"] {
    width: 100%;
    min-width: 0;
    margin: 0;
}

/* Active slider slide down */
.hud-slider-wrapper.active {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
}

/* Mobile responsive adjustments for HUD */
@media (max-width: 768px) {
    .hud-status {
        left: 12px;
        top: 12px;
        font-size: 0.95rem;
        padding: 4px 10px;
    }
    #hud-bar-container {
        top: 12px;
        width: calc(100% - 24px);
    }
    #hud-bar {
        padding: 2px 6px;
        gap: 1px;
        border-radius: 24px;
    }
    .hud-btn {
        min-width: 38px;
        height: 38px;
        padding: 2px 6px;
    }
    .hud-svg {
        width: 18px;
        height: 18px;
    }
    .hud-btn-label {
        font-size: 0.52rem;
    }
    .hud-divider {
        height: 14px;
        margin: 0 2px;
    }
    .hud-slider-wrapper {
        width: 200px;
        padding: 6px 12px;
    }
}

/* Hide floating elements behind/away when mobile drawer (control panel) is open */
@media (max-width: 768px) {
    body.panel-active #overlay-ui,
    body.panel-active #mini-canvas-container {
        opacity: 0 !important;
        pointer-events: none !important;
        visibility: hidden !important;
    }
}
