/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --cream:      #faf9f5;
    --parchment:  #f0ece0;
    --gold:       #b8860b;
    --gold-lt:    #e8d5a3;
    --ink:        #1a1a1a;
    --ink-mid:    #444;
    --ink-lt:     #888;
    --blue:       #5b6fa8;   /* periwinkle — matches the Riemenschneider cover */
    --blue-dark:  #3d4d80;
    --blue-lt:    #e4e9f4;
    --red:        #8b1a1a;
    --staff-line: #2a2a2a;
    --notehead:   #1a1a1a;
    --ghost:      #aaaaaa;
    --border:     #ccc5a8;
    --radius:     6px;
    --shadow:     0 2px 8px rgba(0,0,0,0.12);
}

body {
    font-family: 'Georgia', serif;
    background: var(--cream);
    color: var(--ink);
    line-height: 1.6;
    min-height: 100vh;
}

/* ===== Header — inspired by the Riemenschneider Bach chorale book ===== */
header {
    background: var(--blue);
    padding: 32px 40px;
    position: relative;
    border-bottom: 3px solid var(--gold);
    text-align: center;
    overflow: hidden;
}

/* Double inner frame lines echoing the book's ornamental border */
header::before {
    content: '';
    position: absolute;
    inset: 9px;
    border: 1px solid rgba(255,255,255,0.38);
    pointer-events: none;
    z-index: 1;
}
header::after {
    content: '';
    position: absolute;
    inset: 14px;
    border: 1px solid rgba(255,255,255,0.18);
    pointer-events: none;
    z-index: 1;
}

/* Botanical corner ornaments in the blue border area */
.corner-ornament {
    position: absolute;
    color: rgba(255,255,255,0.28);
    font-size: 2.4rem;
    line-height: 1;
    z-index: 2;
    font-family: 'Georgia', serif;
    user-select: none;
}
.corner-tl { top: 18px;  left: 24px;  transform: rotate(-20deg); }
.corner-tr { top: 18px;  right: 24px; transform: rotate(200deg); }
.corner-bl { bottom: 18px; left: 24px;  transform: rotate(160deg); }
.corner-br { bottom: 18px; right: 24px; transform: rotate(-20deg) scaleX(-1); }

.home-link {
    position: fixed;
    top: 14px;
    left: 24px;
    z-index: 9002;
    color: rgba(255,255,255,0.75);
    font-family: 'Georgia', serif;
    font-size: 0.78rem;
    letter-spacing: 0.03em;
    text-decoration: none;
    border: 1px solid rgba(255,255,255,0.30);
    border-radius: 20px;
    padding: 0.2rem 0.8rem;
    transition: color 0.2s, border-color 0.2s, background 0.2s;
    white-space: nowrap;
}
.home-link:hover {
    color: white;
    border-color: rgba(255,255,255,0.7);
    background: rgba(255,255,255,0.12);
}

/* White inner panel — like the book's cream-white title page */
.header-panel {
    background: white;
    max-width: 600px;
    margin: 0 auto;
    padding: 2.5rem 2.5rem 2.25rem;
    position: relative;
    z-index: 2;
    box-shadow: 0 0 0 1px rgba(74,95,150,0.14), 0 6px 28px rgba(0,0,0,0.16);
}

/* Thin inner border on the white panel */
.header-panel::before {
    content: '';
    position: absolute;
    inset: 7px;
    border: 1px solid rgba(91,111,168,0.12);
    pointer-events: none;
}

/* Bach portrait */
.bach-portrait-wrap {
    display: flex;
    justify-content: center;
    margin-bottom: 1.25rem;
}

.bach-portrait {
    width: 84px;
    height: 102px;
    object-fit: cover;
    object-position: 50% 6%;
    border-radius: 42px / 51px;
    border: 2px solid var(--blue);
    box-shadow: 0 2px 12px rgba(91,111,168,0.3);
    filter: sepia(12%);
    display: block;
}

/* Ornamental rule between portrait and title */
.header-rule {
    color: var(--blue);
    opacity: 0.4;
    letter-spacing: 0.55em;
    margin: 0 0 1rem;
    font-size: 0.78rem;
    font-family: 'Georgia', serif;
}

header h1 {
    font-size: 1.85rem;
    font-weight: normal;
    letter-spacing: 0.13em;
    text-transform: uppercase;
    color: var(--blue);
    line-height: 1.35;
    margin-bottom: 0.4rem;
}

header .subtitle {
    font-size: 0.88rem;
    color: var(--ink-mid);
    font-style: italic;
    letter-spacing: 0.02em;
    margin-top: 0.2rem;
}

/* ===== Main layout ===== */
main {
    max-width: 1000px;
    margin: 2rem auto;
    padding: 0 1.5rem 3rem;
}

section {
    background: white;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.75rem;
    margin-bottom: 2rem;
    box-shadow: var(--shadow);
}

