@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');

:root {
    --bg-color: #0f172a;
    --text-color: #f8fafc;
    --text-muted: #94a3b8;
    --card-bg: rgba(30, 41, 59, 0.7);
    --card-border: rgba(255, 255, 255, 0.1);
    --primary: #3b82f6;
    --primary-hover: #2563eb;
    --accent: #8b5cf6;
    --success: #10b981;
    --danger: #ef4444;
    --warning: #f59e0b;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    scroll-padding-top: 90px;
}

body {
    font-family: 'Inter', sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    overflow-x: hidden;
}

/* Glassmorphism utility */
.glass {
    background: var(--card-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--card-border);
    border-radius: 12px;
    padding: 20px;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}

/* ============================================
   HEADER
   ============================================ */
header {
    padding: 20px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 15px;
    border-bottom: 1px solid var(--card-border);
    background: rgba(15, 23, 42, 0.8);
    backdrop-filter: blur(10px);
    position: sticky;
    top: 0;
    z-index: 100;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 15px;
}

h1 {
    font-size: 1.5rem;
    font-weight: 700;
    background: linear-gradient(to right, var(--primary), var(--accent));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

/* ============================================
   GAMIFICATION UI
   ============================================ */
.gamification-header {
    display: flex;
    align-items: center;
    gap: 20px;
}

.stat-group {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 5px;
}

.level-badge {
    background: linear-gradient(135deg, var(--accent), var(--primary));
    padding: 5px 15px;
    border-radius: 20px;
    font-weight: 600;
    font-size: 0.9rem;
    box-shadow: 0 0 10px rgba(139, 92, 246, 0.5);
    transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.level-badge-theory {
    background: linear-gradient(135deg, #10b981, #059669);
}

.xp-container {
    width: 120px;
    display: flex;
    flex-direction: column;
    gap: 5px;
}

.xp-text {
    font-size: 0.8rem;
    color: var(--text-muted);
    text-align: right;
}

.xp-bar-bg {
    width: 100%;
    height: 8px;
    background: rgba(255, 255, 255, 0.15);
    border-radius: 4px;
    overflow: hidden;
}

.xp-bar-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--primary), var(--success));
    width: 0%;
    transition: width 0.5s ease-out;
}

