/* Which Way Next - Mobile-first styles */

:root {
    --bg: #0f0f12;
    --bg-card: #1a1a1f;
    --bg-input: #25252d;
    --text: #e8e6e3;
    --text-muted: #9a9a9a;
    --accent: #c9a227;
    --accent-hover: #e0b83d;
    --border: #2d2d35;
    --error: #c75c5c;
    --success: #5c9e6b;
    --radius: 8px;
    --shadow: 0 4px 20px rgba(0,0,0,0.3);
    --font-sans: 'Segoe UI', system-ui, -apple-system, sans-serif;
    --font-display: 'Georgia', 'Times New Roman', serif;
}

*,
*::before,
*::after {
    box-sizing: border-box;
}

html {
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
}

body {
    margin: 0;
    font-family: var(--font-sans);
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

.main {
    flex: 1;
    width: 100%;
    max-width: 720px;
    margin: 0 auto;
    padding: 1rem 1.25rem 2rem;
}

/* Header */
.site-header {
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 100;
}

.header-inner {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0.75rem 1.25rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
}

.logo {
    font-weight: 700;
    font-size: 1.25rem;
    color: var(--accent);
    text-decoration: none;
    font-family: var(--font-display);
}

.logo:hover {
    color: var(--accent-hover);
}

.nav-toggle {
    display: none;
    width: 2rem;
    height: 2rem;
    padding: 0;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text);
    cursor: pointer;
    position: relative;
}

.nav-toggle::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 1rem;
    height: 2px;
    background: currentColor;
    box-shadow: 0 -4px 0 currentColor, 0 4px 0 currentColor;
}

.nav-toggle[aria-expanded="true"]::before {
    height: 0;
    box-shadow: none;
}

.nav-toggle[aria-expanded="true"]::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) rotate(45deg);
    width: 1rem;
    height: 2px;
    background: currentColor;
}

.nav-toggle[aria-expanded="true"] {
    font-size: 0;
}

.nav {
    display: flex;
    align-items: center;
    gap: 0.5rem 1rem;
    flex-wrap: wrap;
}

.nav a {
    color: var(--text-muted);
    text-decoration: none;
    padding: 0.35rem 0.5rem;
    border-radius: 4px;
}

.nav a:hover {
    color: var(--accent);
}

.nav-form {
    margin: 0;
}

.btn-link {
    background: none;
    border: none;
    color: var(--text-muted);
    font: inherit;
    cursor: pointer;
    padding: 0.35rem 0.5rem;
    border-radius: 4px;
}

.btn-link:hover {
    color: var(--accent);
}

@media (max-width: 600px) {
    .nav-toggle {
        display: block;
    }

    .nav {
        display: none;
        position: absolute;
        top: 100%;
        left: 0;
        right: 0;
        background: var(--bg-card);
        border-bottom: 1px solid var(--border);
        flex-direction: column;
        align-items: stretch;
        padding: 1rem;
        gap: 0.25rem;
    }

    .nav.is-open {
        display: flex;
    }

    .nav-form {
        padding-top: 0.5rem;
        border-top: 1px solid var(--border);
    }
}

/* Buttons & forms */
.btn {
    display: inline-block;
    padding: 0.6rem 1.25rem;
    font: inherit;
    font-size: 0.95rem;
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    text-decoration: none;
    text-align: center;
    transition: background 0.2s, color 0.2s;
    min-height: 44px;
    line-height: 1.3;
    -webkit-tap-highlight-color: transparent;
}

.btn-primary {
    background: var(--accent);
    color: var(--bg);
}

.btn-primary:hover {
    background: var(--accent-hover);
}

.btn-secondary {
    background: var(--bg-input);
    color: var(--text);
    border: 1px solid var(--border);
}

.btn-secondary:hover {
    background: var(--border);
}

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

.btn-danger:hover {
    opacity: 0.9;
}

input[type="text"],
input[type="email"],
input[type="password"],
textarea {
    width: 100%;
    padding: 0.6rem 0.75rem;
    font: inherit;
    font-size: 1rem;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text);
}

input::placeholder,
textarea::placeholder {
    color: var(--text-muted);
}

textarea {
    min-height: 120px;
    resize: vertical;
}

.form-group {
    margin-bottom: 1rem;
}

.form-group label {
    display: block;
    margin-bottom: 0.35rem;
    color: var(--text-muted);
    font-size: 0.9rem;
}

