:root {
    /* Light Mode Palette */
    --bg-color: #f4f4f9;
    --container-bg: #ffffff;
    --text-color: #333333;
    --subtitle-color: #666666;
    --h1-color: #2c3e50;
    --table-header-bg: #2c3e50;
    --table-header-text: #ffffff;
    --table-head-bg: #f8f9fa;
    --row-alt-bg: #e8ecef;
    --link-color: #e74c3c;
    --border-color: #bdc3c7;
    --success-color: #27ae60;
    --checkbox-border: #bdc3c7;
    /* Tells the browser which color scheme to use BEFORE CSS loads, preventing dark-mode flash */
    color-scheme: light dark;
}

/* Auto-detects device dark mode preference */
@media (prefers-color-scheme: dark) {
    :root {
        /* Dark Mode Palette */
        --bg-color: #121212;
        --container-bg: #1e1e1e;
        --text-color: #e2e8f0;
        --subtitle-color: #a0aec0;
        --h1-color: #f8f9fa;
        --table-header-bg: #2d3748;
        --table-header-text: #ffffff;
        --table-head-bg: #2a2a2a;
        --row-alt-bg: #252525;
        --link-color: #fc8181;
        --border-color: #3d3d3d;
        --success-color: #48bb78;
        --checkbox-border: #4a5568;
    }
}

/* Single universal box-sizing reset */
*, *:before, *:after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

/* Prevents the entire page from scrolling horizontally */
html, body {
    overflow-x: hidden;
    width: 100%;
    position: relative;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    line-height: 1.6;
    padding: 20px;
    max-width: 1200px;
    margin: 0 auto;
    transition: background-color 0.3s ease, color 0.3s ease;
}

h1 {
    text-align: center;
    margin-bottom: 5px;
    color: var(--h1-color);
    transition: color 0.3s ease;
}

p.subtitle {
    text-align: center;
    margin-bottom: 25px;
    font-size: 0.9rem;
    color: var(--subtitle-color);
}

/* History Log Styles */
.history-container {
    margin-bottom: 30px;
    background: var(--container-bg);
    border-radius: 8px;
    box-shadow: 0 4px 6px rgba(0,0,0,0.1);
    padding: 15px 20px;
    border-left: 5px solid var(--success-color);
    transition: background-color 0.3s ease;
}

summary {
    font-size: 1.2rem;
    font-weight: bold;
    cursor: pointer;
    color: var(--text-color);
    outline: none;
    user-select: none;
}

summary:hover {
    color: var(--success-color);
}

.history-content {
    margin-top: 15px;
}

#history-list {
    list-style-type: none;
    max-height: 250px;
    overflow-y: auto;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    padding: 10px;
    background: var(--bg-color);
}

#history-list li {
    padding: 8px 5px;
    border-bottom: 1px solid var(--border-color);
    font-size: 0.95rem;
}

#history-list li:last-child {
    border-bottom: none;
}

.timestamp {
    font-weight: bold;
    color: var(--success-color);
    margin-right: 10px;
}

/* Wrapper that holds the Clear and Share buttons side by side */
.history-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 10px;
    align-items: center;
}

.clear-btn {
    background-color: var(--link-color);
    color: white;
    border: none;
    padding: 8px 15px;
    border-radius: 4px;
    cursor: pointer;
    font-weight: bold;
    font-size: 0.85rem;
    transition: opacity 0.2s ease;
    /* Removes the 300ms tap delay on iOS without disabling pinch-zoom */
    touch-action: manipulation;
}

.clear-btn:hover {
    opacity: 0.8;
}

/* Share button — hidden by default, shown only when navigator.share is available */
.share-btn {
    display: none;
    background: none;
    border: 2px solid var(--success-color);
    color: var(--success-color);
    padding: 8px 15px;
    border-radius: 4px;
    cursor: pointer;
    font-weight: bold;
    font-size: 0.85rem;
    font-family: inherit;
    align-items: center;
    gap: 6px;
    transition: background-color 0.2s ease, color 0.2s ease;
    touch-action: manipulation;
}

.share-btn:hover {
    background-color: var(--success-color);
    color: white;
}

/* Table Styles */
.day-container {
    margin-bottom: 20px;
    background: var(--container-bg);
    border-radius: 8px;
    box-shadow: 0 4px 6px rgba(0,0,0,0.1);
    overflow: hidden;
    transition: background-color 0.3s ease;
}

.day-header {
    background-color: var(--table-header-bg);
    color: var(--table-header-text);
    padding: 15px 20px;
    font-size: 1.2rem;
    font-weight: bold;
    cursor: pointer;
    outline: none;
    user-select: none;
    display: flex;
    flex-direction: column;
    gap: 12px;
    touch-action: manipulation;
}

