/* Family reading: bookshelf, sign-in, editor, camera and the shared reader.
   Colors come from theme.css; spacing tokens from styles.css. */

[hidden] {
    display: none !important;
}

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

.hint {
    color: var(--quiet);
}

.back-link {
    margin-bottom: var(--space-2);
}

/* Links that look like the template's buttons. */
.button-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    min-width: 120px;
    min-height: 42px;
    padding: 9px 18px;
    border: 1px solid var(--button);
    border-radius: 6px;
    font-weight: 700;
    color: var(--button-text);
    background: var(--button);
    cursor: pointer;
    -webkit-user-select: none;
    user-select: none;
    touch-action: manipulation;
}

.button-link:hover,
.button-link:focus-visible,
.button-link:focus-within {
    color: var(--button-text);
    background: var(--button-hover);
}

.button-link-secondary,
.button-link-secondary:hover,
.button-link-secondary:focus-visible,
.button-link-secondary:focus-within {
    color: var(--button);
    background: var(--bg);
}

.button-link-secondary:hover,
.button-link-secondary:focus-visible,
.button-link-secondary:focus-within {
    background: color-mix(in srgb, var(--text) 4%, var(--bg));
}

/* A file picker that looks like a button: the real input lies on top,
   invisible, so a tap anywhere on the button opens the picker. */
.file-button {
    position: relative;
    overflow: hidden;
}

.file-button input[type="file"] {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    padding: 0;
    border: 0;
    opacity: 0;
    cursor: pointer;
    font-size: 0;
}

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

button.link-button.danger-link:hover,
button.link-button.danger-link:focus-visible {
    color: var(--danger);
    text-decoration: underline;
}

/* ---- sign-in ------------------------------------------------------------ */

.member-list {
    display: grid;
    gap: var(--space-3);
    max-width: 420px;
    margin: var(--space-4) 0;
    padding: 0;
    list-style: none;
}

.member-button {
    display: block;
    padding: 18px 20px;
    border: 1px solid var(--quiet-border);
    border-radius: 10px;
    font-size: 1.4rem;
    font-weight: 700;
    color: inherit;
    background: var(--card-bg);
}

.member-button:hover,
.member-button:focus-visible {
    border-color: var(--link);
    color: inherit;
}

.code-form input[type="password"],
.code-form input[type="text"]:not(.visually-hidden) {
    padding: 12px;
    font-size: 1.3rem;
    letter-spacing: 0.2em;
}

/* ---- bookshelf ---------------------------------------------------------- */

.shelf-head {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-4);
}

.shelf-head h1 {
    margin: 0;
}

.shelf {
    margin: var(--space-5) 0;
}

.book-grid,
.page-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: var(--space-4);
    margin: 0;
    padding: 0;
    list-style: none;
}

.book-card {
    display: grid;
    gap: var(--space-1);
    color: inherit;
}

.book-card:hover,
.book-card:focus-visible {
    color: inherit;
}

.book-card-cover {
    display: block;
    width: 100%;
    aspect-ratio: 3 / 4;
    object-fit: cover;
    border: 1px solid var(--quiet-border);
    border-radius: 6px;
    background: var(--card-bg);
}

.book-card:hover .book-card-cover,
.book-card:focus-visible .book-card-cover {
    outline: 2px solid var(--link);
    outline-offset: 2px;
}

.book-card-blank {
    display: grid;
    place-items: center;
    color: var(--quiet);
    font-size: 0.9rem;
}

.book-card-title {
    font-size: 1.05rem;
    font-weight: 700;
    line-height: 1.25;
}

.book-card-meta {
    color: var(--quiet);
    font-size: 0.9rem;
}

.live-banner {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    margin: var(--space-4) 0;
    padding: var(--space-3) var(--space-4);
    border: 1px solid var(--live-border);
    border-radius: 8px;
    color: inherit;
    background: var(--live-bg);
}

.live-banner:hover,
.live-banner:focus-visible {
    color: inherit;
    border-color: var(--link);
}

.live-banner-action {
    font-weight: 700;
    color: var(--link);
}

/* ---- one book ----------------------------------------------------------- */

.book-detail {
    display: grid;
    grid-template-columns: minmax(0, 320px) minmax(0, 1fr);
    gap: var(--space-5);
    align-items: start;
}

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

.book-detail-actions {
    display: grid;
    gap: var(--space-3);
    align-content: start;
}

.book-detail-actions > p {
    margin: 0;
}

.read-form {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-3);
}

.read-form button {
    min-height: 52px;
    font-size: 1.15rem;
}

.form-actions .ready-form {
    margin: 0;
}

