:root {
    --bg: #eef2f7;
    --surface: #ffffff;
    --surface-muted: #f7f9fc;
    --ink: #172235;
    --muted: #66758a;
    --line: #d9e1eb;
    --line-strong: #bfcbd9;
    --accent: #1f64c8;
    --accent-strong: #164d9f;
    --accent-soft: #e9f1ff;
    --danger: #be3b3b;
    --shadow: 0 16px 42px rgba(19, 39, 69, .09);
    --radius-xl: 22px;
    --radius-lg: 16px;
    --radius-md: 12px;
}

* { box-sizing: border-box; }

html { min-height: 100%; }

body {
    min-height: 100vh;
    margin: 0;
    background:
        radial-gradient(circle at 12% -8%, rgba(70, 135, 233, .17), transparent 30rem),
        linear-gradient(180deg, #f8fafc 0, var(--bg) 42rem);
    color: var(--ink);
    font-family: "Noto Sans TC", "Microsoft JhengHei", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
select,
textarea { font: inherit; }

button { cursor: pointer; }

.app-shell { padding: 40px 18px 64px; }

.app-container {
    width: min(1420px, 100%);
    margin: 0 auto;
}

.page-header { margin: 0 0 26px; }

.eyebrow {
    margin: 0 0 7px;
    color: var(--accent);
    font-size: .72rem;
    font-weight: 800;
    letter-spacing: .14em;
}

h1,
h2 { margin: 0; letter-spacing: -.025em; }

h1 {
    font-size: clamp(2rem, 4vw, 2.9rem);
    font-weight: 820;
}

h2 {
    font-size: 1.18rem;
    font-weight: 800;
}

.page-description {
    max-width: 740px;
    margin: 10px 0 0;
    color: var(--muted);
    font-size: .98rem;
    line-height: 1.65;
}

.panel,
.help-panel {
    border: 1px solid rgba(202, 213, 226, .92);
    border-radius: var(--radius-xl);
    background: var(--surface);
    box-shadow: var(--shadow);
}

.control-panel { padding: 25px; }

.format-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 16px;
}

.transpose-control-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 18px;
}

