:root {
    --bg: #121212;
    --surface: #1e1e1e;
    --surface-hover: #2c2c2c;
    --primary: #3b82f6;      
    --accent: #ef4444;       
    --min-color: #a855f7;    
    --text: #e5e5e5;
    --muted: #888;
    --border: #333;
    --loop-color: #f59e0b;

    /* Category Colors */
    --cat-cardio: #3b82f6;
    --cat-strength: #ef4444;
    --cat-yoga: #10b981;
    --cat-boxing: #f59e0b;
}

* { box-sizing: border-box; }

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--bg);
    color: var(--text);
    margin: 0;

    /* Use Dynamic Viewport Height so address bars don't cover content */
    height: 100vh;        /* Fallback */
    height: 100dvh;       /* Modern Mobile Fix */

    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* --- UTILS --- */
.hidden { display: none !important; }
.flex { display: flex; }
.center { align-items: center; justify-content: center; }
.gap-2 { gap: 0.5rem; }
.grow { flex-grow: 1; }
.text-xl { font-size: 1.25rem; font-weight: bold; }
.muted { color: var(--muted); }

/* --- BUTTONS & INPUTS --- */
button {
    border: none;
    padding: 10px 14px; 
    border-radius: 6px;
    font-weight: 600;
    cursor: pointer;
    transition: 0.2s;
    font-family: inherit;
    font-size: 0.9rem;
    flex-shrink: 0;
}
button:active { transform: scale(0.98); }
button:hover { opacity: 0.9; }

.btn-primary { background-color: var(--primary); color: white; }
.btn-danger { background-color: var(--accent); color: white; }
.btn-ghost { background-color: transparent; color: var(--muted); border: 1px solid var(--border); }
.btn-ghost:hover { background-color: var(--surface); color: white; }
.btn-lg { padding: 15px 25px; font-size: 1.1rem; }
.btn-sm { padding: 6px 10px; font-size: 0.8rem; }
.btn-glass { background: rgba(255,255,255,0.2); border: 1px solid rgba(255,255,255,0.3); color: white; backdrop-filter: blur(4px); }

