/* Global Styles */
:root {
    --bg-gradient: linear-gradient(135deg, #e0f7fa 0%, #fff9c4 100%);
    --bg-color: #f0f8ff;
    --sidebar-bg: #ffffff;
    --chat-bg: #ffffff;

    /* Playful Palette */
    --primary-color: #ff9f43;
    /* Bright Orange */
    --primary-hover: #ff7f50;
    --secondary-color: #54a0ff;
    /* Sky Blue */
    --accent-color: #ff6b6b;
    /* Soft Red/Pink */
    --success-color: #1dd1a1;
    /* Mint Green */

    --text-color: #2f3542;
    --text-secondary: #57606f;
    --border-color: #f1f2f6;

    --font-heading: 'Fredoka', cursive;
    --font-body: 'Nunito', sans-serif;

    --shadow-soft: 0 8px 24px rgba(149, 157, 165, 0.2);
    --shadow-pop: 0 4px 0px rgba(0, 0, 0, 0.1);
    /* 3D effect shadow */
    --radius-lg: 24px;
    --radius-md: 16px;
    --radius-sm: 12px;
}

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

body {
    font-family: var(--font-body);
    background: var(--bg-gradient);
    color: var(--text-color);
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    /* Prevent body scroll */
}

h1,
h2,
h3,
h4,
h5,
h6 {
    font-family: var(--font-heading);
    letter-spacing: 0.5px;
    color: #333;
}

/* App Container */
.app-container {
    display: flex;
    width: 95vw;
    height: 92vh;
    background-color: rgba(255, 255, 255, 0.85);
    backdrop-filter: blur(12px);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-soft);
    border: 4px solid #fff;
    /* White rim */
    overflow: hidden;
}

/* Sidebar */
.sidebar {
    width: 280px;
    background-color: #fff;
    border-right: 2px dashed #ffeaa7;
    /* Fun dashed border */
    display: flex;
    flex-direction: column;
    padding: 24px;
}

.sidebar.collapsed {
    width: 0;
    transform: translateX(-100%);
    padding: 24px 0;
    overflow: hidden;
    border-right: none;
}

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

.sidebar-header h2 {
    font-size: 1.4rem;
    color: var(--secondary-color);
}

/* Toggle Button */
.toggle-sidebar-btn {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: var(--text-secondary);
    padding: 8px;
    transition: all 0.2s;
}

.toggle-sidebar-btn:hover {
    color: var(--secondary-color);
}

/* Icon Buttons */
.icon-btn {
    background: #f1f2f6;
    border: none;
    font-size: 1.2rem;
    cursor: pointer;
    color: var(--text-secondary);
    transition: all 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    /* Bouncy */
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.icon-btn:hover {
    transform: scale(1.15) rotate(10deg);
    background: #ffeaa7;
    color: var(--primary-color);
}

/* History List */
.history-list {
    flex-grow: 1;
    overflow-y: auto;
    padding: 12px;
}

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

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: #94a3b8;
}

/* History Items */
.history-item {
    padding: 14px 16px;
    margin-bottom: 8px;
    background-color: var(--bg-color);
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: all 0.2s;
    border: 1px solid transparent;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    min-height: 60px;
}

.history-item:hover {
    background-color: #f1f5f9;
    border-color: var(--border-color);
}

.history-item.active {
    background-color: #eff6ff;
    border-color: #bfdbfe;
}

.history-item .history-content {
    display: flex;
    align-items: center;
    flex: 1;
    min-width: 0;
}

.history-item .history-icon {
    font-size: 1.25rem;
    margin-right: 12px;
    flex-shrink: 0;
}

.history-item .history-thumb {
    width: 48px;
    height: 48px;
    border-radius: var(--radius-sm);
    object-fit: cover;
    margin-right: 12px;
    flex-shrink: 0;
    border: 1px solid var(--border-color);
}

.history-item .history-text {
    flex: 1;
    min-width: 0;
}

.history-item .history-title {
    font-size: 0.95rem;
    font-weight: 500;
    color: var(--text-color);
    margin-bottom: 4px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.history-item .history-time {
    font-size: 0.8rem;
    color: var(--text-secondary);
}

.history-item .katex,
.history-item .katex-display,
.history-item .katex-html,
.history-item .base {
    background-color: transparent !important;
    padding: 0 !important;
    margin: 0 !important;
}

.history-item:has(.history-thumb) .history-text {
    display: none;
}

.history-item:has(.history-thumb) .history-content {
    justify-content: center;
}

.delete-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 1.1rem;
    width: 28px;
    height: 28px;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: all 0.2s;
    flex-shrink: 0;
}

.history-item:hover .delete-btn {
    opacity: 1;
}

.delete-btn:hover {
    background: #fee2e2;
    color: #dc2626;
}

