/* ═══════════════════════════════════════════════════════════
   Responsive — RTL Mobile-First  (Premium Warm Light v4)
   ═══════════════════════════════════════════════════════════ */

/* ══ TABLET & BELOW  (< 1100px) ════════════════════════════ */
@media (max-width: 1099.98px) {

  .layout {
    display: block;
    padding: 14px 14px 32px;
    direction: rtl;
  }

  /* ── Sidebar → full-height drawer from the RIGHT (RTL) ── */
  .sidebar {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(85vw, 320px);
    height: 100dvh;
    border-radius: 0 0 0 var(--r-xl);
    transform: translateX(110%);
    transition: transform .3s cubic-bezier(.22,.68,0,1.15);
    overflow-y: auto;
    overflow-x: hidden;
    z-index: 300;
    box-shadow: -8px 0 40px rgba(120,100,70,.18), -2px 0 8px rgba(120,100,70,.10);
    border: none;
    border-radius: var(--r-xl) 0 0 var(--r-xl);
  }
  .sidebar.open { transform: translateX(0) }

  /* Drawer header — amber gradient like main header */
  .sidebar-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    position: sticky;
    top: 0;
    z-index: 2;
    padding: 14px 16px;
    background: var(--grad-brand);
    border-bottom: 1px solid rgba(255,255,255,.18);
  }
  .sidebar-head__title {
    font-size: 16px;
    font-weight: 900;
    color: #fff;
    text-shadow: 0 1px 4px rgba(0,0,0,.16);
  }
  .sidebar-close {
    border-color: rgba(255,255,255,.40);
    background: rgba(255,255,255,.20);
    color: #fff;
  }
  .sidebar-close:hover { background: rgba(255,255,255,.32) }

  .sidebar-body { padding: 14px }

  .sidebar-section {
    margin-top: 12px;
    background: var(--bg);
    border: 1.5px solid var(--border);
    border-radius: var(--r);
    padding: 10px;
  }
  .sidebar-title {
    font-size: 10px;
    font-weight: 800;
    letter-spacing: 1px;
    text-transform: uppercase;
    color: var(--muted);
    padding: 0 4px;
    margin-bottom: 6px;
  }

  .btn-nav {
    min-height: 48px;
    margin: 2px 0;
    padding: 10px 14px;
    border-radius: var(--r-sm);
    font-size: 14px;
    font-weight: 700;
    justify-content: space-between;
  }
  .btn-nav::after {
    content: '‹';
    font-size: 18px;
    opacity: .35;
    font-weight: 400;
    flex-shrink: 0;
  }
  .btn-nav.active::after,
  .btn-nav.primary::after { opacity: .75; color: var(--brand-deep) }
  .btn-nav.danger::after  { opacity: .75; color: var(--danger) }

  .layout > .content { width: 100%; padding: 0 }

  /* Chip grid single col on drawer */
  .chip-grid { grid-template-columns: 1fr 1fr }
}

