:root {
    --bg: #ffffff;
    --fg: #1a1a1a;
    --muted: #6b7280;
    --border: #e5e7eb;
    --pad-bg: #f9fafb;
    --pad-hover: #f3f4f6;
    --accent: #3b82f6;
    --accent-dark: #2563eb;
    --success: #10b981;
    --warning: #f59e0b;
    --error: #ef4444;
    --radius: 8px;
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--fg);
    line-height: 1.6;
}

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

.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 24px;
}

h1 {
    font-size: 28px;
    font-weight: 700;
    margin-bottom: 8px;
}

.subtitle {
    color: var(--muted);
    margin-bottom: 24px;
}

.mpc-image {
    height: 80px;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.chord-matcher {
    background: #f0f9ff;
    border: 2px dashed var(--accent);
    border-radius: var(--radius);
    padding: 16px;
    margin-bottom: 24px;
}

.chord-matcher-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    cursor: pointer;
    user-select: none;
}

.chord-matcher-header h3 {
    font-size: 16px;
    font-weight: 600;
    color: var(--accent);
    margin: 0;
}

.optional-badge {
    background: white;
    color: var(--accent);
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.expand-icon {
    width: 20px;
    height: 20px;
    color: var(--accent);
    transition: transform 0.2s ease;
}

.chord-matcher.expanded .expand-icon {
    transform: rotate(180deg);
}

.chord-matcher-content {
    display: none;
    margin-top: 16px;
}

.chord-matcher.expanded .chord-matcher-content {
    display: block;
}

.chord-matcher-description {
    font-size: 14px;
    color: var(--muted);
    margin-bottom: 16px;
    line-height: 1.5;
}

.chord-input-row {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
}

.chord-input-row select {
    flex: 0 1 120px;
}

.chord-input-row button {
    flex: 0 0 auto;
}

.selected-chords {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 12px;
    min-height: 32px;
}

.chord-tag {
    background: white;
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 4px 12px;
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 14px;
    font-weight: 500;
}

.chord-tag button {
    background: none;
    border: none;
    color: var(--muted);
    cursor: pointer;
    padding: 0;
    width: 16px;
    height: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: all 0.2s ease;
}

.chord-tag button:hover {
    background: var(--error);
    color: white;
}

.key-mode-suggestions {
    margin-top: 16px;
    padding: 12px;
    background: white;
    border-radius: var(--radius);
    font-size: 14px;
}

.suggestion-label {
    font-weight: 600;
    color: var(--accent);
    margin-bottom: 8px;
}

.suggestion-list {
    color: var(--fg);
    line-height: 1.6;
}

.suggestion-item {
    padding: 2px 0;
}

.suggestion-item.compatible {
    color: var(--success);
}

.suggestion-item.incompatible {
    color: var(--muted);
    text-decoration: line-through;
}

.controls {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;
    margin-bottom: 16px;
}

.filter-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.filter-group label {
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    color: var(--muted);
    letter-spacing: 0.05em;
}

select, input[type="text"], button {
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 14px;
    background: white;
    transition: all 0.2s ease;
}

select:focus, input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

button {
    cursor: pointer;
    font-weight: 500;
}

button:hover {
    background: var(--pad-hover);
}

button.primary {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}

button.primary:hover {
    background: var(--accent-dark);
}

button:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.action-buttons {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 24px;
}

.action-buttons .full-width {
    width: 100%;
    margin-bottom: 16px;
}

.random-buttons-group {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
    padding: 16px;
    border: 2px dashed var(--border);
    border-radius: var(--radius);
    background: var(--pad-bg);
}

.random-buttons-group button {
    flex: 1 1 auto;
    min-width: 200px;
}

/* Context tabs container */
.context-tabs-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
    gap: 16px;
    flex-wrap: wrap;
}