.help-text {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-top: 0.25rem;
}

.alert {
    padding: 0.75rem 1rem;
    border-radius: var(--radius);
    margin-bottom: 1rem;
}

.flash-message {
    margin-top: 0;
}

.alert-error {
    background: rgba(199, 92, 92, 0.15);
    border: 1px solid var(--error);
    color: #e8a0a0;
}

.alert-success {
    background: rgba(92, 158, 107, 0.15);
    border: 1px solid var(--success);
    color: #a8d4b0;
}

.alert-notice {
    background: rgba(201, 162, 39, 0.12);
    border: 1px solid var(--accent);
    color: var(--text);
}

/* Cards & book list */
.card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem;
    margin-bottom: 1rem;
    box-shadow: var(--shadow);
}

.card h2,
.card h3 {
    margin-top: 0;
    font-family: var(--font-display);
}

.about-benefits {
    margin: 1rem 0;
    padding-left: 1.5rem;
}

.about-benefits li {
    margin-bottom: 0.75rem;
}

.about-benefits li:last-child {
    margin-bottom: 0;
}

.book-list {
    list-style: none;
    padding: 0;
    margin: 0;
}

.book-list li {
    border-bottom: 1px solid var(--border);
    padding: 1rem 0;
}

.book-list li:last-child {
    border-bottom: none;
}

.book-list a {
    color: var(--text);
    text-decoration: none;
    font-weight: 500;
}

.book-list a:hover {
    color: var(--accent);
}

.book-list .meta {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-top: 0.25rem;
}

/* Book grid with cover / placeholder */
.book-grid {
    display: grid;
    gap: 1.5rem;
}

.book-list-item {
    display: grid;
    grid-template-columns: 100px 1fr;
    gap: 1rem;
    align-items: start;
    border-bottom: 1px solid var(--border);
    padding-bottom: 1rem;
}

.book-list-item:last-child {
    border-bottom: none;
}

.book-cover-link {
    display: block;
    text-decoration: none;
    border-radius: var(--radius);
    overflow: hidden;
    background: var(--bg-input);
}

.book-cover-img {
    width: 100%;
    height: auto;
    aspect-ratio: 2/3;
    object-fit: cover;
    display: block;
}

.book-cover-placeholder {
    aspect-ratio: 2/3;
    min-height: 120px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0.5rem;
    background: linear-gradient(135deg, var(--border) 0%, var(--bg-input) 100%);
    border: 1px solid var(--border);
}

