/* ==================== CRITICAL MOBILE FIXES ==================== */
/* IMPORTANT: These styles use !important to override desktop styles */

/* ==================== FORCE MOBILE LAYOUT ==================== */
@media (max-width: 1023px) {

    /* CRITICAL: Override grid layout */
    .layout {
        display: flex !important;
        flex-direction: column !important;
        grid-template-columns: none !important;
        gap: 16px !important;
        margin-top: 8px !important;
    }

    /* Force chat column to stack below */
    .chatColumn {
        width: 100% !important;
        max-width: 100% !important;
        order: 2 !important;
        margin: 0 !important;
    }

    /* Force content to full width */
    .content {
        width: 100% !important;
        max-width: 100% !important;
        order: 1 !important;
    }

    /* Fix panel sizing */
    .panel {
        width: 100% !important;
        max-width: 100vw !important;
        min-width: 320px !important;
        padding: 12px !important;
        margin: 0 !important;
        border-radius: 0 !important;
        min-height: 100vh !important;
    }

    /* Remove body padding */
    body {
        padding: 0 !important;
    }

    /* Hide decorative elements */
    .panel::before,
    .panel::after,
    body::before {
        display: none !important;
    }
}

/* ==================== HEADER MOBILE FIX ==================== */
@media (max-width: 1023px) {
    .header {
        flex-wrap: wrap !important;
        gap: 12px !important;
    }

    .global-search-wrap {
        flex: 1 1 100% !important;
        max-width: 100% !important;
        margin: 0 !important;
        order: 3 !important;
    }

    .brand {
        flex-shrink: 0 !important;
    }

    .account {
        display: flex !important;
        gap: 8px !important;
        flex-wrap: wrap !important;
    }
}

/* ==================== TABS MOBILE FIX ==================== */
@media (max-width: 1023px) {
    .tabs {
        overflow-x: auto !important;
        overflow-y: hidden !important;
        -webkit-overflow-scrolling: touch !important;
        scrollbar-width: none !important;
        flex-wrap: nowrap !important;
        gap: 4px !important;
        padding-bottom: 8px !important;
    }

    .tabs::-webkit-scrollbar {
        display: none !important;
    }

    .tab {
        flex-shrink: 0 !important;
        white-space: nowrap !important;
        font-size: 13px !important;
        padding: 10px 14px !important;
        min-height: 44px !important;
    }
}

/* ==================== TOUCH TARGETS ==================== */
@media (max-width: 1023px) {

    button,
    .tab,
    .authToggleBtn,
    .logoutBtn,
    a {
        min-height: 44px !important;
        min-width: 44px !important;
    }

    input,
    select,
    textarea {
        min-height: 44px !important;
        font-size: 16px !important;
    }
}

/* ==================== CHAT MOBILE FIX ==================== */
@media (max-width: 1023px) {
    .chatBox {
        max-height: 300px !important;
        min-height: 200px !important;
    }

    .chatInput {
        flex-wrap: wrap !important;
        gap: 8px !important;
    }

    .chatInput input {
        flex: 1 1 100% !important;
        min-width: 0 !important;
    }

    .chatInput button {
        flex: 1 1 auto !important;
    }
}

/* ==================== AUTH SCREEN MOBILE ==================== */
@media (max-width: 1023px) {
    .authContainer {
        padding: 16px !important;
    }

    .authCard {
        width: 100% !important;
        max-width: 100% !important;
        padding: 24px 16px !important;
        border-radius: 12px !important;
    }

    .authBgGlow {
        display: none !important;
    }
}

/* ==================== PERFORMANCE: DISABLE ANIMATIONS ==================== */
@media (max-width: 1023px) {

    .rankBadge,
    .rankBadge::before,
    .chatMessage {
        animation: none !important;
    }

    .panel,
    .authCard,
    .chatColumn {
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2) !important;
    }
}

/* ==================== RESPONSIVE TYPOGRAPHY ==================== */
@media (max-width: 1023px) {
    .title {
        font-size: 20px !important;
    }

    .authBrandTitle {
        font-size: 24px !important;
    }

    .authTitle {
        font-size: 22px !important;
    }

    .small {
        font-size: 12px !important;
    }
}

