/* ==========================================
   CSS VARIABLES - DESIGN SYSTEM
   ========================================== */
:root {
    --main: #60A5FA;
    --main-hover: #3b82f6;
    --dark: #111;
    --panel: #0E1114;
    --panel-elevated: #151820;
    --panel-hover: #1a1a1a;
    --text: #dcd7d7;
    --text-muted: #9aa0a6;
    --border: #333;
    --border-light: #444;
    
    /* Reading-optimized typography */
    --font-reading: 'Georgia', 'Times New Roman', serif;
    --font-ui: 'Courier New', Courier, monospace;
    
    /* Fluid typography for reading */
    --text-base: clamp(1rem, 2.5vw + 0.5rem, 1.25rem);
    --text-reading: clamp(1.1rem, 3vw + 0.5rem, 1.6rem);
    --text-heading: clamp(1.5rem, 4vw + 1rem, 3rem);
    --text-logo: clamp(2.5rem, 8vw + 1rem, 5rem);
    
    /* Spacing system */
    --space-xs: clamp(0.25rem, 1vw, 0.5rem);
    --space-sm: clamp(0.5rem, 2vw, 1rem);
    --space-md: clamp(1rem, 3vw, 1.5rem);
    --space-lg: clamp(1.5rem, 4vw, 2.5rem);
    --space-xl: clamp(2rem, 6vw, 4rem);
    
    /* Safe areas for notch/dynamic island */
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
    
    /* Reading container max-width (optimal: 65-75 characters) */
    --reading-width: min(75ch, 100% - 2rem);
    --container-max: 1200px;
}

/* ==========================================
   THEME OVERRIDES
   ========================================== */
body.theme-light {
    /* Warm, low-glare reading theme */
    --panel: #f6f3ee;
    --panel-elevated: #ffffff;
    --panel-hover: #efe9e2;
    --text: #1f2937;
    --text-muted: #4b5563;
    --border: #c9c6c2;
    --border-light: #dedbd6;
}

/* ==========================================
   BASE RESET & STYLES
   ========================================== */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

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

body {
    background-color: var(--panel);
    width: 100%;
    min-height: 80vh;
    min-height: 80dvh;
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 0;
    padding: 0;
    font-family: var(--font-ui);
    overflow: hidden;
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
    color: var(--text);
    line-height: 1.5;
}

/* Prevent iOS Safari text selection/callout on tappable UI */
button,
[role="button"],
.menu-btn,
.pause-btn,
.inv-btn,
.zoom-btn,
.filter-dd-btn,
.filter-dd-item,
.btn-back-text,
.clear-cache-bottom,
#choices-list .choice-option {
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: transparent;
}

.hidden {
    display: none !important;
}

/* ==========================================
   DEVICE LAYOUT - FULL VIEWPORT
   ========================================== */
#device {
    width: 100%;
    max-width: 100vw;
    height: 100vh;
    height: 100dvh;
    background: var(--panel);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    /* Safe area padding */
    padding-top: var(--safe-top);
    padding-bottom: var(--safe-bottom);
    padding-left: var(--safe-left);
    padding-right: var(--safe-right);
}

/* Left Panel (Main Screen) */
#device-left {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--panel);
    padding: var(--space-md);
    min-width: 0;
    min-height: 0;
    overflow: hidden;
}

/* device-right removed (all controls are now in the main screen) */

/* ==========================================
   TOP BAR
   ========================================== */
#top-bar {
    height: 24px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 10px;
    color: var(--text-muted);
    border-bottom: 1px solid var(--border);
    margin-bottom: var(--space-xs);
    text-transform: uppercase;
    flex-shrink: 0;
    max-width: var(--container-max);
    width: 100%;
    margin-left: auto;
    margin-right: auto;
}

/* Account controls strip (below the top bar) */
#auth-strip {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 10px;
    width: 100%;
    max-width: var(--container-max);
    margin: 0 auto var(--space-sm);
    height: 28px;
    flex-shrink: 0;
}

/* Itch demo removes auth strip from HTML; keep spacing tight */
#auth-strip:empty {
    display: none;
}

/* Theme toggle stays at the far left */
#theme-toggle-btn {
    margin-right: auto;
}

.auth-user {
    max-width: 44vw;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: var(--text-muted);
}

.auth-top-btn {
    border: 1px solid var(--border);
    background: transparent;
    color: var(--text-muted);
    font-size: 10px;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    padding: 3px 8px;
    border-radius: 999px;
    cursor: pointer;
    transition: transform 0.12s ease, border-color 0.12s ease, color 0.12s ease;
    flex: 0 0 auto;
}

.auth-top-btn:hover {
    transform: translateY(-1px);
    border-color: var(--main);
    color: var(--main);
}

/* Auth modal */
.auth-overlay {
    position: fixed;
    inset: 0;
    z-index: 5000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--space-md);
    background: rgba(0,0,0,0.6);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
}