.day-header:hover {
    opacity: 0.9;
}

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

table {
    width: 100%;
    border-collapse: collapse;
    min-width: 650px;
}

th, td {
    padding: 15px;
    text-align: left;
    border-bottom: 1px solid var(--border-color);
}

th {
    background-color: var(--table-head-bg);
    font-weight: 600;
    color: var(--text-color);
}

tr:nth-child(even) {
    background-color: var(--row-alt-bg);
}

td.checkbox-cell {
    text-align: center;
    width: 60px;
}

/* --- Dynamic Inputs & Timers --- */
.reps-cell {
    min-width: 100px;
    font-weight: 500;
}

.sets-cell {
    min-width: 250px;
}

.set-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 10px;
    margin-bottom: 12px;
    padding-bottom: 12px;
    border-bottom: 1px dashed var(--border-color);
}

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

.set-control-group {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 85px;
    touch-action: manipulation;
}

.set-label {
    margin-left: 8px;
    font-size: 0.95rem;
    /* Specific transition instead of 'all' to avoid unexpected side-effects */
    transition: color 0.2s ease, text-decoration 0.2s ease;
}

.completed-set {
    text-decoration: line-through;
    color: var(--subtitle-color);
}

/* Custom Checkbox Styling */
input[type="checkbox"].exercise-cb {
    appearance: none;
    -webkit-appearance: none;
    width: 24px;
    height: 24px;
    border: 2px solid var(--checkbox-border);
    border-radius: 50%;
    background-color: transparent;
    cursor: pointer;
    position: relative;
    transition: background-color 0.2s ease, border-color 0.2s ease;
    margin-right: 5px;
    /* Enlarges the tap target without changing visual size */
    touch-action: manipulation;
}

input[type="checkbox"].exercise-cb:checked {
    background-color: var(--success-color);
    border-color: var(--success-color);
}

input[type="checkbox"].exercise-cb:checked::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -60%) rotate(45deg);
    width: 5px;
    height: 10px;
    border: solid white;
    border-width: 0 2px 2px 0;
}

/* --- Inline Video Expansion --- */
button.video-toggle {
    background: none;
    border: none;
    color: var(--link-color);
    font-weight: 600;
    font-size: 0.9rem;
    cursor: pointer;
    padding: 4px 0;
    display: inline-flex;
    align-items: center;
    margin-top: 8px;
    font-family: inherit;
    touch-action: manipulation;
}

button.video-toggle::before {
    content: "▶";
    font-size: 0.7rem;
    margin-right: 5px;
    transition: transform 0.2s ease;
}

button.video-toggle[aria-expanded="true"]::before {
    transform: rotate(90deg);
}

button.video-toggle:hover {
    text-decoration: underline;
}

.inline-video-container {
    display: none;
    margin-top: 10px;
    width: 100%;
    max-width: 400px;
    background: #000;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 4px 6px rgba(0,0,0,0.3);
}

.inline-video-container.show {
    display: block;
    animation: sweep .3s ease-in-out;
}

.inline-video-container iframe {
    width: 100%;
    aspect-ratio: 16 / 9;
    border: none;
    display: block;
}

/* --- Video Facade (Thumbnail Preview) ---
   Shown on first "Watch Video" click. The real iframe is only loaded
   when the user taps the play button, saving bandwidth and memory. */
.video-facade {
    position: relative;
    width: 100%;
    cursor: pointer;
    border-radius: 8px;
    overflow: hidden;
    background: #000;
    line-height: 0; /* eliminates phantom space beneath the img element */
}

.video-facade img {
    width: 100%;
    aspect-ratio: 16 / 9;
    display: block;
    object-fit: cover;
    object-position: center;
}

.facade-play-btn {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(0, 0, 0, 0.65);
    color: white;
    border: 3px solid rgba(255, 255, 255, 0.9);
    border-radius: 50%;
    width: 64px;
    height: 64px;
    font-size: 1.4rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    /* Nudge right to visually center the ▶ glyph inside the circle */
    padding-left: 4px;
    transition: background 0.2s ease, transform 0.2s ease, border-color 0.2s ease;
    touch-action: manipulation;
}

.facade-play-btn:hover {
    background: rgba(231, 76, 60, 0.9);
    border-color: white;
    transform: translate(-50%, -50%) scale(1.1);
}

.facade-play-btn:active {
    transform: translate(-50%, -50%) scale(0.95);
}

/* Animations */
details[open] summary ~ * {
    animation: sweep .3s ease-in-out;
}