.book-cover-placeholder-text {
    font-size: 0.7rem;
    font-weight: 600;
    text-align: center;
    color: var(--text-muted);
    line-height: 1.2;
    display: -webkit-box;
    -webkit-line-clamp: 6;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.book-title-link {
    color: var(--text);
    text-decoration: none;
    font-weight: 500;
}

.book-title-link:hover {
    color: var(--accent);
}

.book-list-info {
    min-width: 0;
}

/* Book detail page (cover, info, description, Start reading) */
.book-detail {
    max-width: 720px;
}

.book-detail-nav {
    margin-bottom: 1.25rem;
    font-size: 0.9rem;
}

.book-detail-nav a {
    color: var(--text-muted);
    text-decoration: none;
}

.book-detail-nav a:hover {
    color: var(--accent);
}

.book-detail-layout {
    display: grid;
    gap: 1.5rem;
    align-items: start;
}

.book-detail-cover {
    max-width: 280px;
}

.book-detail-cover-img {
    width: 100%;
    height: auto;
    display: block;
    border-radius: var(--radius);
    border: 1px solid var(--border);
}

.book-detail-cover-placeholder {
    aspect-ratio: 2/3;
    min-height: 200px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    background: linear-gradient(135deg, var(--border) 0%, var(--bg-input) 100%);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.book-detail-cover-placeholder-text {
    font-size: 0.9rem;
    font-weight: 600;
    text-align: center;
    color: var(--text-muted);
    line-height: 1.3;
}

.book-detail-title {
    margin: 0 0 0.5rem;
    font-family: var(--font-display);
    font-size: 1.75rem;
    line-height: 1.3;
}

.book-detail-meta,
.book-detail-stars {
    margin: 0 0 0.35rem;
    font-size: 0.95rem;
    color: var(--text-muted);
}

.book-detail-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem;
    margin: 0.75rem 0 1rem;
}

.book-detail-description {
    margin: 0 0 1.25rem;
    font-size: 1rem;
    line-height: 1.6;
    color: var(--text);
}

.book-detail-description:empty {
    display: none;
}

.book-detail-start {
    display: inline-block;
}

@media (min-width: 520px) {
    .book-detail-layout {
        grid-template-columns: 200px 1fr;
    }

    .book-detail-cover {
        max-width: 100%;
    }
}

@media (min-width: 640px) {
    .book-detail-layout {
        grid-template-columns: 240px 1fr;
        gap: 2rem;
    }
}

/* Book description on home page (optional expand) */
.book-description {
    margin: 0.5rem 0 0;
    font-size: 0.9rem;
    color: var(--text-muted);
}

.book-description-text {
    display: block;
}

.book-description--collapsed .book-description-text {
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.book-description-toggle {
    display: inline-block;
    margin-top: 0.25rem;
    padding: 0;
    background: none;
    border: none;
    font: inherit;
    font-size: 0.85rem;
    color: var(--accent);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

.book-description-toggle:hover {
    color: var(--accent-hover);
    text-decoration: underline;
}

.book-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem;
}

.tag-pill {
    display: inline-block;
    padding: 0.2rem 0.5rem;
    font-size: 0.75rem;
    color: var(--accent);
    background: rgba(201, 162, 39, 0.15);
    border-radius: 999px;
    text-decoration: none;
}

.tag-pill:hover {
    background: rgba(201, 162, 39, 0.25);
    color: var(--accent-hover);
}

@media (min-width: 500px) {
    .book-list-item {
        grid-template-columns: 110px 1fr;
    }
}

/* Reader: chapter view */
.chapter-title {
    font-family: var(--font-display);
    font-size: 1.5rem;
    margin-bottom: 1rem;
    line-height: 1.4;
}

.chapter-content {
    margin-bottom: 2rem;
    font-size: 1.05rem;
    line-height: 1.6;
}

.chapter-content p {
    margin: 0.5em 0;
}

.chapter-content p:first-child {
    margin-top: 0;
}

.chapter-content p:last-child {
    margin-bottom: 0;
}

.chapter-content ul,
.chapter-content ol {
    margin: 0.5em 0;
    padding-left: 1.5em;
}

.chapter-content h1, .chapter-content h2, .chapter-content h3 {
    margin: 0.75em 0 0.35em;
    font-family: var(--font-display);
}

.chapter-content blockquote {
    margin: 0.5em 0;
    padding-left: 1em;
    border-left: 3px solid var(--border);
    color: var(--text-muted, #666);
}

/* Plain-text chapter content (no HTML tags) keeps line breaks */
.chapter-content:not(:has(p)):not(:has(ul)):not(:has(ol)):not(:has(blockquote)) {
    white-space: pre-wrap;
}

.chapter-editor-wrap {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg-card);
}

.chapter-editor-wrap .ql-toolbar {
    border: none;
    border-bottom: 1px solid var(--border);
    padding: 0.5rem;
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem;
}

.chapter-editor-wrap .ql-toolbar .ql-formats {
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem;
    margin-right: 0.5rem;
}

.chapter-editor-wrap .ql-toolbar button,
.chapter-editor-wrap .ql-toolbar .ql-picker {
    width: 2rem;
    height: 2rem;
    min-width: 2rem;
    padding: 0.25rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius);
    transition: background-color 0.2s;
}

.chapter-editor-wrap .ql-toolbar button:hover,
.chapter-editor-wrap .ql-toolbar button.ql-active,
.chapter-editor-wrap .ql-toolbar .ql-picker:hover,
.chapter-editor-wrap .ql-toolbar .ql-picker.ql-expanded {
    background-color: var(--bg-input);
}

.chapter-editor-wrap .ql-toolbar .ql-picker-label {
    padding: 0.25rem 0.5rem;
    min-height: 2rem;
    display: inline-flex;
    align-items: center;
}

.chapter-editor-wrap .ql-container {
    border: none;
    font-size: 1rem;
    min-height: 240px;
}

.chapter-editor-wrap .ql-editor {
    min-height: 220px;
    padding: 1rem;
    font-size: 1rem;
    line-height: 1.6;
}

.chapter-editor-wrap .ql-editor:focus {
    outline: none;
}

/* Mobile optimizations */
@media (max-width: 600px) {
    .chapter-editor-wrap .ql-toolbar {
        padding: 0.5rem 0.25rem;
        gap: 0.25rem;
    }

    .chapter-editor-wrap .ql-toolbar .ql-formats {
        margin-right: 0.25rem;
        gap: 0.25rem;
    }

    .chapter-editor-wrap .ql-toolbar button,
    .chapter-editor-wrap .ql-toolbar .ql-picker {
        width: 2.5rem;
        height: 2.5rem;
        min-width: 2.5rem;
        padding: 0.375rem;
        /* Ensure tap targets are at least 44x44px for accessibility */
        touch-action: manipulation;
    }

    .chapter-editor-wrap .ql-toolbar button svg,
    .chapter-editor-wrap .ql-toolbar .ql-picker svg {
        width: 1.25rem;
        height: 1.25rem;
    }

    .chapter-editor-wrap .ql-toolbar .ql-picker-label {
        padding: 0.375rem 0.5rem;
        min-height: 2.5rem;
        font-size: 0.9rem;
    }

    .chapter-editor-wrap .ql-container {
        font-size: 16px; /* Prevent zoom on iOS when focusing input */
        min-height: 200px;
    }

    .chapter-editor-wrap .ql-editor {
        min-height: 180px;
        padding: 0.75rem;
        font-size: 16px; /* Prevent zoom on iOS */
        line-height: 1.6;
    }

    /* Ensure editor content is readable and scrollable on mobile */
    .chapter-editor-wrap .ql-editor p,
    .chapter-editor-wrap .ql-editor ul,
    .chapter-editor-wrap .ql-editor ol {
        margin: 0.5em 0;
    }

    /* Prevent text selection issues on mobile */
    .chapter-editor-wrap .ql-editor {
        -webkit-user-select: text;
        user-select: text;
        -webkit-tap-highlight-color: transparent;
    }
}

/* Very small screens */
@media (max-width: 400px) {
    .chapter-editor-wrap .ql-toolbar {
        padding: 0.375rem 0.125rem;
    }

    .chapter-editor-wrap .ql-toolbar button,
    .chapter-editor-wrap .ql-toolbar .ql-picker {
        width: 2.25rem;
        height: 2.25rem;
        min-width: 2.25rem;
    }

    .chapter-editor-wrap .ql-toolbar button svg,
    .chapter-editor-wrap .ql-toolbar .ql-picker svg {
        width: 1.1rem;
        height: 1.1rem;
    }

    .chapter-editor-wrap .ql-editor {
        padding: 0.625rem;
    }
}

.chapter-image {
    width: 100%;
    max-width: 100%;
    height: auto;
    border-radius: var(--radius);
    margin-bottom: 1rem;
    display: block;
}

.choices {
    list-style: none;
    padding: 0;
    margin: 0;
}

.choices li {
    margin-bottom: 0.75rem;
}

.choices a {
    display: block;
    padding: 0.85rem 1rem;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--accent);
    text-decoration: none;
    transition: background 0.2s, border-color 0.2s;
    min-height: 44px;
    -webkit-tap-highlight-color: transparent;
}