@media (max-width: 720px) {
    .form-actions .ready-form {
        width: 100%;
    }
}

.delete-form {
    margin-top: var(--space-4);
}

/* ---- the Familjen page -------------------------------------------------- */

.member-rows {
    display: grid;
    gap: var(--space-2);
    max-width: 560px;
    margin: var(--space-4) 0;
    padding: 0;
    list-style: none;
}

.member-row {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    justify-content: space-between;
    gap: var(--space-2) var(--space-4);
    padding: var(--space-3) var(--space-4);
    border: 1px solid var(--quiet-border);
    border-radius: 8px;
    color: inherit;
}

.member-row:hover,
.member-row:focus-visible {
    color: inherit;
    border-color: var(--link);
}

.member-row-name {
    font-size: 1.15rem;
    font-weight: 700;
}

.member-row-can {
    color: var(--quiet);
}

/* ---- the page editor ---------------------------------------------------- */

.upload-status {
    min-height: 1.45em;
    margin-top: var(--space-3);
    color: var(--quiet);
}

.upload-status.is-problem {
    color: var(--danger);
}

.pages-heading {
    margin-bottom: var(--space-2);
}

.pages-count {
    color: var(--quiet);
    font-size: 1rem;
    font-weight: 400;
}

.page-grid {
    margin: var(--space-3) 0 var(--space-5);
}

.page-tile {
    display: grid;
    gap: var(--space-2);
    align-content: start;
}

.page-tile.is-busy {
    opacity: 0.5;
}

button.page-tile-image {
    display: block;
    width: 100%;
    min-width: 0;
    min-height: 0;
    aspect-ratio: 3 / 4;
    padding: 0;
    border: 1px solid var(--quiet-border);
    border-radius: 6px;
    background: var(--card-bg);
    overflow: hidden;
}

button.page-tile-image:hover,
button.page-tile-image:focus-visible {
    background: var(--card-bg);
    outline: 2px solid var(--link);
    outline-offset: 2px;
}

.page-tile-image img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: contain;
}

/* Every page's tools are always on screen: small and quiet, louder under
   the pointer. */
.page-tile-tools {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-1);
}

button.tool-button {
    width: auto;
    min-width: 36px;
    min-height: 36px;
    padding: 4px 8px;
    border: 1px solid var(--quiet-border);
    border-radius: 5px;
    font-size: 0.85rem;
    font-weight: 400;
    color: var(--quiet);
    background: var(--bg);
}

button.tool-button:hover,
button.tool-button:focus-visible {
    border-color: var(--text);
    color: var(--text);
    background: var(--bg);
}

button.tool-button.tool-number {
    font-weight: 700;
    color: var(--text);
}

button.tool-button.tool-danger:hover,
button.tool-button.tool-danger:focus-visible {
    border-color: var(--danger);
    color: var(--danger);
}

.title-form {
    margin-top: var(--space-6);
}

.page-preview {
    position: fixed;
    inset: 0;
    z-index: 2000;
    display: grid;
    place-items: center;
    padding: 56px 12px 12px;
    background: rgba(0, 0, 0, 0.92);
}

.page-preview img {
    max-width: 100%;
    max-height: calc(100dvh - 72px);
    object-fit: contain;
}

button.page-preview-close {
    position: absolute;
    top: max(10px, env(safe-area-inset-top));
    right: 12px;
    width: auto;
    color: #111;
    background: #f2f2f2;
}

/* ---- full-screen pages: camera and reader -------------------------------- */

html:has(body.full-page) {
    background: var(--reader-bg);
}

body.full-page {
    min-height: 100dvh;
    margin: 0;
    overflow: hidden;
    overscroll-behavior: none;
    color: var(--reader-text);
    background: var(--reader-bg);
}

body.full-page a {
    color: var(--reader-text);
}

body.full-page button {
    width: auto;
}

body.full-page .button-link {
    color: var(--reader-bg);
    background: var(--reader-text);
    border-color: var(--reader-text);
}

body.full-page .button-link-secondary {
    color: var(--reader-text);
    background: transparent;
    border-color: var(--reader-line);
}

/* ---- camera -------------------------------------------------------------- */

.camera {
    position: fixed;
    inset: 0;
    display: grid;
    grid-template-rows: auto 1fr auto;
    background: #000;
}

.camera-video {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: contain;
    background: #000;
}

.camera-flash {
    position: absolute;
    inset: 0;
    background: #fff;
    opacity: 0;
    pointer-events: none;
}

.camera-flash.is-flashing {
    animation: camera-flash 0.35s ease-out;
}

