/* Base & Theme System */
:root {
    --font-ui: 'Inter', system-ui, sans-serif;

    /* Light Theme */
    --bg-app: #F3F4F6;
    --bg-surface: #FFFFFF;
    --bg-hover: #F3F4F6;
    --border-color: #E5E7EB;

    --text-main: #111827;
    --text-muted: #6B7280;

    --primary: #2563EB;
    --primary-hover: #1D4ED8;

    --grade-1: #EF4444;
    /* Again - Red */
    --grade-2: #F59E0B;
    /* Hard - Orange */
    --grade-3: #3B82F6;
    /* Good - Blue */
    --grade-4: #10B981;
    /* Easy - Green */

    --card-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}

[data-theme="dark"] {
    --bg-app: #0F172A;
    /* Deep blue background */
    --bg-surface: #1E293B;
    /* Slate surface */
    --bg-hover: #334155;
    --border-color: #334155;

    --text-main: #F8FAFC;
    --text-muted: #94A3B8;

    --primary: #EAB308;
    /* Gold/Amber accent */
    --primary-hover: #CA8A04;

    --card-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.5), 0 4px 6px -2px rgba(0, 0, 0, 0.3);
}

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

body {
    font-family: var(--font-ui);
    background-color: var(--bg-app);
    color: var(--text-main);
    line-height: 1.5;
    transition: background-color 0.3s, color 0.3s;
}

ol,
ul {
    padding-left: 1.5rem;
    margin-top: 0.5rem;
}

pre,
code {
    font-family: monospace;
    background: rgba(0, 0, 0, 0.1);
    padding: 2px 4px;
    border-radius: 4px;
}

[data-theme="dark"] code {
    background: rgba(255, 255, 255, 0.1);
}

/* Layout */
.app-container {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

.sidebar {
    width: 250px;
    background-color: var(--bg-surface);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    padding: 1.5rem;
    transition: background-color 0.3s;
    flex-shrink: 0;
}

.brand {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    color: var(--primary);
    margin-bottom: 2rem;
}

.brand h2 {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--text-main);
}

.nav-links {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    padding: 0;
    flex: 1;
}

.nav-links a {
    text-decoration: none;
    color: var(--text-muted);
    font-weight: 500;
    padding: 0.75rem 1rem;
    border-radius: 6px;
    display: block;
    transition: all 0.2s;
}

.nav-links a:hover {
    background-color: var(--bg-hover);
    color: var(--text-main);
}

.nav-links a.active {
    background-color: rgba(234, 179, 8, 0.1);
    color: var(--primary);
}

.theme-toggle {
    margin-top: auto;
}

.btn-icon {
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 0.5rem;
    border-radius: 4px;
}

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

/* Theme Icon logic */
[data-theme="dark"] .moon-icon {
    display: none;
}

[data-theme="light"] .sun-icon {
    display: none;
}

.main-content {
    flex: 1;
    overflow-y: auto;
    position: relative;
    background-color: var(--bg-app);
}

.view-section {
    display: none;
    padding: 2rem;
    max-width: 1200px;
    margin: 0 auto;
    height: 100%;
}

.view-section.active {
    display: block;
}

/* Dashboard */
.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1.5rem;
    margin-bottom: 3rem;
}

.stat-card {
    background-color: var(--bg-surface);
    border: 1px solid var(--border-color);
    padding: 1.5rem;
    border-radius: 12px;
    box-shadow: var(--card-shadow);
    text-align: center;
}

.stat-card h3 {
    font-size: 0.875rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 0.5rem;
}

.stat-value {
    font-size: 2.5rem;
    font-weight: 700;
    color: var(--primary);
}

.heatmap-container {
    background-color: var(--bg-surface);
    border: 1px solid var(--border-color);
    padding: 1.5rem;
    border-radius: 12px;
    box-shadow: var(--card-shadow);
}

.heatmap-container h3 {
    font-size: 1rem;
    margin-bottom: 1rem;
}

.heatmap {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
}

