/* VN Layout — Full-viewport visual novel layer system */

:root {
    --vn-dialogue-max-width: 880px;
    --vn-dialogue-width: 94%;
    --vn-character-width: clamp(150px, 14vw, 190px);
    --vn-character-height: clamp(220px, 24vw, 280px);
    --vn-control-bottom-gap: max(8px, env(safe-area-inset-bottom, 0px));
}

/* Reset for VN mode */
.vn-viewport {
    position: fixed;
    inset: 0;
    overflow: hidden;
    background: #000;
    font-family: 'Georgia', 'Times New Roman', serif;
    user-select: none;
    cursor: default;
    -webkit-tap-highlight-color: transparent;
}

/* Stage fills viewport — no aspect ratio letterboxing */
.vn-stage {
    position: absolute;
    inset: 0;
    overflow: hidden;
}

/* === LAYER STACK === */

.vn-layer-background {
    position: absolute;
    inset: 0;
    z-index: 0;
    overflow: hidden;
}

.vn-layer-background img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* Dual background slots for crossfade transitions */
.vn-bg-current,
.vn-bg-next {
    position: absolute;
    inset: 0;
}

.vn-bg-current img,
.vn-bg-next img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.vn-layer-characters {
    position: absolute;
    inset: 0;
    z-index: 10;
    pointer-events: none;
    overflow: hidden;
    white-space: nowrap;
    isolation: isolate;
}

.vn-layer-effects {
    position: absolute;
    inset: 0;
    z-index: 20;
    pointer-events: none;
}

.vn-layer-dialogue {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 30;
    pointer-events: none;
}

.vn-layer-choices {
    position: absolute;
    inset: 0;
    z-index: 40;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
}

.vn-layer-choices.active {
    pointer-events: auto;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(2px);
}

.vn-layer-ui {
    position: absolute;
    inset: 0;
    z-index: 50;
    pointer-events: none;
}

.vn-layer-modal {
    position: absolute;
    inset: 0;
    z-index: 100;
    display: none;
}

.vn-layer-modal.active {
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.7);
}

/* === CHARACTER POSITIONS === */

.vn-character {
    position: absolute !important;
    bottom: 4%;
    transform: translateX(-50%);
    pointer-events: none;
    overflow: hidden !important;
    display: block;
    float: none;
    clear: none;
    flex: none;
    max-width: none;
    max-height: none;
    width: var(--vn-character-width);
    height: var(--vn-character-height);
    border-radius: 10px;
    box-shadow: 0 4px 24px rgba(0, 0, 0, 0.55), 0 0 0 1px rgba(255,255,255,0.08);
}

.vn-character img {
    display: block;
    width: var(--vn-character-width) !important;
    height: var(--vn-character-height) !important;
    object-fit: cover;
    /* Lift the focal point so the face — typically generated in the upper
       third of the source image — is the part we keep when cropping. */
    object-position: 50% 18%;
    max-width: none;
    max-height: none;
}

/* Portraits flank the centered dialogue column at ~880px max-width.
   On a 1280px viewport that leaves ~200px gutters on each side; the
   left/right positions sit centered in those gutters instead of
   crowding the dialogue text. */
.vn-character[data-position="left"]         { left: clamp(6%, 9vw, 10%); }
.vn-character[data-position="center-left"]  { left: clamp(16%, 20vw, 22%); }
.vn-character[data-position="center"]       { left: 50%; }
.vn-character[data-position="center-right"] { left: clamp(78%, 80vw, 84%); }
.vn-character[data-position="right"]        { left: clamp(90%, 92vw, 94%); }

/* === IMAGE LOADING STATES === */

.vn-character.loading img {
    opacity: 0;
}

.vn-character.loading::after {
    content: '';
    position: absolute;
    inset: 10% 15%;
    background: linear-gradient(
        110deg,
        rgba(255,255,255,0.03) 30%,
        rgba(255,255,255,0.08) 50%,
        rgba(255,255,255,0.03) 70%
    );
    background-size: 200% 100%;
    animation: vn-shimmer 1.5s ease-in-out infinite;
    border-radius: 12px;
    border: 1px solid rgba(255,255,255,0.05);
}

