*,
*::before,
*::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg: #1a1a2e;
    --surface: #16213e;
    --accent: #0f3460;
    --highlight: #e94560;
    --highlight-dim: #a83248;
    --mute: #6f7d96;
    --text: #eee;
    --text-dim: #888;
    --beat-overlay: rgba(233, 69, 96, 0.30);
    --beat-overlay-accent: rgba(233, 69, 96, 0.45);
}

/* ─── Theme Presets ─── */

[data-theme="classical"] {
    --bg: #2E2E2E;
    --surface: #5A3E36;
    --accent: #C8A96A;
    --highlight: #C8A96A;
    --highlight-dim: #8B6F47;
    --mute: #6B5B47;
    --text: #F5F1E8;
    --text-dim: #B8A892;
    --beat-overlay: rgba(200, 169, 106, 0.25);
    --beat-overlay-accent: rgba(200, 169, 106, 0.40);
}

[data-theme="rock"] {
    --bg: #0A0A0A;
    --surface: #1A1A1A;
    --accent: #B11226;
    --highlight: #B11226;
    --highlight-dim: #8B0000;
    --mute: #6E6E6E;
    --text: #FFFFFF;
    --text-dim: #B0B0B0;
    --beat-overlay: rgba(177, 18, 38, 0.25);
    --beat-overlay-accent: rgba(177, 18, 38, 0.40);
}

[data-theme="reggae"] {
    --bg: #1F7A3A;
    --surface: #2D5C40;
    --accent: #F2C94C;
    --highlight: #C0392B;
    --highlight-dim: #8B2E1F;
    --mute: #5D4037;
    --text: #FFFACD;
    --text-dim: #E6D480;
    --beat-overlay: rgba(242, 201, 76, 0.25);
    --beat-overlay-accent: rgba(192, 57, 43, 0.40);
}

[data-theme="electronic"] {
    --bg: #050505;
    --surface: #1A0033;
    --accent: #00E5FF;
    --highlight: #FF2D95;
    --highlight-dim: #8A2BE2;
    --mute: #4A148C;
    --text: #FFFFFF;
    --text-dim: #00E5FF;
    --beat-overlay: rgba(255, 45, 149, 0.25);
    --beat-overlay-accent: rgba(255, 45, 149, 0.40);
}

[data-theme="jazz"] {
    --bg: #1C2A44;
    --surface: #2D3E5F;
    --accent: #D4AF37;
    --highlight: #D4AF37;
    --highlight-dim: #6D1A36;
    --mute: #6D1A36;
    --text: #F8F4E3;
    --text-dim: #B8AFA3;
    --beat-overlay: rgba(212, 175, 55, 0.25);
    --beat-overlay-accent: rgba(212, 175, 55, 0.40);
}

[data-theme="country"] {
    --bg: #5D4B45;
    --surface: #7A6B61;
    --accent: #87AFC7;
    --highlight: #87AFC7;
    --highlight-dim: #6B8E23;
    --mute: #6B8E23;
    --text: #F5E6CC;
    --text-dim: #D4C4A8;
    --beat-overlay: rgba(135, 175, 199, 0.25);
    --beat-overlay-accent: rgba(135, 175, 199, 0.40);
}

[data-theme="hiphop"] {
    --bg: #121212;
    --surface: #1F1F1F;
    --accent: #FFD700;
    --highlight: #FFD700;
    --highlight-dim: #D4A017;
    --mute: #8B0000;
    --text: #F5E6D3;
    --text-dim: #D4C4A8;
    --beat-overlay: rgba(255, 215, 0, 0.25);
    --beat-overlay-accent: rgba(255, 215, 0, 0.40);
}

[data-theme="lofi"] {
    --bg: #6C8EA4;
    --surface: #8BA8C4;
    --accent: #B8A9C9;
    --highlight: #F2C6C2;
    --highlight-dim: #B8A9C9;
    --mute: #A8A8A8;
    --text: #1A1A1A;
    --text-dim: #6B6B6B;
    --beat-overlay: rgba(242, 198, 194, 0.25);
    --beat-overlay-accent: rgba(242, 198, 194, 0.40);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    padding: 2rem 1rem;
    padding-top: calc(2rem + 8px); /* offset for fixed progress bar */
    transition: background 0.3s, color 0.3s;
}

/* ─── Login Modal ─── */

.login-modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
}

