/* ---------- Design Tokens ---------- */
:root {
    --bg: #f4f5f7;
    --bg-elevated: #ffffff;
    --bg-input: #ffffff;
    --text: #14171f;
    --text-muted: #5b6270;
    --border: #dde1e7;
    --accent: #0891b2;
    --accent-contrast: #ffffff;
    --accent-soft: #e0f7fb;
    --danger: #dc2626;
    --danger-soft: #fde2e2;
    --success: #16a34a;
    --success-soft: #dcfce7;
    --warn: #d97706;
    --warn-soft: #fef3c7;
    --pr-gold: #b45309;
    --radius: 14px;
    --radius-sm: 9px;
    --shadow: 0 1px 3px rgba(20, 23, 31, 0.08), 0 1px 2px rgba(20, 23, 31, 0.06);
    --nav-height: 64px;
    --header-height: 56px;
    --safe-top: env(safe-area-inset-top, 0px);
    --header-total-height: calc(var(--header-height) + var(--safe-top));
    --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

:root[data-theme="dark"] {
    --bg: #0f1115;
    --bg-elevated: #1a1d24;
    --bg-input: #22252e;
    --text: #f1f2f4;
    --text-muted: #9aa0ac;
    --border: #2c303a;
    --accent: #22d3ee;
    --accent-contrast: #062a30;
    --accent-soft: #113239;
    --danger: #f87171;
    --danger-soft: #3a1f22;
    --success: #4ade80;
    --success-soft: #16321f;
    --warn: #fbbf24;
    --warn-soft: #3a2c10;
    --pr-gold: #fbbf24;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #0f1115;
        --bg-elevated: #1a1d24;
        --bg-input: #22252e;
        --text: #f1f2f4;
        --text-muted: #9aa0ac;
        --border: #2c303a;
        --accent: #22d3ee;
        --accent-contrast: #062a30;
        --accent-soft: #113239;
        --danger: #f87171;
        --danger-soft: #3a1f22;
        --success: #4ade80;
        --success-soft: #16321f;
        --warn: #fbbf24;
        --warn-soft: #3a2c10;
        --pr-gold: #fbbf24;
    }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font);
    -webkit-tap-highlight-color: transparent;
    overscroll-behavior-y: contain;
}
h1, h2, h3, h4 { margin: 0 0 8px; line-height: 1.2; }
p { margin: 0 0 8px; }
a { color: inherit; }
button, input, select, textarea { font-family: inherit; font-size: 16px; }

/* ---------- Layout ---------- */
.app-header {
    position: sticky; top: 0; z-index: 20;
    height: var(--header-total-height);
    padding-top: var(--safe-top);
    box-sizing: border-box;
    display: flex; align-items: center; gap: 8px;
    padding-left: 12px; padding-right: 12px;
    background: var(--bg-elevated);
    border-bottom: 1px solid var(--border);
}
.app-header h1 { flex: 1; font-size: 17px; font-weight: 600; text-align: center; margin: 0; }
.icon-btn {
    width: 36px; height: 36px; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    background: transparent; border: none; color: var(--text);
    font-size: 18px; cursor: pointer;
}
.icon-btn:active { background: var(--accent-soft); }

.app-root {
    padding: 12px 12px calc(var(--nav-height) + 24px);
    min-height: calc(100vh - var(--header-total-height));
    max-width: 640px;
    margin: 0 auto;
}

.bottom-nav {
    position: fixed; bottom: 0; left: 0; right: 0; z-index: 20;
    display: flex;
    background: var(--bg-elevated);
    border-top: 1px solid var(--border);
    padding-bottom: env(safe-area-inset-bottom, 0);
}
.bottom-nav a {
    flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 2px; padding: 8px 0; text-decoration: none; color: var(--text-muted);
    font-size: 11px; height: var(--nav-height);
}
.bottom-nav a.active { color: var(--accent); }
.nav-icon { font-size: 20px; }

.offline-banner {
    background: var(--warn-soft); color: var(--warn);
    text-align: center; font-size: 13px;
    padding: calc(6px + var(--safe-top)) 10px 6px;
    position: sticky; top: 0; z-index: 30;
}

