/* =========================================================================
 * Audiobook Tracker v3 – Utility-First Design System
 *
 * Mobile-first, vanilla CSS. No framework dependency.
 * Warm, reading-oriented palette with clear visual hierarchy.
 * ========================================================================= */

/* === Design Tokens === */
:root {
    --c-bg: #F5F0EB;
    --c-surface: #FFFFFF;
    --c-surface-alt: #EDE7E0;
    --c-primary: #2C3E50;
    --c-primary-rgb: 44, 62, 80;
    --c-accent: #C0832D;
    --c-accent-hover: #A8701F;
    --c-accent-light: #F4E4C8;
    --c-text: #2C2C2C;
    --c-text-muted: #7A7269;
    --c-text-inv: #FFFFFF;
    --c-border: #D6CFC6;
    --c-border-light: #E8E2DA;
    --c-success: #2D8A56;
    --c-error: #C0392B;
    --c-warning: #E67E22;
    --c-info: #2980B9;

    --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    --font-serif: 'Merriweather', Georgia, serif;
    --text-xs: 0.75rem;
    --text-sm: 0.8125rem;
    --text-base: 0.9375rem;
    --text-lg: 1.125rem;
    --text-xl: 1.375rem;
    --text-2xl: 1.75rem;
    --text-3xl: 2.25rem;
    --lh-tight: 1.25;
    --lh-normal: 1.5;
    --lh-relaxed: 1.75;

    --sp-1: 0.25rem;
    --sp-2: 0.5rem;
    --sp-3: 0.75rem;
    --sp-4: 1rem;
    --sp-5: 1.25rem;
    --sp-6: 1.5rem;
    --sp-8: 2rem;
    --sp-10: 2.5rem;
    --sp-12: 3rem;
    --sp-16: 4rem;

    --r-sm: 4px;
    --r-md: 8px;
    --r-lg: 12px;
    --r-xl: 16px;
    --r-full: 9999px;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.06);
    --shadow-md: 0 2px 8px rgba(0, 0, 0, 0.08);
    --shadow-lg: 0 4px 16px rgba(0, 0, 0, 0.10);
    --shadow-xl: 0 8px 24px rgba(0, 0, 0, 0.12);

    --max-w: 720px;
    --header-h: 56px;
    --footer-h: 56px;
    --ease: cubic-bezier(0.4, 0, 0.2, 1);
    --dur: 200ms;
}

/* === Reset === */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
}

body {
    font-family: var(--font-sans);
    font-size: var(--text-base);
    line-height: var(--lh-normal);
    color: var(--c-text);
    background: var(--c-bg);
    min-height: 100dvh;
    -webkit-font-smoothing: antialiased;
}

a {
    color: var(--c-accent);
    text-decoration: none;
}

a:hover {
    color: var(--c-accent-hover);
}

img {
    max-width: 100%;
    height: auto;
    display: block;
}

button,
input,
select,
textarea {
    font: inherit;
    color: inherit;
    border: none;
    background: none;
    outline: none;
}

ul,
ol {
    list-style: none;
}

/* === Page Layout === */
.page-wrap {
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
}

.page-content {
    flex: 1;
    padding: calc(var(--header-h) + var(--sp-4)) var(--sp-4) calc(var(--footer-h) + var(--sp-4));
    max-width: var(--max-w);
    margin: 0 auto;
    width: 100%;
}

/* === Sticky Header === */
.site-header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--header-h);
    background: var(--c-primary);
    color: var(--c-text-inv);
    display: flex;
    align-items: center;
    padding: 0 var(--sp-4);
    z-index: 100;
    box-shadow: var(--shadow-md);
}

.header-left,
.header-right {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    flex-shrink: 0;
}

.header-centre {
    flex: 1;
    min-width: 0;
    text-align: center;
}

.header-title {
    font-size: var(--text-lg);
    font-weight: 700;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.header-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: var(--r-full);
    color: var(--c-text-inv);
    cursor: pointer;
    position: relative;
    transition: background var(--dur) var(--ease);
}

.header-btn:hover {
    background: rgba(255, 255, 255, 0.12);
}

.header-btn svg {
    width: 20px;
    height: 20px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.header-badge {
    position: absolute;
    top: 2px;
    right: 2px;
    min-width: 16px;
    height: 16px;
    border-radius: var(--r-full);
    background: var(--c-error);
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 4px;
    line-height: 1;
}

/* User menu */
.user-menu {
    position: relative;
}

.user-avatar {
    width: 32px;
    height: 32px;
    border-radius: var(--r-full);
    background: var(--c-accent);
    color: var(--c-text-inv);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: var(--text-sm);
    cursor: pointer;
    transition: box-shadow var(--dur) var(--ease);
}

.user-avatar:hover {
    box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.3);
}

.user-dropdown {
    display: none;
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    min-width: 180px;
    background: var(--c-surface);
    border-radius: var(--r-md);
    box-shadow: var(--shadow-xl);
    overflow: hidden;
    z-index: 200;
    color: var(--c-text);
}

.user-dropdown.open {
    display: block;
}

.user-dropdown-item {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    padding: var(--sp-3) var(--sp-4);
    font-size: var(--text-sm);
    color: var(--c-text);
    cursor: pointer;
    transition: background var(--dur) var(--ease);
}