.choices a:hover {
    background: var(--border);
    border-color: var(--accent);
}

.ending-notice {
    margin-top: 1.5rem;
    padding: 1rem;
    background: rgba(201, 162, 39, 0.1);
    border: 1px solid var(--accent);
    border-radius: var(--radius);
    text-align: center;
}

.ending-notice a {
    color: var(--accent);
}

/* Author: dashboard & forms */
.author-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-top: 1rem;
}

.author-actions .btn {
    font-size: 0.9rem;
}

.page-header {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.page-header h1 {
    margin: 0;
    font-family: var(--font-display);
    font-size: 1.75rem;
}

/* Filter form (e.g. all-books admin) */
.filter-form .form-group.filter-form-inner {
    margin-bottom: 0;
}

.filter-form-row {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    align-items: flex-end;
}

.filter-form-row .filter-input {
    flex: 1;
    min-width: 12rem;
}

@media (max-width: 400px) {
    .filter-form-row {
        flex-direction: column;
        align-items: stretch;
    }

    .filter-form-row .filter-input {
        min-width: 0;
    }
}

/* Chapter graph */
.graph-container {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem;
    margin: 1rem 0;
    min-height: 320px;
    height: 60vmin;
    max-height: 560px;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
}

.graph-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    margin-bottom: 1rem;
    font-size: 0.9rem;
    color: var(--text-muted);
}