.day-square {
    width: 16px;
    height: 16px;
    border-radius: 3px;
    background-color: var(--bg-hover);
}

.day-square[data-level="1"] {
    background-color: rgba(37, 99, 235, 0.3);
}

[data-theme="dark"] .day-square[data-level="1"] {
    background-color: rgba(234, 179, 8, 0.3);
}

.day-square[data-level="2"] {
    background-color: rgba(37, 99, 235, 0.6);
}

[data-theme="dark"] .day-square[data-level="2"] {
    background-color: rgba(234, 179, 8, 0.6);
}

.day-square[data-level="3"] {
    background-color: rgba(37, 99, 235, 1);
}

[data-theme="dark"] .day-square[data-level="3"] {
    background-color: rgba(234, 179, 8, 1);
}

/* Study View */
#view-study {
    display: none;
    flex-direction: column;
}

#view-study.active {
    display: flex;
}

.study-header {
    padding-bottom: 1.5rem;
}

.progress-container {
    width: 100%;
    height: 6px;
    background: var(--border-color);
    border-radius: 3px;
    margin-bottom: 0.5rem;
    overflow: hidden;
}

.progress-bar {
    height: 100%;
    width: 0%;
    background: var(--primary);
    transition: width 0.3s ease;
}

.study-stats {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.85rem;
    color: var(--text-muted);
}

.flashcard-container {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    perspective: 1000px;
    margin-bottom: 2rem;
}

.flashcard {
    width: 100%;
    max-width: 600px;
    height: 400px;
    cursor: pointer;
}

.card-inner {
    position: relative;
    width: 100%;
    height: 100%;
    text-align: center;
    transition: transform 0.6s cubic-bezier(0.4, 0.2, 0.2, 1);
    transform-style: preserve-3d;
}

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

.card-front,
.card-back {
    position: absolute;
    width: 100%;
    height: 100%;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    background-color: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    box-shadow: var(--card-shadow);
    padding: 3rem 2rem;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    overflow-y: auto;
}

.card-back {
    transform: rotateY(180deg);
    background-color: var(--bg-surface);
}

.card-category {
    position: absolute;
    top: 1.5rem;
    left: 1.5rem;
    font-size: 0.75rem;
    text-transform: uppercase;
    font-weight: 700;
    color: var(--text-muted);
    letter-spacing: 0.05em;
}

.card-front h2 {
    font-size: 1.75rem;
    line-height: 1.4;
    color: var(--text-main);
}

.card-back h3 {
    font-size: 1rem;
    color: var(--text-muted);
    margin-bottom: 1rem;
}

.answer-content {
    font-size: 1.125rem;
    line-height: 1.6;
    text-align: left;
    width: 100%;
    max-width: 500px;
}

.study-controls {
    display: flex;
    justify-content: center;
    min-height: 60px;
    margin-bottom: 2rem;
}

.btn-large {
    font-size: 1.1rem;
    padding: 0.75rem 2rem;
}

.grade-controls {
    display: flex;
    gap: 1rem;
    flex-wrap: wrap;
    justify-content: center;
}

.btn-grade {
    font-weight: 600;
    padding: 0.75rem 1.5rem;
    color: #fff;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.bg-again {
    background-color: var(--grade-1);
}

.bg-again:hover {
    background-color: #DC2626;
}

.bg-hard {
    background-color: var(--grade-2);
}

.bg-hard:hover {
    background-color: #D97706;
}

.bg-good {
    background-color: var(--grade-3);
}

.bg-good:hover {
    background-color: #2563EB;
}

.bg-easy {
    background-color: var(--grade-4);
}

.bg-easy:hover {
    background-color: #059669;
}

.study-complete {
    text-align: center;
    margin: auto;
}

.study-complete h2 {
    font-size: 2rem;
    color: var(--primary);
    margin-bottom: 1rem;
}

.hidden {
    display: none !important;
}

.mt-4 {
    margin-top: 1.5rem;
}

