/* ═══════════════════════════════════════════════════════════════
   Responsive Improvements — Mobile-first polish
   Loaded last so its rules win over site.css / components.css.
   ═══════════════════════════════════════════════════════════════ */

/* ─── 1. Mobile search: replace desktop search with a toggle button ─── */
@media (max-width: 768px) {
    .header-search {
        display: none !important;
    }
    .header-search.mobile-open {
        display: flex !important;
        position: fixed;
        top: var(--header-h, 60px);
        right: 0;
        left: 0;
        z-index: 180;
        padding: .5rem .75rem;
        background: #fff;
        border-bottom: 1px solid var(--border);
        max-width: none;
        box-shadow: 0 4px 12px rgba(0,0,0,.06);
    }
    .header-search.mobile-open .search-input-wrap { flex: 1; }
    .header-search.mobile-open .search-input { width: 100%; }
    .mobile-search-toggle {
        display: inline-flex !important;
        align-items: center;
        justify-content: center;
        width: 38px;
        height: 38px;
        border: 1px solid var(--border);
        background: #fff;
        border-radius: 10px;
        font-size: 1.05rem;
        cursor: pointer;
        flex-shrink: 0;
    }
    .mobile-search-toggle:hover { background: var(--bg-secondary); }
}
.mobile-search-toggle { display: none; }

/* ─── 2. Page content padding on small screens ───────────────────── */
@media (max-width: 768px) {
    .page-content {
        padding: .75rem !important;
    }
    .page-header {
        flex-direction: column;
        align-items: stretch;
        gap: .65rem;
    }
    .page-header h2 {
        font-size: 1.1rem;
        line-height: 1.35;
    }
    .page-header .btn-group-actions {
        flex-wrap: wrap;
        gap: .4rem;
    }
    .page-header .btn-group-actions .btn {
        flex: 1 1 auto;
        min-width: 0;
        font-size: .85rem;
        padding: .45rem .7rem;
    }
}

/* ─── 3. Responsive tables — stack only where explicitly marked ──── */
@media (max-width: 768px) {
    /* Tables marked for mobile stacking — turn into card rows.
       Other tables keep their min-width so they horizontal-scroll inside .table-scroll-mobile. */
    .table-stack-mobile {
        min-width: 0 !important;
    }
    .table-stack-mobile thead { display: none; }
    .table-stack-mobile tbody,
    .table-stack-mobile tr,
    .table-stack-mobile td {
        display: block;
        width: 100%;
    }
    .table-stack-mobile tbody tr {
        border: 1px solid var(--border);
        border-radius: 10px;
        margin-bottom: .55rem;
        padding: .55rem .75rem;
        background: #fff;
    }
    .table-stack-mobile tbody td {
        border: none !important;
        padding: .3rem 0 !important;
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: .75rem;
    }
    .table-stack-mobile tbody td::before {
        content: attr(data-label);
        font-weight: 600;
        color: var(--text-muted);
        font-size: .8rem;
        flex-shrink: 0;
    }
    .table-stack-mobile tbody td:empty,
    .table-stack-mobile tbody td:not([data-label])::before {
        display: none;
    }
    .table-stack-mobile tbody td.text-end,
    .table-stack-mobile tbody td.money { text-align: end; }
}

/* Ensure table cards without stacking scroll horizontally on narrow screens */
@media (max-width: 768px) {
    .table-card {
        overflow-x: auto;
    }
    .table-card:has(.table-stack-mobile) {
        overflow-x: visible;
    }
}

/* ─── 4. Filter bars: collapse gracefully on mobile ────────────────── */
@media (max-width: 768px) {
    .filter-bar,
    .sales-filter,
    .purchase-filter,
    .report-filter,
    form.filter-form {
        display: grid !important;
        grid-template-columns: 1fr !important;
        gap: .6rem !important;
    }
    .filter-bar .form-control,
    .filter-bar input,
    .filter-bar select {
        width: 100% !important;
    }
    .sales-filter-field-date,
    .sales-filter-field-customer {
        width: 100% !important;
        min-width: 0 !important;
    }
}

