﻿/* ========= Theme tokens ========= */
:root {
    /* Parchment theme */
    --bg: #f7f2e7;
    --fg: #2b2111;
    --muted: #6a5b47;
    --panel: #fffdf7;
    --border: #d9cbb3;
    --accent: #8a5a1f;
    --radius: 12px;
    --shadow: 0 6px 18px rgba(75, 50, 20, .12);
    /* Density */
    --font-base: 16px;
    --button-fs: .9rem;
    --button-pad-y: .35rem;
    --button-pad-x: .6rem;
    /* Mobile panel heights */
    --map-min-h: 24vh;
    --log-min-h: 40vh;
    /* Map height (used by #campaignMap; overridden on mobile) */
    --map-h: 280px;
}

* {
    box-sizing: border-box
}

html, body {
    height: 100%
}

    /* ======== Parchment base ======== */
    body.theme-parchment {
        margin: 0;
        color: var(--fg);
        background: var(--bg);
        font: var(--font-base)/1.5 "EB Garamond", Georgia, "Times New Roman", serif;
        background-image: radial-gradient(1200px 600px at 20% -10%, rgba(138,90,31,.06), transparent 60%), radial-gradient(1000px 600px at 110% 10%, rgba(138,90,31,.05), transparent 55%), repeating-linear-gradient(0deg, rgba(120,90,50,.02), rgba(120,90,50,.02) 2px, transparent 2px, transparent 4px);
        background-attachment: fixed;
    }

/* ======== Top bar ======== */
.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 14px;
    border-bottom: 1px solid var(--border);
    background: linear-gradient(180deg, rgba(255,255,255,.6), rgba(255,255,255,.2));
}

    .topbar h1 {
        margin: 0;
        font-family: "IM Fell English", "EB Garamond", serif;
        font-size: 20px;
        letter-spacing: .2px;
        color: var(--fg);
    }

#stats {
    font-size: .95rem;
    color: var(--muted)
}

/* ======== Layout ======== */
.layout {
    display: grid;
    gap: 12px;
    padding: 12px;
    max-width: 1200px;
    margin: 0 auto;
    grid-template-columns: 1.3fr .7fr;
}

.control-slot {
    position: relative;
}
    /* Orders overlay lives *on top* of the same card area */
    .control-slot #orders {
        position: absolute;
        inset: 0;
        display: none;
        padding: 12px;
        border-radius: inherit;
        background: var(--paper, #f7f2e7);
        z-index: 2;
    }

    .control-slot #actionsPanel {
        position: relative;
        z-index: 1;
    }

/* Hide the rarely-used Reduce button on small screens */
@media (max-width: 760px) {
    #btnReduce {
        display: none;
    }
}

/* Buttons */
button:disabled {
    opacity: .55;
    cursor: not-allowed;
    filter: grayscale(.3);
}

/* Panels */
.panel {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow), inset 0 0 120px rgba(139,69,19,.06);
    padding: 12px;
}

/* ======== Log / narrative ======== */
#log {
    height: auto;
    max-height: 52vh; /* base height; overridden on mobile by --log-min-h */
    overflow: auto;
    padding-right: 6px;
    position: relative; /* quill positioning context */
    -webkit-overflow-scrolling: touch;
}

#logBody {
    display: flex;
    flex-direction: column;
    gap: .5rem;
}

    #logBody p {
        margin: .2rem 0
    }

    #logBody .day-h {
        font-weight: 600;
        margin: .6rem 0;
        font-family: "IM Fell English","EB Garamond",serif;
        letter-spacing: .15px;
    }

.day-sep {
    border: none;
    border-top: 1px solid var(--border);
    margin: .5rem 0;
}

#logBody strong {
    color: #4b2f12
}

/* ======== Orders panel ======== */
#orders {
    margin-top: .5rem
}

    #orders .title {
        font-weight: 600;
        margin-right: .5rem
    }

    #orders .hint {
        display: block;
        margin-top: .25rem;
        color: var(--muted)
    }

    #orders[hidden] {
        display: none
    }

/* ======== Map pane ======== */
#mapPane {
    display: block;
    min-height: 200px
}

#mapMount {
    height: 100%;
    min-height: 180px;
    border: 1px dashed var(--border);
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--muted);
    background: radial-gradient(800px 400px at 50% -10%, rgba(138,90,31,.04), transparent 60%), linear-gradient(180deg, rgba(255,255,240,.5), rgba(255,255,255,.5));
}

/* Let the map handle its own gestures (prevents page pinch/scroll while over the map) */
#campaignMap {
    touch-action: pan-x pan-y pinch-zoom;
}

/* ======== Controls (compact) ======== */
.controls {
    position: sticky;
    bottom: 0;
    padding: 10px 12px;
    display: grid;
    gap: 8px;
    background: rgba(255, 250, 240, .85);
    backdrop-filter: blur(6px);
    border-top: 1px solid var(--border);
}

    .controls .row {
        display: flex;
        gap: 8px;
        flex-wrap: wrap;
        align-items: center
    }

        .controls .row.buttons {
            display: grid;
            grid-template-columns: repeat(5, minmax(0,1fr));
            gap: 6px;
        }