.login-container {
    background: var(--surface);
    border-radius: 12px;
    padding: 2rem;
    max-width: 300px;
    width: 100%;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

.login-container h2 {
    color: var(--highlight);
    margin-bottom: 1.5rem;
    text-align: center;
    font-size: 1.5rem;
}

#login-form {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

#login-form input {
    padding: 0.75rem;
    border: 1px solid var(--accent);
    border-radius: 6px;
    background: rgba(255, 255, 255, 0.05);
    color: var(--text);
    font-size: 1rem;
    transition: border-color 0.15s;
}

#login-form input:focus {
    outline: none;
    border-color: var(--highlight);
    background: rgba(255, 255, 255, 0.1);
}

.login-btn {
    padding: 0.75rem;
    background: var(--highlight);
    color: #fff;
    border: none;
    border-radius: 6px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s;
}

.login-btn:hover {
    background: var(--highlight-dim);
}

.login-btn:active {
    transform: scale(0.98);
}

.login-error {
    color: #ff6b6b;
    font-size: 0.85rem;
    text-align: center;
    min-height: 1.2em;
}

.app {
    max-width: 1100px;
    width: 100%;
}

/* ─── Header ─── */

/* Duration progress bar pinned to very top */
.duration-progress-bar {
    height: 8px;
    background: rgba(0, 0, 0, 0.2);
    width: 100%;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    overflow: hidden;
    z-index: 9999;
}

.duration-progress-fill {
    height: 100%;
    width: 0%;
    background: linear-gradient(90deg, var(--highlight), var(--accent));
    transition: background-color 0.3s;
}

.duration-progress-fill.critical {
    background: linear-gradient(90deg, #ff0000, #cc0000);
}

header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 2rem;
    position: relative;
    gap: 1rem;
}

.header-title {
    text-align: left;
    flex: 1;
}

header h1 {
    font-size: 2.5rem;
    letter-spacing: 0.1em;
    color: var(--highlight);
    margin: 0;
}

.subtitle {
    margin: 0.25rem 0 0 0;
    font-size: 0.95rem;
    color: var(--text-dim);
}

.header-right {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 0.5rem;
}

.header-timer-row {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    font-size: 0.9rem;
    color: var(--text);
}

.header-clock {
    font-family: monospace;
    font-size: 1.1rem;
    font-weight: bold;
    color: var(--text);
    min-width: 48px;
}

.header-duration-label {
    color: var(--text-dim);
    font-size: 0.85rem;
}

.duration-input {
    width: 46px;
    background: var(--accent);
    color: var(--text);
    border: none;
    border-radius: 6px;
    padding: 0.25rem 0.35rem;
    font-size: 0.9rem;
    text-align: center;
    font-weight: 600;
    appearance: textfield;
    -moz-appearance: textfield;
}

.duration-input::-webkit-outer-spin-button,
.duration-input::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.lesson-timer-btn {
    background: #2d8a4e;
    color: #fff;
    border: none;
    border-radius: 6px;
    padding: 0.25rem 0.6rem;
    font-size: 0.95rem;
    cursor: pointer;
    transition: background 0.15s;
    min-width: 32px;
}

.lesson-timer-btn:hover {
    background: #35a35e;
}

.lesson-timer-btn.running {
    background: #8b2020;
}

.lesson-timer-btn.running:hover {
    background: #a52525;
}

.header-controls-row {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}

.header-clock-row {
    text-align: right;
    margin-top: 0.1rem;
}

.header-controls {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}

.theme-select,
.header-btn {
    background: var(--accent);
    color: var(--text);
    border: none;
    border-radius: 6px;
    padding: 0.5rem 0.75rem;
    font-size: 0.9rem;
    cursor: pointer;
    transition: background 0.15s;
}

.theme-select:hover,
.header-btn:hover {
    background: var(--highlight);
}

.theme-select:active,
.header-btn:active {
    transform: scale(0.95);
}

.theme-select {
    font-weight: 600;
}

.subtitle {
    color: var(--text-dim);
    font-size: 0.9rem;
    margin-top: 0.25rem;
}

/* ─── Controls Panel ─── */

.controls {
    background: var(--surface);
    border-radius: 12px;
    padding: 1.5rem;
    margin-bottom: 2rem;
}

/* BPM */

.bpm-section {
    margin-bottom: 1.25rem;
}

