:root {
    --primary-color: #0d6efd;
    --success-color: #198754;
    --info-color: #0dcaf0;
    --warning-color: #ffc107;
    --danger-color: #dc3545;
    --dark-color: #212529;
    --light-color: #f8f9fa;
    --border-radius: 0.5rem;
    --box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075);
    --transition: all 0.15s ease-in-out;
    --bg-color: #ffffff; 
    --text-color: #111; 
    --card-bg: #ffffff; 
    --muted-text: #6c757d;
    --appbar-bg: #ffffff; 
    --appbar-border: #e5e5e5;
    --card-font-size: 1.2rem;
    --card-font-family: 'Noto Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    --study-text-scale: 1;
    /* card text colors */
    --flash-front-text-color: #000000;
    --flash-back-text-color: #000000;
    /* @tweakable mobile quick stats gap (rem) */
    --qs-gap: .5rem;
    --qs-pad-x: .75rem;
    /* @tweakable Base animation duration for UI micro-interactions (ms) */
    --ui-anim-fast: 180ms;
    /* @tweakable Global border radius for cards and buttons (rem) */
    --ui-radius: 0.5rem;
    /* @tweakable Shadow strength (0 to 1) */
    --ui-shadow-strength: 0.12;
    /* @tweakable Hover brightness multiplier */
    --ui-hover-brightness: 1.03;
}

[data-theme="sepia"] {
    --bg-color: #f4ecd8; 
    --text-color: #2b2118; 
    --card-bg: #fff9e8;
    --appbar-bg: #fff6dd; 
    --appbar-border: #e1d2b8;
}

[data-theme="dark"] {
    /* @tweakable Background color for dark mode */
    --bg-color: #121212; 
    /* @tweakable Main text color for dark mode */
    --text-color: #e1e1e1; 
    /* @tweakable Card background color for dark mode */
    --card-bg: #1e1e1e;
    /* @tweakable App bar background for dark mode */
    --appbar-bg: #181818; 
    /* @tweakable Border color for dark mode */
    --appbar-border: #2a2a2a;
    /* @tweakable Muted text color for dark mode */
    --muted-text: #888;
    color-scheme: dark;

    --bs-body-color: var(--text-color);
    --bs-body-bg: var(--bg-color);
    --bs-secondary-bg: #2b2b2b;
    --bs-tertiary-bg: #222;
}

body {
    font-family: 'Noto Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    padding-top: 76px;
}

.navbar-brand {
    font-size: 1.5rem;
}