/* Empty History State */
.empty-history {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px 20px;
    text-align: center;
    color: var(--text-secondary);
}

.empty-history .empty-icon {
    font-size: 3rem;
    margin-bottom: 16px;
}

.empty-history h3 {
    font-size: 1.1rem;
    margin-bottom: 8px;
    color: var(--text-color);
    font-weight: 600;
}

.empty-history p {
    font-size: 0.95rem;
    margin-bottom: 20px;
}

.empty-history .start-btn {
    background: var(--secondary-color);
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: var(--radius-md);
    font-size: 0.95rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
}

.empty-history .start-btn:hover {
    background: var(--secondary-hover);
}

/* Loading Animation */
.history-loading {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 40px 20px;
}

.loading-dots {
    display: flex;
    gap: 6px;
}

.loading-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background-color: var(--secondary-color);
    animation: loading 1.5s ease-in-out infinite;
}

.loading-dot:nth-child(2) {
    animation-delay: 0.2s;
}

.loading-dot:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes loading {
    0%, 100% {
        transform: scale(0.8);
        opacity: 0.5;
    }
    50% {
        transform: scale(1);
        opacity: 1;
    }
}

/* Chat Area */
.chat-container {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    background-color: #f7f9fc;
    /* Very light gray/blue */
    background-image: radial-gradient(#e0e7ff 1px, transparent 1px);
    /* Polka dots */
    background-size: 20px 20px;
}

.chat-header {
    padding: 20px 30px;
    background-color: rgba(255, 255, 255, 0.9);
    backdrop-filter: blur(5px);
    border-bottom: 2px dashed #dfe4ea;
    display: flex;
    justify-content: space-between;
    align-items: center;
    z-index: 10;
}

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

.header-left h1 {
    font-size: 1.6rem;
    color: var(--secondary-color);
    text-shadow: 1px 1px 0px #c2e2fe;
    /* Cute shadow */
}

.header-right {
    display: flex;
    align-items: center;
    gap: 16px;
}

.user-profile {
    display: flex;
    align-items: center;
    gap: 10px;
}

.user-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--secondary-color), #8b5cf6);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 0.95rem;
}

.header-right .username {
    color: var(--text-color);
    font-size: 0.95rem;
    font-weight: 500;
}