/* UNIFIED BUTTON STYLE ACROSS THE APP (not just inside .controls) */
.app button, .app .btn {
    -webkit-appearance: none;
    appearance: none;
    padding: var(--button-pad-y) var(--button-pad-x);
    border-radius: 8px;
    border: 1px solid var(--border);
    background: #f6efe0;
    color: var(--fg);
    cursor: pointer;
    font-size: var(--button-fs);
    font-weight: 600;
    box-shadow: 0 1px 0 #e4d7c3, inset 0 0 0 9999px rgba(255,255,255,.25);
}

    .app button:hover, .app .btn:hover {
        background: #fff6e9;
    }

    .app button.disabled, .app button:disabled {
        opacity: .6;
        cursor: not-allowed
    }

label {
    color: var(--muted);
    font-size: .95rem
}

select, input[type="number"] {
    background: #fffaf0;
    border: 1px solid var(--border);
    color: var(--fg);
    padding: .35rem .5rem;
    border-radius: 8px;
    min-height: 2rem;
}

/* Hide actions when an overlay/endcard is up */
#actionsPanel.hidden {
    display: none
}

/* End-card container uses the same slot as Orders */
#orders.endcard {
    display: block;
    padding: 0;
}

.endcard__frame {
    background: var(--paper, #f6f1e3);
    border: 1px solid rgba(0,0,0,.15);
    box-shadow: inset 0 0 0 2px rgba(0,0,0,.05);
    padding: 12px;
}

.endcard__title {
    margin: 0 0 .35rem 0;
    font-family: "IM Fell English", "EB Garamond", serif;
    font-size: 1.15rem;
    letter-spacing: .2px;
}

.endcard__figure {
    margin: .25rem 0 0;
}

.endcard__art {
    display: block;
    width: 100%;
    height: auto;
    border: 1px solid rgba(0,0,0,.12);
    background: #e8e1cf;
}

    .endcard__art.is-sepia {
        filter: grayscale(.8) sepia(.65) contrast(1.1) brightness(.98) saturate(.75);
    }

.endcard__figure {
    position: relative;
}

    .endcard__figure::after {
        content: "";
        position: absolute;
        inset: 0;
        background-image: linear-gradient(135deg, rgba(0,0,0,.06) 25%, transparent 25%, transparent 50%, rgba(0,0,0,.06) 50%, rgba(0,0,0,.06) 75%, transparent 75%, transparent);
        background-size: 8px 8px;
        pointer-events: none;
        mix-blend-mode: multiply;
    }

.endcard__caption {
    font: italic .92rem/1.3 "EB Garamond", serif;
    color: rgba(0,0,0,.6);
    text-align: center;
    margin-top: .35rem;
}

.endcard__text {
    margin-top: .5rem;
    font-size: .95rem
}

/* ======== Mobile layout ======== */
@media (max-width: 900px) {
    .layout {
        grid-template-columns: 1fr
    }

    #mapPane {
        min-height: var(--map-min-h)
    }
    /* denser controls on small screens */
    :root {
        --button-fs: .85rem;
        --button-pad-y: .32rem;
        --button-pad-x: .5rem;
    }
}

@media (max-width: 560px) {
    body {
        touch-action: none;
    }

    #log {
        max-height: 30dvh;
    }
    /* tighter controls */
    .controls {
        gap: 6px;
        padding: 8px 10px
    }

        .controls .row {
            gap: 6px
        }

            .controls .row.buttons {
                grid-template-columns: repeat(5, 1fr);
                gap: 5px
            }

    select, input[type="number"] {
        padding: .3rem .45rem;
        font-size: .95rem
    }

    #stats {
        font-size: .9rem
    }

    /* shrink map & log on phones */
    :root {
        --map-h: 238px; /* 280 * 0.85 (map -15%) */
        --map-min-h: 20vh;
        --log-min-h: 32vh; /* 40vh * 0.8 (log -20%) */
    }
}

/* ——— Desktop layout fixes ——— */
@media (min-width: 1024px) {
    .app {
        max-width: 1200px;
        margin: 0 auto;
        padding: 8px 12px 24px;
    }

    /* Two-column grid: journal (flexible) + a neat side rail */
    main.layout {
        display: grid;
        grid-template-columns: minmax(560px, 1fr) 420px;
        gap: 16px 20px;
        align-items: start;
    }

    /* Journal takes the whole left column and gets tall */
    .journal {
        grid-column: 1 / 2;
        min-height: calc(100vh - 180px);
    }

    /* Right column stacks cards and stays visible while scrolling */
    .side {
        grid-column: 2 / 3;
        display: grid;
        gap: 16px;
        grid-auto-rows: max-content;
        position: sticky;
        top: calc(env(safe-area-inset-top, 0) + 64px);
        align-self: start;
    }

    /* Make the plan row give the destination select room */
    #actionsPanel .row--plan {
        display: grid;
        grid-template-columns: 1fr auto;
        gap: .5rem .75rem;
    }

    #actionsPanel .field.small input {
        width: 6rem;
    }

    #destSelect {
        width: 100%;
    }

    /* Tidy action buttons */
    #actionsPanel .row--actions {
        display: grid;
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: .5rem;
    }

    #btnFortify {
        grid-column: span 1;
    }

    details.advanced .row--more {
        display: flex;
        gap: .75rem;
        align-items: center;
        flex-wrap: wrap;
    }

    /* Map gets a fixed, sensible height on PC */
    #campaignMap {
        width: 100%;
        height: 320px;
        display: block;
    }
}