.user-dropdown-item:hover {
    background: var(--c-surface-alt);
}

.user-dropdown-item svg {
    width: 16px;
    height: 16px;
    stroke: currentColor;
    fill: none;
    stroke-width: 2;
}

.user-dropdown-divider {
    height: 1px;
    background: var(--c-border-light);
}

.user-dropdown-item.danger {
    color: var(--c-error);
}

/* === Sticky Footer === */
.site-footer {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: var(--footer-h);
    background: var(--c-surface);
    border-top: 1px solid var(--c-border);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--sp-3);
    padding: 0 var(--sp-4);
    z-index: 100;
}

.footer-inner {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--sp-3);
    max-width: var(--max-w);
    width: 100%;
}

/* === Buttons === */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--sp-2);
    padding: var(--sp-2) var(--sp-4);
    border-radius: var(--r-md);
    font-size: var(--text-sm);
    font-weight: 600;
    cursor: pointer;
    transition: all var(--dur) var(--ease);
    white-space: nowrap;
    line-height: 1.4;
}

.btn svg {
    width: 18px;
    height: 18px;
    stroke: currentColor;
    fill: none;
    stroke-width: 2;
    flex-shrink: 0;
}

.btn:disabled,
.btn.loading {
    opacity: 0.5;
    pointer-events: none;
}

.btn-primary {
    background: var(--c-primary);
    color: var(--c-text-inv);
}

.btn-primary:hover {
    background: #1d2d3d;
}

.btn-accent {
    background: var(--c-accent);
    color: var(--c-text-inv);
}

.btn-accent:hover {
    background: var(--c-accent-hover);
}

.btn-ghost {
    background: transparent;
    color: var(--c-text);
    border: 1px solid var(--c-border);
}

.btn-ghost:hover {
    background: var(--c-surface-alt);
}

.btn-danger {
    background: var(--c-error);
    color: var(--c-text-inv);
}

.btn-danger:hover {
    background: #a0331f;
}

.btn-icon {
    width: 40px;
    height: 40px;
    padding: 0;
    border-radius: var(--r-full);
}

.btn-sm {
    padding: var(--sp-1) var(--sp-3);
    font-size: var(--text-xs);
}

.btn-lg {
    padding: var(--sp-3) var(--sp-6);
    font-size: var(--text-base);
}

/* Collapse footer buttons to icon-only on small screens */
@media (max-width: 479px) {
    .site-footer .btn-label {
        display: none;
    }

    .site-footer .btn:not(.btn-icon) {
        width: 40px;
        height: 40px;
        padding: 0;
        border-radius: var(--r-full);
    }
}

/* === Cards & Sections === */
.card {
    background: var(--c-surface);
    border-radius: var(--r-lg);
    box-shadow: var(--shadow-sm);
    overflow: hidden;
    transition: box-shadow var(--dur) var(--ease);
}

.card:hover {
    box-shadow: var(--shadow-md);
}

.card-body {
    padding: var(--sp-4);
}

.section {
    background: var(--c-surface);
    border-radius: var(--r-lg);
    box-shadow: var(--shadow-sm);
    margin-bottom: var(--sp-4);
    overflow: hidden;
}

.section-header {
    font-size: var(--text-xs);
    font-weight: 700;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--c-text-muted);
    padding: var(--sp-4) var(--sp-4) var(--sp-2);
}

.section-body {
    padding: 0 var(--sp-4) var(--sp-4);
}

/* === Typography Utilities === */
.text-xs {
    font-size: var(--text-xs);
}

.text-sm {
    font-size: var(--text-sm);
}

.text-base {
    font-size: var(--text-base);
}

.text-lg {
    font-size: var(--text-lg);
}

.text-xl {
    font-size: var(--text-xl);
}

.text-2xl {
    font-size: var(--text-2xl);
}

.font-bold {
    font-weight: 700;
}

.font-semibold {
    font-weight: 600;
}

.font-medium {
    font-weight: 500;
}

.text-muted {
    color: var(--c-text-muted);
}

.text-accent {
    color: var(--c-accent);
}

.text-success {
    color: var(--c-success);
}

.text-error {
    color: var(--c-error);
}

.text-center {
    text-align: center;
}

.text-right {
    text-align: right;
}

.leading-tight {
    line-height: var(--lh-tight);
}

.leading-relaxed {
    line-height: var(--lh-relaxed);
}

.text-full {
    overflow: visible;
    white-space: normal;
    word-break: break-word;
}

/* === Spacing === */
.m-0 {
    margin: 0;
}

.mt-1 {
    margin-top: var(--sp-1);
}

.mt-2 {
    margin-top: var(--sp-2);
}

.mt-3 {
    margin-top: var(--sp-3);
}

.mt-4 {
    margin-top: var(--sp-4);
}

.mt-6 {
    margin-top: var(--sp-6);
}

.mt-8 {
    margin-top: var(--sp-8);
}

.mb-1 {
    margin-bottom: var(--sp-1);
}

.mb-2 {
    margin-bottom: var(--sp-2);
}

.mb-3 {
    margin-bottom: var(--sp-3);
}

.mb-4 {
    margin-bottom: var(--sp-4);
}

.mb-6 {
    margin-bottom: var(--sp-6);
}

.ml-auto {
    margin-left: auto;
}

