:root {
    --bg: #fdf6f0;
    --surface: #fff9f5;
    --text: #5a4a42;
    --text-dim: #8a7a72;
    --text-muted: #b0a098;
    --border: #e8ddd0;
    --border-dim: #f0e8e0;
    --shadow: rgba(180,140,120,0.08);
    --shadow-strong: rgba(180,140,120,0.18);
    --btn-bg: #7ec8e3;
    --btn-text: #fff;
    --btn-hover: #5eb8d8;
    --btn-disabled: #ddd;
    --danger: #e06060;
    --success: #60c080;
    --warning: #e0a040;
    --accent: #7ec8e3;
    --radius: 16px;
    --radius-sm: 10px;
    --radius-lg: 20px;
    --font: 'Segoe UI', 'Inter', system-ui, -apple-system, sans-serif;
}
[data-theme="dark"] {
    --bg: #2a2520;
    --surface: #3a332e;
    --text: #e8ddd0;
    --text-dim: #c0b0a0;
    --text-muted: #908070;
    --border: #5a4a42;
    --border-dim: #4a3a32;
    --shadow: rgba(0,0,0,0.15);
    --shadow-strong: rgba(0,0,0,0.3);
    --btn-bg: #5eb8d8;
    --btn-text: #fff;
    --btn-hover: #4aa0c0;
    --danger: #d05050;
    --success: #50a870;
    --accent: #5eb8d8;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
body {
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    min-height: 100dvh;
    line-height: 1.6;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ========== LAYOUT ========== */
.app-layout {
    display: flex;
    min-height: 100dvh;
}

/* Sidebar */
.sidebar {
    width: 240px;
    background: var(--surface);
    border-right: 1px solid var(--border);
    padding: 20px 0;
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 100;
    transition: transform 0.3s ease;
}
.sidebar-header {
    padding: 0 20px 16px;
    border-bottom: 1px solid var(--border-dim);
    margin-bottom: 8px;
}
.sidebar-header h1 {
    font-size: 18px;
    color: var(--accent);
    letter-spacing: -0.3px;
}
.sidebar-header .subtitle {
    font-size: 12px;
    color: var(--text-muted);
}
.sidebar-nav {
    flex: 1;
    overflow-y: auto;
    padding: 8px 0;
}
.nav-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 20px;
    color: var(--text-dim);
    cursor: pointer;
    transition: all 0.15s;
    border: none;
    background: none;
    width: 100%;
    text-align: left;
    font-size: 14px;
    font-family: var(--font);
}
.nav-item:hover { background: var(--border-dim); color: var(--text); }
.nav-item.active {
    background: rgba(126,200,227,0.12);
    color: var(--accent);
    font-weight: 600;
    border-right: 3px solid var(--accent);
}
.nav-item .icon { font-size: 18px; width: 24px; text-align: center; }
.nav-item .badge {
    margin-left: auto;
    background: var(--accent);
    color: #fff;
    font-size: 11px;
    padding: 2px 8px;
    border-radius: 10px;
    font-weight: 600;
}

/* Main content */
.main-content {
    flex: 1;
    margin-left: 240px;
    padding: 24px 32px;
    max-width: 1000px;
}
.main-content.wide { max-width: 1200px; }

/* Top bar */
.top-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 24px;
    gap: 16px;
}
.top-bar h2 {
    font-size: 24px;
    font-weight: 700;
    color: var(--text);
}
.top-bar-actions {
    display: flex;
    gap: 8px;
    align-items: center;
}

/* ========== CARDS & CONTAINERS ========== */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: 0 4px 20px var(--shadow);
    padding: 20px 24px;
}
.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--border-dim);
}
.card-header h3 { font-size: 16px; font-weight: 600; }

