/* =========================================
   1. FONTS & VARIABLES
   ========================================= */

/* Import UDEV Gothic locally */
@font-face {
    font-family: 'UDEV Gothic';
    /* Ensure the file is exactly at: /public/fonts/UDEVGothic-Regular.ttf */
    src: url('/fonts/UDEVGothic-Regular.ttf') format('truetype');
    font-weight: normal;
    font-style: normal;
    font-display: swap;
}

:root {
    --bg-color: #f4f4f9;
    --sidebar-bg: #1e1e24;
    --text-color: #333;
    --chord-color: #8f071d;
    --chord-underline-color: #660314cb;
    --accent-color: #d31f34;
    /* Fallback to Biz if UDEV fails */
    --font-stack: 'UDEV Gothic', 'Biz UDGothic', monospace;
}

* { box-sizing: border-box; }

/* =========================================
   2. MAIN LAYOUT (DESKTOP DEFAULT)
   ========================================= */

body {
    margin: 0;
    padding: 0;
    font-family: 'Inter', sans-serif; /* UI Font */
    background: var(--bg-color);
    color: var(--text-color);

    /* Desktop Default: Sidebar on Left, Content on Right */
    display: flex;
    flex-direction: row;
    height: 100vh;
    overflow: hidden; /* Prevent body scroll, let children scroll */
}

/* Sidebar */
.sidebar {
    width: 300px;
    background: var(--sidebar-bg);
    color: #fff;
    display: flex;
    flex-direction: column;
    padding: 20px;
    overflow-y: auto;
    flex-shrink: 0; /* Keep width fixed on desktop */
    height: 100%;
}

.sidebar h2 { margin-top: 0; font-size: 1.2rem; }

/* Main Content Area */
.main {
    flex-grow: 1;
    padding: 40px;
    overflow-y: auto; /* Scroll ONLY the song area */
    height: 100%;
}

/* =========================================
   3. SONG RENDERING & COLUMNS
   ========================================= */

.song-container {
    font-family: var(--font-stack);
    font-size: 16px;
    line-height: 1.1;
    overflow-x: hidden;
    white-space: normal;

    /* Default: 2 Columns */
    display: block;
    column-count: 2;
    column-gap: 60px;
    column-rule: 1px solid #ddd;

    orphans: 3;
    widows: 3;

    /* --- THE SPACING FIX --- */
    padding-top: 0px;      /* Do NOT use padding for separation */
    margin-top: 30px;    /* Use Margin to push the whole container down, both columns will go down*/
}

.line-block {
    display: flex;
    flex-wrap: wrap;
    margin-bottom: 10px;
    position: relative;
    min-height: 1em;
}

.segment {
    display: inline-flex;
    flex-direction: column;
}

/* The Chords Line */
.chord {
    white-space: pre-wrap; /* Allows wrapping while preserving spaces */
    word-break: break-word;
    color: var(--chord-color);
    font-weight: bold;
    min-height: 1.2em;
    margin-bottom: 0.1em;
    display: block;
}

/* The Lyrics Line */
.lyrics {
    white-space: pre-wrap; /* Allows wrapping while preserving spaces */
    word-break: break-word;
    display: block;
    color: #000;
}

.anchor {
    text-decoration: underline;
    text-decoration-color: var(--chord-underline-color);
    text-decoration-thickness: 2px;
    text-underline-offset: 3px;
}

/* =========================================
   UPDATED SECTION BOX STYLES (NO COLUMN CUT-OFF)
   ========================================= */

.song-section {
    width: 100%;
    margin-bottom: 30px;
    padding: 30px 15px 10px 15px; /* Increased top padding to make room inside */
    position: relative;
    display: block;

    break-inside: avoid;
    -webkit-column-break-inside: avoid;
    page-break-inside: avoid;

    /* 1. Remove the physical border from the parent container */
    border: none !important;
    background: transparent;
}

/* 2. Draw the border using a pseudo-element, pushed DOWN slightly */
.song-section::before {
    content: '';
    position: absolute;
    top: 12px; /* Pushes the top line of the border down into the box */
    left: 0;
    right: 0;
    bottom: 0;
    border: 1.5px solid #ccc;
    border-radius: 12px;
    z-index: -1; /* Keeps it strictly behind your text */
}

/* 3. The Label sits safely inside the box, no longer negative! */
.section-label-container {
    position: absolute;
    top: 0; /* Safe inside the bounding box. Goodbye cut-off bug! */
    left: 10px;
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--bg-color); /* This perfectly masks the border underneath */
    padding: 0 10px;
    z-index: 1;
}