@keyframes camera-flash {
    from {
        opacity: 0.85;
    }

    to {
        opacity: 0;
    }
}

.camera-top {
    position: relative;
    z-index: 1;
    padding: max(12px, env(safe-area-inset-top)) 16px 12px;
    background: rgba(0, 0, 0, 0.45);
}

.camera-title {
    margin: 0;
    font-weight: 700;
}

.camera-hint {
    margin: 4px 0 0;
    color: var(--reader-muted);
    font-size: 0.95rem;
}

.camera-message {
    position: relative;
    z-index: 1;
    grid-row: 2;
    align-self: center;
    justify-self: center;
    display: grid;
    gap: 12px;
    max-width: 340px;
    margin: 16px;
    padding: 20px;
    border-radius: 12px;
    text-align: center;
    background: rgba(0, 0, 0, 0.75);
}

.camera-message p {
    margin: 0;
}

.camera-bar {
    position: relative;
    z-index: 1;
    grid-row: 3;
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    gap: 12px;
    padding: 12px 16px max(16px, env(safe-area-inset-bottom));
    background: rgba(0, 0, 0, 0.45);
}

.camera-last {
    justify-self: start;
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 48px;
    font-size: 0.95rem;
}

.camera-last img {
    width: 48px;
    height: 64px;
    object-fit: cover;
    border: 1px solid rgba(255, 255, 255, 0.5);
    border-radius: 4px;
}

.shutter-wrap {
    display: grid;
    justify-items: center;
    gap: 6px;
}

body.full-page button.shutter {
    width: 76px;
    height: 76px;
    min-width: 0;
    min-height: 0;
    padding: 0;
    border: 4px solid #000;
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 0 0 4px #fff;
}

body.full-page button.shutter:hover,
body.full-page button.shutter:focus-visible {
    background: #e6e6e6;
}

body.full-page button.shutter:active {
    transform: scale(0.94);
}

body.full-page button.shutter:disabled,
body.full-page button.shutter:disabled:hover {
    background: #fff;
    opacity: 0.4;
}

.shutter-label {
    font-size: 0.95rem;
}

body.full-page .camera-done {
    justify-self: end;
    display: inline-flex;
    align-items: center;
    min-height: 48px;
    padding: 10px 18px;
    border-radius: 8px;
    font-weight: 700;
    color: #111;
    background: #fff;
}

.camera-status {
    position: absolute;
    left: 0;
    right: 0;
    bottom: calc(132px + env(safe-area-inset-bottom));
    z-index: 1;
    margin: 0;
    padding: 0 16px;
    text-align: center;
    color: #fff;
    text-shadow: 0 1px 3px #000;
}

.camera-status.is-problem {
    color: #ffb4ab;
}

/* ---- the shared reader --------------------------------------------------- */

.reader {
    position: fixed;
    inset: 0;
    display: grid;
    grid-template-rows: auto 1fr auto;
    color: var(--reader-text);
    background: var(--reader-bg);
}

.watch-only .reader {
    grid-template-rows: 1fr;
}

.reader-top {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: max(8px, env(safe-area-inset-top)) 12px 8px;
    background: var(--reader-bar-bg);
}

.reader-leave {
    padding: 8px 4px;
    white-space: nowrap;
}

.reader-title {
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
    font-weight: 700;
    text-align: center;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.reader-presence {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    color: var(--reader-muted);
    font-size: 0.95rem;
    white-space: nowrap;
}

.reader-presence::before {
    content: "";
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--reader-muted);
}

.reader-presence:empty::before {
    display: none;
}

.reader-presence.is-here {
    color: var(--reader-text);
}

.reader-presence.is-here::before {
    background: var(--present);
}

.reader-stage {
    position: relative;
    min-height: 0;
    overflow: hidden;
    touch-action: pan-y;
}

.reader-page {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    padding: 6px;
    object-fit: contain;
    opacity: 0;
    transition: transform 0.35s ease, opacity 0.35s ease;
    pointer-events: none;
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
}

.reader-page.is-front {
    opacity: 1;
}

.reader-page.at-right {
    transform: translateX(100%);
}

.reader-page.at-left {
    transform: translateX(-100%);
}

.reader-page.no-anim {
    transition: none;
}

body.full-page button.reader-zone {
    position: absolute;
    top: 0;
    bottom: 0;
    z-index: 1;
    width: 35%;
    min-width: 0;
    min-height: 0;
    padding: 0;
    border: 0;
    border-radius: 0;
    background: transparent;
    opacity: 0;
}

body.full-page button.reader-zone-prev {
    left: 0;
}

body.full-page button.reader-zone-next {
    right: 0;
    width: 65%;
}

