/* ==========================================
   1. BASE STYLING & 3D CANVAS
   ========================================== */
body { 
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; 
    margin: 0; 
    padding: 0;
    background-color: #1a1a1a; 
    color: #333; 
    overflow: hidden; /* Prevents scrollbars from the 3D canvas */
}

/* Make the Three.js Canvas a fixed background */
canvas {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    z-index: -1; 
}

/* ==========================================
   2. MAIN GAME CONTAINER (The "2.5D" Layer)
   ========================================== */
#game-container { 
    position: absolute;
    top: 0;
    left: 0;
    width: 100vw; 
    height: 100vh;
    display: flex; 
    flex-direction: column; 
    justify-content: space-between; /* Pushes Header UP and Footer DOWN */
    
    /* CRITICAL: Let clicks pass through the empty space to control the 3D camera! */
    pointer-events: none; 
}

/* Re-enable clicks on all the actual UI elements */
#status-bar, #input-area, #inspector-panel, .overlay, #history-panel, #rina-ui-overlay {
    pointer-events: auto;
}

/* --- Status Bar (Top Header) --- */ 
#status-bar { 
    background-color: rgba(62, 39, 35, 0.95); 
    padding: 15px 25px; 
    color: #fff; 
    display: flex; 
    align-items: center; 
    justify-content: space-between; 
    border-bottom: 3px solid #d84315; 
    box-shadow: 0 4px 15px rgba(0,0,0,0.5);
    z-index: 10;
}

.status-left { display: flex; align-items: center; gap: 15px; } 
.status-right { display: flex; align-items: center; gap: 20px; } 
.status-right p { margin: 0; font-style: italic; opacity: 0.9; font-size: 1.1rem; }

#settings-btn { background-color: #5d4037; padding: 8px 15px; font-size: 0.9rem; } 
#settings-btn:hover { background-color: #4e342e; }

/* ==========================================
   3. MIDDLE DISPLAY AREA (Bubbles & Inspector)
   ========================================== */
#main-display { 
    flex-grow: 1; 
    display: flex; 
    justify-content: flex-end; /* Pushes the panel to the right edge */
    overflow: hidden; 
}

/* --- Rina's Floating UI (Speech & TTS) --- */
#rina-ui-overlay {
    position: absolute;
    top: 10%; /* Float it near the upper middle */
    width: 90%;
    max-width: 600px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 15px;
    z-index: 5;
}

.preview-bubble { 
    width: 100%;
    padding: 15px 25px; 
    border-radius: 12px; 
    font-size: 1.1rem; 
    line-height: 1.4; 
    box-shadow: 0 6px 20px rgba(0,0,0,0.3); 
    box-sizing: border-box;
    /* Soft animation when bubbles appear */
    animation: popIn 0.3s ease-out;
}

@keyframes popIn {
    0% { opacity: 0; transform: translateY(10px) scale(0.95); }
    100% { opacity: 1; transform: translateY(0) scale(1); }
}

.speech-bubble { 
    background-color: rgba(255, 255, 255, 0.95); 
    color: #222; 
    border: 2px solid #bdc3c7; 
    font-weight: bold; 
} 

.thought-bubble { 
    background-color: rgba(241, 245, 248, 0.95); 
    color: #555; 
    border: 2px dashed #95a5a6; 
    font-style: italic; 
}

/* NEW: TTS Button */
#play-tts-btn {
    align-self: flex-start;
    background-color: #3498db;
    padding: 8px 20px;
    height: auto;
    font-size: 0.9rem;
    border-radius: 20px;
    box-shadow: 0 4px 10px rgba(52, 152, 219, 0.4);
}
#play-tts-btn:hover { background-color: #2980b9; transform: scale(1.05); }

/* --- Side Inspector Panel (Evaluation) --- */ 
#inspector-panel { 
    position: relative; /* Anchored nicely into the flexbox layout */
    width: 350px; 
    background-color: rgba(252, 249, 242, 0.95); 
    border-left: 3px solid #d84315; 
    padding: 25px; 
    box-sizing: border-box; 
    overflow-y: auto; 
    z-index: 30; 
    box-shadow: -5px 0 25px rgba(0,0,0,0.4);
    display: flex;
    flex-direction: column;
}

