/* ===== Reset & Cloak ===== */
[x-cloak] { display: none !important; }
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* ===== CSS Variables ===== */
:root {
  /* Core colors */
  --bg-primary: #0a0e14;
  --bg-card: #131920;
  --bg-elevated: #1a2332;
  --border-default: #F37421;
  --border-active: #36C2D9;
  --text-primary: #FFFFFF;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;

  /* Buttons */
  --btn-bg: #131920;
  --btn-text: #FFFFFF;
  --btn-border: #334155;
  --btn-primary-bg: #36C2D9;
  --btn-primary-text: #FFFFFF;
  --btn-primary-border: #36C2D9;
  --btn-success: #10B981;
  --btn-info: #36C2D9;
  --btn-warning: #F59E0B;
  --btn-danger: #EF4444;
  --status-completed: #10B981;
  --status-cancelled: #EF4444;
  --status-pending: #F59E0B;
  --status-delivered: #36C2D9;
  --btn-active-bg: #0f766e;
  --btn-active-text: #FFFFFF;
  --btn-active-border: #36C2D9;
  --btn-success-bg: transparent;
  --btn-success-text: #10B981;
  --btn-success-border: #10B981;
  --btn-info-bg: transparent;
  --btn-info-text: #36C2D9;
  --btn-info-border: #36C2D9;
  --btn-warning-bg: transparent;
  --btn-warning-text: #F59E0B;
  --btn-warning-border: #F59E0B;
  --btn-danger-bg: transparent;
  --btn-danger-text: #EF4444;
  --btn-danger-border: #EF4444;
  --accent-glow: rgba(54, 194, 217, 0.15);

  /* Sidebar */
  --sidebar-width: 260px;
  --sidebar-bg: #080c12;
  --sidebar-link-bg: transparent;
  --sidebar-link-text: #94a3b8;
  --sidebar-link-border: #1e293b;
  --sidebar-active-text: #FFFFFF;
  --sidebar-active-glow: #36C2D9;
  --sidebar-hover-glow: #F37421;
  --card-highlight: #36C2D9;

  /* Table */
  --tbl-header-bg: linear-gradient(135deg, #0f1923 0%, #162033 100%);
  --tbl-header-text: #e2e8f0;
  --tbl-row-bg: rgba(19, 25, 32, 0.6);
  --tbl-row-alt-bg: rgba(26, 35, 50, 0.4);
  --tbl-row-hover: rgba(54, 194, 217, 0.07);
  --tbl-border: rgba(255, 255, 255, 0.04);
  --tbl-text: #d1d5db;
  --tbl-total-bg: #1a2332;
  --tbl-total-text: #FFFFFF;
  --tbl-divider-bg: #242424;
  --tbl-divider-text: #94a3b8;

  /* Spacing */
  --space-xs: 4px;
  --space-sm: 8px;
  --space-md: 16px;
  --space-lg: 24px;
  --space-xl: 32px;

  /* Font sizes */
  --font-xs: 11px;
  --font-sm: 13px;
  --font-base: 14px;
  --font-lg: 16px;
  --font-xl: 20px;
  --font-2xl: 28px;
  --font-3xl: 36px;

  /* Radius */
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --radius-xl: 20px;
}

/* ===== Base ===== */
html { overflow-x: hidden; }
body { overflow-x: hidden; max-width: 100vw; }
body {
  background: var(--bg-primary);
  color: var(--text-primary);
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  min-height: 100vh;
  font-size: var(--font-base);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}
body[dir="rtl"] { font-family: 'Cairo', sans-serif; }

/* ===== Typography ===== */
h1 { font-size: var(--font-3xl); font-weight: 700; letter-spacing: -0.5px; }
h2 { font-size: var(--font-2xl); font-weight: 700; letter-spacing: -0.3px; }
h3 { font-size: var(--font-xl); font-weight: 600; }
h4 { font-size: var(--font-lg); font-weight: 600; }

/* ===== Main Content ===== */
.main-content {
  padding: var(--space-lg);
  padding-top: 72px;
  transition: padding 0.35s cubic-bezier(0.4, 0, 0.2, 1);
  width: 100%;
  min-height: 100vh;
}
body.sidebar-open .main-content { padding-left: calc(var(--sidebar-width) + var(--space-lg)); }
body[dir="rtl"].sidebar-open .main-content { padding-left: var(--space-lg); padding-right: calc(var(--sidebar-width) + var(--space-lg)); }
body.sidebar-closed .main-content { padding-left: var(--space-lg); padding-right: var(--space-lg); }

/* ===== Sidebar ===== */
.sidebar {
  position: fixed !important;
  top: 0 !important; left: 0 !important; right: auto !important; bottom: 0 !important;
  width: var(--sidebar-width) !important;
  max-width: 80vw;
  height: 100vh !important;
  z-index: 50;
  transition: transform 0.35s cubic-bezier(0.4, 0, 0.2, 1);
  overflow-y: auto; overflow-x: hidden;
  padding: 20px 14px 20px 14px;
  background: var(--sidebar-bg);
  border-right: 1px solid rgba(255,255,255,0.04);
  transform: translateX(-100%);
  display: flex !important; flex-direction: column !important;
  box-shadow: 4px 0 30px rgba(0,0,0,0.5);
  backdrop-filter: blur(20px);
}
body[dir="rtl"] .sidebar {
  left: auto !important; right: 0 !important;
  transform: translateX(100%);
  border-right: none; border-left: 1px solid rgba(255,255,255,0.04);
  box-shadow: -4px 0 30px rgba(0,0,0,0.5);
}
.sidebar.open { transform: translateX(0) !important; }

/* Sidebar title / logo */
.sidebar-title { text-align: center; margin-bottom: 8px; padding-top: 40px; }

/* User info */
.sidebar-user {
  text-align: center; margin-bottom: 20px; padding: 16px 12px;
  background: rgba(255,255,255,0.03); border-radius: var(--radius-md);
  border: 1px solid rgba(255,255,255,0.04);
}
.sidebar-user-label { font-size: var(--font-xs); color: var(--text-muted); margin-bottom: 2px; }
.sidebar-user-name { font-weight: 700; color: var(--text-primary); font-size: var(--font-lg); }
.sidebar-user-role {
  font-size: var(--font-xs); color: var(--border-active);
  text-transform: uppercase; letter-spacing: 2px; font-weight: 600; margin-top: 2px;
}

/* Sidebar nav */
.sidebar-nav {
  flex: 1 1 auto; overflow-y: auto;
  display: flex; flex-direction: column; gap: 2px;
  padding: 0 2px;
}
.sidebar-group-label {
  font-size: 10px !important; text-transform: uppercase; letter-spacing: 2px;
  color: var(--text-muted) !important; padding: 20px 12px 8px !important;
  font-weight: 700; opacity: 0.7;
}

/* Sidebar links */
.sidebar-link {
  display: flex; align-items: center; width: 100%; text-align: left;
  padding: 11px 15px; border-radius: var(--radius-sm);
  font-weight: 500; font-size: var(--font-sm);
  color: var(--sidebar-link-text); text-decoration: none;
  border: 1px solid var(--sidebar-link-border);
  background: var(--sidebar-link-bg);
  transition: all 0.28s cubic-bezier(0.34, 1.56, 0.64, 1);
  position: relative; overflow: hidden;
  margin-bottom: 2px;
}

/* Hover: slide + orange glow sweep */
.sidebar-link::after {
  content: '';
  position: absolute; inset: 0;
  background: linear-gradient(90deg,
    transparent,
    color-mix(in srgb, var(--sidebar-active-glow) 22%, transparent),
    transparent);
  transform: translateX(-120%);
  transition: transform 0.5s ease;
  pointer-events: none;
}
.sidebar-link:hover {
  color: var(--sidebar-active-text);
  border-color: color-mix(in srgb, var(--sidebar-active-glow) 40%, transparent);
  transform: translateX(4px);
  box-shadow: inset 0 0 16px color-mix(in srgb, var(--sidebar-active-glow) 8%, transparent);
}
body[dir="rtl"] .sidebar-link:hover { transform: translateX(-4px); }
.sidebar-link:hover::after { transform: translateX(120%); }

/* Active: animated gradient sweep + glowing bar + breathing glow */
.sidebar-link-active {
  color: var(--sidebar-active-text) !important;
  font-weight: 700 !important;
  border: 1px solid color-mix(in srgb, var(--sidebar-active-glow) 45%, transparent) !important;
  background: linear-gradient(110deg,
    color-mix(in srgb, var(--sidebar-active-glow) 22%, transparent) 0%,
    color-mix(in srgb, var(--sidebar-active-glow) 8%, transparent) 50%,
    color-mix(in srgb, var(--sidebar-active-glow) 22%, transparent) 100%) !important;
  background-size: 200% 100% !important;
  animation: sidebar-sweep 3s ease infinite, sidebar-breathe 2.5s ease-in-out infinite;
  position: relative;
  transform: translateX(2px);
}
.sidebar-link-active::before {
  content: '';
  position: absolute; left: 0; top: 12%; height: 76%; width: 3px;
  background: var(--sidebar-active-glow);
  border-radius: 0 4px 4px 0;
  box-shadow: 0 0 12px var(--sidebar-active-glow), 0 0 4px var(--sidebar-active-glow);
  z-index: 2;
}
body[dir="rtl"] .sidebar-link-active {
  transform: translateX(-2px);
}
body[dir="rtl"] .sidebar-link-active::before {
  left: auto; right: 0; border-radius: 4px 0 0 4px;
}
.sidebar-link-active::after { display: none; }

@keyframes sidebar-sweep {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}
@keyframes sidebar-breathe {
  0%, 100% { box-shadow: inset 0 0 16px color-mix(in srgb, var(--sidebar-active-glow) 8%, transparent), 0 0 10px color-mix(in srgb, var(--sidebar-active-glow) 12%, transparent); }
  50% { box-shadow: inset 0 0 24px color-mix(in srgb, var(--sidebar-active-glow) 15%, transparent), 0 0 22px color-mix(in srgb, var(--sidebar-active-glow) 22%, transparent); }
}

/* Logout */
.sidebar-logout {
  margin-top: 12px; padding: 10px 14px; border-radius: var(--radius-sm);
  font-weight: 600; font-size: var(--font-sm);
  background: rgba(239, 68, 68, 0.1); border: 1px solid rgba(239, 68, 68, 0.2);
  color: #f87171; cursor: pointer; width: 100%;
  transition: all 0.2s;
}
.sidebar-logout:hover {
  background: rgba(239, 68, 68, 0.2); border-color: rgba(239, 68, 68, 0.4);
  box-shadow: 0 0 16px rgba(239, 68, 68, 0.15);
}

/* ===== Cards ===== */
.app-card {
  background: var(--bg-card);
  border: 1px solid rgba(255,255,255,0.06);
  border-radius: var(--radius-md);
  padding: var(--space-md);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.app-card:hover {
  border-color: rgba(54, 194, 217, 0.15);
  box-shadow: 0 8px 32px rgba(0,0,0,0.3), 0 0 16px rgba(54, 194, 217, 0.05);
}
.app-card-active {
  border-color: var(--border-active) !important;
  box-shadow: 0 0 24px rgba(54, 194, 217, 0.2);
}

/* ===== Inputs ===== */
.app-input {
  background: var(--bg-card); border: 1px solid rgba(255,255,255,0.08);
  color: var(--text-primary); padding: 10px 14px;
  border-radius: var(--radius-sm); width: 100%; outline: none;
  transition: all 0.2s; font-size: var(--font-base);
}
.app-input:focus {
  border-color: var(--border-active);
  box-shadow: 0 0 0 3px rgba(54, 194, 217, 0.12), 0 0 16px rgba(54, 194, 217, 0.08);
  background: var(--bg-elevated);
}

/* ===== Buttons ===== */
.app-btn {
  padding: 10px 18px; border-radius: var(--radius-sm); font-weight: 600;
  transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1); cursor: pointer;
  background: var(--btn-bg); border: 1px solid var(--btn-border);
  color: var(--btn-text); display: inline-flex; align-items: center;
  justify-content: center; font-size: var(--font-sm); white-space: nowrap;
  position: relative; overflow: hidden;
}
.app-btn::after {
  content: ''; position: absolute; inset: 0;
  background: linear-gradient(90deg,
    transparent,
    color-mix(in srgb, currentColor 28%, transparent),
    transparent);
  transform: translateX(-120%); transition: transform 0.55s ease;
  pointer-events: none;
}
.app-btn:hover:not(:disabled)::after { transform: translateX(120%); }
.app-btn:hover:not(:disabled) {
  box-shadow: 0 0 16px color-mix(in srgb, currentColor 45%, transparent);
  transform: translateY(-1px);
  filter: brightness(1.08);
}
.app-btn:active:not(:disabled) { transform: translateY(0); }
.app-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.app-btn-primary {
  background: var(--btn-primary-bg);
  color: var(--btn-primary-text);
  border: 1px solid var(--btn-primary-border); font-weight: 700;
}
.app-btn-primary:hover:not(:disabled) {
  box-shadow: 0 4px 20px color-mix(in srgb, var(--btn-primary-bg) 50%, transparent);
  filter: brightness(1.12);
}

/* ===== Tables — unified, fully theme-driven ===== */
.overflow-x-auto {
  overflow-x: auto; -webkit-overflow-scrolling: touch; max-width: 100%;
  border-radius: var(--radius-lg);
  border: 1px solid var(--tbl-border);
  box-shadow: 0 4px 24px rgba(0,0,0,0.22);
}

table, .styled-table {
  font-size: var(--font-sm); width: 100%;
  border-collapse: separate; border-spacing: 0;
  border-radius: var(--radius-lg);
}

/* Header — all identical, driven by settings */
table thead th, .styled-table thead th {
  background: var(--tbl-header-bg) !important;
  color: var(--tbl-header-text) !important;
  padding: 13px 16px !important;
  font-weight: 700; font-size: var(--font-xs);
  text-transform: uppercase; letter-spacing: 0.8px;
  border: none !important;
  border-right: 1px solid var(--tbl-border) !important;
  border-bottom: 2px solid var(--border-active) !important;
  white-space: nowrap; position: sticky; top: 0; z-index: 2;
  text-align: center; vertical-align: middle;
}
table thead th:last-child, .styled-table thead th:last-child { border-right: none !important; }

/* Body — every row identical */
table tbody tr, .styled-table tbody tr {
  background: var(--tbl-row-bg);
  transition: all 0.18s cubic-bezier(0.4,0,0.2,1);
}
table tbody td, .styled-table tbody td {
  padding: 12px 16px !important;
  border: none !important;
  border-bottom: 1px solid var(--tbl-border) !important;
  border-right: 1px solid var(--tbl-border) !important;
  color: var(--tbl-text);
  text-align: center; vertical-align: middle;
  font-size: var(--font-sm);
  background: var(--tbl-row-bg);
}
table tbody td:last-child, .styled-table tbody td:last-child { border-right: none !important; }

/* Hover — accent bar + soft glow from accent color */
table tbody tr:hover td, .styled-table tbody tr:hover td {
  background: color-mix(in srgb, var(--border-active) 8%, var(--tbl-row-bg)) !important;
}
table tbody tr:hover td:first-child, .styled-table tbody tr:hover td:first-child {
  box-shadow: inset 3px 0 0 var(--border-active);
}
body[dir="rtl"] table tbody tr:hover td:first-child,
body[dir="rtl"] .styled-table tbody tr:hover td:first-child {
  box-shadow: inset -3px 0 0 var(--border-active);
}

/* Footer — header tone, all identical */
table tfoot td, table tfoot th,
.styled-table tfoot td, .styled-table tfoot th {
  background: var(--tbl-header-bg) !important;
  color: var(--tbl-header-text) !important;
  padding: 13px 16px !important; font-weight: 700;
  border: none !important;
  border-top: 2px solid var(--border-active) !important;
  border-right: 1px solid var(--tbl-border) !important;
  text-align: center;
}
table tfoot td:last-child, .styled-table tfoot td:last-child { border-right: none !important; }

/* Rounded shell */
table thead tr:first-child th:first-child,
.styled-table thead tr:first-child th:first-child { border-top-left-radius: var(--radius-lg); }
table thead tr:first-child th:last-child,
.styled-table thead tr:first-child th:last-child { border-top-right-radius: var(--radius-lg); }
table tbody tr:last-child td:first-child,
.styled-table tbody tr:last-child td:first-child { border-bottom-left-radius: var(--radius-lg); }
table tbody tr:last-child td:last-child,
.styled-table tbody tr:last-child td:last-child { border-bottom-right-radius: var(--radius-lg); }

/* Special semantic rows */
.styled-table tr.row-total td, table tr.row-total td,
.styled-table tr.row-subtotal td, table tr.row-subtotal td {
  background: var(--tbl-total-bg) !important;
  color: var(--tbl-total-text) !important;
  font-weight: 700;
  border-top: 2px solid var(--border-active) !important;
}
.styled-table tr.row-divider td, table tr.row-divider td {
  background: var(--tbl-divider-bg) !important;
  color: var(--tbl-divider-text) !important;
  font-weight: 600; text-transform: uppercase;
  letter-spacing: 1.2px; font-size: var(--font-xs);
}
.styled-table tr.row-spacer td, table tr.row-spacer td {
  background: transparent !important; border: none !important;
  padding: 5px !important; height: 5px;
}


/* ===== Scrollbar ===== */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.1); border-radius: 10px; }
::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,0.2); }