h2 {
    font-size: 1.2rem;
    font-weight: normal;
    color: var(--blue);
    border-bottom: 1px solid var(--border);
    padding-bottom: 0.5rem;
    margin-bottom: 1rem;
}

h3 {
    font-size: 1rem;
    font-weight: bold;
    color: var(--ink-mid);
    margin: 1.25rem 0 0.75rem;
}

.help-text {
    color: var(--ink-mid);
    font-size: 0.9rem;
    margin-bottom: 1rem;
}

/* ===== Toolbar ===== */
#toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
    align-items: center;
    background: var(--parchment);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.75rem 1rem;
    margin-bottom: 1rem;
}

.toolbar-group {
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

.toolbar-group + .toolbar-group {
    padding-left: 0.75rem;
    border-left: 1px solid var(--border);
}

.toolbar-label {
    font-size: 0.78rem;
    color: var(--ink-lt);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    margin-right: 0.2rem;
}

.toolbar-actions {
    margin-left: auto;
}

/* Duration and accidental buttons */
.dur-btn, .acc-btn {
    background: white;
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 0.35rem 0.6rem;
    font-size: 0.82rem;
    cursor: pointer;
    color: var(--ink-mid);
    transition: background 0.15s, color 0.15s, border-color 0.15s;
    white-space: nowrap;
}

.dur-btn:hover, .acc-btn:hover {
    background: var(--blue-lt);
    border-color: var(--blue);
    color: var(--blue);
}

.dur-btn.active, .acc-btn.active {
    background: var(--blue);
    border-color: var(--blue);
    color: white;
}

/* Music-symbol spans inside duration buttons */
.mus-sym {
    font-family: 'Bravura', 'Petaluma', 'FreeSerif', 'Symbola', 'Segoe UI Symbol', serif;
    font-size: 1.15em;
    line-height: 1;
}

.note-glyph {
    font-family: 'Segoe UI Symbol', 'Noto Sans Symbols', sans-serif;
}

#key-select {
    font-size: 0.82rem;
    font-family: inherit;
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 0.3rem 0.5rem;
    background: white;
    color: var(--ink-mid);
    cursor: pointer;
}

#key-select:focus {
    outline: 2px solid var(--blue);
    outline-offset: 1px;
}

/* Edit action buttons */
#undo-btn, #clear-btn, #play-btn {
    background: white;
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 0.35rem 0.7rem;
    font-size: 0.82rem;
    cursor: pointer;
    color: var(--ink-mid);
    transition: background 0.15s;
}

#undo-btn:hover, #clear-btn:hover { background: #fff0f0; border-color: var(--red); color: var(--red); }
#play-btn:hover { background: var(--blue-lt); border-color: var(--blue); color: var(--blue); }

/* ===== Staff SVG ===== */
#staff-wrapper {
    position: relative;
    background: #fdfcf8;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow-x: auto;   /* scroll when SVG grows wider than the container */
    overflow-y: hidden;
    margin-bottom: 0.5rem;
}

#staff-svg {
    display: block;
    cursor: crosshair;
    /* width is set programmatically — do not override with CSS */
}

#staff-hint {
    position: absolute;
    bottom: 6px;
    right: 10px;
    font-size: 0.75rem;
    color: var(--ink-lt);
    font-style: italic;
    pointer-events: none;
}

/* ===== Input summary ===== */
#input-summary {
    font-size: 0.85rem;
    color: var(--ink-mid);
    min-height: 1.5em;
    margin-bottom: 0.5rem;
}

/* ===== Options Panel ===== */
#options-panel {
    background: var(--parchment);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.75rem 1rem 1rem;
    margin-bottom: 1.25rem;
}

.options-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.6rem 1.5rem;
}

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

.option {
    display: flex;
    flex-direction: row;
    align-items: flex-start;
    gap: 0.4rem;
    font-size: 0.88rem;
    cursor: pointer;
}

.option input[type="checkbox"] {
    flex-shrink: 0;
    margin-top: 3px;
    accent-color: var(--blue);
}

.option select {
    font-size: 0.85rem;
    border: 1px solid var(--border);
    border-radius: 3px;
    padding: 0.15rem 0.3rem;
    background: white;
    accent-color: var(--blue);
}

.option-label {
    display: flex;
    flex-direction: column;
    gap: 0.1rem;
}

.option-note {
    font-size: 0.75rem;
    color: var(--ink-lt);
    font-style: italic;
}

/* ===== Search button ===== */
.primary-btn {
    background: var(--blue);
    color: white;
    border: none;
    border-radius: var(--radius);
    padding: 0.65rem 2rem;
    font-size: 1rem;
    font-family: inherit;
    cursor: pointer;
    transition: background 0.15s;
    margin-right: 1rem;
}