/* Make panes visually distinct on large screens too */
@media (min-width: 1024px) {
    .card {
        border: 1px solid rgba(0,0,0,.12);
        box-shadow: 0 1px 0 rgba(0,0,0,.04);
    }

    .pane-title {
        font-variant: small-caps;
        letter-spacing: .03em;
        margin: -6px -6px 8px -6px;
        padding: 6px 8px;
        background: rgba(0,0,0,.04);
        border-bottom: 1px solid rgba(0,0,0,.08);
        text-align: center;
    }

    /* Give the journal its own surface so it does not visually merge with the map */
    .journal {
        background: var(--paper, #f7f1e3);
        border: 1px solid rgba(0,0,0,.12);
        border-radius: 6px;
        padding: 10px;
    }

    /* Ensure the #logBody scrolls nicely inside the journal */
    #log {
        max-height: calc(100vh - 220px);
    }

    #logBody {
        max-height: calc(100vh - 220px);
        overflow: auto;
        padding-right: 6px;
    }
}

/* Map palette */
:root {
    --map-sea: #c5d6e3;
    --map-land: #ead9b6;
    --map-stroke: #684b2e;
    --map-ink: #2c1c10;
    --map-trail: #5b3b24;
}

#mapPane {
    margin-top: .75rem;
}

/* Drive map height by variable (overridden on desktop to 320px) */
#campaignMap {
    width: 100%;
    height: var(--map-h, 280px);
    display: block;
    border: 1px solid var(--ink-weak);
    border-radius: 8px;
    background: var(--map-sea);
}

/* Background */
.map-sea {
    fill: var(--map-sea);
    stroke: none;
}

.map-land {
    fill: var(--map-land);
    stroke: var(--map-stroke);
    stroke-width: .8;
}

/* Places */
.map-node {
    fill: var(--map-ink);
    opacity: .85;
    cursor: pointer;
}

    .map-node.city {
        fill: var(--map-ink);
    }

    .map-node.majorTown {
        opacity: .9;
    }

    .map-node.town {
        opacity: .8;
    }

    .map-node.village {
        opacity: .7;
    }

/* Army marker */
.army-dot {
    fill: none;
    stroke: var(--map-ink);
    stroke-width: 1.5;
}

/* Movement + ranges */
.range-disc {
    vector-effect: non-scaling-stroke;
    color: #b21a1a;
}

.move-range {
    fill: rgba(160, 0, 0, 0.08);
    stroke: rgba(160, 0, 0, 0.35);
    stroke-width: 0.6;
    pointer-events: none;
}

.march-leg {
    stroke: #9d1c1c;
    stroke-width: 1.2;
    fill: none;
}

/* Slightly smaller, denser buttons for desktop to keep things compact */
@media (min-width: 1024px) {
    .app button, .app .btn {
        padding: .45rem .6rem;
        font-size: .95rem;
        line-height: 1.1;
    }

    .side {
        grid-column: 2 / 3;
        display: grid;
        gap: 0;
        grid-auto-rows: max-content;
        position: sticky;
        top: 0;
        align-self: start;
    }

    #mapPane {
        margin-top: 0;
        padding-top: 0;
    }
}

/* ======== Journal styling & quill leader ======== */

/* Cursive vibe for the journal */
#logBody {
    font-family: "Caveat", "IM Fell English", "EB Garamond", serif;
    font-size: 1.4rem;
    line-height: 1.55;
    letter-spacing: .01em;
}

    #logBody .day-h {
        font-family: "IM Fell English","EB Garamond",serif;
    }

/* Quill cursor for typing effect */
.quill-leader {
    position: absolute;
    z-index: 3;
    width: 20px;
    height: 20px;
    pointer-events: none;
    opacity: 0;
    transition: opacity .18s ease;
}

    .quill-leader svg {
        width: 100%;
        height: 100%;
        display: block;
    }

.log-caret {
    display: inline-block;
    width: 0;
    height: 1em;
}

@media (max-width:560px) {
    .quill-leader {
        width: 18px;
        height: 18px;
    }
}

/* Log scrolling stability */
#log {
    overflow-anchor: none; /* stop browser from auto-shifting while we append */
    overscroll-behavior-y: contain; /* avoid bounce during programmatic scroll (mobile) */
    padding-bottom: 10px; /* keeps last line from kissing the edge */
}