/* ========== BUTTONS ========== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px 20px;
    border-radius: var(--radius);
    border: none;
    font-family: var(--font);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.15s;
    background: var(--btn-bg);
    color: var(--btn-text);
    line-height: 1.2;
}
.btn:hover {
    background: var(--btn-hover);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(126,200,227,0.3);
}
.btn:active { transform: translateY(0); }
.btn:disabled { background: var(--btn-disabled); cursor: not-allowed; transform: none; box-shadow: none; }
.btn-sm { padding: 6px 14px; font-size: 13px; border-radius: var(--radius-sm); }
.btn-outline {
    background: transparent;
    border: 1.5px solid var(--border);
    color: var(--text-dim);
}
.btn-outline:hover { background: var(--border-dim); color: var(--text); box-shadow: none; }
.btn-danger { background: var(--danger); }
.btn-danger:hover { background: #d05050; box-shadow: 0 4px 12px rgba(224,96,96,0.3); }
.btn-success { background: var(--success); }
.btn-success:hover { background: #50a870; box-shadow: 0 4px 12px rgba(96,192,128,0.3); }
.btn-ghost {
    background: transparent;
    color: var(--text-dim);
    padding: 8px 12px;
}
.btn-ghost:hover { background: var(--border-dim); color: var(--text); transform: none; box-shadow: none; }

/* ========== FORMS ========== */
.input {
    width: 100%;
    padding: 10px 14px;
    border: 1.5px solid var(--border);
    border-radius: var(--radius-sm);
    font-family: var(--font);
    font-size: 14px;
    background: var(--surface);
    color: var(--text);
    transition: border-color 0.15s, box-shadow 0.15s;
    outline: none;
}
.input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(126,200,227,0.15);
}
.textarea {
    width: 100%;
    padding: 10px 14px;
    border: 1.5px solid var(--border);
    border-radius: var(--radius-sm);
    font-family: var(--font);
    font-size: 14px;
    background: var(--surface);
    color: var(--text);
    resize: vertical;
    min-height: 80px;
    outline: none;
    transition: border-color 0.15s, box-shadow 0.15s;
}
.textarea:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(126,200,227,0.15);
}
label { display: block; margin-bottom: 4px; font-size: 13px; font-weight: 600; color: var(--text-dim); }
.form-group { margin-bottom: 16px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }
select.input { cursor: pointer; }

/* ========== LOGIN PAGE ========== */
.login-page {
    min-height: 100dvh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg);
    padding: 20px;
}
.login-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: 0 8px 32px var(--shadow-strong);
    padding: 40px;
    width: 100%;
    max-width: 380px;
}
.login-card h1 {
    text-align: center;
    color: var(--accent);
    font-size: 28px;
    margin-bottom: 4px;
    letter-spacing: -0.5px;
}
.login-card .subtitle {
    text-align: center;
    color: var(--text-muted);
    font-size: 14px;
    margin-bottom: 28px;
}
.login-card .form-group { margin-bottom: 20px; }
.login-card .btn { width: 100%; padding: 12px; font-size: 15px; margin-top: 4px; }
.login-error {
    color: var(--danger);
    font-size: 13px;
    text-align: center;
    margin-top: 12px;
    display: none;
}
.remember-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 16px;
    font-size: 13px;
    color: var(--text-dim);
}
.remember-row input[type="checkbox"] { accent-color: var(--accent); }

/* ========== DECKS GRID ========== */
.decks-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: 16px;
}
.deck-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 20px;
    cursor: pointer;
    transition: all 0.15s;
    box-shadow: 0 2px 8px var(--shadow);
}
.deck-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 24px var(--shadow-strong);
    border-color: var(--accent);
}
.deck-card .color-bar {
    height: 4px;
    border-radius: 2px;
    margin-bottom: 12px;
    width: 40px;
}
.deck-card h3 { font-size: 16px; margin-bottom: 4px; }
.deck-card .desc { font-size: 13px; color: var(--text-muted); margin-bottom: 12px; }
.deck-card .stats {
    display: flex;
    gap: 12px;
    font-size: 12px;
    color: var(--text-dim);
}
.deck-card .stats span { display: flex; align-items: center; gap: 4px; }
.deck-card .deck-actions {
    display: flex;
    gap: 6px;
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px solid var(--border-dim);
}

