/* =========================================================
   BOVIlink — Responsive CSS
   Tablet: ≤1100px  |  Mobile: ≤768px  |  Small: ≤420px
   ========================================================= */

/* ── Global touch improvements ── */
button, a, select, input, textarea {
    -webkit-tap-highlight-color: transparent;
}
input, select, textarea {
    font-size: 16px !important; /* prevents iOS zoom on focus */
}
@media (min-width: 769px) {
    input, select, textarea { font-size: 13.5px !important; }
}

/* ── Scrollable table wrapper ── */
.table-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}
table {
    min-width: 600px; /* forces scroll rather than breaking layout */
}

/* Wrap all tables in a scroll container via CSS */
#animalsTable table,
#recentAnimalsTable table,
#slaughterTable table,
#groupsTable table,
#groupMembersTable table,
#feedTypesTable table {
    min-width: 0; /* override; these get their own rules below */
}

/* =========================================================
   TABLET (≤1100px)
   ========================================================= */
@media (max-width: 1100px) {
    .app-shell { grid-template-columns: 220px 1fr; }
    .main { padding: 24px 28px 60px; }
    .stats-row { grid-template-columns: repeat(2, 1fr); gap: 12px; }
}

/* =========================================================
   MOBILE (≤880px) — sidebar becomes off-canvas drawer
   ========================================================= */
@media (max-width: 880px) {

    /* Shell */
    .app-shell { grid-template-columns: 1fr; }

    /* Mobile topbar */
    .mobile-topbar {
        display: flex;
        align-items: center;
        gap: 10px;
        position: fixed;
        top: 0; left: 0; right: 0;
        height: 56px;
        background: var(--charcoal);
        padding: 0 16px;
        z-index: 260;
        box-shadow: 0 2px 10px rgba(0,0,0,0.2);
    }
    .mobile-topbar-title {
        font-family: var(--font-display);
        font-weight: 700;
        font-size: 17px;
        color: #fff;
    }
    .burger-btn {
        background: none; border: none; padding: 8px; margin-left: -8px;
        display: flex; flex-direction: column; justify-content: center; gap: 5px;
        cursor: pointer;
    }
    .burger-btn span {
        display: block; width: 22px; height: 2px;
        background: #EFEAE0; border-radius: 2px;
        transition: transform .2s, opacity .2s;
    }

    /* Off-canvas sidebar */
    .sidebar {
        position: fixed;
        top: 0; left: 0;
        height: 100vh;
        width: 270px;
        max-width: 85vw;
        transform: translateX(-100%);
        transition: transform .25s ease;
        z-index: 270;
        padding: 20px 16px;
        overflow-y: auto;
        flex-direction: column;
    }
    .sidebar.open { transform: translateX(0); }
    .sidebar-close-btn {
        display: block;
        position: absolute; top: 14px; right: 14px;
        background: none; border: none; color: #D8D1C4;
        font-size: 26px; line-height: 1; cursor: pointer;
        padding: 4px 8px;
    }
    .sidebar-close-btn:hover { color: #fff; }
    .sidebar-backdrop.show { display: block; }

    /* Main content pushed below fixed topbar */
    .main {
        padding: 18px 16px 80px;
        margin-top: 56px;
        max-width: 100%;
    }

    /* Topbar — stack on mobile */
    .topbar {
        flex-direction: column;
        align-items: flex-start;
        gap: 12px;
    }
    .topbar > div:last-child {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
        width: 100%;
    }
    .topbar > div:last-child .btn {
        flex: 1 1 auto;
        justify-content: center;
        min-width: 120px;
    }

    /* Stats */
    .stats-row {
        grid-template-columns: repeat(2, 1fr);
        gap: 10px;
        margin-bottom: 20px;
    }
    .stat-card .num { font-size: 24px; }

    /* Card head — stack search + filters */
    .card-head {
        flex-direction: column;
        align-items: stretch;
        gap: 10px;
    }
    .card-head > div[style*="flex"] {
        flex-wrap: wrap !important;
    }
    .search-box { min-width: 0; width: 100%; }
    .filter-select { flex: 1 1 auto; min-width: 130px; }

    /* Bulk actions bar — stack */
    .bulk-actions-bar {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
        padding: 12px 14px;
    }
    .bulk-actions-bar > div {
        flex-wrap: wrap;
        width: 100%;
    }
    .bulk-actions-bar .btn { flex: 1 1 auto; justify-content: center; }

    /* Tables — horizontal scroll */
    #animalsTable,
    #recentAnimalsTable,
    #slaughterTable,
    #groupsTable,
    #groupMembersTable,
    #feedTypesTable {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    #animalsTable table,
    #recentAnimalsTable table,
    #slaughterTable table,
    #groupsTable table,
    #groupMembersTable table,
    #feedTypesTable table {
        min-width: 580px;
    }

    /* Tabs — horizontal scroll */
    .tabs {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        flex-wrap: nowrap;
        border-bottom: 1px solid var(--line);
        padding-bottom: 0;
        gap: 0;
    }
    .tab-btn {
        white-space: nowrap;
        flex-shrink: 0;
        padding: 10px 14px;
    }

    /* Weight summary — single column */
    .weight-summary-row {
        grid-template-columns: repeat(2, 1fr);
        gap: 10px;
    }

    /* Sub-items — better mobile layout */
    .sub-item {
        flex-wrap: wrap;
        gap: 8px;
        padding: 12px;
    }
    .sub-item .main-info { flex: 1 1 100%; }
    .sub-item .value { font-size: 16px; }
    .sub-item .btn { flex-shrink: 0; }

    /* Form grid — single column */
    .form-grid { grid-template-columns: 1fr; }
    .form-grid .field.full { grid-column: 1; }

    /* Form actions — stack */
    .form-actions {
        flex-direction: column-reverse;
        gap: 8px;
    }
    .form-actions .btn {
        width: 100%;
        justify-content: center;
        padding: 13px;
    }

    /* Modals — full screen on mobile */
    .modal-overlay {
        padding: 0;
        align-items: flex-end;
    }
    .modal {
        max-width: 100%;
        width: 100%;
        border-radius: 20px 20px 0 0;
        padding: 24px 20px 32px;
        max-height: 92vh;
        overflow-y: auto;
    }

    /* Login page — tighter */
    .login-wrap { max-width: 100%; }
    .login-card { padding: 26px 20px; border-radius: 12px; }

    /* Register page */
    .register-card { padding: 24px 18px; border-radius: 12px; }

    /* Group stats */
    #groupStatsRow { grid-template-columns: repeat(2, 1fr) !important; }

    /* Profile topbar — stack buttons */
    #view-profile .topbar > div:last-child,
    #view-group-detail .topbar > div:last-child {
        flex-direction: row;
        flex-wrap: wrap;
    }

    /* Hide 'Back' button text overflow on very small */
    #btnBackToList, #btnBackToGroups { font-size: 13px; }

    /* Sortable headers — smaller text */
    thead th { font-size: 10.5px; padding: 0 8px 8px; }
    tbody td { padding: 10px 8px; font-size: 13px; }

    /* Searchable select list */
    .searchable-select-list { max-height: 180px; }

    /* Add-to-group list */
    #addToGroupList { max-height: 250px; }
}

