:root {
    --bg-primary: var(--tg-theme-bg-color, #0f0f12);
    --bg-secondary: var(--tg-theme-secondary-bg-color, #171721);
    --text-primary: var(--tg-theme-text-color, #ffffff);
    --text-secondary: var(--tg-theme-hint-color, #a1a1b5);
    --accent: var(--tg-theme-button-color, #5856d6);
    --accent-text: var(--tg-theme-button-text-color, #ffffff);
    
    --card-bg: color-mix(in srgb, var(--bg-secondary) 88%, transparent);
    --card-border: rgba(128, 128, 128, 0.18);
    
    --green: #2ecc71;
    --red: #e74c3c;
    --blue: #3498db;
    --yellow: #f1c40f;
    --orange: #e67e22;
    --cyan: #1abc9c;
}

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

body {
    font-family: 'Outfit', sans-serif;
    background-color: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    overflow-x: hidden;
    line-height: 1.5;
}

#app-container {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    max-width: 480px;
    margin: 0 auto;
    position: relative;
    padding-bottom: 75px; /* Space for Nav Bar */
}

/* Header */
.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 16px;
    background-color: color-mix(in srgb, var(--bg-primary) 85%, transparent);
    backdrop-filter: blur(10px);
    position: sticky;
    top: 0;
    z-index: 100;
    border-bottom: 1px solid var(--card-border);
}

.user-badge {
    display: flex;
    align-items: center;
    gap: 12px;
}

.user-avatar {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--accent), #ff2d55);
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 1.1rem;
    box-shadow: 0 4px 10px rgba(0,0,0,0.3);
}

.user-meta h1 {
    font-size: 1.15rem;
    font-weight: 700;
    letter-spacing: -0.3px;
}

.user-meta p {
    font-size: 0.85rem;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 4px;
}

.freeze-pill {
    display: flex;
    align-items: center;
    gap: 6px;
    background: rgba(26, 188, 156, 0.1);
    border: 1px solid rgba(26, 188, 156, 0.2);
    padding: 6px 12px;
    border-radius: 20px;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--cyan);
}

/* Main */
.app-main {
    padding: 16px;
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

/* Cards */
.card {
    background-color: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: 16px;
    padding: 16px;
    position: relative;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
}

.card.glass {
    backdrop-filter: blur(16px);
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
}

.card-title {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 8px;
}

.clickable-card {
    cursor: pointer;
    transition: transform 0.2s, background-color 0.2s;
}

.clickable-card:active {
    transform: scale(0.98);
    background-color: color-mix(in srgb, var(--text-primary) 6%, transparent);
}

/* Donut Chart and center text */
.donut-container {
    position: relative;
    width: 150px;
    height: 150px;
    margin: 10px auto;
}

.donut-center-text {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    pointer-events: none;
}

.donut-val {
    display: block;
    font-size: 1.6rem;
    font-weight: 800;
}

.donut-lbl {
    display: block;
    font-size: 0.75rem;
    color: var(--text-secondary);
}

/* Macro List */
.macro-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
    height: 100%;
    justify-content: center;
}

.macro-item {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.macro-lbl {
    display: flex;
    justify-content: space-between;
    font-size: 0.85rem;
    font-weight: 500;
}

.progress-bar-bg {
    width: 100%;
    height: 8px;
    background-color: rgba(255,255,255,0.05);
    border-radius: 4px;
    overflow: hidden;
}

.progress-bar-fill {
    height: 100%;
    border-radius: 4px;
    transition: width 0.8s cubic-bezier(0.1, 0.8, 0.25, 1);
}

/* Banner achievement */
.achievement-banner {
    display: flex;
    align-items: center;
    gap: 16px;
}

.banner-icon {
    font-size: 2rem;
}

.banner-text h4 {
    font-size: 0.95rem;
    font-weight: 600;
    margin-bottom: 2px;
}

.banner-text p {
    font-size: 0.8rem;
    color: var(--text-secondary);
}

.banner-arrow {
    margin-left: auto;
    color: var(--text-secondary);
    font-size: 0.9rem;
}

/* Grids */
.grid-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

.grid-3 {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
}

/* Navigation tabs subheader */
.tab-sub-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}

.tab-sub-header h2 {
    font-size: 1.15rem;
    font-weight: 700;
}

/* Toggle group */
.toggle-group {
    background: var(--bg-secondary);
    border: 1px solid var(--card-border);
    padding: 3px;
    border-radius: 20px;
    display: flex;
    gap: 2px;
}

.toggle-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-family: inherit;
    font-size: 0.8rem;
    font-weight: 600;
    padding: 6px 12px;
    border-radius: 17px;
    cursor: pointer;
    transition: background-color 0.2s, color 0.2s;
}

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

/* Chart Wrapper */
.chart-wrapper {
    position: relative;
    height: 200px;
    width: 100%;
    margin-top: 10px;
}

/* Badges Grid */
.badges-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 16px 12px;
    margin-top: 10px;
}