.graph-legend span {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
}

.graph-legend .dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
}

.graph-legend .dot-start {
    background: var(--success);
}

.graph-legend .dot-ending {
    background: var(--accent);
}

.graph-legend .dot-chapter {
    background: var(--text-muted);
}

/* Account */
.settings-section {
    margin-bottom: 2rem;
}

.settings-section h2 {
    font-size: 1.25rem;
    margin-bottom: 1rem;
    font-family: var(--font-display);
}

/* Footer */
.site-footer {
    background: var(--bg-card);
    border-top: 1px solid var(--border);
    margin-top: auto;
}

.footer-inner {
    max-width: 1200px;
    margin: 0 auto;
    padding: 1rem 1.25rem;
    text-align: center;
    font-size: 0.9rem;
    color: var(--text-muted);
}

/* Badges */
.badge {
    display: inline-block;
    padding: 0.2rem 0.5rem;
    font-size: 0.75rem;
    border-radius: 4px;
    margin-left: 0.5rem;
}

.badge-hidden {
    background: rgba(199, 92, 92, 0.3);
    color: #e8a0a0;
}

.badge-draft {
    background: rgba(154, 154, 154, 0.4);
    color: var(--text-muted);
}

/* Flag section (reader) */
.flag-section {
    margin-top: 1.5rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border);
}

.flag-details {
    font-size: 0.95rem;
}

.flag-details summary {
    cursor: pointer;
    color: var(--text-muted);
}

.flag-details .flag-form {
    margin-top: 0.75rem;
}

/* Flags list (author/admin) */
.flags-list {
    list-style: none;
    padding: 0;
    margin: 0;
}

.flag-item {
    padding: 0.75rem 0;
    border-bottom: 1px solid var(--border);
}

.flag-item:last-child {
    border-bottom: none;
}

.flag-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 0.5rem;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.flag-chapter {
    margin-bottom: 0.5rem;
    font-size: 0.95rem;
}

.flag-meta {
    font-size: 0.85rem;
    color: var(--text-muted);
    white-space: nowrap;
}

.flag-desc {
    margin: 0.35rem 0 0.5rem;
}

.flag-item-actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem;
    margin-top: 0.5rem;
}

.flag-delete-form {
    display: inline;
    margin: 0;
}

.btn-sm {
    padding: 0.35rem 0.75rem;
    font-size: 0.85rem;
    min-height: 0;
}

/* Admin: users table (desktop) */
.card-table-wrap .users-table {
    width: 100%;
    border-collapse: collapse;
}

.card-table-wrap .users-table th,
.card-table-wrap .users-table td {
    text-align: left;
    padding: 0.5rem;
    border-bottom: 1px solid var(--border);
}

.card-table-wrap .users-table tbody tr:last-child td {
    border-bottom: none;
}

/* Admin: users table – mobile card layout */
@media (max-width: 600px) {
    .card-table-wrap .users-table thead {
        display: none;
    }

    .card-table-wrap .users-table tbody tr {
        display: block;
        border: 1px solid var(--border);
        border-radius: var(--radius);
        padding: 1rem;
        margin-bottom: 0.75rem;
        background: var(--bg);
    }

    .card-table-wrap .users-table tbody tr:last-child {
        margin-bottom: 0;
    }

    .card-table-wrap .users-table tbody td {
        display: block;
        padding: 0.4rem 0;
        border: none;
    }

    .card-table-wrap .users-table tbody td[data-label]::before {
        content: attr(data-label);
        font-weight: 600;
        font-size: 0.8rem;
        color: var(--text-muted);
        display: block;
        margin-bottom: 0.15rem;
    }

    .card-table-wrap .users-table tbody td[data-label=""]::before {
        content: none;
    }

    .card-table-wrap .users-table tbody td:last-child {
        padding-top: 0.75rem;
        margin-top: 0.5rem;
        border-top: 1px solid var(--border);
    }
}

/* Utilities */
.text-muted {
    color: var(--text-muted);
}

.mt-1 { margin-top: 0.5rem; }
.mt-2 { margin-top: 1rem; }
.mb-2 { margin-bottom: 1rem; }