/* ==================== STATS CONTROLS MOBILE ==================== */
@media (max-width: 1023px) {
    .statsControls {
        flex-wrap: wrap !important;
        gap: 8px !important;
    }

    .toggle-group,
    .filter-group {
        flex-wrap: wrap !important;
        width: 100% !important;
    }

    .toggle-btn,
    .filter-btn {
        flex: 1 1 auto !important;
        min-width: 80px !important;
        padding: 10px 12px !important;
        font-size: 12px !important;
    }
}

/* ==================== SCROLLBAR MOBILE ==================== */
@media (max-width: 1023px) {
    *::-webkit-scrollbar {
        width: 6px !important;
        height: 6px !important;
    }

    * {
        scrollbar-width: thin !important;
    }
}

/* ==================== SAFE AREA INSETS ==================== */
@supports (padding: max(0px)) {
    @media (max-width: 1023px) {

        .panel,
        .authContainer {
            padding-left: max(12px, env(safe-area-inset-left)) !important;
            padding-right: max(12px, env(safe-area-inset-right)) !important;
            padding-bottom: max(12px, env(safe-area-inset-bottom)) !important;
        }
    }
}

/* ==================== LANDSCAPE MOBILE ==================== */
@media (max-width: 1023px) and (orientation: landscape) {
    .panel {
        min-height: auto !important;
    }

    .chatBox {
        max-height: 200px !important;
    }
}

/* ==================== ACCESSIBILITY ==================== */
@media (max-width: 1023px) {
    *:focus-visible {
        outline: 3px solid var(--accent) !important;
        outline-offset: 2px !important;
    }
}

/* ==================== PREVENT TEXT SELECTION ON BUTTONS ==================== */
button,
.tab,
.toggle-btn,
.filter-btn {
    -webkit-user-select: none !important;
    user-select: none !important;
    -webkit-tap-highlight-color: transparent !important;
}

/* ==================== REDUCE MOTION ==================== */
@media (prefers-reduced-motion: reduce) {

    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

/* ==================== TABLET OPTIMIZATIONS ==================== */
@media (min-width: 768px) and (max-width: 1023px) {
    .panel {
        width: 95vw !important;
        max-width: 900px !important;
        padding: 20px !important;
        border-radius: 16px !important;
    }

    body {
        padding: 24px !important;
    }

    .tab {
        font-size: 14px !important;
        padding: 12px 18px !important;
    }
}

/* ==================== DESKTOP (RESTORE NORMAL) ==================== */
@media (min-width: 1024px) {
    .layout {
        display: grid !important;
        grid-template-columns: 1fr 380px !important;
        gap: 24px !important;
    }

    .chatColumn {
        max-width: 380px !important;
        order: 0 !important;
    }

    .panel {
        max-width: 1400px !important;
        padding: 28px !important;
        border-radius: 24px !important;
    }
}

/* ==================== STATS GRID MOBILE FIX ==================== */
@media (max-width: 1023px) {

    .stats-top-grid,
    .stats-bottom-grid {
        display: flex !important;
        flex-direction: column !important;
        grid-template-columns: none !important;
    }

    .stats-right-col {
        flex-direction: row !important;
        gap: 12px !important;
    }

    .stats-right-col .stat-card-mini {
        padding: 16px !important;
    }
}

/* ==================== RESPONSIVE FIT LAYER (all resolutions) ==================== */

/* --- CRITICAL: make grid/flex children shrinkable (fixes edge cutoff) --- */
.layout,
.content,
.stats-top-grid,
.stats-bottom-grid,
.stats-right-col,
.chart-card,
.stat-card-mini,
.compileCard,
.main-grid {
    min-width: 0 !important;
}

.stats-top-grid > *,
.stats-bottom-grid > *,
.chart-card,
.stat-card-mini {
    min-width: 0 !important;
    max-width: 100% !important;
    box-sizing: border-box !important;
}

/* Desktop layout column must be allowed to shrink (plain 1fr cannot) */
@media (min-width: 1024px) {
    .layout {
        grid-template-columns: minmax(0, 1fr) minmax(0, 380px) !important;
    }
}

/* All canvases (charts, globe) contained inside their cards */
.content canvas {
    max-width: 100% !important;
    width: 100% !important;
    display: block !important;
}

#cobe-globe {
    width: 180px !important;
    height: 180px !important;
    margin: 0 auto !important;
}