/* ========== STUDY VIEW ========== */
.study-container {
    max-width: 700px;
    margin: 0 auto;
}
.study-progress {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 20px;
    font-size: 14px;
    color: var(--text-dim);
}
.study-progress .bar {
    flex: 1;
    height: 6px;
    background: var(--border-dim);
    border-radius: 3px;
    overflow: hidden;
}
.study-progress .bar-fill {
    height: 100%;
    background: var(--accent);
    border-radius: 3px;
    transition: width 0.3s ease;
}
.flashcard {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 40px;
    min-height: 300px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    box-shadow: 0 4px 24px var(--shadow);
    margin-bottom: 20px;
    cursor: pointer;
    transition: all 0.2s;
    position: relative;
}
.flashcard:hover { box-shadow: 0 6px 32px var(--shadow-strong); }
.flashcard .card-type-badge {
    position: absolute;
    top: 12px;
    right: 12px;
    font-size: 11px;
    padding: 3px 10px;
    border-radius: 10px;
    background: var(--border-dim);
    color: var(--text-muted);
}
.flashcard .card-front {
    font-size: 22px;
    font-weight: 600;
    line-height: 1.5;
    color: var(--text);
}
.flashcard .card-back {
    font-size: 18px;
    line-height: 1.5;
    color: var(--text-dim);
    display: none;
}
.flashcard .card-hint {
    font-size: 14px;
    color: var(--text-muted);
    margin-top: 16px;
    font-style: italic;
    display: none;
}
.flashcard .tap-hint {
    font-size: 13px;
    color: var(--text-muted);
    margin-top: 24px;
    opacity: 0.6;
}
.flashcard.revealed .card-back { display: block; }
.flashcard.revealed .tap-hint { display: none; }
.flashcard.revealed .card-hint { display: block; }

/* Active recall input */
.active-recall-area {
    width: 100%;
    margin-bottom: 16px;
}
.active-recall-area .input {
    text-align: center;
    font-size: 18px;
    padding: 14px;
}
.recall-feedback {
    text-align: center;
    font-size: 14px;
    margin-top: 8px;
    display: none;
}
.recall-feedback.correct { color: var(--success); display: block; }
.recall-feedback.wrong { color: var(--danger); display: block; }

/* Rating buttons */
.rating-buttons {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr 1fr;
    gap: 8px;
    display: none;
}
.rating-buttons.show { display: grid; }
.rating-btn {
    padding: 14px 8px;
    border: 1.5px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    cursor: pointer;
    text-align: center;
    transition: all 0.15s;
    font-family: var(--font);
}
.rating-btn:hover { transform: translateY(-2px); }
.rating-btn .label { font-size: 13px; font-weight: 600; display: block; }
.rating-btn .desc { font-size: 11px; color: var(--text-muted); display: block; margin-top: 2px; }
.rating-btn.again:hover { border-color: var(--danger); background: rgba(224,96,96,0.08); }
.rating-btn.hard:hover { border-color: var(--warning); background: rgba(224,160,64,0.08); }
.rating-btn.good:hover { border-color: var(--success); background: rgba(96,192,128,0.08); }
.rating-btn.easy:hover { border-color: var(--accent); background: rgba(126,200,227,0.08); }
.rating-btn.again .label { color: var(--danger); }
.rating-btn.hard .label { color: var(--warning); }
.rating-btn.good .label { color: var(--success); }
.rating-btn.easy .label { color: var(--accent); }

/* ========== MODAL ========== */
.modal-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.4);
    z-index: 1000;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 20px;
}
.modal-overlay.show { display: flex; }
.modal {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: 0 8px 40px rgba(0,0,0,0.2);
    padding: 28px;
    width: 100%;
    max-width: 540px;
    max-height: 85vh;
    overflow-y: auto;
}
.modal h3 { font-size: 18px; margin-bottom: 16px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 20px; }
.modal-close {
    position: absolute;
    top: 16px;
    right: 16px;
    background: none;
    border: none;
    font-size: 20px;
    cursor: pointer;
    color: var(--text-muted);
}