.mr-auto {
    margin-right: auto;
}

.p-0 {
    padding: 0;
}

.p-2 {
    padding: var(--sp-2);
}

.p-3 {
    padding: var(--sp-3);
}

.p-4 {
    padding: var(--sp-4);
}

.p-6 {
    padding: var(--sp-6);
}

.px-4 {
    padding-left: var(--sp-4);
    padding-right: var(--sp-4);
}

.pt-4 {
    padding-top: var(--sp-4);
}

.py-2 {
    padding-top: var(--sp-2);
    padding-bottom: var(--sp-2);
}

.py-3 {
    padding-top: var(--sp-3);
    padding-bottom: var(--sp-3);
}

/* === Flex === */
.flex {
    display: flex;
}

.flex-col {
    flex-direction: column;
}

.flex-wrap {
    flex-wrap: wrap;
}

.items-center {
    align-items: center;
}

.items-start {
    align-items: flex-start;
}

.justify-center {
    justify-content: center;
}

.justify-between {
    justify-content: space-between;
}

.justify-end {
    justify-content: flex-end;
}

.gap-1 {
    gap: var(--sp-1);
}

.gap-2 {
    gap: var(--sp-2);
}

.gap-3 {
    gap: var(--sp-3);
}

.gap-4 {
    gap: var(--sp-4);
}

.flex-1 {
    flex: 1;
    min-width: 0;
}

.flex-shrink-0 {
    flex-shrink: 0;
}

.grid {
    display: grid;
}

.grid-cols-2 {
    grid-template-columns: repeat(2, 1fr);
}

/* === Display / Border === */
.hidden {
    display: none !important;
}

.block {
    display: block;
}

.relative {
    position: relative;
}

/* Expand a primary link across its positioned container, retaining sibling links. */
.stretched-link,
.stretched-link:hover {
    color: inherit;
}

.stretched-link::after {
    content: '';
    position: absolute;
    inset: 0;
    z-index: 1;
}

.stretched-link:focus-visible::after {
    outline: 2px solid var(--c-primary);
    outline-offset: -3px;
}

.raised-link {
    position: relative;
    z-index: 2;
}

.border {
    border: 1px solid var(--c-border);
}

.border-b {
    border-bottom: 1px solid var(--c-border-light);
}

.rounded {
    border-radius: var(--r-md);
}

.rounded-lg {
    border-radius: var(--r-lg);
}

.rounded-full {
    border-radius: var(--r-full);
}

.view-switch {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    padding: 0.75rem;
}

.nested-group {
    margin: 0.5rem;
    border-radius: 0.75rem;
    background: var(--c-surface-alt);
}

.w-full {
    width: 100%;
}

/* === Chips === */
.chip {
    display: inline-flex;
    align-items: center;
    padding: var(--sp-1) var(--sp-3);
    border-radius: var(--r-full);
    font-size: var(--text-xs);
    font-weight: 600;
    background: var(--c-surface-alt);
    color: var(--c-text);
    white-space: nowrap;
}

.chip-accent {
    background: var(--c-accent-light);
    color: var(--c-accent-hover);
}

.chip-primary {
    background: var(--c-primary);
    color: var(--c-text-inv);
}

.chip-success {
    background: rgba(45, 138, 86, 0.12);
    color: var(--c-success);
}

.chip-error {
    background: rgba(192, 57, 43, 0.12);
    color: var(--c-error);
}

.chip-warning {
    background: rgba(230, 126, 34, 0.12);
    color: var(--c-warning);
}

.chip-outline {
    background: transparent;
    border: 1px solid var(--c-border);
}

/* === Filter Chips === */
.filter-row {
    display: flex;
    gap: var(--sp-2);
    padding: var(--sp-2) 0;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
}

.filter-row::-webkit-scrollbar {
    display: none;
}

.filter-chip {
    display: inline-flex;
    align-items: center;
    padding: var(--sp-2) var(--sp-4);
    border-radius: var(--r-full);
    font-size: var(--text-sm);
    font-weight: 500;
    background: var(--c-surface);
    color: var(--c-text-muted);
    border: 1px solid var(--c-border);
    cursor: pointer;
    transition: all var(--dur) var(--ease);
    white-space: nowrap;
}

.filter-chip:hover {
    border-color: var(--c-accent);
    color: var(--c-accent);
}

.filter-chip.active {
    background: var(--c-primary);
    color: var(--c-text-inv);
    border-color: var(--c-primary);
}

/* === Shelf Cards === */
.shelf-card {
    display: block;
    background: var(--c-surface);
    border-radius: var(--r-lg);
    margin-bottom: var(--sp-5);
    overflow: hidden;
    transition: box-shadow var(--dur) var(--ease);
}

.shelf-card:hover {
    box-shadow: var(--shadow-lg);
}

.shelf-card-head {
    display: flex;
    align-items: flex-start;
    gap: var(--sp-3);
    padding: var(--sp-4);
}

.shelf-card-info {
    flex: 1;
    min-width: 0;
}

.shelf-card-title {
    font-size: var(--text-lg);
    font-weight: 700;
    color: var(--c-text);
    line-height: var(--lh-tight);
    word-break: break-word;
}

.shelf-card-meta {
    font-size: var(--text-sm);
    color: var(--c-text-muted);
    margin-top: var(--sp-1);
}