.auth-modal {
    width: min(440px, 92vw);
    border: 1px solid #2b2b2b;
    border-radius: 12px;
    background: var(--panel);
    color: #ddd;
    box-shadow: 0 16px 60px rgba(0,0,0,0.55);
    padding: var(--space-md);
}

.auth-modal h3 {
    margin: 0 0 10px;
    font-size: 14px;
    color: #eee;
    letter-spacing: 0.02em;
}

.auth-field {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin: 10px 0;
}

.auth-field label {
    font-size: 10px;
    color: #999;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.auth-field input {
    height: 38px;
    border-radius: 10px;
    border: 1px solid #3a3a3a;
    background: rgba(0,0,0,0.35);
    color: #e6e6e6;
    padding: 0 12px;
    outline: none;
    font-size: 16px; /* Prevents iOS zoom on focus */
}

.auth-field input:focus {
    border-color: var(--main);
}

.auth-field input:-webkit-autofill,
.auth-field input:-webkit-autofill:hover,
.auth-field input:-webkit-autofill:focus {
    -webkit-text-fill-color: #e6e6e6;
    caret-color: #e6e6e6;
    -webkit-box-shadow: 0 0 0px 1000px rgba(0,0,0,0.55) inset;
    box-shadow: 0 0 0px 1000px rgba(0,0,0,0.55) inset;
    border: 1px solid #3a3a3a;
}

.auth-actions {
    display: flex;
    gap: 10px;
    justify-content: flex-end;
    margin-top: 14px;
}

.auth-btn {
    height: 38px;
    padding: 0 14px;
    border-radius: 10px;
    border: 1px solid var(--border-light);
    background: rgba(255,255,255,0.02);
    color: #ddd;
    cursor: pointer;
    text-transform: uppercase;
    font-weight: bold;
    letter-spacing: 0.04em;
    transition: transform 0.12s ease, border-color 0.12s ease;
}

.auth-btn:hover {
    transform: translateY(-1px);
    border-color: var(--main);
}

.auth-btn.primary {
    background: var(--main);
    border-color: var(--main);
    color: #000;
}

.auth-alt {
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px solid #222;
    display: flex;
    gap: 10px;
    justify-content: space-between;
    flex-wrap: wrap;
}

.auth-msg {
    margin-top: 10px;
    font-size: 11px;
    color: #aaa;
    min-height: 16px;
}

#connection-icon {
    width: 12px;
    height: 12px;
    border: 2px solid var(--border-light);
    border-radius: 50%;
    border-top-color: #888;
    transform: rotate(45deg);
}

/* ==========================================
   SCREEN & VIEWS
   ========================================== */
#screen {
    flex: 1;
    position: relative;
    overflow: hidden;
    color: var(--text);
    min-height: 0;
    display: flex;
    flex-direction: column;
}

/* Give the menu + library titles a bit more space from the top strip */
#main-menu,
#library-view {
    padding-top: var(--space-sm);
}

.view-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

#library-view {
    min-height: 0;
}

/* Allow the story list to become the scrolling region inside the flex column */
#library-view {
    overflow: hidden;
}

.library-title-row {
    width: 100%;
    max-width: var(--container-max);
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 12px;
    flex-shrink: 0;
}

.library-title-row::after {
    content: '';
    width: 36px;
    flex: 0 0 36px;
}

.library-back-btn {
    width: 36px;
    height: 36px;
    border-radius: 999px;
    border: none;
    background: transparent;
    color: var(--text);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-left: -6px;
    font-size: 18px;
    line-height: 1;
    transition: transform 0.12s ease, color 0.12s ease;
}

.library-back-btn:hover {
    transform: translateY(-1px);
    color: var(--main);
}

.library-title-row h2 {
    flex: 1;
    text-align: center;
    margin: 0;
    font-size: var(--text-heading);
}


/* Logo Area */
.logo-area {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: var(--space-lg) 0;
}

.logo-area h1 {
    text-align: center;
    font-size: var(--text-logo);
    line-height: 0.9;
    margin-bottom: var(--space-lg);
    letter-spacing: -0.05em;
    color: var(--text);
}

.logo-area .visit-site {
    display: inline-block;
    margin-top: -8px;
    font-size: 12px;
    color: white;
    text-decoration: none;
    letter-spacing: 0.02em;
}

.logo-area .visit-site:hover {
    color: var(--main);
    text-decoration: underline;
}

.visit-site-row {
    width: 100%;
    max-width: var(--container-max);
    margin: 0 auto var(--space-sm);
    text-align: center;
}

/* Menu Actions */
.menu-actions {
    display: flex;
    flex-direction: column;
    gap: var(--space-md);
    align-items: center;
    padding: 0 var(--space-md);
}