.bpm-controls {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.bpm-btn {
    background: var(--accent);
    color: var(--text);
    border: none;
    border-radius: 6px;
    padding: 0.5rem 0.75rem;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    min-width: 40px;
    transition: background 0.15s;
    user-select: none;
}

.bpm-btn:hover {
    background: var(--highlight);
}

.bpm-btn:active {
    transform: scale(0.95);
}

#bpm-slider {
    flex: 1;
    height: 6px;
    -webkit-appearance: none;
    appearance: none;
    background: var(--accent);
    border-radius: 3px;
    outline: none;
    cursor: pointer;
}

#bpm-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: var(--highlight);
    cursor: pointer;
}

#bpm-slider::-moz-range-thumb {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: var(--highlight);
    cursor: pointer;
    border: none;
}

.bpm-display {
    text-align: center;
    margin-top: 0.75rem;
    font-size: 1.4rem;
    font-weight: bold;
}

#bpm-value {
    color: var(--highlight);
    font-size: 2rem;
    font-variant-numeric: tabular-nums;
}

/* Transport */

.transport {
    display: flex;
    gap: 0.75rem;
    margin-bottom: 1rem;
}

.transport-btn {
    flex: 1;
    padding: 0.75rem;
    border: none;
    border-radius: 8px;
    font-size: 1.1rem;
    cursor: pointer;
    transition: all 0.15s;
    font-weight: 600;
}

.start-btn {
    background: #2d8a4e;
    color: #fff;
}

.start-btn:hover:not(:disabled) {
    background: #35a35e;
}

.start-btn:disabled {
    background: #1a5c32;
    cursor: not-allowed;
    opacity: 0.6;
}

.stop-btn {
    background: #c0392b;
    color: #fff;
}

.stop-btn:hover:not(:disabled) {
    background: #e74c3c;
}

.stop-btn:disabled {
    background: #7a2520;
    cursor: not-allowed;
    opacity: 0.6;
}

/* Beat Indicator */

.beat-indicator {
    display: flex;
    justify-content: center;
    gap: 1rem;
    padding: 0.5rem 0;
}

.beat-dot {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--accent);
    transition: background 0.05s, transform 0.05s;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--text);
    cursor: pointer;
    font-variant-numeric: tabular-nums;
    user-select: none;
    border: 2px solid transparent;
}

.beat-dot.active {
    background: var(--highlight);
    transform: scale(1.15);
}

.beat-dot.accent {
    background: #f39c12;
    color: #141414;
    border-color: #f39c12;
}

.beat-dot.accent.active {
    background: #f39c12;
    transform: scale(1.2);
}

.beat-dot.muted {
    background: var(--mute);
    color: #fff;
    border-color: var(--mute);
}

.beat-dot.muted.active {
    background: var(--mute);
    transform: scale(1.15);
}

.beat-dot:hover {
    border-color: var(--highlight);
}

/* Metronome Settings */

.metro-settings {
    display: flex;
    justify-content: center;
    gap: 1.5rem;
    padding: 0.75rem 0 0;
    flex-wrap: wrap;
}

.setting-group {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    font-size: 0.85rem;
    color: var(--text-dim);
}

.setting-group label {
    white-space: nowrap;
    margin-right: 0.15rem;
}

.setting-group span {
    min-width: 1.5em;
    text-align: center;
    font-weight: 600;
    color: var(--text);
    font-variant-numeric: tabular-nums;
}

.setting-stack {
    align-items: flex-start;
    flex-direction: column;
    gap: 0.45rem;
}

.beat-toggle-row {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem;
}

.beat-toggle {
    width: 32px;
    height: 32px;
    border: 1px solid rgba(255,255,255,0.12);
    border-radius: 50%;
    background: var(--accent);
    color: var(--text);
    cursor: pointer;
    font-size: 0.85rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    line-height: 1;
    transition: background 0.15s, border-color 0.15s, transform 0.15s;
}

.beat-toggle:hover {
    border-color: rgba(255,255,255,0.35);
}

.beat-toggle:active {
    transform: scale(0.94);
}

.beat-toggle.accent-on {
    background: #f39c12;
    color: #141414;
}

.beat-toggle.mute-on {
    background: var(--mute);
    color: #fff;
}

.toggle-label {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    cursor: pointer;
}

.toggle-label input[type="checkbox"] {
    accent-color: var(--highlight);
    width: 16px;
    height: 16px;
    cursor: pointer;
}

/* ─── Exercise / PDF Section ─── */