.appbar {
    background: var(--appbar-bg, #fff);
    border-bottom: 1px solid var(--appbar-border, #e5e5e5);
    height: auto;
    min-height: 64px;
}

.app-tab {
    color: var(--text-color, #1a1a1a);
    text-decoration: none;
    opacity: .8;
    border: none;
    background: none;
}

.app-tab.active, .app-tab:hover {
    opacity: 1;
}

.navbar.navbar-dark.bg-primary {
    background-color: #6b4f2a !important;
}

.card {
    border: 1px solid #e1d2b8;
    border-radius: var(--ui-radius);
    box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, var(--ui-shadow-strength));
    transition: var(--transition);
    background-color: var(--card-bg);
}

.card:hover {
    box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
    transform: translateY(-2px);
}

.btn {
    border-radius: var(--ui-radius);
    font-weight: 500;
    transition: filter var(--ui-anim-fast), background-color var(--ui-anim-fast), color var(--ui-anim-fast);
}

.btn:focus {
    box-shadow: 0 0 0 0.2rem rgba(13, 110, 253, 0.25);
}

.flashcard {
    width: 100%;
    height: 300px;
    perspective: 1000px;
    margin: 0 auto 2rem;
}

.flashcard-inner {
    position: relative;
    width: 100%;
    height: 100%;
    text-align: center;
    transition: transform 0.6s;
    transform-style: preserve-3d;
    cursor: pointer;
}

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

.flashcard.flipped-vertical .flashcard-inner {
    transform: rotateX(180deg);
}

.flashcard.flipped-vertical .flashcard-back {
    transform: rotateX(180deg);
}

.flashcard-front,
.flashcard-back {
    position: absolute;
    width: 100%;
    height: 100%;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    border: 2px solid var(--primary-color);
    border-radius: var(--border-radius);
    display: flex;
    align-items: center;
    justify-content: center;
    background: #fff9e8;
    color: var(--flash-front-text-color, #2b2118);
    border-color: #6b4f2a;
    padding: 2rem;
    font-size: var(--card-font-size);
    font-weight: 500;
    font-family: var(--card-font-family);
}

.flashcard-back {
    background-color: #d9c19b;
    color: var(--flash-back-text-color, #1c140b);
    transform: rotateX(180deg);
}

.flashcard-front:hover,
.flashcard-back:hover {
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
}

/* Ensure card images stretch to the max appropriate size while preserving aspect ratio */
/* @tweakable Max rendered image height inside cards (rem) */
.flashcard-front img,
.flashcard-back img,
.card-body img {
    display: block;
    margin: 0 auto;
    width: 100%;                  /* allow image to grow to available width */
    height: auto;                 /* preserve aspect ratio */
    max-width: 100%;
    max-height: calc(var(--card-image-max-height-rem, 6) * 1rem); /* controlled via tweakable */
    object-fit: contain;          /* contain within bounds without cropping */
}

.card-stack { position: relative; height: 360px; margin: 0 auto 1rem; perspective: 1000px; }
.stack-card { position: absolute; left: 0; right: 0; margin: 0 auto; width: 100%; max-width: 680px; height: 100%; transition: transform .35s ease, opacity .25s ease; }
.stack-card.prev { transform: translateY(calc(-1 * var(--stack-gap, 120px) - var(--stack-between, 1em))) scale(var(--stack-side-scale, .92)); opacity: .85; z-index: 1; }
.stack-card.current { transform: translateY(0) scale(1); z-index: 2; }
.stack-card.next { transform: translateY(calc(var(--stack-gap, 120px) + var(--stack-between, 1em))) scale(var(--stack-side-scale, .92)); opacity: .85; z-index: 1; }

.stack-card .flashcard-inner { position: relative; width: 100%; height: 100%; text-align: center; transition: transform 0.6s; transform-style: preserve-3d; cursor: pointer; }
.stack-card.flipped .flashcard-inner { transform: rotateY(180deg); }
.stack-card.flipped-vertical .flashcard-inner { transform: rotateX(180deg); }

.stack-card.current:not(.flipped):not(.flipped-vertical) .flashcard-front,
.flashcard:not(.flipped):not(.flipped-vertical) .flashcard-front {
    background-color: var(--flash-front-active-bg, #e3f9c2);
}
.stack-card.current.flipped .flashcard-back,
.stack-card.current.flipped-vertical .flashcard-back,
.flashcard.flipped .flashcard-back,
.flashcard.flipped-vertical .flashcard-back {
    background-color: var(--flash-back-active-bg, #e2e2fd);
}

.study-controls {
    display: flex;
    justify-content: center;
    gap: 1rem;
    margin-top: 2rem;
    flex-wrap: wrap;
}

.study-controls .btn {
    min-width: 120px;
}

.study-progress {
    margin-bottom: 2rem;
}

.progress {
    height: 8px;
    border-radius: 10px;
    background-color: #e9dcc3;
}

.progress-bar {
    border-radius: 10px;
    background-color: #6b4f2a;
    transition: width 0.3s ease;
}

.set-card {
    transition: var(--transition);
    border: 1px solid #dee2e6;
    border-radius: var(--border-radius);
    min-height: 150px; 
}

.set-card:hover {
    border-color: var(--primary-color);
    transform: translateY(-2px);
    filter: brightness(var(--ui-hover-brightness));
}

.set-card .card-header {
    background-color: transparent;
    border-bottom: 1px solid #dee2e6;
}

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

.set-actions .btn {
    flex: 1;
    min-width: 100px;
}

.achievement-badge {
    display: inline-block;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--warning-color), var(--success-color));
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;
    margin: 0.25rem;
    transition: var(--transition);
}

.achievement-badge:hover {
    transform: scale(1.1);
}

.achievement-badge.locked {
    background: #6c757d;
    opacity: 0.5;
}

.chart-container {
    position: relative;
    height: 300px;
    margin: 1rem 0;
}

#study-timer {
    position: fixed;
    top: 90px;
    right: 18px;
    z-index: 9998;
    transition: transform 0.2s ease, opacity 0.2s ease;
}
#study-timer .study-timer-inner button {
    display: flex;
    align-items: center;
    gap: .5rem;
}
#study-timer.minimized {
    top: auto;
    bottom: 18px;
    right: 18px;
    transform: translateY(0);
    opacity: 1;
}
#study-timer.minimized .study-timer-inner button {
    padding: .4rem .55rem;
    border-radius: 999px;
}

.blocks-grid {
    display: grid;
    gap: 0.5rem;
    grid-template-columns: repeat(3, 1fr);
    margin-top: 1rem;
}
.block-tile {
    height: auto;
    display:flex;
    align-items:flex-start;
    justify-content:center;
    border-radius: .5rem;
    border: 1px solid #ddd;
    background: #fff;
    cursor: pointer;
    user-select: none;
    padding: .5rem;
    transition: transform .12s ease, background .12s ease;
    overflow: hidden; /* moved scrolling into the inner element so clicks/taps register on the tile */
    white-space: normal;       /* enable wrapping */
    word-break: break-word;    /* break long words */
    text-align: center;
    touch-action: manipulation; /* @tweakable help mobile taps register while preventing passive-gesture delays */
}
.block-tile .inner {
    width: 100%;
    max-height: 100%;
    overflow: auto;                 /* allow scrolling inside the inner area only */
    -webkit-overflow-scrolling: touch;
    text-align: center;
}
.block-center {
    background: var(--mobile-motivational-bg, linear-gradient(90deg, #fffef6, #f0f7ff));
    border: 1px dashed #cfcfcf;
    font-weight: 600;
    color: #444;
    display:flex;
    align-items:center;
    justify-content:center;
    padding: .75rem;
    pointer-events: none;
}
.block-tile.matched {
    background: #fff;
    border: 2px solid #198754;
    box-shadow: 0 0 0 4px rgba(25,135,84,0.06);
}

.nav-icons .nav-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: var(--text-color);
    border: none;
    background: transparent;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: background 0.12s ease, transform 0.08s ease;
}
.nav-icons .nav-icon:active {
    transform: scale(0.98);
}
.nav-icons .nav-icon:hover, .nav-icons .nav-icon:focus {
    background: rgba(0,0,0,0.03);
    border-radius: 10px;
    filter: brightness(var(--ui-hover-brightness));
}

#activity-timer {
    font-weight: 700;
    text-decoration: underline;
}