.key-card {
    border: 1px solid var(--line);
    border-radius: var(--radius-lg);
    background: linear-gradient(180deg, #ffffff 0%, #f8fbff 100%);
    padding: 18px;
}

.key-card-heading {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 15px;
}

.key-card-heading .eyebrow { margin-bottom: 0; }
.key-card-heading h2 { font-size: 1.08rem; }

.key-card-body {
    display: grid;
    grid-template-columns: minmax(0, .9fr) minmax(0, 1.1fr) minmax(0, auto);
    gap: 14px;
    align-items: start;
}

.key-card-source .key-card-body {
    grid-template-columns: minmax(0, .9fr) minmax(0, 1.1fr);
}

.notation-field .segmented-control { width: 100%; }
.notation-field .segment { flex: 1 1 0; white-space: nowrap; }

.auto-rule-note {
    color: var(--muted);
    font-size: .86rem;
    font-weight: 700;
    line-height: 1.6;
}

.control-footer-actions {
    align-items: center;
}


.field { display: block; }

.field-label {
    display: block;
    margin: 0 0 8px;
    color: #43536a;
    font-size: .84rem;
    font-weight: 800;
}

.select-control {
    width: 100%;
    min-height: 48px;
    padding: 0 42px 0 13px;
    border: 1px solid var(--line-strong);
    border-radius: var(--radius-md);
    outline: none;
    background: #fff;
    color: var(--ink);
    font-weight: 700;
    transition: border-color .16s ease, box-shadow .16s ease, background .16s ease;
}

.select-control:hover:not(:disabled) { border-color: #aab9cb; }

.select-control:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 4px rgba(31, 100, 200, .12);
}

.select-control:disabled {
    cursor: not-allowed;
    border-color: #e4e9f0;
    background: #f4f6f9;
    color: #98a4b2;
}

.key-detect-row {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 26px;
    margin-top: 8px;
}

.detect-key-button {
    flex: 0 0 auto;
    min-height: 29px;
    padding: 4px 8px;
    border: 1px solid #b9cce9;
    border-radius: 8px;
    background: var(--accent-soft);
    color: var(--accent-strong);
    font-size: .78rem;
    font-weight: 800;
    line-height: 1;
    transition: background .14s ease, border-color .14s ease, transform .14s ease;
}

.detect-key-button:hover:not(:disabled) {
    border-color: #8daee0;
    background: #dceaff;
    transform: translateY(-1px);
}

.detect-key-button:disabled {
    cursor: not-allowed;
    border-color: #e2e7ee;
    background: #f4f6f9;
    color: #9aa6b5;
}

.key-detect-hint {
    min-width: 0;
    color: var(--muted);
    font-size: .76rem;
    font-weight: 650;
    line-height: 1.35;
}

.key-detect-hint.is-detected {
    color: #187355;
}

.control-footer {
    display: flex;
    align-items: end;
    justify-content: space-between;
    gap: 18px;
    margin-top: 22px;
    padding-top: 21px;
    border-top: 1px solid var(--line);
}

.notation-label { margin-bottom: 8px; }

.segmented-control {
    display: inline-flex;
    padding: 3px;
    border: 1px solid var(--line);
    border-radius: 11px;
    background: var(--surface-muted);
}

.segment {
    min-height: 36px;
    padding: 6px 13px;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: #596a80;
    font-size: .88rem;
    font-weight: 800;
}

.segment:hover { background: #edf2f7; }

.segment.is-active {
    background: #fff;
    color: var(--accent);
    box-shadow: 0 2px 7px rgba(30, 50, 78, .12);
}

.action-group {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 10px;
}

.button {
    min-height: 47px;
    padding: 10px 18px;
    border: 1px solid transparent;
    border-radius: var(--radius-md);
    font-weight: 800;
    transition: transform .14s ease, background .14s ease, border-color .14s ease, box-shadow .14s ease;
}

.button:hover { transform: translateY(-1px); }
.button:active { transform: translateY(0); }

.button-primary {
    border-color: var(--accent);
    background: var(--accent);
    color: #fff;
    box-shadow: 0 7px 14px rgba(31, 100, 200, .22);
}

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

.button-secondary {
    border-color: var(--line-strong);
    background: #fff;
    color: #3f5066;
}

.button-secondary:hover { border-color: #aab9cb; background: #f8fafc; }

.status-message {
    min-height: 1.4em;
    margin: 15px 0 0;
    color: var(--muted);
    font-size: .86rem;
    font-weight: 650;
}

.status-message.is-error { color: var(--danger); }
.status-message.is-success { color: #147a58; }

.editor-grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 22px;
    margin-top: 22px;
}

.editor-panel {
    min-width: 0;
    padding: 22px;
}

.editor-heading {
    display: flex;
    align-items: end;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 14px;
}

.editor-actions { display: flex; gap: 10px; }

.text-button {
    padding: 4px 0;
    border: 0;
    background: transparent;
    color: var(--accent);
    font-size: .88rem;
    font-weight: 800;
}

.text-button:hover { color: var(--accent-strong); text-decoration: underline; }

.sheet-textarea {
    display: block;
    width: 100%;
    min-height: 54vh;
    padding: 16px;
    overflow: auto;
    border: 1px solid var(--line-strong);
    border-radius: var(--radius-lg);
    outline: none;
    background:
        linear-gradient(90deg, rgba(31, 100, 200, .032) 1px, transparent 1px) 0 0 / 4ch 100%,
        #fff;
    color: #202d40;
    font-family: "JetBrains Mono", "Cascadia Mono", Consolas, "Courier New", monospace;
    font-size: .91rem;
    line-height: 1.82;
    tab-size: 4;
    resize: vertical;
    transition: border-color .16s ease, box-shadow .16s ease;
}

.sheet-textarea:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 4px rgba(31, 100, 200, .12);
}

.sheet-textarea[readonly] {
    background:
        linear-gradient(90deg, rgba(31, 100, 200, .026) 1px, transparent 1px) 0 0 / 4ch 100%,
        #fbfcfe;
}

.editor-note {
    margin: 11px 2px 0;
    color: var(--muted);
    font-size: .8rem;
    line-height: 1.6;
}

code {
    padding: .08em .32em;
    border-radius: 5px;
    background: #edf2f8;
    color: #34465e;
    font-family: "JetBrains Mono", Consolas, monospace;
    font-size: .94em;
}

.help-panel {
    margin-top: 22px;
    padding: 0 22px;
    box-shadow: none;
}

.help-panel summary {
    padding: 18px 0;
    color: #3e4f65;
    font-size: .92rem;
    font-weight: 800;
    cursor: pointer;
}

.help-content {
    padding: 0 0 18px;
    border-top: 1px solid var(--line);
    color: #596a7e;
    font-size: .9rem;
    line-height: 1.75;
}

.help-content p { margin: 14px 0 0; }

@media (max-width: 1050px) {
    .format-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .key-card-body,
    .key-card-source .key-card-body { grid-template-columns: 1fr; }
}

@media (max-width: 760px) {
    .app-shell { padding: 22px 13px 36px; }

    .page-header { margin-bottom: 18px; }
    .page-description { font-size: .9rem; }

    .control-panel,
    .editor-panel { padding: 18px; border-radius: 18px; }

    .format-grid,
    .transpose-control-grid,
    .editor-grid { grid-template-columns: 1fr; }

    .format-grid,
    .transpose-control-grid { gap: 13px; }

    .key-card { padding: 15px; }
    .key-card-body,
    .key-card-source .key-card-body { grid-template-columns: 1fr; }

    .key-detect-row {
        align-items: flex-start;
        flex-direction: column;
        gap: 6px;
    }

    .control-footer {
        align-items: stretch;
        flex-direction: column;
        gap: 16px;
        margin-top: 18px;
        padding-top: 18px;
    }

    .action-group { justify-content: stretch; }
    .action-group .button { flex: 1 1 0; }

    .editor-grid { gap: 18px; margin-top: 18px; }
    .editor-heading { align-items: flex-start; }

    .sheet-textarea { min-height: 38vh; font-size: .84rem; }

    .help-panel { margin-top: 18px; padding: 0 18px; border-radius: 18px; }
}

@media (max-width: 390px) {
    .action-group { flex-direction: column-reverse; }
    .action-group .button { width: 100%; }
    .editor-heading { flex-direction: column; }
}


.format-detect-hint {
    display: block;
    min-height: 1.35rem;
    margin-top: 7px;
    color: var(--muted);
    font-size: .78rem;
    line-height: 1.45;
}

.format-detect-hint.is-detected {
    color: var(--success);
    font-weight: 700;
}


.import-panel {
    margin-bottom: 22px;
    padding: 21px 25px;
}

.import-heading {
    display: flex;
    align-items: end;
    justify-content: space-between;
    gap: 18px;
    margin-bottom: 16px;
}

.import-heading > p {
    max-width: 620px;
    margin: 0;
    color: var(--muted);
    font-size: .86rem;
    line-height: 1.55;
}

.import-row {
    display: flex;
    align-items: end;
    gap: 12px;
}

.import-url-field {
    display: block;
    min-width: 0;
    flex: 1 1 auto;
}

.text-control {
    display: block;
    width: 100%;
    min-height: 48px;
    padding: 0 13px;
    border: 1px solid var(--line-strong);
    border-radius: var(--radius-md);
    outline: none;
    background: #fff;
    color: var(--ink);
    font-family: "JetBrains Mono", "Cascadia Mono", Consolas, "Courier New", monospace;
    font-size: .87rem;
    transition: border-color .16s ease, box-shadow .16s ease;
}

.text-control::placeholder {
    color: #9aa7b6;
}

.text-control:hover {
    border-color: #aab9cb;
}

.text-control:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 4px rgba(31, 100, 200, .12);
}

.button-import {
    flex: 0 0 auto;
    min-width: 150px;
    border-color: #315d98;
    background: #315d98;
    color: #fff;
}

.button-import:hover:not(:disabled) {
    border-color: #244878;
    background: #244878;
}

.button-import:disabled {
    cursor: wait;
    opacity: .7;
}

.import-meta {
    min-height: 1.3rem;
    margin: 11px 0 0;
    color: var(--muted);
    font-size: .8rem;
    line-height: 1.55;
}

.import-meta.is-success { color: #147a58; }
.import-meta.is-error { color: var(--danger); }

@media (max-width: 760px) {
    .import-panel {
        margin-bottom: 18px;
        padding: 18px;
        border-radius: 18px;
    }

    .import-heading,
    .import-row {
        align-items: stretch;
        flex-direction: column;
        gap: 12px;
    }

    .button-import {
        width: 100%;
    }
}

/* Private login and library additions */
.page-header {
    display: flex;
    align-items: start;
    justify-content: space-between;
    gap: 18px;
}

.logout-link {
    flex: 0 0 auto;
    margin-top: 4px;
    padding: 9px 12px;
    border: 1px solid var(--line-strong);
    border-radius: 10px;
    color: #4c5d72;
    font-size: .86rem;
    font-weight: 800;
    text-decoration: none;
}

.logout-link:hover {
    border-color: #aab9cb;
    background: #f8fafc;
    color: var(--accent-strong);
}

.import-buttons,
.library-buttons {
    display: flex;
    flex: 0 0 auto;
    flex-wrap: wrap;
    align-items: end;
    gap: 10px;
}

.library-panel {
    margin-top: 18px;
    padding: 22px 25px;
}

.library-heading {
    display: flex;
    align-items: start;
    justify-content: space-between;
    gap: 18px;
    margin-bottom: 16px;
}

.library-heading > p {
    max-width: 620px;
    margin: 0;
    color: var(--muted);
    font-size: .88rem;
    line-height: 1.55;
}

.library-row {
    display: flex;
    align-items: end;
    gap: 14px;
}

.library-select-field {
    flex: 1 1 430px;
}

.button-danger {
    border-color: #e3b4b4;
    background: #fff7f7;
    color: #a13434;
}

.button-danger:hover:not(:disabled) {
    border-color: #ce8c8c;
    background: #ffefef;
}

.button-tertiary {
    border-color: #c7d4e7;
    background: #f6f9ff;
    color: #365987;
}

.button-tertiary:hover:not(:disabled) {
    border-color: #99b5da;
    background: #eef4ff;
}

.button:disabled {
    cursor: not-allowed;
    transform: none;
    opacity: .55;
}

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

.login-shell {
    width: min(430px, 100%);
}

.register-shell {
    width: min(480px, 100%);
}

.login-card {
    padding: 30px;
    border: 1px solid rgba(202, 213, 226, .92);
    border-radius: var(--radius-xl);
    background: var(--surface);
    box-shadow: var(--shadow);
}

.login-card h1 {
    font-size: clamp(1.8rem, 7vw, 2.35rem);
}

.login-description {
    margin: 11px 0 22px;
    color: var(--muted);
    line-height: 1.6;
}

.login-form {
    display: grid;
    gap: 15px;
}

.text-control {
    width: 100%;
    min-height: 48px;
    padding: 0 13px;
    border: 1px solid var(--line-strong);
    border-radius: var(--radius-md);
    outline: none;
    background: #fff;
    color: var(--ink);
    font-weight: 650;
}

.text-control:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 4px rgba(31, 100, 200, .12);
}

.login-button { width: 100%; }

.login-message {
    margin: 0 0 16px;
    padding: 11px 12px;
    border-radius: 10px;
    font-size: .87rem;
    font-weight: 700;
}

.login-message.is-error {
    border: 1px solid #efc0c0;
    background: #fff4f4;
    color: #a53232;
}

.field-optional {
    color: var(--muted);
    font-size: .82rem;
    font-weight: 650;
}

.login-switch,
.register-note {
    margin: 18px 0 0;
    color: var(--muted);
    font-size: .86rem;
    line-height: 1.6;
    text-align: center;
}

.register-note {
    margin-top: 14px;
    padding-top: 14px;
    border-top: 1px solid var(--line);
    font-size: .8rem;
}

.login-switch a {
    color: var(--accent-strong);
    font-weight: 850;
    text-decoration: none;
}

.login-switch a:hover {
    text-decoration: underline;
}

@media (max-width: 760px) {
    .page-header,
    .library-heading,
    .library-row {
        align-items: stretch;
        flex-direction: column;
    }

    .logout-link { align-self: flex-start; }

    .library-panel { padding: 18px; }

    .import-buttons,
    .library-buttons {
        width: 100%;
    }

    .import-buttons .button,
    .library-buttons .button {
        flex: 1 1 160px;
    }
}


/* Full-sheet reading mode */
.app-container {
    width: min(1700px, 100%);
}

.sheet-reader-panel {
    margin-top: 22px;
    padding: 24px;
}

.sheet-reader-heading {
    display: flex;
    align-items: end;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 9px;
}

.sheet-reader-actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: flex-end;
    gap: 10px;
}

