:root {
    --wood-dark: #2d1b15;
    --wood-light: #5d4037;
    --sidebar-bg: #1a1a1a;
    --active-color: #ff9800;
    --safe-left: env(safe-area-inset-left);
    --safe-right: env(safe-area-inset-right);
    --safe-top: env(safe-area-inset-top);
    --safe-bottom: env(safe-area-inset-bottom);
}

* {
    box-sizing: border-box;
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;
    -webkit-tap-highlight-color: transparent;
}

body {
    margin: 0; padding: 0; background: #000;
    position: fixed; 
    top: 0; left: 0; right: 0; bottom: 0;
    height: 100dvh; width: 100vw; 
    overflow: hidden;
    overscroll-behavior: none;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    display: flex; flex-direction: column;
}

#rotate-message { display: none; }

/* --- MAIN LAYOUT --- */
#main-app {
    display: flex; 
    flex-direction: row; 
    width: 100%; 
    height: 100%;
    
    /* Default Landscape Padding */
    padding-left: var(--safe-left); 
    padding-right: var(--safe-right);
    
    position: absolute;
    top: 50%; left: 50%;
    transform: translate(-50%, -50%);
    
    transition: width 0.3s, height 0.3s, transform 0.3s, padding 0.3s;
    overflow: hidden; 
    background: #000;
}

/* --- PORTRAIT MODE TRANSFORM --- */
@media screen and (orientation: portrait) {
    #main-app {
        /* Swap dimensions */
        width: 100dvh; 
        height: 100vw;
        /* Rotate 90deg Clockwise */
        transform: translate(-50%, -50%) rotate(90deg);
        
        /* Add "Top" Padding globally to the app */
        /* Since rotated 90deg CW, Container Left = Visual Top */
        padding-left: calc(30px + var(--safe-top)); 
        padding-right: var(--safe-bottom);
    }
    
    /* In portrait, calculate chord bar width based on height (vh) */
    #chord-bar {
        width: clamp(140px, 25vh, 240px) !important;
    }

    /* FIX: Move Settings Trigger down to clear the notch (Visual Top) */
    /* Container Left = Visual Top, so we adjust 'left' */
    #settings-trigger {
        left: calc(20px + var(--safe-top)) !important; 
        top: 20px !important;
    }
}

/* --- GUITAR BODY --- */
#guitar-body {
    flex: 1;
    background: radial-gradient(circle at 40% 50%, var(--wood-light) 0%, var(--wood-dark) 60%, #000 95%);
    position: relative;
    display: flex; flex-direction: column;
    overflow: hidden;
}

#sound-hole {
    position: absolute; top: 50%; left: -20%;
    transform: translate(0, -50%);
    height: 90%; aspect-ratio: 1/1;
    background: #100805; border-radius: 50%;
    border: 6px solid #4e342e;
    box-shadow: inset 0 0 50px #000; z-index: 0;
}

/* Settings Button */
#settings-trigger {
    position: absolute; 
    /* Default Landscape Position (Left = Visual Left) */
    /* We add safe-left/safe-top here too for safety on all devices */
    top: calc(15px + var(--safe-top)); 
    left: calc(15px + var(--safe-left));
    
    width: 44px; height: 44px; 
    background: rgba(0,0,0,0.6); border: 1px solid #555;
    border-radius: 50%; color: white; font-size: 1.4rem;
    display: flex; justify-content: center; align-items: center;
    z-index: 100; cursor: pointer; backdrop-filter: blur(4px);
    box-shadow: 0 2px 5px rgba(0,0,0,0.5);
}

/* --- STRINGS --- */
#strings-container {
    z-index: 10;
    display: flex; flex-direction: column;
    justify-content: space-between;
    padding: 4vh 0; 
    height: 100%; width: 100%;
}