/* ─── 5. Forms: full width inputs + sensible grids ────────────────── */
@media (max-width: 768px) {
    .form-grid-2,
    .form-grid-3,
    .form-grid-4,
    .invoice-grid,
    .invoice-section,
    .invoice-add-bar,
    .inv-info-grid,
    .dash-grid-2,
    .payment-create-grid {
        grid-template-columns: 1fr !important;
    }
    .form-control,
    input[type="text"],
    input[type="date"],
    input[type="number"],
    input[type="email"],
    input[type="password"],
    select,
    textarea {
        font-size: 16px; /* Prevents iOS zoom on focus */
    }
    .form-group { margin-bottom: .65rem; }
    .form-label { font-size: .85rem; }
}

/* ─── 6. Invoice form: fix late breakpoint collapse ───────────────── */
@media (max-width: 768px) {
    .invoice-grid { grid-template-columns: 1fr !important; }
    .invoice-section { grid-template-columns: 1fr !important; }
    .invoice-add-bar { grid-template-columns: 1fr !important; }
    .invoice-actions {
        flex-direction: column-reverse;
        gap: .5rem;
    }
    .invoice-actions .btn { width: 100%; }
}

/* ─── 7. KPI / Summary cards ─────────────────────────────────────── */
@media (max-width: 768px) {
    .kpi-grid,
    .dash-kpi-grid {
        grid-template-columns: 1fr 1fr !important;
        gap: .65rem !important;
    }
    .purchases-index-summary {
        gap: .5rem !important;
    }
    .invoice-summary-chips {
        flex-wrap: wrap;
        gap: .35rem !important;
    }
    .dash-kpi-card { padding: .9rem .85rem; }
    .dash-kpi-value { font-size: 1.15rem; line-height: 1.25; }
    .dash-kpi-label { font-size: .72rem; }
    .dash-kpi-sub   { font-size: .7rem; }
}
@media (max-width: 420px) {
    .kpi-grid,
    .dash-kpi-grid {
        grid-template-columns: 1fr !important;
    }
}

/* ─── 8. Dashboard noise reduction ───────────────────────────────── */
.dash-kpi-card .dash-kpi-sub {
    font-size: .78rem;
    opacity: .7;
}
.dash-kpi-card:hover .dash-kpi-sub {
    opacity: 1;
}
.dash-section-note {
    font-size: .85rem;
    color: var(--text-muted);
}
.dash-extra-kpis[open] summary::after { transform: rotate(180deg); }
.dash-extra-kpis summary {
    list-style: none;
    cursor: pointer;
    user-select: none;
    padding: .6rem .9rem;
    border: 1px dashed var(--border);
    border-radius: 10px;
    background: var(--bg-secondary);
    font-weight: 600;
    font-size: .9rem;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    gap: .5rem;
    margin-bottom: .75rem;
}
.dash-extra-kpis summary::-webkit-details-marker { display: none; }
.dash-extra-kpis summary::after {
    content: "▾";
    margin-inline-start: auto;
    transition: transform .2s;
    font-size: .9rem;
}
.dash-extra-kpis[open] > summary {
    background: var(--primary-soft);
    color: var(--primary);
    border-style: solid;
}

/* ─── 9. Sales/Purchase/Party detail KPI bar: wrap on mobile ──────── */
@media (max-width: 768px) {
    .inv-kpi-bar {
        display: grid !important;
        grid-template-columns: 1fr 1fr !important;
        gap: .5rem !important;
    }
    .inv-kpi {
        padding: .65rem .5rem !important;
    }
    .inv-kpi-label { font-size: .72rem !important; }
    .inv-kpi-value { font-size: .95rem !important; }
}

