:root {
    --bg: #1a1a2e;
    --surface: #16213e;
    --surface-2: #0f3460;
    --text: #e0e0e0;
    --text-muted: #8a8a9a;
    --accent: #e94560;
    --accent-hover: #ff6b81;
    --green: #2ecc71;
    --red: #e74c3c;
    --yellow: #f1c40f;
    --beat-inactive: #2a2a4a;
    --beat-active: #e94560;
    --beat-accent: #ff6b81;
    --border: #2a2a4a;
    --radius: 8px;
}

*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

/* Remove 300ms tap delay and prevent double-tap zoom on interactive controls */
button, input, select, label, a {
    touch-action: manipulation;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100dvh;
}

.app {
    max-width: 700px;
    margin: 0 auto;
    padding: 20px;
}

header {
    text-align: center;
    padding: 16px 0;
}

header h1 {
    font-size: 1.8rem;
    color: var(--accent);
    letter-spacing: 2px;
    text-transform: uppercase;
}

/* Sections */
.section {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 16px;
}

.section h2 {
    font-size: 1rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-bottom: 12px;
}

/* BPM Section */
.bpm-display {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    margin-bottom: 12px;
}

#bpm-input {
    width: 100px;
    font-size: 2.5rem;
    font-weight: 700;
    text-align: center;
    background: var(--surface-2);
    color: var(--text);
    border: 2px solid var(--border);
    border-radius: var(--radius);
    padding: 4px;
    outline: none;
}

#bpm-input:focus {
    border-color: var(--accent);
}

.bpm-label {
    font-size: 1.2rem;
    color: var(--text-muted);
}

#bpm-slider {
    width: 100%;
    height: 8px;
    appearance: none;
    background: var(--surface-2);
    border-radius: 4px;
    outline: none;
    margin-bottom: 16px;
}

#bpm-slider::-webkit-slider-thumb {
    appearance: none;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
}

#bpm-slider::-moz-range-thumb {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    border: none;
}

.tempo-presets {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    justify-content: center;
}

.tempo-presets button {
    padding: 6px 12px;
    font-size: 0.8rem;
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}

.tempo-presets button.active {
    background: var(--accent);
    border-color: var(--accent);
}

/* Time Signature */
.time-sig-row {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-top: 16px;
    justify-content: center;
}

.time-sig-row label {
    font-size: 0.9rem;
    color: var(--text-muted);
}

#time-sig {
    padding: 8px 12px;
    font-size: 1.1rem;
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    cursor: pointer;
    outline: none;
}

/* Beat Indicator */
.beat-indicator {
    display: flex;
    justify-content: center;
    gap: 12px;
    padding: 16px 0;
    flex-wrap: wrap;
}

.beat-circle {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--beat-inactive);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    font-weight: 700;
    color: var(--text-muted);
    transition: transform 0.06s ease-out, background-color 0.06s;
}

.beat-circle.active {
    background: var(--beat-active);
    color: #fff;
    transform: scale(1.3);
}

.beat-circle.accent.active {
    background: var(--beat-accent);
}

.measure-display {
    text-align: center;
    font-size: 0.9rem;
    color: var(--text-muted);
    margin-top: 8px;
}

/* Transport Controls */
.transport-section {
    display: flex;
    justify-content: center;
    gap: 12px;
    flex-wrap: wrap;
}

.transport-btn {
    padding: 12px 28px;
    font-size: 1rem;
    font-weight: 600;
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    transition: background 0.15s, transform 0.1s;
    min-height: 44px;
}

.transport-btn:active {
    transform: scale(0.96);
}

.play-btn {
    background: var(--green);
    color: #fff;
}

.stop-btn {
    background: var(--red);
    color: #fff;
}

.tap-btn {
    background: var(--surface-2);
    color: var(--text);
    border: 2px solid var(--accent);
}

.reset-btn {
    background: var(--surface-2);
    color: var(--text);
    border: 2px solid var(--text-muted);
}

/* Tempo Map */
.tempo-map-controls {
    display: flex;
    gap: 8px;
    margin-bottom: 12px;
    flex-wrap: wrap;
}