.section-initials {
    width: 30px;  /* circle for the section name abbreviation*/
    height: 30px;
    border: 1.5px solid #333;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.75rem;
    font-weight: bold;
    font-family: 'Inter', sans-serif;
    background: var(--bg-color); /* Ensures the line doesn't show through the circle */
}

.section-label {
    background: transparent;
    color: #000;
    padding: 0;
    font-family: 'Inter', sans-serif;
    font-size: 0.85rem;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 1px;
}

/* Adjust the comment line so it lines up with the new internal spacing */
.comment-line {
    position: absolute;
    top: 22px;
    right: 15px;
    background: transparent;
    border: none;
    font-style: normal;
    color: #888;
    font-size: 0.8rem;
}

/*.empty-line { height: 1em; }*/


/* =========================================
   5. UI COMPONENTS (SEARCH, BUTTONS, MODAL)
   ========================================= */

/* Song Tabs */
.song-tabs {
    display: flex;
    gap: 10px;
    margin-bottom: 15px;
}

.tab-btn {
    background: #444;
    color: white;
    border: none;
    padding: 8px 15px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.9rem;
    font-family: 'Inter', sans-serif;
    font-weight: bold;
    transition: 0.2s;
}

.tab-btn:hover {
    background: #666;
}

.tab-btn.active {
    background: var(--accent-color);
}

.tab-content {
    display: none;
}

.tab-content.active {
    display: block;
}

/* Romaji-Specific Spacing Improvements */
#romajiView .line-block {
    margin-bottom: 0.5em; /* (1) Space between different lyric lines */
}

#romajiView .chord:empty {
    display: none;
}


#romajiView .song-section {
    padding-top: 45px; /* (3) More space below Verse/Chorus labels in Romaji */
}


/* Search Box */
.search-box {
    width: 100%;
    padding: 10px;
    margin-bottom: 20px;
    border: none;
    border-radius: 4px;
    background: #2b2b36;
    color: #fff;
}

/* Song List in Sidebar */
.song-list { list-style: none; padding: 0; }
.song-list li { margin-bottom: 5px; }
.song-list a {
    color: #aaa;
    text-decoration: none;
    display: block;
    padding: 8px;
    border-radius: 4px;
    transition: 0.2s;
}
.song-list a:hover, .song-list a.active {
    background: var(--accent-color);
    color: white;
}