/* ══ MOBILE  (< 900px) ══════════════════════════════════════ */
@media (max-width: 899.98px) {

  /* Forms collapse */
  .form-row { grid-template-columns: 1fr !important }
  .grid-2, .grid-3, .grid-4,
  .two-col, .row-2, .row-3 { grid-template-columns: 1fr !important }
  .mv-sections, .pp-knesset-grid, .pp-tables-row, .pp-two-col {
    grid-template-columns: 1fr !important;
  }

  /* Header actions stack */
  .header-actions {
    align-items: stretch !important;
    flex-direction: column;
  }
  .header-actions h2,
  .header-actions h3 {
    width: 100%;
    text-align: right !important;
    margin: 0 0 8px !important;
  }
  .header-actions .btn,
  .header-actions button,
  .links .btn,
  .links button,
  .smart-search > * { width: 100% !important }
  .smart-search input { min-width: 0 !important }

  /* Touch targets */
  input, select, textarea, button, .btn { min-height: 46px }
  .iconbtn { width: 44px; height: 44px; border-radius: var(--r-sm) }

  /* ── Stacked tables → cards ── */
  .table.table-stacked,
  .table-stacked { min-width: 0 !important }
  .table-stacked thead { display: none !important }
  .table-stacked tbody { display: grid !important; gap: 10px }
  .table-stacked tr {
    display: block !important;
    background: var(--card);
    border: 1.5px solid var(--border);
    border-radius: var(--r);
    padding: 14px 14px 10px;
    box-shadow: var(--sh-xs);
    transition: box-shadow var(--t-base);
  }
  .table-stacked tr:hover { box-shadow: var(--sh-sm) }

  /* Primary cell = card title */
  .table-stacked td[data-col="primary"] {
    display: block !important;
    font-weight: 800;
    font-size: 15px;
    color: var(--text);
    padding: 0 0 10px 0 !important;
    border: 0 !important;
    border-bottom: 1.5px solid var(--bg-2) !important;
    margin-bottom: 8px;
  }
  .table-stacked td[data-col="primary"]::before { display: none }

  /* Regular cells = label : value rows */
  .table-stacked td:not([data-col="primary"]):not([data-label="الإجراءات"]):not([data-label="إجراء"]) {
    display: flex !important;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    border: 0 !important;
    padding: 6px 0 !important;
    font-size: 13px;
  }
  .table-stacked td:not([data-col="primary"]):not([data-label="الإجراءات"]):not([data-label="إجراء"]) + td {
    border-top: 1px solid var(--bg-3) !important;
  }
  .table-stacked td:not([data-col="primary"]):not([data-label="الإجراءات"]):not([data-label="إجراء"])::before {
    content: attr(data-label);
    color: var(--muted);
    font-weight: 700;
    font-size: 12px;
    flex: 0 0 auto;
    min-width: 80px;
    max-width: 45%;
    white-space: nowrap;
    text-align: right;
  }

  /* Action cell */
  .table-stacked td[data-label="الإجراءات"],
  .table-stacked td[data-label="إجراء"] {
    display: block !important;
    border: 0 !important;
    padding: 10px 0 0 !important;
    margin-top: 6px;
    border-top: 1.5px solid var(--bg-2) !important;
  }
  .table-stacked td[data-label="الإجراءات"]::before,
  .table-stacked td[data-label="إجراء"]::before { display: none }

  /* Buttons inside action cells — never full-width */
  .table-stacked td[data-label="الإجراءات"] .actions,
  .table-stacked td[data-label="إجراء"] .actions,
  .table-stacked td[data-label="الإجراءات"] .act-row,
  .table-stacked td[data-label="إجراء"] .act-row,
  .table-stacked td[data-label="الإجراءات"] .row-actions,
  .table-stacked td[data-label="إجراء"] .row-actions {
    display: flex !important;
    flex-direction: row !important;
    flex-wrap: wrap;
    gap: 8px !important;
    align-items: center;
  }
  .table-stacked td[data-label="الإجراءات"] .btn,
  .table-stacked td[data-label="الإجراءات"] button,
  .table-stacked td[data-label="إجراء"] .btn,
  .table-stacked td[data-label="إجراء"] button,
  .table-stacked td[data-label="الإجراءات"] a,
  .table-stacked td[data-label="إجراء"] a {
    width: auto !important;
    min-width: 80px !important;
    flex: 1 1 auto;
    max-width: 160px;
    font-size: 13px !important;
    padding: 0 12px !important;
    height: 40px !important;
    min-height: 40px !important;
    justify-content: center;
  }

  /* Table-wrap reset when stacked */
  .table-wrap { border: none; border-radius: 0; background: transparent; box-shadow: none }
  .table-stacked { background: transparent }

  /* Pager center */
  .pager { justify-content: center !important; flex-wrap: wrap }

  /* KPI grid */
  .stats-grid { grid-template-columns: repeat(3, minmax(0,1fr)) !important }
  .summary-strip { grid-template-columns: repeat(2, minmax(0,1fr)) !important }
  .kpi { padding: 16px }
  .kpi .num { font-size: clamp(22px, 3.5vw, 30px) }

  /* mem-table */
  .mem-table th,
  .mem-table td { padding: 8px 10px; font-size: 13px }
  .mem-table .name-info .sub { display: none }
  .av { width: 34px; height: 34px; font-size: 13px }

  /* pp-table needs scroll */
  .pp-table-wrap,
  .pp-table-block .pp-table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch }
  .pp-table { min-width: 500px }
}