@keyframes sweep {
    0%    { opacity: 0; margin-top: -10px; }
    100%  { opacity: 1; margin-top: 0; }
}

/* Respects the user's OS-level "Reduce Motion" accessibility preference */
@media (prefers-reduced-motion: reduce) {
    *, *:before, *:after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

.progress-background {
    height: 6px;
    background-color: rgba(255, 255, 255, 0.2);
    border-radius: 3px;
    width: 100%;
    overflow: hidden;
}

.progress-fill {
    height: 100%;
    background-color: var(--success-color);
    width: 0%;
    transition: width 0.4s ease;
}

#toast-container {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 9999;
    display: flex;
    flex-direction: column;
    gap: 10px;
    pointer-events: none;
}

.toast {
    background-color: var(--table-header-bg);
    color: var(--table-header-text);
    padding: 12px 20px;
    border-radius: 6px;
    font-size: 0.9rem;
    font-weight: 500;
    box-shadow: 0 4px 12px rgba(0,0,0,0.3);
    opacity: 0;
    transform: translateY(20px);
    animation: slideIn 0.3s forwards;
    pointer-events: auto;
}

.toast.success { border-left: 5px solid var(--success-color); }
.toast.error   { border-left: 5px solid var(--link-color); }
.toast.fadeOut { animation: fadeOut 0.3s forwards; }

@keyframes slideIn { to { opacity: 1; transform: translateY(0); } }
@keyframes fadeOut { to { opacity: 0; transform: translateY(20px); } }

/* --- Dynamic Inputs & Timers --- */
.weight-input {
    width: 60px;
    padding: 4px;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    background: var(--bg-color);
    color: var(--text-color);
    font-size: 0.9rem;
    outline: none;
}

.weight-input:focus {
    border-color: var(--success-color);
}

.inline-timer {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    background: var(--table-head-bg);
    padding: 2px 8px;
    border-radius: 6px;
    border: 1px solid var(--border-color);
}

.time-display {
    font-weight: 600;
    font-family: monospace;
    font-size: 0.95rem;
    min-width: 32px;
    text-align: center;
}

.timer-btn {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-color);
    font-size: 1rem;
    /* 44×44px minimum tap target per Apple HIG — visually unchanged */
    min-width: 44px;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: color 0.2s ease;
    touch-action: manipulation;
}

.timer-btn:hover {
    color: var(--link-color);
}

/* --- Settings & Pill Selector --- */
.settings-container {
    margin-bottom: 20px;
    background: var(--container-bg);
    border-radius: 8px;
    box-shadow: 0 4px 6px rgba(0,0,0,0.1);
    padding: 15px 20px;
    display: flex;
    transition: background-color 0.3s ease;
}

.settings-container.vertical {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
}

.settings-container label {
    font-weight: 700;
    color: var(--subtitle-color);
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Day Pills — 7-column grid so all days always fit without horizontal scrolling */
.day-selector-pills {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 8px;
    width: 100%;
}

.day-pill {
    padding: 10px 4px;
    border-radius: 25px;
    border: 2px solid var(--border-color);
    background: var(--bg-color);
    color: var(--text-color);
    font-weight: 600;
    font-size: 0.95rem;
    cursor: pointer;
    white-space: nowrap;
    transition: background-color 0.2s ease, border-color 0.2s ease, color 0.2s ease;
    outline: none;
    font-family: inherit;
    text-align: center;
    touch-action: manipulation;
}

/* Retains A11y keyboard focus */
.day-pill:focus-visible {
    border-color: var(--link-color);
    box-shadow: 0 0 0 3px rgba(231, 76, 60, 0.2);
}

.day-pill:active {
    transform: scale(0.95);
}

.day-pill.active {
    background: var(--link-color);
    color: #ffffff;
    border-color: var(--link-color);
    box-shadow: 0 4px 10px rgba(0,0,0,0.15);
}

.day-stepper {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--table-head-bg);
    border: 2px solid var(--border-color);
    border-radius: 8px;
    padding: 6px;
    width: 100%;
}

.stepper-btn {
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    color: var(--text-color);
    font-size: 1.2rem;
    padding: 12px 24px;
    border-radius: 6px;
    cursor: pointer;
    transition: border-color 0.2s ease, color 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: manipulation;
}

.stepper-btn:hover,
.stepper-btn:focus-visible {
    border-color: var(--link-color);
    color: var(--link-color);
    outline: none;
}

.stepper-btn:active {
    transform: scale(0.95);
    background: var(--border-color);
}

