:root {
    --bg: #0f1216;
    --surface: #171c22;
    --surface-2: #1f262e;
    --border: #2a323c;
    --text: #e6eaef;
    --muted: #93a0ad;
    --accent: #4ade80;
    --accent-2: #38bdf8;
    --danger: #f87171;
    --radius: 12px;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.35);
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    overflow-wrap: break-word;
}
img, canvas, svg, video { max-width: 100%; }

a { color: var(--accent-2); text-decoration: none; }

/* ---------- Top bar ---------- */
#topbar {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: max(0.6rem, env(safe-area-inset-top)) max(1rem, env(safe-area-inset-right)) 0.6rem max(1rem, env(safe-area-inset-left));
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 20;
    flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 1.15rem; color: var(--text); }
#mainnav { display: flex; gap: 0.25rem; flex: 1; flex-wrap: wrap; }
#mainnav a {
    color: var(--muted);
    padding: 0.35rem 0.7rem;
    border-radius: 8px;
    font-size: 0.95rem;
}
#mainnav a:hover { background: var(--surface-2); color: var(--text); }
#mainnav a.active { background: var(--surface-2); color: var(--accent); }
.topbar-right { display: flex; align-items: center; gap: 0.6rem; }
.whoami { color: var(--muted); font-size: 0.9rem; }

/* ---------- Layout ---------- */
.view { max-width: 960px; margin: 0 auto; padding: 1.25rem max(1rem, env(safe-area-inset-right)) 4rem max(1rem, env(safe-area-inset-left)); }
.page-title { margin: 0.25rem 0 1rem; font-size: 1.5rem; }
.row { display: flex; gap: 1rem; flex-wrap: wrap; }
.grid { display: grid; gap: 1rem; }
.muted { color: var(--muted); }
.center { text-align: center; }
.stack > * + * { margin-top: 0.75rem; }

/* ---------- Cards ---------- */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem;
    box-shadow: var(--shadow);
}
.card h3 { margin: 0 0 0.5rem; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 1rem; }

.stat-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 0.75rem; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 0.9rem; text-align: center; }
.stat .num { font-size: 1.8rem; font-weight: 700; color: var(--accent); }
.stat .lbl { color: var(--muted); font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.04em; }

/* ---------- Forms ---------- */
label { display: block; font-size: 0.85rem; color: var(--muted); margin-bottom: 0.25rem; }
input, select, textarea {
    width: 100%;
    padding: 0.6rem 0.7rem;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 9px;
    color: var(--text);
    font-size: 1rem;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-2); border-color: transparent; }
