* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

:root {
    --accent: #00ded6;
    --bg: #0a0a0f;
    --card-bg: rgba(255, 255, 255, 0.06);
    --card-border: rgba(255, 255, 255, 0.08);
    --text: #fff;
    --text-dim: rgba(255, 255, 255, 0.45);
    --text-mid: rgba(255, 255, 255, 0.7);
    --green: #34c759;
    --yellow: #ffd60a;
    --red: #ff453a;
    --radius: 16px;

    /* Telegram Web App header offset — updated by JS */
    --tg-top: 0px;
    /* Bottom nav height */
    --nav-height: 64px;
}

html, body {
    width: 100%;
    height: 100%;
    height: 100dvh;
    overflow: hidden;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, 'SF Pro Rounded', 'Helvetica Neue', sans-serif;
    -webkit-font-smoothing: antialiased;
}

.screen {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    height: 100dvh;
    flex-direction: column;
    z-index: 1;
}

.screen.active {
    display: flex;
}

/* Generic scrollable area inside screens */
.screen-scroll {
    width: 100%;
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 20px;
    padding-top: calc(var(--tg-top) + 12px);
    padding-bottom: calc(var(--nav-height) + env(safe-area-inset-bottom, 0px) + 16px);
    position: relative;
    z-index: 1;
}

/* Screen header */
.screen-header {
    margin-bottom: 16px;
}

.screen-header-title {
    font-size: clamp(24px, 7vw, 32px);
    font-weight: 800;
    letter-spacing: -0.5px;
}

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

.bottom-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: calc(var(--nav-height) + env(safe-area-inset-bottom, 0px));
    padding-bottom: env(safe-area-inset-bottom, 0px);
    background: rgba(10, 10, 15, 0.92);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-top: 1px solid rgba(255, 255, 255, 0.08);
    display: flex;
    align-items: center;
    justify-content: space-around;
    z-index: 50;
}

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

.nav-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    padding: 6px 16px;
    background: none;
    border: none;
    color: var(--text-dim);
    cursor: pointer;
    font-family: inherit;
    -webkit-appearance: none;
    transition: color 0.2s;
    touch-action: manipulation;
}

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

.nav-icon {
    font-size: 22px;
    line-height: 1.2;
}

.nav-label {
    font-size: 10px;
    font-weight: 600;
    letter-spacing: 0.2px;
}

/* ========== MENU ========== */

#screen-menu .screen-scroll {
    /* Menu uses the generic scroll */
}

.menu-header {
    margin-bottom: 28px;
}

.menu-title {
    font-size: clamp(28px, 8vw, 36px);
    font-weight: 800;
    letter-spacing: -0.5px;
}

.menu-subtitle {
    font-size: clamp(14px, 4vw, 17px);
    font-weight: 500;
    color: var(--text-dim);
    margin-top: 6px;
}

.menu-cards {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.exercise-card {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 14px;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: var(--radius);
    cursor: pointer;
    color: var(--text);
    text-align: left;
    font-family: inherit;
    font-size: inherit;
    transition: background 0.15s;
    -webkit-appearance: none;
    appearance: none;
    touch-action: manipulation;
    position: relative;
    z-index: 2;
}

.exercise-card:active {
    background: rgba(255, 255, 255, 0.1);
}

.card-icon {
    width: 48px;
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    background: rgba(0, 222, 214, 0.12);
    border-radius: 12px;
    flex-shrink: 0;
}

.card-text {
    display: flex;
    flex-direction: column;
    gap: 2px;
    flex: 1;
    min-width: 0;
}

.card-name {
    font-size: clamp(15px, 4.5vw, 18px);
    font-weight: 600;
}

.card-desc {
    font-size: clamp(12px, 3.5vw, 14px);
    color: var(--text-dim);
}

.card-chevron {
    font-size: 20px;
    font-weight: 600;
    color: rgba(255, 255, 255, 0.3);
    flex-shrink: 0;
}

/* ========== WORKOUT ========== */

.workout-container {
    position: relative;
    width: 100%;
    height: 100%;
    background: #000;
    overflow: hidden;
}

#camera-feed {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    transform: scaleX(-1);
}

