/* ============================================================
   MORAL FORGE — Realms of Dominion
   Aesthetic: medieval cartography. Aged vellum, oxidised brass,
   ox-blood crimson, ink-black, gold leaf accents. Display in
   engraved Cinzel; body in EB Garamond.
   ============================================================ */

:root {
    /* Parchment palette */
    --vellum-0:   #f4e8cf;   /* highlight */
    --vellum-1:   #e8d8b3;   /* surface */
    --vellum-2:   #d8c293;   /* surface darker */
    --vellum-3:   #b89b6a;   /* edge */

    /* Inks */
    --ink-1:      #1a0e07;
    --ink-2:      #3a230f;
    --ink-3:      #5a3a1f;

    /* Brand */
    --crimson:    #7a1d1d;
    --crimson-deep: #4a0f0f;
    --gold:       #b8893b;
    --gold-deep:  #8a6420;
    --moss:       #4a5d2a;
    --moss-deep:  #2f3d18;

    /* Resource colours */
    --c-wood:  #6a4525;
    --c-stone: #6b6358;
    --c-iron:  #4d4e57;
    --c-food:  #7a3a1a;
    --c-gold:  #a87826;
    --c-pop:   #3a4a2a;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    min-height: 100vh;
    font-family: 'EB Garamond', Georgia, serif;
    font-size: 16px;
    color: var(--ink-1);
    background-color: #2a1d10;
    background-image:
        radial-gradient(ellipse at 20% 10%, rgba(184,137,59,0.10), transparent 60%),
        radial-gradient(ellipse at 85% 90%, rgba(122,29,29,0.10), transparent 55%),
        repeating-linear-gradient(45deg, rgba(0,0,0,0.04) 0 2px, transparent 2px 4px),
        linear-gradient(135deg, #2a1d10, #1a1108);
    overflow-x: hidden;
}

button { font-family: inherit; cursor: pointer; }

.hidden { display: none !important; }

/* Parchment grain layered over everything */
.grain {
    position: fixed;
    inset: 0;
    pointer-events: none;
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='200' height='200'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0.12  0 0 0 0 0.08  0 0 0 0 0.04  0 0 0 0.20 0'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>");
    opacity: 0.55;
    mix-blend-mode: multiply;
    z-index: 1000;
}

/* ============================================================
   AUTH OVERLAY
   ============================================================ */
.auth-shell {
    position: fixed;
    inset: 0;
    display: grid;
    place-items: center;
    padding: 24px;
    z-index: 10;
}

.auth-card {
    width: min(440px, 100%);
    background:
        radial-gradient(ellipse at top, var(--vellum-0), var(--vellum-1) 70%, var(--vellum-2));
    color: var(--ink-1);
    padding: 38px 36px 30px;
    border: 1px solid var(--vellum-3);
    border-radius: 4px;
    text-align: center;
    box-shadow:
        0 0 0 1px rgba(184,137,59,0.4) inset,
        0 0 0 6px #1a0e07,
        0 0 0 7px var(--gold-deep),
        0 30px 80px rgba(0,0,0,0.6),
        0 12px 30px rgba(0,0,0,0.5);
    position: relative;
}
.auth-card::before, .auth-card::after {
    content: "";
    position: absolute;
    width: 24px; height: 24px;
    border: 1.5px solid var(--gold-deep);
}
.auth-card::before { top: 10px; left: 10px; border-right: none; border-bottom: none; }
.auth-card::after  { bottom: 10px; right: 10px; border-left: none; border-top: none; }

.auth-seal {
    color: var(--crimson);
    display: inline-block;
    margin-bottom: 6px;
    filter: drop-shadow(0 2px 0 rgba(0,0,0,0.2));
}

.auth-title {
    font-family: 'Cinzel', serif;
    font-weight: 900;
    font-size: 38px;
    letter-spacing: 0.06em;
    margin: 4px 0 0;
    color: var(--ink-1);
    text-shadow: 0 1px 0 rgba(255,255,255,0.3);
}
.auth-sub {
    font-family: 'Cinzel', serif;
    font-weight: 500;
    font-size: 13px;
    letter-spacing: 0.42em;
    text-transform: uppercase;
    margin: 4px 0 22px;
    color: var(--ink-3);
}

.auth-tabs {
    display: flex;
    gap: 4px;
    border-bottom: 1px solid var(--vellum-3);
    margin-bottom: 22px;
}
.auth-tabs .tab {
    flex: 1;
    background: transparent;
    border: none;
    padding: 10px 6px;
    font-family: 'Cinzel', serif;
    font-size: 11px;
    letter-spacing: 0.22em;
    text-transform: uppercase;
    color: var(--ink-3);
    border-bottom: 2px solid transparent;
    transition: color 0.2s, border-color 0.2s;
}
.auth-tabs .tab.active {
    color: var(--crimson);
    border-bottom-color: var(--crimson);
}

.auth-form { display: flex; flex-direction: column; gap: 14px; text-align: left; }
.auth-form label { display: flex; flex-direction: column; gap: 4px; }
.auth-form label span {
    font-family: 'Cinzel', serif;
    font-size: 10px;
    letter-spacing: 0.18em;
    text-transform: uppercase;
    color: var(--ink-3);
}
.auth-form input {
    background: rgba(255,255,255,0.35);
    border: 1px solid var(--vellum-3);
    border-radius: 2px;
    padding: 10px 12px;
    font-family: 'EB Garamond', serif;
    font-size: 17px;
    color: var(--ink-1);
    outline: none;
    transition: border-color 0.15s, background 0.15s;
}
.auth-form input:focus { border-color: var(--crimson); background: rgba(255,255,255,0.55); }

.btn-primary {
    background: var(--crimson);
    color: var(--vellum-0);
    border: 1px solid var(--crimson-deep);
    padding: 12px 16px;
    font-family: 'Cinzel', serif;
    font-size: 12px;
    letter-spacing: 0.26em;
    text-transform: uppercase;
    border-radius: 2px;
    margin-top: 6px;
    box-shadow:
        0 0 0 1px var(--gold) inset,
        0 2px 0 var(--crimson-deep);
    transition: transform 0.08s, box-shadow 0.08s;
}
.btn-primary:hover  { background: #8d2424; }
.btn-primary:active { transform: translateY(1px); box-shadow: 0 0 0 1px var(--gold) inset; }

.auth-msg {
    min-height: 18px;
    font-size: 14px;
    color: var(--crimson);
    margin: 4px 0 0;
    text-align: center;
    font-style: italic;
}
.auth-msg.success { color: var(--moss-deep); }

.auth-foot {
    margin-top: 18px;
    font-size: 13px;
    color: var(--ink-3);
    font-style: italic;
    line-height: 1.5;
}

/* ============================================================
   GAME SHELL
   ============================================================ */
.game-shell {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

/* --- Top bar --- */
.top-bar {
    display: grid;
    grid-template-columns: minmax(240px, 1fr) auto minmax(160px, 1fr);
    align-items: center;
    gap: 24px;
    padding: 14px 24px;
    background:
        linear-gradient(180deg, #1a0e07 0%, #0e0904 100%);
    color: var(--vellum-1);
    border-bottom: 1px solid var(--gold-deep);
    box-shadow: 0 0 0 1px rgba(184,137,59,0.25) inset, 0 4px 18px rgba(0,0,0,0.6);
}

.brand { display: flex; align-items: center; gap: 14px; }
.brand-mark {
    font-size: 32px;
    color: var(--gold);
    text-shadow: 0 0 16px rgba(184,137,59,0.4);
    line-height: 1;
}
.brand-name {
    font-family: 'Cinzel', serif;
    font-weight: 900;
    font-size: 18px;
    letter-spacing: 0.16em;
    color: var(--vellum-0);
    text-transform: uppercase;
}
.brand-sub {
    font-family: 'EB Garamond', serif;
    font-style: italic;
    font-size: 14px;
    color: var(--vellum-3);
    margin-top: 1px;
}

.resources {
    display: flex;
    gap: 4px;
    padding: 6px 10px;
    background: rgba(244,232,207,0.06);
    border: 1px solid rgba(184,137,59,0.35);
    border-radius: 2px;
}
.resource {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 6px 14px;
    min-width: 80px;
    border-right: 1px solid rgba(184,137,59,0.18);
}
.resource:last-child { border-right: none; }
.resource .label {
    font-family: 'Cinzel', serif;
    font-size: 9px;
    letter-spacing: 0.2em;
    text-transform: uppercase;
    color: var(--vellum-3);
    margin-bottom: 2px;
}
.resource .amount {
    font-family: 'EB Garamond', serif;
    font-size: 19px;
    font-weight: 600;
    color: var(--vellum-0);
    line-height: 1;
    letter-spacing: 0.02em;
}
.resource .rate {
    font-size: 11px;
    color: var(--gold);
    font-style: italic;
    margin-top: 2px;
}
.resource .rate.zero { color: var(--vellum-3); }

.player {
    display: flex;
    align-items: center;
    gap: 14px;
    justify-content: flex-end;
}
.player-name {
    font-family: 'Cinzel', serif;
    font-size: 12px;
    letter-spacing: 0.16em;
    color: var(--vellum-1);
}
.link-btn {
    background: transparent;
    border: 1px solid rgba(184,137,59,0.5);
    color: var(--gold);
    padding: 6px 12px;
    font-family: 'Cinzel', serif;
    font-size: 10px;
    letter-spacing: 0.22em;
    text-transform: uppercase;
    border-radius: 2px;
    transition: background 0.15s, color 0.15s;
}
.link-btn:hover { background: var(--gold); color: var(--ink-1); }

/* --- Play area --- */
.play-area {
    flex: 1;
    display: grid;
    grid-template-columns: 1fr 360px;
    gap: 24px;
    padding: 24px;
    align-items: start;
}

/* --- Grid --- */
.grid-wrap {
    display: grid;
    place-items: center;
}
.grid-frame {
    position: relative;
    padding: 28px;
    background:
        radial-gradient(ellipse at center, var(--vellum-0) 0%, var(--vellum-1) 55%, var(--vellum-2) 100%);
    border: 1px solid var(--vellum-3);
    box-shadow:
        0 0 0 1px rgba(184,137,59,0.45) inset,
        0 0 0 6px #1a0e07,
        0 0 0 7px var(--gold-deep),
        0 30px 60px rgba(0,0,0,0.5);
    border-radius: 4px;
}
.grid-frame::before, .grid-frame::after {
    content: "";
    position: absolute;
    width: 30px; height: 30px;
    border: 1.5px solid var(--gold-deep);
    opacity: 0.7;
}
.grid-frame::before { top: 6px; left: 6px; border-right: none; border-bottom: none; }
.grid-frame::after  { bottom: 6px; right: 6px; border-left: none; border-top: none; }

.grid {
    display: grid;
    grid-template-columns: repeat(13, 1fr);
    gap: 2px;
    background: var(--vellum-3);
    padding: 2px;
    border: 1px solid var(--ink-3);
    box-shadow: 0 0 30px rgba(0,0,0,0.18) inset;
}
.cell {
    aspect-ratio: 1 / 1;
    width: 44px;
    background:
        repeating-linear-gradient(45deg, rgba(0,0,0,0.04) 0 4px, transparent 4px 8px),
        radial-gradient(ellipse at top left, var(--vellum-0), var(--vellum-1) 80%);
    display: grid;
    place-items: center;
    position: relative;
    cursor: pointer;
    transition: filter 0.1s, outline-color 0.1s, box-shadow 0.1s;
    outline: 1px solid transparent;
}
.cell:hover { filter: brightness(1.06); outline-color: var(--crimson); }
.cell.selected {
    outline: 2px solid var(--crimson);
    box-shadow: 0 0 0 1px var(--gold) inset, 0 0 14px rgba(122,29,29,0.4);
    z-index: 2;
}

/* Empty cell faint terrain hatching */
.cell.empty::after {
    content: "";
    position: absolute;
    inset: 4px;
    border: 1px dashed rgba(58,35,15,0.18);
}

/* Buildings */
.cell .building {
    width: 100%;
    height: 100%;
    display: grid;
    place-items: center;
    color: var(--ink-2);
    font-family: 'Cinzel', serif;
    font-weight: 700;
    position: relative;
}
.cell .building svg { width: 75%; height: 75%; }
.cell .building .lvl {
    position: absolute;
    bottom: 2px; right: 3px;
    font-size: 9px;
    font-family: 'Cinzel', serif;
    font-weight: 700;
    color: var(--crimson);
    line-height: 1;
    text-shadow: 0 1px 0 var(--vellum-0);
}

/* Cell tints by building type — soft, parchment-friendly */
.cell.b-castle      { background: radial-gradient(ellipse at top, #d4b88a, #b8893b); }
.cell.b-cottage     { background: radial-gradient(ellipse at top, #e8d4ae, #c4a47a); }
.cell.b-farm        { background: radial-gradient(ellipse at top, #d9c97a, #b3a050); }
.cell.b-lumber_mill { background: radial-gradient(ellipse at top, #b8a578, #8a7448); }
.cell.b-quarry      { background: radial-gradient(ellipse at top, #c8c4ba, #969084); }
.cell.b-iron_mine   { background: radial-gradient(ellipse at top, #a8a4a0, #6f6d6c); }
.cell.b-marketplace { background: radial-gradient(ellipse at top, #e8c082, #b88234); }

.compass {
    position: absolute;
    top: -2px;
    right: -2px;
    transform: translate(50%, -50%);
    width: 56px; height: 56px;
    border-radius: 50%;
    background: radial-gradient(ellipse at center, #1a0e07 30%, #3a230f 100%);
    box-shadow: 0 0 0 1px var(--gold-deep), 0 0 0 4px #1a0e07, 0 6px 16px rgba(0,0,0,0.6);
    display: grid;
    place-items: center;
    color: var(--gold);
    font-family: 'Cinzel', serif;
    font-weight: 700;
}
.compass-n {
    position: absolute;
    top: 4px;
    font-size: 10px;
    letter-spacing: 0.1em;
}
.compass-ring { font-size: 22px; color: var(--gold); }

/* --- Sidebar panels --- */
.sidebar {
    display: flex;
    flex-direction: column;
    gap: 18px;
}
.panel {
    background:
        radial-gradient(ellipse at top, var(--vellum-0), var(--vellum-1) 70%, var(--vellum-2));
    border: 1px solid var(--vellum-3);
    padding: 18px;
    border-radius: 4px;
    box-shadow:
        0 0 0 1px rgba(184,137,59,0.45) inset,
        0 0 0 4px #1a0e07,
        0 0 0 5px var(--gold-deep),
        0 16px 36px rgba(0,0,0,0.4);
    position: relative;
}
.panel-title {
    font-family: 'Cinzel', serif;
    font-weight: 700;
    font-size: 11px;
    letter-spacing: 0.28em;
    text-transform: uppercase;
    color: var(--crimson);
    margin: 0 0 12px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--vellum-3);
    text-align: center;
}
.muted { color: var(--ink-3); font-style: italic; margin: 0; font-size: 14px; }
.hint  { color: var(--ink-3); font-style: italic; margin: 10px 0 0; font-size: 13px; text-align: center; }

/* Palette */
.palette {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}
.palette-item {
    background: rgba(255,255,255,0.25);
    border: 1px solid var(--vellum-3);
    padding: 10px 8px;
    text-align: left;
    border-radius: 2px;
    transition: background 0.12s, border-color 0.12s, transform 0.08s;
}
.palette-item:hover { background: rgba(255,255,255,0.5); border-color: var(--crimson); }
.palette-item.active {
    background: var(--ink-1);
    color: var(--vellum-0);
    border-color: var(--gold);
}
.palette-item.active .pi-name { color: var(--gold); }
.palette-item.active .pi-cost { color: var(--vellum-1); }
.palette-item:disabled { opacity: 0.5; cursor: not-allowed; }

.pi-row { display: flex; align-items: center; gap: 8px; }
.pi-icon {
    width: 28px; height: 28px;
    flex: 0 0 28px;
    color: var(--ink-2);
}
.palette-item.active .pi-icon { color: var(--gold); }
.pi-name {
    font-family: 'Cinzel', serif;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.06em;
    color: var(--ink-1);
}
.pi-cost {
    font-size: 11px;
    color: var(--ink-3);
    margin-top: 3px;
    line-height: 1.3;
}

/* Tile info panel */
.tile-info h3 {
    font-family: 'Cinzel', serif;
    font-weight: 700;
    font-size: 16px;
    letter-spacing: 0.08em;
    margin: 0 0 4px;
    color: var(--ink-1);
}
.tile-info .lvl-tag {
    display: inline-block;
    background: var(--crimson);
    color: var(--vellum-0);
    font-family: 'Cinzel', serif;
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.12em;
    padding: 2px 8px;
    border-radius: 2px;
    margin-bottom: 8px;
}
.tile-info .desc { font-size: 14px; color: var(--ink-2); line-height: 1.45; margin: 0 0 12px; }
.tile-info .stat-row {
    display: flex;
    justify-content: space-between;
    padding: 4px 0;
    border-bottom: 1px dotted var(--vellum-3);
    font-size: 14px;
}
.tile-info .stat-row:last-of-type { border-bottom: none; }
.tile-info .stat-row .k { color: var(--ink-3); font-style: italic; }
.tile-info .stat-row .v { color: var(--ink-1); font-weight: 600; }

.tile-actions {
    display: flex;
    gap: 8px;
    margin-top: 14px;
}
.btn {
    flex: 1;
    background: var(--vellum-0);
    border: 1px solid var(--ink-3);
    color: var(--ink-1);
    padding: 8px 10px;
    font-family: 'Cinzel', serif;
    font-size: 10px;
    letter-spacing: 0.18em;
    text-transform: uppercase;
    border-radius: 2px;
    transition: background 0.1s, color 0.1s, border-color 0.1s;
}
.btn:hover { background: var(--ink-1); color: var(--gold); border-color: var(--ink-1); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn.danger { color: var(--crimson); border-color: var(--crimson); }
.btn.danger:hover { background: var(--crimson); color: var(--vellum-0); }

/* --- Toasts --- */
.toast-region {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    flex-direction: column;
    gap: 8px;
    z-index: 999;
    pointer-events: none;
}
.toast {
    background: #1a0e07;
    color: var(--vellum-0);
    padding: 10px 22px;
    border: 1px solid var(--gold-deep);
    box-shadow: 0 0 0 1px rgba(184,137,59,0.45) inset, 0 8px 24px rgba(0,0,0,0.5);
    font-family: 'Cinzel', serif;
    font-size: 11px;
    letter-spacing: 0.18em;
    text-transform: uppercase;
    border-radius: 2px;
    animation: toast-in 0.25s ease-out;
}
.toast.error { color: #f4c8c8; border-color: var(--crimson); }
.toast.success { color: #c8dcb0; border-color: var(--moss); }
@keyframes toast-in {
    from { opacity: 0; transform: translateY(8px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* --- Responsive --- */
@media (max-width: 1100px) {
    .play-area {
        grid-template-columns: 1fr;
    }
    .sidebar { order: 2; }
}
@media (max-width: 900px) {
    .top-bar {
        grid-template-columns: 1fr;
        gap: 12px;
        text-align: center;
    }
    .player { justify-content: center; }
    .resources { overflow-x: auto; }
    .resource { min-width: 70px; padding: 4px 8px; }
}
@media (max-width: 700px) {
    .cell { width: 34px; }
    .grid-frame { padding: 16px; }
}
@media (max-width: 500px) {
    .cell { width: 26px; }
    .auth-title { font-size: 30px; }
    .palette { grid-template-columns: 1fr; }
}