/* ---------- Cards / Sections ---------- */
.card {
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px;
    margin-bottom: 12px;
    box-shadow: var(--shadow);
}
.card-title {
    font-size: 13px; text-transform: uppercase; letter-spacing: .04em;
    color: var(--text-muted); margin-bottom: 8px; font-weight: 600;
}
.section-title { font-size: 20px; font-weight: 700; margin: 4px 0 12px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.stat-block { text-align: center; }
.stat-value { font-size: 24px; font-weight: 700; color: var(--accent); }
.stat-label { font-size: 12px; color: var(--text-muted); }

/* ---------- Forms ---------- */
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; color: var(--text-muted); margin-bottom: 5px; }
input[type="text"], input[type="email"], input[type="password"], input[type="number"],
input[type="date"], input[type="search"], select, textarea {
    width: 100%; padding: 11px 12px; border-radius: var(--radius-sm);
    border: 1px solid var(--border); background: var(--bg-input); color: var(--text);
    appearance: none; -webkit-appearance: none;
}
textarea { resize: vertical; min-height: 60px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.checkbox-row { display: flex; align-items: center; gap: 8px; }
.checkbox-row input { width: 20px; height: 20px; }
.form-error { color: var(--danger); font-size: 13px; margin: 6px 0; min-height: 1em; }
.form-hint { color: var(--text-muted); font-size: 12px; margin-top: 4px; }

.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    padding: 12px 16px; border-radius: var(--radius-sm); border: none;
    background: var(--accent); color: var(--accent-contrast);
    font-weight: 600; font-size: 15px; cursor: pointer; width: 100%;
    min-height: 46px;
}
.btn:active { filter: brightness(0.92); }
.btn:disabled { opacity: .5; }
.btn-secondary { background: var(--bg-input); color: var(--text); border: 1px solid var(--border); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-ghost { background: transparent; color: var(--accent); width: auto; padding: 8px 10px; min-height: unset; }
.btn-sm { width: auto; padding: 8px 12px; min-height: 38px; font-size: 14px; }
.btn-row { display: flex; gap: 8px; }
.btn-row .btn { width: auto; flex: 1; }
.fab {
    position: fixed; right: 18px; bottom: calc(var(--nav-height) + 18px); z-index: 15;
    width: 56px; height: 56px; border-radius: 50%; background: var(--accent);
    color: var(--accent-contrast); border: none; font-size: 26px; box-shadow: var(--shadow);
}

/* ---------- Lists ---------- */
.list-item {
    display: flex; align-items: center; justify-content: space-between; gap: 10px;
    padding: 12px 4px; border-bottom: 1px solid var(--border);
}
.list-item:last-child { border-bottom: none; }
.list-item-title { font-weight: 600; }
.list-item-sub { font-size: 13px; color: var(--text-muted); }
.badge {
    display: inline-block; font-size: 11px; font-weight: 700; padding: 2px 8px;
    border-radius: 999px; background: var(--accent-soft); color: var(--accent);
}
.badge-pr { background: var(--warn-soft); color: var(--pr-gold); }
.badge-deload { background: var(--warn-soft); color: var(--warn); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }
.badge-success { background: var(--success-soft); color: var(--success); }

/* ---------- Tabs / Segmented control ---------- */
.tabs { display: flex; background: var(--bg-input); border-radius: var(--radius-sm); padding: 3px; margin-bottom: 14px; }
.tabs button {
    flex: 1; border: none; background: transparent; color: var(--text-muted);
    padding: 8px 4px; border-radius: 8px; font-weight: 600; font-size: 13px; cursor: pointer;
}
.tabs button.active { background: var(--accent); color: var(--accent-contrast); }

/* ---------- Muscle group overview ---------- */
.muscle-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.muscle-tile {
    border-radius: var(--radius-sm); padding: 10px 12px; background: var(--bg-input);
    border: 1px solid var(--border);
}
.muscle-tile .fresh { border-left: 4px solid var(--danger); }
.muscle-tile-name { font-weight: 600; font-size: 14px; }
.muscle-tile-days { font-size: 12px; color: var(--text-muted); }
.heat-0 { border-left: 4px solid var(--success); }
.heat-1 { border-left: 4px solid var(--warn); }
.heat-2 { border-left: 4px solid var(--danger); }

/* ---------- Workout / Set logging ---------- */
.exercise-block { margin-bottom: 16px; }
.exercise-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.set-row-header {
    display: grid; grid-template-columns: 28px 1fr 1fr 1fr 34px; gap: 6px;
    font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .03em;
    padding: 0 4px 4px; text-align: center;
}
.set-row-header div:first-child, .set-row-header div:last-child { visibility: hidden; }
.set-row {
    display: grid; grid-template-columns: 28px 1fr 1fr 1fr 34px; gap: 6px; align-items: center;
    margin-bottom: 6px;
}
.set-row input { text-align: center; padding: 9px 4px; }
.set-row .set-idx { font-size: 12px; color: var(--text-muted); text-align: center; }
.set-row.warmup .set-idx { color: var(--warn); }
.set-row.done { opacity: .55; }
.set-check {
    width: 32px; height: 32px; border-radius: 8px; border: 1px solid var(--border);
    background: var(--bg-input); color: var(--text-muted); font-size: 16px;
}
.set-check.checked { background: var(--success); color: #fff; border-color: var(--success); }
.suggestion-hint { font-size: 12px; color: var(--text-muted); margin-bottom: 6px; }

.timer-overlay {
    position: fixed; inset: 0; background: rgba(0,0,0,.6); z-index: 50;
    display: flex; align-items: center; justify-content: center;
}
.timer-card {
    background: var(--bg-elevated); border-radius: var(--radius); padding: 28px;
    text-align: center; width: 260px;
}
.timer-display { font-size: 48px; font-weight: 700; font-variant-numeric: tabular-nums; margin: 12px 0; }
.timer-controls { display: flex; gap: 8px; margin-top: 12px; }
.timer-card.timer-complete {
    animation: timer-flash .35s ease-in-out 2;
}
.timer-card.timer-complete .timer-display { color: var(--success); }
@keyframes timer-flash {
    0%, 100% { background: var(--bg-elevated); transform: scale(1); }
    50% { background: var(--success-soft); transform: scale(1.04); }
}

/* ---------- Calendar ---------- */
.calendar-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.calendar-dow { text-align: center; font-size: 11px; color: var(--text-muted); padding-bottom: 4px; }
.calendar-day {
    aspect-ratio: 1; border-radius: 8px; display: flex; align-items: center; justify-content: center;
    font-size: 13px; background: var(--bg-input); border: 1px solid transparent; color: var(--text-muted);
}
.calendar-day.trained { background: var(--accent-soft); color: var(--accent); font-weight: 700; border-color: var(--accent); }
.calendar-day.deload { background: var(--warn-soft); color: var(--warn); border-color: var(--warn); }
.calendar-day.empty { visibility: hidden; }
.calendar-day.today { box-shadow: inset 0 0 0 2px var(--text); }

/* ---------- Chart ---------- */
.chart-wrap { width: 100%; overflow-x: auto; }
.chart-svg text { fill: var(--text-muted); font-size: 10px; }
.chart-line { fill: none; stroke: var(--accent); stroke-width: 2.5; }
.chart-point { fill: var(--accent); }
.chart-point.pr { fill: var(--pr-gold); r: 5; }
.chart-grid { stroke: var(--border); stroke-width: 1; }

/* ---------- Toasts ---------- */
.toast-container {
    position: fixed; top: calc(var(--header-total-height) + 10px); left: 50%; transform: translateX(-50%);
    z-index: 60; display: flex; flex-direction: column; gap: 8px; width: min(92vw, 380px);
}
.toast {
    background: var(--bg-elevated); border: 1px solid var(--border); border-radius: var(--radius-sm);
    padding: 10px 14px; box-shadow: var(--shadow); font-size: 14px; animation: toast-in .2s ease;
}
.toast.success { border-color: var(--success); color: var(--success); }
.toast.error { border-color: var(--danger); color: var(--danger); }
.toast.pr { border-color: var(--pr-gold); color: var(--pr-gold); font-weight: 700; }
@keyframes toast-in { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; transform: translateY(0); } }

/* ---------- Empty states / misc ---------- */
.empty-state { text-align: center; padding: 40px 16px; color: var(--text-muted); }
.centered-screen {
    min-height: calc(100vh - var(--header-total-height));
    display: flex; flex-direction: column; justify-content: center; max-width: 380px; margin: 0 auto;
}
.text-center { text-align: center; }
.mt-16 { margin-top: 16px; }
.link-btn { background: none; border: none; color: var(--accent); font-size: 14px; cursor: pointer; padding: 6px 0; }
hr.sep { border: none; border-top: 1px solid var(--border); margin: 16px 0; }

@media (min-width: 700px) {
    .bottom-nav { max-width: 640px; left: 50%; transform: translateX(-50%); border-radius: var(--radius) var(--radius) 0 0; border-left: 1px solid var(--border); border-right: 1px solid var(--border); }
}