#pose-canvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.workout-hud {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    padding: 16px;
    padding-top: calc(var(--tg-top) + 8px);
    padding-bottom: 16px;
    pointer-events: none;
    z-index: 10;
}

.workout-hud > * {
    pointer-events: auto;
}

.hud-top {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
}

.hud-left {
    display: flex;
    flex-direction: column;
    gap: 2px;
    max-width: 65%;
}

.hud-primary {
    font-size: clamp(40px, 14vw, 64px);
    font-weight: 800;
    text-shadow: 0 2px 12px rgba(0, 0, 0, 0.7);
    line-height: 1;
}

.hud-phase {
    font-size: clamp(13px, 3.8vw, 16px);
    font-weight: 600;
    color: rgba(255, 255, 255, 0.9);
    text-shadow: 0 1px 6px rgba(0, 0, 0, 0.7);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

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

.hud-timer {
    font-size: clamp(16px, 5vw, 22px);
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    color: rgba(255, 255, 255, 0.85);
    text-shadow: 0 1px 6px rgba(0, 0, 0, 0.7);
}

.hud-quality-dot {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: #888;
    border: 1px solid rgba(255, 255, 255, 0.4);
    transition: background 0.2s;
}

.hud-quality-dot.good { background: var(--green); box-shadow: 0 0 8px rgba(52, 199, 89, 0.6); }
.hud-quality-dot.acceptable { background: var(--yellow); box-shadow: 0 0 8px rgba(255, 214, 10, 0.6); }
.hud-quality-dot.poor { background: var(--red); box-shadow: 0 0 8px rgba(255, 69, 58, 0.6); }

.hud-guidance {
    margin: auto;
    text-align: center;
    font-size: clamp(13px, 3.8vw, 15px);
    font-weight: 500;
    color: rgba(255, 255, 255, 0.95);
    padding: 14px 20px;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border-radius: 14px;
    max-width: min(300px, 85vw);
    white-space: pre-line;
}

.hud-guidance.hidden {
    display: none;
}

.hud-bottom {
    margin-top: auto;
    display: flex;
    gap: 12px;
    align-items: center;
    padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 20px);
}

.btn-circle {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.15);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: none;
    color: #fff;
    font-size: 18px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    -webkit-appearance: none;
}

.btn-circle:active {
    background: rgba(255, 255, 255, 0.25);
}

.btn-stop {
    flex: 1;
    height: 50px;
    border-radius: 14px;
    background: rgb(230, 64, 64);
    border: none;
    color: #fff;
    font-size: clamp(15px, 4.2vw, 17px);
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    -webkit-appearance: none;
}

.btn-stop:active {
    background: rgb(200, 50, 50);
}

/* ========== RESULTS ========== */

#screen-results {
    overflow: hidden;
}

.results-scroll {
    width: 100%;
    height: 100%;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 20px;
    padding-top: calc(var(--tg-top) + 8px);
    padding-bottom: calc(var(--nav-height) + env(safe-area-inset-bottom, 0px) + 24px);
}

.results-header {
    text-align: center;
    margin-bottom: 8px;
}

.results-header h2 {
    font-size: clamp(20px, 6vw, 26px);
    font-weight: 700;
}

.results-header p {
    font-size: clamp(13px, 3.6vw, 15px);
    color: var(--text-dim);
    margin-top: 6px;
}

.results-hero {
    text-align: center;
    padding: 16px 0;
}

.hero-value {
    font-size: clamp(48px, 18vw, 80px);
    font-weight: 800;
    color: var(--accent);
    line-height: 1;
}

.hero-label {
    font-size: clamp(14px, 4.2vw, 18px);
    font-weight: 500;
    color: var(--text-dim);
    margin-top: 4px;
}

.stats-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-bottom: 14px;
}

.stat-card {
    padding: 14px;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: var(--radius);
}