.header-right .logout-btn {
    background: none;
    border: 1px solid transparent;
    width: 40px;
    height: 40px;
    border-radius: var(--radius-md);
    cursor: pointer;
    color: var(--text-secondary);
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.header-right .logout-btn:hover {
    background: #fef2f2;
    color: #dc2626;
    border-color: #fecaca;
}

.chat-window {
    flex-grow: 1;
    padding: 24px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

/* Messages */
.message {
    max-width: 75%;
    padding: 16px 20px;
    border-radius: var(--radius-lg);
    line-height: 1.6;
    font-size: 1.05rem;
    position: relative;
    word-wrap: break-word;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
    animation: slideIn 0.2s ease-out;
}

@keyframes slideIn {
    from {
        transform: translateY(8px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.message.user {
    align-self: flex-end;
    background: #e8f5e9; /* Light green background */
    color: #000; /* Black text */
    border-bottom-right-radius: 4px;
    /* Callout shape */
}

.message.assistant {
    align-self: flex-start;
    background-color: #fff;
    color: #2f3542;
    border: 2px solid #dfe6e9;
    border-bottom-left-radius: 4px;
    /* Callout shape */
}

.message img {
    max-width: 100%;
    border-radius: var(--radius-sm);
    margin-top: 10px;
    border: 1px solid var(--border-color);
}

/* Input Area */
.input-area {
    padding: 16px 24px;
    background-color: var(--sidebar-bg);
    border-top: 1px solid var(--border-color);
    position: relative; /* Anchor for the handwriting panel */
}

.input-wrapper {
    display: flex;
    align-items: flex-end;
    gap: 12px;
    background-color: var(--bg-color);
    padding: 12px 16px;
    border-radius: var(--radius-lg);
    border: 1px solid var(--border-color);
    transition: border 0.2s;
}

.input-wrapper:focus-within {
    border-color: var(--secondary-color);
    background-color: #fff;
}

.upload-btn {
    cursor: pointer;
    font-size: 1.3rem;
    transition: transform 0.2s;
    background: none;
    border: none;
    padding: 4px;
    color: var(--text-secondary);
}

.upload-btn:hover {
    transform: scale(1.1);
    color: var(--secondary-color);
}

.rich-input {
    flex-grow: 1;
    border: none;
    background: transparent;
    outline: none;
    resize: none;
    font-family: var(--font-body);
    font-size: 1rem;
    min-height: 24px;
    max-height: 120px;
    overflow-y: auto;
    color: var(--text-color);
    line-height: 1.5;
    white-space: pre-wrap;
    word-break: break-all;
}

.rich-input[placeholder]:empty:before {
    content: attr(placeholder);
    color: #a0aec0;
    pointer-events: none;
    display: block; /* For Firefox */
}

/* Make math in input inline and legible */
.rich-input .katex-display {
    display: inline-block !important;
    margin: 0 4px !important;
    padding: 2px 6px !important;
}

/* Image Preview */
.image-preview {
    display: flex;
    align-items: center;
    background: #e2e8f0;
    padding: 6px 10px;
    border-radius: var(--radius-sm);
    position: relative;
}

.image-preview.hidden {
    display: none;
}

.preview-thumb {
    width: 36px;
    height: 36px;
    object-fit: cover;
    border-radius: var(--radius-sm);
}

.rm-preview {
    position: absolute;
    top: -6px;
    right: -6px;
    background: #ef4444;
    color: white;
    border: none;
    border-radius: 50%;
    width: 18px;
    height: 18px;
    font-size: 11px;
    cursor: pointer;
    line-height: 1;
}

#send-btn {
    background-color: var(--secondary-color);
    color: #fff;
    border: none;
    padding: 10px 20px;
    border-radius: var(--radius-md);
    cursor: pointer;
    font-weight: 500;
    font-size: 0.95rem;
    transition: all 0.2s;
}

#send-btn:hover {
    background-color: var(--secondary-hover);
}

#send-btn:active {
    transform: scale(0.98);
}

/* Welcome Message */
.welcome-message {
    text-align: center;
    margin-top: 60px;
    padding: 40px;
}

.welcome-message h3 {
    font-size: 1.5rem;
    color: var(--text-color);
    margin-bottom: 12px;
    font-weight: 600;
}

.welcome-message p {
    color: var(--text-secondary);
    font-size: 1rem;
}

/* Summary Card */
.summary-card {
    background: #fff;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 20px;
    margin: 20px 0;
    box-shadow: var(--shadow-soft);
    animation: bounceIn 0.4s ease;
}

@keyframes bounceIn {
    0% {
        transform: scale(0.95);
        opacity: 0;
    }
    100% {
        transform: scale(1);
        opacity: 1;
    }
}

.summary-card h3 {
    color: var(--text-color);
    margin-bottom: 16px;
    text-align: center;
    font-size: 1.25rem;
    font-weight: 600;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--border-color);
}

.summary-card h4 {
    color: var(--text-color);
    margin-top: 16px;
    margin-bottom: 8px;
    font-size: 1rem;
    font-weight: 600;
}

.summary-card ul {
    background: var(--bg-color);
    padding: 12px 12px 12px 28px;
    border-radius: var(--radius-md);
}

.summary-card li {
    margin-bottom: 6px;
    color: var(--text-color);
}

.markdown-content {
    color: var(--text-color);
    line-height: 1.7;
}

/* Practice Problems */
.practice-problem {
    margin-bottom: 12px;
    background: var(--bg-color);
    padding: 12px;
    border-radius: var(--radius-md);
}

.details-content {
    margin-top: 10px;
    padding: 12px;
    background-color: #f0fdf4;
    border-radius: var(--radius-sm);
    border-left: 3px solid var(--accent-color);
}

/* LaTeX Styling */
.katex {
    font-size: 1em;
    background-color: #fffac8; /* Light yellow background */
    padding: 2px 6px;
    border-radius: 4px;
}

.katex-display {
    margin: 12px 0;
    overflow-x: auto;
    background-color: #fffac8; /* Light yellow background */
    padding: 15px;
    border-radius: 8px;
}

.katex-display .katex {
    background-color: transparent;
    padding: 0;
}

/* User Math Styling */
.message.user .katex {
    background-color: rgba(0, 0, 0, 0.05); /* Slight dark translucent for light green bg */
    color: #000;
}

.message.user .katex-display {
    background-color: rgba(0, 0, 0, 0.05);
    color: #000;
    padding: 10px;
    border-radius: 8px;
}

.message.user .katex-display .katex {
    background-color: transparent;
}

/* Bold Text Styling */
strong,
b {
    background-color: #fffbeb;
    padding: 0 4px;
    border-radius: 4px;
}

/* Sidebar Overlay */
.sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.3);
    z-index: 50;
}

.sidebar-overlay.active {
    display: block;
}

/* Summary Modal */
.modal {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: auto;
    background-color: rgba(0, 0, 0, 0.5);
}

.modal.hidden {
    display: none;
}

.modal:not(.hidden) {
    display: flex;
    justify-content: center;
    align-items: center;
}

.modal-content {
    background-color: #fefefe;
    padding: 30px;
    border-radius: var(--radius-lg);
    width: 80%;
    max-width: 600px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
    position: relative;
    animation: slideUp 0.3s ease-out;
}

.close-modal {
    color: var(--text-secondary);
    float: right;
    font-size: 28px;
    font-weight: 400;
    cursor: pointer;
    line-height: 1;
}

