/* ============================
   VIRTUAL KEYBOARD
   ============================ */

/* Global Bottom Area */
#bottom-area {
    display: flex;
    flex-direction: row;
    /* Horizontal Layout: Chart | KB | Rules */
    align-items: stretch;
    /* Full height */
    background: var(--bg-panel);
    border-top: 1px solid var(--border);
    /* Initial Height, modifiable by JS */
    height: 33vh;
    /* Exact 1/3 starting point */
    min-height: 33vh;
    /* Enforce 1/3 minimum */
    max-height: 60vh;
    /* Never take more than 60% of screen */
    width: 100%;
    overflow-y: auto;
    /* Allow scrolling if squashed */
    overflow-x: hidden;
    position: relative;
    padding-top: 5px;
    /* Space for drag handle */

    flex-shrink: 0;
    /* Prevent auto-shrinking in flex column; respect set height */

    /* Smooth Slide Transition */
    transition: height 0.3s cubic-bezier(0.4, 0, 0.2, 1), min-height 0.3s ease, padding 0.3s ease, opacity 0.3s ease;
}

#bottom-area.is-resizing {
    transition: none !important;
}

#bottom-area.collapsed {
    height: 0 !important;
    min-height: 0 !important;
    padding-top: 0 !important;
    border-top: none;
    opacity: 0;
    pointer-events: none;
}

/* Horizontal Resizer (Top of Bottom Area) */
.resizer-horizontal {
    width: 100%;
    height: 6px;
    background: var(--bg-dark);
    border-top: 1px solid var(--border);
    border-bottom: 1px solid var(--border);
    cursor: row-resize;
    z-index: 100;
    flex-shrink: 0;
}

.resizer-horizontal.collapsed {
    display: none;
}

.resizer-horizontal:hover,
.resizer-horizontal.dragging {
    background: var(--primary);
}

/* Vertical Resizer (Between Panels) */
.resizer-vertical {
    width: 6px;
    background: var(--bg-dark);
    cursor: col-resize;
    z-index: 10;
    flex-shrink: 0;
    transition: background 0.2s;
}

.resizer-vertical:hover,
.resizer-vertical.dragging {
    background: var(--primary);
}

/* LEFT PANEL (Phonetic Chart) */
#left-panel {
    /* Initial width: 30% of bottom area */
    width: 30%;
    min-width: 140px;
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--border);
    padding: 1rem;
    overflow-y: auto;
    background: var(--bg-dark);
    /* Themed */

    /* Scrollbar */
    scrollbar-color: var(--border) transparent;

    /* Transition for Collapsing */
    transition: width 0.3s cubic-bezier(0.4, 0, 0.2, 1), padding 0.3s ease, opacity 0.3s ease;
}

#left-panel.is-resizing {
    transition: none !important;
}

/* Keyboard Container (Center) */
.vk-container {
    flex: 1;
    /* Take remaining space */
    min-width: 300px;
    display: flex;
    flex-direction: column;
    align-items: center;
    /* Center horizontally */
    justify-content: center;
    /* Center vertically */
    padding: 1rem;

    /* Consistent Spacing Variable */
    --vk-gap: 6px;
    gap: var(--vk-gap);

    user-select: none;
    overflow: hidden;

    /* Container Queries for responsive sub-elements */
    container-type: size;
}

/* RIGHT PANEL (Ad Space) */
#right-panel-ad {
    height: 100%;
    width: auto;
    aspect-ratio: 1 / 1;
    border-left: 1px solid var(--border);
    background: #151518;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    flex-shrink: 0;
    padding: 10px;
}

.ad-placeholder {
    width: 100%;
    height: 100%;
    color: var(--text-muted);
    font-size: 0.8rem;
    border: 2px dashed var(--border);
    border-radius: 8px;
    opacity: 0.5;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    background: #18181b;
}

#keyboard-helper.is-resizing {
    transition: none !important;
}


/* ... HELPER STYLES ... */
.helper-title {
    font-size: 0.65rem;
    text-transform: uppercase;
    color: var(--primary);
    font-weight: 700;
    margin-bottom: 0.8rem;
    text-align: center;
    letter-spacing: 0.05em;
    opacity: 0.8;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
}

.helper-title::before {
    content: "";
    width: 36px;
    height: 4px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 2px;
}