.menu-btn {
    background: var(--panel);
    border: 1px solid var(--border-light);
    border-radius: 8px;
    color: var(--text);
    padding: var(--space-md) var(--space-lg);
    font-weight: bold;
    cursor: pointer;
    text-transform: uppercase;
    transition: all 0.2s ease;
    overflow: hidden;
    width: 100%;
    max-width: 400px;
    min-height: 56px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: var(--text-base);
}

.menu-btn:hover {
    background: var(--panel-hover);
    border-color: var(--main);
    transform: translateY(-2px);
}

.menu-btn.selected {
    background: var(--main);
    color: #000;
}

.menu-btn.disabled {
    border-color: var(--border);
    color: var(--border);
    cursor: default;
    background: var(--panel);
}

.menu-btn.disabled:hover {
    transform: none;
    border-color: var(--border);
}

.menu-btn-danger {
    border-color: #f44336 !important;
    color: #f44336 !important;
}

.menu-btn-danger:hover {
    background: #f44336 !important;
    color: #000 !important;
}

.menu-btn-danger.selected {
    background: #f44336 !important;
    color: #000 !important;
}

/* Story List */
#story-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(min(280px, 100%), 1fr));
    gap: var(--space-sm);
    align-items: stretch;
    align-content: start;
    padding: var(--space-sm);
    padding-bottom: calc(var(--space-sm) + var(--safe-bottom) + 24px);
    width: 100%;
    max-width: var(--container-max);
    margin: 0 auto;
    justify-content: center;
    flex: 1;
    min-height: 0;
    min-width: 0;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
}

/* Library filters */
#library-filters {
    display: flex;
    gap: 10px;
    align-items: center;
    justify-content: center;
    width: 100%;
    max-width: var(--container-max);
    margin: var(--space-xs) auto var(--space-sm);
    flex-shrink: 0;
    flex-wrap: wrap;
}

.filter-dd {
    flex: 1;
    min-width: 120px;
    max-width: 200px;
    position: relative;
}

.filter-dd-btn {
    width: 100%;
    background: var(--panel-hover);
    color: var(--text);
    border: 1px solid var(--border);
    padding: 10px 12px;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    outline: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 10px;
    justify-content: space-between;
}

.filter-dd-btn:focus,
.filter-dd.open .filter-dd-btn {
    border-color: var(--main);
}

.filter-dd-label {
    color: var(--text-muted);
    font-weight: 800;
    white-space: nowrap;
}

.filter-dd-value {
    flex: 1;
    text-align: right;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    color: var(--text);
}

.filter-dd-caret {
    color: var(--text-muted);
    font-size: 12px;
    margin-left: 6px;
    flex: 0 0 auto;
}

.filter-dd-menu {
    position: absolute;
    top: calc(100% + 6px);
    left: 0;
    right: 0;
    background: var(--panel-elevated);
    border: 1px solid var(--border);
    max-height: 240px;
    overflow: auto;
    z-index: 50;
    padding: 6px;
}

.filter-dd-item {
    width: 100%;
    text-align: left;
    background: transparent;
    color: var(--text);
    border: 1px solid transparent;
    padding: 10px;
    font-size: 12px;
    letter-spacing: 0.6px;
    text-transform: uppercase;
    cursor: pointer;
}

.filter-dd-item:hover {
    border-color: var(--border-light);
    background: rgba(96,165,250,0.08);
}

.filter-dd-item.selected {
    border-color: var(--main);
    background: rgba(96,165,250,0.16);
    color: var(--text);
}

#library-view {
    position: relative;
}

/* MY LIST hub: keep Catalog-like grid, capped at 3 columns */
#library-view.mylist-hub #story-list {
    grid-template-columns: repeat(3, minmax(0, 1fr));
}

@media (max-width: 900px) {
    #library-view.mylist-hub #story-list {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (max-width: 600px) {
    #library-view.mylist-hub #story-list {
        grid-template-columns: 1fr;
    }
}

#library-view.mylist-hub .list-section-title,
#library-view.mylist-hub .list-empty {
    grid-column: 1 / -1;
}

#library-view.saved-mode #story-list {
    padding-bottom: 80px;
}

.clear-cache-bottom {
    position: absolute;
    left: 50%;
    bottom: 14px;
    transform: translateX(-50%);
    z-index: 60;
    background: rgba(0,0,0,0.35);
    color: var(--text);
    border: 1px solid var(--border-light);
    border-radius: 6px;
    padding: 7px 10px;
    font-size: 10px;
    font-weight: 800;
    letter-spacing: 1px;
    text-transform: uppercase;
    cursor: pointer;
    white-space: nowrap;
}

body.theme-light .clear-cache-bottom {
    background: rgba(255,255,255,0.78);
}

body.theme-light .clear-cache-bottom:hover {
    background: rgba(255,255,255,0.92);
}

.clear-cache-bottom:hover {
    border-color: #f44336;
    color: #f44336;
    background: rgba(0,0,0,0.55);
}