.reader-waiting {
    position: absolute;
    inset: 0;
    z-index: 2;
    display: grid;
    place-content: center;
    justify-items: center;
    gap: 16px;
    padding: 24px;
    text-align: center;
    background: var(--reader-bg);
}

.reader-waiting-books {
    margin: 0;
    font-size: 72px;
    line-height: 1;
}

.reader-waiting-text {
    max-width: 28ch;
    margin: 0;
    font-size: 1.4rem;
}

.reader-bottom {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 8px 12px max(10px, env(safe-area-inset-bottom));
    background: var(--reader-bar-bg);
}

body.full-page .reader-bottom button {
    min-width: 0;
    min-height: 48px;
}

body.full-page button.reader-turn {
    gap: 6px;
    min-width: 112px;
    font-size: 1.05rem;
    color: var(--reader-bg);
    background: var(--reader-text);
}

body.full-page button.reader-turn:hover,
body.full-page button.reader-turn:focus-visible {
    background: #d9d9d9;
}

body.full-page button.reader-turn:disabled,
body.full-page button.reader-turn:disabled:hover {
    opacity: 0.35;
    background: var(--reader-text);
}

body.full-page button.reader-count,
body.full-page button.reader-close {
    border: 1px solid var(--reader-line);
    font-weight: 400;
    color: var(--reader-text);
    background: transparent;
}

body.full-page button.reader-count:hover,
body.full-page button.reader-count:focus-visible,
body.full-page button.reader-close:hover,
body.full-page button.reader-close:focus-visible {
    border-color: var(--reader-text);
    background: rgba(255, 255, 255, 0.08);
}

.reader-grid {
    position: fixed;
    inset: 0;
    z-index: 10;
    overflow-y: auto;
    padding: max(12px, env(safe-area-inset-top)) 16px 24px;
    background: rgba(10, 10, 10, 0.97);
}

.reader-grid-head {
    position: sticky;
    top: 0;
    z-index: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
    padding: 6px 0;
    font-size: 1.2rem;
    font-weight: 700;
    background: rgba(10, 10, 10, 0.97);
}

body.full-page .reader-grid-head button {
    color: var(--reader-text);
    background: transparent;
    border-color: var(--reader-line);
}

.reader-grid-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
    gap: 12px;
    margin: 0;
    padding: 0;
    list-style: none;
}

body.full-page .reader-grid-list button {
    display: grid;
    gap: 4px;
    width: 100%;
    min-width: 0;
    padding: 6px;
    border: 2px solid transparent;
    font-weight: 400;
    color: var(--reader-text);
    background: transparent;
}

body.full-page .reader-grid-list button:hover,
body.full-page .reader-grid-list button:focus-visible {
    border-color: var(--reader-line);
}

body.full-page .reader-grid-list button.is-current {
    border-color: var(--present);
}

.reader-grid-list img {
    width: 100%;
    aspect-ratio: 3 / 4;
    object-fit: contain;
    background: #000;
}

body.full-page button.reader-start {
    position: fixed;
    inset: 0;
    z-index: 20;
    width: 100%;
    height: 100%;
    border-radius: 0;
    font-size: 1.6rem;
    color: #fff;
    background: rgba(0, 0, 0, 0.55);
}

body.full-page button.reader-start:hover,
body.full-page button.reader-start:focus-visible {
    background: rgba(0, 0, 0, 0.5);
}

.reader-connection {
    position: fixed;
    top: max(10px, env(safe-area-inset-top));
    left: 50%;
    z-index: 30;
    padding: 4px 12px;
    border-radius: 999px;
    font-size: 0.85rem;
    color: #fff;
    background: rgba(0, 0, 0, 0.7);
    transform: translateX(-50%);
}

@media (max-width: 720px) {
    .book-detail {
        grid-template-columns: 1fr;
    }

    .book-detail-cover {
        width: auto;
        max-width: 100%;
        max-height: 42vh;
    }

    /* On a phone the buttons come first, so "Läs för Arvid" is on screen
       without scrolling; the cover follows. */
    .book-detail-actions {
        order: -1;
    }

    .book-grid,
    .page-grid {
        grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
        gap: var(--space-3);
    }

    .button-link {
        width: 100%;
    }

    .shelf-head .button-link {
        width: auto;
    }

    body.full-page button.reader-turn {
        min-width: 96px;
    }
}

@media (prefers-reduced-motion: reduce) {
    .reader-page {
        transition: opacity 0.2s ease;
    }

    .reader-page.at-right,
    .reader-page.at-left {
        transform: none;
    }
}
