/* ══════════════════════════════════════════════════════════════════════════
   The Wandering Ledger — Web GUI Theme
   Medieval parchment / dark wood aesthetic
   ══════════════════════════════════════════════════════════════════════════ */

@import url('https://fonts.googleapis.com/css2?family=Cinzel:wght@400;700&family=Crimson+Text:ital,wght@0,400;0,600;1,400&family=JetBrains+Mono:wght@300;400;500&display=swap');

:root {
    --parchment:     #d2b98b;
    --parchment-dk:  #c4a870;
    --wood-dark:     #2a1e0e;
    --wood-mid:      #3a2810;
    --wood-light:    #4a3820;
    --gold:          #c89b14;
    --gold-bright:   #e6be32;
    --gold-dim:      #8b6b3a;
    --text-dark:     #1a1008;
    --text-light:    #e8dcc8;
    --border-ornate: #8b6b3a;
    --red:           #c44040;
    --green:         #5a9a3a;
    --blue:          #4a8ab0;
    --violet:        #8a6ac0;
    --teal:          #3a8a8a;
    --orange:        #c07030;
    --cyan:          #40b0c0;
    --brown:         #8a6a40;
    --pink:          #c070a0;
    --lime:          #70c040;
    --silver:        #a8a8a8;
    --cream:         #f0e8d0;
    --amber:         #d09030;
    --indigo:        #6040c0;
}

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

html, body {
    height: 100%;
    overflow: hidden;
    background: var(--wood-dark);
    color: var(--text-light);
    font-family: 'Crimson Text', Georgia, serif;
    font-size: 16px;
}

/* ── Layout ─────────────────────────────────────────────────────────────── */

#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

/* ── HUD (top bar) ──────────────────────────────────────────────────────── */

#hud {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 20px;
    background: linear-gradient(180deg, var(--wood-mid), var(--wood-dark));
    border-bottom: 2px solid var(--gold-dim);
    min-height: 48px;
    flex-shrink: 0;
}

#hud .title {
    font-family: 'Cinzel', serif;
    font-weight: 700;
    font-size: 1.1rem;
    color: var(--gold-bright);
    letter-spacing: 2px;
    text-transform: uppercase;
}

#hud .stats {
    display: flex;
    gap: 24px;
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.82rem;
    color: var(--text-light);
}

#hud .stats span {
    opacity: 0.85;
}

#hud .stats .label {
    color: var(--gold);
    font-weight: 500;
}

#hud .stats .value {
    color: var(--cream);
}

/* ── Main area (narrative + action panel) ───────────────────────────────── */

#main {
    display: flex;
    flex: 1;
    min-height: 0;
    overflow: hidden;
}

/* ── Narrative Log ──────────────────────────────────────────────────────── */

#narrative {
    flex: 7;
    overflow-y: auto;
    overflow-x: auto;
    padding: 16px 20px;
    background:
        linear-gradient(180deg, rgba(42, 30, 14, 0.97), rgba(58, 40, 16, 0.95)),
        repeating-linear-gradient(
            0deg,
            transparent,
            transparent 28px,
            rgba(139, 107, 58, 0.04) 28px,
            rgba(139, 107, 58, 0.04) 29px
        );
    font-family: 'JetBrains Mono', 'Cascadia Code', 'Consolas', 'DejaVu Sans Mono', monospace;
    font-size: 0.85rem;
    line-height: 1.55;
    border-right: 2px solid var(--gold-dim);
    scroll-behavior: smooth;
}

#narrative .line {
    white-space: pre-wrap;
    word-break: break-word;
    overflow-wrap: break-word;
    padding: 1px 0;
}

#narrative .divider {
    text-align: center;
    padding: 8px 0;
    color: var(--gold-dim);
    font-size: 0.9rem;
    letter-spacing: 6px;
    opacity: 0.7;
}

/* ── Action Panel ───────────────────────────────────────────────────────── */

#actions {
    flex: 3;
    display: flex;
    flex-direction: column;
    background: linear-gradient(180deg, var(--wood-mid), var(--wood-dark));
    padding: 16px;
    overflow-y: auto;
    min-width: 280px;
    max-width: 380px;
}