.shelf-card-desc {
    font-size: var(--text-sm);
    color: var(--c-text-muted);
    margin-top: var(--sp-2);
    line-height: var(--lh-relaxed);
}

.thumb-strip {
    display: flex;
    gap: var(--sp-2);
    padding: 0 var(--sp-4) var(--sp-3);
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
}

.thumb-strip::-webkit-scrollbar {
    display: none;
}

.thumb-item {
    position: relative;
    flex-shrink: 0;
    width: 56px;
    cursor: pointer;
    transition: transform var(--dur) var(--ease);
}

.thumb-item:hover {
    transform: translateY(-2px);
}

.thumb-item img {
    width: 56px;
    height: 80px;
    object-fit: cover;
    border-radius: var(--r-sm);
    box-shadow: var(--shadow-sm);
}

.thumb-placeholder {
    width: 56px;
    height: 80px;
    border-radius: var(--r-sm);
    background: var(--c-surface-alt);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: var(--text-xl);
    color: var(--c-text-muted);
}

.thumb-listened {
    position: absolute;
    bottom: -4px;
    right: -4px;
    width: 18px;
    height: 18px;
    border-radius: var(--r-full);
    background: var(--c-success);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
}

.thumb-listened svg {
    width: 12px;
    height: 12px;
}

.progress-bar {
    height: 6px;
    background: var(--c-border-light);
    border-radius: 3px;
    overflow: hidden;
}

.progress-fill {
    height: 100%;
    background: var(--c-accent);
    border-radius: 3px;
    transition: width 0.5s var(--ease);
}

.progress-row {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    padding: 0 var(--sp-4) var(--sp-4);
}

.progress-text {
    font-size: var(--text-xs);
    font-weight: 600;
    color: var(--c-text-muted);
    flex-shrink: 0;
}

.count-badge {
    display: inline-flex;
    align-items: center;
    gap: var(--sp-1);
    padding: var(--sp-1) var(--sp-2);
    border-radius: var(--r-full);
    background: var(--c-primary);
    color: var(--c-text-inv);
    font-size: var(--text-xs);
    font-weight: 700;
}

.new-badge {
    display: inline-flex;
    padding: 2px var(--sp-2);
    border-radius: var(--r-full);
    background: var(--c-warning);
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.05em;
}

/* === Book Detail Hero === */
.hero {
    display: flex;
    gap: var(--sp-4);
    padding: var(--sp-4);
    background: var(--c-surface);
    border-radius: var(--r-lg);
    box-shadow: var(--shadow-sm);
    margin-bottom: var(--sp-4);
}

.hero-cover {
    flex-shrink: 0;
    width: 120px;
}

.hero-cover img {
    width: 120px;
    height: 170px;
    object-fit: cover;
    border-radius: var(--r-md);
    box-shadow: var(--shadow-md);
    cursor: pointer;
}

.hero-cover-ph {
    width: 120px;
    height: 170px;
    border-radius: var(--r-md);
    background: var(--c-surface-alt);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: var(--text-3xl);
    font-weight: 700;
    color: var(--c-text-muted);
}

.hero-info {
    flex: 1;
    min-width: 0;
}

.hero-title {
    font-size: var(--text-xl);
    font-weight: 700;
    line-height: var(--lh-tight);
    word-break: break-word;
}

.hero-author {
    font-size: var(--text-base);
    color: var(--c-text-muted);
    margin-top: var(--sp-1);
}

.hero-narrator {
    font-size: var(--text-sm);
    color: var(--c-text-muted);
    margin-top: var(--sp-1);
}

.hero-meta {
    font-size: var(--text-sm);
    color: var(--c-text-muted);
    margin-top: var(--sp-2);
    display: flex;
    flex-wrap: wrap;
    gap: var(--sp-2);
}

.hero-series {
    display: inline-flex;
    align-items: center;
    gap: var(--sp-1);
    padding: var(--sp-1) var(--sp-2);
    border-radius: var(--r-sm);
    background: var(--c-accent-light);
    color: var(--c-accent-hover);
    font-size: var(--text-xs);
    font-weight: 600;
    margin-top: var(--sp-2);
    cursor: pointer;
}

.hero-series-badge {
    display: inline-flex;
    align-items: center;
    gap: var(--sp-1);
    padding: var(--sp-1) var(--sp-2);
    border-radius: var(--r-sm);
    background: var(--c-surface-alt);
    color: var(--c-text-muted);
    font-size: var(--text-xs);
    font-weight: 600;
    margin-top: var(--sp-1);
}

