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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    line-height: 1.6;
    color: #333;
    background-color: #f5f5f5;
}

/* App Container */
.app {
    min-height: 100vh;
    position: relative;
}

/* Screen Management */
.screen {
    display: none;
    min-height: 100vh;
    padding: 1rem;
}

.screen.active {
    display: block;
}

.container {
    max-width: 100vw;
    width: 100%;
    margin: 0 auto;
    background: white;
    border-radius: 12px;
    padding: 1rem;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    min-height: calc(100vh - 2rem);
    overflow-x: hidden; /* Prevent horizontal scroll */
}

/* Responsive container sizing */
@media (min-width: 768px) {
    .container {
        max-width: 95vw;
        padding: 1.5rem;
    }
}

@media (min-width: 1024px) {
    .container {
        max-width: 90vw;
        padding: 2rem;
    }
}

/* Typography */
h1 {
    text-align: center;
    margin-bottom: 0;
    color: #2c3e50;
    font-size: 1.8rem;
}

h2 {
    text-align: center;
    margin-bottom: 1.5rem;
    color: #2c3e50;
    font-size: 1.5rem;
}

/* App Header */
.app-header {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 2rem;
    gap: 1rem;
}

.header-buttons {
    display: flex;
    gap: 0.75rem;
    flex-wrap: wrap;
    justify-content: center;
}

.header-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 0.75rem;
    border: none;
    border-radius: 6px;
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    text-decoration: none;
    color: white;
    min-height: 36px;
}