.vn-bg-loading {
    position: absolute;
    inset: 0;
    z-index: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0,0,0,0.6);
    backdrop-filter: blur(8px);
}

.vn-bg-loading-content {
    text-align: center;
    color: rgba(255,255,255,0.6);
    font-size: 0.85rem;
}

.vn-bg-loading-bar {
    width: 120px;
    height: 3px;
    background: rgba(255,255,255,0.1);
    border-radius: 2px;
    margin: 12px auto 0;
    overflow: hidden;
}

.vn-bg-loading-bar::after {
    content: '';
    display: block;
    width: 40%;
    height: 100%;
    background: rgba(255,255,255,0.4);
    border-radius: 2px;
    animation: vn-loading-slide 1.2s ease-in-out infinite;
}

@keyframes vn-shimmer {
    0% { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

@keyframes vn-loading-slide {
    0% { transform: translateX(-100%); }
    100% { transform: translateX(350%); }
}

/* === MAIN LOADING OVERLAY === */

.vn-loading {
    position: absolute;
    inset: 0;
    z-index: 90;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.85);
    backdrop-filter: blur(4px);
    color: #ccc;
    font-size: 1.15rem;
    gap: 1rem;
}

.vn-loading-spinner {
    width: 36px;
    height: 36px;
    border: 2px solid rgba(255, 255, 255, 0.1);
    border-top-color: rgba(255, 255, 255, 0.7);
    border-radius: 50%;
    animation: vn-spin 0.7s linear infinite;
}

@keyframes vn-spin {
    to { transform: rotate(360deg); }
}

.vn-loading-text {
    opacity: 0.6;
    font-style: italic;
    font-size: 0.9rem;
}

.vn-loading-subtext {
    opacity: 0.35;
    font-size: 0.75rem;
    margin-top: -0.5rem;
}

/* === UI CONTROLS === */

.vn-controls {
    position: absolute;
    bottom: var(--vn-control-bottom-gap);
    right: 8px;
    display: flex;
    gap: 4px;
    pointer-events: auto;
    z-index: 35; /* Above dialogue layer */
}

.vn-control-btn {
    background: rgba(0, 0, 0, 0.55);
    color: rgba(255, 255, 255, 0.65);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 4px;
    padding: 6px 12px;
    font-size: 0.85rem;
    cursor: pointer;
    transition: all 0.15s ease;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.vn-control-btn:hover,
.vn-control-btn:active {
    background: rgba(255, 255, 255, 0.15);
    color: #fff;
}

.vn-control-btn.active {
    background: rgba(100, 150, 255, 0.3);
    border-color: rgba(100, 150, 255, 0.5);
    color: #fff;
}

/* === SIDEBAR OVERLAY === */

.vn-sidebar {
    position: absolute;
    top: 0;
    right: 0;
    width: 300px;
    height: 100%;
    background: rgba(10, 10, 20, 0.94);
    border-left: 1px solid rgba(255, 255, 255, 0.06);
    transform: translateX(100%);
    transition: transform 0.3s ease;
    overflow-y: auto;
    pointer-events: auto;
    z-index: 55;
    padding: 1rem;
    color: #ddd;
    backdrop-filter: blur(10px);
}

.vn-sidebar.open {
    transform: translateX(0);
}

/* Sidebar sections — stats, relationships, flags, inventory. */
.vn-sidebar-section {
    margin-bottom: 1.2rem;
    padding-bottom: 0.8rem;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}
.vn-sidebar-section:last-child {
    border-bottom: none;
}
.vn-sidebar-section h4 {
    color: #8ab4f8;
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    margin-bottom: 0.6rem;
    font-weight: 600;
}
.vn-sidebar-empty {
    color: rgba(255,255,255,0.32);
    font-size: 0.82rem;
    font-style: italic;
    margin: 0.25rem 0;
}
.vn-stat-row {
    margin-bottom: 0.55rem;
    border-radius: 4px;
    padding: 4px 6px;
    transition: background 0.4s ease;
}
.vn-stat-row-head {
    display: flex;
    justify-content: space-between;
    font-size: 0.85rem;
    margin-bottom: 4px;
}
.vn-stat-label {
    color: rgba(255,255,255,0.75);
    text-transform: capitalize;
}
.vn-stat-value {
    color: rgba(255,255,255,0.95);
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}
.vn-stat-bar {
    height: 4px;
    background: rgba(255,255,255,0.07);
    border-radius: 2px;
    overflow: hidden;
}
.vn-stat-bar-fill {
    height: 100%;
    background: linear-gradient(90deg, #4a6fa5 0%, #8ab4f8 100%);
    border-radius: 2px;
    transition: width 0.4s ease;
}
.vn-stat-row-flat {
    display: flex;
    justify-content: space-between;
    font-size: 0.82rem;
    padding: 3px 6px;
}
.vn-flag-list {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem;
}
.vn-flag-chip,
.vn-inv-chip {
    display: inline-block;
    background: rgba(74, 111, 165, 0.18);
    border: 1px solid rgba(74, 111, 165, 0.4);
    color: rgba(255,255,255,0.85);
    font-size: 0.78rem;
    padding: 3px 9px;
    border-radius: 12px;
}
.vn-inv-chip {
    background: rgba(140, 80, 160, 0.18);
    border-color: rgba(140, 80, 160, 0.4);
}
.vn-changed {
    /* The JS animate() pulse handles the visible flash; the class is
       also a stable hook for diffing-driven tests. */
    box-shadow: inset 0 0 0 1px rgba(255, 214, 102, 0.4);
}

.vn-sidebar-toggle {
    position: absolute;
    top: 8px;
    right: 8px;
    pointer-events: auto;
    z-index: 56;
}

/* === MOBILE OPTIMIZATION === */

@media (max-width: 768px) {
    /* On mobile, only show the first (speaking) character */
    .vn-character {
        --vn-character-width: 130px;
        --vn-character-height: 200px;
        bottom: 16%;
        border-radius: 8px;
        display: none; /* Hide all by default on mobile */
    }

    /* Show only the first character (speaker) */
    .vn-character:first-child {
        display: block;
        left: 50% !important; /* Center the single portrait */
    }

    .vn-character img {
        width: var(--vn-character-width) !important;
        height: var(--vn-character-height) !important;
    }

    .vn-character[data-position="left"] { left: 25%; }
    .vn-character[data-position="center-left"] { left: 35%; }
    .vn-character[data-position="center"] { left: 50%; }
    .vn-character[data-position="center-right"] { left: 65%; }
    .vn-character[data-position="right"] { left: 75%; }

    .vn-sidebar {
        width: 100%;
    }

    .vn-controls {
        bottom: 4px;
        right: 4px;
        gap: 3px;
    }

    .vn-control-btn {
        padding: 6px 8px;
        font-size: 0.65rem;
        min-height: 32px;
        min-width: 32px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .vn-sidebar-toggle {
        top: 6px;
        right: 6px;
        min-height: 32px;
    }
}

/* Very small screens (phones) */
@media (max-width: 480px) {
    .vn-character {
        --vn-character-width: 100px;
        --vn-character-height: 160px;
        bottom: 22%;
    }

    .vn-character img {
        width: var(--vn-character-width) !important;
        height: var(--vn-character-height) !important;
    }

    .vn-controls {
        bottom: 2px;
        right: 2px;
        gap: 2px;
    }

    .vn-control-btn {
        padding: 5px 6px;
        font-size: 0.6rem;
    }
}

/* Landscape phones */
@media (max-height: 500px) and (orientation: landscape) {
    .vn-character {
        --vn-character-width: 120px;
        --vn-character-height: 180px;
        bottom: 20%;
    }

    .vn-character img {
        width: var(--vn-character-width) !important;
        height: var(--vn-character-height) !important;
    }
}

/* Editor preview placeholder: shown when vn-player.html is loaded with
   ?preview=true and no scene has been pushed from the editor yet. */
body.vn-preview-waiting::after {
    content: 'Click "Generate Preview" in the editor';
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    color: rgba(255, 255, 255, 0.5);
    font: 14px/1.4 system-ui, sans-serif;
    pointer-events: none;
}