.close-modal:hover,
.close-modal:focus {
    color: var(--text-color);
    text-decoration: none;
    cursor: pointer;
}

@keyframes slideUp {
    from {
        transform: translateY(20px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

/* Parsed Content Card */
.parsed-content-card {
    background: #fff;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 16px;
    margin: 12px 0;
    box-shadow: var(--shadow-soft);
    align-self: flex-start;
    width: 90%;
    max-width: 600px;
    animation: fadeIn 0.3s ease;
}

.parsed-content-card h4 {
    color: var(--text-color);
    margin-bottom: 8px;
    font-size: 1rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 8px;
}

.chat-image {
    cursor: zoom-in;
    transition: transform 0.2s;
    border-radius: var(--radius-md);
    margin-top: 10px;
    border: 1px solid var(--border-color);
    max-width: 250px;
    max-height: 250px;
    object-fit: cover;
}

.chat-image:hover {
    transform: scale(1.02);
}

.status-indicator-msg {
    color: var(--text-secondary);
    font-style: italic;
    padding: 10px 0;
    font-size: 0.95rem;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

/* Image Modal */
.image-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    z-index: 2000;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: zoom-out;
    animation: fadeIn 0.2s;
}

.modal-image {
    max-width: 90%;
    max-height: 90%;
    border-radius: var(--radius-md);
    animation: zoomIn 0.3s ease;
}

@keyframes zoomIn {
    from {
        transform: scale(0.9);
        opacity: 0;
    }
    to {
        transform: scale(1);
        opacity: 1;
    }
}

/* Handwriting Panel */
.handwriting-panel {
    background: #fff;
    border-top: 1px solid var(--border-color);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    box-shadow: 0 -4px 15px rgba(0, 0, 0, 0.1);
    display: flex;
    flex-direction: column;
    height: 350px;
    transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    overflow: hidden;
    position: absolute;
    bottom: 100%; /* Anchor it exactly above the input area */
    left: 0;
    width: 100%;
    z-index: 50;
    transform-origin: bottom center;
}

.handwriting-panel.hidden {
    height: 0;
    border-width: 0;
    opacity: 0;
    pointer-events: none;
    transform: scaleY(0);
}

.panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 16px;
    background: #f1f5f9;
    border-bottom: 1px solid var(--border-color);
    font-weight: 600;
    color: var(--text-color);
}

.close-panel-btn {
    width: 28px;
    height: 28px;
    font-size: 1.2rem;
}

#myscript-editor {
    flex: 1;
    width: 100%;
    /* MyScript requires relative positioning to attach UI overlay properly */
    position: relative; 
    background-image: radial-gradient(#e2e8f0 1px, transparent 1px);
    background-size: 20px 20px;
}

.panel-footer {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    padding: 10px 16px;
    background: #f8fafc;
    border-top: 1px solid var(--border-color);
}

.secondary-btn {
    background: #f1f5f9;
    border: 1px solid var(--border-color);
    padding: 6px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 0.9rem;
    color: var(--text-secondary);
    transition: all 0.2s;
}

.secondary-btn:hover:not(:disabled) {
    background: #e2e8f0;
    color: var(--text-color);
}

.secondary-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.primary-btn {
    background: var(--secondary-color);
    color: white;
    border: none;
    padding: 6px 16px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 500;
    transition: all 0.2s;
}

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

.icon-toggle-btn {
    background: none;
    border: none;
    font-size: 1.3rem;
    cursor: pointer;
    padding: 4px;
    color: var(--text-secondary);
    transition: transform 0.2s;
}

.icon-toggle-btn:hover {
    transform: scale(1.1);
}

.icon-toggle-btn.active {
    color: var(--secondary-color);
    background: #eff6ff;
    border-radius: var(--radius-sm);
}

/* Mobile Styles */
@media (max-width: 768px) {
    .app-container {
        flex-direction: column;
    }

    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        width: 280px;
        height: 100%;
        z-index: 200;
        box-shadow: 4px 0 20px rgba(0, 0, 0, 0.15);
    }

    .sidebar.collapsed {
        transform: translateX(-100%);
    }

    .toggle-sidebar-btn {
        display: block;
    }

    .chat-header {
        padding: 12px 16px;
    }

    .header-right {
        gap: 12px;
    }

    .user-profile .username {
        display: none;
    }

    .user-avatar {
        width: 32px;
        height: 32px;
        font-size: 0.85rem;
    }

    .header-right .logout-btn {
        width: 36px;
        height: 36px;
    }

    .chat-window {
        padding: 16px;
    }

    .input-area {
        padding: 12px 16px;
    }

    .message {
        max-width: 85%;
    }

    .sidebar-overlay {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 150;
    }

    .sidebar-overlay.active {
        display: block;
    }
}