.context-tabs {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.context-tab {
    padding: 10px 20px;
    background: white;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
}

.context-tab:hover {
    background: var(--pad-hover);
    border-color: var(--accent);
}

.context-tab.active {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}

.context-tab.active:hover {
    background: var(--accent-dark);
}

.left-handed-toggle {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: var(--bg-secondary);
    border-radius: var(--radius);
    font-size: 14px;
}

.left-handed-toggle label {
    display: flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
    margin: 0;
    user-select: none;
}

.left-handed-toggle input[type="checkbox"] {
    cursor: pointer;
    width: 16px;
    height: 16px;
}

.progressions {
    display: grid;
    gap: 20px;
    margin-bottom: 24px;
}

.progression-card {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px;
    background: white;
}

.progression-header {
    display: flex;
    justify-content: space-between;
    align-items: start;
    margin-bottom: 16px;
    gap: 12px;
}

.progression-info {
    flex: 1;
    min-width: 0;
}

.progression-title {
    font-size: 18px;
    font-weight: 600;
}

.progression-meta {
    display: flex;
    gap: 16px;
    margin-top: 4px;
    font-size: 14px;
    color: var(--muted);
}

.progression-meta .key {
    color: var(--success);
    font-weight: 500;
}

.progression-meta .pattern {
    color: var(--warning);
    font-weight: 500;
}

.download-btn {
    background: transparent;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    width: 32px;
    height: 32px;
    flex-shrink: 0;
}

.download-btn:hover {
    background: var(--pad-hover);
    border-color: var(--accent);
}

.download-btn svg {
    width: 18px;
    height: 18px;
    stroke: var(--muted);
}

.download-btn:hover svg {
    stroke: var(--accent);
}

.chord-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    max-width: 600px;
}

/* Wider grid for keyboard and guitar contexts */
body[data-context="keyboard"] .chord-grid,
body[data-context="guitar"] .chord-grid {
    max-width: 100%;
}

/* Hide download buttons in Keyboard and Guitar contexts */
body[data-context="keyboard"] .download-btn,
body[data-context="guitar"] .download-btn {
    display: none;
}