.field { margin-bottom: 0.85rem; }
.field-inline { display: flex; gap: 0.5rem; align-items: end; }

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    padding: 0.6rem 1rem;
    border: 1px solid transparent;
    border-radius: 9px;
    background: var(--accent);
    color: #06210f;
    font-weight: 600;
    font-size: 0.95rem;
    cursor: pointer;
}
.btn:hover { filter: brightness(1.05); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-sm { padding: 0.35rem 0.65rem; font-size: 0.85rem; }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { background: var(--danger); color: #2a0a0a; }
.btn-block { width: 100%; }

/* ---------- Tables / lists ---------- */
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 0.5rem 0.6rem; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-weight: 600; font-size: 0.85rem; }

.list-item {
    display: flex; align-items: center; justify-content: space-between; gap: 0.75rem;
    padding: 0.75rem 1rem;
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.list-item + .list-item { margin-top: 0.5rem; }
.tag {
    display: inline-block; padding: 0.1rem 0.5rem; border-radius: 999px;
    background: var(--surface-2); color: var(--muted); font-size: 0.75rem; border: 1px solid var(--border);
}
.tag.custom { color: var(--accent); border-color: var(--accent); }

/* Exercise images */
.exercise-thumb {
    width: 48px; height: 48px; flex-shrink: 0;
    object-fit: cover; border-radius: 9px;
    border: 1px solid var(--border); background: var(--surface-2);
}
.exercise-hero {
    width: 100%; max-height: 260px; object-fit: contain;
    border-radius: var(--radius); border: 1px solid var(--border);
    background: var(--surface-2);
}
select[multiple] { min-height: 7.5rem; padding: 0.2rem; }

/* ---------- Auth ---------- */
.auth-wrap { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: 1rem; }
.auth-card { width: 100%; max-width: 380px; }
.auth-logo { font-size: 2rem; text-align: center; margin-bottom: 0.5rem; }
.divider { display: flex; align-items: center; gap: 0.75rem; color: var(--muted); margin: 1rem 0; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }
#qr-video { width: 100%; border-radius: var(--radius); background: #000; aspect-ratio: 4/3; object-fit: cover; }

/* ---------- QR card ---------- */
.qr-card { display: grid; place-items: center; gap: 1rem; text-align: center; }
#qr-target { background: #fff; padding: 1rem; border-radius: var(--radius); }
#qr-target img, #qr-target canvas { display: block; }

/* ---------- Modal ---------- */
.modal-backdrop {
    position: fixed; inset: 0; background: rgba(0,0,0,0.6);
    display: grid; place-items: center; z-index: 50; padding: 1rem;
}
.modal { width: 100%; max-width: 520px; max-height: 90vh; overflow: auto; }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.75rem; }
.modal-head h3 { margin: 0; }

/* ---------- Toast ---------- */
.toast {
    position: fixed; left: 50%; bottom: max(1.5rem, calc(env(safe-area-inset-bottom) + 0.75rem)); transform: translateX(-50%);
    background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
    padding: 0.7rem 1.1rem; border-radius: 999px; box-shadow: var(--shadow); z-index: 100;
    max-width: 90vw;
}
.toast.error { border-color: var(--danger); color: #ffd9d9; }
.toast.ok { border-color: var(--accent); }

/* ---------- Inline "open details" name button ---------- */
.btn-link {
    display: inline-flex; align-items: center; gap: 0.35rem;
    background: none; border: 0; padding: 0; margin: 0;
    color: var(--text); font: inherit; font-weight: 600; text-align: left;
    cursor: pointer; min-width: 0;
    text-decoration: underline; text-decoration-style: dotted;
    text-decoration-color: var(--border); text-underline-offset: 3px;
}
.btn-link:hover, .btn-link:focus-visible { color: var(--accent); text-decoration-color: var(--accent); }
.btn-link-hint { font-size: 0.75rem; opacity: 0.55; text-decoration: none; }

/* ---------- Session draft (autosave) ---------- */
.draft-status { color: var(--muted); font-size: 0.8rem; }
.draft-notice {
    display: flex; align-items: center; justify-content: space-between; gap: 0.75rem;
    flex-wrap: wrap; padding: 0.6rem 0.9rem; margin-bottom: 0.75rem;
    border-color: var(--accent); font-size: 0.9rem;
}

/* ---------- Misc ---------- */
.spinner { text-align: center; color: var(--muted); padding: 2rem; }
.set-grid { display: grid; grid-template-columns: 1.4fr 0.8fr 0.8fr 0.7fr auto; gap: 0.4rem; align-items: center; }
.set-grid .head { color: var(--muted); font-size: 0.75rem; }
.set-grid input { min-width: 0; }
canvas { max-width: 100%; }

/* ---------- Touch devices: bigger, comfier tap targets ---------- */
@media (pointer: coarse) {
    .btn { min-height: 44px; }
    .btn-sm { min-height: 36px; }
    input, select, textarea { font-size: 16px; } /* keeps iOS from zooming on focus */
}

/* ---------- Phones ---------- */
@media (max-width: 640px) {
    /* Two-row header: brand + logout on top, a scrollable tab strip below. */
    #topbar {
        gap: 0.5rem 0.75rem;
        padding: max(0.5rem, env(safe-area-inset-top)) max(0.75rem, env(safe-area-inset-right)) 0.5rem max(0.75rem, env(safe-area-inset-left));
    }
    .brand { order: 0; font-size: 1.05rem; }
    .topbar-right { order: 1; margin-left: auto; }
    #mainnav {
        order: 2;
        flex: 1 1 100%;
        min-width: 0;
        flex-wrap: nowrap;
        overflow-x: auto;
        gap: 0.2rem;
        padding-bottom: 0.15rem;
        scrollbar-width: none;
        -webkit-overflow-scrolling: touch;
    }
    #mainnav::-webkit-scrollbar { display: none; }
    #mainnav a { flex: 0 0 auto; white-space: nowrap; padding: 0.4rem 0.75rem; }

    .view { padding: 1rem max(0.85rem, env(safe-area-inset-right)) calc(3.5rem + env(safe-area-inset-bottom)) max(0.85rem, env(safe-area-inset-left)); }
    .page-title { font-size: 1.3rem; }
    .whoami { display: none; }

    /* Keep the set logger a single aligned row that fits a phone. */
    .set-grid { grid-template-columns: 1.5rem 1fr 1fr 1fr auto; gap: 0.3rem; }
    .set-grid input { padding: 0.55rem 0.35rem; }

    /* Long names/notes shouldn't shove the action buttons off-screen. */
    .list-item { overflow-wrap: anywhere; }
    .list-item > div:first-child { min-width: 0; }

    .modal { max-height: 92dvh; }
}
