/* --- 1. THEME & GLOBAL --- */
* { box-sizing: border-box; }
html, body { max-width: 100vw; overflow-x: hidden; }

:root {
    --bg-page: #f8fafc;
    --bg-sidebar: #ffffff;
    --bg-header: #0f172a;
    --bg-input: #f1f5f9;
    --text-main: #1e293b;
    --text-muted: #64748b;
    --border: #e2e8f0;
    --ai-bubble-bg: #1e293b;
    --ai-bubble-text: #f1f5f9;
    --accent: #3b82f6;
}

body.dark {
    --bg-page: #0a1020;
    --bg-sidebar: #0f172a;
    --bg-header: #050a14;
    --bg-input: #1e293b;
    --text-main: #e2e8f0;
    --text-muted: #94a3b8;
    --border: #1e293b;
    --ai-bubble-bg: #e2e8f0; 
    --ai-bubble-text: #0f172a;
}

.bg-page { background-color: var(--bg-page); }
.bg-sidebar { background-color: var(--bg-sidebar); }
.bg-header { background-color: var(--bg-header); }
.bg-input { background-color: var(--bg-input); }
.text-page-text { color: var(--text-main); }
.custom-scrollbar::-webkit-scrollbar { width: 4px; }
.custom-scrollbar::-webkit-scrollbar-thumb { background: #334155; border-radius: 10px; }


/* Force the sidebar to stay inside the window boundaries */
#chat-sidebar {
    max-width: 100vw;
    right: 0;
    position: relative;
}

#chat-sidebar.expanded {
    /* Use 70% instead of calc to avoid scrollbar math errors */
    width: 70% !important; 
    min-width: 320px;
}

/* Ensure the header buttons inside the sidebar don't fly off screen */
#chat-sidebar .p-3.border-b {
    padding-right: 1rem !important;
    display: flex !important;
    justify-content: flex-start !important; /* Keeps toggle and robot on the left */
    gap: 12px;
}

/* --- 2. SIDEBARS --- */
#left-sidebar.collapsed { width: 0 !important; border: none; }
.chapter-item { 
    padding: 8px 12px; border-radius: 6px; font-size: 0.8rem; 
    color: var(--text-main); cursor: pointer !important; user-select: none;
}
.chapter-item:hover { background: var(--bg-input); color: var(--accent); }
.chapter-item.active { background: #e0f2fe; color: #0369a1; font-weight: 800; border-left: 3px solid #0369a1; }

/* --- 3. READER --- */
.en-text { color: var(--text-main); font-family: 'Georgia', serif; font-size: 1.1rem; line-height: 1.6; }
.zh-text { color: var(--text-muted); font-family: sans-serif; font-size: 1rem; line-height: 1.6; }
.sentence-row { 
    display: grid; grid-template-columns: 1fr 1fr; gap: 2.5rem; 
    padding: 1rem; border-radius: 10px; cursor: pointer; position: relative; 
}
.sentence-row.active { background-color: rgba(59, 130, 246, 0.08); box-shadow: inset 4px 0 0 var(--accent); }

.add-chat-btn {
    position: absolute; right: 10px; top: 50%; transform: translateY(-50%);
    background: #10b981; color: white; width: 26px; height: 26px;
    border-radius: 50%; font-size: 12px; display: none; align-items: center; justify-content: center;
    z-index: 10; cursor: pointer !important;
}
.sentence-row:hover .add-chat-btn { display: flex; }

/* --- 4. AI TUTOR (GUTTER & BLEED FIXES) --- */
#chat-sidebar {
    transition: width 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex; flex-direction: column;
}

#chat-controls {
    flex-shrink: 0; background: var(--bg-sidebar); border-top: 1px solid var(--border);
    width: 100%; box-sizing: border-box;
}

/* Fixed Wrapper for control alignment */
.controls-wrapper {
    width: 100%;
    max-width: 280px; 
}

/* EXPANDED DASHBOARD */
#chat-sidebar.expanded { 
    width: calc(100vw - 16.5rem) !important; 
    z-index: 50; 
}
#chat-sidebar.expanded #chat-main-container { 
    display: grid !important; 
    grid-template-columns: 1.1fr 0.9fr !important;
    width: 100% !important;
    height: 100%;
}
#chat-sidebar.expanded #chat-history { border-right: 1px solid var(--border); height: 100%; }
#chat-sidebar.expanded #chat-controls { 
    padding: 1rem; display: flex; flex-direction: column; justify-content: center; align-items: center;
}

#chat-sidebar.expanded .controls-wrapper {
    max-width: 360px; /* Hard-coded width for symmetry */
}

/* --- 5. BUTTONS & UI --- */
.suggest-btn { 
    text-align: center; background: var(--bg-input); border: 1px solid var(--border); border-radius: 6px; 
    color: var(--text-muted); cursor: pointer !important; font-weight: 600; font-size: 10px;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 8px 4px;
}
.suggest-btn:hover { background: var(--accent); color: white; }

.ai-bubble { font-size: 1.1rem; line-height: 1.6; background-color: var(--ai-bubble-bg); color: var(--ai-bubble-text); padding: 1rem; border-radius: 10px; }
.ai-bubble strong { color: #3b82f6; background: rgba(59, 130, 246, 0.1); padding: 0 4px; border-radius: 4px; font-weight: 800; }

#space-tip-toast { transition: opacity 0.5s; pointer-events: none; }
#space-tip-toast.show { opacity: 1; }
button i { pointer-events: none; }