@media (max-width: 991.98px) {
    body {
        padding-top: 120px;
    }
    
    .appbar {
        min-height: 80px;
    }
}

@media (max-width: 768px) {
    .flashcard {
        height: 250px;
    }
    
    .flashcard-front,
    .flashcard-back {
        padding: 1rem;
        font-size: 1rem;
    }
    
    .study-controls {
        gap: 0.5rem;
    }
    
    .study-controls .btn {
        min-width: 100px;
        font-size: 0.875rem;
    }
    
    .set-actions .btn {
        min-width: 80px;
        font-size: 0.875rem;
    }
}

@media (max-width: 576px) {
    .flashcard { 
        height: 220px; 
    }
    .flashcard-front, .flashcard-back { 
        font-size: calc(var(--card-font-size) * 0.9); 
    }

    .blocks-grid {
        grid-template-columns: repeat(2, 1fr); /* mobile: two columns */
    }

    /* hide the center motivational tile on mobile (removed from layout) */
    .blocks-grid #block-center {
        display: none !important;
    }

    /* use the motivational background for regular block tiles on mobile */
    .blocks-grid .block-tile {
        background: var(--mobile-motivational-bg, linear-gradient(90deg, #fffef6, #f0f7ff));
        border: 1px solid rgba(0,0,0,0.06);
    }

    /* @tweakable hide True/False badge on mobile */
    .tf-badge { display: none !important; }
}

@media (min-width: 1200px) {
    .flashcard { 
        height: 340px; 
    }
    .flashcard-front, .flashcard-back { 
        font-size: calc(var(--card-font-size) * 1.05); 
    }
}

.loading-dots {
    display: inline-block;
}

.loading-dots::after {
    content: '';
    animation: loading-dots 1.5s infinite;
}

@keyframes loading-dots {
    0%, 20% { content: ''; }
    40% { content: '.'; }
    60% { content: '..'; }
    80%, 100% { content: '...'; }
}

.feedback-correct {
    background-color: #d1e7dd;
    color: #0f5132;
    border: 1px solid #badbcc;
    border-radius: var(--border-radius);
    padding: 1rem;
    margin: 1rem 0;
}

.feedback-incorrect {
    background-color: #f8d7da;
    color: #842029;
    border: 1px solid #f5c2c7;
    border-radius: var(--border-radius);
    padding: 1rem;
    margin: 1rem 0;
}

.form-control:invalid {
    border-color: var(--danger-color);
}

.form-control:valid {
    border-color: var(--success-color);
}

@media (prefers-color-scheme: dark) {
    body {
        background-color: #1a1a1a;
        color: #ffffff;
    }
    
    .card {
        background-color: #2d2d2d;
        border-color: #404040;
    }
    
    .flashcard-front {
        background-color: #2d2d2d;
        color: #ffffff;
        border-color: var(--primary-color);
    }
    
    .modal-content {
        background-color: #2d2d2d;
        color: #ffffff;
    }
    
    .form-control,
    .form-select {
        background-color: #1f1f1f;
        color: #ffffff;
        border-color: #555;
    }
    
    .form-control::placeholder {
        color: #aaaaaa;
    }
}

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

.skip-link {
    position: absolute;
    top: -40px;
    left: 6px;
    background: var(--primary-color);
    color: white;
    padding: 8px;
    text-decoration: none;
    border-radius: 0 0 4px 4px;
    z-index: 1000;
    transition: top 0.3s;
}

.skip-link:focus {
    top: 0;
}

.btn:focus,
.form-control:focus,
.form-select:focus {
    outline: 2px solid var(--primary-color);
    outline-offset: 2px;
}

.fade-in {
    animation: fadeIn 0.3s ease-in;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.slide-up {
    animation: slideUp 0.3s ease-out;
}

@keyframes slideUp {
    from {
        transform: translateY(20px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.text-muted {
    color: var(--muted-text) !important;
}

.learn-back {
    background-color: var(--learn-back-bg, #e8f4ff) !important;
    color: #0b2e4f;
}

#study-view .lead,
#study-view .card-body p,
.blocks-grid .inner {
    font-size: calc(var(--card-font-size) * var(--study-text-scale, 1));
}

#quick-stats-carousel .carousel-control-prev, 
#quick-stats-carousel .carousel-control-next { 
    width: 12%; 
}

.card-image-full {
    opacity: 0;
    transition: opacity var(--ui-anim-fast) ease;
}
/* when loaded */
.card-image-full.is-loaded { opacity: 1; }