.string { position: relative; width: 100%; height: 40px; display: flex; align-items: center; }
.string-line { width: 100%; box-shadow: 0 3px 6px rgba(0,0,0,0.8); transition: transform 0.05s; }
.s0 .string-line { height: 4.5px; background: #ffb74d; }
.s1 .string-line { height: 3.8px; background: #ffb74d; }
.s2 .string-line { height: 3.2px; background: #ffb74d; }
.s3 .string-line { height: 2.5px; background: #ffb74d; }
.s4 .string-line { height: 1.5px; background: #cfd8dc; }
.s5 .string-line { height: 1.0px; background: #cfd8dc; }
.vibrating .string-line { animation: vibrate 0.15s linear infinite; }
@keyframes vibrate { 0% { transform: translateY(0); } 50% { transform: translateY(2px); } 100% { transform: translateY(0); } }

/* --- CHORD BAR (FLEXBOX) --- */
#chord-bar {
    width: clamp(110px, 28vw, 220px);
    min-width: 110px;
    height: 100%;
    
    background: var(--sidebar-bg);
    border-left: 2px solid #333;
    
    /* Flexbox with Wrap for responsive layout */
    display: flex;
    flex-wrap: wrap; 
    align-content: flex-start; 
    justify-content: center; 
    
    gap: 8px; 
    padding: 10px 8px;
    padding-right: calc(8px + var(--safe-right)); 
    
    overflow-y: auto; 
    overflow-x: hidden;
    overscroll-behavior: contain; 
    z-index: 20;
    box-shadow: -5px 0 15px rgba(0,0,0,0.6);
}

.chord-btn {
    /* Flex: grow to fill, base 50px */
    flex: 1 0 50px;
    aspect-ratio: 1/1;
    
    /* Cap width so single items don't look huge */
    max-width: 80px;
    
    border-radius: 50%; border: 2px solid #444;
    background: linear-gradient(145deg, #2a2a2a, #1a1a1a);
    color: #ddd; 
    font-size: clamp(0.8rem, 2.5vh, 1.1rem); 
    font-weight: bold;
    display: flex; justify-content: center; align-items: center;
    box-shadow: 0 4px 6px rgba(0,0,0,0.4);
    word-break: break-all;
    transition: transform 0.1s;
}
.chord-btn.active { background: var(--active-color); color: #2d1b15; border-color: var(--active-color); transform: scale(1.05); z-index: 2; }
.chord-btn.add-btn { background: transparent; border: 1px dashed #555; color: #888; font-size: 1.5rem; }

/* --- SETTINGS MODAL --- */
#settings-modal {
    display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0,0,0,0.95); z-index: 2000;
    flex-direction: column; align-items: center;
    touch-action: pan-y; -webkit-overflow-scrolling: touch;
    padding: 40px var(--safe-right) 40px var(--safe-left);
    overflow-y: auto;
    overscroll-behavior: contain; 
}
#settings-modal * { touch-action: pan-y; }

.modal-content { width: 100%; max-width: 500px; padding-bottom: 80px; }
.modal-header { display: flex; justify-content: space-between; border-bottom: 1px solid #333; padding-bottom: 10px; margin-bottom: 15px; }
.modal-title { color: #fff; font-size: 1.2rem; font-weight: bold; }
.close-icon { font-size: 1.5rem; color: #888; cursor: pointer; padding: 0 10px; }
.setting-label { color: var(--active-color); font-size: 0.8rem; text-transform: uppercase; margin-bottom: 8px; margin-top: 15px; }
.sound-options { display: flex; gap: 8px; }
.sound-btn { flex: 1; padding: 10px; background: #222; border: 1px solid #444; color: #aaa; border-radius: 6px; font-size: 0.8rem; }
.sound-btn.selected { background: var(--active-color); color: #000; font-weight: bold; }
.chord-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(42px, 1fr)); gap: 6px; }
.lib-btn { aspect-ratio: 1/1; background: #222; border: 1px solid #444; color: #fff; border-radius: 5px; font-size: 0.8rem; }
.lib-btn.added { border-color: var(--active-color); color: var(--active-color); background: #331f00; }
.lib-btn.sharp { color: #ffab91; border-color: #5d4037; }
.lib-btn.minor { font-size: 0.85rem; }

.action-btn { width: 100%; padding: 12px; background: #333; color: var(--active-color); border: 1px solid #555; border-radius: 8px; font-size: 0.9rem; font-weight: bold; margin-bottom: 10px; cursor: pointer; }
#preset-list { display: flex; flex-direction: column; gap: 8px; max-height: 150px; overflow-y: auto; background: #111; padding: 10px; border-radius: 8px; border: 1px solid #333; }
.preset-item { display: flex; justify-content: space-between; align-items: center; background: #222; border: 1px solid #444; border-radius: 6px; padding: 8px 12px; }
.preset-name { color: #ccc; font-size: 0.85rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; margin-right: 10px; cursor: pointer; }
.preset-del { background: transparent; border: none; color: #666; font-size: 1.2rem; line-height: 1; padding: 0 5px; }
.capo-controls { display: flex; justify-content: center; gap: 20px; background: #222; padding: 10px; border-radius: 8px; border: 1px solid #444; }
.capo-btn { width: 40px; height: 40px; background: #333; border: 1px solid #555; color: white; border-radius: 50%; font-size: 1.5rem; }
#capo-display { color: var(--active-color); font-size: 1.5rem; font-weight: bold; min-width: 30px; text-align: center; }

#start-overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0,0,0,0.92); color: white;
    display: flex; flex-direction: column; justify-content: center; align-items: center;
    z-index: 1000; backdrop-filter: blur(8px);
}
.app-title { margin-bottom: 5px; color: var(--active-color); }
.subtitle { color:#aaa; font-size: 0.9rem; margin-bottom: 25px; }
.overlay-options { width: 250px; margin-bottom: 25px; }
#start-btn { padding: 12px 50px; background: var(--active-color); border: none; border-radius: 30px; font-weight: bold; color: #000; }