#action-prompt {
    font-family: 'Cinzel', serif;
    font-size: 0.95rem;
    color: var(--gold-bright);
    margin-bottom: 12px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--gold-dim);
}

#action-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

/* Menu buttons */
.menu-btn {
    display: block;
    width: 100%;
    padding: 10px 14px;
    background: linear-gradient(135deg, var(--parchment), var(--parchment-dk));
    color: var(--text-dark);
    border: 1px solid var(--border-ornate);
    border-radius: 4px;
    font-family: 'Crimson Text', serif;
    font-size: 0.95rem;
    text-align: left;
    cursor: pointer;
    transition: all 0.15s ease;
    position: relative;
    overflow: hidden;
}

.menu-btn:hover {
    border-color: var(--gold);
    padding-left: 20px;
    background: linear-gradient(135deg, #dcc494, var(--parchment));
    box-shadow: 0 0 8px rgba(200, 155, 20, 0.3);
}

.menu-btn:active {
    transform: scale(0.98);
}

.menu-btn .num {
    font-family: 'JetBrains Mono', monospace;
    font-weight: 500;
    color: var(--gold-dim);
    margin-right: 8px;
}

.menu-btn.selected {
    border-color: var(--gold-bright);
    box-shadow: 0 0 12px rgba(200, 155, 20, 0.5);
}

/* Yes/No buttons */
.yn-group {
    display: flex;
    gap: 10px;
}

.yn-btn {
    flex: 1;
    padding: 12px;
    font-family: 'Cinzel', serif;
    font-size: 1rem;
    border: 2px solid var(--border-ornate);
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.15s ease;
}

.yn-btn.yes {
    background: linear-gradient(135deg, #4a6a30, #3a5a20);
    color: var(--cream);
}

.yn-btn.no {
    background: linear-gradient(135deg, #6a3030, #5a2020);
    color: var(--cream);
}

.yn-btn:hover {
    border-color: var(--gold-bright);
    box-shadow: 0 0 10px rgba(200, 155, 20, 0.4);
}

.yn-btn .key-hint {
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.7rem;
    opacity: 0.6;
    display: block;
    margin-top: 4px;
}

/* Text input */
.text-input-wrap {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.text-btn-row {
    display: flex;
    gap: 8px;
}

.text-btn-row .send-btn {
    flex: 2;
}

.text-btn-row .back-btn {
    flex: 1;
}

/* Voice input mic button */
.mic-btn {
    flex: 1;
    padding: 10px;
    background: linear-gradient(135deg, var(--wood-light), var(--wood-mid));
    color: var(--gold-bright);
    border: 1px solid var(--gold-dim);
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.15s;
    font-size: 1.1rem;
}

.mic-btn:hover {
    border-color: var(--gold-bright);
    box-shadow: 0 0 8px rgba(200, 155, 20, 0.3);
}

.mic-btn.recording {
    border-color: var(--gold-bright);
    box-shadow: 0 0 12px rgba(200, 155, 20, 0.5);
    animation: mic-pulse 0.8s ease-in-out infinite;
}

@keyframes mic-pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.08); }
}

/* TTS controls in HUD */
.tts-controls {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-left: auto;
    padding: 0 6px;
}
.tts-btn {
    background: none;
    border: 1px solid var(--gold-dim);
    color: var(--gold-bright);
    font-size: 1.1rem;
    cursor: pointer;
    padding: 3px 7px;
    border-radius: 4px;
    transition: all 0.15s;
    line-height: 1;
}
.tts-btn:hover {
    border-color: var(--gold-bright);
    box-shadow: 0 0 6px rgba(200, 155, 20, 0.3);
}
.tts-btn.active {
    border-color: var(--gold-bright);
    box-shadow: 0 0 6px rgba(200, 155, 20, 0.4);
}
.tts-panel {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.75rem;
    color: var(--gold-dim);
}
.tts-panel label {
    margin: 0;
    font-size: 0.75rem;
}
.tts-panel input[type="range"] {
    width: 70px;
    accent-color: var(--gold-bright);
    cursor: pointer;
}
.tts-panel .hidden {
    display: none;
}

.text-input {
    width: 100%;
    padding: 10px 12px;
    background: var(--parchment);
    color: var(--text-dark);
    border: 2px solid var(--border-ornate);
    border-radius: 4px;
    font-family: 'Crimson Text', serif;
    font-size: 1rem;
    outline: none;
    transition: border-color 0.15s;
}

.text-input:focus {
    border-color: var(--gold-bright);
    box-shadow: 0 0 8px rgba(200, 155, 20, 0.3);
}

.send-btn {
    padding: 10px;
    background: linear-gradient(135deg, var(--wood-light), var(--wood-mid));
    color: var(--gold-bright);
    border: 1px solid var(--gold-dim);
    border-radius: 4px;
    font-family: 'Cinzel', serif;
    cursor: pointer;
    transition: all 0.15s;
}

.send-btn:hover {
    border-color: var(--gold-bright);
    box-shadow: 0 0 8px rgba(200, 155, 20, 0.3);
}

/* Number input */
.int-input-wrap {
    display: flex;
    gap: 8px;
    align-items: center;
}

.int-input {
    width: 100px;
    padding: 10px;
    background: var(--parchment);
    color: var(--text-dark);
    border: 2px solid var(--border-ornate);
    border-radius: 4px;
    font-family: 'JetBrains Mono', monospace;
    font-size: 1rem;
    text-align: center;
    outline: none;
}

.int-input:focus {
    border-color: var(--gold-bright);
}

.range-hint {
    color: var(--silver);
    font-size: 0.8rem;
    font-family: 'JetBrains Mono', monospace;
}

.back-btn {
    padding: 8px 14px;
    background: linear-gradient(135deg, #4a3a28, #3a2a18);
    color: var(--silver);
    border: 1px solid var(--gold-dim);
    border-radius: 4px;
    font-family: 'Crimson Text', serif;
    cursor: pointer;
    transition: all 0.15s;
}

.back-btn:hover {
    color: var(--gold-bright);
    border-color: var(--gold);
}

/* Pause button */
.pause-btn {
    padding: 14px;
    background: linear-gradient(135deg, var(--wood-light), var(--wood-mid));
    color: var(--gold-bright);
    border: 2px solid var(--gold-dim);
    border-radius: 4px;
    font-family: 'Cinzel', serif;
    font-size: 1rem;
    cursor: pointer;
    animation: pulse-border 2s infinite;
}

@keyframes pulse-border {
    0%, 100% { border-color: var(--gold-dim); }
    50% { border-color: var(--gold-bright); box-shadow: 0 0 12px rgba(200, 155, 20, 0.4); }
}

.pause-btn:hover {
    animation: none;
    border-color: var(--gold-bright);
    box-shadow: 0 0 12px rgba(200, 155, 20, 0.5);
}

/* ── NPC Portrait ───────────────────────────────────────────────────────── */

#portrait-area {
    text-align: center;
    margin-bottom: 12px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--gold-dim);
    transition: opacity 0.4s ease;
}