/* Sub-series grouping on collection page */
.subseries-header {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    padding: var(--sp-2) var(--sp-3);
    background: var(--c-surface-alt);
    border-bottom: 1px solid var(--c-border-light);
    font-size: var(--text-xs);
    font-weight: 700;
    color: var(--c-primary);
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.subseries-header .subseries-count {
    color: var(--c-text-muted);
    font-weight: 400;
}

.subseries-tag {
    display: inline-block;
    padding: 1px var(--sp-1);
    border-radius: var(--r-sm);
    background: var(--c-accent-light);
    color: var(--c-accent-hover);
    font-size: 0.6875rem;
    font-weight: 600;
    white-space: nowrap;
}

.subseries-order {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 20px;
    height: 20px;
    border-radius: var(--r-full);
    background: var(--c-accent-light);
    color: var(--c-accent-hover);
    font-size: 0.625rem;
    font-weight: 700;
    flex-shrink: 0;
}

.collection-order {
    color: var(--c-text-muted);
    font-size: var(--text-xs);
    font-weight: 600;
    flex-shrink: 0;
    min-width: 28px;
}

/* Stars */
.star-rating,
.star-input {
    display: inline-flex;
    gap: 2px;
    margin-top: var(--sp-2);
}

.star-rating svg,
.star-input svg {
    width: 20px;
    height: 20px;
    cursor: pointer;
}

.star-filled {
    fill: var(--c-accent);
    stroke: var(--c-accent);
}

.star-empty {
    fill: none;
    stroke: var(--c-border);
}

/* === Character Hero === */
.char-hero {
    display: flex;
    flex-direction: column;
    gap: 0;
    background: var(--c-surface);
    border-radius: var(--r-lg);
    box-shadow: var(--shadow-sm);
    margin-bottom: var(--sp-4);
    overflow: hidden;
}

.char-hero .flex-1 {
    padding: var(--sp-4);
}

.char-portrait {
    flex-shrink: 0;
    width: 100%;
    position: relative;
}

.char-portrait img {
    width: 100%;
    height: 280px;
    object-fit: cover;
    object-position: top center;
    border-radius: 0;
    box-shadow: none;
    cursor: pointer;
    position: relative;
    z-index: 1;
    display: block;
}

.char-portrait-ph {
    width: 100%;
    height: 280px;
    border-radius: 0;
    background: var(--c-surface-alt);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: var(--text-2xl);
    font-weight: 700;
    color: var(--c-text-muted);
}

.portrait-upload-label {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.35em;
    cursor: pointer;
    font-size: var(--text-xs);
    color: var(--c-text-muted);
    padding: var(--sp-2) var(--sp-3);
    border: 1px dashed var(--c-border);
    border-radius: var(--r-sm);
    margin-top: var(--sp-2);
    transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.portrait-upload-label svg {
    width: 14px;
    height: 14px;
    flex-shrink: 0;
}

.portrait-upload-label input[type="file"] {
    display: none;
}

.portrait-upload-label:hover {
    color: var(--c-accent);
    border-color: var(--c-accent);
    background: color-mix(in srgb, var(--c-accent) 6%, transparent);
}

/* Stacked-card effect – hidden on mobile (full-width portrait) */
.char-portrait.has-stack::before,
.char-portrait.has-stack::after {
    display: none;
}

@media (min-width: 640px) {
    .char-hero {
        flex-direction: row;
        align-items: flex-start;
        gap: var(--sp-4);
        padding: var(--sp-4);
        overflow: visible;
    }

    .char-hero .flex-1 {
        padding: 0;
    }

    .char-portrait {
        width: 270px;
    }

    .char-portrait img {
        width: 270px;
        height: 350px;
        border-radius: var(--r-md);
        box-shadow: var(--shadow-md);
        object-position: top center;
    }

    .char-portrait-ph {
        width: 270px;
        height: 350px;
        border-radius: var(--r-md);
    }

    /* Stacked-card effect when character has multiple portraits */
    .char-portrait.has-stack::before,
    .char-portrait.has-stack::after {
        display: block;
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        width: 270px;
        height: 350px;
        border-radius: var(--r-md);
        background: var(--c-surface-alt);
        box-shadow: var(--shadow-sm);
    }

    .char-portrait.has-stack::before {
        transform: rotate(-4deg) translate(-3px, 5px);
        z-index: 0;
    }

    .char-portrait.has-stack::after {
        transform: rotate(3deg) translate(3px, 4px);
        z-index: -1;
    }
}

.portrait-count {
    position: absolute;
    top: -6px;
    right: -6px;
    background: var(--c-accent);
    color: #fff;
    border-radius: var(--r-full);
    width: 20px;
    height: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: var(--text-xs);
    font-weight: 700;
    z-index: 2;
    border: 2px solid var(--c-surface);
    pointer-events: none;
}

.char-hero-bio-label {
    text-transform: uppercase;
    letter-spacing: 0.06em;
}

.role-badge {
    display: inline-flex;
    padding: var(--sp-1) var(--sp-2);
    border-radius: var(--r-sm);
    font-size: var(--text-xs);
    font-weight: 700;
    text-transform: capitalize;
}

.role-protagonist {
    background: rgba(41, 128, 185, 0.15);
    color: var(--c-info);
}

.role-antagonist {
    background: rgba(192, 57, 43, 0.15);
    color: var(--c-error);
}

.role-supporting {
    background: rgba(44, 62, 80, 0.12);
    color: var(--c-primary);
}

.role-minor {
    background: var(--c-surface-alt);
    color: var(--c-text-muted);
}

/* Character card grid */
.char-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: var(--sp-3);
}

.char-card {
    display: flex;
    flex-direction: column;
    background: var(--c-surface);
    border-radius: var(--r-lg);
    overflow: hidden;
    box-shadow: var(--shadow-sm);
    transition: transform var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
    cursor: pointer;
}

.char-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
}

.char-card-avatar {
    width: 100%;
    aspect-ratio: 3 / 4;
    object-fit: cover;
    display: block;
}