.sheet-view-switch,
.sheet-layout-switch {
    display: inline-flex;
    padding: 3px;
    border: 1px solid var(--line);
    border-radius: 11px;
    background: var(--surface-muted);
}

.sheet-view-button {
    min-height: 35px;
    padding: 6px 12px;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: #596a80;
    font-size: .84rem;
    font-weight: 800;
}

.sheet-view-button:hover:not(:disabled) {
    background: #edf2f7;
}

.sheet-view-button.is-active {
    background: #fff;
    color: var(--accent);
    box-shadow: 0 2px 7px rgba(30, 50, 78, .12);
}

.sheet-view-button:disabled {
    cursor: not-allowed;
    opacity: .5;
}

.button-compact {
    min-height: 40px;
    padding: 8px 13px;
    font-size: .86rem;
}

.sheet-zoom-control {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 3px;
    border: 1px solid var(--line);
    border-radius: 11px;
    background: var(--surface-muted);
}

.sheet-zoom-button {
    min-height: 34px;
    padding: 5px 9px;
    border: 0;
    border-radius: 8px;
    background: #fff;
    color: #365987;
    font-size: .83rem;
    font-weight: 850;
    cursor: pointer;
    box-shadow: 0 1px 4px rgba(30, 50, 78, .08);
}

.sheet-zoom-button:hover {
    background: #f1f6ff;
}

