/* Palette: ruby (act), sapphire (navigate, backdrop), gold (frame, ornament) */
:root {
    --ruby: #E0115F;
    --ruby-bright: #FF5C8A;
    --sapphire: #0F52BA;
    --sapphire-bright: #6EA8FF;
    --gold: #E6B93A;
    --gold-dim: #A88429;
    --ink: #050B1E;
    --panel-bg: rgba(5, 12, 36, 0.78);
    --text: #EEF3FF;
    --muted-text: #A9B8D8;
    --top-bg: #0A2A6B;
    --bottom-bg: #000000;
    --warning: #FFB74D;
    --success: #66BB6A;
    --error: #EF5350;
    --glow: 0 0 30px rgba(224, 17, 95, 0.22), 0 0 10px rgba(230, 185, 58, 0.14);
    --panel-radius: 16px;
    --button-radius: 28px;
    --backdrop-blur: 8px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--bottom-bg);
    background-image: linear-gradient(to bottom, var(--top-bg), var(--bottom-bg));
    color: var(--text);
    min-height: 100vh;
    min-height: 100dvh;
    padding: 1.5rem;
    padding-top: max(1.5rem, env(safe-area-inset-top));
    padding-right: max(1.5rem, env(safe-area-inset-right));
    padding-bottom: max(1.5rem, env(safe-area-inset-bottom));
    padding-left: max(1.5rem, env(safe-area-inset-left));
    overflow-x: hidden;
    position: relative;
}

#background-assets { position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: -1; pointer-events: none; opacity: 0.4; }