.helper-container {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.helper-grid-row {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 4px;
}

.rule-badge.compact {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: var(--bg-dark);
    /* Matches Keymap */
    padding: 0.4rem 0.2rem;
    border-radius: 6px;
    font-size: 0.75rem;
    border: 1px solid var(--border);
    min-height: 0;
    cursor: pointer;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    transition: all 0.2s;
}

.rule-badge.compact:hover {
    background: var(--bg-hover);
    border-color: var(--primary);
    box-shadow: 0 0 8px rgba(183, 148, 75, 0.15);
    /* Gold glow */
    transform: translateY(-1px);
}

.rule-badge .seq {
    color: var(--primary) !important;
    /* Force Gold */
    font-size: 9px;
    font-weight: 500;
}

.rule-badge .out {
    color: var(--primary);
    /* Gold/Orange */
    font-weight: 700;
    font-size: 14px;
}


/* ... KEYMAP GRID (Left Panel Content) ... */
/* ... KEYMAP GRID (Left Panel Content) ... */
.keymap-grid.small-grid {
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding-bottom: 20px;
}

.km-section {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.km-header {
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 4px 0;
    user-select: none;
    transition: color 0.2s;
}

.km-header:hover {
    color: var(--primary);
}

.km-header .arrow {
    font-size: 10px;
    transition: transform 0.2s;
}

.km-section.collapsed .km-header .arrow {
    transform: rotate(-90deg);
}

.km-content {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(45px, 1fr));
    gap: 6px;
    transition: all 0.3s ease;
    overflow: hidden;
    max-height: 1000px;
    /* Arbitrary large height for animation */
    opacity: 1;
}

.km-section.collapsed .km-content {
    max-height: 0;
    opacity: 0;
    margin: 0;
}

/* Override .key-card explicitly for small-grid context */
.keymap-grid.small-grid .key-card {
    padding: 0.5rem;
    border-radius: 6px;
    background: var(--bg-dark);
    /* Inverted: Darker than panel */
    /* Themed */
    border: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    transition: all 0.2s;
    cursor: pointer;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.keymap-grid.small-grid .key-card:hover {
    background: var(--bg-hover);
    /* Themed */
    border-color: var(--primary);
    box-shadow: 0 0 8px rgba(183, 148, 75, 0.15);
    /* Gold glow */
    transform: translateY(-1px);
}

.keymap-grid.small-grid .key-card .k-mongol {
    font-size: 1.2rem;
    color: var(--text-main);
}

.keymap-grid.small-grid .key-card .k-cyr {
    font-size: 0.65rem;
    color: var(--primary);
    /* Swapped to Gold */
}

.keymap-grid.small-grid .key-card .k-lat {
    font-size: 0.7rem;
    font-weight: 700;
    color: var(--text-muted);
    /* Swapped to Muted */
}

/* ... KEYBOARD STYLES ... */
.vk-row {
    display: flex;
    gap: var(--vk-gap);
    width: 100%;
    flex: 1;
    /* Rows scale with height */
    min-height: 0;
    /* No margin bottom, gap handles it */
}

.vk-key {
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 0.3rem 0.4rem 0.5rem 0.4rem;
    /* Increased bottom padding */
    flex: 1;
    min-width: 0;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    overflow: hidden;
}

.vk-key:hover {
    background: var(--bg-hover);
    border-color: var(--primary);
    box-shadow: 0 0 8px rgba(183, 148, 75, 0.15);
    /* Gold glow */
    transform: translateY(-1px);
}

.vk-modifier {
    background: var(--bg-panel);
    font-size: 10px;
    text-transform: uppercase;
    color: var(--text-muted);
}

.vk-enter-key {
    background: var(--bg-panel) !important;
    /* Neutral Dark Gray */
    color: #ffffff !important;
    font-size: 13px !important;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    font-weight: 700;
    opacity: 1;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.4);
    /* padding-bottom: 0; */
}

.vk-enter-key:hover,
.vk-enter-key.active-press {
    background: var(--bg-hover) !important;
    border-color: var(--primary);
    opacity: 1;
    filter: none;
    box-shadow: 0 0 8px rgba(183, 148, 75, 0.15);
}

.vk-center-text {
    align-items: center;
    justify-content: center;
}

/* CapsLock Indicator Dot */
.vk-key.caps-locked {
    border-color: var(--primary) !important;
    position: relative;
}

.vk-key.caps-locked::after {
    content: "";
    position: absolute;
    top: 6px;
    right: 8px;
    width: 6px;
    height: 6px;
    background: #FF8C00;
    border-radius: 50%;
    box-shadow: 0 0 5px #FF8C00;
}

.vk-key .lat {
    font-size: clamp(11px, 2vh, 15px);
    font-weight: 700;
    color: var(--text-main);
    line-height: 1;
    align-self: flex-start;
    white-space: nowrap;
}

.vk-key .cyr {
    font-size: clamp(9px, 1.5vh, 12px);
    font-weight: 600;
    color: var(--primary);
    align-self: flex-end;
    line-height: 1;
    opacity: 0.95;
    /* Increased opacity */
    margin-bottom: 2px;
    /* Pull it up from the inner edge */
    white-space: nowrap;
}

.vk-key .cyr:empty {
    display: none;
}

/* Spacebar */
.vk-space {
    background: var(--bg-panel);
}

/* Suggestion/Predictive Styles */
/* Shared Active/Flash State for all interactive items */
.vk-key.active-press,
.key-card.active-press,
.rule-badge.active-press {
    background: var(--primary) !important;
    opacity: 1 !important;
    transform: translateY(1px);
    transition: none !important;
    /* Instant on */
}

.vk-key.active-press *,
.key-card.active-press *,
.rule-badge.active-press * {
    color: #000 !important;
    opacity: 1 !important;
}

/* Marker class for suggested keys - styles applied via .cyr below */
/* Marker class for suggested keys - styles applied via .cyr below */
.vk-key.suggested-key {
    border-color: var(--primary) !important;
    box-shadow: 0 0 12px rgba(183, 148, 75, 0.4);
}

.vk-key.suggested-key .cyr {
    color: var(--primary) !important;
    /* Gold for the prediction */
    font-weight: 800;
    font-size: 15px;
    text-shadow: 0 0 12px rgba(183, 148, 75, 0.6);
    transform: scale(1.15);
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}


/* Removed dimming styles to keep functionality clean */

/* ============================
   RESPONSIVE KEYBOARD (MOBILE)
   ============================ */

.mobile-switcher {
    display: none;
}

@media (max-width: 1024px) {
    #bottom-area {
        height: auto;
        /* Allow it to grow naturally */
        min-height: 350px;
        flex-direction: column;
    }

    /* Hide Secondary Panels on Mobile to save space */
    #left-panel,
    #right-panel-ad {
        display: none;
    }

    /* Keyboard Container */
    .vk-container {
        min-width: 100%;
        /* Stretch to full width */
        padding: 4px;
        /* Reduce padding */
        overflow-y: auto;
        /* Allow internal scrolling if keys text is huge, though unlikely */
    }

    /* Force keyboard to not be huge */
    #bottom-area {
        flex-shrink: 0;
        /* Never shrink below min content, but... */
        max-height: 55dvh;
        /* restrict height so it doesn't eat screen */
        overflow-y: auto;
        position: relative;
        /* Context for absolute positioning if needed */
        flex-direction: column;
        /* Ensure vertical stack for switcher layout */
    }

    /* Mobile Switcher Controls */
    .mobile-switcher {
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 20px;
        padding: 5px 0;
        background: var(--bg-panel);
        border-bottom: 1px solid var(--border);
        width: 100%;
        order: 0;
        /* Top */
    }

    .mobile-switch-btn {
        background: transparent;
        border: none;
        color: var(--text-muted);
        font-size: 13px;
        font-weight: 600;
        padding: 8px 16px;
        cursor: pointer;
        opacity: 0.7;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        transition: all 0.2s;
        border-radius: 20px;
    }

    .mobile-switch-btn.active {
        color: #fff;
        opacity: 1;
        background: rgba(255, 255, 255, 0.05);
        box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.2);
    }

    /* Panel Visibility Management */
    #left-panel,
    #virtual-keyboard-container,
    #mobile-settings-panel {
        display: none;
        /* Hidden by default on mobile, toggled by switcher */
        width: 100% !important;
        /* Full width when shown */
        flex: 1;
        overflow-y: auto;
        order: 1;
        min-width: 0;
        /* Allow shrinking */
    }

    /* Show keyboard by default via class? Or JS will handle it. 
       Let's use a class 'active-mobile-panel' to show. 
       Actually, standard style is display:flex/block. We override here. */

    .mobile-panel-active {
        display: flex !important;
        flex-direction: column;
        flex: 1;
    }

    .mobile-settings-view {
        width: 100%;
        flex: 1;
        overflow-y: auto;
    }

    /* Ad Space always visible at bottom */
    #right-panel-ad {
        display: flex;
        width: 100%;
        height: 60px;
        /* Reduced for mobile to save keyboard space */
        min-height: 60px;
        border-left: none;
        border-top: 1px solid var(--border);
        order: 10;
        padding: 5px;
        background: #000;
        transition: none !important;
        /* Prevent collapse transition affecting ad */
    }

    /* 
       Visibility Logic for Collapsed State:
       Ad must stay visible, but keyboard hides. 
    */
    #bottom-area.collapsed {
        height: auto !important;
        min-height: 0 !important;
        opacity: 1 !important;
        pointer-events: auto !important;
        padding-top: 0 !important;
        border-top: none;
    }

    #bottom-area.collapsed .mobile-switcher,
    #bottom-area.collapsed #virtual-keyboard-container,
    #bottom-area.collapsed #left-panel,
    #bottom-area.collapsed #mobile-settings-panel {
        display: none !important;
    }

    #bottom-area.collapsed #right-panel-ad {
        display: flex !important;
        border-top: 1px solid var(--border);
        /* Ensure boundary */
    }

    .ad-placeholder {
        font-size: 9px;
        /* Smaller text */
        border-style: solid;
        border-width: 1px;
        opacity: 0.4;
    }

    /* Sizing adjustments for keys */
    .vk-container.vk-mobile {
        padding: 6px;
        --vk-gap: 5px;
        /* Fixed distance that won't change */
        background: var(--bg-panel);
        /* Unified Panel Color */
        width: 100%;
        height: auto;
        /* Balanced ratio for 5 rows */
        aspect-ratio: 10 / 6.5;
        box-sizing: border-box;

        /* ======================================================
           ELEGANT MOBILE COLOR SYSTEM (Matches Desktop)
           ====================================================== */
        border-top: 1px solid var(--border);
    }

    .vk-container.vk-mobile .vk-row {
        gap: var(--vk-gap);
        margin-bottom: 0;
        flex: 1;
        /* Rows stretch to fill height */
    }

    /* Elegant Phone Key Style */
    .vk-container.vk-mobile .vk-key {
        border-color: var(--border);
        background: var(--bg-panel);
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);

        padding: 4px;
        border-radius: 6px;
        min-height: 0;
        /* Allow flex to dictate height */
        justify-content: stretch;
        align-items: stretch;
        display: flex;

        /* Performance Fixes */
        touch-action: none;
        /* Prevents 300ms delay */
        user-select: none;
        -webkit-tap-highlight-color: transparent;
        transition: transform 0.1s, background 0.1s;
    }

    .vk-container.vk-mobile .vk-key:active,
    .vk-container.vk-mobile .vk-key.active-tap {
        background: var(--primary) !important;
        border-color: var(--primary) !important;
        transform: translateY(1px);
        box-shadow: none;
    }

    .vk-container.vk-mobile .vk-key.active-tap *,
    .vk-container.vk-mobile .vk-key:active * {
        color: #000 !important;
    }

    /* Dedicated Number Row Styling */
    .vk-container.vk-mobile .vk-key.vk-row-numbers {
        /* Matching standard keys */
        border-color: var(--border);
    }

    .vk-container.vk-mobile .vk-key.vk-row-numbers .lat {
        grid-row: 1 / span 3;
        /* Center vertically in the cap */
        align-self: center;
        font-size: clamp(12px, 4cqi, 18px);
        color: var(--text-main);
    }

    /* ====================================================== */

    .vk-container.vk-mobile .vk-key .vk-cap {
        display: grid;
        grid-template-columns: 1fr 1fr;
        grid-template-rows: auto 1fr auto;
        width: 100%;
        height: 100%;
        pointer-events: none;
        padding: 0;
        /* Fixed internal structure */
    }

    .vk-container.vk-mobile .vk-key .lat {
        grid-column: 1 / span 2;
        grid-row: 2;
        /* Use Container Query Units for scaling text */
        font-size: clamp(14px, 5cqi, 24px);
        font-weight: 500;
        color: var(--text-main);
        /* White */
        font-family: var(--font-heading);
        align-self: center;
        justify-self: center;
    }

    .vk-container.vk-mobile .vk-key .cyr {
        grid-column: 1 / span 2;
        grid-row: 3;
        font-size: clamp(8px, 2.5cqi, 12px);
        color: var(--primary);
        /* Gold/Orange */
        font-weight: 600;
        opacity: 0.95;
        margin-left: 2px;
        margin-bottom: 2px;
        text-align: center;
    }

    /* Modifiers (Shift, Backspace, etc.) */
    .vk-modifier {
        font-size: 10px;
        background: var(--bg-panel) !important;
        /* Match Fullscreen Secondary */
        color: var(--text-muted) !important;
        text-transform: uppercase;
        font-weight: 600;
    }

    /* Spacebar */
    .vk-space {
        background: var(--bg-panel) !important;
        /* Match Fullscreen Secondary */
    }

    /* Force Enter to look like a primary action */
    .vk-enter-key {
        font-size: 11px !important;
        font-weight: 700 !important;
        text-transform: uppercase !important;
        background: var(--bg-panel) !important;
        /* Uniform Dark */
        /* Steel Blue/Gray */
        color: #f8fafc !important;
        border: 1px solid var(--border) !important;
    }

    /* Visual indication for active Shift on mobile */
    .vk-container.vk-mobile .vk-key.shift-active {
        background: rgba(255, 255, 255, 0.1) !important;
        border-color: #fff !important;
    }

    /* Smaller and closer dot for mobile keys */
    .vk-container.vk-mobile .vk-key.caps-locked::after {
        top: 4px;
        right: 4px;
        width: 5px;
        height: 5px;
    }
}