#portrait-area.hidden {
    display: none;
}

#portrait-img {
    width: 160px;
    height: 160px;
    object-fit: cover;
    border: 3px solid var(--gold-dim);
    border-radius: 8px;
    background: var(--wood-light);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}

#portrait-img.loading {
    opacity: 0.4;
    animation: portrait-pulse 1.5s ease-in-out infinite;
}

@keyframes portrait-pulse {
    0%, 100% { opacity: 0.4; }
    50% { opacity: 0.7; }
}

#portrait-name {
    font-family: 'Cinzel', serif;
    font-size: 0.95rem;
    color: var(--gold-bright);
    margin-top: 6px;
    font-weight: 700;
}

#portrait-archetype {
    font-family: 'Crimson Text', serif;
    font-size: 0.8rem;
    color: var(--silver);
    font-style: italic;
}

/* ── Portrait Gallery (group intimate scenes) ─────────────────────────── */

#portrait-gallery {
    display: flex;
    justify-content: center;
    gap: 10px;
    margin-bottom: 12px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--gold-dim);
    transition: opacity 0.4s ease;
    flex-wrap: wrap;
}

#portrait-gallery.hidden {
    display: none;
}

.gallery-slot {
    text-align: center;
    flex: 0 0 auto;
}

.gallery-slot img {
    width: 120px;
    height: 120px;
    object-fit: cover;
    border: 2px solid var(--gold-dim);
    border-radius: 6px;
    background: var(--wood-light);
    box-shadow: 0 3px 12px rgba(0, 0, 0, 0.4);
    transition: border-color 0.3s ease, box-shadow 0.3s ease;
}