.xp-bar-fill-theory {
    background: linear-gradient(90deg, #10b981, #059669);
}

/* ============================================
   MAIN LAYOUT
   ============================================ */
.main-container {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
    padding: 20px 40px;
    flex: 1;
}

.panel {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.panel-flex {
    flex: 1;
    display: flex;
    flex-direction: column;
}

h2 {
    font-size: 1.2rem;
    margin-bottom: 15px;
    display: flex;
    align-items: center;
    gap: 8px;
}

/* ============================================
   BUTTONS & INPUTS
   ============================================ */
button {
    background: var(--primary);
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 8px;
    font-family: inherit;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
}

button:hover {
    background: var(--primary-hover);
    transform: translateY(-1px);
}

button:disabled {
    background: var(--text-muted);
    cursor: not-allowed;
    transform: none;
}

button:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

button.secondary {
    background: transparent;
    border: 1px solid var(--primary);
    color: var(--primary);
}

button.secondary:hover {
    background: rgba(59, 130, 246, 0.1);
}

button.theory-btn {
    background: var(--success);
    border: none;
    color: white;
}

button.theory-btn:hover {
    background: #059669;
    transform: translateY(-1px);
}

.btn-small {
    padding: 6px 12px;
    font-size: 0.8rem;
    min-height: 32px;
    min-width: 32px;
}

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

.btn-danger-outline {
    width: 100%;
    margin-top: 15px;
    background: rgba(239, 68, 68, 0.2);
    border: 1px solid var(--danger);
    color: var(--danger);
}

.btn-danger-outline:hover {
    background: rgba(239, 68, 68, 0.3);
}

.btn-full {
    width: 100%;
}

textarea,
input[type="text"] {
    width: 100%;
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid var(--card-border);
    color: var(--text-color);
    padding: 15px;
    border-radius: 8px;
    font-family: 'Inter', sans-serif;
    font-size: 0.95rem;
    resize: vertical;
    transition: border-color 0.2s;
}

textarea:focus,
input[type="text"]:focus {
    outline: none;
    border-color: var(--primary);
}

textarea:focus-visible,
input[type="text"]:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: -2px;
}

.CodeMirror {
    font-family: 'Consolas', 'Courier New', monospace;
    min-height: 200px;
    height: auto;
    border-radius: 8px;
    border: 1px solid var(--card-border);
    background: rgba(0, 0, 0, 0.3) !important;
    line-height: 1.5;
    padding: 10px 0;
}

/* ============================================
   LAYOUT HELPER CLASSES (migrated from inline)
   ============================================ */
.flex-row {
    display: flex;
    align-items: center;
    gap: 10px;
}

.flex-row-wrap {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin-bottom: 15px;
}

.flex-col {
    display: flex;
    flex-direction: column;
}

.text-muted {
    color: var(--text-muted);
}

.text-small {
    font-size: 0.85rem;
}

.text-success {
    color: var(--success);
}

.mb-10 {
    margin-bottom: 10px;
}

.mb-15 {
    margin-bottom: 15px;
}

.mt-10 {
    margin-top: 10px;
}

.mt-15 {
    margin-top: 15px;
}

/* ============================================
   AI TUTOR
   ============================================ */
.task-box {
    background: rgba(139, 92, 246, 0.1);
    border-left: 4px solid var(--accent);
    padding: 15px;
    border-radius: 0 8px 8px 0;
    margin-bottom: 15px;
    line-height: 1.5;
}

/* Markdown Rendering Styles */
.task-box p,
#ai-feedback p,
#ai-help-response p {
    margin-bottom: 10px;
}

.task-box p:last-child,
#ai-feedback p:last-child,
#ai-help-response p:last-child {
    margin-bottom: 0;
}

.task-box ul,
.task-box ol,
#ai-feedback ul,
#ai-feedback ol,
#ai-help-response ul,
#ai-help-response ol {
    margin-left: 20px;
    margin-bottom: 10px;
}

.task-box li,
#ai-feedback li,
#ai-help-response li {
    margin-bottom: 5px;
}

.task-box code,
#ai-feedback code,
#ai-help-response code {
    background: rgba(0, 0, 0, 0.2);
    padding: 2px 5px;
    border-radius: 4px;
    font-family: 'Consolas', 'Courier New', monospace;
    font-size: 0.9em;
    color: var(--accent);
}

.task-box pre,
#ai-feedback pre,
#ai-help-response pre {
    background: rgba(0, 0, 0, 0.3);
    padding: 10px;
    border-radius: 6px;
    overflow-x: auto;
    margin-bottom: 10px;
    border: none;
    min-height: auto;
}

.task-box pre code,
#ai-feedback pre code,
#ai-help-response pre code {
    background: none;
    padding: 0;
    color: var(--text-color);
    font-size: 0.85rem;
}

.task-box h1,
.task-box h2,
.task-box h3,
.task-box h4 {
    margin-top: 15px;
    margin-bottom: 10px;
    color: var(--text-color);
    font-size: 1.1rem;
}

.ai-status {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    gap: 5px;
}

/* AI Feedback Styling */
#ai-feedback {
    margin-top: 15px;
    font-size: 0.9rem;
    padding: 10px;
    border-radius: 8px;
    display: none;
}

.feedback-correct {
    background-color: rgba(16, 185, 129, 0.2);
    border: 1px solid var(--success);
}

.feedback-incorrect {
    background-color: rgba(239, 68, 68, 0.2);
    border: 1px solid var(--danger);
}

/* ============================================
   DOCUMENT LIMIT WARNING
   ============================================ */