.primary-btn:hover:not(:disabled) { background: var(--blue-dark); }
.primary-btn:disabled { background: #aaa; cursor: not-allowed; }

#search-status {
    font-size: 0.85rem;
    color: var(--ink-lt);
    font-style: italic;
}

/* ===== Results ===== */
#results-summary {
    font-size: 0.9rem;
    color: var(--ink-mid);
    margin-bottom: 1rem;
}

.result-card {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: 1.5rem;
    overflow: hidden;
}

.result-header {
    background: var(--blue);
    color: white;
    padding: 0.6rem 1rem;
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    flex-wrap: wrap;
    gap: 0.4rem;
}

.result-header h3 {
    color: white;
    margin: 0;
    font-size: 0.95rem;
}

.result-meta {
    font-size: 0.8rem;
    color: var(--gold-lt);
}

.result-body {
    padding: 1rem;
    background: #fdfcf8;
}

/* abcjs rendered notation */
.result-notation {
    overflow-x: auto;
    margin-bottom: 0.75rem;
    background: white;
    border-radius: 4px;
    padding: 0.5rem;
}

.result-notation svg {
    max-width: 100%;
}

.result-context {
    font-size: 0.82rem;
    color: var(--ink-mid);
    background: var(--parchment);
    border-radius: 4px;
    padding: 0.5rem 0.75rem;
}

.result-context strong {
    color: var(--red);
}

.ctx-note {
    font-style: italic;
    color: var(--ink-lt);
    font-weight: normal;
}

.play-btn {
    background: white;
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 0.3rem 0.8rem;
    font-size: 0.8rem;
    cursor: pointer;
    color: var(--ink-mid);
    transition: background 0.15s;
    margin-bottom: 0.5rem;
}

.play-btn:hover { background: var(--blue-lt); border-color: var(--blue); color: var(--blue); }
.play-btn.playing { background: var(--parchment); border-color: var(--gold); color: #665500; }

.no-results {
    text-align: center;
    color: var(--ink-lt);
    padding: 2rem;
    font-style: italic;
}

.loading {
    text-align: center;
    color: var(--ink-lt);
    padding: 1rem;
    font-style: italic;
}

.data-warning {
    background: #fff8e0;
    border: 1px solid var(--gold);
    border-radius: var(--radius);
    padding: 0.75rem 1rem;
    margin-bottom: 1rem;
    font-size: 0.88rem;
    color: #665500;
}

/* ===== Page-wide border frame (fixed overlay, sits above all content) ===== */
.page-frame {
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 9000;
    border: 12px solid var(--blue);
    box-shadow:
        inset 0 0 0 3px var(--blue),
        inset 0 0 0 5px rgba(255,255,255,0.55),
        inset 0 0 0 8px var(--blue);
}

.page-corner {
    position: fixed;
    color: rgba(255,255,255,0.75);
    font-size: 1.6rem;
    line-height: 1;
    pointer-events: none;
    z-index: 9001;
    font-family: 'Georgia', serif;
    user-select: none;
}
.page-corner-tl { top: 3px;    left: 6px;   transform: rotate(-20deg); }
.page-corner-tr { top: 3px;    right: 6px;  transform: rotate(200deg); }
.page-corner-bl { bottom: 3px; left: 6px;   transform: rotate(160deg); }
.page-corner-br { bottom: 3px; right: 6px;  transform: rotate(-20deg) scaleX(-1); }

/* ===== Header credit ===== */
.header-credit {
    font-size: 0.78rem;
    color: var(--ink-lt);
    letter-spacing: 0.06em;
    margin-top: 0.5rem;
    font-style: italic;
}

/* ===== Details / Disclaimers ===== */
.disclaimers {
    background: white;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.75rem 1.25rem;
    margin-bottom: 2rem;
    font-size: 0.88rem;
    color: var(--ink-mid);
    box-shadow: var(--shadow);
}

.disclaimers summary {
    font-weight: bold;
    color: var(--blue);
    cursor: pointer;
    letter-spacing: 0.03em;
    list-style: none;
    user-select: none;
}
.disclaimers summary::before { content: '▸ '; font-size: 0.75rem; }
.disclaimers[open] summary::before { content: '▾ '; }
.disclaimers summary::-webkit-details-marker { display: none; }

.disclaimer-body {
    margin-top: 0.75rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}
.disclaimer-body a { color: var(--blue); }

/* ===== Footer ===== */
footer {
    text-align: center;
    color: var(--ink-lt);
    font-size: 0.8rem;
    padding: 1rem 1rem 1.5rem;
    border-top: 1px solid var(--border);
}

/* Support link — sits between input and results */
.mid-support {
    text-align: center;
    margin: 0 0 0.75rem;
    font-size: 0.85rem;
}
.mid-support a { color: var(--blue); text-decoration: none; font-style: italic; }
.mid-support a:hover { text-decoration: underline; }
