:root {
    color-scheme: light;
    --bg: #f6f7fb;
    --panel: #ffffff;
    --ink: #1d2433;
    --muted: #687084;
    --border: #dfe3ec;
    --primary: #4c3f98;
    --primary-soft: #efedff;
    --success: #157347;
    --success-soft: #e8f6ee;
    --warning: #995d00;
    --warning-soft: #fff4dc;
    --danger: #b42318;
    --danger-soft: #ffedeb;
}

* { box-sizing: border-box; }
body {
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button, input, select, textarea { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }

.topbar {
    display: flex;
    align-items: end;
    justify-content: space-between;
    gap: 24px;
    padding: 22px 28px;
    background: var(--panel);
    border-bottom: 1px solid var(--border);
}
.topbar h1 { margin: 3px 0 0; font-size: 24px; }
.topbar p { margin: 4px 0 0; color: var(--muted); }
.back-link { color: var(--muted); text-decoration: none; font-size: 12px; }
.back-link:hover { color: var(--primary); }
.title-row { display: flex; align-items: center; gap: 10px; }
.topbar-actions { display: flex; align-items: end; gap: 8px; flex-wrap: wrap; }
.topbar-actions label { color: var(--muted); font-size: 11px; }
.topbar-actions input,
.topbar-actions select {
    display: block;
    min-width: 78px;
    margin-top: 3px;
    padding: 7px;
    border: 1px solid var(--border);
    border-radius: 7px;
    background: #fff;
}

.button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 36px;
    padding: 7px 13px;
    border: 1px solid var(--primary);
    border-radius: 7px;
    background: var(--primary);
    color: white;
    cursor: pointer;
    text-decoration: none;
    font-weight: 600;
}
.button:hover { filter: brightness(.96); }
.button:disabled { cursor: wait; opacity: .55; }
.button.secondary { background: white; color: var(--ink); border-color: var(--border); }
.button.danger { background: white; color: var(--danger); border-color: #f0b8b3; }
.button.small { min-height: 30px; padding: 4px 10px; font-size: 12px; }

.notice { min-height: 0; padding: 0 28px; transition: padding .15s; }
.notice.visible { padding: 10px 28px; border-bottom: 1px solid; }
.notice.info { color: var(--primary); background: var(--primary-soft); border-color: #cec8ff; }
.notice.success { color: var(--success); background: var(--success-soft); border-color: #b9e1ca; }
.notice.error { color: var(--danger); background: var(--danger-soft); border-color: #f2c0bb; }

.tabs { display: flex; gap: 4px; padding: 14px 28px 0; overflow-x: auto; }
.tab { padding: 9px 14px; border: 0; border-radius: 8px 8px 0 0; background: transparent; color: var(--muted); cursor: pointer; }
.tab.active { background: var(--panel); color: var(--primary); font-weight: 700; box-shadow: 0 -1px 0 var(--border), 1px 0 0 var(--border), -1px 0 0 var(--border); }

main { padding: 0 28px 32px; }
.workspace { display: grid; grid-template-columns: minmax(240px, 310px) minmax(520px, 1fr); gap: 16px; }
.entity-list-panel, .editor-card {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 18px;
    min-width: 0;
}
.single-panel { max-width: 1000px; margin: 0 auto; }
.section-heading { display: flex; justify-content: space-between; align-items: start; gap: 12px; margin-bottom: 14px; }
.section-heading h2, .section-heading h3 { margin: 0; font-size: 16px; }
.section-heading p { margin: 3px 0 0; }
.muted { color: var(--muted); font-size: 12px; }
.hidden { display: none !important; }
.mono, .code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }

.control { width: 100%; border: 1px solid var(--border); border-radius: 7px; padding: 8px 10px; background: white; color: var(--ink); }
.control:focus { outline: 3px solid rgba(76, 63, 152, .13); border-color: var(--primary); }
textarea.control { resize: vertical; }
.code { font-size: 12px; line-height: 1.55; }
.tall { min-height: 520px; }
.hint { display: block; color: var(--muted); font-size: 11px; margin-top: 3px; }

.entity-list-panel > .control { margin-bottom: 10px; }
.entity-list { display: flex; flex-direction: column; gap: 5px; max-height: 660px; overflow-y: auto; }
.entity-row { width: 100%; text-align: left; border: 1px solid transparent; border-radius: 7px; background: transparent; padding: 9px; cursor: pointer; }
.entity-row:hover { background: var(--bg); }
.entity-row.active { background: var(--primary-soft); border-color: #cec8ff; }
.entity-row strong { display: block; }
.entity-row span { display: flex; justify-content: space-between; gap: 8px; color: var(--muted); font-size: 11px; margin-top: 2px; }
.empty { color: var(--muted); text-align: center; padding: 30px 10px; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-grid label, .book-picker label, #genreForm > label { display: block; font-weight: 600; font-size: 12px; }
.form-grid .control, .book-picker .control, #genreForm > label .control { margin-top: 5px; font-weight: 400; }
.form-grid .full { grid-column: 1 / -1; }
.subsection { margin: 20px 0 0; padding: 14px; border: 1px solid var(--border); border-radius: 9px; }
.subsection legend { padding: 0 6px; color: var(--muted); font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.cover-upload-actions { display: flex; align-items: center; gap: 10px; }
.cover-upload-actions .hint { margin: 0; }
.member-picker { margin-bottom: 14px; }
.member-picker > label { display: block; margin-bottom: 6px; font-size: 12px; font-weight: 650; }
.member-results {
    max-height: 320px;
    margin-top: 7px;
    overflow-y: auto;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: white;
}
.member-results-hint { margin: 0; padding: 16px; color: var(--muted); font-size: 12px; text-align: center; }
.member-result {
    display: grid;
    grid-template-columns: 34px minmax(0, 1fr) auto;
    align-items: center;
    width: 100%;
    gap: 10px;
    padding: 7px 10px;
    border: 0;
    border-bottom: 1px solid var(--border);
    background: white;
    color: var(--ink);
    cursor: pointer;
    text-align: left;
}
.member-result:last-child { border-bottom: 0; }
.member-result:hover, .member-result:focus-visible { background: var(--primary-soft); outline: none; }
.member-result:disabled { cursor: wait; opacity: .55; }
.member-result-label { min-width: 0; }
.member-result-label strong, .member-result-label > span {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.member-result-label > span { color: var(--muted); font-size: 11px; }
.member-result-action { color: var(--primary); font-size: 12px; font-weight: 700; }
.member-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 5px; }
.member-row { display: grid; grid-template-columns: 28px 42px minmax(0, 1fr) auto; gap: 9px; align-items: center; padding: 7px; border: 1px solid var(--border); border-radius: 7px; }
.member-position { color: var(--muted); font: 11px ui-monospace, SFMono-Regular, Menlo, monospace; text-align: center; }
.member-thumbnail {
    display: grid;
    width: 34px;
    aspect-ratio: 2 / 3;
    overflow: hidden;
    place-items: center;
    border-radius: 4px;
    background: #e8e5f5;
    color: var(--primary);
    font-weight: 700;
}
.member-row > .member-thumbnail { width: 42px; }
.member-thumbnail img { width: 100%; height: 100%; object-fit: cover; }
.member-thumbnail.placeholder { box-shadow: inset 0 0 0 1px rgba(76, 63, 152, .1); }
.member-label strong, .member-label span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.member-label span { color: var(--muted); font-size: 11px; }
.member-actions { display: flex; gap: 4px; }
.icon-button { width: 29px; height: 29px; border: 1px solid var(--border); border-radius: 6px; background: white; cursor: pointer; }
.icon-button:hover { background: var(--bg); }
.icon-button.danger { color: var(--danger); }

.layout-editor { display: grid; gap: 16px; }
.layout-group {
    padding: 16px;
    border: 1px solid var(--border);
    border-radius: 9px;
    background: #fbfbfd;
}
.layout-group.compact { max-width: 520px; }
.layout-group.compact > p { margin: 3px 0 10px; }
.layout-group-heading { display: flex; align-items: start; justify-content: space-between; gap: 12px; margin-bottom: 11px; }
.layout-group-heading h3 { margin: 0; font-size: 14px; }
.layout-group-heading p { margin: 3px 0 0; }
.layout-list { display: flex; flex-direction: column; gap: 6px; margin: 0; padding: 0; list-style: none; }
.layout-row {
    display: grid;
    grid-template-columns: 28px minmax(0, 1fr) auto;
    align-items: center;
    gap: 10px;
    padding: 9px;
    border: 1px solid var(--border);
    border-radius: 7px;
    background: white;
}
.layout-row-content { min-width: 0; }
.layout-row-content strong,
.layout-row-content > span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.layout-row-content > span { margin-top: 2px; color: var(--muted); font-size: 11px; }
.layout-name { padding: 5px 7px; font-weight: 650; }
.layout-row-actions { display: flex; gap: 4px; }
.layout-picker {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: end;
    gap: 8px;
    margin-top: 11px;
}
.layout-picker label { grid-column: 1 / -1; color: var(--muted); font-size: 11px; font-weight: 650; }

.book-picker { margin-bottom: 20px; }
.book-picker .hint { margin: 8px 0; }
.book-picker .hint:empty { display: none; }
.book-picker > .button { margin-top: 8px; }
.book-picker .member-result[aria-pressed="true"] { background: var(--primary-soft); }
.selected-book { padding: 12px; margin-bottom: 16px; border-left: 4px solid var(--primary); background: var(--primary-soft); border-radius: 6px; }
.selected-book strong, .selected-book span { display: block; }
.selected-book span { color: var(--muted); font-size: 12px; }
.genre-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 7px; }
.genre-option { display: flex; align-items: center; gap: 7px; padding: 6px 8px; border-radius: 6px; }
.genre-option:hover { background: var(--bg); }

@media (max-width: 850px) {
    .topbar { align-items: start; flex-direction: column; }
    .workspace { grid-template-columns: 1fr; }
    .entity-list { max-height: 250px; }
    .form-grid, .book-picker { grid-template-columns: 1fr; }
    .layout-row { grid-template-columns: 24px minmax(0, 1fr); }
    .layout-row-actions { grid-column: 2; }
    main, .tabs { padding-left: 14px; padding-right: 14px; }
    .topbar { padding: 18px 14px; }
}

/* Native drag handles retain keyboard-accessible arrow controls. */
.drag-grip { cursor: grab; user-select: none; min-width: 2.5rem; }
.drag-grip:active { cursor: grabbing; }
.dragging { opacity: .45; }
.drop-before { box-shadow: 0 -3px 0 #51429a; }
.drop-after { box-shadow: 0 3px 0 #51429a; }
.collection-results { display: flex; flex-wrap: wrap; gap: .5rem; margin-top: .75rem; }
.member-label a { color: inherit; text-decoration: underline; text-underline-offset: .2em; }
#bookCollections .member-row { display: flex; justify-content: space-between; gap: 1rem; }
[hidden] { display: none !important; }


.book-availability-warning {
    display: block;
    width: fit-content;
    margin-top: 5px;
    padding: 3px 7px;
    border-radius: 4px;
    color: var(--warning);
    background: var(--warning-soft);
    font-size: 12px;
    white-space: normal;
}
