/* ==========================================================================
   EquationEditor Stylesheet
   Themes: Light and Dark mode with neutral palette
   ========================================================================== */

/* --- CSS Variables & Color Themes --- */
:root {
    /* Fonts */
    --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    --font-mono: 'Fira Code', Consolas, Monaco, 'Courier New', monospace;

    /* Light Theme Palette (Neutral & Clean) */
    --bg-app: #f8fafc;
    --bg-card: #ffffff;
    --bg-secondary: #f1f5f9;
    --bg-hover: #e2e8f0;
    --bg-active: #cbd5e1;

    --border-color: #e2e8f0;
    --border-focus: #6366f1;

    --text-primary: #0f172a;
    --text-secondary: #475569;
    --text-muted: #94a3b8;

    --accent-primary: #4f46e5;
    --accent-primary-hover: #4338ca;
    --accent-text: #ffffff;

    --danger-color: #ef4444;
    --danger-hover: #dc2626;

    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);

    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 16px;

    --transition-fast: 0.15s ease;
    --transition-normal: 0.25s ease;
}

[data-theme="dark"] {
    /* Dark Theme Palette */
    --bg-app: #0f172a;
    --bg-card: #1e293b;
    --bg-secondary: #334155;
    --bg-hover: #475569;
    --bg-active: #64748b;

    --border-color: #334155;
    --border-focus: #818cf8;

    --text-primary: #f8fafc;
    --text-secondary: #cbd5e1;
    --text-muted: #64748b;

    --accent-primary: #6366f1;
    --accent-primary-hover: #4f46e5;
    --accent-text: #ffffff;

    --danger-color: #f87171;
    --danger-hover: #ef4444;

    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.4);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.3), 0 2px 4px -1px rgba(0, 0, 0, 0.2);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.4), 0 4px 6px -2px rgba(0, 0, 0, 0.25);
}

/* --- Base & Reset --- */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: var(--font-sans);
    background-color: var(--bg-app);
    color: var(--text-primary);
    min-height: 100vh;
    line-height: 1.5;
    transition: background-color var(--transition-normal), color var(--transition-normal);
}

.app-container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    gap: 1.5rem;
}

/* --- Header --- */
.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: var(--bg-card);
    padding: 1rem 1.5rem;
    border-radius: var(--radius-md);
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-sm);
}

.logo-area {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.logo-icon {
    width: 28px;
    height: 28px;
    color: var(--accent-primary);
}

.logo-area h1 {
    font-size: 1.25rem;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.badge {
    background-color: var(--bg-secondary);
    color: var(--accent-primary);
    font-size: 0.75rem;
    font-weight: 600;
    padding: 0.15rem 0.5rem;
    border-radius: 9999px;
    border: 1px solid var(--border-color);
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

/* --- Buttons --- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.5rem 1rem;
    font-size: 0.875rem;
    font-weight: 500;
    font-family: var(--font-sans);
    border-radius: var(--radius-sm);
    border: 1px solid transparent;
    cursor: pointer;
    transition: all var(--transition-fast);
    user-select: none;
    white-space: nowrap;
}

.btn svg {
    width: 16px;
    height: 16px;
}

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

.btn-primary:hover {
    background-color: var(--accent-primary-hover);
    box-shadow: var(--shadow-sm);
}

.btn-secondary {
    background-color: var(--bg-card);
    color: var(--text-primary);
    border-color: var(--border-color);
}

.btn-secondary:hover {
    background-color: var(--bg-secondary);
    border-color: var(--border-focus);
}

.btn-ghost {
    background-color: transparent;
    color: var(--text-secondary);
}

.btn-ghost:hover {
    background-color: var(--bg-secondary);
    color: var(--text-primary);
}

.btn-icon {
    padding: 0.5rem;
    border-radius: var(--radius-sm);
    background-color: var(--bg-secondary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
}

.btn-icon:hover {
    background-color: var(--bg-hover);
}

.btn-sm {
    padding: 0.25rem 0.5rem;
    font-size: 0.75rem;
}

/* Theme Icons Toggle */
[data-theme="light"] .sun-icon { display: block; }
[data-theme="light"] .moon-icon { display: none; }
[data-theme="dark"] .sun-icon { display: none; }
[data-theme="dark"] .moon-icon { display: block; }

/* --- Main Layout Grid --- */
.main-content {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5rem;
    flex: 1;
}

@media (max-width: 960px) {
    .main-content {
        grid-template-columns: 1fr;
    }
}

/* --- Editor Section --- */
.editor-section {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.toolbar-container {
    background-color: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    box-shadow: var(--shadow-sm);
}

.toolbar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
}

.toolbar-header span {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.preset-dropdown {
    background-color: var(--bg-secondary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    padding: 0.4rem 0.75rem;
    font-size: 0.825rem;
    font-family: var(--font-sans);
    outline: none;
    cursor: pointer;
    transition: border-color var(--transition-fast);
}

.preset-dropdown:focus {
    border-color: var(--border-focus);
}

.symbols-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(48px, 1fr));
    gap: 0.35rem;
    max-height: 200px;
    overflow-y: auto;
    padding-right: 0.25rem;
}

.symbol-btn {
    background-color: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-family: var(--font-mono);
    font-size: 0.85rem;
    padding: 0.45rem 0.2rem;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: all var(--transition-fast);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.symbol-btn sup, .symbol-btn sub {
    font-size: 0.65rem;
    line-height: 0;
}

.symbol-btn:hover {
    background-color: var(--accent-primary);
    color: var(--accent-text);
    border-color: var(--accent-primary);
    transform: translateY(-1px);
}

/* Editor Input Wrapper */
.editor-input-wrapper {
    background-color: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    flex: 1;
    min-height: 280px;
    box-shadow: var(--shadow-sm);
}

.editor-input-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.editor-input-header label {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-secondary);
}

.editor-input-header .hint {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.editor-input-header code {
    font-family: var(--font-mono);
    background-color: var(--bg-secondary);
    padding: 0.1rem 0.3rem;
    border-radius: 4px;
}

#editor-textarea {
    width: 100%;
    flex: 1;
    min-height: 220px;
    background-color: var(--bg-app);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    padding: 0.875rem;
    font-family: var(--font-mono);
    font-size: 0.925rem;
    line-height: 1.6;
    resize: vertical;
    outline: none;
    transition: border-color var(--transition-fast);
}

#editor-textarea:focus {
    border-color: var(--border-focus);
    box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);
}

.editor-actions {
    display: flex;
    gap: 0.75rem;
    justify-content: flex-end;
}

/* --- Preview Section --- */
.preview-section {
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
}

.preview-card {
    background-color: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-sm);
    display: flex;
    flex-direction: column;
    min-height: 280px;
}