.badge-card {
    min-width: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    padding: 16px 8px;
    border-radius: 16px;
    opacity: 0.5;
    filter: grayscale(1);
    transition: opacity 0.3s, filter 0.3s, transform 0.2s;
}

.badge-card.unlocked {
    opacity: 1;
    filter: grayscale(0);
    border-color: rgba(241, 196, 15, 0.3);
    box-shadow: 0 4px 15px rgba(241, 196, 15, 0.1);
}

.badge-card:active {
    transform: scale(0.95);
}

.badge-icon {
    font-size: 2.2rem;
    margin-bottom: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 48px;
}

.badge-card h4 {
    font-size: 0.85rem;
    font-weight: 600;
    line-height: 1.2;
    margin-bottom: 4px;
}

.badge-card p {
    font-size: 0.7rem;
    color: var(--text-secondary);
    line-height: 1.2;
}

/* Premium Card */
.card-premium {
    background: linear-gradient(135deg, color-mix(in srgb, var(--accent) 14%, var(--bg-secondary)) 0%, var(--bg-secondary) 100%);
    border: 1px solid color-mix(in srgb, var(--accent) 30%, transparent);
    box-shadow: 0 8px 32px color-mix(in srgb, var(--accent) 15%, transparent);
    margin-bottom: 16px;
    overflow: hidden;
}

.premium-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.card-tag {
    font-size: 0.75rem;
    font-weight: 800;
    color: var(--accent);
    letter-spacing: 1px;
}

.health-score-gauge {
    font-size: 2.2rem;
    font-weight: 800;
    color: var(--text-primary);
    line-height: 1;
}

.score-denominator {
    font-size: 1rem;
    color: var(--text-secondary);
    font-weight: 500;
}

.premium-divider {
    height: 1px;
    background: linear-gradient(90deg, rgba(138, 43, 226, 0.4), transparent);
    margin: 16px 0;
}

.coach-text {
    font-size: 0.95rem;
    color: var(--text-primary);
    line-height: 1.6;
    font-style: italic;
    overflow-wrap: anywhere;
}

/* Mini Cards for health card scores */
.card-mini {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    padding: 12px;
    border-radius: 12px;
    text-align: center;
}

.mini-score {
    font-size: 1.4rem;
    font-weight: 700;
    margin-bottom: 2px;
}

.mini-label {
    font-size: 0.75rem;
    color: var(--text-secondary);
    font-weight: 500;
}

.mini-trend {
    font-size: 0.75rem;
    font-weight: 600;
    margin-top: 4px;
}

/* Navigation bar */
.app-nav {
    display: flex;
    justify-content: space-around;
    align-items: center;
    background-color: color-mix(in srgb, var(--bg-secondary) 92%, transparent);
    border-top: 1px solid var(--card-border);
    backdrop-filter: blur(20px);
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 64px;
    z-index: 100;
    max-width: 480px;
    margin: 0 auto;
    border-radius: 20px 20px 0 0;
}

.nav-item {
    background: none;
    border: none;
    color: var(--text-secondary);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    flex: 1 1 0;
    min-width: 0;
    padding: 0 2px;
    height: 100%;
    justify-content: center;
    cursor: pointer;
    font-family: inherit;
    transition: color 0.2s;
}

.nav-item i {
    font-size: 1.25rem;
}

.nav-item span {
    font-size: 0.68rem;
    font-weight: 600;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

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

/* Overlay & Spinners */
.overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--bg-primary);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    transition: opacity 0.3s;
}

.spinner-container {
    text-align: center;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 3px solid rgba(255,255,255,0.05);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 1s infinite linear;
    margin: 0 auto 16px;
}

/* Helpers */
.hidden {
    display: none !important;
}

.text-center {
    text-align: center;
}

.text-secondary {
    color: var(--text-secondary);
}

.text-green { color: var(--green); }
.text-red { color: var(--red); }
.text-blue { color: var(--blue); }
.text-yellow { color: var(--yellow); }
.text-orange { color: var(--orange); }
.text-cyan { color: var(--cyan); }