.char-card-avatar-ph {
    width: 100%;
    aspect-ratio: 3 / 4;
    background: var(--c-surface-alt);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: var(--text-2xl);
    color: var(--c-text-muted);
}

.char-card-body {
    padding: var(--sp-2) var(--sp-3) var(--sp-3);
    display: flex;
    flex-direction: column;
    gap: var(--sp-1);
}

.char-card-name {
    font-weight: 600;
    font-size: var(--text-sm);
    line-height: 1.3;
}

.char-card-bio {
    font-size: var(--text-xs);
    color: var(--c-text-muted);
    line-height: 1.4;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.char-card .role-badge {
    align-self: flex-start;
}

/* Detail rows */
.detail-row {
    display: flex;
    gap: var(--sp-2);
    padding: var(--sp-2) 0;
    border-bottom: 1px solid var(--c-border-light);
    font-size: var(--text-sm);
}

.detail-row:last-child {
    border-bottom: none;
}

.detail-label {
    font-weight: 600;
    color: var(--c-text-muted);
    flex-shrink: 0;
    min-width: 70px;
}

.detail-value {
    flex: 1;
    min-width: 0;
    word-break: break-word;
}

/* Character physical description: heading above content, full-width text */
.detail-row.stacked {
    display: block;
}

.detail-row.stacked .detail-label {
    display: block;
    min-width: auto;
    margin-bottom: var(--sp-1);
}

.detail-row.stacked .detail-value {
    display: block;
    width: 100%;
}

/* Comparable series – stacked layout */
.comparable-item {
    padding: var(--sp-3) 0;
    border-bottom: 1px solid var(--c-border-light);
}

.comparable-item:last-child {
    border-bottom: none;
}

.comparable-item-name {
    font-weight: 600;
    font-size: var(--text-sm);
}

.comparable-item-reason {
    font-size: var(--text-sm);
    color: var(--c-text-muted);
    margin-top: var(--sp-1);
}

/* === Forms === */
.form-group {
    margin-bottom: var(--sp-4);
}

.form-label {
    display: block;
    font-size: var(--text-sm);
    font-weight: 600;
    color: var(--c-text-muted);
    margin-bottom: var(--sp-1);
}

.form-input,
.form-select,
.form-textarea {
    width: 100%;
    padding: var(--sp-3);
    border: 1px solid var(--c-border);
    border-radius: var(--r-md);
    font-size: var(--text-base);
    background: var(--c-surface);
    transition: border-color var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}

.form-input:focus,
.form-select:focus,
.form-textarea:focus {
    border-color: var(--c-accent);
    box-shadow: 0 0 0 3px rgba(192, 131, 45, 0.15);
}

.form-textarea {
    resize: vertical;
    min-height: 80px;
}

/* Toggle */
.toggle-wrap {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    cursor: pointer;
}

.toggle {
    position: relative;
    width: 44px;
    height: 24px;
    flex-shrink: 0;
}

.toggle input {
    display: none;
}

.toggle-track {
    position: absolute;
    inset: 0;
    background: var(--c-border);
    border-radius: 12px;
    transition: background var(--dur) var(--ease);
}

.toggle input:checked+.toggle-track {
    background: var(--c-success);
}

.toggle-thumb {
    position: absolute;
    top: 2px;
    left: 2px;
    width: 20px;
    height: 20px;
    border-radius: var(--r-full);
    background: #fff;
    box-shadow: var(--shadow-sm);
    transition: transform var(--dur) var(--ease);
}

.toggle input:checked~.toggle-thumb {
    transform: translateX(20px);
}

.toggle-label {
    font-size: var(--text-sm);
}

/* === Inline Editing === */
[data-editable] {
    cursor: default;
    border-radius: var(--r-sm);
    transition: background var(--dur) var(--ease);
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
    touch-action: manipulation;
}

[data-editable]:hover {
    background: rgba(0, 0, 0, 0.03);
}

.inline-input,
.inline-textarea {
    width: 100%;
    padding: var(--sp-2);
    border: 2px solid var(--c-accent);
    border-radius: var(--r-sm);
    font: inherit;
    font-size: inherit;
    line-height: inherit;
    color: inherit;
    background: #fff;
    box-shadow: 0 0 0 3px rgba(192, 131, 45, 0.15);
}

.inline-textarea {
    resize: none;
    overflow: hidden;
    min-height: 2em;
    field-sizing: content;
}

/* === Search === */
.search-result {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    padding: var(--sp-3);
    border-bottom: 1px solid var(--c-border-light);
}

.search-result:last-child {
    border-bottom: none;
}

.search-cover {
    width: 48px;
    height: 68px;
    object-fit: cover;
    border-radius: var(--r-sm);
    flex-shrink: 0;
}

.search-cover-ph {
    width: 48px;
    height: 68px;
    border-radius: var(--r-sm);
    background: var(--c-surface-alt);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    color: var(--c-text-muted);
    flex-shrink: 0;
}

.search-info {
    flex: 1;
    min-width: 0;
}

.search-title {
    font-weight: 600;
    font-size: var(--text-sm);
}

.search-meta {
    font-size: var(--text-xs);
    color: var(--c-text-muted);
    margin-top: 2px;
}

/* Source toggle */
.source-toggle {
    display: flex;
    background: var(--c-surface-alt);
    border-radius: var(--r-md);
    padding: 3px;
    margin-bottom: var(--sp-3);
}

.source-btn {
    flex: 1;
    padding: var(--sp-2);
    text-align: center;
    border-radius: 6px;
    font-size: var(--text-sm);
    font-weight: 500;
    cursor: pointer;
    transition: all var(--dur) var(--ease);
    color: var(--c-text-muted);
}

.source-btn.active {
    background: var(--c-surface);
    color: var(--c-text);
    box-shadow: var(--shadow-sm);
}

/* AI Import */
.ai-book-row {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    padding: var(--sp-3) 0;
    border-bottom: 1px solid var(--c-border-light);
}

.ai-book-row:last-child {
    border-bottom: none;
}

.ai-book-order {
    width: 28px;
    height: 28px;
    border-radius: var(--r-full);
    background: var(--c-primary);
    color: var(--c-text-inv);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: var(--text-xs);
    font-weight: 700;
    flex-shrink: 0;
}

.ai-book-cover {
    width: 40px;
    height: 56px;
    object-fit: cover;
    border-radius: var(--r-sm);
    flex-shrink: 0;
}

.ai-book-info {
    flex: 1;
    min-width: 0;
}

.ai-book-title {
    font-weight: 600;
    font-size: var(--text-sm);
}

.ai-book-meta {
    font-size: var(--text-xs);
    color: var(--c-text-muted);
    margin-top: 2px;
}

/* === Modals === */
.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 300;
    justify-content: center;
    align-items: flex-end;
}