.stat-card-title {
    font-size: clamp(11px, 3.2vw, 13px);
    font-weight: 500;
    color: var(--text-dim);
    margin-bottom: 6px;
}

.stat-card-value {
    font-size: clamp(22px, 6.5vw, 28px);
    font-weight: 700;
    line-height: 1.1;
}

/* Form distribution */
.form-distribution {
    padding: 14px;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: var(--radius);
    margin-bottom: 14px;
}

.form-dist-title {
    font-size: clamp(13px, 3.6vw, 15px);
    font-weight: 600;
    color: var(--text-mid);
    margin-bottom: 10px;
}

.form-bar {
    display: flex;
    gap: 2px;
    height: 12px;
    border-radius: 6px;
    overflow: hidden;
    margin-bottom: 10px;
}

.form-bar-segment {
    border-radius: 4px;
    min-width: 8px;
}

.form-bar-segment.good { background: var(--green); }
.form-bar-segment.acceptable { background: var(--yellow); }
.form-bar-segment.poor { background: var(--red); }

.form-legend {
    display: flex;
    gap: 14px;
    flex-wrap: wrap;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 5px;
    font-size: clamp(11px, 3.2vw, 13px);
    font-weight: 500;
    color: rgba(255, 255, 255, 0.6);
}

.legend-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
}

.legend-dot.good { background: var(--green); }
.legend-dot.acceptable { background: var(--yellow); }
.legend-dot.poor { background: var(--red); }

/* Rep / hold list */
.rep-list-section {
    padding: 14px;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: var(--radius);
    margin-bottom: 14px;
}

.rep-list-title {
    font-size: clamp(13px, 3.6vw, 15px);
    font-weight: 600;
    color: var(--text-mid);
    margin-bottom: 10px;
}

.rep-row {
    display: flex;
    align-items: center;
    height: 20px;
    margin-bottom: 6px;
    gap: 8px;
}

.rep-id {
    font-size: 14px;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    color: rgba(255, 255, 255, 0.5);
    width: 32px;
    flex-shrink: 0;
}

.rep-bar-wrap {
    flex: 1;
    height: 8px;
    background: rgba(255, 255, 255, 0.06);
    border-radius: 3px;
    overflow: hidden;
}

.rep-bar-fill {
    height: 100%;
    border-radius: 3px;
    transition: width 0.3s;
}

.rep-bar-fill.good { background: rgba(52, 199, 89, 0.7); }
.rep-bar-fill.acceptable { background: rgba(255, 214, 10, 0.7); }
.rep-bar-fill.poor { background: rgba(255, 69, 58, 0.7); }

.rep-value {
    font-size: 13px;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    color: rgba(255, 255, 255, 0.6);
    width: 44px;
    text-align: right;
    flex-shrink: 0;
}

.rep-quality-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
}

.btn-primary {
    width: 100%;
    height: 50px;
    border-radius: var(--radius);
    background: var(--accent);
    border: none;
    color: #000;
    font-size: clamp(15px, 4.2vw, 17px);
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    -webkit-appearance: none;
    margin-top: 10px;
}

.btn-primary:active {
    opacity: 0.85;
}

/* ========== TAB ROWS ========== */

.tab-row {
    display: flex;
    gap: 6px;
    margin-bottom: 12px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 2px;
}

.tab-row::-webkit-scrollbar {
    display: none;
}

.tab-row-secondary {
    margin-bottom: 16px;
}

.tab-btn {
    padding: 7px 14px;
    border-radius: 20px;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    color: var(--text-dim);
    font-size: clamp(12px, 3.5vw, 14px);
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    white-space: nowrap;
    flex-shrink: 0;
    -webkit-appearance: none;
    transition: all 0.2s;
}

.tab-btn.active {
    background: var(--accent);
    border-color: var(--accent);
    color: #000;
}

.tab-btn:active {
    opacity: 0.8;
}

.tab-content {
    min-height: 200px;
}

/* ========== SECTION CARDS (Stats / Leaderboard) ========== */