/* ===== Modal ===== */
.modal-backdrop {
  position: fixed;
  top: 0; right: 0; bottom: 0; left: 0;
  width: 100vw; height: 100vh;
  background: rgba(0, 0, 0, 0.85);
  display: flex; align-items: center; justify-content: center;
  padding: var(--space-md); z-index: 100;
  overflow-y: auto;
  /* لا نستخدم backdrop-filter هنا: يسبب اضطراب تموضع مع position:fixed */
  animation: fadeIn 0.2s ease-out;
}
.modal-backdrop > div { max-width: 95vw; max-height: 90vh; overflow-y: auto; }

/* ===== Hamburger ===== */
.menu-toggle {
  display: block; position: fixed; top: 14px; left: 14px; z-index: 60;
  background: linear-gradient(135deg, var(--border-active), #2da8bd);
  color: white; border: none; padding: 10px 12px; border-radius: var(--radius-sm);
  font-size: 20px; cursor: pointer; line-height: 1;
  box-shadow: 0 4px 16px rgba(54, 194, 217, 0.3);
  transition: all 0.2s;
}
.menu-toggle:hover { transform: scale(1.05); box-shadow: 0 4px 24px rgba(54, 194, 217, 0.4); }
body[dir="rtl"] .menu-toggle { left: auto; right: 14px; }

/* ===== Sidebar Overlay ===== */
.sidebar-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.6); z-index: 45;
  cursor: pointer; backdrop-filter: blur(4px);
}
.sidebar-overlay.active { display: block; animation: fadeIn 0.2s ease-out; }

