:root {
    --bg: var(--tg-theme-bg-color, #ffffff);
    --text: var(--tg-theme-text-color, #000000);
    --hint: var(--tg-theme-hint-color, #999999);
    --link: var(--tg-theme-link-color, #2678b6);
    --btn: var(--tg-theme-button-color, #3390ec);
    --btn-text: var(--tg-theme-button-text-color, #ffffff);
    --secondary-bg: var(--tg-theme-secondary-bg-color, #f0f0f0);
    --section-bg: var(--tg-theme-section-bg-color, var(--bg));
    --section-header: var(--tg-theme-section-header-text-color, var(--hint));
    --accent: var(--tg-theme-accent-text-color, var(--link));
    --destructive: var(--tg-theme-destructive-text-color, #e53935);
    --subtitle: var(--tg-theme-subtitle-text-color, var(--hint));
    --radius: 12px;
    --radius-sm: 8px;
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased;
}

#app {
    max-width: 480px;
    margin: 0 auto;
    padding: 0 16px 80px;
}

/* ========== NAVIGATION ========== */

nav#nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    background: var(--secondary-bg);
    border-top: 1px solid var(--hint);
    z-index: 100;
    padding: 4px 0;
    justify-content: center;
    gap: 0;
}

.nav-btn {
    flex: 1;
    max-width: 120px;
    background: none;
    border: none;
    padding: 8px 4px;
    font-size: 12px;
    color: var(--hint);
    cursor: pointer;
    transition: color 0.2s;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
}

.nav-btn.active {
    color: var(--accent);
    font-weight: 600;
}

.nav-btn.hidden {
    display: none;
}

/* ========== PAGES ========== */

.page {
    display: none;
    padding-top: 16px;
    animation: fadeIn 0.25s ease;
}

.page.active {
    display: block;
}

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

.hidden {
    display: none !important;
}

h2 {
    font-size: 22px;
    font-weight: 700;
    margin-bottom: 8px;
    text-align: center;
}

.subtitle {
    text-align: center;
    color: var(--subtitle);
    font-size: 14px;
    margin-bottom: 20px;
}

/* ========== FORMS ========== */

.form-group {
    margin-bottom: 14px;
}

.form-group label {
    display: block;
    font-size: 13px;
    font-weight: 600;
    color: var(--section-header);
    margin-bottom: 4px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

select,
input[type="text"],
textarea {
    width: 100%;
    padding: 10px 12px;
    font-size: 15px;
    border: 1px solid var(--hint);
    border-radius: var(--radius-sm);
    background: var(--secondary-bg);
    color: var(--text);
    outline: none;
    transition: border-color 0.2s;
    -webkit-appearance: none;
}

select:focus,
input[type="text"]:focus,
textarea:focus {
    border-color: var(--accent);
}

textarea {
    resize: vertical;
    font-family: inherit;
}

/* ========== BUTTONS ========== */

.btn-primary {
    display: block;
    width: 100%;
    padding: 14px;
    font-size: 16px;
    font-weight: 600;
    border: none;
    border-radius: var(--radius);
    background: var(--btn);
    color: var(--btn-text);
    cursor: pointer;
    transition: opacity 0.2s, transform 0.1s;
    margin-top: 8px;
}

.btn-primary:active {
    transform: scale(0.98);
    opacity: 0.85;
}

.btn-primary:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-sm {
    padding: 8px 16px;
    font-size: 14px;
    font-weight: 600;
    border: none;
    border-radius: var(--radius-sm);
    background: var(--btn);
    color: var(--btn-text);
    cursor: pointer;
}

.btn-danger {
    background: var(--destructive);
    color: #fff;
    border: none;
    padding: 6px 12px;
    border-radius: var(--radius-sm);
    font-size: 13px;
    cursor: pointer;
}

/* ========== TOGGLE GROUP ========== */

.toggle-group {
    display: flex;
    gap: 0;
    border-radius: var(--radius-sm);
    overflow: hidden;
    border: 1px solid var(--hint);
}

.toggle-btn {
    flex: 1;
    padding: 10px;
    font-size: 14px;
    font-weight: 600;
    border: none;
    background: var(--secondary-bg);
    color: var(--hint);
    cursor: pointer;
    transition: all 0.2s;
}

.toggle-btn.active {
    background: var(--btn);
    color: var(--btn-text);
}

/* ========== QUIZ PLAY ========== */

.quiz-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
    font-size: 14px;
    font-weight: 600;
    color: var(--subtitle);
    flex-wrap: wrap;
    gap: 4px;
}

.timer-bar {
    width: 100%;
    height: 4px;
    background: var(--secondary-bg);
    border-radius: 2px;
    overflow: hidden;
    margin-top: 4px;
}

#timer-fill {
    height: 100%;
    background: var(--btn);
    border-radius: 2px;
    width: 100%;
    transition: width 0.3s linear;
}

#timer-fill.warning {
    background: #ff9800;
}

#timer-fill.danger {
    background: var(--destructive);
}

.question-text {
    font-size: 17px;
    font-weight: 500;
    line-height: 1.5;
    margin-bottom: 16px;
    padding: 16px;
    background: var(--secondary-bg);
    border-radius: var(--radius);
}

.options-grid {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.option-btn {
    display: block;
    width: 100%;
    text-align: left;
    padding: 14px 16px;
    font-size: 15px;
    border: 2px solid var(--hint);
    border-radius: var(--radius);
    background: var(--section-bg);
    color: var(--text);
    cursor: pointer;
    transition: all 0.2s;
}

.option-btn:active {
    transform: scale(0.98);
}

.option-btn.selected {
    border-color: var(--btn);
    background: var(--btn);
    color: var(--btn-text);
}

.option-btn.correct {
    border-color: #4caf50;
    background: #4caf50;
    color: #fff;
}

.option-btn.wrong {
    border-color: var(--destructive);
    background: var(--destructive);
    color: #fff;
}

.option-btn:disabled {
    cursor: default;
    opacity: 0.9;
}

/* Word input */
#q-word-input {
    display: flex;
    gap: 8px;
    margin-top: 8px;
}

#q-word-input input {
    flex: 1;
}

#q-word-input .btn-primary {
    width: auto;
    margin-top: 0;
    padding: 10px 20px;
}

.feedback {
    margin-top: 12px;
    padding: 12px 16px;
    border-radius: var(--radius-sm);
    font-size: 15px;
    font-weight: 600;
    text-align: center;
}

.feedback.correct-fb {
    background: rgba(76, 175, 80, 0.15);
    color: #4caf50;
}

.feedback.wrong-fb {
    background: rgba(229, 57, 53, 0.15);
    color: var(--destructive);
}

/* ========== RESULT ========== */

.result-score {
    text-align: center;
    font-size: 48px;
    font-weight: 800;
    color: var(--accent);
    margin: 20px 0;
}

#result-details {
    margin: 16px 0;
}