/* ─── 10. Trend chart on dashboard: shrink gracefully ──────────────── */
@media (max-width: 768px) {
    .dash-trend-chart {
        min-width: 0 !important;
        overflow-x: auto;
        padding-bottom: .25rem;
    }
    .dash-trend-col {
        min-width: 18px;
        flex: 0 0 auto;
    }
    .dash-trend-day { font-size: .65rem; }
    .dash-legend { font-size: .8rem; flex-wrap: wrap; }
}

/* ─── 11. Payment allocation rows: card layout on mobile ───────────── */
@media (max-width: 768px) {
    #allocationsTable thead { display: none; }
    #allocationsTable,
    #allocationsTable tbody,
    #allocationsTable tfoot {
        display: block; width: 100%;
    }
    #allocationsTable tbody tr,
    #allocationsTable tfoot tr {
        display: flex;
        flex-wrap: wrap;
        gap: .25rem .75rem;
        align-items: center;
        padding: .65rem;
        border: 1px solid var(--border);
        border-radius: 10px;
        margin-bottom: .6rem;
        background: #fff;
    }
    #allocationsTable tbody tr.alloc-active {
        border-color: var(--success);
        box-shadow: 0 0 0 3px var(--success-soft);
        background: color-mix(in srgb, var(--success-soft) 40%, #fff);
    }
    #allocationsTable td {
        padding: 0 !important;
        border: 0 !important;
        display: inline-flex;
        align-items: center;
        gap: .25rem;
        font-size: .82rem;
    }
    #allocationsTable td:nth-child(1) { order: 1; }
    #allocationsTable td:nth-child(2) {
        order: 2;
        font-weight: 700;
        font-size: .95rem;
        flex: 1 1 auto;
    }
    #allocationsTable td:nth-child(3) { order: 3; color: var(--text-muted); }
    #allocationsTable td:nth-child(3)::before { content: "📅"; margin-inline-end: .2rem; }
    #allocationsTable td:nth-child(4) { order: 4; }
    #allocationsTable td:nth-child(4)::before { content: "الإجمالي:"; color: var(--text-muted); margin-inline-end: .25rem; font-weight: 500; }
    #allocationsTable td:nth-child(5) { order: 5; }
    #allocationsTable td:nth-child(5)::before { content: "مدفوع:"; color: var(--text-muted); margin-inline-end: .25rem; font-weight: 500; }
    #allocationsTable td:nth-child(6) { order: 6; }
    #allocationsTable td:nth-child(6)::before { content: "متبقي:"; color: var(--text-muted); margin-inline-end: .25rem; font-weight: 500; }

    #allocationsTable td:nth-child(7) {
        order: 7;
        flex: 1 1 100%;
        padding-top: .4rem !important;
        border-top: 1px dashed var(--border) !important;
        margin-top: .35rem;
    }
    #allocationsTable td:nth-child(7)::before {
        content: "قيمة السداد:";
        color: var(--text-muted);
        font-size: .82rem;
        font-weight: 500;
        margin-inline-end: .5rem;
    }
    #allocationsTable input.alloc-amount {
        width: 100% !important;
        max-width: 220px;
        text-align: end;
    }

    #allocationsTable tfoot tr {
        background: var(--bg-secondary);
        border-style: dashed;
        justify-content: space-between;
    }
    #allocationsTable tfoot td {
        font-size: .88rem;
    }
    #allocationsTable tfoot td:first-child {
        color: var(--text-muted);
        flex: 1 1 auto;
    }
    #allocationsTable tfoot td:last-child {
        font-weight: 700;
    }
    #allocationsTable tfoot td::before { content: none !important; }
}

/* ─── 12. Buttons: touch-friendly on mobile ───────────────────────── */
@media (max-width: 768px) {
    .btn {
        min-height: 40px;
        font-size: .9rem;
    }
    .btn-sm { min-height: 34px; }
}