/* ══ SMALL MOBILE  (< 640px) ════════════════════════════════ */
@media (max-width: 639.98px) {

  .layout { padding: 10px 10px 28px }
  .card { padding: 14px; border-radius: var(--r) }
  .content > h2, .page-title { font-size: 19px !important }

  /* Header compress */
  .site-header__inner { padding: 0 12px; gap: 5px }
  .brand { font-size: 13px; flex-shrink: 1; min-width: 0; overflow: hidden }
  .brand > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap }
  .site-header .btn { padding: 0 10px; height: 34px; font-size: 12px }
  .site-header .btn span { display: none }
  .site-header .btn.invert span { display: none }
  .nav-hide-mobile { display: none !important }
  .nav { gap: 4px }
  .site-header .btn svg { flex-shrink: 0 }

  /* Sidebar full-width on small */
  .sidebar { width: min(92vw, 320px) }

  /* KPI 2 columns */
  .stats-grid { grid-template-columns: repeat(2, minmax(0,1fr)) !important }
  .summary-strip { grid-template-columns: 1fr 1fr !important }
  .kpi { padding: 13px }
  .kpi .num { font-size: 22px }

  /* Chip grid 1 col */
  .chip-grid { grid-template-columns: 1fr }

  /* Member actions — compact icon row */
  .act-row,
  .actions {
    flex-wrap: nowrap !important;
    gap: 6px !important;
    justify-content: flex-start !important;
    overflow-x: auto;
    padding-bottom: 2px;
    -webkit-overflow-scrolling: touch;
  }
  .act-row form,
  .actions form {
    display: inline-flex !important;
    flex: 0 0 auto !important;
  }
  .act-btn,
  .actions .btn,
  .actions button,
  .actions a {
    width: 40px !important;
    height: 40px !important;
    min-width: 40px !important;
    max-width: 40px !important;
    min-height: 40px !important;
    padding: 0 !important;
    flex: 0 0 40px !important;
    justify-content: center !important;
    border-radius: var(--r-sm) !important;
    font-size: 0 !important;
  }
  .act-btn span,
  .actions .btn span,
  .actions button span,
  .actions a span { display: none !important }
  .act-btn svg,
  .actions svg { width: 18px; height: 18px }

  /* Quick links full width */
  .quick-links { flex-direction: column }
  .ql-btn { width: 100%; justify-content: flex-start }

  /* Inf / type-stats grids: 2 cols */
  .inf-grid,
  .type-stats-grid {
    grid-template-columns: 1fr 1fr !important;
    gap: 10px;
  }
  .inf-card, .ts-card { padding: 12px 13px }
  .inf-big, .ts-big { font-size: 26px }

  /* Member grid: 1 col */
  .member-grid { grid-template-columns: 1fr !important }
  .member-card { padding: 12px }

  /* Forms */
  .section-label { font-size: 10px }

  /* Profile hero */
  .profile-hero,
  .entity-head,
  .mv-hero,
  .pp-hero { flex-direction: column; align-items: center; text-align: center }
  .profile-badges,
  .mv-actions,
  .pp-hero-meta { justify-content: center }
  .mv-card h3 { font-size: 11px }

  /* Entity / members headers */
  .entity-head .btn,
  .type-page-header .btn,
  .members-header .btn { font-size: 13px; padding: 0 12px; height: 40px; min-height: 38px }

  /* Approval cards */
  .approval-card { padding: 12px }
  .approval-meta { flex-direction: column; gap: 5px }
  .approval-actions { flex-direction: column; align-items: stretch }
  .approval-actions .notes-input { max-width: 100% !important }
  .approval-actions .btn { width: 100%; justify-content: center }

  /* stats-table → cards */
  .stats-table { min-width: 0 !important }
  .table-wrap { border-radius: var(--r) }
  .stats-table thead { display: none !important }
  .stats-table tbody { display: grid !important; gap: 8px }
  .stats-table tr {
    display: grid !important;
    grid-template-columns: 1fr auto;
    align-items: center;
    background: var(--card);
    border: 1.5px solid var(--border);
    border-radius: var(--r-sm);
    padding: 10px 12px;
    gap: 4px 10px;
  }
  .stats-table td {
    padding: 2px 0 !important;
    border: none !important;
    font-size: 13px;
  }
  .stats-table td:first-child {
    grid-column: 1 / -1;
    font-weight: 700;
    font-size: 14px;
    color: var(--text);
    padding-bottom: 6px !important;
    border-bottom: 1px solid var(--border) !important;
    margin-bottom: 2px;
  }
  .stats-table td:not(:first-child):not(:nth-child(2)) {
    font-size: 12px;
    color: var(--muted);
  }
  .stats-table td:last-child { grid-column: 1 / -1 }
  .stats-table .progress { min-width: 0 !important; width: 100% }

  /* mem-table font size */
  .mem-table th,
  .mem-table td { padding: 7px 8px; font-size: 12px }
  .name-info .nm { font-size: 13px }
}