.document-limit-warning {
    color: var(--warning);
    font-size: 0.85rem;
    margin-bottom: 10px;
    font-weight: 600;
    background: rgba(245, 158, 11, 0.1);
    padding: 8px;
    border-radius: 4px;
    border-left: 3px solid var(--warning);
}

/* ============================================
   CHAT UI
   ============================================ */
.ai-chat-history {
    display: flex;
    flex-direction: column;
    gap: 15px;
    max-height: 500px;
    overflow-y: auto;
    padding-right: 10px;
    margin-bottom: 15px;
}

.chat-bubble {
    padding: 12px 15px;
    border-radius: 12px;
    max-width: 90%;
    line-height: 1.5;
    font-size: 0.95rem;
}

.chat-bubble p {
    margin-bottom: 10px;
}

.chat-bubble p:last-child {
    margin-bottom: 0;
}

.chat-bubble.ai-msg {
    background: rgba(139, 92, 246, 0.1);
    border-left: 4px solid var(--accent);
    align-self: flex-start;
    border-bottom-left-radius: 2px;
}

.chat-bubble.user-msg {
    background: rgba(59, 130, 246, 0.1);
    border-right: 4px solid var(--primary);
    align-self: flex-end;
    border-bottom-right-radius: 2px;
}

.chat-bubble pre {
    background: rgba(0, 0, 0, 0.3);
    padding: 10px;
    border-radius: 6px;
    overflow-x: auto;
    margin: 10px 0;
    border: none;
}

.chat-bubble code {
    background: rgba(0, 0, 0, 0.2);
    padding: 2px 5px;
    border-radius: 4px;
    font-family: 'Consolas', 'Courier New', monospace;
    color: var(--accent);
}

/* Feedback bubbles in chat (after answer evaluation) */
.chat-bubble.feedback-bubble-correct {
    border-left: 4px solid var(--success);
    background: rgba(16, 185, 129, 0.1);
}

.chat-bubble.feedback-bubble-incorrect {
    border-left: 4px solid var(--danger);
    background: rgba(239, 68, 68, 0.1);
}

.chat-input-area {
    display: flex;
    gap: 10px;
    background: rgba(0,0,0,0.2);
    padding: 10px;
    border-radius: 8px;
    border-left: 3px solid var(--primary);
}

/* ============================================
   CONSOLE OUTPUT (modernized)
   ============================================ */
#output {
    background: rgba(0, 0, 0, 0.5);
    color: #e2e8f0;
    padding: 15px;
    border-radius: 8px;
    font-family: 'Consolas', 'Courier New', monospace;
    overflow-x: auto;
    overflow-y: auto;
    min-height: 100px;
    max-height: 300px;
    border: 1px solid var(--card-border);
    font-size: 0.9rem;
    line-height: 1.5;
}

/* ============================================
   R PLOT OUTPUT
   ============================================ */
.plot-container {
    margin-top: 15px;
    padding: 10px;
    background: #f8fafc;
    border-radius: 8px;
    border: 1px solid var(--card-border);
}

.plot-container canvas {
    max-width: 100%;
    height: auto;
    display: block;
    margin: 0 auto;
}

/* ============================================
   DOCUMENT LIST
   ============================================ */
.document-list {
    max-height: 200px;
    overflow-y: auto;
    border: 1px solid var(--card-border);
    border-radius: 8px;
    background: rgba(0, 0, 0, 0.1);
    margin-bottom: 10px;
}

.document-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 15px;
    border-bottom: 1px solid var(--card-border);
    transition: background 0.2s;
}

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

.document-item:hover {
    background: rgba(255, 255, 255, 0.05);
}

.document-item-left {
    display: flex;
    align-items: center;
    gap: 10px;
    flex: 1;
    overflow: hidden;
}

.document-name {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 0.9rem;
}

.document-delete-btn {
    background: transparent;
    color: var(--danger);
    border: 1px solid var(--danger);
    padding: 6px 10px;
    border-radius: 4px;
    font-size: 0.8rem;
    cursor: pointer;
    min-height: 32px;
    min-width: 32px;
}