/* Deck Table */
.filters {
    display: flex;
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.input-text,
.input-select {
    padding: 0.5rem 0.75rem;
    border-radius: 6px;
    border: 1px solid var(--border-color);
    background: var(--bg-surface);
    color: var(--text-main);
    font-family: var(--font-ui);
    outline: none;
}

.input-text:focus,
.input-select:focus {
    border-color: var(--primary);
}

#search-cards {
    flex: 1;
}

.table-container {
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    overflow-x: auto;
}

.card-table {
    width: 100%;
    border-collapse: collapse;
    text-align: left;
}

.card-table th,
.card-table td {
    padding: 1rem;
    border-bottom: 1px solid var(--border-color);
}

.card-table th {
    background: var(--bg-hover);
    font-weight: 600;
    font-size: 0.875rem;
    color: var(--text-muted);
    text-transform: uppercase;
}

.card-table tr:last-child td {
    border-bottom: none;
}

.card-table tbody tr:hover {
    background: rgba(0, 0, 0, 0.02);
}

[data-theme="dark"] .card-table tbody tr:hover {
    background: rgba(255, 255, 255, 0.02);
}

.td-q {
    max-width: 300px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.badge {
    font-size: 0.7rem;
    padding: 0.1rem 0.4rem;
    border-radius: 4px;
    background: var(--bg-hover);
    font-weight: 500;
}

/* Buttons */
.btn {
    font-family: var(--font-ui);
    font-size: 0.875rem;
    font-weight: 500;
    padding: 0.5rem 1rem;
    border-radius: 6px;
    border: 1px solid transparent;
    cursor: pointer;
    transition: all 0.2s;
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
}

.btn-primary {
    background: var(--primary);
    color: #fff;
}

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

.btn-outline {
    background: transparent;
    border-color: var(--border-color);
    color: var(--text-main);
}

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

.btn-danger {
    background: rgba(239, 68, 68, 0.1);
    color: var(--grade-1);
    border-color: rgba(239, 68, 68, 0.2);
}

.btn-danger:hover {
    background: var(--grade-1);
    color: #fff;
}

.header-actions {
    display: flex;
    gap: 0.5rem;
}

/* Modal Overlay */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    padding: 1rem;
    backdrop-filter: blur(2px);
}

.modal {
    background: var(--bg-surface);
    width: 100%;
    max-width: 500px;
    border-radius: 12px;
    border: 1px solid var(--border-color);
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.5);
    overflow: hidden;
}

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

.btn-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    color: var(--text-muted);
    cursor: pointer;
}

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

.modal-body {
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

.form-group label {
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--text-muted);
}

.modal-footer {
    padding: 1.25rem 1.5rem;
    border-top: 1px solid var(--border-color);
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
    background: var(--bg-app);
}

/* Toast */
.toast {
    position: fixed;
    bottom: 2rem;
    right: 2rem;
    background: var(--text-main);
    color: var(--bg-app);
    padding: 0.75rem 1.5rem;
    border-radius: 8px;
    font-weight: 500;
    font-size: 0.9rem;
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.3);
    transform: translateY(100px);
    opacity: 0;
    transition: all 0.3s;
    z-index: 2000;
}

.toast.show {
    transform: translateY(0);
    opacity: 1;
}

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

    .sidebar {
        width: 100%;
        padding: 1rem;
        flex-direction: row;
        align-items: center;
        gap: 1rem;
        border-right: none;
        border-bottom: 1px solid var(--border-color);
    }

    .nav-links {
        flex-direction: row;
        justify-content: center;
    }

    .nav-links a {
        padding: 0.5rem;
        font-size: 0.85rem;
    }

    .brand {
        margin-bottom: 0;
    }

    .brand h2 {
        display: none;
    }

    .view-section {
        padding: 1rem;
    }

    .flashcard {
        height: 350px;
    }

    .grade-controls {
        gap: 0.5rem;
    }

    .btn-grade {
        padding: 0.5rem 0.75rem;
        font-size: 0.8rem;
    }
}