.sheet-zoom-value {
    min-width: 48px;
    color: #596a80;
    font-size: .78rem;
    font-weight: 850;
    text-align: center;
}

.sheet-zoom-reset {
    color: #66758a;
    font-size: .78rem;
}

.sheet-viewer-meta {
    min-height: 1.35rem;
    margin: 0 0 13px;
    color: var(--muted);
    font-size: .83rem;
    line-height: 1.5;
}

.sheet-viewer {
    min-height: 62vh;
    margin: 0;
    padding: 22px;
    overflow-x: auto;
    overflow-y: visible;
    border: 1px solid var(--line-strong);
    border-radius: var(--radius-lg);
    outline: none;
    background:
        linear-gradient(90deg, rgba(31, 100, 200, .032) 1px, transparent 1px) 0 0 / 4ch 100%,
        #fff;
    color: #202d40;
    font-family: "JetBrains Mono", "Cascadia Mono", Consolas, "Courier New", monospace;
    font-size: calc(clamp(.88rem, 1.12vw, 1.02rem) * var(--sheet-font-scale, 1));
    font-variant-ligatures: none;
    line-height: 1.86;
    tab-size: 4;
}

.sheet-viewer-columns {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 0;
    min-width: 0;
}

.sheet-viewer.is-smart-columns .sheet-viewer-columns {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 24px;
}

.sheet-viewer-column {
    min-width: 0;
    margin: 0;
    overflow-x: auto;
    color: inherit;
    font: inherit;
    font-variant-ligatures: inherit;
    line-height: inherit;
    tab-size: inherit;
    white-space: pre;
}

.sheet-viewer.is-smart-columns .sheet-viewer-column + .sheet-viewer-column {
    padding-left: 24px;
    border-left: 1px solid var(--line);
}

.sheet-viewer:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 4px rgba(31, 100, 200, .12);
}

.sheet-viewer.is-empty {
    display: grid;
    min-height: 42vh;
    place-items: center;
    color: var(--muted);
    font-family: inherit;
    font-size: calc(.95rem * var(--sheet-font-scale, 1));
    text-align: center;
    white-space: pre-wrap;
}

.sheet-viewer-note {
    margin: 11px 2px 0;
    color: var(--muted);
    font-size: .8rem;
    line-height: 1.6;
}

.editor-panel--single {
    margin-top: 22px;
    padding: 24px;
}

.editor-panel--single .sheet-textarea--editor {
    min-height: 72vh;
}

.editor-panel[hidden],
.sheet-reader-panel[hidden] {
    display: none;
}

@media (max-width: 760px) {
    .sheet-reader-panel,
    .editor-panel--single {
        margin-top: 18px;
        padding: 18px;
        border-radius: 18px;
    }

    .sheet-reader-heading {
        align-items: stretch;
        flex-direction: column;
    }

    .sheet-reader-actions {
        justify-content: stretch;
    }

    .sheet-view-switch,
    .sheet-layout-switch,
    .sheet-zoom-control,
    .sheet-reader-actions .button {
        flex: 1 1 auto;
    }

    .sheet-view-button {
        flex: 1 1 0;
    }

    .sheet-zoom-control {
        justify-content: center;
    }

    .sheet-viewer {
        min-height: 54vh;
        padding: 15px;
        font-size: .8rem;
        line-height: 1.78;
    }

    .editor-panel--single .sheet-textarea--editor {
        min-height: 64vh;
        font-size: .84rem;
    }
}