.stepper-display {
    font-weight: 700;
    font-size: 1.1rem;
    color: var(--text-color);
    text-transform: uppercase;
    letter-spacing: 1px;
    min-width: 120px;
    text-align: center;
    user-select: none;
}

/* Custom Dropdown Arrow */
.select-wrapper::after {
    content: "▼";
    position: absolute;
    right: 20px;
    top: 50%;
    transform: translateY(-50%);
    pointer-events: none;
    color: var(--link-color);
    font-size: 0.8rem;
}

#climbing-day-select {
    appearance: none;
    -webkit-appearance: none;
    width: 100%;
    padding: 16px 20px;
    border-radius: 8px;
    border: 2px solid var(--border-color);
    background-color: var(--table-head-bg);
    color: var(--text-color);
    font-size: 1.1rem;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    outline: none;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

#climbing-day-select:focus {
    border-color: var(--link-color);
    box-shadow: 0 0 0 3px rgba(231, 76, 60, 0.2);
}

@media (prefers-color-scheme: dark) {
    #climbing-day-select:focus {
        box-shadow: 0 0 0 3px rgba(252, 129, 129, 0.2);
    }
}

/* --- A11Y Keyboard Navigation & Focus States --- */
:focus {
    outline: none;
}

/* Shows focus rings for keyboard navigators only */
:focus-visible {
    outline: 3px solid var(--link-color);
    outline-offset: 3px;
    border-radius: 4px;
}

input[type="checkbox"].exercise-cb:focus-visible {
    outline: 3px solid var(--link-color);
    outline-offset: 2px;
}

label.set-control-group {
    cursor: pointer;
}

/* --- Portfolio Footer --- */
.app-footer {
    margin-top: 50px;
    padding-top: 30px;
    padding-bottom: 40px;
    border-top: 1px solid var(--border-color);
    text-align: center;
}

.footer-content {
    display: flex;
    justify-content: center;
    align-items: center;
}

.portfolio-link {
    color: var(--subtitle-color);
    text-decoration: none;
    font-size: 0.85rem;
    font-weight: 500;
    transition: color 0.2s ease;
}

.portfolio-link:hover,
.portfolio-link:focus-visible {
    color: var(--link-color);
    outline: none;
}

.day-container.today-highlight {
    border: 2px solid var(--success-color);
    box-shadow: 0 0 15px rgba(39, 174, 96, 0.2);
}

@media (prefers-color-scheme: dark) {
    .day-container.today-highlight {
        box-shadow: 0 0 15px rgba(72, 187, 120, 0.3);
    }
}

/* ============================================================
   MOBILE RESPONSIVE (all mobile rules consolidated here)
   ============================================================ */
