:root {
    /* Color Palette */
    --bg-color: #0f172a;
    /* Slate 900 */
    --surface-color: #1e293b;
    /* Slate 800 */
    --surface-hover: #334155;
    /* Slate 700 */
    --primary-color: #8b5cf6;
    /* Violet 500 */
    --primary-hover: #7c3aed;
    /* Violet 600 */
    --secondary-color: #06b6d4;
    /* Cyan 500 */
    --text-main: #f8fafc;
    /* Slate 50 */
    --text-muted: #cbd5e1;
    /* Slate 300 */
    --danger-color: #ef4444;
    /* Red 500 */

    /* Gradients */
    --gradient-primary: linear-gradient(135deg, var(--primary-color), var(--secondary-color));

    /* Typography */
    --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    --font-display: 'Outfit', var(--font-sans);

    /* Utilities */
    --border-radius-sm: 8px;
    --border-radius-md: 12px;
    --border-radius-lg: 20px;
    --transition-speed: 0.3s;
    --card-height: 250px;
}

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

body {
    font-family: var(--font-sans);
    background-color: var(--bg-color);
    color: var(--text-main);
    line-height: 1.6;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    overflow-x: hidden;
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
}

/* Layout */
.app-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 2rem 1.5rem;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

/* Header */
header {
    text-align: center;
    margin-bottom: 3rem;
    animation: fadeInDown 0.8s ease-out;
}

header h1 {
    font-family: var(--font-display);
    font-size: clamp(2.5rem, 5vw, 4rem);
    font-weight: 700;
    margin-bottom: 0.5rem;
    background: var(--gradient-primary);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

header p {
    color: var(--text-muted);
    font-size: 1.125rem;
    max-width: 600px;
    margin: 0 auto;
}

/* Main Input Section */
.input-section {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    max-width: 800px;
    margin: 0 auto 4rem;
    animation: fadeIn 1s ease-out;
}

.textarea-wrapper {
    position: relative;
    width: 100%;
    margin-bottom: 1.5rem;
}

textarea {
    width: 100%;
    min-height: 200px;
    padding: 1.5rem;
    font-family: var(--font-sans);
    font-size: 1rem;
    color: var(--text-main);
    background-color: rgba(30, 41, 59, 0.7);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: var(--border-radius-md);
    resize: vertical;
    backdrop-filter: blur(10px);
    transition: all var(--transition-speed) ease;
    z-index: 2;
    position: relative;
}

textarea:focus {
    outline: none;
    border-color: var(--primary-color);
    background-color: rgba(30, 41, 59, 0.9);
    box-shadow: 0 0 0 2px rgba(139, 92, 246, 0.2);
}

.textarea-glow {
    position: absolute;
    top: -2px;
    left: -2px;
    right: -2px;
    bottom: -2px;
    background: var(--gradient-primary);
    border-radius: calc(var(--border-radius-md) + 2px);
    z-index: 1;
    opacity: 0;
    transition: opacity var(--transition-speed) ease;
    filter: blur(8px);
}

textarea:focus+.textarea-glow {
    opacity: 0.3;
}

.pii-warning {
    font-size: 0.75rem;
    color: var(--text-muted);
    text-align: center;
    margin-top: 0.5rem;
    margin-bottom: 2rem;
    opacity: 0.8;
}

/* Button */
.primary-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    background: var(--gradient-primary);
    color: white;
    font-family: var(--font-display);
    font-size: 1.125rem;
    font-weight: 600;
    padding: 1rem 2.5rem;
    border: none;
    border-radius: 50px;
    cursor: pointer;
    transition: all calc(var(--transition-speed) * 0.5) ease;
    box-shadow: 0 10px 20px -10px rgba(139, 92, 246, 0.5);
    position: relative;
    overflow: hidden;
}

.primary-btn::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
    transition: left 0.5s ease;
}

.primary-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 15px 25px -10px rgba(139, 92, 246, 0.7);
}

.primary-btn:hover::before {
    left: 100%;
}

.primary-btn:active {
    transform: translateY(1px);
}

.primary-btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none;
}

