/*
 * AP Comic Flipbook — Frontend Styles
 * All selectors use the "apcf-" prefix to stay isolated.
 * Zero impact on any existing page/theme styles.
 */

/* ─── Custom properties (scoped to wrapper) ─────────────────────────────── */
.apcf-wrapper {
    --apcf-bg:          #1a1a2e;
    --apcf-scene-bg:    #0f0f1a;
    --apcf-accent:      #e94560;
    --apcf-ctrl-bg:     rgba(15, 15, 26, 0.95);
    --apcf-ctrl-border: rgba(255, 255, 255, 0.08);
    --apcf-btn-bg:      rgba(255, 255, 255, 0.07);
    --apcf-btn-hover:   rgba(233, 69, 96, 0.25);
    --apcf-btn-active:  rgba(233, 69, 96, 0.4);
    --apcf-text:        #e0e0e0;
    --apcf-text-muted:  #888;
    --apcf-shadow-book: 0 30px 80px rgba(0, 0, 0, 0.8), 0 10px 30px rgba(0, 0, 0, 0.5);
    --apcf-radius:      10px;
    --apcf-ctrl-h:      52px;
    --apcf-transition:  0.2s ease;

    /* Typography */
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;

    /* Layout */
    position:   relative;
    width:      100%;
    background: var(--apcf-bg);
    border-radius: var(--apcf-radius);
    overflow:   hidden;
    box-sizing: border-box;
    line-height: 1.4;
    color:      var(--apcf-text);
}

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

/* ─── Loader ─────────────────────────────────────────────────────────────── */
.apcf-loader {
    display:         flex;
    flex-direction:  column;
    align-items:     center;
    justify-content: center;
    gap:             20px;
    min-height:      340px;
    padding:         40px 24px;
    text-align:      center;
}

/* Animated "open book" icon */
.apcf-loader__book {
    position: relative;
    width:    64px;
    height:   48px;
}