header { text-align: center; margin-bottom: 2rem; }
.main-title { font-size: clamp(2rem, 8vw, 3.5rem); font-weight: 900; text-transform: uppercase; letter-spacing: 2px; color: #FFF4D2; text-shadow: 0 0 20px var(--ruby), 0 0 5px var(--gold); margin-bottom: 0.5rem; }
.subtitle { font-size: 0.9rem; color: var(--sapphire-bright); letter-spacing: 1px; text-transform: uppercase; opacity: 0.95; }
.header-actions { margin-top: 15px; display: flex; gap: 10px; justify-content: center; }

main { max-width: 600px; margin: 0 auto; padding-bottom: 4rem; }
.view { display: none; }
.view.active { display: block; animation: fadeIn 0.4s ease-out; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

.panel { background: var(--panel-bg); border: 1px solid var(--gold-dim); border-radius: var(--panel-radius); padding: 1.5rem; margin-bottom: 1.5rem; box-shadow: var(--glow); backdrop-filter: blur(var(--backdrop-blur)); -webkit-backdrop-filter: blur(var(--backdrop-blur)); }
.panel-title { font-size: 1.1rem; color: #FFF4D2; text-transform: uppercase; letter-spacing: 1px; margin-bottom: 1rem; border-bottom: 1px solid rgba(230, 185, 58, 0.3); padding-bottom: 0.5rem; }

.form-field { margin-bottom: 1.25rem; }
label { display: block; font-size: 0.85rem; color: var(--muted-text); margin-bottom: 0.5rem; text-transform: uppercase; letter-spacing: 0.5px; }
/* 16px keeps iPhones from zooming the page when a field is focused */
input[type="text"], textarea { width: 100%; background: rgba(3, 8, 26, 0.65); border: 1px solid var(--gold-dim); border-radius: 8px; padding: 0.75rem; color: #FFFFFF; font-family: inherit; font-size: 16px; transition: all 0.3s ease; }
input:focus, textarea:focus { outline: none; border-color: var(--sapphire-bright); box-shadow: 0 0 10px rgba(110, 168, 255, 0.35); }
textarea { min-height: 100px; resize: vertical; }

.actions-row { display: flex; gap: 1rem; justify-content: center; margin-top: 1.5rem; flex-wrap: wrap; }
.actions-row.tight { margin-top: 0; margin-bottom: 1rem; gap: 0.75rem; }
.btn { border: none; border-radius: var(--button-radius); padding: 0.75rem 1.5rem; font-family: inherit; font-size: 0.9rem; font-weight: 700; text-transform: uppercase; letter-spacing: 1px; cursor: pointer; transition: all 0.3s ease; display: inline-flex; align-items: center; justify-content: center; -webkit-tap-highlight-color: transparent; }
.btn-primary_ruby { background: var(--ruby); color: #FFFFFF; box-shadow: 0 0 15px rgba(224, 17, 95, 0.45); }
.btn-outline_sapphire { background: transparent; border: 2px solid var(--sapphire-bright); color: var(--sapphire-bright); }
.btn-outline_ruby { background: transparent; border: 2px solid var(--ruby-bright); color: var(--ruby-bright); }
.btn:disabled { opacity: 0.35; cursor: not-allowed; box-shadow: none; }
.btn:focus-visible, .history-group-toggle:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }
.hidden { display: none !important; }

.muted-note { color: var(--muted-text); font-size: 0.85rem; line-height: 1.5; }
.muted-note.center { text-align: center; }
.muted-note.error { color: var(--error); }
.field-hint { margin-top: 0.4rem; }

.status-banner { max-width: 600px; margin: -0.75rem auto 1.25rem; padding: 0.75rem 1rem; border: 1px solid var(--warning); border-radius: 12px; background: rgba(255, 183, 77, 0.12); color: var(--warning); font-size: 0.9rem; line-height: 1.45; text-align: center; }

/* Modal */
.modal-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(2, 5, 18, 0.9); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); display: flex; justify-content: center; align-items: center; z-index: 2000; padding: 1rem; overflow-y: auto; }
.modal-content { width: 92%; max-width: 500px; margin-bottom: 0; border: 2px solid var(--ruby); box-shadow: 0 0 30px rgba(224, 17, 95, 0.3); }
.modal-title { color: var(--sapphire-bright); }

/* History drawer */
#history-drawer { position: fixed; top: 0; right: 0; width: min(340px, 92vw); height: 100%; z-index: 1000; margin: 0; padding-top: max(1.5rem, env(safe-area-inset-top)); border-radius: 0; border-left: 2px solid var(--ruby); background: rgba(3, 8, 26, 0.97); overflow-y: auto; overscroll-behavior: contain; transition: transform 0.3s ease, visibility 0s; }
#history-drawer.hidden { display: block !important; transform: translateX(105%); visibility: hidden; pointer-events: none; transition: transform 0.3s ease, visibility 0s linear 0.3s; }
#history-backdrop { position: fixed; inset: 0; z-index: 900; background: rgba(0, 0, 0, 0.55); }

#history-list .panel { padding: 1rem; margin-bottom: 1rem; }
.history-item h3 { color: var(--ruby-bright); font-size: 1rem; line-height: 1.35; overflow-wrap: anywhere; }
.history-item small, .history-part small { display: block; color: var(--muted-text); font-size: 0.75rem; font-weight: 400; letter-spacing: 0; text-transform: none; margin-top: 2px; }
.history-item .btn { margin-top: 10px; }
.history-group { border-color: var(--sapphire-bright); }
.history-group-toggle { width: 100%; display: flex; justify-content: space-between; align-items: center; gap: 0.5rem; background: none; border: none; color: var(--sapphire-bright); font-family: inherit; font-size: 1rem; font-weight: 700; text-align: left; cursor: pointer; overflow-wrap: anywhere; }
.history-group-toggle::after { content: '▾'; transition: transform 0.2s ease; }
.history-group-toggle[aria-expanded="true"]::after { transform: rotate(180deg); }
.history-parts { margin-top: 10px; display: flex; flex-direction: column; gap: 8px; }
.history-part { flex-direction: column; align-items: flex-start; text-align: left; text-transform: none; letter-spacing: 0; font-size: 0.9rem; overflow-wrap: anywhere; }

/* Loading */
#loading-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(2, 5, 18, 0.92); z-index: 3000; display: flex; flex-direction: column; justify-content: center; align-items: center; padding: 1.5rem; text-align: center; }
.spinner { width: 60px; height: 60px; border: 5px solid rgba(230, 185, 58, 0.28); border-top-color: var(--ruby-bright); border-radius: 50%; animation: spin 1s linear infinite; margin-bottom: 1rem; }
@keyframes spin { to { transform: rotate(360deg); } }
#loading-detail { color: var(--sapphire-bright); font-size: 0.9rem; margin-top: 0.5rem; min-height: 1.3em; }
#loading-cancel { margin-top: 1.5rem; }

/* Results */
.story-title { text-align: center; color: var(--ruby-bright); margin-bottom: 0.5rem; text-shadow: 0 0 10px rgba(224, 17, 95, 0.5); overflow-wrap: anywhere; }
#story-meta { margin-bottom: 1rem; }
#lore-status { min-height: 1.3em; margin-top: -0.5rem; }
.story-panel { padding: 1.5rem; line-height: 1.8; font-size: 1.1rem; color: #F2F5FF; background: rgba(0, 0, 0, 0.3); overflow-wrap: anywhere; }
.story-panel p { margin: 0 0 1em; }
.story-panel p:last-child { margin-bottom: 0; }
.scene-break { text-align: center; color: var(--gold); letter-spacing: 0.5em; margin: 1.25em 0; line-height: 1; }
.scene-break::before { content: '✦ ✦ ✦'; }
.pagination-controls { display: flex; justify-content: space-between; align-items: center; margin-top: 1rem; gap: 0.5rem; }
.pagination-controls .page-label { color: var(--muted-text); font-size: 0.9rem; text-align: center; }

/* Toasts */
#toast-region { position: fixed; left: 50%; transform: translateX(-50%); bottom: max(1rem, env(safe-area-inset-bottom)); width: min(92vw, 460px); display: flex; flex-direction: column; gap: 0.5rem; z-index: 4000; pointer-events: none; }
.toast { pointer-events: auto; padding: 0.8rem 1rem; border-radius: 12px; background: rgba(5, 11, 30, 0.97); border: 1px solid var(--gold-dim); border-left-width: 4px; color: var(--text); font-size: 0.95rem; line-height: 1.4; box-shadow: 0 6px 24px rgba(0, 0, 0, 0.5); animation: fadeIn 0.25s ease-out; overflow-wrap: anywhere; }
.toast-error { border-color: var(--error); }
.toast-warning { border-color: var(--warning); }
.toast-success { border-color: var(--success); }

@media (prefers-reduced-motion: reduce) {
    .view.active, .toast { animation: none; }
    #history-drawer, #history-drawer.hidden { transition: none; }
}

@media (max-width: 380px) {
    body { padding-left: 1rem; padding-right: 1rem; }
    .panel { padding: 1.1rem; }
    .btn { padding: 0.7rem 1.1rem; }
}