/* ===== Animations ===== */
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes fadeInUp { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: translateY(0); } }
@keyframes pageIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideDown { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; transform: translateY(0); } }
@keyframes pulse-active {
  0%, 100% { box-shadow: 0 0 8px rgba(54, 194, 217, 0.3); }
  50% { box-shadow: 0 0 20px rgba(54, 194, 217, 0.5); }
}
.pulse-active { animation: pulse-active 2s infinite; }
.animate-fadeInUp { animation: fadeInUp 0.4s ease-out both; }
.animate-fadeIn { animation: fadeIn 0.3s ease-out both; }

/* Table row stagger */
table tbody tr { animation: fadeInUp 0.25s ease-out both; }
table tbody tr:nth-child(1) { animation-delay: 0.02s; }
table tbody tr:nth-child(2) { animation-delay: 0.04s; }
table tbody tr:nth-child(3) { animation-delay: 0.06s; }
table tbody tr:nth-child(4) { animation-delay: 0.08s; }
table tbody tr:nth-child(5) { animation-delay: 0.1s; }
table tbody tr:nth-child(6) { animation-delay: 0.12s; }
table tbody tr:nth-child(7) { animation-delay: 0.14s; }
table tbody tr:nth-child(8) { animation-delay: 0.16s; }
table tbody tr:nth-child(9) { animation-delay: 0.18s; }
table tbody tr:nth-child(10) { animation-delay: 0.2s; }
table tbody tr:nth-child(11) { animation-delay: 0.22s; }
table tbody tr:nth-child(12) { animation-delay: 0.24s; }

/* ===== Images ===== */
img, video, svg { max-width: 100%; height: auto; }

/* ===== Mobile ===== */
@media (max-width: 1024px) {
  .sidebar { width: min(280px, 85vw) !important; }
  body.sidebar-open .main-content,
  body[dir="rtl"].sidebar-open .main-content,
  body.sidebar-closed .main-content,
  body[dir="rtl"].sidebar-closed .main-content {
    padding-left: var(--space-md) !important;
    padding-right: var(--space-md) !important;
    padding-top: 72px !important;
  }
  .sidebar a, .sidebar button, .sidebar nav {
    position: relative; z-index: 56; pointer-events: auto !important;
  }
}
@media (max-width: 768px) {
  [class*="grid-cols-2"], [class*="grid-cols-3"], [class*="grid-cols-4"],
  [class*="grid-cols-5"], [class*="grid-cols-6"] {
    grid-template-columns: 1fr !important;
  }
  .flex { flex-wrap: wrap; }
  table { font-size: var(--font-xs); }
  table thead th, table tbody td { padding: 8px 10px; }
}

/* ===== Settings Tabs ===== */
.tab-btn {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 12px 24px; font-weight: 600; font-size: var(--font-base);
  color: var(--text-secondary); background: transparent;
  border: none; border-bottom: 3px solid transparent;
  cursor: pointer; transition: all 0.25s cubic-bezier(0.4,0,0.2,1);
  margin-bottom: -2px; border-radius: var(--radius-sm) var(--radius-sm) 0 0;
}
.tab-btn:hover { color: var(--text-primary); background: rgba(255,255,255,0.03); }
.tab-btn-active {
  color: var(--border-active) !important;
  border-bottom-color: var(--border-active) !important;
  background: linear-gradient(180deg, transparent, rgba(54,194,217,0.06)) !important;
}


/* ===== Semantic colored buttons (theme-driven: bg + text + border) ===== */
.app-btn.btn-success { background: var(--btn-success-bg) !important; color: var(--btn-success-text) !important; border-color: var(--btn-success-border) !important; }
.app-btn.btn-info    { background: var(--btn-info-bg) !important;    color: var(--btn-info-text) !important;    border-color: var(--btn-info-border) !important; }
.app-btn.btn-warning { background: var(--btn-warning-bg) !important; color: var(--btn-warning-text) !important; border-color: var(--btn-warning-border) !important; }
.app-btn.btn-danger  { background: var(--btn-danger-bg) !important;  color: var(--btn-danger-text) !important;  border-color: var(--btn-danger-border) !important; }
.app-btn.btn-success:hover:not(:disabled) { box-shadow: 0 0 16px color-mix(in srgb, var(--btn-success-border) 45%, transparent); filter: brightness(1.1); }
.app-btn.btn-info:hover:not(:disabled)    { box-shadow: 0 0 16px color-mix(in srgb, var(--btn-info-border) 45%, transparent); filter: brightness(1.1); }
.app-btn.btn-warning:hover:not(:disabled) { box-shadow: 0 0 16px color-mix(in srgb, var(--btn-warning-border) 45%, transparent); filter: brightness(1.1); }
.app-btn.btn-danger:hover:not(:disabled)  { box-shadow: 0 0 16px color-mix(in srgb, var(--btn-danger-border) 45%, transparent); filter: brightness(1.1); }


/* ===== Active toggle buttons (This Month, Language, etc.) ===== */
.app-btn.app-card-active,
button.app-card-active {
  background: var(--btn-active-bg) !important;
  color: var(--btn-active-text) !important;
  border: 1px solid var(--btn-active-border) !important;
  box-shadow: 0 0 14px color-mix(in srgb, var(--btn-active-border) 35%, transparent) !important;
}