@media (max-width: 768px) {

    body {
        padding: 10px;
    }

    .day-header {
        font-size: 1.1rem;
    }

    /* Slightly smaller text on very narrow screens (iPhone SE etc.) */
    @media (max-width: 400px) {
        .day-pill {
            font-size: 0.78rem;
            padding: 8px 2px;
        }
    }

    /* Disable the desktop horizontal scroll wrapper — cards use block layout instead */
    .table-responsive {
        overflow-x: hidden !important;
    }

    /* Convert table elements to block layout for card-style display */
    .table-responsive table,
    .table-responsive tbody,
    .table-responsive tr,
    .table-responsive td {
        display: block !important;
        width: 100% !important;
        max-width: 100% !important;
        /* Overrides table{min-width:650px}, .sets-cell{min-width:250px}, .reps-cell{min-width:100px}
           which would otherwise all beat width:100% since min-width always wins */
        min-width: 0 !important;
        box-sizing: border-box !important;
        margin: 0 !important;
    }

    /* Hide the desktop column headers */
    .table-responsive thead {
        display: none !important;
    }

    /* Each exercise row becomes a self-contained card */
    .table-responsive tr {
        background: var(--container-bg) !important;
        border: 1px solid var(--border-color) !important;
        border-radius: 12px !important;
        margin-bottom: 16px !important;
        padding: 16px !important;
        box-shadow: 0 4px 6px rgba(0,0,0,0.05) !important;
        display: flex !important;
        flex-direction: column !important;
        overflow: hidden !important;
    }

    /* Force all text to wrap and never overflow the card */
    .table-responsive td {
        white-space: normal !important;
        word-wrap: break-word !important;
        overflow-wrap: break-word !important;
        text-align: left !important;
    }

    /* 1st column: Exercise name */
    .table-responsive td:nth-of-type(1) {
        order: 1;
        font-size: 1.25rem !important;
        font-weight: 800 !important;
        border-bottom: 1px solid var(--border-color) !important;
        padding: 0 0 8px 0 !important;
        margin-bottom: 8px !important;
    }

    /* 3rd column: Reps/Hold — moved under the title */
    .table-responsive td:nth-of-type(3) {
        order: 2;
        font-weight: 700 !important;
        color: var(--subtitle-color) !important;
        padding: 0 0 12px 0 !important;
        font-size: 1.05rem !important;
    }

    /* 2nd column: Description & Video */
    .table-responsive td:nth-of-type(2) {
        order: 3;
        padding: 0 0 16px 0 !important;
        font-size: 0.95rem !important;
        line-height: 1.5 !important;
    }

    /* Video container: fill full card width on mobile */
    .inline-video-container {
        max-width: 100% !important;
    }

    .inline-video-container.show {
        margin-top: 12px !important;
        width: 100% !important;
    }

    /* Match iframe fixed height for the facade thumbnail on mobile */
    .inline-video-container iframe,
    .video-facade img {
        height: 200px !important;
        width: 100% !important;
        max-width: 100% !important;
        border-radius: 8px !important;
        aspect-ratio: unset !important;
        object-fit: cover !important;
    }

    /* 4th column: Sets/Checkboxes */
    .table-responsive td:nth-of-type(4) {
        order: 4;
        background: var(--table-head-bg) !important;
        border-radius: 8px !important;
        border: 1px solid var(--border-color) !important;
        padding: 12px !important;
    }

    .set-row {
        display: flex !important;
        flex-wrap: wrap !important;
        align-items: center !important;
        justify-content: space-between !important;
        width: 100% !important;
        padding: 0 0 8px 0 !important;
        margin: 0 0 8px 0 !important;
        border-bottom: 1px dashed var(--border-color) !important;
    }

    .set-row:last-child {
        border-bottom: none !important;
        margin-bottom: 0 !important;
        padding-bottom: 0 !important;
    }

    .set-control-group {
        flex-shrink: 0 !important;
        margin-right: auto !important;
    }

    .weight-input {
        width: 70px !important;
        padding: 6px !important;
        text-align: center !important;
        margin-left: 8px !important;
    }
}

/* ============================================================
   TIMER URGENCY — visual pulse at ≤3 seconds remaining
   Pairs with the Web Audio countdown beeps for dual-sensory cue.
   Respects prefers-reduced-motion (the global rule above disables
   the animation for users who have that preference set).
   ============================================================ */
.inline-timer.timer-urgent .time-display {
    color: var(--link-color);
    animation: pulse-urgent 0.5s ease-in-out infinite alternate;
}

@keyframes pulse-urgent {
    from { opacity: 1;   transform: scale(1);    }
    to   { opacity: 0.5; transform: scale(1.05); }
}

/* ============================================================
   UPDATE TOAST — persistent, clickable, dismisses only on reload
   ============================================================ */
.toast.update {
    border-left: 5px solid #3498db;
    cursor: pointer;
}

.toast.update:hover {
    opacity: 0.9;
}

/* ============================================================
   ACTIVE TIMER BANNER
   Fixed pill at the bottom of the screen showing the currently
   running timer's exercise name and live countdown.
   Hidden by default; JS adds .visible when a timer is active.
   Tapping scrolls the timer widget into view.
   ============================================================ */
#active-timer-bar {
    position: fixed;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%) translateY(16px);
    background: var(--table-header-bg);
    color: var(--table-header-text);
    padding: 10px 20px;
    border-radius: 28px;
    border: 2px solid var(--success-color);
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 0.9rem;
    font-weight: 600;
    white-space: nowrap;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.35);
    z-index: 9998;
    cursor: pointer;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease, transform 0.3s ease;
}

#active-timer-bar.visible {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
    pointer-events: auto;
}

.active-timer-icon {
    font-size: 1rem;
    flex-shrink: 0;
}

.active-timer-label {
    max-width: 160px;
    overflow: hidden;
    text-overflow: ellipsis;
    font-weight: 600;
}

.active-timer-time {
    font-family: monospace;
    font-size: 1.1rem;
    color: var(--success-color);
    min-width: 36px;
    text-align: right;
    flex-shrink: 0;
}

.active-timer-hint {
    font-size: 0.72rem;
    font-weight: 400;
    opacity: 0.55;
    flex-shrink: 0;
}

/* On very narrow phones, compress the hint text */
@media (max-width: 400px) {
    #active-timer-bar {
        padding: 8px 14px;
        gap: 7px;
        font-size: 0.82rem;
    }
    .active-timer-label {
        max-width: 110px;
    }
    .active-timer-hint {
        display: none;
    }
}