/* Loading Indicator */
.loading-indicator {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-top: 2rem;
    gap: 1rem;
    animation: fadeIn 0.3s ease-in;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 3px solid rgba(255, 255, 255, 0.1);
    border-radius: 50%;
    border-top-color: var(--secondary-color);
    animation: spin 1s ease-in-out infinite;
}

.hidden {
    display: none !important;
}

.error-message {
    margin-top: 1.5rem;
    color: var(--danger-color);
    background-color: rgba(239, 68, 68, 0.1);
    border: 1px solid rgba(239, 68, 68, 0.3);
    padding: 1rem;
    border-radius: var(--border-radius-sm);
    text-align: center;
    width: 100%;
}

/* Flashcards Section */
.flashcards-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 2rem;
    perspective: 1000px;
}

/* Flashcard Styles */
.flashcard-wrapper {
    perspective: 1000px;
    background-color: transparent;
    height: var(--card-height);
    min-height: 350px;
    cursor: pointer;
    animation: scaleIn 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275) backwards;
}

/* Allow auto-expansion for multiple choice cards */
.flashcard-wrapper.auto-expand {
    height: 100%;
    min-height: 350px;
}

.flashcard-inner {
    position: relative;
    width: 100%;
    height: 100%;
    /* Keep a minimum height but allow it to grow if parent allows */
    min-height: 100%;
    text-align: center;
    transition: transform 0.6s cubic-bezier(0.4, 0.0, 0.2, 1);
    transform-style: preserve-3d;
    box-shadow: 0 10px 30px -10px rgba(0, 0, 0, 0.5);
    border-radius: var(--border-radius-lg);
}

.flashcard-wrapper.flipped .flashcard-inner {
    transform: rotateY(180deg);
}

.flashcard-front,
.flashcard-back {
    position: absolute;
    width: 100%;
    height: 100%;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    padding: 3rem 1.5rem 2rem 1.5rem;
    border-radius: var(--border-radius-lg);
    background-color: var(--surface-color);
    border: 1px solid rgba(255, 255, 255, 0.05);
    overflow-y: auto;
}

/* Allow auto-expansion for multiple choice cards */
.flashcard-wrapper.auto-expand {
    height: auto;
    min-height: 350px;
}
.flashcard-wrapper.auto-expand .flashcard-front {
    position: relative;
    height: auto;
    min-height: 350px;
}
.flashcard-wrapper.auto-expand .flashcard-back {
    position: absolute;
    height: 100%; /* Match exactly what the front stretched it to */
}
/* Hide scrollbar for cleaner look but allow scrolling */
.flashcard-front::-webkit-scrollbar,
.flashcard-back::-webkit-scrollbar {
    display: none;
}
.flashcard-front,
.flashcard-back {
    -ms-overflow-style: none;  /* IE and Edge */
    scrollbar-width: none;  /* Firefox */
}