.exercise-section {
    background: var(--surface);
    border-radius: 12px;
    padding: 1.5rem;
    margin-bottom: 80px;
}

.mobile-notice {
    background: rgba(212, 175, 55, 0.1);
    border-left: 4px solid var(--highlight);
    padding: 1rem;
    border-radius: 6px;
    margin-bottom: 1rem;
    font-size: 0.9rem;
    color: var(--text);
}

.mobile-notice p {
    margin: 0;
}

.pdf-toolbar {
    display: flex;
    align-items: center;
    gap: 1rem;
    margin-bottom: 0.75rem;
    flex-wrap: wrap;
}

.pdf-file-label {
    background: var(--accent);
    color: var(--text);
    padding: 0.45rem 0.9rem;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 600;
    transition: background 0.15s;
}

.pdf-file-label:hover {
    background: var(--highlight);
}

.pdf-status {
    color: var(--text-dim);
    font-size: 0.85rem;
    flex: 1;
}

.page-nav {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

#page-info {
    font-size: 0.9rem;
    font-variant-numeric: tabular-nums;
    min-width: 4em;
    text-align: center;
}

.hint {
    color: var(--text-dim);
    font-size: 0.8rem;
    margin-bottom: 0.5rem;
}

/* Block settings row */

.block-settings {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
    font-size: 0.9rem;
    color: var(--text-dim);
}

.block-settings label {
    white-space: nowrap;
}


.block-info {
    color: var(--highlight);
    font-weight: 600;
    font-size: 0.9rem;
}

/* File tree */

.exercise-layout {
    display: flex;
    gap: 1rem;
    align-items: flex-start;
}

.file-tree {
    background: var(--accent);
    border-radius: 8px;
    padding: 0.5rem;
    width: 200px;
    min-width: 200px;
    max-height: 70vh;
    overflow-y: auto;
    flex-shrink: 0;
}

.file-tree[style*="display: none"] {
    width: 0;
    min-width: 0;
    padding: 0;
    margin: 0;
}

.viewer-panel {
    flex: 1;
    min-width: 0;
}

.file-tree-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.35rem 0.5rem;
    border-radius: 5px;
    cursor: pointer;
    font-size: 0.85rem;
    color: var(--text);
    transition: background 0.12s;
}

.file-tree-item:hover {
    background: rgba(255,255,255,0.08);
}

.file-tree-item.active {
    background: var(--highlight);
}

.file-tree-item .ft-icon {
    font-size: 1rem;
    flex-shrink: 0;
}

.file-tree-item .ft-name {
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.file-tree-item .ft-size {
    color: var(--text-dim);
    font-size: 0.75rem;
    flex-shrink: 0;
}

.file-tree-item .ft-del {
    background: none;
    border: none;
    color: var(--text-dim);
    cursor: pointer;
    font-size: 0.85rem;
    padding: 0 0.25rem;
    border-radius: 3px;
    flex-shrink: 0;
}

.file-tree-item .ft-del:hover {
    color: var(--highlight);
    background: rgba(233,69,96,0.15);
}

/* Image container */

.img-container {
    position: relative;
    background: #fff;
    border-radius: 8px;
    overflow: hidden;
    min-height: 120px;
    cursor: crosshair;
    display: inline-block;
    isolation: isolate;
}

.img-container img {
    display: block;
    max-width: 100%;
    height: auto;
}

/* PDF canvas container */

.pdf-container {
    position: relative;
    background: #fff;
    border-radius: 8px;
    overflow: hidden;
    min-height: 120px;
    cursor: crosshair;
    display: inline-block;
    isolation: isolate;
}

#pdf-canvas {
    display: block;
}

/* Selection rectangles */

.selections-layer {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    pointer-events: none;
    z-index: 1;
}

.selection-box {
    position: absolute;
    border: 2px dashed var(--highlight);
    background: rgba(233, 69, 96, 0.08);
    pointer-events: none;
    z-index: 1;
}