/* ─── 13. Pagination wrap on mobile ───────────────────────────────── */
@media (max-width: 768px) {
    .pagination,
    .pager {
        flex-wrap: wrap;
        gap: .3rem;
    }
    .pagination a,
    .pagination button {
        min-width: 34px;
        padding: .35rem .55rem;
        font-size: .8rem;
    }
}

/* ─── 14. Inline action buttons on tables ─────────────────────────── */
@media (max-width: 768px) {
    .inline-actions,
    .btn-group-actions {
        display: flex;
        flex-wrap: wrap;
        gap: .3rem;
    }
    .inline-actions .btn,
    .btn-group-actions .btn {
        font-size: .78rem;
        padding: .35rem .55rem;
    }
}

/* ─── 15. Dashboard hero card: calmer layout ──────────────────────── */
@media (max-width: 768px) {
    .dash-hero-main {
        display: block !important;
    }
    .dash-hero-copy,
    .dash-hero-side { margin-bottom: .75rem; }
    .dash-title { font-size: 1.15rem; }
    .dash-subtitle { font-size: .82rem; }
    .dash-hero-meta {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: .4rem;
    }
    .dash-signal-value { font-size: 1.4rem; }
    .dash-quick-actions {
        display: grid !important;
        grid-template-columns: 1fr 1fr !important;
        gap: .55rem !important;
    }
    .dash-quick-btn {
        padding: .7rem .6rem !important;
        font-size: .82rem;
    }
    .dash-quick-btn strong { font-size: .92rem; }
    .dash-quick-btn span:last-child { display: none; }
    .dash-range-grid {
        grid-template-columns: 1fr 1fr !important;
        gap: .5rem !important;
    }
    .dash-range-actions { grid-column: 1 / -1; }
    .dash-range-actions .btn { width: 100%; }
}

/* ─── 16. Calmer visual weight for secondary text ─────────────────── */
.dash-section-head {
    margin-block: 1rem .5rem;
}
.dash-section-title {
    font-size: 1.05rem;
    margin-bottom: .15rem;
}
@media (max-width: 768px) {
    .dash-section-title { font-size: .98rem; }
    .dash-section-note { font-size: .78rem; }
}

/* ─── 17. Sidebar polish: wider tap targets ───────────────────────── */
@media (max-width: 768px) {
    .nav-item {
        padding: .7rem .9rem;
        font-size: .92rem;
    }
    .nav-section {
        font-size: .7rem;
        padding: .6rem .9rem .25rem;
    }
}

/* ─── 18. Toast positions ─────────────────────────────────────────── */
@media (max-width: 480px) {
    .toast-container,
    #appToastRoot {
        inset: auto 10px 10px 10px !important;
        max-width: none !important;
    }
    .toast,
    .app-toast {
        font-size: .85rem;
    }
}

/* ─── 19. Prevent horizontal overflow app-wide ────────────────────── */
html, body { overflow-x: hidden; }
img, video, canvas, svg { max-width: 100%; height: auto; }

/* ─── 20. Small global refinements to reduce visual noise ─────────── */
.card-header {
    font-size: .95rem;
}
.inv-kpi-label {
    letter-spacing: 0;
}

/* ═══════════════════════════════════════════════════════════════
   Payment Status UI — pills, progress bars, filter chips
   ═══════════════════════════════════════════════════════════════ */