/* ========== CARD LIST ========== */
.card-list { display: flex; flex-direction: column; gap: 8px; }
.card-list-item {
    background: var(--surface);
    border: 1px solid var(--border-dim);
    border-radius: var(--radius-sm);
    padding: 12px 16px;
    display: flex;
    align-items: center;
    gap: 12px;
    transition: all 0.15s;
}
.card-list-item:hover { border-color: var(--border); box-shadow: 0 2px 8px var(--shadow); }
.card-list-item .type-badge {
    font-size: 11px;
    padding: 2px 8px;
    border-radius: 8px;
    background: var(--border-dim);
    color: var(--text-muted);
    white-space: nowrap;
    text-transform: uppercase;
}
.card-list-item .front-text {
    flex: 1;
    font-size: 14px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.card-list-item .card-srs-info {
    font-size: 11px;
    color: var(--text-muted);
    white-space: nowrap;
}
.card-list-item .actions { display: flex; gap: 4px; }

/* ========== AI CHAT ========== */
.chat-container {
    display: flex;
    flex-direction: column;
    height: calc(100dvh - 120px);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    overflow: hidden;
}
.chat-header {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border-dim);
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.chat-header h3 { font-size: 15px; }
.chat-header .card-context {
    font-size: 12px;
    color: var(--text-muted);
}
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}
.chat-msg {
    max-width: 85%;
    padding: 12px 16px;
    border-radius: var(--radius);
    font-size: 14px;
    line-height: 1.5;
    animation: fadeIn 0.2s ease;
}
.chat-msg.user {
    background: var(--accent);
    color: #fff;
    align-self: flex-end;
    border-bottom-right-radius: 4px;
}
.chat-msg.assistant {
    background: var(--border-dim);
    color: var(--text);
    align-self: flex-start;
    border-bottom-left-radius: 4px;
}
.chat-msg pre {
    background: rgba(0,0,0,0.06);
    padding: 8px 12px;
    border-radius: 8px;
    overflow-x: auto;
    font-size: 13px;
    margin: 8px 0;
}
.chat-msg code {
    background: rgba(0,0,0,0.06);
    padding: 1px 4px;
    border-radius: 4px;
    font-size: 13px;
}
.chat-msg .create-cards-btn {
    margin-top: 8px;
}
.chat-input-area {
    padding: 16px 20px;
    border-top: 1px solid var(--border-dim);
    display: flex;
    gap: 8px;
}
.chat-input-area .input { flex: 1; }
.chat-input-area .btn { white-space: nowrap; }

/* ========== AI IMPORT ========== */
.ai-import-container { max-width: 700px; margin: 0 auto; }
.ai-import-container .card { margin-bottom: 16px; }
.import-tabs {
    display: flex;
    gap: 4px;
    margin-bottom: 16px;
    background: var(--border-dim);
    padding: 4px;
    border-radius: var(--radius-sm);
}
.import-tab {
    flex: 1;
    padding: 8px 16px;
    border: none;
    border-radius: 8px;
    background: transparent;
    color: var(--text-dim);
    cursor: pointer;
    font-family: var(--font);
    font-size: 13px;
    font-weight: 600;
    transition: all 0.15s;
}
.import-tab.active {
    background: var(--surface);
    color: var(--text);
    box-shadow: 0 1px 4px var(--shadow);
}
.import-pane { display: none; }
.import-pane.active { display: block; }

/* ========== STATS ========== */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 12px;
    margin-bottom: 24px;
}
.stat-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    text-align: center;
}
.stat-card .value {
    font-size: 28px;
    font-weight: 700;
    color: var(--accent);
}
.stat-card .label {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 4px;
}