.unit-toggle { font-size: 0.75rem; padding: 6px 8px; min-width: 45px; color: white; }
.unit-s { background-color: #444; }
.unit-m { background-color: var(--min-color); }

.dot-red { background-color: #ef4444; }
.dot-green { background-color: #22c55e; }
.dot-blue { background-color: #3b82f6; }

input, select {
    background: transparent;
    border: 1px solid transparent;
    color: white;
    padding: 10px;
    border-radius: 6px;
    font-family: inherit;
    font-size: 1rem;
    transition: all 0.2s ease;
}

input:hover, select:hover {
    background: rgba(255, 255, 255, 0.05);
}

input:focus, select:focus {
    background: var(--surface-hover);
    border-color: var(--primary);
    outline: none;
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.3);
}

/* Ensure inputs in the toolbar are slightly more visible */
.toolbar input, .toolbar select {
    background: rgba(255,255,255,0.05);
    border-color: rgba(255,255,255,0.1);
}
.toolbar input:focus, .toolbar select:focus {
    background: var(--surface-hover);
    border-color: var(--primary);
}

.color-dot {
    width: 42px; height: 42px; /* Larger touch target */
    border-radius: 50%; 
    border: 2px solid rgba(255,255,255,0.1);
    cursor: pointer; 
    box-shadow: 0 4px 6px rgba(0,0,0,0.2);
    transition: transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    flex-shrink: 0;
}
.color-dot:active { transform: scale(0.9); }
.color-dot:hover { border-color: white; transform: scale(1.05); }

/* --- MAIN LAYOUT --- */
#app-container { display: flex; height: 100%; width: 100%; overflow: hidden; position: relative; }

#editor { flex: 1; display: flex; flex-direction: column; max-width: 100%; height: 100%; background: var(--bg); }

/* Toolbar */
.toolbar { border-bottom: 1px solid var(--border); background: var(--surface); padding: 10px 15px; display: flex; flex-direction: column; gap: 10px; }
.toolbar-row { 
    display: flex; 
    justify-content: space-between; 
    align-items: center; 
    gap: 20px;           /* Adds safety space between Title and Time */
}
.toolbar-controls { display: flex; gap: 8px; flex-wrap: wrap; justify-content: space-between; }
.branding { color: var(--primary); }
.save-group { display: flex; gap: 5px; flex-grow: 1; }
#routine-name { flex-grow: 1; min-width: 80px; }

/* Scroll Area */
.scroll-area { flex: 1; overflow-y: auto; padding: 15px; -webkit-overflow-scrolling: touch; }
.block-list { display: flex; flex-direction: column; gap: 12px; max-width: 800px; margin: 0 auto; width: 100%; }
.add-buttons-area { margin-top: 20px; border-top: 1px solid #333; padding-top: 20px; padding-bottom: 40px; }

/* Animation Keyframes */
@keyframes slideIn {
    from { opacity: 0; transform: translateY(10px) scale(0.98); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}

/* Blocks */
.block { 
    background: var(--surface); 
    padding: 12px; 
    border-radius: 12px; /* Softer corners */
    display: flex; 
    align-items: center; 
    gap: 12px; 
    border-left: 4px solid #555; 
    flex-wrap: wrap;
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
    animation: slideIn 0.25s ease-out backwards; /* Smooth entry */
    transition: transform 0.2s, box-shadow 0.2s;
}

/* Lift effect on hover for desktop */
.block:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}

.block-content { display: flex; align-items: center; gap: 8px; flex-grow: 1; flex-wrap: wrap; }
.block input[type="text"] { font-weight: 600; flex: 1; min-width: 100px; font-size: 1.05rem; }
.block input[type="number"] { width: 70px; text-align: center; font-variant-numeric: tabular-nums; }

.loop-block { 
    background: #181818; 
    border: 1px dashed var(--loop-color); 
    padding: 16px; /* More breathing room */
    border-radius: 12px; 
    animation: slideIn 0.25s ease-out backwards;
}
.loop-header { color: var(--loop-color); font-weight: bold; margin-bottom: 12px; display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 10px; }
.loop-controls { display: flex; align-items: center; gap: 8px; }
.loop-children { padding-left: 10px; display: flex; flex-direction: column; gap: 10px; border-left: 2px solid #333; }

/* specific styling for the loop iteration number */
.loop-header input[type="number"] {
    width: 80px !important; /* Force it wider */
    font-size: 1.1rem;      /* Larger text */
    font-weight: bold;
    text-align: center;
    padding: 8px;
    margin: 0 5px;         /* Space around it */
}

.loop-controls span {
    font-size: 1rem;
    font-weight: 600;
}

.loop-status-container {
    display: flex;
    flex-direction: column; /* Stack if nested loops */
    gap: 8px;
    margin-bottom: 15px;
    align-items: center;
    min-height: 30px; /* Prevent layout jump */
}

.loop-badge {
    background: rgba(255, 255, 255, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.4);
    padding: 6px 16px;
    border-radius: 20px;
    font-size: 1.2rem;
    font-weight: 600;
    color: white;
    letter-spacing: 1px;
    display: flex;
    align-items: center;
    gap: 10px;
    box-shadow: 0 4px 6px rgba(0,0,0,0.2);
    backdrop-filter: blur(4px);
}

.loop-badge span {
    color: var(--loop-color); /* The Yellow/Orange color */
    font-weight: 800;
}

/* On mobile, make sure it doesn't get too huge */
@media (max-width: 600px) {
    .loop-badge { font-size: 1rem; padding: 4px 12px; }
}

/* Action Bar */
.action-bar {
    padding: 15px;
    background: var(--surface);
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: center;

    /* Add extra padding for the mobile home indicator/nav bar */
    padding-bottom: calc(15px + env(safe-area-inset-bottom));
}

.start-btn {
    width: 100%;
    max-width: 400px;
    padding: 18px;
    font-size: 1.2rem;
}

/* --- MODAL & LIBRARY --- */
.modal {
    position: fixed; top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.8); z-index: 200;
    display: flex; align-items: center; justify-content: center;
    backdrop-filter: blur(5px);
}
.modal-content {
    background: var(--bg); border: 1px solid var(--border);
    width: 90%; max-width: 600px; max-height: 85vh;
    border-radius: 12px; display: flex; flex-direction: column;
}
.modal-header { padding: 15px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; background: var(--surface); border-radius: 12px 12px 0 0; }
.modal-body { padding: 20px; overflow-y: auto; }

.lib-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 10px; margin-top: 10px; }

.lib-card {
    background: var(--surface); padding: 15px; border-radius: 8px;
    border-left: 5px solid #555; display: flex; justify-content: space-between; align-items: center;
}
.lib-info { display: flex; flex-direction: column; }
.lib-name { font-weight: bold; margin-bottom: 4px; }
.lib-meta { font-size: 0.8rem; color: var(--muted); }
.lib-actions { display: flex; gap: 5px; }

/* Category Border Colors */
.cat-cardio { border-color: var(--cat-cardio) !important; }
.cat-strength { border-color: var(--cat-strength) !important; }
.cat-yoga { border-color: var(--cat-yoga) !important; }
.cat-boxing { border-color: var(--cat-boxing) !important; }

/* --- PLAYER --- */
#player { position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: black; z-index: 100; display: flex; transition: background-color 0.5s ease; }
#player.bg-red { background-color: #7f1d1d; }
#player.bg-green { background-color: #14532d; }
#player.bg-blue { background-color: #1e3a8a; }

.player-main { flex: 2; display: flex; flex-direction: column; align-items: center; justify-content: center; border-right: 1px solid rgba(255,255,255,0.1); padding: 20px; text-align: center; }
.timer-big { font-size: 18vh; font-variant-numeric: tabular-nums; line-height: 1; font-weight: 700; margin: 20px 0; color: white; text-shadow: 0 4px 10px rgba(0,0,0,0.3); }
.action-name { font-size: 4vh; color: white; text-transform: uppercase; font-weight: 800; word-break: break-word; text-shadow: 0 2px 5px rgba(0,0,0,0.3); }
.progress-track { width: 80%; height: 8px; background: rgba(0,0,0,0.3); border-radius: 4px; overflow: hidden; margin-top: 20px; }
.progress-fill { height: 100%; background: white; width: 0; transition: width 1s linear; }
.player-controls { margin-top: 40px; display: flex; gap: 20px; }

.player-sidebar { flex: 1; min-width: 300px; background: rgba(0,0,0,0.3); backdrop-filter: blur(10px); display: flex; flex-direction: column; border-left: 1px solid rgba(255,255,255,0.1); }
.timeline-header { padding: 15px; border-bottom: 1px solid rgba(255,255,255,0.1); font-weight: bold; background: rgba(0,0,0,0.2); }
.timeline-scroll { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.timeline-item { padding: 14px 15px; border-bottom: 1px solid rgba(255,255,255,0.1); cursor: pointer; display: flex; justify-content: space-between; color: rgba(255,255,255,0.6); }
.timeline-item.active { background: rgba(255, 255, 255, 0.2); border-left: 4px solid white; color: white; }
.timeline-time { font-family: monospace; }

/* --- TIMELINE LOOP VISUALS --- */

/* CONSTANTS for the reference:
   Loop Header Height: 40px
   Round Marker Height: 30px
*/

/* 1. Major Header (Loop Name) */
.timeline-loop-start {
    background: #222;
    color: var(--loop-color);

    height: 40px;           /* Fixed height */
    padding: 0 10px;
    display: flex;
    align-items: center;

    font-size: 0.85rem;
    font-weight: 800;
    text-transform: uppercase;

    border-top: 1px solid #444;
    border-bottom: 1px solid #444;
    border-left: 4px solid var(--loop-color);
    margin-top: 0;

    position: sticky;       /* STICKY */
    box-shadow: 0 2px 5px rgba(0,0,0,0.5);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* 2. Minor Header (Round Number) */
.timeline-round-marker {
    background: #333;       /* Needs opaque background to hide scrolling items */
    color: #ccc;

    height: 30px;           /* Fixed height */
    padding: 0 10px;
    display: flex;
    align-items: center;

    font-size: 0.75rem;
    font-weight: bold;
    border-left: 1px dotted #666;
    border-bottom: 1px solid #444;
    text-transform: uppercase;

    position: sticky;       /* STICKY */
    box-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

.timeline-item {
    background: var(--surface);
    border-bottom: 1px solid rgba(255,255,255,0.05);
}

/* The separator row showing "Round X / Y" */
.timeline-loop-header {
    background: #1a1a1a;
    border-left: 2px solid var(--loop-color);
    color: var(--loop-color);
    padding: 6px 10px;
    font-size: 0.75rem;
    font-weight: bold;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-top: 5px;
    margin-bottom: 2px;

    /* Sticky header effect (optional, keeps it visible while scrolling) */
    position: sticky;
    top: 0;
    z-index: 5;
}

/* Ensure items respect left padding (indentation) */
.timeline-item {
    transition: all 0.2s;
    /* We will set padding-left dynamically in JS */
}

/* Add a subtle connection line for indented items */
.timeline-item.indented {
    border-left: 1px solid #333;
}

/* --- MOBILE FIXES --- */
@media (max-width: 600px) {

    /* 1. Toolbar Controls: Allow wrapping */
    .toolbar-controls {
        display: flex;
        flex-wrap: wrap;
        gap: 10px;
        width: 100%;
    }

    /* 2. Top Buttons (Help/Library): Share first row */
    .toolbar-controls > button {
        flex: 1;
        text-align: center;
        justify-content: center;
    }

    /* 3. Save Group: Grid Layout for strict positioning */
    .save-group {
        width: 100%;
        margin-top: 5px;
        display: grid;
        /* Col 1: Fits content (Category), Col 2: Fills rest (Name) */
        grid-template-columns: auto 1fr;
        grid-template-rows: auto auto; /* Two rows */
        gap: 8px;
        align-items: center;
    }

    /* --- ROW 1 --- */
    #routine-category {
        grid-row: 1;
        grid-column: 1;
        width: 100%;
    }

    #routine-name {
        grid-row: 1;
        grid-column: 2;
        width: 100%;
        min-width: 0;
    }

    /* --- ROW 2 --- */
    /* Target the Save button (1st button in the group) */
    .save-group > button:nth-of-type(1) {
        grid-row: 2;
        grid-column: 1;
        width: 100%; /* Fill the small column under category */
    }

    /* Target the JSON button (2nd button in the group) */
    .save-group > button:nth-of-type(2) {
        grid-row: 2;
        grid-column: 2;
        justify-self: start; /* Don't stretch, align left */
        min-width: 100px;    /* But keep it clickable */
    }
}
/* --- HELP MODAL VISUALS --- */
.help-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 30px; /* More space between topics */
}

.help-item {
    display: flex;
    flex-direction: column;
    gap: 10px;
    border-bottom: 1px solid #333;
    padding-bottom: 20px;
}

.help-item:last-child { border-bottom: none; }

.help-text h4 { margin: 0 0 5px 0; color: var(--primary); font-size: 1.1rem; }
.help-text p { margin: 0 0 10px 0; font-size: 0.95rem; color: #ccc; line-height: 1.5; }

/* "Demo" Container - scales down the UI elements slightly */
.demo-ui {
    pointer-events: none; /* Prevents clicking on help examples */
    opacity: 1;
    transform: scale(0.95);
    transform-origin: left top;
    width: 100%;
    margin-top: 5px;
}

/* Adjustments for Demo Blocks to fit in modal */
.demo-ui .block, .demo-ui .loop-block { margin-bottom: 5px; }
.demo-ui input { color: #aaa; border-color: #444; } /* Dimmed inputs */
.demo-ui .btn-danger { display: none; } /* Hide delete buttons in help */

/* Specific layout for the Color example */
.color-row { display: flex; gap: 10px; flex-wrap: wrap; }
.color-row .block { flex: 1; min-width: 120px; }

/* --- RESPONSIVE PLAYER --- */
@media (max-width: 800px) {
    #player {
        flex-direction: column;
    }
    .player-main {
        flex: 0 0 60%; /* Fixed 60% height */
        border-right: none;
        border-bottom: 1px solid rgba(255,255,255,0.1);
        width: 100%;
        padding: 10px;
    }
    .player-sidebar {
        flex: 1; /* Takes remaining space */
        width: 100%;
        min-width: 0; /* Override the desktop min-width */
        border-left: none;
        border-top: 1px solid rgba(255,255,255,0.1);
    }
    .timer-big {
        font-size: 14vh; /* Slightly smaller to ensure fit */
        margin: 10px 0;
    }
    .player-controls {
        margin-top: 20px;
    }
}