#library-filters .filter-clear {
    background: transparent;
    color: var(--text);
    border: 1px solid var(--border-light);
    padding: 10px 12px;
    font-size: 11px;
    font-weight: 800;
    letter-spacing: 1px;
    text-transform: uppercase;
    cursor: pointer;
    white-space: nowrap;
}

#library-filters .filter-clear:hover {
    border-color: var(--main);
    color: var(--main);
}

/* Library cards */
.menu-btn.story-card {
    width: 100%;
    height: 132px;
    min-height: 132px;
    padding: 14px 12px;
    max-width: 100%;
    border-width: 1px;
    border-color: var(--border-light);
    border-radius: 8px;
    display: flex;
    align-items: stretch;
    justify-content: flex-start;
    text-transform: none;
}

.story-card-inner {
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 4px;
    min-width: 0;
    height: 100%;
}

.story-card-title {
    font-weight: 900;
    font-size: 13px;
    letter-spacing: 0.8px;
    text-transform: uppercase;
    line-height: 1.2;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.story-card-author {
    font-size: 10px;
    color: var(--text-muted);
    letter-spacing: 0.4px;
    line-height: 1.1;
    text-align: center;
    text-transform: none;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.story-card-meta {
    display: flex;
    flex-direction: column;
    gap: 2px;
    justify-content: center;
    align-items: center;
    width: 100%;
    font-size: 10px;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.8px;
    text-align: center;
    line-height: 1.25;
    padding: 6px 0;
}

.story-card.selected .story-card-meta {
    color: #000;
    opacity: 0.75;
}

.story-card-rating {
    color: #c9b458;
}

.story-card-rating.unrated {
    color: var(--text-muted);
}

/* ==========================================
   CHARACTER CREATION
   ========================================== */
.creation-layout {
    display: flex;
    gap: 50px;
    align-items: center;
    height: 100%;
    flex-direction: column;
}

.creation-title{
    font-size: 1.6rem;
    text-align: center;
}

.dice-box {
    margin-top: 50px;
    width: 50%;
    border: 0px solid #333;
    border-radius: 12px;
    padding: 30px;
    flex: 1;
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 20px;
}

#creation-step-text {
    font-size: 1.2rem;
    color: var(--main);
    font-weight: bold;
    letter-spacing: 2px;
    text-transform: uppercase;
    margin: 0;
    text-align: center;
}

.dice-animation-large {
    font-size: 5rem;
    color: var(--main);
    font-weight: bold;
    cursor: pointer;
    user-select: none;
    transition: transform 0.2s ease;
}

.dice-animation-large:hover {
    transform: scale(1.1);
}

.dice-animation-large.rolling {
    animation: diceShake 0.1s linear infinite;
}

.creation-info {
    text-align: center;
    flex: 1;
    font-size: 1.2rem;
    line-height: 1.5;
    color: var(--text);
}

/* Hide desktop instructions on mobile */
.creation-instruction-desktop {
    display: none;
}

.creation-instruction-mobile {
    display: block;
    font-size: 0.8rem;
}

/* ==========================================
   PAUSE MENU
   ========================================== */
#pause-menu {
    position: absolute;
    top: 0;
    left: 0;
    padding-top: 80px;
    width: 100%;
    height: 100%;
    background: var(--panel);
    z-index: 1000;
    display: flex;
    justify-content: center;
}

.pause-content {
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
    max-width: 400px;
    width: 90%;
}

.pause-content h2 {
    font-size: 3rem;
    color: var(--main);
    text-transform: uppercase;
    letter-spacing: 5px;
    margin: 0 0 20px 0;
}

.pause-btn {
    background: var(--panel);
    border: 1px solid var(--border-light);
    border-radius: 8px;
    color: var(--text);
    padding: 18px 40px;
    font-weight: bold;
    cursor: pointer;
    text-transform: uppercase;
    transition: all 0.2s;
    width: 100%;
    font-size: 1.2rem;
    letter-spacing: 2px;
}

.pause-btn:hover {
    background: var(--panel-hover);
    border-color: var(--main);
    transform: translateY(-2px);
}

.pause-btn.selected {
    box-shadow: 0 0 0 3px rgba(240, 200, 60, 0.18);
    background: rgba(0, 0, 0, 0.25);
}

.pause-btn-danger {
    border-color: #666;
    color: #888;
}

.pause-btn-danger:hover {
    background: rgba(255, 68, 68, 0.1);
    border-color: #ff4444;
    color: #ff4444;
}

/* ==========================================
   IN-GAME SCREEN - READING OPTIMIZED
   ========================================== */
#ingame-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px dashed var(--border);
    padding-bottom: var(--space-sm);
    margin-bottom: var(--space-sm);
    flex-shrink: 0;
    gap: var(--space-sm);
    flex-wrap: wrap;
}

#location-text {
    font-size: 12px;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 1px;
}

.inv-btn {
    background: var(--panel);
    border: 1px solid var(--border-light);
    color: var(--text);
    padding: 5px 10px;
    font-size: 10px;
    cursor: pointer;
    border-radius: 8px;
    transition: 0.15s;
}