/* ══ VERY SMALL  (< 420px) ══════════════════════════════════ */
@media (max-width: 419.98px) {

  .inf-grid,
  .type-stats-grid {
    grid-template-columns: 1fr !important;
    gap: 8px;
  }
  .inf-label, .ts-lbl { max-width: 140px }
  .inf-card h4, .ts-card h4 { font-size: 10px; margin-bottom: 7px }
  .inf-row, .ts-row { gap: 6px; font-size: 12px; margin-bottom: 5px }
  .inf-big, .ts-big { font-size: 28px }
}

/* ══ VERY SMALL  (< 380px) ══════════════════════════════════ */
@media (max-width: 379.98px) {

  .stats-grid { grid-template-columns: 1fr 1fr !important }
  .summary-strip { grid-template-columns: 1fr 1fr !important }
  .act-btn { min-width: 40px; padding: 0 }
  .site-header .btn { padding: 0 7px; height: 32px }
  .brand { font-size: 13px }
  .card { padding: 12px }
}

/* ══ TABLET  (641px – 1099px) tweaks ════════════════════════ */
@media (min-width: 641px) and (max-width: 1099.98px) {
  .stats-grid { grid-template-columns: repeat(4, minmax(0,1fr)) }
  .summary-strip { grid-template-columns: repeat(4, minmax(0,1fr)) }
  .grid-2 { grid-template-columns: repeat(2, minmax(0,1fr)) }
}

/* ══ MOBILE SAFE AREA (notched devices) ═════════════════════ */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  .layout { padding-bottom: calc(28px + env(safe-area-inset-bottom)) }
}

/* ══ TOUCH DEVICE ENHANCEMENTS ══════════════════════════════ */
@media (hover: none) and (pointer: coarse) {
  /* Remove hover transforms that feel wrong on touch */
  .card:hover,
  .kpi:hover,
  .inf-card:hover,
  .ts-card:hover,
  .member-card:hover { transform: none }

  /* Ensure tap targets are large enough */
  button, .btn, .btn-nav, .ql-btn { min-height: 48px }
  .iconbtn { width: 48px; height: 48px }

  /* Active state feedback instead of hover */
  button:active, .btn:active { opacity: .8; transform: scale(.97) }
  .btn-nav:active { background: var(--bg-3) }
}