.selection-box .sel-badge {
    position: absolute;
    top: -10px;
    left: -10px;
    background: var(--highlight);
    color: #fff;
    border-radius: 50%;
    width: 20px;
    height: 20px;
    font-size: 11px;
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Overlay grid — now positioned absolutely on the selection */

.overlay-container {
    position: absolute;
    display: flex;
    pointer-events: none;
}

.overlay-column {
    flex: 1;
    transition: background 0.04s;
}

.overlay-column.active {
    background: rgba(233, 69, 96, 0.55);
    mix-blend-mode: screen;
}

.overlay-column.accent {
    background: rgba(233, 69, 96, 0.75);
    mix-blend-mode: screen;
}

.overlay-column.muted {
    background: rgba(111, 125, 150, 0.45);
    mix-blend-mode: screen;
}

/* ─── Footer ─── */

footer {
    text-align: center;
    margin-top: 1.5rem;
    color: var(--text-dim);
    font-size: 0.85rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

footer kbd {
    background: var(--accent);
    padding: 0.15em 0.5em;
    border-radius: 4px;
    font-size: 0.85em;
}

.footer-bottom {
    padding-top: 0.5rem;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.paypal-link {
    display: inline-block;
}

.paypal-link a {
    color: var(--highlight);
    text-decoration: none;
    transition: opacity 0.15s;
}

.paypal-link a:hover {
    opacity: 0.8;
}

/* ─── Inline Zoom ─── */

.zoom-inline {
    margin-top: 0.75rem;
}

.zoom-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.5rem 0;
    color: var(--text-dim);
    font-size: 0.9rem;
}

.zoom-canvas-wrapper {
    position: relative;
    background: #fff;
    border-radius: 8px;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
    isolation: isolate;
}

#zoom-canvas {
    display: block;
    max-width: 100%;
}

.zoom-canvas-inner {
    position: relative;
    display: inline-block;
}

#zoom-overlay-container {
    position: absolute;
    top: 0;
    left: 0;
    display: flex;
    pointer-events: none;
}

/* ─── Responsive ─── */

/* Mobile devices */
@media (max-width: 768px) {
    body {
        padding: 0.5rem;
        padding-top: calc(0.5rem + 8px);
    }

    .app {
        display: flex;
        flex-direction: column;
        height: 100vh;
        overflow: hidden;
    }

    header {
        margin-bottom: 1rem;
        align-items: flex-start;
    }

    header h1 {
        font-size: 1.4rem;
        margin-bottom: 0;
    }

    .subtitle {
        display: none;
    }

    .header-right {
        gap: 0.35rem;
    }

    .header-timer-row {
        font-size: 0.8rem;
        gap: 0.3rem;
    }

    .header-clock {
        font-size: 1rem;
    }

    .duration-input {
        width: 38px;
        padding: 0.2rem 0.25rem;
        font-size: 0.85rem;
    }

    .header-controls-row {
        gap: 0.35rem;
    }

    .login-container {
        margin: 1rem;
        max-width: calc(100% - 2rem);
    }

    .bpm-btn {
        padding: 0.4rem 0.5rem;
        font-size: 0.8rem;
        min-width: 32px;
    }

    .transport-btn {
        padding: 0.8rem 1rem;
        font-size: 1.1rem;
        flex: 1;
        min-height: 48px;
    }

    .beat-dot {
        width: 36px;
        height: 36px;
        font-size: 0.7rem;
    }

    .metro-settings {
        gap: 1rem;
    }

    .beat-indicator {
        gap: 0.75rem;
    }

    .controls {
        position: relative;
        bottom: auto;
        left: auto;
        right: auto;
        flex: 0 0 auto;
        max-width: none;
        border-radius: 0;
        margin: 0;
        z-index: auto;
        padding: 1rem 0.5rem;
        max-height: none;
        overflow-y: visible;
        box-shadow: none;
    }

    /* Hide exercise section entirely on mobile */
    .exercise-section {
        display: none !important;
    }

    .pdf-toolbar {
        flex-direction: column;
        gap: 0.5rem;
    }

    .pdf-file-label,
    .bpm-btn {
        touch-action: manipulation;
        -webkit-user-select: none;
        user-select: none;
    }

    .file-tree {
        max-width: 100%;
        max-height: 40vh;
    }

    .page-nav {
        flex-wrap: wrap;
    }

    /* Hide selection features on mobile */
    .selections-layer,
    .selection-box,
    #drawing-box,
    #img-drawing-box,
    .overlay-container,
    #overlay-container,
    #img-overlay-container,
    #zoom-overlay-container {
        display: none !important;
    }

    footer {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: var(--surface);
        padding: 0.5rem;
        text-align: center;
        border-top: 1px solid var(--mute);
        z-index: 100;
    }

    footer p {
        display: none;
    }

    .paypal-link {
        display: block !important;
    }
}