/* ========== EMPTY STATE ========== */
.empty-state {
    text-align: center;
    padding: 60px 20px;
    color: var(--text-muted);
}
.empty-state .icon { font-size: 48px; margin-bottom: 16px; }
.empty-state h3 { font-size: 18px; color: var(--text-dim); margin-bottom: 8px; }
.empty-state p { font-size: 14px; margin-bottom: 20px; }

/* ========== RESPONSIVE ========== */
@media (max-width: 768px) {
    .sidebar {
        transform: translateX(-100%);
    }
    .sidebar.open { transform: translateX(0); }
    .main-content {
        margin-left: 0;
        padding: 16px;
    }
    .mobile-menu-btn {
        display: flex !important;
    }
    .decks-grid {
        grid-template-columns: 1fr;
    }
    .form-row { grid-template-columns: 1fr; }
    .rating-buttons { grid-template-columns: 1fr 1fr; }
    .chat-msg { max-width: 95%; }
    .stats-grid { grid-template-columns: 1fr 1fr; }
    .flashcard { padding: 24px 16px; min-height: 220px; }
    .flashcard .card-front { font-size: 18px; }
}
@media (min-width: 769px) {
    .mobile-menu-btn { display: none !important; }
    .mobile-overlay { display: none !important; }
}

/* Mobile overlay */
.mobile-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.3);
    z-index: 99;
    display: none;
}
.mobile-overlay.show { display: block; }

/* ========== ANIMATIONS ========== */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}
@keyframes slideUp {
    from { opacity: 0; transform: translateY(20px); }
    to { opacity: 1; transform: translateY(0); }
}
.fade-in { animation: fadeIn 0.2s ease; }
.slide-up { animation: slideUp 0.25s ease; }

/* ========== UTILITY ========== */
.flex { display: flex; }
.flex-col { flex-direction: column; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.gap-8 { gap: 8px; }
.gap-12 { gap: 12px; }
.gap-16 { gap: 16px; }
.mt-8 { margin-top: 8px; }
.mt-16 { margin-top: 16px; }
.mb-16 { margin-bottom: 16px; }
.text-center { text-align: center; }
.text-muted { color: var(--text-muted); }
.text-dim { color: var(--text-dim); }
.w-full { width: 100%; }
.truncate { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Loading */
.loading {
    text-align: center;
    padding: 40px;
    color: var(--text-muted);
}
.loading::after {
    content: '...';
    animation: dots 1.5s steps(4, end) infinite;
}
@keyframes dots {
    0%, 20% { content: ''; }
    40% { content: '.'; }
    60% { content: '..'; }
    80%, 100% { content: '...'; }
}

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

/* Cloze styling */
.cloze-blank {
    display: inline-block;
    background: rgba(126,200,227,0.15);
    padding: 0 6px;
    border-radius: 4px;
    border-bottom: 2px dashed var(--accent);
    font-weight: 600;
    color: var(--accent);
}
.cloze-filled {
    display: inline-block;
    background: rgba(96,192,128,0.15);
    padding: 0 6px;
    border-radius: 4px;
    border-bottom: 2px solid var(--success);
    font-weight: 600;
    color: var(--success);
}

/* Multiple choice in study */
.mc-options {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 16px;
    width: 100%;
    max-width: 500px;
}
.mc-option {
    padding: 12px 16px;
    border: 1.5px solid var(--border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    text-align: left;
    font-family: var(--font);
    font-size: 14px;
    color: var(--text);
    background: var(--surface);
    transition: all 0.15s;
}
.mc-option:hover { border-color: var(--accent); background: rgba(126,200,227,0.05); }
.mc-option.selected { border-color: var(--accent); background: rgba(126,200,227,0.1); }
.mc-option.correct { border-color: var(--success); background: rgba(96,192,128,0.1); }
.mc-option.wrong { border-color: var(--danger); background: rgba(224,96,96,0.1); }

/* Search */
.search-bar {
    position: relative;
}
.search-bar .input { padding-left: 36px; }
.search-bar .search-icon {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-muted);
    font-size: 16px;
}