@media (max-width: 760px) {
    .sheet-viewer.is-smart-columns .sheet-viewer-columns {
        grid-template-columns: minmax(0, 1fr);
        gap: 0;
    }

    .sheet-viewer.is-smart-columns .sheet-viewer-column + .sheet-viewer-column {
        padding-top: 22px;
        padding-left: 0;
        border-top: 1px solid var(--line);
        border-left: 0;
    }
}

/* Manual save and category library additions */
.manual-save-box,
.category-manager {
    margin: 16px 0 18px;
    padding: 18px;
    border: 1px solid var(--line);
    border-radius: 18px;
    background: #fbfdff;
}

.manual-save-heading,
.category-manager-heading {
    display: flex;
    align-items: end;
    justify-content: space-between;
    gap: 14px;
    margin-bottom: 14px;
}

.manual-save-heading h3,
.category-manager-heading h3 {
    margin: 0;
    color: var(--ink);
    font-size: 1.08rem;
}

.manual-save-heading > p {
    max-width: 560px;
    margin: 0;
    color: var(--muted);
    font-size: .82rem;
    line-height: 1.55;
}

.manual-save-grid,
.category-manager-grid {
    display: grid;
    grid-template-columns: minmax(180px, 1.2fr) minmax(180px, 1fr) minmax(170px, .85fr) auto;
    align-items: end;
    gap: 12px;
}

.manual-save-actions,
.category-manager-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.library-filter-row {
    align-items: end;
    margin-top: 18px;
}

.library-filter-field {
    flex: 0 1 220px;
}

.library-search-field {
    width: 100%;
}

.library-search-control {
    position: relative;
    display: block;
}

.library-search-control .text-control {
    width: 100%;
    padding-right: 42px;
}

.library-search-clear {
    position: absolute;
    top: 50%;
    right: 9px;
    width: 28px;
    height: 28px;
    padding: 0;
    transform: translateY(-50%);
    border: 0;
    border-radius: 50%;
    background: #e9eef6;
    color: var(--muted);
    font: inherit;
    font-size: 1.1rem;
    line-height: 1;
    cursor: pointer;
}

.library-search-clear:hover,
.library-search-clear:focus-visible {
    background: #dce5f1;
    color: var(--ink);
    outline: none;
}

.library-select-field {
    flex: 1 1 420px;
}

#savedSheetSelect optgroup {
    color: #66758a;
    font-weight: 850;
}

#savedSheetSelect option {
    color: var(--ink);
    font-weight: 650;
}

@media (max-width: 1050px) {
    .manual-save-grid,
    .category-manager-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .manual-save-actions,
    .category-manager-actions {
        grid-column: 1 / -1;
    }
}

@media (max-width: 760px) {
    .manual-save-heading,
    .category-manager-heading {
        align-items: stretch;
        flex-direction: column;
        gap: 8px;
    }

    .manual-save-grid,
    .category-manager-grid {
        grid-template-columns: 1fr;
    }

    .manual-save-actions .button,
    .category-manager-actions .button {
        flex: 1 1 150px;
    }

    .library-filter-field,
    .library-select-field {
        flex: 1 1 auto;
        width: 100%;
    }
}

/* Workspace layout refresh */
.app-shell--workspace {
    padding: 18px;
}

.app-container--workspace {
    width: min(1680px, 100%);
}

.workspace-topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 18px;
    margin-bottom: 16px;
    padding: 18px 20px;
    border: 1px solid rgba(202, 213, 226, .92);
    border-radius: var(--radius-xl);
    background: rgba(255, 255, 255, .9);
    box-shadow: 0 10px 30px rgba(19, 39, 69, .06);
    backdrop-filter: blur(10px);
}

.workspace-brand h1 {
    font-size: clamp(1.45rem, 2vw, 2.15rem);
    line-height: 1.1;
}

.workspace-status {
    margin-top: 8px;
    max-width: 720px;
}

.library-current-badge {
    display: inline-flex;
    align-items: center;
    width: fit-content;
    margin-top: 8px;
    padding: 5px 10px;
    border: 1px solid rgba(202, 213, 226, .95);
    border-radius: 999px;
    background: #f8fafc;
    color: #344054;
    font-size: .84rem;
    font-weight: 750;
}

.workspace-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: 8px;
}

.logout-link--topbar {
    min-height: 38px;
    padding: 8px 12px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: #fff;
    color: var(--muted);
    font-size: .86rem;
    font-weight: 800;
    text-decoration: none;
}

.app-workspace {
    display: grid;
    grid-template-columns: minmax(260px, 320px) minmax(0, 1fr);
    gap: 16px;
    align-items: start;
}

.workspace-sidebar {
    position: sticky;
    top: 16px;
    align-self: start;
    max-height: calc(100vh - 36px);
    overflow: auto;
}

.workspace-main {
    min-width: 0;
}

.library-panel--sidebar {
    padding: 18px;
}

.library-heading--compact {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 16px;
}

.library-heading--compact h2 {
    font-size: 1.25rem;
}

.library-filter-row--sidebar,
.library-buttons--sidebar {
    display: grid;
    gap: 12px;
}