.inv-btn:hover {
    border-color: var(--main);
    color: var(--main);
    background: var(--panel-hover);
}

.zoom-btn {
    background: var(--panel);
    border: 1px solid var(--border-light);
    color: var(--text);
    padding: 5px 8px;
    font-size: 11px;
    cursor: pointer;
    font-weight: bold;
    transition: 0.15s;
    min-width: 32px;
}

.zoom-btn:disabled {
    opacity: 0.35;
    cursor: not-allowed;
}

.zoom-btn:hover:not(:disabled) {
    border-color: var(--main);
    color: var(--main);
    background: var(--panel-hover);
}

.zoom-btn:active:not(:disabled) {
    background: var(--main);
    color: #000;
    border-color: var(--main);
}

.rate-btn {
    width: 36px;
    padding: 0;
    font-size: 14px;
    letter-spacing: 0;
}

/* Scroll Container - READING OPTIMIZED */
#scroll-container {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 0 var(--space-sm) var(--space-lg);
    scrollbar-width: thin;
    scrollbar-color: var(--border) transparent;
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
    overscroll-behavior: contain;
    min-height: 0;
}

#scroll-container::-webkit-scrollbar {
    width: 6px;
}

#scroll-container::-webkit-scrollbar-track {
    background: transparent;
}

#scroll-container::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 3px;
}

#scroll-container::-webkit-scrollbar-thumb:hover {
    background: var(--border-light);
}

/* Story Text - READING OPTIMIZED TYPOGRAPHY */
#story-text {
    font-family: var(--font-reading);
    font-size: var(--text-reading);
    line-height: 1.7;
    color: var(--text);
    text-align: left;
    hyphens: auto;
    -webkit-hyphens: auto;
    word-wrap: break-word;
    overflow-wrap: break-word;
    padding-bottom: var(--space-lg);
    -webkit-user-select: text;
    user-select: text;
    max-width: var(--reading-width);
    margin: 0 auto;
    /* Optimal line length for reading */
    letter-spacing: 0.01em;
    word-spacing: 0.05em;
}

/* Story footer */
#story-footer {
    padding: 0 0 var(--space-md);
    max-width: var(--reading-width);
    margin: 0 auto;
}

.story-end-card {
    border: 1px solid var(--border);
    background: rgba(0,0,0,0.18);
    border-radius: 12px;
    padding: var(--space-md);
}

body.theme-light .story-end-card {
    background: rgba(255,255,255,0.72);
}

.story-end-title {
    font-size: 12px;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--main);
    margin: 0 0 8px;
}

.story-end-meta {
    font-size: 12px;
    color: var(--text-muted);
    margin: 0 0 12px;
}

.story-end-actions {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

.story-end-btn {
    border: 1px solid var(--border);
    background: transparent;
    color: var(--text);
    font-size: 11px;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    padding: 10px 12px;
    border-radius: 10px;
    cursor: pointer;
    transition: transform 0.12s ease, border-color 0.12s ease, color 0.12s ease;
}

.story-end-btn:hover {
    transform: translateY(-1px);
    border-color: var(--main);
    color: var(--main);
}

.story-end-btn.primary {
    background: var(--main);
    border-color: var(--main);
    color: #000;
}

/* Rating modal */
.rate-overlay {
    position: fixed;
    inset: 0;
    z-index: 4800;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--space-md);
    background: rgba(0,0,0,0.6);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
}

.rate-modal {
    width: min(520px, 92vw);
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--panel);
    color: var(--text);
    box-shadow: 0 16px 60px rgba(0,0,0,0.55);
    padding: var(--space-md);
}

.rate-modal h3 {
    margin: 0 0 8px;
    font-size: 14px;
    color: var(--text);
}

.rate-sub {
    margin: 0 0 12px;
    font-size: 12px;
    color: var(--text-muted);
}

.rate-stars {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    margin: 10px 0 14px;
}

.rate-star {
    width: 44px;
    height: 44px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: rgba(255,255,255,0.02);
    color: var(--text-muted);
    cursor: pointer;
    font-size: 18px;
    line-height: 1;
    transition: transform 0.12s ease, border-color 0.12s ease, color 0.12s ease;
}

.rate-star:hover {
    transform: translateY(-1px);
    border-color: var(--main);
    color: var(--main);
}

.rate-star.selected {
    background: var(--main);
    border-color: var(--main);
    color: #000;
}

.rate-actions {
    display: flex;
    gap: 10px;
    justify-content: flex-end;
    margin-top: 12px;
}

.rate-msg {
    margin-top: 10px;
    font-size: 12px;
    color: var(--text-muted);
    min-height: 16px;
}

#continue-prompt {
    text-align: center;
    padding: var(--space-lg) var(--space-md);
    font-size: var(--text-base);
    color: var(--main);
    font-weight: bold;
    letter-spacing: 2px;
    border-top: 1px dashed var(--border);
    margin-top: var(--space-md);
    cursor: pointer;
    transition: all 0.2s ease;
    max-width: var(--reading-width);
    margin-left: auto;
    margin-right: auto;
}

