:root {
    --bg: #fff8ef;
    --card: #ffffff;
    --text: #28170b;
    --muted: #7d6d5e;
    --primary: #683d1b;
    --primary-dark: #4d2b12;
    --primary-soft: #f0dfce;
    --border: #eadbca;
    --danger: #a32929;
    --danger-soft: #ffe7e7;
    --success: #286b3b;
    --success-soft: #e7f6eb;
    --shadow: 0 14px 30px rgba(54, 31, 14, .10);
}

* { box-sizing: border-box; }

html { min-height: 100%; }

body {
    margin: 0;
    min-height: 100%;
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    background: var(--bg);
    color: var(--text);
    padding-bottom: 84px;
}

body.admin-body,
body.login-body { padding-bottom: 24px; }

a { color: inherit; }

.app-header,
.viewer-header,
.admin-topbar {
    background: linear-gradient(135deg, #81502a, #573014);
    color: #fff;
    padding: max(24px, env(safe-area-inset-top)) 20px 24px;
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    border-bottom-left-radius: 28px;
    border-bottom-right-radius: 28px;
    box-shadow: var(--shadow);
}

.app-header.compact { display: block; }

.app-header h1,
.viewer-header h1,
.admin-topbar h1 {
    margin: 4px 0 6px;
    font-size: clamp(1.75rem, 7vw, 2.6rem);
    line-height: 1.05;
}

.viewer-header h1 { font-size: clamp(1.25rem, 5vw, 1.9rem); }
.admin-topbar h1 { font-size: clamp(1.5rem, 5vw, 2.25rem); }

.eyebrow {
    margin: 0;
    text-transform: uppercase;
    letter-spacing: .12em;
    font-size: .72rem;
    font-weight: 700;
    opacity: .85;
}

.eyebrow.dark { color: var(--primary); }

.subtitle {
    margin: 0;
    color: rgba(255,255,255,.86);
    line-height: 1.45;
}

.container {
    width: min(1080px, calc(100% - 28px));
    margin: 20px auto 0;
}

.admin-container { margin-top: 18px; }

.search-card,
.form-card,
.empty-card,
.song-card,
.notice,
.panel-card,
.stat-card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 22px;
    box-shadow: var(--shadow);
}

.search-card,
.form-card,
.empty-card,
.panel-card,
.stat-card { padding: 18px; }

.no-shadow { box-shadow: none; }

.search-card label,
.form-card label {
    display: block;
    font-weight: 800;
    margin-bottom: 8px;
}

.search-row,
.inline-search,
.form-actions,
.action-grid,
.admin-top-actions {
    display: flex;
    gap: 10px;
    align-items: center;
    flex-wrap: wrap;
}

input[type="search"],
input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
input[type="file"] {
    width: 100%;
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 14px 14px;
    font: inherit;
    background: #fffdf9;
    color: var(--text);
}

input[type="file"] { padding: 12px; }
input[type="checkbox"] { width: auto; }

input:focus {
    outline: 3px solid var(--primary-soft);
    border-color: var(--primary);
}

.button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 46px;
    border: 0;
    border-radius: 999px;
    padding: 12px 18px;
    background: var(--primary);
    color: #fff;
    text-decoration: none;
    font: inherit;
    font-weight: 800;
    cursor: pointer;
    white-space: nowrap;
}

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

.button-light {
    background: #fff;
    color: var(--primary);
}

.button-secondary,
.button-secondary-light {
    background: var(--primary-soft);
    color: var(--primary);
}