.flashcard-front {
    background: linear-gradient(145deg, var(--surface-color), #1a2333);
}

.flashcard-back {
    background: linear-gradient(145deg, #1e293b, #151d2b);
    transform: rotateY(180deg);
    border-color: rgba(139, 92, 246, 0.3);
}

.card-label {
    position: absolute;
    top: 1rem;
    left: 1rem;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    font-weight: 600;
    color: var(--secondary-color);
    opacity: 0.8;
}

.flashcard-back .card-label {
    color: var(--primary-color);
}

.card-content {
    font-size: 1.25rem;
    font-weight: 500;
}

.flashcard-back .card-content {
    font-weight: 400;
    font-size: 1.1rem;
    color: var(--text-muted);
}

.flip-hint {
    position: absolute;
    bottom: 1rem;
    font-size: 0.875rem;
    color: var(--text-muted);
    opacity: 0.5;
    transition: opacity var(--transition-speed);
}

.flashcard-wrapper:hover .flip-hint {
    opacity: 1;
}

/* Footer */
footer {
    margin-top: auto;
    text-align: center;
    padding-top: 4rem;
    color: var(--text-muted);
    font-size: 0.875rem;
}

/* Animations */
@keyframes spin {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

@keyframes fadeInDown {
    from {
        opacity: 0;
        transform: translateY(-20px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes scaleIn {
    from {
        opacity: 0;
        transform: scale(0.9);
    }

    to {
        opacity: 1;
        transform: scale(1);
    }
}

/* --- Navigation Bar --- */
.top-nav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 2rem;
    background-color: rgba(30, 41, 59, 0.8);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    position: sticky;
    top: 0;
    z-index: 100;
}

.nav-brand {
    font-family: var(--font-display);
    font-weight: 700;
    font-size: 1.25rem;
    background: var(--gradient-primary);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.nav-controls {
    display: flex;
    align-items: center;
    gap: 1rem;
}

#user-controls {
    display: flex;
    align-items: center;
    gap: 1rem;
}

#user-name {
    font-size: 0.875rem;
    color: var(--text-muted);
    display: none;
    /* hidden on small screens */
}

/* Nav Buttons */
.nav-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    font-family: var(--font-sans);
    font-size: 0.875rem;
    font-weight: 500;
    padding: 0.5rem 1rem;
    border-radius: var(--border-radius-sm);
    cursor: pointer;
    transition: all var(--transition-speed);
    border: none;
}

.nav-btn.primary {
    background: var(--gradient-primary);
    color: white;
}

.nav-btn.primary:hover {
    box-shadow: 0 4px 12px rgba(139, 92, 246, 0.3);
    transform: translateY(-1px);
}

.nav-btn.secondary {
    background-color: var(--surface-hover);
    color: var(--text-main);
}

.nav-btn.secondary:hover {
    background-color: #475569;
}

.nav-btn.outlined {
    background-color: transparent;
    color: var(--text-muted);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.nav-btn.outlined:hover {
    border-color: rgba(255, 255, 255, 0.3);
    color: var(--text-main);
}

/* --- Sidebar --- */
.overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(15, 23, 42, 0.6);
    backdrop-filter: blur(4px);
    z-index: 999;
    opacity: 0;
    visibility: hidden;
    transition: all 0.3s ease;
}

.overlay.active {
    opacity: 1;
    visibility: visible;
}

.sidebar {
    position: fixed;
    top: 0;
    right: -400px;
    width: 100%;
    max-width: 400px;
    height: 100vh;
    background-color: var(--surface-color);
    box-shadow: -5px 0 25px rgba(0, 0, 0, 0.5);
    z-index: 1000;
    display: flex;
    flex-direction: column;
    transition: right 0.4s cubic-bezier(0.16, 1, 0.3, 1);
}

.sidebar.open {
    right: 0;
}

.sidebar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1.5rem;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.sidebar-header h2 {
    font-family: var(--font-display);
    font-size: 1.25rem;
    font-weight: 600;
}

.close-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 1.5rem;
    cursor: pointer;
    line-height: 1;
    padding: 0.25rem;
    transition: color 0.2s;
}

.close-btn:hover {
    color: var(--danger-color);
}

.sidebar-content {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.empty-state {
    text-align: center;
    color: var(--text-muted);
    font-style: italic;
    margin-top: 2rem;
}

/* History Item Card */
.history-item {
    background-color: var(--bg-color);
    border: 1px solid rgba(255, 255, 255, 0.05);
    border-radius: var(--border-radius-sm);
    padding: 1rem;
    cursor: pointer;
    transition: all var(--transition-speed);
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    position: relative;
}

.history-item:hover {
    border-color: rgba(139, 92, 246, 0.3);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.history-item-date {
    font-size: 0.75rem;
    color: var(--primary-color);
    font-weight: 600;
}

.history-item-snippet {
    font-size: 0.875rem;
    color: var(--text-muted);
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.delete-deck-btn {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 1.2rem;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.2s, color 0.2s;
    line-height: 1;
    padding: 0.2rem;
}

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

.delete-deck-btn:hover {
    opacity: 1 !important;
    color: var(--danger-color);
}

.small-spinner {
    width: 24px;
    height: 24px;
    border-width: 2px;
}

.sidebar-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    color: var(--text-muted);
}

/* Responsive */
@media (max-width: 768px) {
    .app-container {
        padding: 1rem;
    }

    header {
        margin-bottom: 2rem;
    }

    .flashcards-grid {
        grid-template-columns: 1fr;
    }

    textarea {
        min-height: 150px;
    }

    #user-name {
        display: none !important;
    }

    .sidebar {
        right: -100%;
        max-width: 100%;
    }
}