.bg-green { background-color: var(--green); }
.bg-red { background-color: var(--red); }
.bg-blue { background-color: var(--blue); }
.bg-yellow { background-color: var(--yellow); }
.bg-orange { background-color: var(--orange); }
.bg-cyan { background-color: var(--cyan); }

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* General Layout spacing */
.tab-content {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.tab-content h2 {
    margin-bottom: 0;
}

.tab-content .section-desc {
    margin-top: 0;
    margin-bottom: 0;
}

h2 {
    font-size: 1.25rem;
    font-weight: 700;
    margin-bottom: 4px;
}

.section-desc {
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-bottom: 12px;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 10px 20px;
    border-radius: 20px;
    border: none;
    font-family: inherit;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    margin-top: 12px;
    text-decoration: none;
}

.btn-primary {
    background-color: var(--accent);
    color: var(--accent-text);
}

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

/* Medication library, five-step setup and intake history. */
#medications-tab { overflow-wrap: anywhere; }
#medications-tab h2, #medications-tab h3 { margin-bottom: 16px; }
.med-button { margin: 6px 8px 6px 0; padding: 10px 14px; border: 1px solid #64748b; color: inherit; background: transparent; }
.med-button:disabled { opacity: .5; }
.med-row { padding: 16px 0; border-bottom: 1px solid #64748b55; }
.med-row p { margin: 8px 0; }
.med-field { display: flex; flex-direction: column; gap: 8px; margin: 18px 0; }
.med-field input { width: 100%; min-height: 44px; padding: 10px; border-radius: 8px; border: 1px solid #64748b; font: inherit; color: inherit; background: transparent; }
.med-field input[type=checkbox] { width: 24px; min-height: 24px; }
.med-stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px; }
.med-stats strong { display: block; font-size: 28px; }
.med-stats span { display: block; font-size: 13px; }
.med-chart { position: relative; height: 220px; margin-top: 18px; }
.med-days { display:flex; flex-wrap:wrap; gap: 14px; margin: 16px 0; }
.med-days label { display:flex; align-items:center; gap:6px; min-height:44px; }
#med-error { color: #f87171; margin: 16px; }

#medications-tab [hidden] { display: none !important; }
/* Daily entry forms remain readable at narrow widths and with browser zoom. */
.ux-summary { white-space: pre-line; line-height: 1.6; }
.ux-actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 16px 0; }
.ux-button { min-height: 44px; padding: 10px 14px; cursor: pointer; color: var(--text-primary); background: var(--bg-secondary); border: 1px solid #8886; }
.ux-meal-row { padding: 12px 0; border-top: 1px solid #8884; overflow-wrap: anywhere; }
.ux-meal-row button { margin: 6px; }
.ux-dialog { width: min(92vw, 480px); max-height: 90vh; overflow-y: auto; border-radius: 16px; padding: 24px; margin: auto; background: var(--bg-secondary); color: var(--text-primary); border: 1px solid var(--card-border); }
.ux-dialog::backdrop { background: #0009; }
.ux-dialog label { display: block; margin: 12px 0; line-height: 1.5; }
.ux-dialog input:not([type=checkbox]), .ux-dialog select { display: block; box-sizing: border-box; width: 100%; min-height: 44px; margin-top: 6px; padding: 10px; font-size: 16px; border-radius: 8px; border: 1px solid var(--card-border); color: var(--text-primary); background: var(--bg-primary); }
.ux-dialog input[type=checkbox] { margin-left: 12px; width: 22px; height: 22px; }
.ux-dialog button { margin: 6px 4px 6px 0; min-height: 44px; }
.ux-dialog :focus-visible, .ux-button:focus-visible { outline: 3px solid #5ddbc3; outline-offset: 3px; }
.ux-button { max-width: 100%; white-space: normal; overflow-wrap: anywhere; }
.card, .grid-2 > *, .nav-item { min-width: 0; }
.card-title { overflow-wrap: anywhere; }
.donut-container canvas { max-width: 100%; max-height: 100%; }
.nav-item { flex: 1; padding: 0 4px; }
.nav-item span { overflow-wrap: anywhere; }
@media (max-width: 420px) {
    .grid-2 { grid-template-columns: minmax(0, 1fr); }
    .grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* Workouts share the dashboard cards and palette, with mobile-safe text wrapping. */
.wo-field { display:flex; flex-direction:column; gap:8px; margin:14px 0; overflow-wrap:anywhere; }
.wo-field input,.wo-field select,.wo-field textarea { width:100%; max-width:100%; min-width:0; box-sizing:border-box; padding:12px; border-radius:10px; background:var(--bg-secondary); color:var(--text-primary); border:1px solid var(--card-border); font:inherit; }
.wo-field textarea { min-height:90px; resize:vertical; }
.wo-button { margin:6px 6px 6px 0; min-height:44px; white-space:normal; overflow-wrap:anywhere; max-width:100%; }
.wo-history { margin:14px 0; }
.wo-history summary { cursor:pointer; display:flex; flex-direction:column; gap:6px; min-height:44px; overflow-wrap:anywhere; }
.wo-history p,.wo-exercise,.wo-medal { overflow-wrap:anywhere; white-space:pre-wrap; }
.wo-exercise { padding:12px 0; border-bottom:1px solid #334155; }
.wo-exercise p { margin:5px 0; }
.wo-chart { position:relative; height:220px; margin:18px 0; }
.wo-badges { display:grid; grid-template-columns:repeat(2,minmax(0,1fr)); gap:12px; }
.wo-medal { min-width:0; opacity:.65; }
.wo-medal.unlocked { opacity:1; border-color:#5ddbc3; }
.wo-medal-icon { font-size:32px; }
.wo-pagination { display:flex; align-items:center; justify-content:center; gap:12px; }
.wo-button:focus-visible,.wo-field :focus-visible { outline:3px solid #5ddbc3; outline-offset:3px; }
@media(max-width:350px) { .wo-badges { grid-template-columns:minmax(0,1fr); } .nav-item { padding:0 2px; } .nav-item span { font-size:10px; } }
.wo-chart canvas { max-width:100%; }