.tempo-map-controls button,
.file-upload-label {
    padding: 8px 16px;
    font-size: 0.85rem;
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    cursor: pointer;
    transition: background 0.15s;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
}

.tempo-map-table-wrapper {
    overflow-x: auto;
}

#tempo-map-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85rem;
}

#tempo-map-table th,
#tempo-map-table td {
    padding: 8px 10px;
    text-align: center;
    border-bottom: 1px solid var(--border);
}

#tempo-map-table th {
    color: var(--text-muted);
    font-weight: 600;
    text-transform: uppercase;
    font-size: 0.75rem;
    letter-spacing: 0.5px;
}

#tempo-map-table input,
#tempo-map-table select {
    width: 70px;
    padding: 6px;
    /* 16px avoids iOS Safari auto-zoom on focus */
    font-size: 16px;
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 4px;
    text-align: center;
}

#tempo-map-table .cue-select {
    width: 110px;
}

#tempo-map-table .remove-row-btn {
    background: none;
    border: none;
    color: var(--red);
    font-size: 1.4rem;
    cursor: pointer;
    padding: 4px 8px;
    min-width: 36px;
    min-height: 36px;
}

#tempo-map-table tr.active-measure {
    background: var(--surface-2);
}

/* Microphone Feedback */
.mic-controls {
    display: flex;
    align-items: center;
    gap: 16px;
    margin-bottom: 12px;
    flex-wrap: wrap;
}

#mic-btn {
    padding: 10px 20px;
    font-size: 0.9rem;
    background: var(--surface-2);
    color: var(--text);
    border: 2px solid var(--accent);
    border-radius: var(--radius);
    cursor: pointer;
    transition: background 0.15s;
    min-height: 44px;
}

#mic-btn.active {
    background: var(--accent);
    color: #fff;
}

.sensitivity-label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.85rem;
    color: var(--text-muted);
}

#sensitivity-slider {
    width: 120px;
    height: 6px;
    appearance: none;
    background: var(--surface-2);
    border-radius: 3px;
    outline: none;
}

#sensitivity-slider::-webkit-slider-thumb {
    appearance: none;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
}

#sensitivity-slider::-moz-range-thumb {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    border: none;
}

.subdivision-toggle-label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.85rem;
    color: var(--text-muted);
    margin: 8px 0 12px 0;
    cursor: pointer;
    user-select: none;
}

.subdivision-toggle-label input[type="checkbox"] {
    width: 18px;
    height: 18px;
    cursor: pointer;
    accent-color: var(--accent);
    flex-shrink: 0;
}

.mic-status {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-bottom: 8px;
}

.feedback-bar {
    height: 60px;
    border-radius: var(--radius);
    background: var(--surface-2);
    transition: background-color 0.1s;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;
    font-weight: 700;
    color: #fff;
    margin-bottom: 8px;
}

.feedback-bar.on-tempo {
    background: var(--green);
}

.feedback-bar.rushing {
    animation: flash-red 0.3s ease-in-out infinite;
}

.feedback-bar.dragging {
    animation: flash-yellow 0.3s ease-in-out infinite;
}

@keyframes flash-red {
    0%, 100% { background-color: var(--red); }
    50% { background-color: var(--surface-2); }
}

@keyframes flash-yellow {
    0%, 100% { background-color: var(--yellow); }
    50% { background-color: var(--surface-2); }
}

.timing-detail {
    font-size: 0.8rem;
    color: var(--text-muted);
    text-align: center;
}

/* Tempo Detector */
.detector-desc {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-bottom: 12px;
}

.detector-controls {
    margin-bottom: 16px;
}

#detector-btn {
    padding: 10px 20px;
    font-size: 0.9rem;
    background: var(--surface-2);
    color: var(--text);
    border: 2px solid var(--accent);
    border-radius: var(--radius);
    cursor: pointer;
    transition: background 0.15s;
    min-height: 44px;
}

#detector-btn.active {
    background: var(--accent);
    color: #fff;
}