/* ===== Page frame — thin glowing border around content ===== */
.main-content {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
.main-content > * {
  position: relative;
}
/* Optional page wrapper frame */
.page-frame {
  border: 1px solid color-mix(in srgb, var(--border-active) 18%, rgba(255,255,255,0.05));
  border-radius: var(--radius-lg);
  padding: var(--space-lg);
  box-shadow: inset 0 0 40px color-mix(in srgb, var(--border-active) 3%, transparent),
              0 0 0 1px rgba(255,255,255,0.02);
  background: color-mix(in srgb, var(--bg-card) 20%, transparent);
}


/* ===== Button hover: transparent fill, keep colored border, colored wave ===== */
.app-btn:hover:not(:disabled) {
  background: transparent !important;
}
.app-btn.btn-success:hover:not(:disabled) { color: var(--btn-success-text) !important; border-color: var(--btn-success-border) !important; }
.app-btn.btn-info:hover:not(:disabled)    { color: var(--btn-info-text) !important;    border-color: var(--btn-info-border) !important; }
.app-btn.btn-warning:hover:not(:disabled) { color: var(--btn-warning-text) !important; border-color: var(--btn-warning-border) !important; }
.app-btn.btn-danger:hover:not(:disabled)  { color: var(--btn-danger-text) !important;  border-color: var(--btn-danger-border) !important; }
/* Primary keeps its border color but goes transparent inside, text switches to border color for visibility */
.app-btn-primary:hover:not(:disabled) {
  background: transparent !important;
  color: var(--btn-primary-border) !important;
  border: 1px solid var(--btn-primary-border) !important;
}

/* ===== Worker Card (Withdrawals) ===== */
.worker-card { padding: 0; overflow: hidden; }
.worker-card-head {
  display: flex; align-items: center; gap: 10px;
  padding: 16px 18px 14px;
  border-bottom: 1px solid color-mix(in srgb, var(--border-active) 25%, transparent);
  background: linear-gradient(180deg, color-mix(in srgb, var(--border-active) 6%, transparent), transparent);
}
.worker-card-head h3 { font-size: var(--font-lg); font-weight: 700; margin: 0; }
.worker-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--border-active);
  box-shadow: 0 0 8px var(--border-active), 0 0 4px var(--border-active);
  flex-shrink: 0;
}
.worker-rows { padding: 6px 18px; }
.worker-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 0; font-size: var(--font-sm);
  border-bottom: 1px solid rgba(255,255,255,0.07);
}
.worker-row:last-child { border-bottom: none; }
.worker-row > span:first-child {
  color: var(--text-secondary); font-weight: 500;
  letter-spacing: 0.3px;
}
.worker-row > span:last-child {
  color: #FFFFFF; font-weight: 600; font-size: var(--font-base);
}
.worker-row-warn > span { color: #f87171 !important; }
/* padding for button so it doesn't touch edges */
.worker-card { padding-bottom: 14px; }
.worker-card > .app-btn { margin: 0 14px; width: calc(100% - 28px); }
.worker-remaining {
  display: flex; justify-content: space-between; align-items: center;
  margin: 12px 14px 16px; padding: 16px 18px;
  border-radius: var(--radius-md);
  border: 1px solid color-mix(in srgb, var(--card-highlight) 35%, transparent);
  background: linear-gradient(135deg, color-mix(in srgb, var(--card-highlight) 14%, transparent), color-mix(in srgb, var(--card-highlight) 4%, transparent));
  box-shadow: inset 0 0 24px color-mix(in srgb, var(--card-highlight) 6%, transparent);
}
.worker-remaining-label {
  font-weight: 700; font-size: var(--font-xs);
  text-transform: uppercase; letter-spacing: 1.5px;
  color: var(--text-secondary);
}
.worker-remaining-value { font-size: var(--font-2xl); font-weight: 800; letter-spacing: -0.5px; }
.worker-remaining .worker-remaining-value { color: var(--card-highlight); }
.worker-remaining.is-neg { border-color: color-mix(in srgb, var(--btn-danger-border) 40%, transparent); background: linear-gradient(135deg, color-mix(in srgb, var(--btn-danger-border) 12%, transparent), color-mix(in srgb, var(--btn-danger-border) 3%, transparent)); }
.worker-remaining.is-neg .worker-remaining-value { color: var(--btn-danger-text); }


/* ===== Preference Switch ===== */
.pref-switch {
  position: relative; flex-shrink: 0;
  width: 46px; height: 26px; padding: 0;
  border-radius: 999px; cursor: pointer;
  border: 1px solid rgba(255,255,255,0.12);
  background: rgba(255,255,255,0.06);
  transition: background 0.25s ease, border-color 0.25s ease;
}
.pref-switch:disabled { opacity: 0.55; cursor: not-allowed; }
.pref-knob {
  position: absolute; top: 50%; left: 3px;
  width: 18px; height: 18px; border-radius: 50%;
  transform: translateY(-50%);
  background: var(--text-muted);
  transition: left 0.25s cubic-bezier(0.34,1.4,0.64,1), background 0.25s ease;
}
.pref-switch.is-on {
  background: color-mix(in srgb, var(--btn-success-border) 26%, transparent);
  border-color: var(--btn-success-border);
}
.pref-switch.is-on .pref-knob {
  left: calc(100% - 21px);
  background: var(--btn-success-text);
}


/* ==================================================================
   MOBILE LAYER v1  —  applies ONLY at <=768px, desktop untouched
   ================================================================== */
@media (max-width: 768px) {

  /* ---------- 1. Typography: scale down (overrides inline --font-base) ---------- */
  body { font-size: 13px; }
  h1 { font-size: 19px; line-height: 1.25; letter-spacing: -0.2px; }
  h2 { font-size: 16px; line-height: 1.3; }
  h3 { font-size: 14px; }
  h4 { font-size: 13px; }

  .text-3xl { font-size: 19px !important; }
  .text-2xl { font-size: 17px !important; }
  .text-xl  { font-size: 15px !important; }
  .text-lg  { font-size: 14px !important; }
  .text-base{ font-size: 13px !important; }
  .text-sm  { font-size: 12px !important; }
  .text-xs  { font-size: 11px !important; }

  /* ---------- 2. Page shell: reclaim horizontal space ---------- */
  .main-content,
  body.sidebar-open .main-content,
  body.sidebar-closed .main-content,
  body[dir="rtl"].sidebar-open .main-content,
  body[dir="rtl"].sidebar-closed .main-content {
    padding-left: 8px !important;
    padding-right: 8px !important;
    padding-top: 62px !important;
    padding-bottom: 16px !important;
  }
  .page-frame { padding: 10px !important; border-radius: 12px; }
  .app-card { padding: 12px !important; }
  .p-6 { padding: 12px !important; }
  .p-4 { padding: 10px !important; }
  .space-y-6 > * + * { margin-top: 12px !important; }

  /* Hamburger smaller + out of the way */
  .menu-toggle {
    top: 8px; left: 8px;
    padding: 7px 9px; font-size: 16px;
    border-radius: 8px;
  }
  body[dir="rtl"] .menu-toggle { left: auto; right: 8px; }

  /* Page title clears the hamburger */
  .main-content > .page-frame > *:first-child h1,
  .main-content h1:first-child { padding-left: 44px; }
  body[dir="rtl"] .main-content h1:first-child { padding-left: 0; padding-right: 44px; }

  /* ---------- 3. Action buttons: compact 2-up grid instead of stacking ---------- */
  .app-btn {
    padding: 9px 10px !important;
    font-size: 12px !important;
    border-radius: 8px;
    min-height: 38px;
    flex: 1 1 calc(50% - 6px);
    min-width: 0;
  }
  .app-btn.w-full, .app-btn-primary.w-full { flex: 1 1 100%; }

  /* Toolbars (title + buttons) become a clean stack, buttons a 2-col grid */
  .flex.items-center.justify-between,
  .flex.justify-between.items-center {
    flex-wrap: wrap;
    gap: 8px;
  }
  .flex.gap-2, .flex.gap-3, .flex.gap-4 { gap: 6px !important; }

  /* Category / filter chip rows: horizontal swipe strip, no ragged wrapping */
  .flex.flex-wrap.gap-2 {
    flex-wrap: nowrap !important;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 4px;
    scroll-snap-type: x proximity;
  }
  .flex.flex-wrap.gap-2::-webkit-scrollbar { display: none; }
  .flex.flex-wrap.gap-2 > * {
    flex: 0 0 auto !important;
    scroll-snap-align: start;
    white-space: nowrap;
  }

  /* ---------- 4. Inputs: readable, no iOS auto-zoom ---------- */
  .app-input, input, select, textarea {
    font-size: 16px !important;   /* <16px triggers iOS zoom */
    padding: 9px 11px !important;
    min-height: 40px;
  }
  input[type="color"] { min-height: 36px; }
  input[type="range"] { min-height: 28px; }

  /* ---------- 5. TABLES: readable, nothing clipped ---------- */
  .overflow-x-auto {
    border-radius: 10px;
    /* fade hint on the right edge = "there is more, swipe" */
    background:
      linear-gradient(to left, rgba(255,255,255,0.07), transparent) right center / 18px 100% no-repeat;
  }
  table, .styled-table { font-size: 11px !important; }

  table thead th, .styled-table thead th {
    padding: 8px 6px !important;
    font-size: 9px !important;
    letter-spacing: 0.3px;
    white-space: normal !important;   /* let headers wrap instead of stretching */
    line-height: 1.25;
  }
  table tbody td, .styled-table tbody td {
    padding: 8px 6px !important;
    font-size: 11px !important;
    /* the fix for truncated names */
    white-space: normal !important;
    word-break: break-word;
    overflow-wrap: anywhere;
    overflow: visible !important;
    text-overflow: clip !important;
    max-width: none !important;
    line-height: 1.35;
  }
  table tfoot td, .styled-table tfoot td {
    padding: 8px 6px !important;
    font-size: 11px !important;
  }
  /* first column readable and pinned so you never lose context */
  table tbody td:first-child, .styled-table tbody td:first-child {
    min-width: 92px;
    font-weight: 600;
  }
  /* inputs inside table cells shrink to fit */
  table .app-input, .styled-table .app-input {
    font-size: 12px !important;
    padding: 5px 6px !important;
    min-height: 30px;
    width: 100% !important;
    min-width: 0 !important;
  }
  table select, .styled-table select {
    font-size: 12px !important;
    padding: 5px 4px !important;
    min-height: 30px;
  }
  /* row action buttons stay tiny inside cells */
  table .app-btn, .styled-table .app-btn {
    flex: 0 0 auto;
    padding: 5px 8px !important;
    font-size: 10px !important;
    min-height: 28px;
  }
  /* kill desktop min-widths that force clipping */
  table[style*="min-width"] { min-width: 0 !important; }

  /* ---------- 6. Modals: full-height sheet, easy to close ---------- */
  .modal-backdrop { padding: 8px !important; align-items: center; }
  .modal-backdrop > div {
    max-width: 100% !important;
    width: 100% !important;
    max-height: 92vh !important;
    border-radius: 14px;
    padding: 14px !important;
  }

  /* ---------- 7. Grids: single column ---------- */
  [class*="grid-cols-"] { grid-template-columns: 1fr !important; }
  .gap-6 { gap: 10px !important; }
  .gap-4 { gap: 8px !important; }

  /* ---------- 8. Worker cards tighter ---------- */
  .worker-card-head { padding: 12px 14px 10px; }
  .worker-rows { padding: 4px 14px; }
  .worker-row { padding: 8px 0; }
  .worker-remaining { margin: 10px 12px 12px; padding: 12px 14px; }
  .worker-remaining-value { font-size: 18px; }

  /* ---------- 9. Sidebar: comfortable touch targets ---------- */
  .sidebar { padding: 16px 10px; }
  .sidebar-link { padding: 12px 14px; font-size: 13px; }
  .sidebar-title { padding-top: 46px; }

  /* ---------- 10. Safe area (notches / home bar) ---------- */
  .main-content { padding-bottom: calc(16px + env(safe-area-inset-bottom)) !important; }
  .sidebar { padding-bottom: calc(16px + env(safe-area-inset-bottom)); }
}

/* Extra-narrow phones */
@media (max-width: 400px) {
  h1 { font-size: 17px; }
  .app-btn { font-size: 11px !important; padding: 8px !important; }
  table tbody td, .styled-table tbody td { font-size: 10px !important; padding: 7px 5px !important; }
  table thead th, .styled-table thead th { font-size: 8px !important; padding: 7px 5px !important; }
}


/* ==================================================================
   STICKY TABLE v1 — first column pinned, rest scrolls. Mobile only.
   ================================================================== */
@media (max-width: 768px) {

  .overflow-x-auto {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    border-radius: 10px;
    border: 1px solid var(--tbl-border);
    /* right-edge fade = "swipe for more" */
    background:
      linear-gradient(to left, rgba(255,255,255,0.08), transparent)
      right center / 20px 100% no-repeat;
  }
  .overflow-x-auto::-webkit-scrollbar { height: 4px; }
  .overflow-x-auto::-webkit-scrollbar-thumb {
    background: var(--border-active);
    border-radius: 4px;
  }

  table, .styled-table {
    font-size: 11px !important;
    border-collapse: separate;
    border-spacing: 0;
  }

  /* headers: wrap, compact */
  table thead th, .styled-table thead th {
    padding: 8px 7px !important;
    font-size: 9px !important;
    letter-spacing: 0.3px;
    white-space: normal !important;
    line-height: 1.2;
    min-width: 62px;
  }

  table tbody td, .styled-table tbody td {
    padding: 9px 7px !important;
    font-size: 11px !important;
    white-space: nowrap;          /* keeps rows aligned while scrolling */
    line-height: 1.3;
  }

  /* ---- pin the first column ---- */
  table thead th:first-child,
  .styled-table thead th:first-child {
    position: sticky;
    left: 0;
    z-index: 3;
    min-width: 104px;
    max-width: 132px;
    white-space: normal !important;
    text-align: left;
    box-shadow: 2px 0 6px rgba(0,0,0,0.45);
  }
  table tbody td:first-child,
  .styled-table tbody td:first-child,
  table tfoot td:first-child,
  .styled-table tfoot td:first-child {
    position: sticky;
    left: 0;
    z-index: 2;
    min-width: 104px;
    max-width: 132px;
    /* the fix for clipped names */
    white-space: normal !important;
    word-break: break-word;
    overflow-wrap: anywhere;
    text-align: left;
    font-weight: 700;
    background: var(--tbl-row-bg);
    box-shadow: 2px 0 6px rgba(0,0,0,0.35);
  }
  table thead th:first-child, .styled-table thead th:first-child { background: var(--tbl-header-bg); }
  table tfoot td:first-child, .styled-table tfoot td:first-child { background: var(--tbl-header-bg); }
  table tbody tr.row-total td:first-child,
  .styled-table tbody tr.row-total td:first-child {
    background: var(--tbl-total-bg);
  }
  table tbody tr.row-divider td:first-child,
  .styled-table tbody tr.row-divider td:first-child {
    background: var(--tbl-divider-bg);
  }

  /* RTL: pin to the right instead */
  body[dir="rtl"] table thead th:first-child,
  body[dir="rtl"] .styled-table thead th:first-child,
  body[dir="rtl"] table tbody td:first-child,
  body[dir="rtl"] .styled-table tbody td:first-child,
  body[dir="rtl"] table tfoot td:first-child,
  body[dir="rtl"] .styled-table tfoot td:first-child {
    left: auto;
    right: 0;
    text-align: right;
    box-shadow: -2px 0 6px rgba(0,0,0,0.35);
  }

  /* inputs / buttons inside cells */
  table .app-input, .styled-table .app-input,
  table input, .styled-table input {
    font-size: 13px !important;
    padding: 5px 7px !important;
    min-height: 30px;
    width: 100% !important;
    min-width: 56px !important;
  }
  table select, .styled-table select {
    font-size: 12px !important;
    padding: 5px 4px !important;
    min-height: 30px;
    min-width: 54px;
  }
  table .app-btn, .styled-table .app-btn {
    flex: 0 0 auto;
    padding: 5px 9px !important;
    font-size: 10px !important;
    min-height: 28px;
    white-space: nowrap !important;
  }

  /* desktop min-widths that break phones */
  table[style*="min-width"]:not([style*="1400px"]) { min-width: 0 !important; }
}

@media (max-width: 400px) {
  table thead th, .styled-table thead th { font-size: 8px !important; padding: 7px 5px !important; }
  table tbody td, .styled-table tbody td { font-size: 10px !important; padding: 8px 5px !important; }
  table thead th:first-child, table tbody td:first-child,
  .styled-table thead th:first-child, .styled-table tbody td:first-child {
    min-width: 88px; max-width: 112px;
  }
}


/* ==================================================================
   STICKY FIX v2 — sticky needs border-collapse:separate
   ================================================================== */
@media (max-width: 768px) {
  /* inline border-collapse:collapse disables position:sticky -> override it */
  table, .styled-table, .rm-table,
  table[style*="border-collapse"] {
    border-collapse: separate !important;
    border-spacing: 0 !important;
  }

  /* keep the table wide enough that scrolling makes sense,
     but not so wide that cells get crushed */
  table[style*="min-width: 1200px"],
  table[style*="min-width:1200px"] {
    min-width: 760px !important;
  }

  /* re-assert sticky on the specific table classes used in templates */
  .rm-table thead th:first-child,
  .rm-table tbody td:first-child,
  .rm-table tfoot td:first-child {
    position: sticky !important;
    left: 0;
    z-index: 2;
    min-width: 104px;
    max-width: 132px;
    white-space: normal !important;
    word-break: break-word;
    overflow-wrap: anywhere;
    text-align: left;
    font-weight: 700;
    background: var(--tbl-row-bg);
    box-shadow: 2px 0 6px rgba(0,0,0,0.35);
  }
  .rm-table thead th:first-child { z-index: 3; background: var(--tbl-header-bg); }
  .rm-table tfoot td:first-child { background: var(--tbl-header-bg); }

  body[dir="rtl"] .rm-table thead th:first-child,
  body[dir="rtl"] .rm-table tbody td:first-child,
  body[dir="rtl"] .rm-table tfoot td:first-child {
    left: auto; right: 0;
    text-align: right;
    box-shadow: -2px 0 6px rgba(0,0,0,0.35);
  }

  /* borders must come back as cell borders (collapse is off now) */
  table tbody td, .styled-table tbody td, .rm-table tbody td {
    border-bottom: 1px solid var(--tbl-border) !important;
    border-right: 1px solid var(--tbl-border) !important;
  }
  table tbody td:last-child, .styled-table tbody td:last-child, .rm-table tbody td:last-child {
    border-right: none !important;
  }
  table thead th, .styled-table thead th, .rm-table thead th {
    border-right: 1px solid var(--tbl-border) !important;
    border-bottom: 2px solid var(--border-active) !important;
  }
  table thead th:last-child, .styled-table thead th:last-child, .rm-table thead th:last-child {
    border-right: none !important;
  }
}


/* ==================================================================
   TABLE PAGER v2 — column pages, zero horizontal scroll (mobile)
   ================================================================== */
.tbl-pager { display: none; }
.mcol-off { }

@media (max-width: 768px) {

  /* columns swapped out by the pager */
  .mcol-off { display: none !important; }

  /* no sideways scrolling at all once the pager is active */
  .overflow-x-auto.mcols-active {
    overflow: visible !important;
    background: none !important;
    scroll-snap-type: none;
    touch-action: pan-y;
  }
  .overflow-x-auto.mcols-active.is-swiping { touch-action: none; }
  .overflow-x-auto.mcols-active > table,
  .overflow-x-auto.mcols-active > table.rm-table,
  .overflow-x-auto.mcols-active > table[style*="min-width"] {
    min-width: 0 !important;
    width: 100% !important;
    table-layout: fixed;
  }

  /* columns share the width evenly; no min-width forcing overflow */
  .mcols-active table thead th,
  .mcols-active table tbody td,
  .mcols-active table tfoot td {
    min-width: 0 !important;
    max-width: none !important;
    white-space: normal !important;
    word-break: break-word;
    overflow-wrap: anywhere;
    padding: 9px 6px !important;
    font-size: 11px !important;
  }
  .mcols-active table thead th {
    font-size: 9px !important;
    line-height: 1.2;
  }



  /* inputs fit their cell */
  .mcols-active table .app-input,
  .mcols-active table input,
  .mcols-active table select {
    width: 100% !important;
    min-width: 0 !important;
    font-size: 13px !important;
    padding: 5px 6px !important;
    min-height: 32px;
  }
  .mcols-active table .app-btn {
    width: 100%;
    padding: 5px 6px !important;
    font-size: 10px !important;
    min-height: 30px;
    white-space: nowrap !important;
  }

  /* ---------- pager control ---------- */
  .tbl-pager {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding: 8px 0 10px;
  }
  .tbl-arrow {
    width: 34px; height: 34px; padding: 0;
    display: flex; align-items: center; justify-content: center;
    font-size: 20px; line-height: 1;
    border-radius: 9px; cursor: pointer;
    color: var(--btn-text);
    background: var(--btn-bg);
    border: 1px solid var(--btn-border);
    transition: all 0.2s ease;
  }
  .tbl-arrow:disabled { opacity: 0.3; cursor: not-allowed; }
  .tbl-arrow:active:not(:disabled) {
    border-color: var(--border-active);
    background: color-mix(in srgb, var(--border-active) 14%, var(--btn-bg));
  }
  .tbl-dotrow { display: flex; align-items: center; gap: 6px; }
  .tbl-dot {
    width: 8px; height: 8px; padding: 0;
    border-radius: 50%;
    border: 1px solid var(--tbl-border);
    background: rgba(255,255,255,0.14);
    cursor: pointer;
    transition: all 0.22s cubic-bezier(0.34,1.4,0.64,1);
  }
  .tbl-dot.is-active {
    width: 22px;
    border-radius: 999px;
    background: var(--border-active);
    border-color: var(--border-active);
    box-shadow: 0 0 8px color-mix(in srgb, var(--border-active) 45%, transparent);
  }
}


/* ==================================================================
   TABLE STICKY v3 — dots + header stay put while scrolling (mobile)
   ================================================================== */
@media (max-width: 768px) {

  /* the dot strip sticks just under the hamburger */
  .tbl-pager {
    position: sticky;
    top: 0;
    z-index: 14;
    margin: 0 -10px;
    padding: 7px 10px 8px;
    background: var(--bg-primary);
    border-bottom: 1px solid var(--tbl-border);
  }

  /* header row sticks right below the dots */
  .mcols-active table thead th,
  .mcols-active .styled-table thead th,
  .mcols-active .rm-table thead th {
    position: sticky !important;
    top: 40px;
    z-index: 12;
    background: var(--tbl-header-bg) !important;
    box-shadow: 0 2px 6px rgba(0,0,0,0.4);
  }

  /* tables without a pager: header sticks under the hamburger */
  .overflow-x-auto:not(.mcols-active) table thead th {
    position: sticky;
    top: 0;
    z-index: 12;
    background: var(--tbl-header-bg) !important;
  }

  /* swiping feedback */
  .overflow-x-auto.mcols-active.is-swiping table { opacity: 0.55; transition: opacity 0.1s; }
  .mcols-active table tbody td { transition: none; }

  /* the card must not clip the sticky header */
  .app-card:has(> .overflow-x-auto.mcols-active),
  .app-card:has(> .tbl-pager) { overflow: visible !important; }
  .page-frame { overflow: visible !important; }


}


/* ==================================================================
   BOTTOM NAV v1 — app-style tab bar (mobile only)
   ================================================================== */
.bottom-nav { display: none; }

@media (max-width: 768px) {

  /* the hamburger is retired; "More" opens the sidebar */
  .menu-toggle { display: none !important; }
  .main-content h1:first-child,
  .main-content > .page-frame > *:first-child h1 { padding-left: 0 !important; }
  body[dir="rtl"] .main-content h1:first-child { padding-right: 0 !important; }

  .main-content,
  body.sidebar-open .main-content,
  body.sidebar-closed .main-content,
  body[dir="rtl"].sidebar-open .main-content,
  body[dir="rtl"].sidebar-closed .main-content {
    padding-top: 12px !important;
    padding-bottom: calc(70px + env(safe-area-inset-bottom)) !important;
  }

  .bottom-nav {
    display: flex;
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 48;
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
    background: color-mix(in srgb, var(--sidebar-bg) 92%, transparent);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-top: 1px solid rgba(255,255,255,0.07);
    box-shadow: 0 -6px 24px rgba(0,0,0,0.45);
  }

  .bn-item {
    flex: 1 1 0;
    min-width: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    padding: 7px 2px 5px;
    border: none;
    background: transparent;
    color: var(--text-muted);
    text-decoration: none;
    cursor: pointer;
    border-radius: 10px;
    transition: color 0.2s ease, background 0.2s ease;
    -webkit-tap-highlight-color: transparent;
  }
  .bn-item:active { background: rgba(255,255,255,0.05); }

  .bn-ico {
    position: relative;
    width: 24px; height: 24px;
    display: flex; align-items: center; justify-content: center;
  }
  .bn-ico svg { width: 22px; height: 22px; display: block; }

  .bn-label {
    font-size: 9.5px;
    font-weight: 600;
    letter-spacing: 0.2px;
    line-height: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
  }

  /* active tab */
  .bn-item.is-active {
    color: var(--border-active);
    background: color-mix(in srgb, var(--border-active) 10%, transparent);
  }
  .bn-item.is-active .bn-ico::after {
    content: '';
    position: absolute;
    top: -7px; left: 50%;
    transform: translateX(-50%);
    width: 18px; height: 2.5px;
    border-radius: 0 0 3px 3px;
    background: var(--border-active);
    box-shadow: 0 0 8px var(--border-active);
  }

  /* pending-alerts badge on More */
  .bn-badge {
    position: absolute;
    top: -4px; right: -6px;
    min-width: 15px; height: 15px;
    padding: 0 3px;
    display: flex; align-items: center; justify-content: center;
    border-radius: 999px;
    background: var(--btn-warning-border);
    color: #000;
    font-size: 9px;
    font-weight: 800;
    line-height: 1;
    border: 1.5px solid var(--sidebar-bg);
  }

  /* ---------- sidebar as the "More" sheet ---------- */
  .sidebar {
    width: min(320px, 88vw) !important;
    padding: 18px 12px calc(18px + env(safe-area-inset-bottom)) !important;
  }
  .sidebar-title { padding-top: 6px !important; margin-bottom: 4px; }
  .sidebar-title svg { max-width: 132px; }

  .sidebar-user {
    margin-bottom: 14px;
    padding: 12px 10px;
  }

  .sidebar-nav { gap: 4px; }

  /* group headers become clear separators, not wasted space */
  .sidebar-group-label {
    padding: 14px 12px 6px !important;
    font-size: 9.5px !important;
    letter-spacing: 1.6px;
    border-top: 1px solid rgba(255,255,255,0.06);
    margin-top: 8px;
  }
  .sidebar-nav > .sidebar-group-label:first-child {
    border-top: none;
    margin-top: 0;
    padding-top: 4px !important;
  }

  /* comfortable, thumb-sized rows */
  .sidebar-link {
    padding: 13px 14px !important;
    font-size: 14px !important;
    font-weight: 600;
    border-radius: 10px;
    min-height: 46px;
  }
  .sidebar-link-active { font-weight: 700; }

  .sidebar-logout {
    margin-top: 14px;
    padding: 13px 14px;
    font-size: 14px;
    min-height: 46px;
    border-radius: 10px;
  }

  /* sidebar sits above the tab bar */
  .sidebar { z-index: 60 !important; }
  .sidebar-overlay { z-index: 55; }
}


/* ==================================================================
   STICKY REAL v4 — kills the containing blocks that broke sticky
   ================================================================== */
@media (max-width: 768px) {

  /* fadeInUp leaves transform:translateY(0) applied (fill-mode both),
     which creates a containing block and disables position:sticky.
     Cards holding a paged table must not animate. */
  .app-card.has-paged-table { animation: none !important; transform: none !important; }
  .app-card.has-paged-table,
  .app-card:not(.has-paged-table) { overflow: visible !important; }
  .page-frame { overflow: visible !important; animation: none !important; transform: none !important; }
  .main-content { animation: none !important; }

  /* pinned column (class-driven, so hidden columns can't steal the rule) */
  .mcols-active table th.mcol-pinned,
  .mcols-active table td.mcol-pinned {
    width: 34%;
    text-align: left !important;
    font-weight: 700;
    white-space: normal !important;
    word-break: break-word;
    overflow-wrap: anywhere;
    hyphens: auto;
  }
  body[dir="rtl"] .mcols-active table th.mcol-pinned,
  body[dir="rtl"] .mcols-active table td.mcol-pinned { text-align: right !important; }

  /* numbers must never be split character-by-character */
  .mcols-active table td.mcol-num {
    white-space: nowrap !important;
    word-break: normal !important;
    overflow-wrap: normal !important;
    font-variant-numeric: tabular-nums;
    letter-spacing: -0.2px;
  }

  /* headers: allow wrapping but never letter-stacking */
  .mcols-active table thead th {
    white-space: normal !important;
    word-break: keep-all !important;
    overflow-wrap: break-word !important;
    hyphens: none;
    font-size: 9px !important;
    line-height: 1.15;
    padding: 7px 4px !important;
  }

  /* remaining data columns share what's left evenly */
  .mcols-active table thead th:not(.mcol-pinned):not(.mcol-off),
  .mcols-active table tbody td:not(.mcol-pinned):not(.mcol-off) {
    width: auto;
  }

  /* the pager sits above and stays put */
  .tbl-pager {
    position: sticky;
    top: 0;
    z-index: 16;
    display: flex;
    flex-direction: column;
    gap: 3px;
    margin: 0 -10px;
    padding: 6px 10px 5px;
    background: var(--bg-primary);
    border-bottom: 1px solid var(--tbl-border);
  }
  .tbl-pager .tbl-dotrow { justify-content: center; }
  .tbl-pager .tbl-hint {
    text-align: center;
    font-size: 8px;
    letter-spacing: 0.5px;
    text-transform: uppercase;
    color: var(--text-muted);
    opacity: 0.55;
  }

  /* header row locks right under the pager */
  .mcols-active table thead th {
    position: sticky !important;
    top: 34px;
    z-index: 14;
    background: var(--tbl-header-bg) !important;
    box-shadow: 0 2px 5px rgba(0,0,0,0.45);
  }
  .overflow-x-auto:not(.mcols-active) table thead th {
    position: sticky;
    top: 0;
    z-index: 14;
    background: var(--tbl-header-bg) !important;
  }
}

@media (max-width: 430px) {
  .mcols-active table th.mcol-pinned,
  .mcols-active table td.mcol-pinned { width: 40%; }
  .mcols-active table td { font-size: 10.5px !important; padding: 8px 4px !important; }
}


/* ==================================================================
   TABLE BAR v2 — framed collapsible header row
   ================================================================== */
.tbl-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 14px;
  cursor: pointer;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-default);
  background: linear-gradient(180deg,
    color-mix(in srgb, var(--border-default) 7%, var(--bg-card)),
    var(--bg-card));
  transition: all 0.22s cubic-bezier(0.4, 0, 0.2, 1);
  -webkit-tap-highlight-color: transparent;
}
.tbl-bar:hover {
  border-color: var(--border-active);
  box-shadow: 0 0 14px color-mix(in srgb, var(--border-active) 16%, transparent);
}
/* open: bottom corners square so it reads as one piece with the table */
.tbl-bar:not(.is-closed) {
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
  border-bottom-color: var(--border-active);
  margin-bottom: 0;
}
.tbl-bar.is-closed { margin-bottom: 0; }