.continue-msg-desktop { display: none; }
.continue-msg-mobile { display: inline; }

.blink {
    animation: blink 1.5s ease-in-out infinite;
}

/* The in-game tap/click "proceed" hint should not blink */
#continue-prompt.blink {
    animation: none;
}

/* Don't blink the mobile/touch "proceed" hint (less distracting on phones) */
@media (hover: none) and (pointer: coarse) {
    #continue-prompt.blink {
        animation: none;
    }
}

@keyframes blink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* ==========================================
   CHOICES OVERLAY
   ========================================== */
#choices-overlay {
    position: absolute;
    inset: 0;
    background: var(--panel);
    z-index: 90;
    display: flex;
    flex-direction: column;
    padding: var(--space-lg);
    align-items: center;
    justify-content: center;
    overflow-y: auto;
}

#choices-overlay h3 {
    font-size: var(--text-heading);
    color: var(--main);
    text-transform: uppercase;
    letter-spacing: 3px;
    margin: 0 0 var(--space-lg) 0;
    padding-bottom: var(--space-sm);
    border-bottom: 2px solid var(--border);
    text-align: center;
    flex-shrink: 0;
}

#choices-list {
    display: flex;
    flex-direction: column;
    gap: var(--space-sm);
    max-height: 60vh;
    overflow-y: auto;
    padding: var(--space-sm);
    align-items: center;
    width: 100%;
    max-width: 700px;
}

#choices-list .choice-option {
    background: var(--panel);
    border: 1px solid var(--border-light);
    border-radius: 8px;
    color: var(--text);
    padding: var(--space-xs) var(--space-md);
    font-family: var(--font-reading);
    font-size: var(--text-base);
    line-height: 1.4;
    transition: all 0.2s ease;
    cursor: pointer;
    width: 100%;
    text-align: center;
    user-select: none;
}

#choices-list .choice-option:hover {
    border-color: var(--main);
    background: var(--panel-hover);
}

#choices-list .choice-option.selected {
    background: var(--main);
    color: #000;
    border-color: var(--main);
    font-weight: bold;
}

#choices-list .choice-option.locked {
    background: var(--panel-elevated);
    border-color: var(--border);
    color: var(--text-muted);
    cursor: not-allowed;
    opacity: 0.6;
}

.choice-help {
    text-align: center;
    padding: var(--space-md);
    font-size: 13px;
    color: var(--text-muted);
    font-weight: bold;
    letter-spacing: 1px;
    border-top: 1px solid var(--border);
    margin-top: var(--space-md);
    flex-shrink: 0;
}

.btn-back-text {
    width: 100%;
    max-width: 400px;
    margin: var(--space-md) auto 0;
    padding: 12px 20px;
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    cursor: pointer;
    transition: all 0.2s;
    display: block;
}

.btn-back-text:hover {
    background: transparent;
    color: #888;
}

/* ==========================================
   INVENTORY OVERLAY
   ========================================== */
#inventory-overlay {
    position: absolute;
    inset: 0;
    background: var(--panel);
    z-index: 100;
    padding: var(--space-lg);
    display: flex;
    flex-direction: column;
    backdrop-filter: blur(8px);
    overflow-y: auto;
}

#inventory-overlay h3 {
    font-size: var(--text-heading);
    color: var(--main);
    text-transform: uppercase;
    letter-spacing: 4px;
    margin: 0 0 var(--space-lg) 0;
    padding-bottom: var(--space-sm);
    border-bottom: 2px solid var(--border);
    text-align: center;
    flex-shrink: 0;
}

.inv-flex {
    display: flex;
    gap: var(--space-lg);
    flex: 1;
    min-height: 0;
    overflow: hidden;
}

#inv-stats-grid {
    flex: 0 0 auto;
    width: 200px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: var(--space-sm);
    background: var(--panel);
    border: 1px solid var(--border-light);
    border-radius: 4px;
}

#inv-stats-grid > div {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 10px;
    background: rgba(255, 255, 255, 0.02);
    border-radius: 3px;
    font-size: 13px;
    color: var(--text);
}

#inv-stats-grid > div::after {
    content: attr(data-value);
    color: var(--main);
    font-size: 16px;
    font-weight: bold;
}

#inv-items-list {
    flex: 1;
    border-left: 2px solid var(--border-light);
    padding-left: var(--space-lg);
    list-style: none;
    margin: 0;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
    -webkit-overflow-scrolling: touch;
}

#inv-items-list li {
    padding: var(--space-sm) var(--space-md);
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text);
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 12px;
    transition: all 0.2s ease;
}

#inv-items-list li:hover {
    background: rgba(255, 255, 255, 0.08);
    border-color: var(--main);
    transform: translateX(5px);
}