.result-item {
    padding: 10px 12px;
    margin-bottom: 6px;
    border-radius: var(--radius-sm);
    background: var(--secondary-bg);
    font-size: 14px;
    line-height: 1.4;
}

.result-item.correct-item {
    border-left: 3px solid #4caf50;
}

.result-item.wrong-item {
    border-left: 3px solid var(--destructive);
}

.result-item .ri-question {
    font-weight: 600;
    margin-bottom: 4px;
}

.result-item .ri-answer {
    color: var(--subtitle);
    font-size: 13px;
}

/* ========== LEADERBOARD ========== */

.lb-toggle {
    margin: 12px 0 16px;
}

.lb-list {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.lb-row {
    display: flex;
    align-items: center;
    padding: 10px 12px;
    background: var(--secondary-bg);
    border-radius: var(--radius-sm);
    gap: 12px;
}

.lb-row.me {
    background: var(--btn);
    color: var(--btn-text);
}

.lb-rank {
    font-size: 16px;
    font-weight: 800;
    min-width: 28px;
    text-align: center;
}

.lb-name {
    flex: 1;
    font-size: 15px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.lb-score {
    font-size: 15px;
    font-weight: 700;
}

.lb-myrank {
    margin-top: 12px;
    padding: 12px;
    text-align: center;
    font-size: 14px;
    font-weight: 600;
    color: var(--accent);
    background: var(--secondary-bg);
    border-radius: var(--radius-sm);
}

/* ========== PROFILE ========== */

.profile-card {
    margin-top: 16px;
    padding: 24px 16px;
    background: var(--secondary-bg);
    border-radius: var(--radius);
    text-align: center;
}

.profile-name {
    font-size: 20px;
    font-weight: 700;
    margin-bottom: 20px;
}

.profile-stats {
    display: flex;
    justify-content: center;
    gap: 32px;
}

.stat {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.stat-value {
    font-size: 28px;
    font-weight: 800;
    color: var(--accent);
}

.stat-label {
    font-size: 13px;
    color: var(--subtitle);
    margin-top: 4px;
}

/* ========== ADMIN ========== */

.admin-stats {
    display: flex;
    gap: 12px;
    margin-bottom: 16px;
}

.admin-stats .stat-card {
    flex: 1;
    padding: 14px;
    background: var(--secondary-bg);
    border-radius: var(--radius-sm);
    text-align: center;
}

.admin-stats .stat-card .stat-value {
    font-size: 24px;
    font-weight: 800;
    color: var(--accent);
}

.admin-stats .stat-card .stat-label {
    font-size: 12px;
    color: var(--subtitle);
}

.admin-section {
    margin-top: 20px;
    padding: 16px;
    background: var(--secondary-bg);
    border-radius: var(--radius);
}

.admin-section h3 {
    font-size: 16px;
    font-weight: 700;
    margin-bottom: 12px;
}

.search-row {
    display: flex;
    gap: 8px;
    margin-bottom: 12px;
}

.search-row input {
    flex: 1;
}

.search-row select {
    width: 80px;
}

#admin-results {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.adm-q-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 10px;
    background: var(--bg);
    border-radius: var(--radius-sm);
    font-size: 13px;
    gap: 8px;
}

.adm-q-item .adm-q-text {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.adm-q-item .adm-q-meta {
    color: var(--subtitle);
    font-size: 11px;
    white-space: nowrap;
}

/* ========== LOADER ========== */

.loader {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg);
    z-index: 999;
    transition: opacity 0.3s;
}

.loader.hidden {
    opacity: 0;
    pointer-events: none;
}

.spinner {
    width: 36px;
    height: 36px;
    border: 3px solid var(--secondary-bg);
    border-top-color: var(--btn);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}