.apcf-loader__page {
    position: absolute;
    top:      0;
    width:    30px;
    height:   44px;
    border-radius: 2px;
    background: linear-gradient(160deg, #d4a96a 0%, #c8935a 100%);
}

.apcf-loader__page--left {
    right:           50%;
    transform-origin: right center;
    border-radius:    4px 2px 2px 4px;
    animation:        apcf-page-left 1.4s ease-in-out infinite alternate;
}

.apcf-loader__page--right {
    left:            50%;
    transform-origin: left center;
    border-radius:    2px 4px 4px 2px;
    animation:        apcf-page-right 1.4s ease-in-out infinite alternate;
}

@keyframes apcf-page-left {
    from { transform: rotateY(0deg);   opacity: 1; }
    to   { transform: rotateY(-45deg); opacity: 0.6; }
}

@keyframes apcf-page-right {
    from { transform: rotateY(0deg);  opacity: 1; }
    to   { transform: rotateY(45deg); opacity: 0.6; }
}

.apcf-loader__text {
    margin:    0;
    font-size: 0.875rem;
    color:     var(--apcf-text-muted);
    min-height: 1.4em;
}

.apcf-loader__bar {
    width:         240px;
    max-width:     80%;
    height:        4px;
    background:    rgba(255, 255, 255, 0.1);
    border-radius: 99px;
    overflow:      hidden;
}

.apcf-loader__fill {
    height:           100%;
    width:            0%;
    background:       linear-gradient(90deg, #e94560, #f5a623);
    border-radius:    99px;
    transition:       width 0.3s ease;
}

/* ─── Error state ────────────────────────────────────────────────────────── */
.apcf-error {
    display:         flex;
    flex-direction:  column;
    align-items:     center;
    justify-content: center;
    gap:             12px;
    min-height:      260px;
    padding:         40px 24px;
    text-align:      center;
}

.apcf-error[hidden] { display: none; }

.apcf-error__icon {
    font-size: 2.5rem;
    line-height: 1;
}

.apcf-error__message {
    margin:    0;
    font-size: 0.9375rem;
    color:     var(--apcf-text);
}

.apcf-error__details {
    margin-top: 8px;
    font-size:  0.8125rem;
    color:      var(--apcf-text-muted);
    max-width:  480px;
    text-align: left;
}

.apcf-error__details summary {
    cursor: pointer;
    outline: none;
}

.apcf-error__code {
    display:    block;
    margin-top: 6px;
    padding:    8px 10px;
    background: rgba(0,0,0,0.3);
    border-radius: 4px;
    white-space: pre-wrap;
    word-break: break-all;
    font-size: 0.75rem;
    color: #f87171;
}

/* ─── Stage (scene + controls) ───────────────────────────────────────────── */
.apcf-stage { display: flex; flex-direction: column; }
.apcf-stage[hidden] { display: none; }

/* ─── Scene (dark background around the book) ───────────────────────────── */
.apcf-scene {
    background:      var(--apcf-scene-bg);
    padding:         40px 24px 32px;
    display:         flex;
    justify-content: center;
    align-items:     center;
    position:        relative;
    /* Subtle radial glow behind book */
    background-image: radial-gradient(ellipse at 50% 50%, rgba(233,69,96,0.06) 0%, transparent 70%);
    /* JS sets min-height dynamically; this is the CSS floor */
    min-height:      500px;
    overflow:        visible;
}

/* ─── Book wrap — handles zoom + cover-centering via CSS transform ──────── */
.apcf-book-wrap {
    transform-origin: center top;
    /* Smooth both zoom and cover-centering shift */
    transition:       transform 0.38s cubic-bezier(0.25, 0.46, 0.45, 0.94);
    /* 3-D perspective for realistic page curl */
    perspective: 2000px;

    /* Book outer shadow */
    filter: drop-shadow(var(--apcf-shadow-book));
}

/* ─── Book element (StPageFlip mounts here) ─────────────────────────────── */
.apcf-book {
    /* StPageFlip sizes this; we just give it positioning context */
    position: relative;
}

/* ─── Individual page divs (fed to StPageFlip) ───────────────────────────── */
.apcf-page {
    overflow: hidden;
    background: #f5f0e8;
    /* Subtle page texture */
    background-image:
        repeating-linear-gradient(
            0deg,
            transparent,
            transparent 24px,
            rgba(0,0,0,0.015) 24px,
            rgba(0,0,0,0.015) 25px
        );
    display: flex;
    align-items: center;
    justify-content: center;
}

.apcf-page canvas,
.apcf-page img {
    display: block;
    width:   100%;
    height:  100%;
    object-fit: contain;
}

/* ─── StPageFlip page-shadow override (keeps plugin scoped) ─────────────── */
.apcf-book .stf__block { pointer-events: none; }

/* ─── Controls bar ───────────────────────────────────────────────────────── */
.apcf-controls {
    display:         flex;
    align-items:     center;
    justify-content: center;
    gap:             4px;
    height:          var(--apcf-ctrl-h);
    padding:         0 12px;
    background:      var(--apcf-ctrl-bg);
    border-top:      1px solid var(--apcf-ctrl-border);
    backdrop-filter: blur(8px);
    flex-wrap:       nowrap;
    overflow-x:      auto;
    -webkit-overflow-scrolling: touch;
}

.apcf-controls__divider {
    width:       1px;
    height:      22px;
    background:  var(--apcf-ctrl-border);
    margin:      0 4px;
    flex-shrink: 0;
}

/* ─── Buttons ────────────────────────────────────────────────────────────── */
.apcf-btn {
    display:         inline-flex;
    align-items:     center;
    justify-content: center;
    border:          none;
    cursor:          pointer;
    transition:      background var(--apcf-transition), color var(--apcf-transition), transform 0.1s ease;
    font-family:     inherit;
    user-select:     none;
    -webkit-tap-highlight-color: transparent;
}

.apcf-btn--icon {
    width:         36px;
    height:        36px;
    border-radius: 8px;
    background:    var(--apcf-btn-bg);
    color:         var(--apcf-text);
    padding:       0;
    flex-shrink:   0;
}

.apcf-btn--icon svg {
    width:  18px;
    height: 18px;
    pointer-events: none;
}

.apcf-btn--icon:hover {
    background: var(--apcf-btn-hover);
    color:      #fff;
}

.apcf-btn--icon:active {
    background: var(--apcf-btn-active);
    transform:  scale(0.93);
}

.apcf-btn--icon:focus-visible {
    outline: 2px solid var(--apcf-accent);
    outline-offset: 2px;
}

.apcf-btn--retry {
    padding:       8px 20px;
    border-radius: 6px;
    background:    var(--apcf-accent);
    color:         #fff;
    font-size:     0.875rem;
    font-weight:   600;
    letter-spacing: 0.01em;
}

.apcf-btn--retry:hover  { filter: brightness(1.1); }
.apcf-btn--retry:active { filter: brightness(0.9); }

/* ─── Page counter ───────────────────────────────────────────────────────── */
.apcf-page-counter {
    font-size:   0.875rem;
    font-weight: 500;
    color:       var(--apcf-text);
    min-width:   60px;
    text-align:  center;
    letter-spacing: 0.02em;
    flex-shrink: 0;
}

.apcf-page-counter__sep { color: var(--apcf-text-muted); }
.apcf-page-counter__total { color: var(--apcf-text-muted); }

/* ─── Zoom label ─────────────────────────────────────────────────────────── */
.apcf-zoom-label {
    font-size:   0.8125rem;
    font-weight: 500;
    color:       var(--apcf-text-muted);
    min-width:   38px;
    text-align:  center;
    flex-shrink: 0;
}

/* ─── Notice (inline error from PHP) ────────────────────────────────────── */
.apcf-notice {
    margin:        0;
    padding:       10px 14px;
    border-radius: 6px;
    font-size:     0.875rem;
}

.apcf-notice--error {
    background: rgba(233, 69, 96, 0.1);
    border:     1px solid rgba(233, 69, 96, 0.3);
    color:      #f87171;
}

/* ─── Fullscreen mode ────────────────────────────────────────────────────── */
.apcf-wrapper:fullscreen,
.apcf-wrapper:-webkit-full-screen {
    border-radius: 0;
    background:    var(--apcf-scene-bg);
    height:        100vh;
    width:         100vw;
    display:       flex;
    flex-direction: column;
}

.apcf-wrapper:fullscreen .apcf-stage,
.apcf-wrapper:-webkit-full-screen .apcf-stage {
    flex:           1;
    display:        flex;
    flex-direction: column;
}

.apcf-wrapper:fullscreen .apcf-scene,
.apcf-wrapper:-webkit-full-screen .apcf-scene {
    flex: 1;
}

/* ─── Mobile layout ──────────────────────────────────────────────────────── */
@media (max-width: 640px) {
    .apcf-scene {
        padding:    24px 6px 18px;
        min-height: 360px;
    }

    .apcf-controls {
        gap:     2px;
        padding: 0 6px;
        height:  46px;
    }

    .apcf-btn--icon {
        width:  32px;
        height: 32px;
    }

    .apcf-btn--icon svg {
        width:  16px;
        height: 16px;
    }

    .apcf-zoom-label,
    .apcf-controls__divider:last-of-type {
        display: none; /* Hide zoom % on very narrow screens */
    }

    .apcf-page-counter {
        font-size: 0.8125rem;
        min-width: 48px;
    }
}

/* ─── Tablet ─────────────────────────────────────────────────────────────── */
@media (min-width: 641px) and (max-width: 900px) {
    .apcf-scene { padding: 28px 16px 20px; }
}

/* ─── Touch-device swipe hint (hidden after first interaction) ─────────── */
.apcf-swipe-hint {
    position:   absolute;
    bottom:     64px;
    left:       50%;
    transform:  translateX(-50%);
    background: rgba(0, 0, 0, 0.55);
    color:      rgba(255,255,255,0.8);
    font-size:  0.75rem;
    padding:    5px 12px;
    border-radius: 20px;
    pointer-events: none;
    opacity:    1;
    transition: opacity 0.5s ease;
    white-space: nowrap;
}

.apcf-swipe-hint--hidden { opacity: 0; }

/* ─── Page-flip shadow (inside .stf__parent that StPageFlip creates) ─────── */
/* Make shadows more dramatic for premium feel */
.apcf-book .stf__parent .stf__item {
    box-shadow: none; /* StPageFlip draws its own canvas shadows */
}