#inv-items-list .empty-msg {
    color: #666;
    font-style: italic;
    text-align: center;
    padding: var(--space-lg);
    border: 1px dashed var(--border);
    background: transparent;
}

.inv-footer {
    flex-shrink: 0;
    margin-top: var(--space-lg);
}

#btn-close-inv {
    background: var(--panel);
    border: 1px solid var(--border-light);
    padding: var(--space-sm) var(--space-lg);
    width: 100%;
    font-weight: bold;
    font-size: 14px;
    cursor: pointer;
    color: #ddd;
    text-transform: uppercase;
    letter-spacing: 2px;
    border-radius: 8px;
    transition: all 0.2s ease;
}

#btn-close-inv:hover,
#btn-close-inv.selected {
    background: var(--main);
    color: #000;
    border-color: var(--main);
    transform: translateY(-2px);
}

/* ==========================================
   FULLSCREEN DICE OVERLAY
   ========================================== */
#fullscreen-overlay {
    position: absolute;
    inset: 0;
    background: var(--panel);
    z-index: 999;
    display: flex;
    justify-content: center;
    align-items: center;
}

#fs-content {
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-sm);
    max-width: 600px;
    width: 100%;
    padding: var(--space-md);
}

#fs-title {
    font-size: 1.5rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 3px;
    margin: 0;
}

#fs-target {
    font-size: 1rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 2px;
}

#fs-diff-val {
    color: var(--main);
    font-weight: bold;
    font-size: 1.2rem;
}

#fs-dice {
    font-size: clamp(4rem, 15vw, 8rem);
    font-weight: 900;
    color: var(--text);
    margin: var(--space-md) 0;
    display: inline-block;
    line-height: 1;
    min-width: 150px;
    will-change: transform, opacity;
    transform-origin: center center;
}

#fs-dice.rolling {
    animation: diceRollD20 0.12s ease-in-out infinite;
}

.result-success #fs-dice { color: #4caf50; }
.result-fail #fs-dice { color: #f44336; }

#fs-math {
    font-size: clamp(1.2rem, 4vw, 1.8rem);
    color: var(--text);
    margin: 10px 0;
    min-height: 45px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    opacity: 1;
    transition: opacity 0.3s ease;
}

#fs-math.hidden {
    opacity: 0;
    visibility: hidden;
    display: flex !important;
}

#fs-math span { color: var(--text); font-weight: bold; }
#fs-roll-val { color: var(--text) !important; }
#fs-stat-val { color: var(--main) !important; }
#fs-total-val { color: #4caf50 !important; }

#fs-result-msg {
    font-size: 1.2rem;
    color: var(--text);
    text-transform: uppercase;
    letter-spacing: 2px;
    margin-top: var(--space-md);
}

.fs-msg-desktop { display: none; }
.fs-msg-mobile { display: inline; }

/* ==========================================
   ANIMATIONS
   ========================================== */
@keyframes diceShake {
    0%, 100% { transform: translate(0, 0); }
    10% { transform: translate(-2px, -1px); }
    20% { transform: translate(2px, 1px); }
    30% { transform: translate(-2px, 1px); }
    40% { transform: translate(2px, -1px); }
    50% { transform: translate(-1px, 2px); }
    60% { transform: translate(1px, -2px); }
    70% { transform: translate(-1px, -1px); }
    80% { transform: translate(1px, 1px); }
    90% { transform: translate(-1px, 1px); }
}

@keyframes diceRollD20 {
    0%, 100% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.05); opacity: 0.85; }
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes fadeSlideIn {
    from { opacity: 0; transform: translateY(20px); }
    to { opacity: 1; transform: translateY(0); }
}

@keyframes slideInChoice {
    from { opacity: 0; transform: translateX(-30px); }
    to { opacity: 1; transform: translateX(0); }
}

/* ==========================================
   DESKTOP (min-width: 901px)
   ========================================== */