/* Metadata Header */
.metadata {
    border-bottom: 2px solid #ddd; /* Keep border small */
    padding-bottom: 20px;          /* Keep padding normal */
    column-span: all;
    display: block; /* Ensure it takes up full width */
    width: 100%;
}
.metadata h1 { margin: 0 0 10px 0; }
.meta-info { display: flex; gap: 20px; color: #666; font-size: 0.9rem; align-items: center; }
.meta-tag strong { color: #000; }

.meta-extras {
    margin-top: 10px;
    font-size: 0.85rem;
    color: #888;
    display: flex;
    flex-wrap: wrap;
    gap: 15px;
}
.extra-tag small { text-transform: uppercase; color: #aaa; margin-right: 4px; }

/* Transpose Buttons */
.transpose-controls button {
    background: #444;
    color: white;
    border: none;
    padding: 5px 10px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 1rem;
}
.transpose-controls button:hover { background: var(--accent-color); }

/* Setlist UI */
.setlist-section {
    margin-top: 30px;
    border-top: 1px solid #444;
    padding-top: 20px;
}
.setlist-header {
    display: flex; justify-content: space-between; align-items: center;
    color: #888; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 1px;
    margin-bottom: 10px;
}
.add-set-btn { background: none; border: none; color: #aaa; cursor: pointer; font-size: 1.2rem; }
.add-set-btn:hover { color: #fff; }

.set-group { margin-bottom: 15px; }
.set-title { color: #fff; font-weight: bold; cursor: pointer; display: block; padding: 5px 0; }
.set-songs { list-style: none; padding-left: 15px; margin: 0; display: none; }
.set-songs.open { display: block; }
.set-songs li a { font-size: 0.9rem; color: #bbb; padding: 4px 0; display: block; text-decoration: none; }
.set-songs li a:hover { color: var(--accent-color); }

.add-to-set-btn {
    background: transparent; border: 1px solid #aaa; color: #555;
    padding: 5px 10px; cursor: pointer; border-radius: 4px; margin-left: 10px; font-size: 0.9rem;
}
.add-to-set-btn:hover { background: #eee; border-color: #888; }

/* --- Setlist Flexbox Rows for Delete Buttons --- */
.set-title-row, .set-song-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.set-song-row {
    padding: 4px 0; /* Replaces the padding that was on the <a> tag */
}

/* Make the link take up the remaining space */
.set-song-row a {
    flex-grow: 1;
    padding: 0;
}

/* The Delete Button [-] and [x] */
.delete-btn {
    background: transparent;
    border: none;
    color: #666;
    cursor: pointer;
    font-size: 1.1rem;
    padding: 0 5px;
    line-height: 1;
    display: none; /* Hide by default to keep it clean */
}

/* Only show delete buttons when hovering over the row */
.set-title-row:hover .delete-btn,
.set-song-row:hover .delete-btn {
    display: block;
}

.delete-btn:hover {
    color: var(--accent-color);
    font-weight: bold;
}



/* Modal */
.modal {
    display: none; position: fixed; z-index: 1000;
    left: 0; top: 0; width: 100%; height: 100%;
    background-color: rgba(0,0,0,0.6);
    justify-content: center; align-items: center;
}
.modal-content {
    background-color: #fff; padding: 25px; border-radius: 8px;
    width: 300px; box-shadow: 0 4px 15px rgba(0,0,0,0.3);
}
.modal input, .modal select {
    width: 100%; padding: 8px; margin: 10px 0;
    border: 1px solid #ddd; border-radius: 4px;
}
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 15px; }
.btn-cancel { background: #ccc; border: none; padding: 8px 15px; border-radius: 4px; cursor: pointer; }
.btn-save { background: var(--chord-color); color: white; border: none; padding: 8px 15px; border-radius: 4px; cursor: pointer; }

/* NNS Toggle Switch */
.nns-switch {
    position: relative;
    display: inline-block;
    width: 36px;
    height: 20px;
    margin-top: 3px;
}

.nns-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.nns-slider {
    position: absolute;
    cursor: pointer;
    top: 0; left: 0; right: 0; bottom: 0;
    background-color: #ccc;
    transition: .3s;
    border-radius: 20px;
}

.nns-slider:before {
    position: absolute;
    content: "";
    height: 14px;
    width: 14px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    transition: .3s;
    border-radius: 50%;
}

input:checked + .nns-slider {
    background-color: var(--chord-color);
}

input:focus + .nns-slider {
    box-shadow: 0 0 1px var(--chord-color);
}

input:checked + .nns-slider:before {
    transform: translateX(16px);
}

/* =========================================
   6. AUDIO BAR
   ========================================= */

#audioBar {
    position: sticky;
    bottom: 0;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 0;
    margin-top: 30px;
    border-top: 1px solid #ddd;
    background: var(--bg-color);
}

#audioBar audio {
    flex: 1;
    height: 32px;
    min-width: 0;
}

.audio-ver-btns {
    display: flex;
    gap: 5px;
    flex-shrink: 0;
}

.audio-ver-btn {
    background: #444;
    color: white;
    border: none;
    padding: 5px 12px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.8rem;
    font-family: 'Inter', sans-serif;
    font-weight: bold;
    transition: 0.2s;
}

.audio-ver-btn:hover { background: #666; }
.audio-ver-btn.active { background: var(--accent-color); }

/* =========================================
   7. RESPONSIVE LOGIC (THE IPAD FIX)
   ========================================= */

/* TABLET / IPAD / MOBILE (< 1100px) */
@media (max-width: 1100px) {

    body {
        flex-direction: column;
        height: 100vh;
        overflow: hidden;
    }

    /* --- THE NEW 2-COLUMN TOPBAR --- */
    .sidebar {
        width: 100%;
        height: 10vh; /* Slightly taller to comfortably fit the lists side-by-side */
        min-height: 200px;

        border-bottom: 5px solid var(--accent-color);
        padding: 10px 15px;

        /* MAGIC: Split the topbar into 2 columns using CSS Grid */
        display: grid;
        grid-template-columns: 1fr 1fr; /* Two equal columns */
        grid-template-rows: auto 1fr;   /* Row 1 hugs the search bar, Row 2 fills the rest */
        gap: 5px 15px; /* 5px vertical gap, 15px horizontal gap between columns */
    }

    /* Hide the "Songbook" title to save precious screen space */
    .sidebar h2 { display: none; }

    /* Left Column: Search Box (Top) */
    .search-box {
        grid-column: 1; /* Place in left column */
        grid-row: 1;    /* Place in top row */
        margin-bottom: 0;
    }

    /* Left Column: Song Library (Bottom) */
    .song-list {
        grid-column: 1; /* Place in left column */
        grid-row: 2;    /* Place in bottom row */

        margin: 0;
        overflow-y: auto;
        min-height: 0;
        border: 1px solid #444;
        background: rgba(0,0,0,0.2);
        padding: 5px;
    }

    /* Right Column: Setlist Section (Spans Top to Bottom) */
    .setlist-section {
        grid-column: 2;   /* Place in right column */
        grid-row: 1 / 3;  /* Stretch from row 1 to row 2 */

        margin: 0;
        padding: 0;
        border-top: none;

        /* Make the setlist column itself a flexbox so its inner list can scroll */
        display: flex;
        flex-direction: column;
        overflow: hidden;
    }

    /* Ensure the actual list of setlists can scroll if it gets too long */
    #setlistContainer {
        overflow-y: auto;
        flex-grow: 1;
        min-height: 0;
        padding-right: 5px; /* Keep scrollbar from touching edge */
    }

    /* MAIN CONTENT AREA */
    .main {
        width: 100%;
        padding: 20px;
        flex-grow: 1;
        height: auto;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
}


/* =========================================
   7. PRINT / PDF EXPORT STYLES
   ========================================= */

@media print {
    /* 1. HIDE EVERYTHING WE DON'T NEED */
    .sidebar,
    .transpose-controls,
    .add-to-set-btn,
    .search-box,
    .setlist-section,
    .song-tabs,
    #audioBar,
    #searchInput {
        display: none !important;
    }

    /* 2. RESET THE LAYOUT */
    body {
        display: block; /* Remove Flexbox */
        background: white; /* Save ink/clean look */
        color: black;
        height: auto;
        overflow: visible;
    }

    .main {
        width: 100%;
        margin: 0;
        padding: 0;
        overflow: visible;
        height: auto;
    }

    /* 3. FORMAT THE SONG FOR PAPER/PDF */
    .metadata {
        border-bottom: 2px solid #000;
        margin-bottom: 30px;
        padding-bottom: 10px;
    }

    /* Hide the inactive tab (romaji or chords) */
    .tab-content:not(.active) {
        display: none !important;
    }

    .song-container {
        display: block;
        column-count: 2; /* Keep 2 columns for PDF */
        column-gap: 40px;
        column-rule: 1px solid #ccc;
        font-size: 12pt; /* Standard reading size */
        margin-top: 20px;

        /* Ensure layout integrity */
        orphans: 4;
        widows: 4;
    }

    /* Ensure verses don't get cut in half at bottom of page */
    .song-section {
        break-inside: avoid;
        page-break-inside: avoid;
        display: block;
    }

    /* 4. CLEAN UP COLORS & MATCH NEW LAYOUT FOR PRINT */
    .chords {
        color: #000 !important; /* Make chords black for printing */
        font-weight: bold;
    }
    .anchor {
        text-decoration-color: #000 !important;
    }

    /* Update the Box Border for ink */
    .song-section::before {
        border-color: #999 !important; /* Nice dark gray for printing */
    }

    /* Remove the old Chorus/Bridge overrides so they stay as rounded boxes */
    .song-section.chorus,
    .song-section.bridge {
        border-left: none !important;
        padding-left: 15px !important; /* Keep padding uniform */
    }

    /* Make the masking backgrounds WHITE to match the paper */
    .section-label-container {
        background: #fff !important;
    }

    .section-initials {
        background: #fff !important;
        border-color: #000 !important; /* Crisp black circle */
        color: #000 !important;
    }

    .section-label {
        background: transparent !important;
        border: none !important; /* Strip out the old print border */
        color: #000 !important;
    }
}




/* =========================================
   8. MOBILE PHONE FIX (< 600px)
   ========================================= */

@media (max-width: 600px) {
    /* 1. Force a single column for the song */
    .song-container {
        column-count: 1;
        column-gap: 0;
        column-rule: none;
    }

    /* 2. Allow the header controls to wrap nicely on tiny screens */
    .meta-info {
        flex-wrap: wrap;
        gap: 10px;
    }

    .transpose-controls {
        margin-left: 0 !important; /* Push to left when wrapped */
        margin-top: 10px;
        flex-wrap: wrap;
        width: 100%;
    }
}