/* --- Panel never overflows the viewport --- */
.panel {
    overflow-x: hidden !important;
    max-width: 100vw !important;
}

/* --- Analytics grids: reflow at windowed sizes --- */
@media (max-width: 1240px) and (min-width: 721px) {
    .stats-top-grid {
        grid-template-columns: minmax(0, 1fr) minmax(0, 1.4fr) !important;
    }

    .stats-top-grid > .stats-right-col {
        grid-column: 1 / -1 !important;
        flex-direction: row !important;
    }

    .stats-top-grid > .stats-right-col > .stat-card-mini {
        flex: 1 1 0 !important;
    }
}

@media (max-width: 720px) {
    .stats-top-grid {
        grid-template-columns: minmax(0, 1fr) !important;
    }

    .stats-bottom-grid {
        grid-template-columns: minmax(0, 1fr) !important;
        gap: 12px !important;
    }

    .stat-card-mini {
        padding: 18px !important;
    }

    .stat-value-mini {
        font-size: 26px !important;
    }
}

@media (max-width: 480px) {
    .stats-right-col {
        flex-direction: row !important;
        gap: 10px !important;
    }

    .stats-right-col > .stat-card-mini {
        flex: 1 1 0 !important;
        padding: 14px 10px !important;
    }

    .stat-value-mini {
        font-size: 22px !important;
    }

    .chart-card {
        min-height: 200px !important;
        padding: 14px !important;
    }
}

/* --- Compile card: stack below 1200px (small laptops + down) --- */
@media (max-width: 1200px) {
    .compileCard {
        flex-direction: column !important;
        gap: 20px !important;
        padding: 20px !important;
    }

    .compileCard>div:last-child {
        border-left: none !important;
        padding-left: 0 !important;
        border-top: 1px solid rgba(0, 230, 255, 0.1);
        padding-top: 16px;
        min-width: 0 !important;
    }

    .compileBtn {
        width: 100% !important;
        min-width: 0 !important;
    }
}

/* --- Compile card: compact below 640px --- */
@media (max-width: 640px) {
    .compileCard {
        padding: 14px !important;
        margin-top: 8px !important;
        border-radius: 14px !important;
    }

    .compileCard .formRow select,
    .compileCard .formRow input {
        font-size: 14px !important;
    }

    .compileResult {
        max-height: 320px !important;
        overflow: auto !important;
    }

    .snippetArea {
        font-size: 11px !important;
        max-height: 180px !important;
    }

    .easySetupStep input[type="text"] {
        min-width: 0 !important;
        flex: 1 1 160px !important;
    }
}

/* --- Form rows: never overflow, always wrap --- */
.formRow {
    min-width: 0 !important;
}

.formRow .input,
.formRow input,
.formRow select,
.formRow textarea {
    width: 100% !important;
    max-width: 100% !important;
    box-sizing: border-box !important;
}

/* Inline flex input+button rows (domain check, connect, verify) */
@media (max-width: 720px) {
    .easySetupStep div[style*="display:flex"],
    .compileCard div[style*="display:flex;gap:8px"] {
        flex-wrap: wrap !important;
    }
}

/* --- Template cards + platform chips --- */
#templateContainer,
#platformContainer {
    max-width: 100% !important;
    min-width: 0 !important;
}

@media (max-width: 520px) {
    #platformContainer {
        grid-template-columns: 1fr 1fr !important;
        gap: 8px !important;
    }

    .platformChip {
        padding: 8px 10px !important;
    }
}

@media (max-width: 360px) {
    #platformContainer {
        grid-template-columns: 1fr !important;
    }
}

/* --- Snippet / result horizontal containment (all sizes) --- */
.snippetWrap,
.compileResult,
.easySetupStep {
    max-width: 100% !important;
    min-width: 0 !important;
    overflow-x: hidden !important;
}