#inspector-name { margin-top: 0; color: #3e2723; border-bottom: 2px solid #d84315; padding-bottom: 5px; } 
.inspector-section { margin-top: 20px; display: flex; flex-direction: column; gap: 10px;}
.inspector-section h3 { margin: 0; color: #d84315; font-size: 0.9rem; text-transform: uppercase;} 
.spoken-text { font-weight: bold; font-size: 1.1rem; color: #222; margin: 0;} 
.thought-text { font-style: italic; color: #555; margin: 0;} 

/* ==========================================
   4. INPUT AREA (Bottom Footer)
   ========================================== */
#input-area { 
    padding: 20px 30px; 
    display: flex; 
    align-items: center;
    gap: 15px; 
    background-color: rgba(239, 235, 228, 0.95); /* Slightly transparent */
    border-top: 1px solid #ccc; 
    box-shadow: 0 -5px 15px rgba(0,0,0,0.3);
    z-index: 10;
}

#player-input { 
    width: 100%; 
    height: 50px; 
    padding: 12px; 
    border-radius: 8px; 
    border: 1px solid #bbb; 
    resize: none; 
    font-family: inherit; 
    font-size: 1.1rem; 
    box-sizing: border-box;
}

#player-input:focus { outline: none; border-color: #d84315; }

button { 
    padding: 0 30px; 
    background-color: #d84315; 
    color: white; 
    border: none; 
    border-radius: 8px; 
    font-weight: bold; 
    cursor: pointer; 
    font-size: 1.1rem; 
    transition: background-color 0.2s; 
    height: 50px; /* Match height of other elements */
}

button:hover { background-color: #bf360c; }
button:disabled { background-color: #95a5a6 !important; cursor: not-allowed; }

/* --- Upload Button --- */
#upload-img-btn:hover { background-color: #95a5a6 !important; }

/* ==========================================
   5. OVERLAYS & MODALS
   ========================================== */
.hidden { display: none !important; } 

.overlay { 
    position: fixed; top: 0; left: 0; width: 100%; height: 100%; 
    background-color: rgba(0,0,0,0.85); 
    display: flex; justify-content: center; align-items: center; 
    z-index: 1000; 
} 

.overlay-content { 
    background: #fff; padding: 40px; border-radius: 12px; text-align: left; 
    max-height: 85vh; overflow-y: auto;
}

/* Quick Language Toggle Switch */
.lang-switch-wrapper { display: flex; align-items: center; gap: 8px; color: #333; font-weight: bold; font-size: 0.9rem; background-color: #efebe4; padding: 14px 10px; border-radius: 8px; height: 50px; box-sizing: border-box;} 
.switch { position: relative; display: inline-block; width: 40px; height: 22px; } 
.switch input { opacity: 0; width: 0; height: 0; }
.switch .slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #bdc3c7; transition: .3s; } 
.switch .slider:before { position: absolute; content: ""; height: 16px; width: 16px; left: 3px; bottom: 3px; background-color: white; transition: .3s; box-shadow: 0 2px 4px rgba(0,0,0,0.2); } 
.switch input:checked + .slider { background-color: #d84315; } 
.switch input:checked + .slider:before { transform: translateX(18px); } 
.switch .slider.round { border-radius: 22px; } 
.switch .slider.round:before { border-radius: 50%; }

/* --- Start Menu --- */
.menu-buttons { display: flex; flex-direction: column; gap: 15px; width: 100%; max-width: 300px; margin: 30px auto 10px auto; }
.menu-buttons button { width: 100%; padding: 14px 20px; font-size: 1.1rem; transition: transform 0.2s ease, background-color 0.2s ease; }
.menu-buttons button:hover { transform: translateY(-2px); }

/* --- Settings Modal Tabs --- */
.settings-content { width: 400px; position: relative; color: #333; }
.settings-content h2 { margin-top: 0; border-bottom: 2px solid #eee; padding-bottom: 10px; }
.setting-row { display: flex; justify-content: space-between; align-items: center; margin: 20px 0; font-size: 1.1rem; }
.setting-row select { padding: 5px 10px; font-size: 1rem; border-radius: 5px; }

.tab-nav { display: flex; border-bottom: 2px solid #ddd; margin-bottom: 20px; gap: 10px; }
.tab-btn { background: none; border: none; padding: 10px 15px; font-size: 1rem; color: #666; cursor: pointer; border-bottom: 3px solid transparent; transition: all 0.2s ease; border-radius: 0; height: auto;}
.tab-btn:hover { background-color: #f5f5f5; }
.tab-btn.active { color: #d84315; border-bottom: 3px solid #d84315; font-weight: bold; }
.tab-panel { display: none; }
.tab-panel.active { display: block; animation: fadeIn 0.3s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(5px); } to { opacity: 1; transform: translateY(0); } }

/* ==========================================
   6. HISTORY PANEL
   ========================================== */
#history-panel { 
    position: fixed; top: 0; left: -380px; width: 350px; height: 100%; 
    background-color: #fdfbf7; box-shadow: 5px 0 25px rgba(0,0,0,0.5); 
    transition: left 0.3s ease-in-out; z-index: 2000; box-sizing: border-box; 
    display: flex; flex-direction: column; 
}
#history-panel.open { left: 0; }
#history-panel h2 { color: #3e2723; border-bottom: 2px solid #d84315; padding-bottom: 10px; margin-top: 0; }
.history-header { position: relative; padding: 25px 25px 15px 25px; background-color: #fdfbf7; flex-shrink: 0; z-index: 10; }
#history-list { padding: 10px 25px 25px 25px; overflow-y: auto; flex-grow: 1; }

.history-item { background: #fff; padding: 15px; margin-bottom: 15px; border-radius: 8px; border-left: 5px solid #d84315; box-shadow: 0 3px 8px rgba(0,0,0,0.1); }
.history-turn-number { font-size: 0.8rem; color: #95a5a6; text-transform: uppercase; margin-bottom: 5px; font-weight: bold; }
.history-player-text { font-weight: bold; color: #2c3e50; margin-bottom: 8px; font-size: 1rem; }
.history-summary-text { font-size: 0.9rem; color: #7f8c8d; font-style: italic; background: #f1f5f8; padding: 8px; border-radius: 4px; }
.rewind-btn { width: 100%; background: #bdc3c7; color: #333; padding: 8px; font-size: 0.9rem; margin-top: 10px; border: none; border-radius: 5px; cursor: pointer; transition: background 0.2s; height: auto;}
.rewind-btn:hover { background: #e74c3c; color: white; }
.export-btn { width: 100%; padding: 10px; background-color: #3498db; color: white; border: none; border-radius: 5px; cursor: pointer; font-weight: bold; transition: background 0.2s; height: auto;}
.export-btn:hover { background-color: #2980b9; }

/* ==========================================
   7. MIC & RECORDING ANIMATIONS
   ========================================== */
@keyframes pulse { 
    0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(231, 76, 60, 0.7); background-color: #e74c3c; } 
    70% { transform: scale(1.05); box-shadow: 0 0 0 10px rgba(231, 76, 60, 0); background-color: #c0392b; } 
    100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(231, 76, 60, 0); background-color: #e74c3c; } 
}
.recording-pulse { animation: pulse 1.5s infinite !important; }

@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
.loading-spinner { display: inline-block; animation: spin 1.5s linear infinite; }

@keyframes pulse-orange { 
    0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(211, 84, 0, 0.7); background-color: #d35400; } 
    70% { transform: scale(1.05); box-shadow: 0 0 0 10px rgba(211, 84, 0, 0); background-color: #e67e22; } 
    100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(211, 84, 0, 0); background-color: #d35400; } 
}
.retry-pulse { animation: pulse-orange 2s infinite !important; }

/* Temporary Toast Notification */ 
.toast-message { 
    position: fixed; bottom: 120px; left: 50%; transform: translateX(-50%); 
    background-color: #2c3e50; color: white; padding: 12px 24px; border-radius: 8px; 
    box-shadow: 0 4px 15px rgba(0,0,0,0.4); z-index: 1000; font-weight: bold; pointer-events: none; 
    animation: fadeInOut 4s forwards; 
}
@keyframes fadeInOut { 
    0% { opacity: 0; transform: translate(-50%, 20px); } 10% { opacity: 1; transform: translate(-50%, 0); } 
    85% { opacity: 1; transform: translate(-50%, 0); } 100% { opacity: 0; transform: translate(-50%, -20px); } 
}

/* ==========================================
   8. WORD INTERACTION & FURIGANA
   ========================================== */
.clickable-word {
    cursor: pointer;
    border-bottom: 2px dashed transparent;
    transition: all 0.2s;
    display: inline-block; /* Helps with bounding box for tooltip */
    margin: 0 1px;
}
.clickable-word:hover {
    border-bottom-color: #d84315;
    background-color: rgba(216, 67, 21, 0.1);
    border-radius: 4px;
}

/* Furigana Default (Controlled by class on inspector) */
rt {
    font-size: 0.6em;
    color: #7f8c8d;
    user-select: none;
}
.hide-furigana rt {
    display: none;
}

/* Dictionary Tooltip Popup */
#word-tooltip {
    position: absolute;
    background: #fdfbf7;
    border: 2px solid #d84315;
    border-radius: 8px;
    padding: 15px;
    box-shadow: 0 8px 25px rgba(0,0,0,0.3);
    z-index: 2000;
    width: 250px;
    transition: opacity 0.2s;
}
.tooltip-header { 
    display: flex; 
    justify-content: space-between; 
    align-items: flex-start; 
    border-bottom: 1px solid #ddd; 
    padding-bottom: 5px; 
}
.tooltip-actions { 
    display: flex; 
    gap: 8px; 
    margin-top: 10px; 
}
.tooltip-actions button {
    height: auto;
}

#audio-preview-panel {
    position: fixed;
    bottom: 110px; /* Floats just above the input area */
    left: 20px;    /* Aligns it above the mic controls */
    background: #2c3e50;
    padding: 15px;
    border-radius: 10px;
    box-shadow: 0 4px 15px rgba(0,0,0,0.5);
    z-index: 1000;
    display: flex;
    flex-direction: column;
    border: 1px solid #34495e;
}

.hidden { display: none !important; }

#close-preview {
    align-self: flex-end;
    background: none;
    border: none;
    color: #bdc3c7;
    cursor: pointer;
    font-size: 1.5rem;
    line-height: 1;
    margin-bottom: -10px;
}

#close-preview:hover { color: white; }

#audio-player {
    height: 35px;
    width: 250px;
}
/* Study Desk Overlay */
#study-desk-overlay {
    position: absolute;
    top: 60px; /* Below status bar */
    left: 0;
    width: 100vw;
    height: calc(100vh - 150px); /* Above input area */
    background: rgba(0, 0, 0, 0.6); /* Dims the 3D canvas */
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 20;
    backdrop-filter: blur(4px);
}

.desk-container {
    background: #fdfbf7;
    width: 90%;
    max-width: 600px;
    border-radius: 16px;
    box-shadow: 0 15px 35px rgba(0,0,0,0.5);
    overflow: hidden;
    border: 3px solid #d84315;
    animation: slideUp 0.3s ease-out;
}

.desk-header {
    background: #d84315;
    color: white;
    padding: 15px 25px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.desk-header h3 { margin: 0; }

.quiz-card {
    padding: 30px;
    text-align: center;
}

#quiz-image {
    width: 100%;
    height: 200px;
    object-fit: cover;
    border-radius: 8px;
    margin-bottom: 15px;
}

#quiz-target-word {
    font-size: 3rem;
    color: #2c3e50;
    margin: 10px 0;
}

.options-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 15px;
    margin-top: 20px;
}

.quiz-option-btn {
    background: #ecf0f1;
    color: #2c3e50;
    border: 2px solid #bdc3c7;
    padding: 15px;
    border-radius: 8px;
    font-size: 1.2rem;
    cursor: pointer;
    transition: all 0.2s;
}

.quiz-option-btn:hover {
    background: #3498db;
    color: white;
    border-color: #2980b9;
}

.flex-center { display: flex; justify-content: center; margin-top: 20px;}