.chord-pad {
    background: var(--pad-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 5px;
    cursor: pointer;
    transition: all 0.2s ease;
    position: relative;
    overflow: hidden;
    min-height: 100px;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
}

.chord-pad:hover {
    background: var(--pad-hover);
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}

.chord-pad.playing {
    animation: pulse 0.3s ease;
    background: #dbeafe;
    border-color: var(--accent);
}

.chord-pad.progression-chord {
    background: #fef3c7;
    border-color: #f59e0b;
}

@keyframes pulse {
    0% { transform: scale(1); }
    50% { transform: scale(1.05); }
    100% { transform: scale(1); }
}

.chord-pad-content {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    width: 100%;
}

.chord-info {
    flex: 1;
}

.pad-number {
    font-size: 9px;
    color: var(--muted);
    font-weight: 600;
    text-transform: uppercase;
    opacity: 0.7;
    white-space: nowrap;
    margin-left: 4px;
}

.chord-name {
    font-size: 17px;
    font-weight: 700;
    margin-bottom: 1px;
    color: var(--fg);
}

.chord-quality {
    font-size: 12px;
    color: var(--accent);
    font-weight: 500;
    margin-bottom: 2px;
}

.chord-roman {
    font-size: 13px;
    font-weight: 600;
    color: var(--warning);
    margin-bottom: 1px;
}

.chord-notes {
    font-size: 10px;
    color: var(--muted);
    font-family: 'Monaco', monospace;
    word-break: break-all;
}

.chord-keyboard {
    margin-top: 1px;
    margin-bottom: 1px;
    width: 100%;
    height: 35px;
}

.chord-keyboard svg {
    width: 100%;
    height: 100%;
}

.chord-guitar {
    margin-top: 1px;
    margin-bottom: 1px;
    width: 100%;
    height: 60px;
    display: none;
}

.chord-guitar svg {
    width: 100%;
    height: 100%;
}

/* Show/hide notation based on context */
/* MPC and Keyboard contexts show keyboard diagrams */
body[data-context="mpc"] .chord-keyboard,
body[data-context="keyboard"] .chord-keyboard {
    display: block;
}

/* Only Guitar context shows guitar diagrams */
body[data-context="guitar"] .chord-guitar {
    display: block;
}

/* Hide guitar in MPC/Keyboard, hide keyboard in Guitar */
body[data-context="mpc"] .chord-guitar,
body[data-context="keyboard"] .chord-guitar,
body[data-context="guitar"] .chord-keyboard {
    display: none;
}

/* Hide pad numbers in Keyboard and Guitar contexts */
body[data-context="keyboard"] .pad-number,
body[data-context="guitar"] .pad-number {
    display: none;
}

/* Keyboard context: Move roman numeral to top right */
body[data-context="keyboard"] .chord-pad {
    position: relative;
}

body[data-context="keyboard"] .chord-roman {
    position: absolute;
    top: 12px;
    right: 12px;
    font-size: 11px;
    font-weight: 700;
    color: var(--muted);
    opacity: 0.8;
    margin: 0;
}

/* Two-column layout for Guitar cards */
body[data-context="guitar"] .chord-pad {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 8px;
    align-items: start;
    min-height: 150px;
}

body[data-context="guitar"] .chord-text-column {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

body[data-context="guitar"] .chord-guitar {
    width: 150px;
    height: 140px;
    margin: 0;
}

/* Wrap notes every 2 notes in Guitar context */
body[data-context="guitar"] .chord-notes {
    display: flex;
    flex-wrap: wrap;
    gap: 4px 8px;
    max-width: 150px;
}

body[data-context="guitar"] .note-pair {
    white-space: nowrap;
}

/* Responsive guitar card layout */
@media (max-width: 768px) {
    /* Scale down diagram on tablets */
    body[data-context="guitar"] .chord-guitar {
        width: 120px;
        height: 112px;
    }
}

@media (max-width: 600px) {
    /* Stack layout on narrow viewports */
    body[data-context="guitar"] .chord-pad {
        grid-template-columns: 1fr;
        gap: 12px;
    }

    body[data-context="guitar"] .chord-guitar {
        width: 100%;
        max-width: 180px;
        height: auto;
        aspect-ratio: 150 / 140;
        margin: 0 auto;
    }

    /* Notes in single line */
    body[data-context="guitar"] .chord-notes {
        flex-wrap: nowrap;
        overflow-x: auto;
        max-width: 100%;
    }

    /* Chord name and quality on same line */
    body[data-context="guitar"] .chord-text-column {
        gap: 8px;
    }

    body[data-context="guitar"] .chord-pad-content {
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: baseline;
        flex-wrap: wrap;
    }
}

@media (max-width: 400px) {
    /* Further scale down on very narrow screens */
    body[data-context="guitar"] .chord-guitar {
        max-width: 140px;
    }
}

.tooltip {
    position: fixed;
    background: rgba(0, 0, 0, 0.9);
    color: white;
    padding: 8px 12px;
    border-radius: 6px;
    font-size: 13px;
    line-height: 1.4;
    max-width: 250px;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.2s ease;
    z-index: 1000;
    white-space: normal;
    box-shadow: 0 4px 8px rgba(0,0,0,0.3);
}

.tooltip.visible {
    opacity: 1;
}

.tooltip::before {
    content: '';
    position: absolute;
    bottom: -5px;
    left: 50%;
    transform: translateX(-50%);
    width: 0;
    height: 0;
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
    border-top: 5px solid rgba(0, 0, 0, 0.9);
}

.chord-pad {
    position: relative;
}
.hidden {
    display: none;
}

footer {
    margin-top: 40px;
    padding: 20px 0;
    border-top: 1px solid var(--border);
    text-align: center;
    color: var(--muted);
    font-size: 14px;
}

footer p {
    margin: 0.5rem 0;
}

footer a {
    color: var(--accent);
    text-decoration: none;
    font-weight: 500;
}

footer a:hover {
    text-decoration: underline;
}

@media (max-width: 768px) {
    .controls {
        grid-template-columns: 1fr;
    }

    .header {
        flex-direction: column;
        gap: 12px;
    }

    .random-buttons-group {
        flex-direction: column;
    }

    .random-buttons-group button {
        min-width: 100%;
    }

    .context-tabs-container {
        flex-direction: column;
        align-items: flex-start;
    }

    .chord-grid {
        max-width: 100%;
    }

    .chord-pad {
        min-height: 80px;
        padding: 8px;
    }
}

/* Print styles */
@media print {
    /* Hide all controls and headers */
    .header,
    .chord-matcher,
    .controls,
    .action-buttons,
    .context-tabs-container,
    footer,
    .download-btn {
        display: none !important;
    }

    /* Show progressions container */
    .progressions {
        display: block !important;
    }

    /* Each progression on its own page */
    .progression-card {
        page-break-after: always;
        page-break-inside: avoid;
        border: none;
        padding: 0;
        margin: 0;
        padding-top: 0 !important;
        margin-top: 0 !important;
    }

    /* Remove margins from first progression */
    .progression-card:first-child {
        margin-top: 0 !important;
        padding-top: 0 !important;
    }

    .progression-header {
        margin-bottom: 20px;
        padding-bottom: 10px;
        border-bottom: 2px solid #000;
    }

    .progression-title {
        font-size: 20px;
        font-weight: 700;
    }

    .chord-grid {
        max-width: 100%;
    }

    .chord-pad {
        border: 1px solid #000;
        background: white;
        page-break-inside: avoid;
    }

    /* For Guitar and Keyboard contexts - show appropriate notation */
    /* MPC and Keyboard show keyboard diagrams when printing */
    body[data-context="mpc"] .chord-keyboard,
    body[data-context="keyboard"] .chord-keyboard {
        display: block !important;
    }

    /* Guitar shows guitar diagrams when printing */
    body[data-context="guitar"] .chord-guitar {
        display: block !important;
    }

    /* Hide guitar in MPC/Keyboard print, hide keyboard in Guitar print */
    body[data-context="mpc"] .chord-guitar,
    body[data-context="keyboard"] .chord-guitar,
    body[data-context="guitar"] .chord-keyboard {
        display: none !important;
    }

    /* Default page setup */
    @page {
        margin: 1cm;
    }

    body {
        margin: 0;
        padding: 0;
    }

    .app {
        padding: 0;
        margin: 0;
    }

    .progressions {
        margin: 0;
        padding: 0;
        gap: 0;
    }
}

/* Page orientation for print - must be defined outside @media */
@page {
    size: portrait;
    margin: 1cm;
}

/* Override for keyboard context */
@media print {
    /* Guitar stays portrait (default), keyboard needs landscape */
    body[data-context="keyboard"] {
        /* Handled via JavaScript size injection if needed */
    }
}

/* Sparkle animation for auto-generation feature */
@keyframes rainbowScan {
    0% {
        left: -100%;
    }
    100% {
        left: 100%;
    }
}

button.primary.sparkle {
    position: relative;
    overflow: hidden;
}

button.primary.sparkle::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 60%;
    height: 100%;
    background: linear-gradient(
        90deg,
        transparent,
        rgba(255, 0, 128, 0.7),
        rgba(255, 0, 255, 0.8),
        rgba(0, 128, 255, 0.8),
        rgba(0, 255, 255, 0.8),
        rgba(0, 255, 0, 0.8),
        rgba(255, 255, 0, 0.7),
        transparent
    );
    animation: rainbowScan 0.6s ease-out;
    pointer-events: none;
}