.snippetArea {
    width: 100% !important;
    box-sizing: border-box !important;
    white-space: pre-wrap !important;
    word-break: break-all !important;
}

textarea.snippetArea {
    resize: vertical !important;
}

/* --- Drag link: never overflow its card --- */
#dragLink,
#universalBookmarkLink {
    max-width: 100% !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    display: inline-block !important;
    box-sizing: border-box !important;
}

/* --- Logs: fit on narrow screens --- */
@media (max-width: 900px) {
    .logsList .logRow {
        flex-direction: column !important;
        gap: 4px !important;
        padding: 10px !important;
    }

    .logsList .logRow .time {
        flex: 0 0 auto !important;
        width: auto !important;
    }
}

/* --- Tables and wide lists: horizontal scroll guard --- */
.content table,
.content .tableWrap,
.logsList,
.trackedWalletsList {
    max-width: 100% !important;
}

.content {
    min-width: 0 !important;
    overflow-x: hidden !important;
}

.content table {
    display: block !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch !important;
}

/* --- Chat column: fluid between 1024-1400px so content never crushes --- */
@media (min-width: 1024px) and (max-width: 1399px) {
    .layout {
        grid-template-columns: minmax(0, 1fr) 320px !important;
    }

    .chatColumn {
        max-width: 320px !important;
    }
}

@media (min-width: 1024px) and (max-width: 1199px) {
    .layout {
        grid-template-columns: minmax(0, 1fr) 280px !important;
    }

    .chatColumn {
        max-width: 280px !important;
    }
}

/* --- Very small phones --- */
@media (max-width: 400px) {
    .panel {
        padding: 10px !important;
    }

    .header .title {
        font-size: 17px !important;
    }

    .tab {
        padding: 8px 10px !important;
        font-size: 11.5px !important;
    }

    .authCard {
        padding: 18px 12px !important;
    }
}

/* --- Large desktops: comfortable reading width --- */
@media (min-width: 1600px) {
    .panel {
        max-width: 1560px !important;
    }

    .layout {
        grid-template-columns: minmax(0, 1fr) 400px !important;
    }

    .chatColumn {
        max-width: 400px !important;
    }
}

/* --- Announcement/modal cards: contain images and long words --- */
.content img,
.content svg {
    max-width: 100% !important;
    height: auto !important;
}

.content * {
    overflow-wrap: break-word !important;
}

/* --- Tabs: wrap cleanly (centered rows) on windowed desktop --- */
@media (min-width: 1024px) and (max-width: 1366px) {
    .tabs {
        flex-wrap: wrap !important;
        justify-content: center !important;
        row-gap: 8px !important;
        overflow-x: visible !important;
    }
}

/* --- Chat column + input: never clip the Send button --- */
.chatColumn {
    min-width: 0 !important;
    max-width: 100% !important;
    overflow: hidden !important;
}

.chatInput {
    display: flex !important;
    flex-wrap: nowrap !important;
    gap: 8px !important;
    min-width: 0 !important;
}

.chatInput input {
    flex: 1 1 auto !important;
    min-width: 0 !important;
    width: auto !important;
}

.chatInput button {
    flex: 0 0 auto !important;
    white-space: nowrap !important;
    min-width: 0 !important;
}

.chatHeader,
.chatBox,
.chatInput,
.chatColumn>div {
    max-width: 100% !important;
    min-width: 0 !important;
}

/* --- Selects: native dropdown fit on iOS --- */
select.input {
    -webkit-appearance: none !important;
    appearance: none !important;
    text-overflow: ellipsis !important;
    padding-right: 30px !important;
    background-image: linear-gradient(45deg, transparent 50%, rgba(230, 255, 255, 0.6) 50%),
        linear-gradient(135deg, rgba(230, 255, 255, 0.6) 50%, transparent 50%) !important;
    background-position: calc(100% - 16px) 50%, calc(100% - 11px) 50% !important;
    background-size: 5px 5px, 5px 5px !important;
    background-repeat: no-repeat !important;
}