.detector-display {
    display: flex;
    align-items: baseline;
    justify-content: center;
    gap: 8px;
    margin-bottom: 8px;
}

.detected-bpm {
    font-size: 3rem;
    font-weight: 700;
    color: var(--accent);
    transition: color 0.2s;
}

.detected-bpm.locked {
    color: var(--green);
}

.detected-bpm-label {
    font-size: 1.2rem;
    color: var(--text-muted);
}

.detector-confidence {
    font-size: 0.8rem;
    color: var(--text-muted);
    text-align: center;
    margin-bottom: 12px;
    min-height: 1.2em;
}

.use-detected-btn {
    display: block;
    margin: 0 auto;
    padding: 12px 24px;
    font-size: 0.9rem;
    font-weight: 600;
    background: var(--green);
    color: #fff;
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    transition: background 0.15s;
    min-height: 44px;
}

.use-detected-btn[hidden] {
    display: none;
}

/* ============================================================
   HOVER STATES — only on devices with a real pointer (mouse/trackpad).
   Scoped with @media (hover: hover) so tapped buttons don't keep a
   "stuck" hover color on touchscreens after the finger lifts.
   ============================================================ */
@media (hover: hover) and (pointer: fine) {
    .tempo-presets button:hover {
        background: var(--accent);
        border-color: var(--accent);
    }

    .play-btn:hover {
        background: #27ae60;
    }

    .stop-btn:hover {
        background: #c0392b;
    }

    .tap-btn:hover {
        background: var(--accent);
    }

    .reset-btn:hover {
        background: var(--text-muted);
    }

    .tempo-map-controls button:hover,
    .file-upload-label:hover {
        background: var(--accent);
        border-color: var(--accent);
    }

    #tempo-map-table .remove-row-btn:hover {
        color: var(--accent-hover);
    }

    #mic-btn:hover {
        background: var(--accent);
    }

    #detector-btn:hover {
        background: var(--accent);
    }

    .use-detected-btn:hover {
        background: #27ae60;
    }
}

/* ============================================================
   MOBILE (<= 600px)
   ============================================================ */
@media (max-width: 600px) {
    .app {
        padding: 12px;
    }

    header h1 {
        font-size: 1.4rem;
    }

    #bpm-input {
        font-size: 2rem;
        width: 90px;
    }

    /* Bigger slider thumbs for easier touch dragging */
    #bpm-slider {
        height: 12px;
    }
    #bpm-slider::-webkit-slider-thumb {
        width: 32px;
        height: 32px;
    }
    #bpm-slider::-moz-range-thumb {
        width: 32px;
        height: 32px;
    }
    #sensitivity-slider {
        width: 100%;
        height: 10px;
    }
    #sensitivity-slider::-webkit-slider-thumb {
        width: 26px;
        height: 26px;
    }
    #sensitivity-slider::-moz-range-thumb {
        width: 26px;
        height: 26px;
    }

    .sensitivity-label {
        flex: 1 1 100%;
    }

    .beat-circle {
        width: 40px;
        height: 40px;
        font-size: 0.85rem;
    }

    /* Transport: larger touch targets, especially Tap Tempo */
    .transport-btn {
        padding: 14px 22px;
        font-size: 1rem;
        min-height: 48px;
        flex: 1 1 45%;
    }

    .tap-btn {
        flex: 1 1 100%;
        padding: 18px;
        font-size: 1.1rem;
        min-height: 56px;
    }

    /* Tempo preset buttons: finger-friendly without being huge */
    .tempo-presets button {
        padding: 10px 14px;
        font-size: 0.85rem;
        min-height: 40px;
    }

    /* Tempo map controls: wrap nicely and hit the 44px target */
    .tempo-map-controls button,
    .file-upload-label {
        padding: 12px 16px;
        flex: 1 1 auto;
        justify-content: center;
    }

    #mic-btn,
    #detector-btn {
        padding: 12px 20px;
        min-height: 48px;
    }

    /* Tempo map table: give columns some breathing room */
    #tempo-map-table th,
    #tempo-map-table td {
        padding: 6px 4px;
    }
}