.tbl-bar-toggle {
  flex: 0 0 auto;
  width: 26px; height: 26px; padding: 0;
  display: flex; align-items: center; justify-content: center;
  border-radius: 7px;
  border: 1px solid color-mix(in srgb, var(--border-default) 45%, transparent);
  background: transparent;
  color: var(--border-active);
  cursor: pointer;
  pointer-events: none;
}
.tbl-bar-toggle svg {
  width: 14px; height: 14px;
  transition: transform 0.25s cubic-bezier(0.34, 1.4, 0.64, 1);
}
.tbl-bar-toggle[aria-expanded="false"] svg { transform: rotate(-90deg); }

.tbl-bar-title {
  flex: 1 1 auto;
  min-width: 0;
  font-size: var(--font-sm);
  font-weight: 700;
  letter-spacing: 0.4px;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tbl-bar-count {
  flex: 0 0 auto;
  min-width: 26px;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 10px;
  font-weight: 700;
  text-align: center;
  color: var(--border-active);
  background: color-mix(in srgb, var(--border-active) 12%, transparent);
  border: 1px solid color-mix(in srgb, var(--border-active) 30%, transparent);
}

.tbl-collapsed { display: none !important; }

/* the table sits flush under an open bar */
.tbl-bar:not(.is-closed) + .overflow-x-auto,
.tbl-bar:not(.is-closed) + .tbl-pager {
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}

@media (max-width: 768px) {
  .tbl-bar {
    position: sticky;
    top: 0;
    z-index: 18;
    padding: 10px 12px;
  }
  .tbl-bar.is-closed { position: static; }
  .tbl-pager { top: 47px !important; z-index: 16; }
  .mcols-active table thead th { top: 81px !important; }
  .overflow-x-auto:not(.mcols-active) table thead th { top: 47px !important; }

  .app-card { overflow: visible !important; }
  .app-card.has-paged-table,
  .app-card.has-tbl-bar { animation: none !important; transform: none !important; }
  .page-frame { animation: none !important; transform: none !important; overflow: visible !important; }
  .main-content { animation: none !important; }
}

/* ==================================================================
   TABLE FIT v1 — one uniform, auto-computed font size per table
   ================================================================== */
table[data-fit] td,
table[data-fit] th,
table[data-fit] td .app-input,
table[data-fit] td input,
table[data-fit] td select {
  font-size: var(--tbl-fit, 12px) !important;
}
table.tbl-fitting { visibility: hidden; }

@media (max-width: 768px) {
  /* text must wrap on words, never stack letters */
  table[data-fit] td,
  table[data-fit] th {
    word-break: keep-all !important;
    overflow-wrap: break-word !important;
    hyphens: none !important;
    line-height: 1.25;
  }
  table[data-fit] td.mcol-num {
    word-break: normal !important;
    overflow-wrap: normal !important;
    white-space: nowrap !important;
  }
}


/* ==================================================================
   DASHBOARD v1 — framed KPI tiles + paged table grid
   ================================================================== */

/* ---- KPI tiles: framed on every screen ---- */
.kpi-grid > .app-card {
  border: 1px solid var(--border-default) !important;
  background: linear-gradient(180deg,
    color-mix(in srgb, var(--border-default) 5%, var(--bg-card)),
    var(--bg-card));
  transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}
.kpi-grid > .app-card:hover {
  border-color: var(--border-active) !important;
  box-shadow: 0 0 16px color-mix(in srgb, var(--border-active) 14%, transparent);
}

@media (max-width: 768px) {
  /* four tiles fit two-up, tight and readable */
  .kpi-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 7px !important;
    margin-bottom: 12px !important;
  }
  .kpi-grid > .app-card {
    padding: 9px 10px !important;
    border-radius: 10px;
    min-height: 0;
    animation: none;
  }
  .kpi-grid > .app-card > div:first-child {
    font-size: 9.5px !important;
    line-height: 1.2;
    margin-bottom: 3px !important;
    letter-spacing: 0.2px;
    color: var(--text-muted) !important;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
  .kpi-grid > .app-card > div:last-child {
    font-size: 15px !important;
    font-weight: 700 !important;
    line-height: 1.15;
  }
  .kpi-grid > .app-card .text-xl { font-size: 15px !important; }
}

@media (max-width: 400px) {
  .kpi-grid > .app-card { padding: 8px !important; }
  .kpi-grid > .app-card > div:last-child { font-size: 13.5px !important; }
}

/* ---- Tables: page 4 at a time, vertical dots ---- */
.tables-wrap { position: relative; }
.tbl-vdots { display: none; }

@media (max-width: 768px) {
  .tables-wrap { padding-right: 26px; }
  body[dir="rtl"] .tables-wrap { padding-right: 0; padding-left: 26px; }

  #tables-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 8px !important;
  }
  /* compact table cards */
  #tables-grid > button {
    height: auto !important;
    min-height: 118px;
    padding: 12px 8px !important;
    border: 1px solid var(--border-default);
    border-radius: 12px;
    animation: none;
  }
  #tables-grid > button .text-5xl { font-size: 30px !important; }
  #tables-grid > button .text-2xl { font-size: 15px !important; }
  #tables-grid > button .text-sm { font-size: 9.5px !important; }
  #tables-grid > button .text-xs { font-size: 9px !important; }
  #tables-grid > button .free-table-btn {
    font-size: 8.5px !important;
    padding: 2px 6px !important;
    top: 5px !important; right: 5px !important;
  }
  /* cards outside the current page are removed from flow */
  #tables-grid > button.tpage-off { display: none !important; }

  /* vertical dot rail */
  .tbl-vdots {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 7px;
    position: absolute;
    top: 0; bottom: 0; right: 0;
    width: 26px;
  }
  body[dir="rtl"] .tbl-vdots { right: auto; left: 0; }

  .tbl-vdot {
    width: 8px; height: 8px; padding: 0;
    border-radius: 50%;
    border: 1px solid var(--tbl-border);
    background: rgba(255,255,255,0.14);
    cursor: pointer;
    transition: all 0.22s cubic-bezier(0.34, 1.4, 0.64, 1);
  }
  .tbl-vdot.is-active {
    height: 22px;
    border-radius: 999px;
    background: var(--border-active);
    border-color: var(--border-active);
    box-shadow: 0 0 8px color-mix(in srgb, var(--border-active) 45%, transparent);
  }
}