.library-filter-row--sidebar .library-filter-field,
.library-filter-row--sidebar .library-search-field,
.library-filter-row--sidebar .library-select-field,
.library-filter-row--sidebar .library-buttons {
    width: 100%;
}

#savedSheetSelect[size] {
    min-height: 360px;
    padding: 8px;
    overflow: auto;
}

#savedSheetSelect[size] option {
    padding: 8px 10px;
    border-radius: 8px;
}

#savedSheetSelect[size] optgroup {
    color: var(--accent-strong);
    font-weight: 900;
}

.sheet-reader-panel--workspace {
    padding: 22px;
    min-height: calc(100vh - 150px);
}

.sheet-reader-panel--workspace .sheet-viewer {
    min-height: calc(100vh - 315px);
}

.sheet-reader-actions {
    flex-wrap: wrap;
}

.help-panel--workspace {
    margin-top: 16px;
}

.workspace-drawer[hidden],
.utility-modal[hidden] {
    display: none !important;
}

.workspace-drawer,
.utility-modal {
    position: fixed;
    inset: 0;
    z-index: 2000;
}

.drawer-backdrop,
.modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(15, 23, 42, .42);
    backdrop-filter: blur(3px);
}

.control-panel--drawer {
    position: absolute;
    top: 16px;
    right: 16px;
    bottom: 16px;
    width: min(620px, calc(100vw - 32px));
    overflow: auto;
    padding: 22px;
    border-radius: 24px;
    box-shadow: 0 24px 80px rgba(15, 23, 42, .28);
}

.utility-dialog {
    position: relative;
    width: min(920px, calc(100vw - 32px));
    max-height: calc(100vh - 32px);
    margin: 16px auto;
    padding: 22px;
    overflow: auto;
    box-shadow: 0 24px 80px rgba(15, 23, 42, .28);
}

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

.drawer-heading h2 {
    font-size: 1.25rem;
}

.drawer-close {
    width: 38px;
    height: 38px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: #fff;
    color: var(--muted);
    font-size: 1.35rem;
    font-weight: 800;
    line-height: 1;
}

.drawer-close:hover {
    border-color: var(--accent);
    color: var(--accent-strong);
}

.modal-description {
    margin: -4px 0 16px;
    color: var(--muted);
    font-size: .95rem;
    line-height: 1.65;
}

.transpose-control-grid--drawer {
    grid-template-columns: 1fr;
}

.control-panel--drawer .key-card-body {
    grid-template-columns: 1fr;
}

.control-panel--drawer .control-footer-actions {
    display: grid;
    gap: 14px;
}

.control-panel--drawer .action-group {
    justify-content: flex-end;
}

.manual-save-grid--modal,
.category-manager-grid--modal {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 14px;
}

.manual-save-grid--modal .manual-save-actions,
.category-manager-grid--modal .category-manager-actions {
    grid-column: 1 / -1;
    display: flex;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: 10px;
}

.import-row {
    align-items: end;
}

body.has-open-panel {
    overflow: hidden;
}

@media (max-width: 980px) {
    .workspace-topbar {
        align-items: flex-start;
        flex-direction: column;
    }

    .workspace-actions {
        justify-content: flex-start;
    }

    .app-workspace {
        grid-template-columns: 1fr;
    }

    .workspace-sidebar {
        position: static;
        max-height: none;
    }

    #savedSheetSelect[size] {
        min-height: 220px;
    }

    .sheet-reader-panel--workspace {
        min-height: auto;
    }

    .sheet-reader-panel--workspace .sheet-viewer {
        min-height: 62vh;
    }
}

@media (max-width: 700px) {
    .app-shell--workspace {
        padding: 10px;
    }

    .workspace-topbar,
    .library-panel--sidebar,
    .sheet-reader-panel--workspace,
    .utility-dialog,
    .control-panel--drawer {
        border-radius: 18px;
    }

    .workspace-actions .button,
    .workspace-actions .logout-link {
        flex: 1 1 calc(50% - 8px);
        justify-content: center;
        text-align: center;
    }

    .control-panel--drawer {
        inset: auto 0 0 0;
        width: 100%;
        max-height: min(86vh, 760px);
        border-radius: 22px 22px 0 0;
    }

    .utility-dialog {
        width: calc(100vw - 16px);
        margin: 8px auto;
        max-height: calc(100vh - 16px);
        padding: 18px;
    }

    .manual-save-grid--modal,
    .category-manager-grid--modal {
        grid-template-columns: 1fr;
    }
}

/* Workspace left-rail correction: keep the library as a real left sidebar on tablet/desktop widths. */
@media (min-width: 761px) {
    .app-container--workspace {
        display: grid;
        grid-template-columns: minmax(260px, 320px) minmax(0, 1fr);
        grid-template-rows: auto minmax(0, 1fr);
        gap: 16px;
        align-items: start;
    }

    .workspace-topbar {
        grid-column: 2;
        grid-row: 1;
        margin-bottom: 0;
    }

    .app-workspace {
        display: contents;
    }

    .workspace-sidebar {
        grid-column: 1;
        grid-row: 1 / span 2;
        position: sticky;
        top: 18px;
        align-self: start;
        max-height: calc(100vh - 36px);
        overflow: auto;
        z-index: 5;
    }

    .workspace-main {
        grid-column: 2;
        grid-row: 2;
        min-width: 0;
    }
}