.section-card {
    padding: 14px;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: var(--radius);
    margin-bottom: 14px;
}

.section-title {
    font-size: clamp(13px, 3.6vw, 15px);
    font-weight: 600;
    color: var(--text-mid);
    margin-bottom: 10px;
}

/* ========== HEATMAP ========== */

.heatmap-grid {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 4px;
}

.heatmap-cell {
    aspect-ratio: 1;
    border-radius: 4px;
    background: rgba(255, 255, 255, 0.04);
}

.heatmap-cell.low {
    background: rgba(0, 222, 214, 0.2);
}

.heatmap-cell.mid {
    background: rgba(0, 222, 214, 0.45);
}

.heatmap-cell.high {
    background: rgba(0, 222, 214, 0.8);
}

/* ========== PERSONAL BESTS ========== */

.pb-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.pb-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    font-size: clamp(13px, 3.6vw, 15px);
}

.pb-row:last-child {
    border-bottom: none;
    padding-bottom: 0;
}

.pb-value {
    font-weight: 700;
    color: var(--accent);
    font-variant-numeric: tabular-nums;
}

/* ========== RECENT WORKOUTS ========== */

.recent-workout-row {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.recent-workout-row:last-child {
    border-bottom: none;
    padding-bottom: 0;
}

.rw-icon {
    font-size: 18px;
    flex-shrink: 0;
}

.rw-value {
    font-size: clamp(14px, 4vw, 16px);
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    min-width: 60px;
}

.rw-dots {
    flex: 1;
    display: flex;
    gap: 3px;
    align-items: center;
}

.rw-date {
    font-size: clamp(11px, 3vw, 13px);
    color: var(--text-dim);
    font-variant-numeric: tabular-nums;
    flex-shrink: 0;
}

.quality-mini {
    display: inline-block;
    width: 6px;
    height: 6px;
    border-radius: 50%;
}

.quality-mini.good { background: var(--green); }
.quality-mini.acceptable { background: var(--yellow); }
.quality-mini.poor { background: var(--red); }

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

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

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

.lb-row-top {
    background: rgba(0, 222, 214, 0.06);
    border-color: rgba(0, 222, 214, 0.15);
}

.lb-row-me {
    background: rgba(0, 222, 214, 0.1);
    border-color: rgba(0, 222, 214, 0.3);
}

.lb-rank {
    font-size: clamp(14px, 4vw, 17px);
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    color: var(--text-dim);
    min-width: 28px;
    text-align: center;
    flex-shrink: 0;
}

.lb-row-top .lb-rank {
    font-size: clamp(18px, 5vw, 22px);
}

.lb-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    flex-shrink: 0;
    object-fit: cover;
}

.lb-avatar-placeholder {
    background: rgba(0, 222, 214, 0.2);
    color: var(--accent);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 15px;
    font-weight: 700;
}

.lb-info {
    flex: 1;
    min-width: 0;
}

.lb-name {
    font-size: clamp(13px, 3.8vw, 15px);
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.lb-workouts {
    font-size: clamp(11px, 3vw, 12px);
    color: var(--text-dim);
}

.lb-value {
    font-size: clamp(15px, 4.5vw, 18px);
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    color: var(--accent);
    flex-shrink: 0;
}

.lb-my-position {
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px dashed rgba(255, 255, 255, 0.1);
}

/* ========== EMPTY STATE ========== */

.empty-state {
    text-align: center;
    padding: 40px 20px;
    color: var(--text-dim);
    font-size: clamp(14px, 4vw, 16px);
    line-height: 1.5;
}

/* ========== LOADING (inline) ========== */

.loading-inline {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 40px 0;
}

/* Loading overlay */
.loading-overlay {
    position: fixed;
    inset: 0;
    background: var(--bg);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
    z-index: 100;
}

.loading-overlay.hidden {
    display: none;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 3px solid rgba(255, 255, 255, 0.1);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

.loading-text {
    font-size: 15px;
    color: var(--text-dim);
}
