:root {
    --primary: #4f46e5;         
    --primary-hover: #4338ca;
    --primary-light: #e0e7ff;
    --secondary: #c3eeff;       
    --secondary-hover: #e2e8f0; 
    --bg-color: #f4f6f6;        
    --surface: #9de1f5;
    --danger: #ef4444;
    --danger-hover: #dc2626;
    --warning: #f59e0b;
    --warning-bg: #fef3c7;
    --ai-gradient: linear-gradient(135deg, #6366f1, #a855f7, #ec4899);
    --text-main: #0f172a;       
    --text-muted: #64748b;      
    --border: #e2e8f0;          
    --shadow-sm: 0 2px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 10px 3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
    --radius-md: 10px;
    --radius-lg: 16px;
}

body { 
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; 
    background: var(--bg-color); color: var(--text-main); margin: 0; padding: 0; line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    
}
.hidden { display: none !important; }
.text-center { text-align: center; }
.text-sm { font-size: 0.875rem; }
.text-muted { color: var(--text-muted); }
.font-normal { font-weight: 400; }
.font-semibold { font-weight: 600; }
h1, h2, h3 { margin: 0; color: var(--text-main); letter-spacing: -0.02em; }
p { margin: 0 0 1rem 0; color: var(--text-muted); }
.subtitle { font-size: 1.1rem; color: var(--text-muted); margin-top: 0.25rem; }

.navbar { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 50; padding: 0.75rem 0; }
.nav-content { max-width: 1200px; margin: 0 auto; padding: 0 20px; display: flex; justify-content: space-between; align-items: center; }
.logo { font-size: 1.25rem; font-weight: 700; color: var(--text-main); display: flex; align-items: center; gap: 8px; cursor: pointer; }
.container { max-width: 1000px; margin: 0 auto; padding: 40px 20px; }
.page-header { display: flex; justify-content: space-between; align-items: flex-end; margin-bottom: 2rem; flex-wrap: wrap; gap: 1rem; }

button { cursor: pointer; padding: 0.6rem 1.2rem; border: none; border-radius: 8px; font-family: 'Inter', sans-serif; font-weight: 600; font-size: 0.9rem; transition: all 0.2s ease; display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
.btn-primary { background: var(--primary); color: white; box-shadow: var(--shadow-sm); }
.btn-primary:hover { background: var(--primary-hover); transform: translateY(-1px); }
.btn-secondary { background: var(--secondary); color: var(--text-main); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--secondary-hover); }
.btn-danger { background: #fee2e2; color: var(--danger); }
.btn-danger:hover { background: #fecaca; }
.btn-ghost { background: transparent; color: var(--text-muted); }
.btn-ghost:hover { background: var(--secondary); color: var(--text-main); }
.btn-export { background: #fffbeb; color: #b45309; border: 1px solid #fde68a; }
.btn-export:hover { background: #fef3c7; }
.btn-outline-dashed { background: transparent; border: 2px dashed var(--border); color: var(--text-muted); width: 100%; padding: 1rem; margin-top: 1.5rem; font-size: 1rem; }
.btn-outline-dashed:hover { border-color: var(--primary); color: var(--primary); background: var(--primary-light); }
.button-group { display: flex; gap: 10px; }

.ai-plus-btn { background: var(--ai-gradient); color: white; box-shadow: 0 4px 10px rgba(168, 85, 247, 0.3); border-radius: 20px; padding: 0.5rem 1.25rem; transition: transform 0.2s, box-shadow 0.2s; }
.ai-plus-btn:hover { transform: scale(1.05); box-shadow: 0 6px 15px rgba(168, 85, 247, 0.4); }
.ai-action-btn { background: var(--text-main); color: white; width: 100%; margin-top: 15px; }
.ai-action-btn:hover { background: #000; }

.set-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px; }
.set-card { background: var(--surface); padding: 24px; border-radius: var(--radius-md); box-shadow: var(--shadow-sm); border: 1px solid var(--border); transition: transform 0.2s ease, box-shadow 0.2s ease; display: flex; flex-direction: column; }
.set-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-md); border-color: #cbd5e1; }
.set-card h3 { margin-bottom: 0.25rem; font-size: 1.25rem; }
.set-card p { margin-bottom: 1.5rem; font-size: 0.9rem; }
.set-card .button-group { margin-top: auto; justify-content: flex-start; }

.ai-promo-banner { margin-top: 3rem; padding: 2rem; background: var(--surface); border-radius: var(--radius-md); border: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; gap: 2rem; box-shadow: var(--shadow-sm); position: relative; overflow: hidden; }
.ai-promo-banner::before { content: ''; position: absolute; top: 0; left: 0; width: 4px; height: 100%; background: var(--ai-gradient); }
.ai-promo-content h2 { margin-bottom: 0.5rem; }
.ai-promo-content p { margin-bottom: 0; max-width: 500px; }

.edit-title-container { margin-bottom: 2rem; }
.editable-title { font-size: 2rem; font-weight: 700; font-family: inherit; color: var(--text-main); border: 2px solid transparent; background: transparent; padding: 0.5rem; width: 100%; border-radius: 8px; outline: none; transition: all 0.2s ease; margin-left: -0.5rem; }
.editable-title:hover { background: var(--secondary); }
.editable-title:focus { background: var(--surface); border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-light); }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 20px; }
.mini-card-container { display: flex; flex-direction: column; gap: 10px; background: var(--surface); padding: 12px; border-radius: var(--radius-md); border: 1px solid var(--border); box-shadow: var(--shadow-sm); }
.mini-card { height: 120px; display: flex; align-items: center; justify-content: center; background: var(--bg-color); border-radius: 6px; padding: 12px; font-size: 0.9rem; overflow-y: auto; text-align: center; border: 1px solid var(--border); }
.mini-controls { display: flex; gap: 6px; }
.mini-btn { flex: 1; padding: 6px 4px; font-size: 0.75rem; }

.play-container { display: flex; flex-direction: column; align-items: center; justify-content: center; height: calc(100vh - 150px); perspective: 1500px; overflow: hidden; }
.flashcard { width: 100%; max-width: 560px; height: 420px; position: relative; transform-style: preserve-3d; transition: transform 0.6s cubic-bezier(0.34, 1.56, 0.64, 1); cursor: pointer; }
.flashcard.flipped { transform: rotateY(180deg); }
.flashcard.slide-away { transform: translate3d(-1500px, 0, 0) rotateY(180deg) !important; transition: transform 0.55s ease-in; }
.flashcard.instant-reset { transition: none !important; opacity: 0 !important; }
.side { position: absolute; inset: 0; backface-visibility: hidden !important; -webkit-backface-visibility: hidden !important; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px; padding: 30px; background: var(--surface); border-radius: var(--radius-lg); box-shadow: var(--shadow-lg); text-align: center; border: 1px solid var(--border); box-sizing: border-box; font-size: 1.25rem; overflow: hidden; }
.front { z-index: 2; transform: rotateY(0deg); }
.back { transform: rotateY(180deg) translateZ(1px); background: #fdfdfd; z-index: 1; border-color: #cbd5e1; }
.card-text { width: 100%; max-height: 100%; overflow-y: auto; display: flex; align-items: center; justify-content: center; text-align: center; }
.card-img { max-width: 100%; max-height: 180px; margin-top: 20px; border-radius: 6px; object-fit: contain; }

.modal { position: fixed; inset: 0; background: rgba(15, 23, 42, 0.6); backdrop-filter: blur(4px); display: flex; align-items: center; justify-content: center; z-index: 100; }
.modal-content { background: var(--surface); padding: 30px; border-radius: var(--radius-lg); width: 90%; max-width: 450px; max-height: 90vh; overflow-y: auto; box-shadow: var(--shadow-lg); border: 1px solid rgba(255,255,255,0.1); }
.modal-content h3 { margin-bottom: 1.5rem; font-size: 1.25rem; }
.modal-footer { display: flex; justify-content: flex-end; gap: 10px; margin-top: 1.5rem; }

.form-group { display: flex; flex-direction: column; gap: 6px; margin-bottom: 1rem; }
.form-group label { font-size: 0.875rem; font-weight: 600; color: var(--text-main); }
input[type="text"], input[type="password"], textarea { padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; width: 100%; box-sizing: border-box; font-family: inherit; font-size: 0.95rem; transition: all 0.2s; background: var(--bg-color); }
input[type="text"]:focus, input[type="password"]:focus, textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-light); background: var(--surface); }

.file-upload-box { display: flex; flex-direction: column; gap: 8px; background: var(--bg-color); padding: 12px; border-radius: 8px; border: 1px dashed #cbd5e1; margin-bottom: 1rem; }
.checkbox-container { display: flex; align-items: center; gap: 8px; font-size: 0.9rem; font-weight: 500; color: var(--text-main); cursor: pointer; margin-bottom: 1rem; }
.checkbox-container input { width: 16px; height: 16px; cursor: pointer; }
.divider { border: 0; height: 1px; background: var(--border); margin: 1.5rem 0; }
.divider-text { text-align: center; margin: 15px 0; font-weight: 600; color: var(--text-muted); font-size: 0.85rem; letter-spacing: 1px; }
.warning-box { padding: 12px; background: var(--warning-bg); border: 1px solid #fde68a; border-radius: 8px; color: #92400e; font-size: 0.9rem; text-align: left; }

.ai-container { max-width: 600px; margin: 0 auto; padding: 20px 0; }
.tutorial-box { background: var(--surface); padding: 24px; border-radius: var(--radius-md); border: 1px solid var(--border); margin-bottom: 2rem; margin-top: 2rem; }
.code-block { background: var(--bg-color); padding: 16px; border-radius: 8px; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace; font-size: 0.9rem; border-left: 4px solid var(--primary); margin: 15px 0; color: var(--text-main); overflow-x: auto; }
.ai-options { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.ai-card { background: var(--surface); padding: 24px; border-radius: var(--radius-md); box-shadow: var(--shadow-sm); border: 1px solid var(--border); display: flex; flex-direction: column; justify-content: space-between; }
.premium-icon { font-size: 4rem; margin: 2rem 0; }
.success-text { color: #16a34a; font-weight: 500; }

.premium-container { margin-top: 2rem; }
.premium-checkout { background: linear-gradient(180deg, #ffffff, #f8fafc); padding: 30px; border-radius: 16px; border: 1px solid var(--border); box-shadow: var(--shadow-md); margin-top: 2rem; }
.premium-warning { border: 1px solid #facc15; background: #fef9c3; color: #854d0e; padding: 12px 14px; border-radius: 10px; font-size: 0.9rem; margin-top: 1.25rem; }
.premium-plan-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.premium-plan-card { border: 1px solid #dbeafe; border-radius: 14px; padding: 18px; text-align: left; background: white; color: var(--text-main); transition: border-color 0.2s, box-shadow 0.2s, transform 0.2s; box-shadow: var(--shadow-sm); position: relative; }
.premium-plan-card:hover { transform: translateY(-2px); border-color: #93c5fd; box-shadow: var(--shadow-md); }
.premium-plan-card.active { border: 2px solid var(--primary); box-shadow: 0 0 0 4px var(--primary-light); }
.premium-plan-card.popular { border-color: #d8b4fe; background: linear-gradient(180deg, #ffffff, #faf5ff); }
.popular-pill { position: absolute; top: -10px; right: 12px; background: var(--ai-gradient); color: white; border-radius: 999px; padding: 2px 10px; font-size: 0.72rem; font-weight: 700; letter-spacing: 0.02em; }
.premium-price { font-size: 1.8rem; font-weight: 700; margin: 4px 0 2px 0; color: var(--text-main); }
.premium-price span { font-size: 0.95rem; color: var(--text-muted); font-weight: 500; }
.premium-subtext { font-size: 0.87rem; color: var(--text-muted); margin-bottom: 0.35rem; }
.premium-plan-note { font-size: 0.82rem; color: #334155; margin: 0; font-weight: 500; }
.premium-trust { display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; margin-top: 16px; padding: 10px 12px; border: 1px dashed #cbd5e1; border-radius: 10px; background: #f8fafc; color: #0f172a; font-size: 0.85rem; }
.premium-options { margin-top: 16px; text-align: left; }
.premium-checkout-btn { width: 100%; margin-top: 10px; font-size: 1rem; padding: 0.8rem; min-height: 48px; }
.premium-btn-loading { display: inline-flex; align-items: center; gap: 8px; }
.premium-btn-loading::before { content: ""; width: 14px; height: 14px; border: 2px solid rgba(255,255,255,0.45); border-top-color: white; border-radius: 50%; animation: spin 0.75s linear infinite; }
.premium-success { background: #f0fdf4; padding: 40px; border-radius: 16px; border: 1px solid #bbf7d0; margin-top: 2rem; text-align: center; }
.premium-disclaimer { font-size: 0.78rem; color: #64748b; margin-top: 1rem; text-align: center; line-height: 1.45; }

@media (max-width: 600px) {
    .ai-options { grid-template-columns: 1fr; }
    .ai-promo-banner { flex-direction: column; text-align: center; }
    .ai-promo-banner button { width: 100%; }
    .flashcard { max-width: 92vw; height: 400px; }
    .side { padding: 22px; font-size: 1.05rem; }
    .premium-plan-grid { grid-template-columns: 1fr; }
}

/* =========================================
   LOADING SPINNER
   ========================================= */
.spinner {
    width: 48px;
    height: 48px;
    border: 5px solid var(--primary-light);
    border-top: 5px solid var(--primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.loading-state-container {
    text-align: center;
    padding: 30px 10px;
}

.loading-state-container h3 {
    margin-top: 20px;
    color: var(--primary);
    font-size: 1.25rem;
}

/* Flashcards use legacy class names on buttons */
.secondary-btn { background: var(--secondary); color: var(--text-main); border: 1px solid var(--border); }
.delete-btn { background: #fee2e2; color: var(--danger); }
.delete-btn:hover { background: #fecaca; }

/* Project Assistant */
.home-section { margin-bottom: 0; }
.project-assistant-home {
    margin-top: 3rem;
    padding-top: 2rem;
    border-top: 1px solid var(--border);
}
.section-heading-row {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    flex-wrap: wrap;
    gap: 1rem;
    margin-bottom: 1.5rem;
}
.section-heading-row h2 { font-size: 1.5rem; }

.project-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 20px;
}
.project-card {
    background: var(--surface);
    padding: 24px;
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    display: flex;
    flex-direction: column;
}
.project-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
    border-color: #cbd5e1;
}
.project-card-top {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 12px;
    margin-bottom: 0.5rem;
}
.project-card-top h3 {
    margin: 0;
    font-size: 1.15rem;
    flex: 1;
    line-height: 1.3;
}
.project-pct {
    font-weight: 700;
    font-size: 1rem;
    color: var(--primary);
    white-space: nowrap;
}
.project-card-summary {
    font-size: 0.9rem;
    color: var(--text-muted);
    margin: 0 0 12px 0;
    line-height: 1.45;
    flex: 1;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
.project-progress-bar {
    height: 8px;
    background: var(--bg-color);
    border-radius: 999px;
    overflow: hidden;
    border: 1px solid var(--border);
    margin-bottom: 8px;
}
.project-progress-bar-large {
    height: 12px;
    margin: 12px 0 1.25rem 0;
}
.project-progress-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--primary), #818cf8);
    border-radius: 999px;
    transition: width 0.25s ease;
}
.project-task-meta {
    font-size: 0.82rem;
    color: var(--text-muted);
    margin: 0 0 1rem 0;
}
.project-setup-wrap {
    max-width: 640px;
    margin: 0 auto;
    padding: 0 0 2rem 0;
}

.project-setup-error {
    background: #fef2f2;
    border: 1px solid #fecaca;
    color: #991b1b;
    padding: 12px 14px;
    border-radius: 10px;
    font-size: 0.9rem;
    line-height: 1.45;
    white-space: pre-wrap;
    word-break: break-word;
    margin-bottom: 1.25rem;
}
.project-view-heading {
    font-size: 1.75rem;
    margin: 0.5rem 0 0.25rem 0;
}
.project-view-meta {
    font-size: 0.95rem;
    color: var(--text-muted);
    margin: 0 0 0.5rem 0;
}
.project-view-summary-text {
    font-size: 1rem;
    color: var(--text-main);
    line-height: 1.55;
    margin: 0;
}
.project-view-layout {
    display: grid;
    grid-template-columns: 1fr minmax(280px, 340px);
    gap: 1.5rem;
    align-items: start;
}

.project-view-main {
    min-width: 0;
}

.project-chat-aside {
    position: sticky;
    top: 88px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-sm);
    display: flex;
    flex-direction: column;
    max-height: calc(100vh - 120px);
}

.project-chat-header {
    font-weight: 700;
    font-size: 1rem;
    padding: 14px 14px 4px 14px;
    border-bottom: 1px solid var(--border);
}

.project-chat-subtitle {
    margin: 0;
    padding: 0 14px 10px 14px;
    border-bottom: 1px solid var(--border);
}

.project-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 12px;
    min-height: 200px;
    max-height: 420px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.project-chat-hint {
    margin: 0;
    line-height: 1.45;
}

.project-chat-bubble {
    padding: 10px 12px;
    border-radius: 10px;
    font-size: 0.88rem;
    line-height: 1.45;
    max-width: 100%;
    white-space: pre-wrap;
    word-break: break-word;
}

.project-chat-bubble-user {
    align-self: flex-end;
    background: var(--primary-light);
    color: var(--text-main);
    border: 1px solid #c7d2fe;
}

.project-chat-bubble-assistant {
    align-self: flex-start;
    background: var(--bg-color);
    border: 1px solid var(--border);
}

.project-chat-compose {
    padding: 12px;
    border-top: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.project-chat-compose textarea {
    resize: vertical;
    min-height: 64px;
    font-size: 0.9rem;
}

.project-chat-compose .btn-primary {
    align-self: flex-end;
}

.project-ai-modal-content {
    max-width: 520px;
}

.project-ai-modal-body {
    line-height: 1.5;
    font-size: 0.95rem;
    max-height: 60vh;
    overflow-y: auto;
}

.project-ai-modal-loading {
    color: var(--text-muted);
}

.project-explain-text {
    white-space: pre-wrap;
    word-break: break-word;
}

.project-checklist {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.project-task-wrap {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.project-checklist-row {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 12px 14px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    font-size: 0.95rem;
    line-height: 1.45;
}

.project-checklist-row input[type="checkbox"] {
    margin-top: 4px;
    width: 18px;
    height: 18px;
    cursor: pointer;
    flex-shrink: 0;
}

.project-task-text {
    flex: 1;
    min-width: 0;
}

.project-task-actions {
    display: flex;
    flex-direction: column;
    gap: 6px;
    flex-shrink: 0;
}

.btn-task-ai {
    font-size: 0.72rem;
    font-weight: 600;
    padding: 5px 8px;
    border-radius: 6px;
    border: 1px solid var(--border);
    background: var(--bg-color);
    color: var(--text-main);
    cursor: pointer;
    white-space: nowrap;
}

.btn-task-ai:hover {
    border-color: var(--primary);
    color: var(--primary);
}

.project-subtask-list {
    margin: 4px 0 0 0;
    padding: 0 0 0 1.5rem;
    list-style: none;
    font-size: 0.88rem;
    line-height: 1.45;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.project-subtask-row {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    color: var(--text-main);
}

.project-subtask-row input[type='checkbox'] {
    margin-top: 3px;
    width: 16px;
    height: 16px;
    cursor: pointer;
    flex-shrink: 0;
}

.project-subtask-text {
    flex: 1;
    min-width: 0;
}

.project-subtask-done {
    text-decoration: line-through;
    color: var(--text-muted);
}

.project-task-done {
    text-decoration: line-through;
    color: var(--text-muted);
}

@media (max-width: 960px) {
    /* Stack vertically; show chat first so it is not missed below a long checklist */
    .project-view-layout {
        display: flex;
        flex-direction: column;
    }
    .project-chat-aside {
        order: -1;
        position: relative;
        top: auto;
        max-height: none;
    }
    .project-view-main {
        order: 0;
    }
    .project-chat-messages {
        max-height: 280px;
    }
}

@media (max-width: 600px) {
    .section-heading-row { flex-direction: column; align-items: stretch; }
    .section-heading-row .btn-primary { width: 100%; }
}