/* ==================================================================
   MOBILE FIX v2 — table width lock + modal layout
   ================================================================== */
@media (max-width: 768px) {

  /* ---- 1) الجدول ممنوع يتجاوز عرض الحاوية ---- */
  .overflow-x-auto > table:not([style*="1400px"]) {
    min-width: 0 !important;
    width: 100% !important;
    max-width: 100% !important;
    table-layout: fixed !important;
  }
  .overflow-x-auto > table:not([style*="1400px"]) > thead > tr > th,
  .overflow-x-auto > table:not([style*="1400px"]) > tbody > tr > td,
  .overflow-x-auto > table:not([style*="1400px"]) > tfoot > tr > td {
    white-space: normal !important;
    overflow-wrap: anywhere;
    word-break: break-word;
    padding: 9px 7px !important;
    font-size: 12px !important;
    line-height: 1.35;
    vertical-align: middle;
  }
  .overflow-x-auto > table:not([style*="1400px"]) > thead > tr > th {
    font-size: 10px !important;
    letter-spacing: 0.3px;
    line-height: 1.25;
  }

  /* العمود المثبّت = 42%، الباقي يتقاسم بالتساوي */
  .overflow-x-auto > table th.mcol-pinned,
  .overflow-x-auto > table td.mcol-pinned {
    width: 42% !important;
    text-align: start !important;
  }
  .mcol-off { display: none !important; }
  .mcol-num { text-align: center !important; font-variant-numeric: tabular-nums; }

  /* ---- 2) صف زرّي الصورة: نصّين بالتساوي ---- */
  div.flex:has(> input[type="file"]) {
    display: grid !important;
    grid-template-columns: 1fr 1fr !important;
    gap: 8px !important;
  }
  div.flex:has(> input[type="file"]) > label,
  div.flex:has(> input[type="file"]) > button {
    width: 100% !important;
    min-width: 0 !important;
    flex: none !important;
    padding-left: 6px !important;
    padding-right: 6px !important;
    min-height: 46px;
    display: flex !important;
    align-items: center;
    justify-content: center;
    text-align: center;
    white-space: normal !important;
    line-height: 1.2;
    font-size: 13px !important;
  }

  /* ---- 3) صف المكوّن: مكوّن + كمية + X بسطر واحد ---- */
  div.flex.gap-2.items-center:has(> select.app-input) {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) 78px 46px !important;
    gap: 6px !important;
    align-items: center;
  }
  div.flex.gap-2.items-center:has(> select.app-input) > select {
    width: 100% !important;
    min-width: 0 !important;
    flex: none !important;
    font-size: 13px !important;
  }
  div.flex.gap-2.items-center:has(> select.app-input) > input[type="number"] {
    width: 100% !important;
    min-width: 0 !important;
    flex: none !important;
    text-align: center;
    font-size: 13px !important;
  }
  div.flex.gap-2.items-center:has(> select.app-input) > button {
    width: 46px !important;
    min-width: 46px !important;
    padding: 0 !important;
    flex: none !important;
    min-height: 44px;
    font-weight: 700;
  }

  /* ---- 4) أزرار أسفل المودال بالتساوي ---- */
  .app-card > div.flex.gap-3:has(> button.app-btn-primary),
  .app-card > div.flex.gap-2:has(> button.app-btn-primary) {
    display: grid !important;
    grid-template-columns: repeat(auto-fit, minmax(0, 1fr)) !important;
    gap: 8px !important;
  }
  .app-card > div.flex.gap-3:has(> button.app-btn-primary) > button,
  .app-card > div.flex.gap-2:has(> button.app-btn-primary) > button {
    width: 100% !important;
    min-width: 0 !important;
    flex: none !important;
    min-height: 46px;
    padding-left: 6px !important;
    padding-right: 6px !important;
    white-space: normal !important;
    line-height: 1.2;
    font-size: 13px !important;
  }

  /* ---- 5) المودال نفسه: يملأ الشاشة ويسكرول ---- */
  .modal-backdrop {
    align-items: center !important;
    padding: 10px !important;
  }
  .modal-backdrop > .app-card {
    max-height: calc(100vh - 100px) !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
    padding: 16px !important;
    width: 100% !important;
    max-width: 100% !important;
  }
  .modal-backdrop > .app-card img.w-32 {
    width: 96px !important;
    height: 96px !important;
  }

  /* ---- 6) ما في زر نصّه مقصوص، أبداً ---- */
  .app-btn, .app-btn-primary, label.app-btn, button.app-btn {
    white-space: normal !important;
    height: auto !important;
    line-height: 1.2;
  }
}


/* Modal content box */
.modal-box {
  background: var(--bg-card);
  border: 1px solid var(--border-default);
  border-radius: 12px;
  padding: 24px;
  width: 100%;
  max-width: 560px;
}