.preview-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.875rem 1.25rem;
    border-bottom: 1px solid var(--border-color);
}

.preview-header h2 {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-secondary);
}

.toggle-label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.8rem;
    color: var(--text-secondary);
    cursor: pointer;
    user-select: none;
}

.toggle-label input[type="checkbox"] {
    accent-color: var(--accent-primary);
    cursor: pointer;
}

.preview-body {
    padding: 1.5rem;
    flex: 1;
    overflow-x: auto;
    display: flex;
    flex-direction: column;
    gap: 1rem;
    min-height: 200px;
    font-size: 1.05rem;
    line-height: 1.7;
}

/* KaTeX custom sizing and alignment inside preview */
.preview-body .katex-display {
    margin: 1rem 0;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 0.5rem 0;
}

.preview-body p {
    margin-bottom: 0.75rem;
}

.preview-placeholder {
    color: var(--text-muted);
    font-style: italic;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
}

.export-actions {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
    gap: 0.75rem;
}

/* --- Didactic Tutorial Section --- */
.tutorial-card {
    background-color: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 1.25rem;
    box-shadow: var(--shadow-sm);
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.tutorial-header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.tutorial-icon {
    width: 20px;
    height: 20px;
    color: var(--accent-primary);
}

.tutorial-header h3 {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--text-primary);
}

.tutorial-body {
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
}

.tutorial-guide h4,
.tutorial-examples h4 {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 0.5rem;
}

.tutorial-guide ul {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    font-size: 0.825rem;
    color: var(--text-secondary);
}

.tutorial-guide li code {
    font-family: var(--font-mono);
    background-color: var(--bg-secondary);
    padding: 0.1rem 0.35rem;
    border-radius: 4px;
    color: var(--accent-primary);
}

.example-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 0.75rem;
}

.example-item {
    background-color: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    padding: 0.75rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    transition: border-color var(--transition-fast);
}

.example-item:hover {
    border-color: var(--border-focus);
}

.example-title {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-primary);
}

.example-code {
    font-family: var(--font-mono);
    font-size: 0.75rem;
    color: var(--text-muted);
    background-color: var(--bg-app);
    padding: 0.35rem 0.5rem;
    border-radius: 4px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.btn-insert-example {
    align-self: flex-end;
    font-size: 0.75rem;
    padding: 0.25rem 0.6rem;
}

/* --- History Section --- */
.history-card {
    background-color: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 1rem;
    box-shadow: var(--shadow-sm);
    display: flex;
    flex-direction: column;
    gap: 0.875rem;
}

.history-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.history-header h3 {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.history-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    max-height: 220px;
    overflow-y: auto;
    padding-right: 0.25rem;
}

.empty-history {
    font-size: 0.85rem;
    color: var(--text-muted);
    font-style: italic;
    text-align: center;
    padding: 1rem 0;
}

.history-item {
    background-color: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    padding: 0.6rem 0.75rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.75rem;
    transition: border-color var(--transition-fast);
}

.history-item:hover {
    border-color: var(--border-focus);
}

.history-item-content {
    font-family: var(--font-mono);
    font-size: 0.825rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    cursor: pointer;
    flex: 1;
    color: var(--text-primary);
}

.history-item-actions {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.history-item-btn {
    background: transparent;
    border: none;
    color: var(--text-muted);
    padding: 0.25rem;
    border-radius: 4px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: color var(--transition-fast);
}

.history-item-btn:hover {
    color: var(--danger-color);
    background-color: rgba(239, 68, 68, 0.1);
}

.history-item-btn svg {
    width: 14px;
    height: 14px;
}

/* --- Toast Notifications --- */
.toast-container {
    position: fixed;
    bottom: 1.5rem;
    right: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    z-index: 9999;
    pointer-events: none;
}

.toast {
    background-color: var(--bg-card);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    border-left: 4px solid var(--accent-primary);
    padding: 0.75rem 1rem;
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-lg);
    font-size: 0.875rem;
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    pointer-events: auto;
    animation: toastIn 0.25s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

.toast.toast-error {
    border-left-color: var(--danger-color);
}

.toast.toast-exit {
    animation: toastOut 0.2s cubic-bezier(0.7, 0, 0.84, 0) forwards;
}

@keyframes toastIn {
    from {
        opacity: 0;
        transform: translateY(1rem) scale(0.95);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

@keyframes toastOut {
    from {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
    to {
        opacity: 0;
        transform: translateY(1rem) scale(0.95);
    }
}

/* Custom Scrollbars */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}
::-webkit-scrollbar-track {
    background: transparent;
}
::-webkit-scrollbar-thumb {
    background: var(--bg-hover);
    border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}
