/* ==========================================================================
   FITNESS TRACKER — MAIN STYLESHEET
   ========================================================================== */

/* --------------------------------------------------------------------------
   1. VARIABLES & RESET
   -------------------------------------------------------------------------- */

:root {
    /* Section accent colors */
    --color-weight:   #3b82f6;
    --color-bp:       #ef4444;
    --color-exercise: #22c55e;
    --color-diet:     #f97316;
    --color-habits:   #8b5cf6;

    /* Default page-level accent (overridden per section via body class) */
    --accent: #64748b;

    /* Surface / background */
    --bg:          #eef2f7;
    --surface:     #ffffff;
    --surface-alt: #f8fafc;
    --border:      #e2e8f0;
    --border-mid:  #cbd5e1;

    /* Text */
    --text:       #1e293b;
    --text-mid:   #475569;
    --text-muted: #64748b;
    --text-light: #94a3b8;

    /* Nav */
    --nav-bg: #0f172a;
    --nav-h:  54px;

    /* Tokens */
    --radius-sm: 7px;
    --radius:    12px;
    --shadow-sm: 0 1px 3px rgba(0,0,0,.07);
    --shadow:    0 1px 3px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.06);
    --transition: 140ms ease;
}

/* Section accent injection — set once on <body>, used everywhere via var(--accent) */
body.section-weight   { --accent: var(--color-weight); }
body.section-bp       { --accent: var(--color-bp); }
body.section-exercise { --accent: var(--color-exercise); }
body.section-diet     { --accent: var(--color-diet); }
body.section-habits   { --accent: var(--color-habits); }

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    min-height: 100vh;
    -webkit-font-smoothing: antialiased;
}

/* --------------------------------------------------------------------------
   2. UTILITY CLASSES
   -------------------------------------------------------------------------- */

.text-muted  { color: var(--text-muted); }
.text-light  { color: var(--text-light); }
.text-sm     { font-size: .85rem; }
.text-xs     { font-size: .75rem; }
.text-bold   { font-weight: 700; }
.text-center { text-align: center; }
.text-upper  { text-transform: uppercase; letter-spacing: .07em; }

.flex-between { display: flex; justify-content: space-between; align-items: center; }
.flex-center  { display: flex; justify-content: center; align-items: center; }
.gap-8  { gap: 8px; }
.gap-12 { gap: 12px; }
.gap-20 { gap: 20px; }
.mb-0   { margin-bottom: 0 !important; }
.mb-12  { margin-bottom: 12px; }
.mb-20  { margin-bottom: 20px; }
.mt-0   { margin-top: 0 !important; }
.w-100  { width: 100%; }

/* Accent text colors */
.c-weight   { color: var(--color-weight);   }
.c-bp       { color: var(--color-bp);       }
.c-exercise { color: var(--color-exercise); }
.c-diet     { color: var(--color-diet);     }
.c-muted    { color: var(--text-light);     }

