/* =============================================================================
   Little Monsters — In-Game Virtual Keyboard (steampunk dark fantasy)
   Single global keyboard surface, mounted once per page, driven by state.
   Reuses theme tokens from global-theme.css (--color-copper/bronze/brass/gold-*,
   --font-title/body) with hard fallbacks so it also works on pages that only
   load a subset of the theme.
   ----------------------------------------------------------------------------- */

:root {
    /* Measured at open time by game-keyboard.js */
    --lm-gk-height: 0px;
    --lm-gk-lift: 0px;
    --lm-gk-key-h: clamp(44px, 12vw, 52px);
    --lm-gk-gap: clamp(3px, 1.15vw, 6px);
    /* Local copper/gold palette, falls back to global tokens then literals */
    --lm-gk-copper: var(--color-copper, #b87333);
    --lm-gk-bronze: var(--color-bronze, #cd7f32);
    --lm-gk-brass: var(--color-brass, #b5a642);
    --lm-gk-gold: var(--color-gold-primary, #ffd700);
    --lm-gk-gold-dark: var(--color-gold-dark, #b8860b);
    --lm-gk-ink: #f3e7cf;
    --lm-gk-plate-1: #17130e;
    --lm-gk-plate-2: #241b12;
    --lm-gk-key-1: #2b2118;
    --lm-gk-key-2: #1c150e;
    --lm-gk-safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* Root surface -------------------------------------------------------------- */
#lm-gk {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 2147482000; /* above shop/social modals (~1e6), below steampunk-confirm (2147483600) */
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    font-family: var(--font-body, 'Philosopher', sans-serif);
    color: var(--lm-gk-ink);
    background:
        linear-gradient(180deg, rgba(0,0,0,0.35), rgba(0,0,0,0.55)),
        linear-gradient(180deg, var(--lm-gk-plate-2), var(--lm-gk-plate-1));
    border-top: 2px solid var(--lm-gk-copper);
    box-shadow:
        0 -2px 0 rgba(255, 215, 0, 0.12),
        0 -14px 34px rgba(0, 0, 0, 0.55);
    transform: translateY(112%);
    opacity: 0;
    pointer-events: none;
    transition:
        transform 0.24s cubic-bezier(0.22, 0.8, 0.28, 1),
        opacity 0.2s cubic-bezier(0.22, 0.8, 0.28, 1);
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    width: 100%;
    max-width: 100%;
    margin: 0;
    overflow: hidden;
    isolation: isolate;
}
.lm-gk-plate {
    display: flex;
    flex-direction: column;
    gap: var(--lm-gk-gap);
    width: 100%;
    max-width: min(820px, 100%);
    margin: 0 auto;
    padding-top: clamp(6px, 2vw, 10px);
    padding-bottom: calc(clamp(6px, 2vw, 10px) + var(--lm-gk-safe-bottom));
    padding-left: calc(clamp(6px, 2vw, 12px) + env(safe-area-inset-left, 0px));
    padding-right: calc(clamp(6px, 2vw, 12px) + env(safe-area-inset-right, 0px));
    box-sizing: border-box;
    pointer-events: auto;
}
#lm-gk-shield {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 2147482800;
    background: transparent;
    touch-action: none;
    pointer-events: auto;
}
#lm-gk-shield.is-on { display: block; }
#lm-gk.is-open {
    transform: translateY(0);
    opacity: 1;
    pointer-events: auto;
}
/* Subtle engraved copper hairline just inside the top edge */
#lm-gk::before {
    content: "";
    position: absolute;
    inset: 3px 6px auto 6px;
    height: 1px;
    background: linear-gradient(90deg, transparent, var(--lm-gk-gold-dark), transparent);
    opacity: 0.5;
    pointer-events: none;
}

/* Top bar: field name · char counter · close --------------------------------- */
.lm-gk-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 26px;
    padding: 0 2px;
}
.lm-gk-bar__label {
    font-family: var(--font-title, 'Cinzel', serif);
    font-size: clamp(12px, 3.4vw, 14px);
    letter-spacing: 0.04em;
    color: var(--lm-gk-gold);
    text-shadow: 0 1px 2px rgba(0,0,0,0.6);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1 1 auto;
}
.lm-gk-bar__count {
    font-size: clamp(10px, 3vw, 12px);
    color: var(--lm-gk-brass);
    font-variant-numeric: tabular-nums;
    flex: 0 0 auto;
}
.lm-gk-bar__count.is-limit { color: #ff8a5c; }
.lm-gk-bar__close {
    flex: 0 0 auto;
    width: 30px;
    height: 26px;
    display: grid;
    place-items: center;
    border-radius: 8px;
    border: 1px solid var(--lm-gk-copper);
    background: linear-gradient(180deg, var(--lm-gk-key-1), var(--lm-gk-key-2));
    color: var(--lm-gk-ink);
    font-size: 15px;
    line-height: 1;
    cursor: pointer;
}

/* Key rows ------------------------------------------------------------------ */
.lm-gk-rows { display: flex; flex-direction: column; gap: var(--lm-gk-gap); }
.lm-gk-row {
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: 1fr;
    gap: var(--lm-gk-gap);
    align-items: stretch;
}
.lm-gk-row[data-numpad] { grid-template-columns: repeat(3, 1fr); grid-auto-flow: row; }

/* Keys ---------------------------------------------------------------------- */
.lm-gk-key {
    position: relative;
    min-width: 0;
    width: 100%;
    min-height: var(--lm-gk-key-h);
    display: grid;
    place-items: center;
    padding: 0 2px;
    box-sizing: border-box;
    border: 1px solid rgba(205, 127, 50, 0.55);
    border-radius: 10px;
    background: linear-gradient(180deg, var(--lm-gk-key-1), var(--lm-gk-key-2));
    box-shadow:
        inset 0 1px 0 rgba(255, 224, 150, 0.14),
        inset 0 -2px 4px rgba(0, 0, 0, 0.5),
        0 1px 0 rgba(0, 0, 0, 0.6);
    color: var(--lm-gk-ink);
    font-family: var(--font-body, 'Philosopher', sans-serif);
    font-size: clamp(15px, 4.6vw, 20px);
    font-weight: 600;
    line-height: 1;
    cursor: pointer;
    transition: transform 0.04s ease, box-shadow 0.04s ease, background 0.06s ease;
    -webkit-tap-highlight-color: transparent;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}
.lm-gk-key.is-down {
    transform: translateY(2px);
    background: linear-gradient(180deg, #3a2c1c, #251a10);
    box-shadow:
        inset 0 1px 3px rgba(0, 0, 0, 0.7),
        0 0 10px rgba(205, 127, 50, 0.4);
}
.lm-gk-key[disabled], .lm-gk-key.is-disabled {
    opacity: 0.4;
    pointer-events: none;
}
/* Special (wide/utility) keys wear brass accents */
.lm-gk-key[data-wide] { grid-column: span 2; }
.lm-gk-key--util {
    font-size: clamp(11px, 3.4vw, 14px);
    font-weight: 700;
    letter-spacing: 0.03em;
    color: var(--lm-gk-brass);
}
.lm-gk-key[data-role="page"] {
    white-space: nowrap;
    min-width: 0;
    letter-spacing: 0.04em;
}
.lm-gk-key--accentable::after {
    content: "";
    position: absolute;
    top: 4px;
    right: 5px;
    width: 4px;
    height: 4px;
    border-radius: 50%;
    background: var(--lm-gk-gold-dark);
    opacity: 0.6;
}
.lm-gk-key--shift.is-active {
    color: #0f0b06;
    background: linear-gradient(180deg, var(--lm-gk-gold), var(--lm-gk-gold-dark));
    border-color: var(--lm-gk-gold);
    box-shadow: 0 0 12px rgba(255, 215, 0, 0.45), inset 0 1px 0 rgba(255,255,255,0.35);
}
.lm-gk-key--action {
    color: #0f0b06;
    font-weight: 800;
    background: linear-gradient(180deg, #e7b84a, var(--lm-gk-gold-dark));
    border-color: var(--lm-gk-gold);
    text-shadow: none;
}
.lm-gk-key--action.is-down { background: linear-gradient(180deg, #c99a2f, #9c7414); }

/* Accent long-press popup --------------------------------------------------- */
.lm-gk-accents {
    position: fixed;
    z-index: 2147482100;
    display: flex;
    gap: 4px;
    padding: 5px;
    border: 1px solid var(--lm-gk-gold-dark);
    border-radius: 12px;
    background: linear-gradient(180deg, var(--lm-gk-plate-2), var(--lm-gk-plate-1));
    box-shadow: 0 8px 22px rgba(0, 0, 0, 0.6);
    pointer-events: auto;
}
.lm-gk-accents__item {
    min-width: 40px;
    min-height: 44px;
    display: grid;
    place-items: center;
    border-radius: 8px;
    border: 1px solid rgba(205, 127, 50, 0.55);
    background: linear-gradient(180deg, var(--lm-gk-key-1), var(--lm-gk-key-2));
    color: var(--lm-gk-ink);
    font-size: 20px;
    font-weight: 600;
}
.lm-gk-accents__item.is-active {
    background: linear-gradient(180deg, var(--lm-gk-gold), var(--lm-gk-gold-dark));
    color: #0f0b06;
    border-color: var(--lm-gk-gold);
}

/* Body offset while open: pad the page by the measured keyboard height so
   scrollable screens can bring the field up, then JS translates the host. */
html.lm-gk-open,
html.lm-gk-open body {
    scroll-padding-bottom: var(--lm-gk-height, 0px);
}
html.lm-gk-open body {
    padding-bottom: var(--lm-gk-height, 0px) !important;
}
body.lm-gk-open {
    --lm-gk-open-height: var(--lm-gk-height);
    overflow: hidden;
    overscroll-behavior: none;
}
html.lm-gk-open body.alpha-page {
    overflow-x: hidden;
    overflow-y: auto;
}
html.lm-gk-open body.alpha-page .alpha-shell {
    padding-bottom: calc(var(--lm-gk-height, 0px) + 28px);
}
html.lm-gk-open .login-box.lm-gk-lift-host,
html.lm-gk-open .alpha-card.lm-gk-lift-host,
html.lm-gk-open .name-input-container.lm-gk-lift-host {
    position: relative;
    z-index: 6;
}
/* Opt-in hosts (social lobby) keep transform so existing CSS stays in sync. */
[data-lm-gk-lift] {
    --lm-gk-lift: 0px;
    transform: translate3d(0, calc(-1 * var(--lm-gk-lift, 0px)), 0);
    transition:
        transform 0.26s cubic-bezier(0.22, 0.8, 0.28, 1),
        filter 0.26s cubic-bezier(0.22, 0.8, 0.28, 1);
    will-change: transform;
}
/* Auto-detected hosts (login, alpha, name field) use `translate` so page
   entrance animations that already set `transform` are not overwritten. */
.lm-gk-lift-host {
    --lm-gk-lift: 0px;
    translate: 0 calc(-1 * var(--lm-gk-lift, 0px));
    transition: translate 0.26s cubic-bezier(0.22, 0.8, 0.28, 1);
    will-change: translate;
}
[data-lm-gk-lift].is-gk-lifted {
    filter: drop-shadow(0 -18px 28px rgba(8, 5, 3, 0.28));
}

/* Reduced motion ------------------------------------------------------------ */
@media (prefers-reduced-motion: reduce) {
    #lm-gk { transition: opacity 0.12s ease; transform: none; }
    #lm-gk:not(.is-open) { transform: none; visibility: hidden; }
    #lm-gk.is-open { visibility: visible; }
    .lm-gk-key { transition: none; }
    [data-lm-gk-lift] { transition: none; filter: none; }
    .lm-gk-lift-host { transition: none; filter: none; }
}

@media (min-width: 600px) {
    :root {
        --lm-gk-key-h: clamp(48px, 7vw, 58px);
        --lm-gk-gap: clamp(6px, 1.2vw, 10px);
    }
    .lm-gk-key { font-size: clamp(16px, 2.6vw, 22px); }
}

@media (max-width: 380px) {
    :root {
        --lm-gk-key-h: clamp(42px, 13vw, 48px);
        --lm-gk-gap: 3px;
    }
    .lm-gk-key { font-size: clamp(14px, 4.2vw, 18px); border-radius: 8px; }
}

@media (max-height: 500px) {
    :root {
        --lm-gk-key-h: clamp(34px, 13vh, 42px);
        --lm-gk-gap: 3px;
    }
    .lm-gk-bar { min-height: 22px; }
    .lm-gk-plate { padding-top: 4px; }
}

/* Monster Key caret — one line under the finger, never a text selection */
.lm-key-caret {
    position: fixed;
    width: 2px;
    margin-left: -1px;
    background: #c9e7ff;
    box-shadow: 0 0 6px rgba(94, 173, 255, 0.75);
    pointer-events: none;
    z-index: 2147481900;
    opacity: 0;
}
.lm-key-caret.is-on {
    opacity: 1;
    animation: lm-key-caret-blink 1.05s step-end infinite;
}
@keyframes lm-key-caret-blink {
    50% { opacity: 0; }
}

/* Never show on non-coarse pointers unless explicitly forced (desktop = native) */
@media (pointer: fine) and (hover: hover) {
    #lm-gk:not(.lm-gk-force) { display: none; }
}