.modal-overlay.open {
    display: flex;
}

.modal-panel {
    background: var(--c-surface);
    border-radius: var(--r-xl) var(--r-xl) 0 0;
    width: 100%;
    max-width: var(--max-w);
    max-height: 90dvh;
    overflow-y: auto;
    animation: slideUp 0.3s var(--ease);
}

@keyframes slideUp {
    from {
        transform: translateY(100%);
    }

    to {
        transform: translateY(0);
    }
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--sp-4);
    border-bottom: 1px solid var(--c-border-light);
    position: sticky;
    top: 0;
    background: var(--c-surface);
    z-index: 1;
}

.modal-title {
    font-weight: 700;
    font-size: var(--text-lg);
}

.modal-body {
    padding: var(--sp-4);
}

.modal-close {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--r-full);
    cursor: pointer;
    transition: background var(--dur) var(--ease);
}

.modal-close:hover {
    background: var(--c-surface-alt);
}

.modal-close svg {
    width: 20px;
    height: 20px;
    stroke: var(--c-text);
    fill: none;
    stroke-width: 2;
}

/* === Confirm Dialog === */
.dialog-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 400;
    justify-content: center;
    align-items: center;
    padding: var(--sp-4);
}

.dialog-overlay.open {
    display: flex;
}

.dialog-box {
    background: var(--c-surface);
    border-radius: var(--r-lg);
    padding: var(--sp-6);
    max-width: 360px;
    width: 100%;
    box-shadow: var(--shadow-xl);
    text-align: center;
}

.dialog-text {
    font-size: var(--text-base);
    margin-bottom: var(--sp-6);
}

.dialog-buttons {
    display: flex;
    gap: var(--sp-3);
    justify-content: center;
}

/* === Toast === */
.toast {
    position: fixed;
    top: 0;
    left: 50%;
    transform: translateX(-50%) translateY(-100%);
    padding: var(--sp-3) var(--sp-6);
    border-radius: var(--r-md);
    font-size: var(--text-sm);
    font-weight: 500;
    box-shadow: var(--shadow-lg);
    z-index: 500;
    transition: transform 0.3s var(--ease);
    max-width: calc(100vw - 32px);
    text-align: center;
    pointer-events: none;
}

.toast.show {
    transform: translateX(-50%) translateY(calc(var(--header-h) + var(--sp-4)));
    pointer-events: auto;
}

.toast-error {
    background: var(--c-error);
    color: #fff;
}

.toast-success {
    background: var(--c-success);
    color: #fff;
}

/* Spinner */
.spinner {
    width: 20px;
    height: 20px;
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-top-color: #fff;
    border-radius: var(--r-full);
    animation: spin 0.6s linear infinite;
}

.spinner-dark {
    border-color: rgba(0, 0, 0, 0.1);
    border-top-color: var(--c-primary);
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

/* Empty state */
.empty-state {
    text-align: center;
    padding: var(--sp-12) var(--sp-4);
    color: var(--c-text-muted);
}

.empty-state svg {
    width: 48px;
    height: 48px;
    stroke: var(--c-border);
    margin-bottom: var(--sp-4);
}

.empty-state p {
    margin-top: var(--sp-2);
    font-size: var(--text-sm);
}

/* === Login Page === */
.login-page {
    min-height: 100dvh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--c-bg);
    padding: var(--sp-4);
}

.login-card {
    width: 100%;
    max-width: 400px;
    background: var(--c-surface);
    border-radius: var(--r-xl);
    box-shadow: var(--shadow-lg);
    padding: var(--sp-8);
}

.login-title {
    font-size: var(--text-2xl);
    font-weight: 700;
    text-align: center;
    margin-bottom: var(--sp-2);
}