.document-delete-btn:hover {
    background: rgba(239, 68, 68, 0.1);
    transform: none;
}

.doc-checkbox {
    width: 20px;
    height: 20px;
    min-width: 20px;
    cursor: pointer;
}

/* Material Upload Button */
.upload-label {
    cursor: pointer;
    padding: 10px 15px;
    border: 1px solid var(--primary);
    color: var(--primary);
    border-radius: 8px;
    font-weight: 500;
    font-size: 0.9rem;
    display: inline-block;
    transition: background 0.2s;
}

.upload-label:hover {
    background: rgba(59, 130, 246, 0.1);
}

/* ============================================
   FILE UPLOAD (Data)
   ============================================ */
.file-upload-area {
    border: 2px dashed var(--card-border);
    border-radius: 8px;
    padding: 30px;
    text-align: center;
    cursor: pointer;
    transition: all 0.2s;
}

.file-upload-area:hover,
.file-upload-area.dragover {
    border-color: var(--primary);
    background: rgba(59, 130, 246, 0.05);
}

.file-upload-area p {
    color: var(--text-muted);
    margin-top: 10px;
}

/* ============================================
   LOADING SCREEN
   ============================================ */
#loading-screen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(15, 23, 42, 0.9);
    backdrop-filter: blur(5px);
    z-index: 1000;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    transition: opacity 0.5s;
}

.spinner {
    width: 50px;
    height: 50px;
    border: 4px solid rgba(255, 255, 255, 0.1);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 20px;
}

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

#loading-text {
    font-size: 1.2rem;
    font-weight: 500;
}

#loading-subtext {
    color: var(--text-muted);
    margin-top: 10px;
    font-size: 0.9rem;
    max-width: 400px;
    text-align: center;
}

/* ============================================
   ANIMATIONS
   ============================================ */
@keyframes shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-5px); }
    75% { transform: translateX(5px); }
}

.shake {
    animation: shake 0.4s ease-in-out;
}

@keyframes xpGained {
    0% { transform: scale(1); }
    50% { transform: scale(1.1); color: var(--success); }
    100% { transform: scale(1); }
}

.xp-gained {
    animation: xpGained 0.5s ease-out;
}

/* Loading pulse for AI thinking states */
@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}

.loading-pulse {
    animation: pulse 1.5s ease-in-out infinite;
}

/* ============================================
   UTILITIES
   ============================================ */
.hidden {
    display: none !important;
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.empty-state-text {
    padding: 15px 20px;
    color: var(--text-muted);
    font-size: 0.85rem;
    text-align: center;
}

/* ============================================
   MODAL
   ============================================ */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(15, 23, 42, 0.9);
    backdrop-filter: blur(5px);
    z-index: 2000;
    display: flex;
    justify-content: center;
    align-items: center;
}

.modal-content {
    width: 90%;
    max-width: 400px;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
}

.modal-header h2 {
    margin: 0;
}

.modal-scrollable {
    max-width: 600px;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
}

.modal-body {
    display: flex;
    flex-direction: column;
    gap: 10px;
    overflow-y: auto;
    padding-right: 5px;
    flex: 1;
}

/* API Key Instructions Box */
.api-instructions {
    background: rgba(59, 130, 246, 0.1);
    border-left: 3px solid var(--primary);
    padding: 10px;
    margin-bottom: 15px;
    font-size: 0.85rem;
    color: var(--text-muted);
}

.api-instructions ol {
    margin-left: 20px;
    margin-top: 5px;
    line-height: 1.4;
}

.api-instructions a {
    color: var(--primary);
    text-decoration: none;
    font-weight: bold;
}

.api-instructions a:hover {
    text-decoration: underline;
}

/* ============================================
   R ENVIRONMENT
   ============================================ */
.env-list {
    max-height: 250px;
    overflow-y: auto;
    border: 1px solid var(--card-border);
    border-radius: 8px;
    background: rgba(0, 0, 0, 0.1);
}