/* Tinted pill backgrounds */
.pill {
    display: inline-block;
    padding: .22rem .6rem;
    border-radius: 20px;
    font-size: .72rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .05em;
}
.pill-weight   { background: rgba(59,130,246,.12);  color: #1d4ed8; }
.pill-bp       { background: rgba(239,68,68,.12);   color: #b91c1c; }
.pill-exercise { background: rgba(34,197,94,.12);   color: #15803d; }
.pill-diet     { background: rgba(249,115,22,.12);  color: #c2410c; }

/* --------------------------------------------------------------------------
   3. LAYOUT
   -------------------------------------------------------------------------- */

.page {
    max-width: 1080px;
    margin: 0 auto;
    padding: 2rem 1.5rem 4rem;
}

/* --------------------------------------------------------------------------
   4. NAVIGATION
   -------------------------------------------------------------------------- */

.nav {
    background: var(--nav-bg);
    height: var(--nav-h);
    padding: 0 2rem;
    display: flex;
    align-items: center;
    gap: 1.75rem;
    position: sticky;
    top: 0;
    z-index: 200;
    box-shadow: 0 2px 10px rgba(0,0,0,.35);
}

.nav-brand {
    font-size: .9rem;
    font-weight: 800;
    color: #fff;
    text-decoration: none;
    letter-spacing: .03em;
    margin-right: auto;
    opacity: .9;
    transition: opacity var(--transition);
}
.nav-brand:hover { opacity: 1; }

.nav-link {
    color: #6b7f99;
    text-decoration: none;
    font-size: .8rem;
    font-weight: 600;
    letter-spacing: .03em;
    padding: 4px 2px;
    border-bottom: 2px solid transparent;
    transition: color var(--transition), border-color var(--transition);
    white-space: nowrap;
}
.nav-link:hover  { color: #c8d3e0; }
.nav-link.active { color: #ffffff; border-bottom-color: var(--accent); }

/* --------------------------------------------------------------------------
   5. PAGE TITLE
   -------------------------------------------------------------------------- */

.page-title {
    font-size: 1.45rem;
    font-weight: 800;
    color: var(--text);
    margin-bottom: 1.5rem;
    padding-left: 14px;
    border-left: 4px solid var(--accent);
    line-height: 1.2;
    letter-spacing: -.01em;
}

/* --------------------------------------------------------------------------
   6. CARDS
   -------------------------------------------------------------------------- */

.form-card,
.chart-card,
.table-card {
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 1.5rem;
    margin-bottom: 1.25rem;
}

.form-card  { border-top: 3px solid var(--accent); }
.chart-card { border-top: 3px solid var(--accent); }

.form-card h2,
.chart-card h2,
.table-card h2 {
    font-size: .68rem;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: .12em;
    color: var(--text-muted);
    margin-bottom: 1.1rem;
}

/* --------------------------------------------------------------------------
   7. FORMS
   -------------------------------------------------------------------------- */

.form-row {
    display: flex;
    flex-wrap: wrap;
    gap: .75rem;
    align-items: flex-end;
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: .3rem;
    flex: 1;
    min-width: 95px;
}

.form-group label {
    font-size: .7rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .07em;
    color: var(--text-muted);
}

.form-group input,
.form-group select,
.form-group textarea {
    border: 1.5px solid var(--border);
    border-radius: var(--radius-sm);
    padding: .5rem .75rem;
    font-size: .9rem;
    background: var(--surface-alt);
    color: var(--text);
    outline: none;
    width: 100%;
    font-family: inherit;
    transition: border-color var(--transition), box-shadow var(--transition), background var(--transition);
    -webkit-appearance: none;
    appearance: none;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
    border-color: var(--accent);
    background: var(--surface);
    box-shadow: 0 0 0 3px rgba(100,116,139,.12);
}

/* Custom select arrow */
.form-group select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 10 10'%3E%3Cpath fill='%2364748b' d='M5 7L1 3h8z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right .65rem center;
    padding-right: 2rem;
    cursor: pointer;
}

.form-group textarea { resize: vertical; min-height: 64px; }

/* --------------------------------------------------------------------------
   8. BUTTONS
   -------------------------------------------------------------------------- */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: .55rem 1.25rem;
    border: none;
    border-radius: var(--radius-sm);
    font-size: .85rem;
    font-weight: 700;
    cursor: pointer;
    text-decoration: none;
    font-family: inherit;
    letter-spacing: .02em;
    transition: opacity var(--transition), transform var(--transition);
    white-space: nowrap;
}
.btn:hover  { opacity: .87; }
.btn:active { transform: scale(.97); }

.btn-primary { background: var(--accent); color: #fff; }
.btn-danger  { background: #ef4444; color: #fff; padding: .32rem .72rem; font-size: .74rem; border-radius: 5px; }
.btn-ghost   { background: transparent; border: 1.5px solid var(--border-mid); color: var(--text-mid); }

/* --------------------------------------------------------------------------
   9. FLASH MESSAGES
   -------------------------------------------------------------------------- */

.flash {
    padding: .8rem 1.1rem;
    border-radius: var(--radius-sm);
    margin-bottom: 1.1rem;
    font-size: .875rem;
    font-weight: 600;
    border-left: 4px solid currentColor;
    display: flex;
    align-items: center;
    gap: .5rem;
}
.flash-success { background: #f0fdf4; color: #15803d; }
.flash-error   { background: #fef2f2; color: #b91c1c; }

/* --------------------------------------------------------------------------
   10. TABLES
   -------------------------------------------------------------------------- */

.table-wrap { overflow-x: auto; }

.table-scroll {
    max-height: 480px;
    overflow-y: auto;
    overflow-x: auto;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
}
.table-scroll thead th { position: sticky; top: 0; z-index: 1; }

.select-compact {
    border: 1.5px solid var(--border);
    border-radius: var(--radius-sm);
    padding: .45rem .6rem;
    font-size: .82rem;
    font-weight: 600;
    background: var(--surface-alt);
    color: var(--text);
    font-family: inherit;
    cursor: pointer;
    -webkit-appearance: none;
    appearance: none;
}

table { width: 100%; border-collapse: collapse; font-size: .875rem; }

thead th {
    background: var(--surface-alt);
    color: var(--text-muted);
    font-size: .68rem;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: .1em;
    padding: .65rem .9rem;
    border-bottom: 2px solid var(--border);
    text-align: left;
    white-space: nowrap;
}

tbody td {
    padding: .72rem .9rem;
    border-bottom: 1px solid var(--border);
    color: var(--text);
    vertical-align: middle;
}
tbody tr:last-child td  { border-bottom: none; }
tbody tr:hover td       { background: #f8fafc; }

/* --------------------------------------------------------------------------
   11. BP CATEGORY BADGES
   -------------------------------------------------------------------------- */

.bp-normal   { color: #15803d; font-weight: 700; font-size: .8rem; }
.bp-elevated { color: #92400e; font-weight: 700; font-size: .8rem; }
.bp-stage1   { color: #c2410c; font-weight: 700; font-size: .8rem; }
.bp-stage2   { color: #b91c1c; font-weight: 700; font-size: .8rem; }

/* Intensity labels */
.int-light    { color: #15803d; font-weight: 700; font-size: .8rem; }
.int-moderate { color: #1d4ed8; font-weight: 700; font-size: .8rem; }
.int-vigorous { color: #b91c1c; font-weight: 700; font-size: .8rem; }

/* --------------------------------------------------------------------------
   12. DASHBOARD — STAT CARDS
   -------------------------------------------------------------------------- */

.cards {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: .75rem;
    margin-bottom: 1.25rem;
}

.card {
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 1rem 1.1rem;
}
.card.w { border-top: 3px solid var(--color-weight);   }
.card.b { border-top: 3px solid var(--color-bp);       }
.card.e { border-top: 3px solid var(--color-exercise); }
.card.d { border-top: 3px solid var(--color-diet);     }

.card-label {
    font-size: .65rem;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: .08em;
    color: var(--text-muted);
    margin-bottom: .45rem;
}
.card-value {
    font-size: 1.7rem;
    font-weight: 800;
    line-height: 1;
    letter-spacing: -.02em;
}
.card-value span { font-size: .9rem; font-weight: 500; color: var(--text-muted); margin-left: .15em; }
.card-sub { font-size: .73rem; color: var(--text-muted); margin-top: .35rem; }

/* Dashboard 2×2 chart grid */
.charts-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 1rem;
}

/* Today's calorie banner */
.today-banner {
    background: #fff7ed;
    border: 1.5px solid #fed7aa;
    border-left: 4px solid var(--color-diet);
    border-radius: var(--radius-sm);
    padding: .8rem 1.2rem;
    margin-bottom: 1.25rem;
    font-size: .875rem;
    font-weight: 600;
    color: #c2410c;
}

/* --------------------------------------------------------------------------
   14. LIBRARY MODAL & ADD BUTTON
   -------------------------------------------------------------------------- */

/* "+" button inline with inputs */
.btn-add-lib {
    flex-shrink: 0;
    width: 34px;
    border: 1.5px solid var(--border-mid);
    border-radius: var(--radius-sm);
    background: var(--surface-alt);
    color: var(--text-muted);
    font-size: 1.3rem;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background var(--transition), color var(--transition), border-color var(--transition);
}
.btn-add-lib:hover {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
}

/* Input + button row */
.input-with-add {
    display: flex;
    gap: .35rem;
    align-items: stretch;
}
.input-with-add input { flex: 1; min-width: 0; }

/* Modal overlay */
.lib-modal-overlay {
    position: fixed; inset: 0;
    background: rgba(0,0,0,.5);
    z-index: 500;
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(2px);
}

/* Modal box */
.lib-modal {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 1.5rem;
    width: 90%;
    max-width: 640px;
    box-shadow: 0 12px 40px rgba(0,0,0,.25);
    border-top: 3px solid var(--accent);
}
.lib-modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.25rem;
}
.lib-modal-header h3 { font-size: 1rem; font-weight: 700; color: var(--text); }
.lib-modal-close {
    background: none; border: none;
    font-size: 1.5rem; line-height: 1;
    color: var(--text-light); cursor: pointer;
    padding: 0 .2rem;
    transition: color var(--transition);
}
.lib-modal-close:hover { color: var(--text); }
.lib-modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: .75rem;
    margin-top: 1.25rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border);
}

/* Toast */
.lib-toast {
    position: fixed; bottom: 1.5rem; right: 1.5rem;
    background: #0f172a; color: #fff;
    padding: .7rem 1.1rem;
    border-radius: 8px;
    font-size: .85rem; font-weight: 600;
    opacity: 0; transform: translateY(8px);
    transition: opacity .2s, transform .2s;
    z-index: 9999;
    pointer-events: none;
}
.lib-toast.show { opacity: 1; transform: translateY(0); }

/* --------------------------------------------------------------------------
   13. HABITS
   -------------------------------------------------------------------------- */

.habit-list { display: flex; flex-direction: column; gap: .5rem; }

.habit-row {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: .85rem 1.1rem;
    background: var(--surface-alt);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    transition: background var(--transition);
}
.habit-row.done { background: #f0fdf4; border-color: #bbf7d0; }

.habit-toggle {
    appearance: none;
    width: 22px; height: 22px;
    border: 2px solid var(--border-mid);
    border-radius: 6px;
    cursor: pointer;
    flex-shrink: 0;
    transition: background var(--transition), border-color var(--transition);
    position: relative;
}
.habit-toggle:checked {
    background: var(--accent);
    border-color: var(--accent);
}
.habit-toggle:checked::after {
    content: '';
    position: absolute;
    left: 4px; top: 1px;
    width: 10px; height: 6px;
    border-left: 2px solid #fff;
    border-bottom: 2px solid #fff;
    transform: rotate(-45deg);
}

.habit-name {
    flex: 1;
    font-size: 1rem;
    font-weight: 500;
}
.habit-row.done .habit-name { color: var(--text-muted); text-decoration: line-through; }

.habit-streak {
    font-size: .75rem;
    color: var(--text-muted);
    white-space: nowrap;
}

.habit-history { display: flex; gap: 3px; align-items: center; flex-shrink: 0; }
.habit-dot {
    width: 7px; height: 7px;
    border-radius: 50%;
    background: var(--border);
}
.habit-dot.done { background: var(--accent); }

/* --------------------------------------------------------------------------
   14. LOGIN
   -------------------------------------------------------------------------- */

.login-wrap {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg);
    padding: 2rem 1rem;
}

.login-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 2.5rem 2.25rem;
    width: 100%;
    max-width: 380px;
}

.login-brand {
    font-size: 1.15rem;
    font-weight: 800;
    color: var(--text);
    letter-spacing: .02em;
    margin-bottom: 1.75rem;
    text-align: center;
}

.nav-logout {
    margin-left: auto;
    color: #6b7f99;
    text-decoration: none;
    font-size: .75rem;
    font-weight: 600;
    letter-spacing: .04em;
    padding: 4px 8px;
    border: 1px solid #2d3f57;
    border-radius: 5px;
    transition: color var(--transition), border-color var(--transition);
    white-space: nowrap;
}
.nav-logout:hover { color: #c8d3e0; border-color: #4a5f7a; }

.flash-error {
    background: rgba(239,68,68,.1);
    color: #b91c1c;
    border: 1px solid rgba(239,68,68,.25);
    border-radius: var(--radius-sm);
    padding: .6rem .9rem;
    font-size: .875rem;
    margin-bottom: 1.1rem;
}

/* --------------------------------------------------------------------------
   15. FITBIT / GOOGLE HEALTH SYNC PAGE
   -------------------------------------------------------------------------- */

.badge {
    display: inline-block;
    padding: .25rem .65rem;
    border-radius: 999px;
    font-size: .75rem;
    font-weight: 600;
    letter-spacing: .02em;
}
.badge-success { background: rgba(34,197,94,.15); color: #15803d; }
.badge-muted   { background: var(--surface-2); color: var(--text-muted); }

.alert {
    padding: .7rem 1rem;
    border-radius: var(--radius-sm);
    font-size: .875rem;
    margin-bottom: 1.25rem;
}
.alert-success { background: rgba(34,197,94,.1);  color: #15803d; border: 1px solid rgba(34,197,94,.25); }
.alert-error   { background: rgba(239,68,68,.1);  color: #b91c1c; border: 1px solid rgba(239,68,68,.25); }

.sync-log {
    margin-top: .75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    overflow: hidden;
    font-size: .82rem;
}
.sync-log-row {
    display: flex;
    gap: 1rem;
    padding: .4rem .75rem;
    border-bottom: 1px solid var(--border);
}
.sync-log-row:last-child { border-bottom: none; }
.sync-log-date  { min-width: 50px; color: var(--text-muted); }
.sync-log-items { color: var(--text); }

.fitbit-preview-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 1.5rem;
}
.data-table td, .data-table th { white-space: nowrap; }
.avg-row td { font-weight: 700; border-bottom: 2px solid var(--border); }
.above-avg { color: #16a34a; font-weight: 600; }
.below-avg { color: #dc2626; font-weight: 600; }
.debug-pre {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: .75rem 1rem;
    font-size: .75rem;
    overflow-x: auto;
    white-space: pre-wrap;
    word-break: break-all;
    max-height: 400px;
    overflow-y: auto;
}

.preview-label {
    font-size: .8rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .05em;
    color: var(--text-muted);
    margin: 0 0 .6rem;
}

/* --------------------------------------------------------------------------
   16. RESPONSIVE
   -------------------------------------------------------------------------- */

@media (max-width: 860px) {
    .cards       { grid-template-columns: repeat(2, 1fr); }
    .charts-grid { grid-template-columns: 1fr; }
}

@media (max-width: 620px) {
    .nav { gap: 1rem; padding: 0 1rem; }
    .nav-link { font-size: .74rem; }
    .page { padding: 1.5rem 1rem 3rem; }
}

@media (max-width: 480px) {
    .cards    { grid-template-columns: 1fr; }
    .form-row { flex-direction: column; }
}

/* ---- Peloton stats strip ---- */
.stat-mini { text-align: center; }
.stat-mini-val   { font-size: 1.25rem; font-weight: 700; color: var(--text); }
.stat-mini-label { font-size: .75rem; color: var(--text-muted); margin-top: 2px; }

/* ---- Extra badge variants ---- */
.badge-warning { background: rgba(234,179,8,.15); color: #92400e; }

/* ---- Peloton workout title column ---- */
.pel-title { max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---- Wide modal (exercise / diet log forms) ---- */
.lib-modal-wide { max-width: 900px; }

/* --------------------------------------------------------------------------
   17. PRINT
   -------------------------------------------------------------------------- */

@page { margin: 0.5in; }

@media print {
    .nav,
    .log-modal-trigger,
    .lib-modal-overlay,
    .flash,
    .btn-danger,
    .btn-ghost,
    .chart-card td:last-child { display: none !important; }

    body { background: #fff; }
    .page { padding: 0; max-width: 100%; }
    .table-card, .chart-card { box-shadow: none; border: 1px solid #ccc; border-radius: 4px; }
    .avg-row td { border-bottom: 1px solid #999; }

    /* BP page: chart + 30 rows on one portrait page */
    body.section-bp .chart-card { margin-bottom: .4rem; page-break-inside: avoid; }
    body.section-bp .chart-card canvas { max-height: 110px !important; }
    body.section-bp .table-card { font-size: 8pt; }
    body.section-bp .table-card th,
    body.section-bp .table-card td { padding: 2px 6px; line-height: 1.35; }
    body.section-bp .table-card h2 { font-size: 9pt; margin-bottom: .3rem; }
}