.button-secondary-light { background: rgba(255,255,255,.20); color: #fff; }

.button-danger {
    background: var(--danger);
    color: #fff;
}

.full-width { width: 100%; }

.muted {
    color: var(--muted);
    line-height: 1.45;
}

.notice {
    padding: 14px 16px;
    margin-bottom: 14px;
}

.notice-success {
    color: var(--success);
    border-color: #bfe8c9;
    background: var(--success-soft);
}

.notice-error {
    color: var(--danger);
    border-color: #f0b7b7;
    background: var(--danger-soft);
}

.songs-list {
    display: grid;
    gap: 12px;
    margin-top: 16px;
}

.song-card { overflow: hidden; }

.song-link {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 14px;
    padding: 18px;
    color: inherit;
    text-decoration: none;
}

.song-title {
    font-weight: 850;
    font-size: 1.05rem;
}

.song-meta {
    color: var(--muted);
    font-size: .9rem;
    white-space: nowrap;
}

.back-link {
    display: inline-flex;
    align-items: center;
    min-height: 42px;
    color: #fff;
    text-decoration: none;
    font-weight: 800;
}

.viewer-main {
    height: calc(100vh - 120px);
    padding: 12px;
}

.pdf-viewer {
    width: 100%;
    height: 100%;
    border: 0;
    border-radius: 20px;
    background: #fff;
    box-shadow: var(--shadow);
}

.bottom-nav {
    position: fixed;
    left: 50%;
    bottom: max(12px, env(safe-area-inset-bottom));
    transform: translateX(-50%);
    width: min(420px, calc(100% - 28px));
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    padding: 8px;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: rgba(255, 255, 255, .92);
    box-shadow: var(--shadow);
    backdrop-filter: blur(10px);
    z-index: 20;
}

.bottom-nav a {
    text-align: center;
    text-decoration: none;
    border-radius: 999px;
    padding: 12px;
    font-weight: 800;
    color: var(--primary);
}

.bottom-nav a[aria-current="page"] {
    background: var(--primary);
    color: #fff;
}

.admin-nav {
    width: min(1080px, calc(100% - 28px));
    margin: 14px auto 0;
    display: flex;
    gap: 8px;
    overflow-x: auto;
    padding: 8px;
    background: rgba(255,255,255,.72);
    border: 1px solid var(--border);
    border-radius: 999px;
}

.admin-nav a {
    padding: 10px 14px;
    border-radius: 999px;
    color: var(--primary);
    text-decoration: none;
    font-weight: 800;
    white-space: nowrap;
}

.admin-nav a[aria-current="page"] {
    background: var(--primary);
    color: #fff;
}

.login-wrap {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 20px;
}

.login-card {
    width: min(460px, 100%);
}

.panel-card,
.form-card { margin-bottom: 16px; }

.panel-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 14px;
    flex-wrap: wrap;
    margin-bottom: 14px;
}

.panel-header h2,
.form-card h2 { margin: 0 0 6px; }

.stats-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
    margin-bottom: 16px;
}

.stat-number {
    display: block;
    font-size: 2.4rem;
    font-weight: 900;
    color: var(--primary);
    line-height: 1;
}

.stat-label {
    display: block;
    color: var(--muted);
    margin-top: 6px;
}

.action-grid {
    align-items: stretch;
}

.table-wrap {
    overflow-x: auto;
    border: 1px solid var(--border);
    border-radius: 18px;
}

.admin-table {
    width: 100%;
    border-collapse: collapse;
    min-width: 720px;
    background: #fff;
}

.admin-table th,
.admin-table td {
    text-align: left;
    padding: 13px 14px;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}

.admin-table th {
    background: #fff4e6;
    color: var(--primary);
    font-size: .9rem;
}

.admin-table tr:last-child td { border-bottom: 0; }

.actions-cell {
    white-space: nowrap;
}

.small-link {
    color: var(--primary);
    font-weight: 800;
    text-decoration: none;
    margin-right: 10px;
}

.danger-link { color: var(--danger); }

.badge {
    display: inline-block;
    padding: 3px 8px;
    border-radius: 999px;
    background: var(--primary-soft);
    color: var(--primary);
    font-size: .78rem;
    font-weight: 800;
}

.badge-success {
    background: var(--success-soft);
    color: var(--success);
}

.badge-muted {
    background: #eee;
    color: #666;
}

.inline-search {
    margin-bottom: 14px;
}

.inline-search input[type="search"] {
    flex: 1 1 260px;
}

.checkbox-line {
    display: flex !important;
    align-items: center;
    gap: 10px;
    margin-top: 16px !important;
}

.danger-zone {
    border-color: #f0b7b7;
    background: #fffafa;
}