.star-btn {
    background: linear-gradient(135deg, #f39c12 0%, #e67e22 100%);
}

.star-btn:hover {
    background: linear-gradient(135deg, #e67e22 0%, #d35400 100%);
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.install-btn {
    background: linear-gradient(135deg, #27ae60 0%, #229954 100%);
}

.install-btn:hover {
    background: linear-gradient(135deg, #229954 0%, #1e8449 100%);
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.header-btn svg {
    flex-shrink: 0;
}

@media (max-width: 480px) {
    .app-header {
        gap: 0.75rem;
    }
    
    .header-buttons {
        gap: 0.5rem;
    }
    
    .header-btn {
        padding: 0.4rem 0.6rem;
        font-size: 0.8rem;
        min-height: 32px;
    }
    
    .header-btn svg {
        width: 14px;
        height: 14px;
    }
}

/* Settings Screen */
.settings-group {
    margin-bottom: 1.5rem;
}

.settings-group label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 600;
    color: #555;
}

.settings-group input {
    width: 100%;
    padding: 0.75rem;
    border: 2px solid #ddd;
    border-radius: 8px;
    font-size: 1rem;
    transition: border-color 0.3s ease;
}

.settings-group select {
    width: 100%;
    padding: 0.75rem;
    border: 2px solid #ddd;
    border-radius: 8px;
    font-size: 1rem;
    background-color: white;
    transition: border-color 0.3s ease;
}

.settings-group input:focus,
.settings-group select:focus {
    outline: none;
    border-color: #3498db;
}

/* Checkbox styling */
.settings-group input[type="checkbox"] {
    width: auto;
    margin-right: 0.5rem;
    transform: scale(1.2);
}

.settings-group label[for="show-keyboard"] {
    display: flex;
    align-items: center;
    cursor: pointer;
}

.settings-group label[for="show-keyboard"] span {
    user-select: none;
}

/* Buttons */
.btn-primary, .btn-secondary {
    width: 100%;
    padding: 1rem;
    border: none;
    border-radius: 8px;
    font-size: 1.1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    margin-bottom: 0.5rem;
}

.btn-primary {
    background-color: #3498db;
    color: white;
}

.btn-primary:hover {
    background-color: #2980b9;
    transform: translateY(-2px);
}

.btn-secondary {
    background-color: #95a5a6;
    color: white;
}

.btn-secondary:hover {
    background-color: #7f8c8d;
}

/* Exercise Screen */
.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
    padding: 1rem;
    background-color: #ecf0f1;
    border-radius: 8px;
}

.timer-display {
    font-size: 1.5rem;
    font-weight: bold;
    color: #e74c3c;
}

.progress {
    font-size: 1.2rem;
    font-weight: 600;
    color: #27ae60;
}

/* Exercises Container */
.exercises-container {
    max-height: 65vh;
    overflow-y: auto;
    overflow-x: hidden; /* Prevent horizontal scroll */
    margin-bottom: 1.5rem;
    padding: 0.5rem;
    display: grid;
    grid-template-columns: 1fr;
    gap: 1rem; /* Increased base gap for better tile spacing */
    width: 100%; /* Ensure full width usage */
}

/* Grid mode (default) - tiles layout with max 3 columns */
.exercises-container.grid-mode {
    grid-template-columns: 1fr;
    gap: 1rem;
}

/* Line mode - list layout */
.exercises-container.line-mode {
    display: block;
}

.exercises-container.line-mode .exercise-item {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    margin-bottom: 0.75rem;
    padding: 0.75rem;
    border: 1px solid #ecf0f1;
    border-radius: 8px;
    background-color: #fff;
    transition: all 0.3s ease;
    width: 100%;
}

.exercises-container.line-mode .exercise-question {
    min-width: 120px;
    margin-right: 1rem;
    font-size: 1.1rem;
}

.exercises-container.line-mode .exercise-input {
    width: 80px;
    margin-right: 1rem;
}

.exercises-container.line-mode .exercise-status {
    margin-left: auto;
}

.exercise-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem;
    margin-bottom: 0;
    background-color: #fff;
    border: 2px solid #ecf0f1;
    border-radius: 8px;
    transition: all 0.3s ease;
    min-height: 3rem;
}

.exercise-item:hover {
    border-color: #3498db;
    box-shadow: 0 2px 8px rgba(52, 152, 219, 0.2);
}

.exercise-item.correct {
    border-color: #27ae60;
    background-color: #d5f4e6;
}

.exercise-item.incorrect {
    border-color: #e74c3c;
    background-color: #fdeaea;
}

.exercise-question {
    font-size: 1.2rem;
    font-weight: 600;
    color: #2c3e50;
    flex: 1;
    white-space: nowrap;
}

.exercise-input {
    width: 80px;
    padding: 0.5rem;
    border: 2px solid #ddd;
    border-radius: 6px;
    text-align: center;
    font-size: 1.1rem;
    margin: 0 0.5rem;
}

.exercise-input:focus {
    outline: none;
    border-color: #3498db;
}

.exercise-input.keyboard-selected {
    border-color: #e74c3c;
    box-shadow: 0 0 0 2px rgba(231, 76, 60, 0.2);
    background-color: #fff5f5;
}

.exercise-input.keyboard-selected:focus {
    border-color: #c0392b;
    box-shadow: 0 0 0 2px rgba(192, 57, 43, 0.3);
}

.exercise-status {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    color: white;
}

.exercise-status.correct {
    background-color: #27ae60;
}

.exercise-status.incorrect {
    background-color: #e74c3c;
}

.exercise-status.pending {
    background-color: #bdc3c7;
}

/* Numerical Keyboard */
.numerical-keyboard {
    margin-top: 1rem;
    padding: 1rem;
    background-color: #f8f9fa;
    border-radius: 8px;
    border: 1px solid #e9ecef;
}

.keyboard-row {
    display: flex;
    justify-content: space-between;
    margin-bottom: 0.5rem;
    gap: 0.5rem;
}

.keyboard-row:last-child {
    margin-bottom: 0;
}

.keyboard-btn {
    flex: 1;
    height: 3rem;
    border: 2px solid #dee2e6;
    border-radius: 6px;
    background-color: #ffffff;
    color: #495057;
    font-size: 1.1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    /* Prevent keyboard buttons from stealing focus via tab navigation */
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

.keyboard-btn:hover {
    background-color: #e9ecef;
    border-color: #adb5bd;
    transform: translateY(-1px);
}

.keyboard-btn:active {
    transform: translateY(0);
    background-color: #dee2e6;
}

.keyboard-btn.number-btn {
    background-color: #ffffff;
}

.keyboard-btn.number-btn:hover {
    background-color: #f8f9fa;
    border-color: #3498db;
}

.keyboard-btn.action-btn {
    background-color: #3498db;
    color: white;
    border-color: #3498db;
}

.keyboard-btn.action-btn:hover {
    background-color: #2980b9;
    border-color: #2980b9;
}

.keyboard-btn.action-btn:active {
    background-color: #21618c;
}

/* Footer */
.footer {
    display: flex;
    gap: 0.5rem;
    margin-top: auto; /* Push footer to bottom when no keyboard */
}

.footer button {
    flex: 1;
}

/* Container adjustments for keyboard layout */
.container {
    display: flex;
    flex-direction: column;
    min-height: calc(100vh - 2rem);
}

.container.has-keyboard {
    min-height: auto; /* Reset when keyboard is present */
}

.container.has-keyboard .footer {
    margin-top: 1rem; /* Normal spacing when keyboard is present */
}

/* Results Screen */
.results {
    text-align: center;
    margin-bottom: 2rem;
}

.score {
    font-size: 3rem;
    font-weight: bold;
    color: #3498db;
    margin-bottom: 0.5rem;
}

.time-taken {
    font-size: 1.3rem;
    color: #7f8c8d;
    margin-bottom: 0.5rem;
}

.percentage {
    font-size: 2rem;
    font-weight: bold;
    color: #27ae60;
}

.highscore {
    font-size: 1.1rem;
    color: #8e44ad;
    margin-top: 0.5rem;
    font-weight: 500;
}

.new-highscore {
    font-size: 1.2rem;
    color: #e67e22;
    margin-top: 0.5rem;
    font-weight: bold;
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0% { transform: scale(1); }
    50% { transform: scale(1.05); }
    100% { transform: scale(1); }
}

/* Social Sharing */
.social-sharing {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 12px;
    padding: 1.5rem;
    margin-top: 1.5rem;
    text-align: center;
    border: 1px solid rgba(255, 255, 255, 0.1);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.social-sharing h3 {
    color: white;
    font-size: 1.3rem;
    margin-bottom: 1rem;
    font-weight: 600;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.share-buttons {
    display: flex;
    gap: 0.75rem;
    justify-content: center;
    flex-wrap: wrap;
}

.share-btn {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1rem;
    border: none;
    border-radius: 8px;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.3s ease;
    color: white;
    text-decoration: none;
    min-width: 100px;
    justify-content: center;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    position: relative;
    overflow: hidden;
}

.share-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;
}

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

.share-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2);
}

.share-btn:active {
    transform: translateY(0);
}

.share-btn svg {
    flex-shrink: 0;
}

.share-btn span {
    font-weight: 600;
}

.twitter-btn {
    background: linear-gradient(135deg, #1da1f2 0%, #0d8bd9 100%);
}

.twitter-btn:hover {
    background: linear-gradient(135deg, #0d8bd9 0%, #0a73b8 100%);
}

.facebook-btn {
    background: linear-gradient(135deg, #4267b2 0%, #365899 100%);
}

.facebook-btn:hover {
    background: linear-gradient(135deg, #365899 0%, #2d4373 100%);
}

.generic-btn {
    background: linear-gradient(135deg, #6c757d 0%, #5a6268 100%);
}

.generic-btn:hover {
    background: linear-gradient(135deg, #5a6268 0%, #495057 100%);
}
}



.results-details {
    max-height: 40vh;
    overflow-y: auto;
    margin-bottom: 1.5rem;
}

.result-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem;
    margin-bottom: 0.5rem;
    border-radius: 6px;
    font-size: 1rem;
}

.result-item.correct {
    background-color: #d5f4e6;
    color: #27ae60;
}

.result-item.incorrect {
    background-color: #fdeaea;
    color: #e74c3c;
}

.result-question {
    flex: 1;
}

.result-answer {
    font-weight: bold;
}

/* Responsive Design */
@media (min-width: 768px) {
    .container {
        padding: 2rem;
    }
    
    h1 {
        font-size: 2.2rem;
    }
    
    .exercises-container.grid-mode {
        max-height: 55vh;
        grid-template-columns: repeat(2, 1fr); /* 2 columns for tablets */
        gap: 1.25rem;
    }
    
    .exercise-item {
        padding: 1rem;
    }
    
    .exercise-question {
        font-size: 1.3rem;
    }
    
    .exercise-input {
        width: 100px;
        font-size: 1.2rem;
    }
    
    .keyboard-btn {
        height: 3.5rem;
        font-size: 1.2rem;
    }
    
    .share-buttons {
        gap: 1rem;
    }
    
    .share-btn {
        min-width: 120px;
    }
    
    /* Line mode responsive improvements */
    .exercises-container.line-mode .exercise-question {
        min-width: 150px;
        font-size: 1.3rem;
    }
    
    .exercises-container.line-mode .exercise-input {
        width: 100px;
        font-size: 1.2rem;
    }
}

/* Desktop layouts - maximum 3 columns to prevent horizontal overflow */
@media (min-width: 1024px) {
    .exercises-container.grid-mode {
        max-height: 65vh;
        grid-template-columns: repeat(3, 1fr); /* Max 3 columns for optimal fit */
        gap: 1.5rem;
    }
    
    /* Desktop keyboard layout - side by side with exercises */
    .container.has-keyboard {
        display: grid;
        grid-template-columns: 1fr 320px; /* Main content and keyboard sidebar */
        grid-template-rows: auto 1fr auto; /* Header, content, footer */
        grid-template-areas: 
            "header header"
            "exercises keyboard"
            "footer footer";
        gap: 1.5rem;
        max-height: calc(100vh - 2rem);
        overflow: hidden;
    }
    
    .container.has-keyboard .header {
        grid-area: header;
    }
    
    .container.has-keyboard .exercises-container {
        grid-area: exercises;
        max-height: calc(100vh - 200px); /* Full height minus header/footer space */
        overflow-y: auto;
    }
    
    .container.has-keyboard .numerical-keyboard {
        grid-area: keyboard;
        margin: 0;
        height: fit-content;
        max-height: calc(100vh - 200px);
        overflow-y: auto;
        position: sticky;
        top: 0;
    }
    
    .container.has-keyboard .footer {
        grid-area: footer;
        margin-top: 0;
    }
}



/* Mobile-specific keyboard adjustments */
@media (max-width: 767px) {
    .numerical-keyboard {
        position: sticky;
        bottom: 0;
        margin: 0.5rem -1.5rem -1.5rem -1.5rem;
        border-radius: 0;
        border: none;
        border-top: 1px solid #e9ecef;
        background-color: #ffffff;
        box-shadow: 0 -2px 10px rgba(0,0,0,0.1);
        padding: 1rem;
    }
    
    .keyboard-btn {
        height: 2.8rem;
        font-size: 1rem;
    }
    
    /* Make header sticky on mobile when no keyboard */
    .header {
        position: sticky;
        top: 0;
        z-index: 100;
        margin-bottom: 0.5rem;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    
    /* Make footer sticky on mobile when no keyboard */
    .footer {
        position: sticky;
        bottom: 0;
        background-color: #ffffff;
        padding: 1rem;
        margin: 0 -1.5rem -1.5rem -1.5rem;
        box-shadow: 0 -2px 4px rgba(0,0,0,0.1);
        z-index: 100;
    }
    
    /* Maximize exercises area when keyboard is not present */
    .exercises-container {
        max-height: calc(75vh - 120px); /* Account for sticky header and footer */
        margin-bottom: 0;
        flex: 1;
    }
    
    /* Constrain exercises area when keyboard is present */
    .exercises-container.has-keyboard {
        max-height: calc(60vh - 140px); /* Account for sticky header, footer, and keyboard */
    }
    
    /* Reset sticky positioning when keyboard is present */
    .container.has-keyboard .header {
        position: relative;
        top: auto;
        box-shadow: none;
        margin-bottom: 1.5rem;
    }
    
    .container.has-keyboard .footer {
        position: relative;
        bottom: auto;
        background-color: transparent;
        padding: 0;
        margin: 1rem 0 0 0;
        box-shadow: none;
    }
}

/* Animation for timer warning */
@keyframes pulse {
    0% { color: #e74c3c; }
    50% { color: #c0392b; }
    100% { color: #e74c3c; }
}

.timer-warning {
    animation: pulse 1s infinite;
}

/* Loading and transitions */
.fade-in {
    animation: fadeIn 0.5s ease-in;
}

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

/* Source Code Footer */
.source-footer {
    position: fixed;
    bottom: 10px;
    right: 10px;
    z-index: 1000;
}

.footer-links {
    display: flex;
    flex-direction: column;
    gap: 8px;
    align-items: flex-end;
}

.source-link {
    display: inline-flex;
    align-items: center;
    padding: 6px 12px;
    background-color: rgba(52, 152, 219, 0.1);
    border: 1px solid rgba(52, 152, 219, 0.3);
    border-radius: 15px;
    color: #3498db;
    text-decoration: none;
    font-size: 0.8rem;
    font-weight: 500;
    transition: all 0.2s ease;
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
}

.source-link:hover {
    background-color: rgba(52, 152, 219, 0.2);
    border-color: rgba(52, 152, 219, 0.5);
    color: #2980b9;
    transform: translateY(-1px);
    box-shadow: 0 2px 8px rgba(52, 152, 219, 0.2);
}

.source-link:active {
    transform: translateY(0);
    box-shadow: 0 1px 4px rgba(52, 152, 219, 0.2);
}

/* Star button in footer - more subtle styling */
.star-footer-btn {
    background-color: rgba(241, 196, 15, 0.1) !important;
    border-color: rgba(241, 196, 15, 0.3) !important;
    color: #f1c40f !important;
    border: 1px solid !important;
    cursor: pointer;
    font-family: inherit;
    gap: 6px;
}

.star-footer-btn:hover {
    background-color: rgba(241, 196, 15, 0.2) !important;
    border-color: rgba(241, 196, 15, 0.5) !important;
    color: #e67e22 !important;
}

.star-footer-btn svg {
    flex-shrink: 0;
}

.version-info {
    display: inline-flex;
    align-items: center;
    padding: 4px 8px;
    background-color: rgba(149, 165, 166, 0.1);
    border: 1px solid rgba(149, 165, 166, 0.3);
    border-radius: 12px;
    color: #7f8c8d;
    font-size: 0.75rem;
    font-weight: 400;
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
}

/* Mobile adjustments for source footer */
@media (max-width: 767px) {
    .source-footer {
        bottom: 5px;
        right: 5px;
    }
    
    .footer-links {
        gap: 6px;
    }
    
    .source-link {
        font-size: 0.75rem;
        padding: 5px 10px;
    }
    
    .version-info {
        font-size: 0.7rem;
        padding: 3px 6px;
    }
}