.login-subtitle {
    text-align: center;
    color: var(--c-text-muted);
    font-size: var(--text-sm);
    margin-bottom: var(--sp-8);
}

.login-error {
    background: rgba(192, 57, 43, 0.1);
    color: var(--c-error);
    padding: var(--sp-3);
    border-radius: var(--r-md);
    font-size: var(--text-sm);
    margin-bottom: var(--sp-4);
    text-align: center;
}

.login-forgot {
    text-align: center;
    margin-top: var(--sp-4);
    font-size: var(--text-sm);
    color: var(--c-text-muted);
}

.login-forgot a {
    color: var(--c-accent);
    text-decoration: none;
}

.login-forgot a:hover {
    text-decoration: underline;
}

/* User table */
.user-table {
    width: 100%;
    border-collapse: collapse;
}

.user-table th {
    text-align: left;
    font-size: var(--text-xs);
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--c-text-muted);
    padding: var(--sp-2) var(--sp-3);
    border-bottom: 2px solid var(--c-border);
}

.user-table td {
    padding: var(--sp-3);
    border-bottom: 1px solid var(--c-border-light);
    font-size: var(--text-sm);
}

/* Lightbox */
.lightbox {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.9);
    z-index: 500;
    align-items: center;
    justify-content: center;
    cursor: zoom-out;
}

.lightbox.open {
    display: flex;
}

.lightbox img {
    max-width: 90vw;
    max-height: 90vh;
    object-fit: contain;
    border-radius: var(--r-md);
}

/* Portrait Gallery Lightbox */
.portrait-gallery {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.96);
    z-index: 600;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--sp-3);
}

.portrait-gallery.open {
    display: flex;
}

.portrait-gallery-close {
    position: absolute;
    top: var(--sp-4);
    right: var(--sp-4);
    background: none;
    border: none;
    color: #fff;
    font-size: 1.75rem;
    line-height: 1;
    cursor: pointer;
    opacity: 0.7;
    padding: var(--sp-1);
}

.portrait-gallery-close:hover {
    opacity: 1;
}

.portrait-gallery-main {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    max-height: 62vh;
    width: 100%;
    justify-content: center;
}

.portrait-gallery-img-wrap {
    position: relative;
}

.portrait-gallery-img-wrap img {
    max-width: 80vw;
    max-height: 62vh;
    object-fit: contain;
    border-radius: var(--r-md);
    display: block;
}

.portrait-gallery-counter {
    position: absolute;
    bottom: var(--sp-2);
    right: var(--sp-2);
    color: #fff;
    font-size: var(--text-xs);
    background: rgba(0, 0, 0, 0.55);
    padding: 2px 8px;
    border-radius: var(--r-sm);
}

.portrait-gallery-nav {
    background: rgba(255, 255, 255, 0.1);
    border: none;
    color: #fff;
    font-size: 2.5rem;
    line-height: 1;
    cursor: pointer;
    padding: var(--sp-2) var(--sp-3);
    border-radius: var(--r-md);
    flex-shrink: 0;
    transition: background 0.15s;
}

.portrait-gallery-nav:hover {
    background: rgba(255, 255, 255, 0.25);
}

.portrait-gallery-nav:disabled {
    opacity: 0.2;
    cursor: default;
}

.portrait-gallery-strip {
    display: flex;
    gap: var(--sp-2);
    padding: 0 var(--sp-3);
    overflow-x: auto;
    max-width: 90vw;
    scrollbar-width: thin;
}

.pgthumb {
    width: 48px;
    height: 62px;
    object-fit: cover;
    border-radius: var(--r-sm);
    cursor: pointer;
    opacity: 0.5;
    border: 2px solid transparent;
    flex-shrink: 0;
    transition: opacity 0.15s;
}

.pgthumb:hover {
    opacity: 0.8;
}

.pgthumb.active {
    opacity: 1;
    border-color: var(--c-accent);
}

.portrait-gallery-toolbar {
    display: flex;
    gap: var(--sp-3);
    flex-wrap: wrap;
    justify-content: center;
    padding: 0 var(--sp-4);
}

/* === Responsive === */
@media (min-width: 640px) {
    :root {
        --header-h: 60px;
        --footer-h: 60px;
    }

    .thumb-item {
        width: 64px;
    }

    .thumb-item img {
        width: 64px;
        height: 92px;
    }

    .thumb-placeholder {
        width: 64px;
        height: 92px;
    }

    .hero-cover {
        width: 140px;
    }

    .hero-cover img {
        width: 140px;
        height: 200px;
    }

    .hero-cover-ph {
        width: 140px;
        height: 200px;
    }

    .hero-title {
        font-size: var(--text-2xl);
    }
}

@media (min-width: 768px) {
    .site-header {
        position: sticky;
    }

    .site-footer {
        position: sticky;
    }

    .page-content {
        padding-top: var(--sp-4);
        padding-bottom: var(--sp-4);
    }

    .modal-overlay {
        align-items: center;
    }

    .modal-panel {
        border-radius: var(--r-xl);
        max-height: 80vh;
    }
}

@media (min-width: 1024px) {
    :root {
        --max-w: 800px;
    }
}

@media print {

    .site-header,
    .site-footer,
    .modal-overlay,
    .dialog-overlay,
    .toast {
        display: none !important;
    }

    .page-content {
        padding: 0;
    }
}