/* =========================================================
   SMALL MOBILE (≤480px)
   ========================================================= */
@media (max-width: 480px) {

    .stats-row { grid-template-columns: repeat(2, 1fr); gap: 8px; }
    .stat-card { padding: 14px 14px; }
    .stat-card .num { font-size: 22px; }
    .stat-card .label { font-size: 11px; }

    .topbar h2 { font-size: 20px; }
    .card { padding: 16px 14px; }

    /* Modals taller */
    .modal { max-height: 96vh; padding: 20px 16px 28px; }

    .weight-summary-row { grid-template-columns: 1fr !important; }
    #groupStatsRow { grid-template-columns: 1fr !important; }

    /* Topbar action buttons — full width */
    .topbar > div:last-child .btn { min-width: 0; }

    /* Code pill smaller */
    .code-pill { font-size: 11.5px; padding: 2px 8px; }

    /* Badge smaller */
    .badge { font-size: 10px; padding: 2px 7px; }

    /* Fab-style new button */
    #btnNewAnimal, #btnNewGroup, #btnNewSlaughter {
        position: fixed;
        bottom: 20px;
        right: 16px;
        z-index: 100;
        border-radius: 50px;
        padding: 13px 20px;
        box-shadow: 0 4px 16px rgba(76,107,58,0.4);
    }

    /* Login page */
    .login-body { padding: 16px; }
    .login-card { padding: 22px 16px; }
    .login-brand img { max-width: 120px; }
}

/* =========================================================
   EXTRA SMALL (≤360px)
   ========================================================= */
@media (max-width: 360px) {
    .main { padding: 14px 12px 80px; }
    .stats-row { grid-template-columns: 1fr 1fr; gap: 6px; }
    .filter-select { font-size: 12px; padding: 7px 8px; }
    .bulk-actions-bar .btn { font-size: 12px; padding: 7px 10px; }
}

/* =========================================================
   LARGE SCREENS (≥1400px)
   ========================================================= */
@media (min-width: 1400px) {
    .main { padding: 36px 56px 80px; }
    .stats-row { grid-template-columns: repeat(4, 1fr); }
}

/* =========================================================
   PRINT
   ========================================================= */
@media print {
    .sidebar, .mobile-topbar, .sidebar-backdrop,
    .btn, .bulk-actions-bar, .tabs,
    #btnNewAnimal, #btnEditAnimal, #btnDeleteAnimal { display: none !important; }
    .main { margin: 0; padding: 0; }
    .app-shell { display: block; }
    table { min-width: 0 !important; }
}
