/* ============================================================
   Mobile responsive layer (viewport <= 768px)
   Loaded AFTER style.css and each page's inline <style> block,
   so equal-specificity rules here win over desktop/page rules.
   PC rendering is untouched.
   ============================================================ */

/* Mobile chrome is injected by /js/mobile.js on every page that has a
   sidebar; keep it hidden unless we are actually on a small viewport. */
.mobile-topbar,
.mobile-drawer,
.mobile-drawer-overlay { display: none; }

@media (max-width: 768px) {
    body { height: auto; min-height: 100vh; overflow: visible; }
    .app { display: block; height: auto; min-height: 100vh; }
    .sidebar { display: none; }

    /* ---------- top bar ---------- */
    .mobile-topbar {
        display: flex;
        align-items: center;
        gap: 10px;
        position: fixed;
        top: 0; left: 0; right: 0;
        z-index: 900;
        height: calc(52px + env(safe-area-inset-top));
        padding: env(safe-area-inset-top) 12px 0;
        background: var(--sidebar-bg);
        color: #fff;
        box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25);
    }
    .mobile-topbar .mobile-logo { font-size: 17px; font-weight: 700; }
    .mobile-topbar .mobile-logo span { color: #a5b4fc; }
    .mobile-menu-btn {
        display: inline-flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        gap: 5px;
        width: 40px; height: 40px;
        padding: 8px;
        background: transparent;
        border: none;
        cursor: pointer;
        flex-shrink: 0;
    }
    .mobile-menu-btn i { display: block; width: 20px; height: 2px; border-radius: 2px; background: #fff; }

    /* ---------- drawer ---------- */
    .mobile-drawer-overlay {
        display: block;
        position: fixed; inset: 0;
        z-index: 950;
        background: rgba(15, 23, 42, 0.5);
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.25s ease, visibility 0.25s ease;
    }
    .mobile-drawer-overlay.open { opacity: 1; visibility: visible; }
    .mobile-drawer {
        display: flex;
        flex-direction: column;
        position: fixed;
        top: 0; left: 0; bottom: 0;
        z-index: 960;
        width: min(76vw, 300px);
        background: var(--sidebar-bg);
        color: var(--sidebar-text);
        transform: translateX(-102%);
        transition: transform 0.25s ease;
        padding-top: env(safe-area-inset-top);
        padding-bottom: env(safe-area-inset-bottom);
    }
    .mobile-drawer.open { transform: translateX(0); }
    .mobile-drawer-head {
        padding: 18px 20px;
        font-size: 19px;
        font-weight: 700;
        color: #fff;
        border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    }
    .mobile-drawer-head span { color: #a5b4fc; }
    .mobile-drawer nav { flex: 1; overflow-y: auto; padding: 8px 0; display: flex; flex-direction: column; }
    .mobile-drawer nav a {
        display: block;
        padding: 13px 20px;
        color: var(--sidebar-text);
        text-decoration: none;
        font-size: 15px;
        border-left: 3px solid transparent;
    }
    .mobile-drawer nav a:active { background: rgba(255, 255, 255, 0.08); }
    .mobile-drawer nav a.active { background: rgba(255, 255, 255, 0.1); color: #fff; border-left-color: var(--primary); }
    .mobile-drawer nav .nav-bottom { margin-top: auto; padding-top: 12px; border-top: 1px solid rgba(255, 255, 255, 0.1); }
    body.mobile-no-scroll { overflow: hidden; }

    /* ---------- page scaffold ---------- */
    .main { padding: calc(64px + env(safe-area-inset-top)) 12px 24px; overflow: visible; }
    .main-fit { height: auto; overflow: visible; display: block; }
    .main-fit .header { margin-bottom: 14px; }
    .downstream-main { height: auto; overflow: visible; }

    .header { flex-wrap: wrap; gap: 10px; margin-bottom: 16px; }
    .header h1 { font-size: 20px; }

    .card { padding: 14px; border-radius: 10px; }
    .card-header { flex-wrap: wrap; gap: 8px; }

    .stats-grid { grid-template-columns: repeat(2, 1fr); }
    .kpi-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
    .kpi-card { min-height: 108px; padding: 12px; }
    .kpi-value { font-size: 22px; }
    .overview-layout { grid-template-columns: 1fr; }
    .ranking-grid { grid-template-columns: 1fr; }
    .dashboard-shell { gap: 12px; }

    /* ---------- tables -> stacked cards ---------- */
    .app .table-wrapper { overflow: visible; }
    .app .table-wrapper table { min-width: 0 !important; width: 100% !important; }
    .app .table-wrapper thead { display: none; }
    .app .table-wrapper table,
    .app .table-wrapper tbody,
    .app .table-wrapper tr { display: block; width: 100%; }
    .app .table-wrapper tr {
        background: #fff;
        border: 1px solid var(--border);
        border-radius: 10px;
        padding: 4px 12px;
        margin-bottom: 12px;
    }
    .app .table-wrapper tr.is-retry { background: #fffbeb; box-shadow: inset 3px 0 0 #f59e0b; }
    .app .table-wrapper tr:hover { background: #fff; }
    .app .table-wrapper tr.is-retry:hover { background: #fffbeb; }
    .app .table-wrapper td {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 10px;
        padding: 9px 0;
        border-bottom: 1px solid #f1f5f9;
        white-space: normal;
        text-align: right;
        min-width: 0;
        overflow-wrap: anywhere;
    }
    .app .table-wrapper td:last-child { border-bottom: none; }
    .app .table-wrapper td::before {
        content: attr(data-label);
        flex-shrink: 0;
        margin-right: 12px;
        font-size: 12px;
        font-weight: 600;
        color: var(--text-light);
        text-align: left;
    }
    .app .table-wrapper td[data-label=""]::before,
    .app .table-wrapper td:not([data-label])::before { display: none; }
    .app .table-wrapper td[colspan] { display: block; text-align: center; }
    .app .table-wrapper td.usage-cell { flex-direction: column; align-items: stretch; min-width: 0; }
    .app .table-wrapper td.usage-cell::before { margin-right: 0; margin-bottom: 6px; }
    .app .usage-summary,
    .app .usage-summary-single { min-width: 0; max-width: none; }
    .log-card { display: block; }
    .log-card .table-wrapper { overflow: visible; }
    .log-card table th { position: static; }

    /* ---------- touch-friendly controls ---------- */
    .btn { min-height: 40px; padding: 9px 16px; }
    .btn-sm { min-height: 34px; padding: 6px 12px; font-size: 13px; }
    .form-group input, .form-group select, .form-group textarea {
        font-size: 16px; /* prevents iOS auto-zoom on focus */
        padding: 10px 12px;
        min-height: 42px;
    }
    .switch { width: 46px; height: 26px; }
    .switch-slider { border-radius: 26px; }
    .switch-slider::before { height: 20px; width: 20px; }
    .switch input:checked + .switch-slider::before { transform: translateX(20px); }

    /* ---------- filter bars (log pages) ---------- */
    .filter-bar { flex-direction: column; align-items: stretch; gap: 10px; }
    .filter-field, .filter-field.narrow { min-width: 0; width: 100%; }
    .filter-field input, .filter-field select { height: 42px; font-size: 16px; }
    .filter-actions { width: 100%; }
    .filter-actions .btn { flex: 1; height: 42px; }

    /* ---------- pagination ---------- */
    .pagination-bar { flex-direction: column; align-items: stretch; gap: 10px; }
    .pagination-info { text-align: center; }
    .pagination-controls { justify-content: space-between; width: 100%; }
    .pagination { flex-wrap: wrap; justify-content: center; }

    /* ---------- modals -> bottom sheets ---------- */
    .modal-overlay { align-items: flex-end; justify-content: center; padding: 0; }
    .modal {
        width: 100%;
        max-width: 100%;
        max-height: 88vh;
        border-radius: 16px 16px 0 0;
        padding: 20px 16px calc(20px + env(safe-area-inset-bottom));
    }
    .modal-lg, .model-modal, .binding-modal { width: 100%; }
    .modal-actions { gap: 10px; }
    .modal-actions .btn { flex: 1; min-height: 44px; }
    .customer-picker { max-height: 32vh; }
    .provider-picker { max-height: 32vh; }
    .bind-picker { max-height: 36vh; }
    .provider-option { grid-template-columns: auto minmax(0, 1fr); }
    .provider-weight-cell { grid-column: 1 / -1; justify-content: flex-end; }
    .bind-option { grid-template-columns: auto minmax(0, 1fr); }
    .bind-upstream-input { grid-column: 1 / -1; height: 40px; font-size: 16px; }

    /* ---------- log detail viewer -> full screen ---------- */
    #detailModal { padding: 0; }
    .detail-modal { width: 100vw; height: 100vh; height: 100dvh; max-height: none; border-radius: 0; }
    .detail-modal-header { padding: 14px 14px 12px; }
    .detail-modal-header h3 { font-size: 17px; }
    .detail-close { width: 38px; height: 38px; }
    .detail-meta { padding: 8px 14px; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); column-gap: 16px; }
    .detail-panes { grid-template-columns: 1fr; grid-template-rows: minmax(0, 1fr) minmax(0, 1fr); padding: 12px; gap: 10px; }
    .detail-workspace { padding: 12px; }
    .detail-viewer-layout { grid-template-columns: 1fr; grid-template-rows: auto minmax(0, 1fr); }
    .detail-payload-nav { flex-direction: row; overflow-x: auto; padding-bottom: 4px; }
    .detail-payload-tab { min-width: 180px; min-height: 56px; }
    .detail-code { font-size: 12px; }

    /* ---------- toast ---------- */
    .toast {
        top: calc(10px + env(safe-area-inset-top));
        left: 12px;
        right: 12px;
        text-align: center;
    }

    /* ---------- login / reset password ---------- */
    .login-wrapper { padding: 20px 16px; }
    .login-card { width: 100%; max-width: 400px; padding: 28px 22px; }
    .login-card .captcha-row img { height: 44px; }

    /* ---------- docs page ---------- */
    .doc-content { overflow: visible; padding-right: 0; }
    .doc-content h2 { font-size: 18px; }
    .doc-content pre { font-size: 12px; padding: 12px; }
    .endpoint { flex-wrap: wrap; }
    .path { word-break: break-all; }

    /* ---------- downstream models split layout ---------- */
    .split-layout { display: block; overflow: visible; }
    .model-list-panel { margin-bottom: 12px; }
    .model-list { max-height: 45vh; }
    .binding-panel { overflow: visible; }
    .binding-panel .table-wrapper { overflow: visible; }
    .binding-toolbar { flex-wrap: wrap; }
    .model-search input { height: 42px; font-size: 16px; }
    .model-search .btn { height: 42px; }

    /* ---------- misc ---------- */
    .grid { gap: 12px; }
    .trend-chart-scroll { -webkit-overflow-scrolling: touch; }
    .period-btn { min-height: 34px; padding: 6px 12px; }
    select.period-btn { height: 34px; }
}

@media (max-width: 480px) {
    .grid { grid-template-columns: 1fr; }
    .header .btn { width: 100%; }
}