@media (max-width: 720px) {
    .app-header,
    .viewer-header,
    .admin-topbar {
        flex-direction: column;
        align-items: stretch;
    }

    .song-link {
        align-items: flex-start;
        flex-direction: column;
    }

    .song-meta { white-space: normal; }

    .stats-grid {
        grid-template-columns: 1fr;
    }

    .button,
    .admin-top-actions .button,
    .form-actions .button,
    .action-grid .button {
        width: 100%;
    }

    .viewer-main {
        height: calc(100vh - 180px);
    }
}

.alphabet-card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 22px;
    box-shadow: var(--shadow);
    padding: 18px;
    margin-top: 16px;
}

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

.alphabet-header h2 {
    margin: 0 0 6px;
}

.alphabet-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(58px, 1fr));
    gap: 8px;
}

.letter-chip {
    min-height: 54px;
    border: 1px solid var(--border);
    border-radius: 16px;
    background: #fffdf9;
    color: var(--primary);
    text-decoration: none;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    font-weight: 900;
}

.letter-chip small {
    margin-top: 2px;
    color: var(--muted);
    font-size: .72rem;
    font-weight: 800;
}

.letter-chip.active {
    background: var(--primary);
    border-color: var(--primary);
    color: #fff;
}

.letter-chip.active small {
    color: rgba(255,255,255,.82);
}

.letter-chip.disabled {
    opacity: .38;
    color: var(--muted);
    background: #f4eee7;
}

.pagination {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    flex-wrap: wrap;
    margin: 18px 0 4px;
}

.page-link,
.page-ellipsis {
    min-width: 44px;
    min-height: 44px;
    border-radius: 999px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 10px 14px;
    background: #fff;
    border: 1px solid var(--border);
    color: var(--primary);
    text-decoration: none;
    font-weight: 850;
}

.page-link.active {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
}

.page-ellipsis {
    color: var(--muted);
    background: transparent;
    border-color: transparent;
}

.pdf-shell {
    height: 100%;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.pdf-toolbar {
    background: rgba(255,255,255,.96);
    border: 1px solid var(--border);
    border-radius: 18px;
    box-shadow: var(--shadow);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 8px;
    position: sticky;
    top: 8px;
    z-index: 10;
}

.pdf-page-controls,
.pdf-zoom-controls {
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.pdf-tool-button,
.pdf-open-link {
    min-height: 38px;
    min-width: 38px;
    border: 0;
    border-radius: 999px;
    background: var(--primary-soft);
    color: var(--primary);
    font: inherit;
    font-weight: 900;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 8px 12px;
    cursor: pointer;
}

.pdf-tool-button:disabled {
    opacity: .45;
    cursor: not-allowed;
}

.pdf-page-label {
    color: var(--text);
    font-size: .92rem;
    white-space: nowrap;
}

.pdf-canvas-wrap {
    flex: 1;
    min-height: 0;
    overflow: auto;
    border: 1px solid var(--border);
    border-radius: 20px;
    background: #d7d1c9;
    box-shadow: var(--shadow);
    padding: 8px;
    text-align: center;
    -webkit-overflow-scrolling: touch;
}

.pdf-canvas {
    display: block;
    margin: 0 auto;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 8px 20px rgba(0,0,0,.16);
}

.pdf-status {
    margin: 20px auto;
    color: var(--text);
    font-weight: 800;
}

.pdf-fallback {
    margin-top: 10px;
}

@media (max-width: 720px) {
    .alphabet-header {
        flex-direction: column;
    }

    .alphabet-grid {
        grid-template-columns: repeat(4, minmax(0, 1fr));
    }

    .pdf-toolbar {
        position: static;
        flex-wrap: wrap;
    }

    .pdf-page-controls {
        flex: 1 1 100%;
        justify-content: space-between;
    }

    .pdf-zoom-controls,
    .pdf-open-link {
        flex: 1 1 auto;
    }

    .pdf-tool-button,
    .pdf-open-link {
        min-height: 44px;
    }
}

@supports (height: 100dvh) {
    .viewer-main { height: calc(100dvh - 120px); }
    @media (max-width: 720px) {
        .viewer-main { height: calc(100dvh - 190px); }
    }
}