@media (min-width: 901px) {
    #device {
        flex-direction: row;
        max-width: var(--container-max);
    }
    
    #device-left {
        padding: var(--space-xl);
        max-width: 100%;
    }
    
    /* Better typography for desktop reading */
    #story-text {
        font-size: 1.5rem;
        line-height: 1.8;
    }
    
    #continue-prompt {
        font-size: 16px;
        padding: var(--space-xl) var(--space-md);
    }
    
    .continue-msg-desktop { display: inline; }
    .continue-msg-mobile { display: none; }
    
    #choices-list .choice-option {
        font-size: 1.25rem;
        padding: var(--space-sm) var(--space-lg);
        max-width: 800px;
    }
    
    .menu-btn {
        font-size: 1.2rem;
        max-width: 500px;
    }
    
    .logo-area h1 {
        font-size: 5rem;
    }
    
    .creation-instruction-desktop { display: block; }
    .creation-instruction-mobile { display: none; }
    
    #creation-step-text { font-size: 1.5rem; }
    #creation-view { cursor: pointer; }
    
    .fs-msg-desktop { display: inline; }
    .fs-msg-mobile { display: none; }
    
    #fullscreen-overlay { cursor: pointer; }
    
    #inventory-overlay { padding: var(--space-xl); }
    #inventory-overlay h3 { font-size: 2.5rem; }
    .inv-flex { gap: var(--space-xl); }
    #inv-stats-grid { width: 250px; }
    
    #choices-overlay { padding: var(--space-xl); }
    #choices-overlay h3 { font-size: 2.2rem; }
    #choices-list { max-width: 900px; }
    
    #fs-content { max-width: 700px; }
    #fs-dice { font-size: 10rem; }
    #fs-math { font-size: 2.2rem; }
    
    .choice-help {
        font-size: 14px;
        text-indent: -9999px;
        line-height: 0;
    }
    
    .choice-help::before {
        content: "↑↓ Navigate | ENTER or CLICK to select \A ESC to open menu | I for Inventory";
        display: block;
        text-indent: 0;
        line-height: 1.5;
        white-space: pre-line;
    }
}

/* ==========================================
   TABLET & MOBILE (max-width: 900px)
   ========================================== */
@media (max-width: 900px) {
    #device {
        flex-direction: column;
    }
    
    #device-left {
        flex: 1;
        min-height: 0;
        padding: var(--space-sm) var(--space-md);
    }
    
    
    #story-text {
        font-size: 1.3rem;
        line-height: 1.6;
    }
    
    .logo-area h1 {
        font-size: 3rem;
    }
    
    .menu-btn {
        max-width: 100%;
    }

    /* Uniform cards on mobile/tablet */
    .menu-btn.story-card {
        height: 132px;
        min-height: 132px;
    }

    /* Center the card content vertically within the fixed height */
    .story-card-inner {
        height: 100%;
        justify-content: center;
    }

    /* Make the proceed hint less prominent on mobile */
    #continue-prompt {
        font-size: 12px;
        letter-spacing: 1px;
        padding: var(--space-md) var(--space-sm);
    }
    
    #library-filters {
        flex-direction: column;
        align-items: stretch;
    }
    
    .filter-dd {
        max-width: 100%;
    }
    
    .choice-help {
        display: none;
    }
    
    #choices-overlay {
        padding: var(--space-md);
    }
    
    #choices-list .choice-option {
        font-size: 1.2rem;
        max-width: 100%;
    }
    
    .inv-flex {
        flex-direction: column;
        gap: var(--space-sm);
    }
    
    #inv-stats-grid {
        width: 100%;
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 8px;
    }
    
    #inv-items-list {
        border-left: none;
        border-top: 1px solid var(--border-light);
        padding-left: 0;
        padding-top: var(--space-sm);
        max-height: 200px;
    }
    
    #inventory-overlay {
        padding: var(--space-md);
    }
}

/* Phones: keep the catalog/saved list in a single column so cards don't shrink */
@media (max-width: 700px) {
    #story-list {
        grid-template-columns: 1fr;
    }
}

/* ==========================================
   SMALL MOBILE (max-width: 480px)
   ========================================== */
@media (max-width: 480px) {
    :root {
        --space-md: 0.75rem;
        --space-lg: 1rem;
    }
    
    #device-left {
        padding: 12px;
    }
    
    
    #story-text {
        font-size: 1.15rem;
        line-height: 1.55;
    }
    
    .logo-area h1 {
        font-size: 2.5rem;
    }
    
    #story-list {
        grid-template-columns: 1fr;
    }
    
    
    #choices-list .choice-option {
        font-size: 1.1rem;
        padding: calc(var(--space-xs) + 4px) var(--space-sm);
    }

    /* Only the title (e.g. “MAKE YOUR CHOICE”) should be smaller on small screens */
    #choices-overlay h3 {
        font-size: 1.25rem;
        letter-spacing: 2px;
        margin-bottom: var(--space-md);
    }
    
}

/* ==========================================
   LANDSCAPE MOBILE
   ========================================== */
@media (max-height: 500px) and (orientation: landscape) {
    #device {
        flex-direction: row;
    }
    
    #device-left {
        flex: 1;
        padding: var(--space-xs) var(--space-sm);
    }
    

    
    #story-text {
        font-size: 1rem;
        line-height: 1.5;
    }
    
    .logo-area h1 {
        font-size: 2rem;
        margin-bottom: var(--space-sm);
    }
    
    .logo-area {
        padding: var(--space-xs) 0;
    }
}

/* ==========================================
   PRINT STYLES (for reading)
   ========================================== */
@media print {
    body {
        background: white;
        color: black;
    }
    
    #top-bar,
    #auth-strip,
    #ingame-header,
    #continue-prompt,
    .hidden {
        display: none !important;
    }
    
    #story-text {
        font-size: 12pt;
        line-height: 1.6;
        color: black;
        max-width: 100%;
    }
}