/* Status filter tabs */
.payment-status-filter {
    display: flex;
    gap: .5rem;
    flex-wrap: wrap;
    margin-block: .9rem 1rem;
    padding: .5rem;
    background: var(--bg-secondary, #f8fafc);
    border-radius: 12px;
    border: 1px solid var(--border);
}
.pay-chip {
    display: inline-flex;
    align-items: center;
    gap: .35rem;
    padding: .45rem 1rem;
    border-radius: 999px;
    font-size: .85rem;
    font-weight: 600;
    text-decoration: none;
    border: 1.5px solid transparent;
    background: #fff;
    color: var(--text-muted, #64748b);
    transition: all .15s ease;
    user-select: none;
}
.pay-chip:hover {
    transform: translateY(-1px);
    text-decoration: none;
    border-color: var(--border);
    color: var(--text, #1e293b);
}
.pay-chip.is-active {
    background: var(--primary, #2563eb);
    color: #fff;
    border-color: var(--primary, #2563eb);
    box-shadow: 0 2px 6px rgba(37, 99, 235, .25);
}
.pay-chip--success.is-active {
    background: var(--success, #16a34a);
    border-color: var(--success, #16a34a);
    box-shadow: 0 2px 6px rgba(22, 163, 74, .25);
}
.pay-chip--warning.is-active {
    background: var(--warning, #d97706);
    border-color: var(--warning, #d97706);
    box-shadow: 0 2px 6px rgba(217, 119, 6, .25);
}
.pay-chip--danger.is-active {
    background: var(--danger, #dc2626);
    border-color: var(--danger, #dc2626);
    box-shadow: 0 2px 6px rgba(220, 38, 38, .25);
}

/* Status pill (in row cell) */
.pay-pill {
    display: inline-flex;
    align-items: center;
    gap: .35rem;
    padding: .25rem .7rem;
    border-radius: 999px;
    font-size: .75rem;
    font-weight: 700;
    line-height: 1.3;
    white-space: nowrap;
    border: 1px solid transparent;
}
.pay-pill-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1.2em;
    height: 1.2em;
    border-radius: 50%;
    background: currentColor;
    color: #fff !important;
    font-size: .7em;
    font-weight: 900;
}
.pay-pill-icon::before { content: ""; }
.pay-pill .pay-pill-icon { color: currentColor; }
.pay-pill .pay-pill-icon { background: currentColor; }
.pay-pill--success {
    background: var(--success-soft, #dcfce7);
    color: var(--success, #16a34a);
    border-color: color-mix(in srgb, var(--success, #16a34a) 25%, transparent);
}
.pay-pill--warning {
    background: var(--warning-soft, #fef3c7);
    color: var(--warning, #d97706);
    border-color: color-mix(in srgb, var(--warning, #d97706) 25%, transparent);
}
.pay-pill--danger {
    background: var(--danger-soft, #fee2e2);
    color: var(--danger, #dc2626);
    border-color: color-mix(in srgb, var(--danger, #dc2626) 25%, transparent);
}

/* Progress bar */
.pay-bar {
    display: block;
    width: 100%;
    max-width: 180px;
    height: 6px;
    background: var(--bg-secondary, #f1f5f9);
    border-radius: 999px;
    overflow: hidden;
    position: relative;
}
.pay-bar-fill {
    display: block;
    height: 100%;
    border-radius: 999px;
    transition: width .3s ease;
}
.pay-bar-fill--success { background: var(--success, #16a34a); }
.pay-bar-fill--warning {
    background: linear-gradient(90deg, var(--warning, #d97706), var(--warning, #f59e0b));
}
.pay-bar-fill--danger  { background: var(--danger, #dc2626); opacity: .35; }

/* Status cell layout */
.pay-status-cell {
    display: flex;
    flex-direction: column;
    gap: .4rem;
    align-items: flex-start;
    min-width: 180px;
}
.pay-status-meta {
    display: flex;
    flex-wrap: wrap;
    gap: .3rem;
    font-size: .72rem;
    color: var(--text-muted);
    line-height: 1.3;
}
.pay-status-meta > span { white-space: nowrap; }

/* Mobile: keep pills readable */
@media (max-width: 768px) {
    .payment-status-filter {
        padding: .4rem;
        gap: .35rem;
    }
    .pay-chip {
        flex: 1 1 auto;
        justify-content: center;
        font-size: .78rem;
        padding: .4rem .5rem;
    }
    .pay-status-cell { min-width: 0; }
    .pay-bar { max-width: none; }
}