.gallery-slot.speaking img {
    border-color: var(--gold-bright);
    box-shadow: 0 0 12px rgba(212, 175, 55, 0.5);
}

.gallery-slot .gallery-name {
    font-family: 'Cinzel', serif;
    font-size: 0.75rem;
    color: var(--silver);
    margin-top: 4px;
    max-width: 120px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.gallery-slot.speaking .gallery-name {
    color: var(--gold-bright);
}

/* ── Scrollbar ──────────────────────────────────────────────────────────── */

#narrative::-webkit-scrollbar { width: 8px; }
#narrative::-webkit-scrollbar-track { background: var(--wood-dark); }
#narrative::-webkit-scrollbar-thumb {
    background: var(--gold-dim);
    border-radius: 4px;
}
#narrative::-webkit-scrollbar-thumb:hover { background: var(--gold); }

#actions::-webkit-scrollbar { width: 6px; }
#actions::-webkit-scrollbar-track { background: transparent; }
#actions::-webkit-scrollbar-thumb {
    background: var(--gold-dim);
    border-radius: 3px;
}

/* ── ANSI color classes ─────────────────────────────────────────────────── */

.color-red     { color: #e05050; }
.color-green   { color: #70b050; }
.color-gold    { color: #e6be32; }
.color-amber   { color: #d09030; }
.color-blue    { color: #60a0d0; }
.color-violet  { color: #a080e0; }
.color-silver  { color: #b0b0b0; }
.color-teal    { color: #50b0b0; }
.color-orange  { color: #d08040; }
.color-cyan    { color: #50c0d0; }
.color-brown   { color: #a08050; }
.color-pink    { color: #d080b0; }
.color-lime    { color: #90d050; }
.color-indigo  { color: #7050d0; }
.color-cream   { color: #f0e8d0; }

.ansi-bold     { font-weight: 700; }
.ansi-dim      { opacity: 0.6; }
.ansi-italic   { font-style: italic; }
.ansi-blink    { animation: blink 1.2s step-end infinite; }
@keyframes blink { 50% { opacity: 0; } }

/* Backgrounds */
.bg-52  { background: rgba(120, 20, 20, 0.4); }
.bg-58  { background: rgba(100, 80, 20, 0.4); }
.bg-235 { background: rgba(40, 40, 40, 0.6); }
.bg-17  { background: rgba(10, 10, 60, 0.4); }

/* ── Responsive ─────────────────────────────────────────────────────────── */

@media (max-width: 768px) {
    #app {
        height: 100vh;
        height: 100dvh;
    }
    #main { flex-direction: column; }
    #narrative {
        flex: 1;
        border-right: none;
        border-bottom: 2px solid var(--gold-dim);
    }
    #actions {
        max-width: 100%;
        min-width: auto;
        max-height: 40vh;
    }
    .menu-btn { min-height: 44px; }
}

/* ── Connecting overlay ─────────────────────────────────────────────────── */

#connecting {
    position: fixed;
    inset: 0;
    background: rgba(42, 30, 14, 0.95);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    gap: 16px;
    z-index: 100;
    font-family: 'Cinzel', serif;
    color: var(--gold-bright);
    font-size: 1.2rem;
    transition: opacity 0.5s;
}

#connecting.hidden {
    opacity: 0;
    pointer-events: none;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 3px solid var(--gold-dim);
    border-top-color: var(--gold-bright);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

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

/* ── Player echo (typed text shown in narrative) ──────────────────────── */

.player-echo {
    padding: 4px 8px;
    margin: 4px 0;
    border-left: 3px solid var(--gold);
    background: rgba(200, 155, 20, 0.08);
    font-family: 'Crimson Text', serif;
    font-size: 0.92rem;
    color: var(--cream);
}

.player-label {
    font-family: 'Cinzel', serif;
    font-weight: 700;
    color: var(--gold-bright);
    margin-right: 6px;
    font-size: 0.85rem;
}

/* ── Tablet supplement (augments existing 768px query) ─────────────────── */

@media (max-width: 768px) {

    #hud .stats {
        gap: 16px;
        font-size: 0.78rem;
    }

    #portrait-img {
        width: 140px;
        height: 140px;
    }

    .gallery-slot img {
        width: 100px;
        height: 100px;
    }

    .gallery-slot .gallery-name {
        max-width: 100px;
        font-size: 0.7rem;
    }

    .menu-btn {
        min-height: 44px;
    }
}

/* ── Phone (≤480px) — single scrollable stream ────────────────────────── */

@media (max-width: 480px) {

    /* App fills viewport exactly — #main becomes the scroll container */
    #app {
        height: 100vh;
        height: 100dvh;
        overflow: hidden;
    }

    /* ── HUD: compact, wrapping stats ── */
    #hud {
        flex-wrap: wrap;
        padding: 6px 10px;
        gap: 2px;
        min-height: auto;
    }

    #hud .title {
        width: 100%;
        text-align: center;
        font-size: 0.82rem;
        letter-spacing: 1px;
        padding-bottom: 3px;
        border-bottom: 1px solid var(--gold-dim);
    }

    #hud .stats {
        width: 100%;
        flex-wrap: wrap;
        justify-content: center;
        gap: 2px 10px;
        font-size: 0.7rem;
    }

    .tts-controls {
        width: 100%;
        justify-content: center;
        padding: 3px 0 0;
    }
    .tts-btn { font-size: 0.9rem; padding: 2px 6px; }
    .tts-panel input[type="range"] { width: 60px; }

    /* ── CORE: single scroll container fills remaining height ── */
    #main {
        display: block;
        flex: 1;
        min-height: 0;               /* allow flex child to shrink */
        overflow-y: auto;            /* THE scroll container */
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
    }

    #narrative {
        overflow: visible;           /* no independent scroll */
        border-bottom: none;
        padding: 8px;
        font-size: 0.75rem;
        line-height: 1.4;
    }

    #narrative .line {
        white-space: pre-wrap;
        word-break: break-word;
        overflow-wrap: break-word;
    }

    #actions {
        max-height: none;            /* no height limit — all buttons show */
        max-width: 100%;
        min-width: 0;
        padding: 8px;
        border-top: 2px solid var(--gold-dim);
    }

    /* ── Portrait: compact ── */
    #portrait-area {
        margin-bottom: 6px;
        padding-bottom: 6px;
    }

    #portrait-img {
        width: 90px;
        height: 90px;
        border-width: 2px;
    }

    #portrait-name  { font-size: 0.8rem; }
    #portrait-archetype { font-size: 0.7rem; }

    /* ── Gallery ── */
    #portrait-gallery { gap: 6px; margin-bottom: 6px; padding-bottom: 6px; }
    .gallery-slot img { width: 72px; height: 72px; border-width: 2px; }
    .gallery-slot .gallery-name { font-size: 0.65rem; max-width: 72px; }

    /* ── Buttons: touch-friendly ── */
    .menu-btn {
        padding: 12px 14px;
        min-height: 44px;
        font-size: 0.92rem;
    }
    .menu-btn .num { margin-right: 6px; }
    .menu-btn:hover { padding-left: 14px; }  /* disable hover shift */

    .yn-btn     { padding: 14px; min-height: 48px; font-size: 0.95rem; }
    .pause-btn  { padding: 14px; min-height: 48px; font-size: 0.95rem; }
    .send-btn   { padding: 12px; min-height: 44px; }
    .back-btn   { padding: 10px 14px; min-height: 44px; }

    /* ── Inputs: 16px prevents iOS auto-zoom ── */
    .text-input { font-size: 1rem; padding: 10px; }
    .int-input  { width: 80px; font-size: 1rem; padding: 10px 8px; }
    .int-input-wrap { flex-wrap: wrap; gap: 6px; }
    .range-hint { font-size: 0.75rem; }

    /* ── Misc ── */
    #action-prompt { font-size: 0.85rem; margin-bottom: 6px; padding-bottom: 4px; }
    #action-area { gap: 5px; }
    .player-echo { font-size: 0.82rem; padding: 3px 6px; }
    .player-label { font-size: 0.75rem; }
    #connecting { font-size: 1rem; }
    #narrative .divider { font-size: 0.8rem; letter-spacing: 3px; padding: 4px 0; }

}