.env-item {
    display: grid;
    grid-template-columns: 2fr 1.5fr 1fr;
    padding: 10px 15px;
    border-bottom: 1px solid var(--card-border);
    font-size: 0.85rem;
}

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

.env-header {
    font-weight: 600;
    color: var(--primary);
    background: rgba(0, 0, 0, 0.2);
    border-bottom: 2px solid var(--card-border);
}

/* ============================================
   HISTORY MODAL STYLES
   ============================================ */
.history-item {
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid var(--card-border);
    border-radius: 8px;
    padding: 12px;
    font-size: 0.85rem;
    color: var(--text-color);
}

.history-item.correct {
    border-left: 4px solid var(--success);
}

.history-item.incorrect {
    border-left: 4px solid var(--danger);
}

.history-item-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 8px;
    color: var(--text-muted);
    font-size: 0.8rem;
    font-weight: bold;
}

.history-task {
    margin-bottom: 8px;
}

.history-task-text {
    font-size: 0.9em;
}

.history-answer {
    margin-bottom: 5px;
}

.history-code {
    background: rgba(0, 0, 0, 0.3);
    padding: 2px 4px;
    border-radius: 4px;
    color: var(--accent);
}

/* ============================================
   RESPONSIVE DESIGN
   ============================================ */
@media (max-width: 1024px) {
    .main-container {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 768px) {
    header {
        flex-direction: column;
        gap: 15px;
        padding: 15px 20px;
    }

    .header-left {
        flex-wrap: wrap;
        justify-content: center;
    }

    .gamification-header {
        flex-direction: row;
        gap: 15px;
        justify-content: center;
    }

    .main-container {
        padding: 15px;
        gap: 15px;
    }

    .ai-chat-history {
        max-height: 350px;
    }

    .glass {
        padding: 15px;
    }

    .file-upload-area {
        padding: 20px;
    }
}

@media (max-width: 480px) {
    h1 {
        font-size: 1.2rem;
    }

    .level-badge {
        font-size: 0.75rem;
        padding: 4px 10px;
    }

    .xp-container {
        width: 100px;
    }

    .xp-text {
        font-size: 0.7rem;
    }

    header {
        padding: 10px 15px;
    }

    .main-container {
        padding: 10px;
    }

    .chat-bubble {
        max-width: 95%;
        font-size: 0.9rem;
    }

    button {
        padding: 8px 14px;
        font-size: 0.85rem;
    }
}

/* ============================================
   FOOTER
   ============================================ */
.site-footer {
    text-align: center;
    padding: 20px;
    color: var(--text-muted);
    font-size: 0.85rem;
}

.site-footer a {
    color: var(--text-muted);
    text-decoration: none;
    transition: color 0.2s;
}

.site-footer a:hover {
    color: var(--accent);
}

.footer-sep {
    margin: 0 8px;
    opacity: 0.5;
}

/* ============================================
   LEGAL PAGES
   ============================================ */
.legal-page {
    max-width: 800px;
    margin: 0 auto;
    padding: 40px 20px;
}

.legal-page h1 {
    font-size: 1.8rem;
    margin-bottom: 30px;
    color: var(--accent);
}

.legal-page h2 {
    font-size: 1.2rem;
    margin-top: 30px;
    margin-bottom: 10px;
}

.legal-page p,
.legal-page li {
    line-height: 1.7;
    color: var(--text-muted);
    margin-bottom: 10px;
}

.legal-page ul {
    padding-left: 20px;
    margin-bottom: 15px;
}

.legal-page a {
    color: var(--accent);
}

.legal-page .placeholder {
    background: rgba(239, 68, 68, 0.15);
    border: 1px dashed var(--danger);
    padding: 2px 6px;
    border-radius: 4px;
    color: var(--danger);
    font-weight: bold;
}

.legal-back {
    display: inline-block;
    margin-bottom: 20px;
    color: var(--text-muted);
    text-decoration: none;
    font-size: 0.9rem;
}

.legal-back:hover {
    color: var(--accent);
}

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