@media (max-width: 760px) {
    .app-container--workspace {
        display: block;
    }

    .app-workspace {
        display: grid;
        grid-template-columns: 1fr;
        gap: 16px;
    }
}


/* Workspace inline transpose panel */
.workspace-main {
    display: grid;
    gap: 16px;
    min-width: 0;
}

.control-panel--workspace {
    padding: 18px;
}

.control-panel--workspace .drawer-heading {
    margin-bottom: 14px;
}

.transpose-control-grid--workspace {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 14px;
}

.control-panel--workspace .key-card {
    padding: 15px;
}

.control-panel--workspace .key-card-body,
.control-panel--workspace .key-card-source .key-card-body {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

.control-panel--workspace .key-card-output .key-card-body {
    grid-template-columns: repeat(3, minmax(0, 1fr));
}

.control-panel--workspace .control-footer-actions {
    display: flex;
    justify-content: space-between;
    gap: 14px;
    margin-top: 14px;
}

.control-panel--workspace .action-group {
    flex: 0 0 auto;
}

@media (max-width: 1220px) {
    .transpose-control-grid--workspace {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 860px) {
    .control-panel--workspace .key-card-body,
    .control-panel--workspace .key-card-source .key-card-body,
    .control-panel--workspace .key-card-output .key-card-body {
        grid-template-columns: 1fr;
    }

    .control-panel--workspace .control-footer-actions {
        align-items: stretch;
        flex-direction: column;
    }

    .control-panel--workspace .action-group {
        justify-content: stretch;
    }

    .control-panel--workspace .action-group .button {
        flex: 1 1 0;
    }
}

/* Collapsible library sidebar */
.sidebar-toggle-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-width: 40px;
    min-height: 40px;
    border: 1px solid var(--line-strong);
    border-radius: 12px;
    background: rgba(255, 255, 255, .92);
    color: #6b7280;
    cursor: pointer;
    transition: border-color .16s ease, background .16s ease, color .16s ease, box-shadow .16s ease, transform .16s ease;
}

.sidebar-toggle-button:hover {
    border-color: rgba(31, 95, 190, .48);
    background: #f8fbff;
    color: var(--accent-strong);
    box-shadow: 0 8px 18px rgba(22, 44, 78, .08);
}

.sidebar-toggle-button:focus-visible {
    outline: none;
    box-shadow: 0 0 0 4px rgba(31, 100, 200, .14);
}

.sidebar-toggle-icon {
    flex: 0 0 auto;
}

.sidebar-toggle-label {
    font-size: .86rem;
    font-weight: 900;
    white-space: nowrap;
}

body.sidebar-collapsed .sidebar-toggle-button {
    border-color: rgba(31, 95, 190, .32);
    color: var(--accent-strong);
    background: rgba(31, 95, 190, .08);
}

@media (min-width: 761px) {
    .workspace-sidebar {
        position: sticky;
        top: 18px;
        align-self: start;
        max-height: none;
        min-height: 48px;
        overflow: visible;
    }

    .library-panel--sidebar {
        max-height: calc(100vh - 36px);
        overflow: auto;
        padding-top: 18px;
    }

    .library-heading--compact {
        padding-right: 48px;
    }

    .sidebar-toggle-button {
        position: absolute;
        top: 12px;
        right: 12px;
        z-index: 20;
        width: 40px;
        height: 40px;
        padding: 0;
    }

    .sidebar-toggle-label {
        position: absolute;
        width: 1px;
        height: 1px;
        padding: 0;
        margin: -1px;
        overflow: hidden;
        clip: rect(0, 0, 0, 0);
        white-space: nowrap;
        border: 0;
    }

    body.sidebar-collapsed .app-container--workspace {
        grid-template-columns: 52px minmax(0, 1fr);
        column-gap: 12px;
    }

    body.sidebar-collapsed .workspace-sidebar {
        visibility: visible;
        opacity: 1;
        pointer-events: auto;
        transform: none;
        overflow: visible;
    }

    body.sidebar-collapsed .library-panel--sidebar {
        display: none;
    }

    body.sidebar-collapsed .sidebar-toggle-button {
        position: sticky;
        top: 18px;
        right: auto;
        margin: 0 auto;
        transform: none;
    }
}

@media (max-width: 760px) {
    .workspace-sidebar {
        display: flex;
        flex-direction: column;
        gap: 10px;
    }

    .library-panel--sidebar {
        order: 1;
    }

    .sidebar-toggle-button {
        order: 2;
        width: 100%;
        min-height: 44px;
        padding: 0 14px;
        border-radius: 14px;
        background: #fff;
    }

    body.sidebar-collapsed .workspace-sidebar {
        display: flex;
    }

    body.sidebar-collapsed .library-panel--sidebar {
        display: none;
    }
}

/* 2026-07-09: centered in-panel collapse buttons for library and transpose settings */
.panel-collapse-row {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: -4px 0 10px;
}

.panel-collapse-button,
.library-panel--sidebar .sidebar-toggle-button,
.control-panel--workspace .settings-toggle-button {
    position: static !important;
    inset: auto !important;
    transform: none !important;
    margin: 0 auto;
    width: 40px;
    min-width: 40px;
    height: 40px;
    min-height: 40px;
    padding: 0;
    border: 1px solid var(--line-strong);
    border-radius: 12px;
    background: rgba(255, 255, 255, .94);
    color: #6b7280;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: border-color .16s ease, background .16s ease, color .16s ease, box-shadow .16s ease;
}

.panel-collapse-button:hover,
.library-panel--sidebar .sidebar-toggle-button:hover,
.control-panel--workspace .settings-toggle-button:hover {
    border-color: rgba(31, 95, 190, .48);
    background: #f8fbff;
    color: var(--accent-strong);
    box-shadow: 0 8px 18px rgba(22, 44, 78, .08);
}

.panel-collapse-button:focus-visible,
.library-panel--sidebar .sidebar-toggle-button:focus-visible,
.control-panel--workspace .settings-toggle-button:focus-visible {
    outline: none;
    box-shadow: 0 0 0 4px rgba(31, 100, 200, .14);
}

.sidebar-toggle-label,
.settings-toggle-label {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.library-panel--sidebar .library-heading--compact {
    padding-right: 0;
}

body.sidebar-collapsed .library-panel--sidebar {
    display: block !important;
    visibility: visible;
    opacity: 1;
    pointer-events: auto;
    width: auto;
    min-width: 0;
    padding: 10px 8px;
    overflow: visible;
}

body.sidebar-collapsed .library-content {
    display: none !important;
}

body.sidebar-collapsed .library-collapse-row {
    margin: 0;
}

body.sidebar-collapsed .sidebar-toggle-button,
body.settings-collapsed .settings-toggle-button {
    border-color: rgba(31, 95, 190, .32);
    color: var(--accent-strong);
    background: rgba(31, 95, 190, .08);
}

.control-panel--workspace .settings-collapse-row {
    margin: -4px 0 12px;
}

body.settings-collapsed .settings-content {
    display: none !important;
}

body.settings-collapsed .control-panel--workspace {
    padding: 10px;
}

body.settings-collapsed .settings-collapse-row {
    margin: 0;
}

@media (min-width: 761px) {
    body.sidebar-collapsed .app-container--workspace {
        grid-template-columns: 64px minmax(0, 1fr);
        column-gap: 12px;
    }

    body.sidebar-collapsed .workspace-sidebar {
        max-height: none;
        overflow: visible;
    }

    body.sidebar-collapsed .library-panel--sidebar {
        max-height: none;
    }
}

@media (max-width: 760px) {
    .library-panel--sidebar {
        order: 1;
    }

    .library-collapse-row,
    .settings-collapse-row {
        margin: -2px 0 10px;
    }

    body.sidebar-collapsed .library-panel--sidebar {
        display: block !important;
        padding: 10px;
    }
}

/* 2026-07-09: show panel purpose while collapsed */
.panel-collapsed-title {
    display: none;
    font-size: 13px;
    font-weight: 800;
    letter-spacing: .04em;
    color: var(--ink);
    text-align: center;
    line-height: 1.25;
    white-space: nowrap;
}

body.sidebar-collapsed .library-collapsed-title,
body.settings-collapsed .settings-collapsed-title {
    display: block;
}

body.sidebar-collapsed .library-collapse-row,
body.settings-collapsed .settings-collapse-row {
    flex-direction: column;
    gap: 6px;
}

@media (min-width: 761px) {
    body.sidebar-collapsed .library-panel--sidebar {
        width: 72px;
        min-width: 72px;
    }

    body.sidebar-collapsed .library-collapsed-title {
        writing-mode: vertical-rl;
        letter-spacing: .12em;
        margin-top: 2px;
    }
}

@media (max-width: 760px) {
    .panel-collapsed-title {
        font-size: 14px;
    }

    body.sidebar-collapsed .library-collapse-row,
    body.settings-collapsed .settings-collapse-row {
        flex-direction: row;
        justify-content: center;
        gap: 8px;
    }
}

/* Library account manager */
.account-manager-layout {
    display: grid;
    grid-template-columns: minmax(260px, .85fr) minmax(320px, 1.15fr);
    gap: 18px;
}

.account-list-box,
.account-form-box {
    border: 1px solid var(--line);
    border-radius: 18px;
    background: #fbfdff;
    padding: 16px;
}

.account-list-box h3,
.account-form-box h3 {
    margin: 0 0 12px;
    color: var(--ink);
    font-size: 1.02rem;
}

.account-list {
    display: grid;
    gap: 10px;
}

.account-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: 14px;
    background: #fff;
}

.account-row.is-current {
    border-color: rgba(31, 100, 200, .45);
    background: var(--accent-soft);
}

.account-row-main {
    min-width: 0;
}

.account-row-main h3 {
    margin: 0 0 4px;
    font-size: .98rem;
}

.account-row-main p,
.account-empty {
    margin: 0;
    color: var(--muted);
    font-size: .82rem;
    line-height: 1.45;
    word-break: break-word;
}

.account-manager-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 14px;
}

.account-checkbox-field {
    grid-column: 1 / -1;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    color: var(--muted);
    font-size: .9rem;
    line-height: 1.45;
}

.account-manager-actions {
    grid-column: 1 / -1;
    display: flex;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: 10px;
}

#accountUsernameInput[readonly],
#accountLibraryIdInput[readonly] {
    color: var(--muted);
    background: #f2f5f9;
}

@media (max-width: 900px) {
    .account-manager-layout {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 700px) {
    .account-manager-grid {
        grid-template-columns: 1fr;
    }

    .account-row {
        align-items: stretch;
        flex-direction: column;
    }

    .account-row .button {
        justify-content: center;
        width: 100%;
    }
}
