/* ============================================================
   RESPONSIVE STYLESHEET — Mobile First
   Breakpoints:
     xs: < 480px   (small phones)
     sm: < 768px   (phones, large phones)
     md: < 1024px  (tablets, small laptops)
     lg: ≥ 1024px  (desktops) ← default layout
   ============================================================ */

/* ══ 1. CSS Variables overrides for mobile ════════════════ */
:root {
  --sidebar-width: 260px;
  --topbar-h:      54px;
  --bottom-nav-h:  0px;
}
@media (max-width: 767px) {
  :root {
    --bottom-nav-h: 60px;
    --topbar-h: 50px;
  }
}

/* ══ 2. Layout — Sidebar + Main ═══════════════════════════ */
.app-wrapper {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: var(--sidebar-width);
  min-height: 100vh;
  flex-shrink: 0;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  transition: transform .25s cubic-bezier(.4,0,.2,1);
  z-index: 200;
}

.main-wrapper {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

/* Tablet: sidebar collapses to icon-only */
@media (max-width: 1023px) and (min-width: 768px) {
  .sidebar {
    width: 64px;
    overflow: hidden;
  }
  .sidebar:hover { width: var(--sidebar-width); box-shadow: 4px 0 20px rgba(0,0,0,.15); }
  .sidebar .sidebar-logo-text,
  .sidebar .sidebar-logo-sub,
  .sidebar .nav-item span,
  .sidebar .nav-group-toggle span,
  .sidebar .nav-sub-label,
  .sidebar .sidebar-user-name,
  .sidebar .sidebar-user-role,
  .sidebar .nav-badge,
  .sidebar .chevron,
  .sidebar .sidebar-section { display: none; }
  .sidebar:hover .sidebar-logo-text,
  .sidebar:hover .sidebar-logo-sub,
  .sidebar:hover .nav-item span,
  .sidebar:hover .nav-group-toggle span,
  .sidebar:hover .nav-sub-label,
  .sidebar:hover .sidebar-user-name,
  .sidebar:hover .sidebar-user-role,
  .sidebar:hover .nav-badge,
  .sidebar:hover .chevron,
  .sidebar:hover .sidebar-section { display: initial; }
  .sidebar-logo { padding: 12px 10px; }
  .nav-item { padding: 10px; justify-content: center; }
  .sidebar:hover .nav-item { padding: 8px 16px; justify-content: flex-start; }
  .nav-group-toggle { padding: 10px; justify-content: center; }
  .sidebar:hover .nav-group-toggle { padding: 8px 16px; justify-content: flex-start; }
  .nav-group-icon { margin: 0 auto; }
  .sidebar:hover .nav-group-icon { margin: 0; }
}

/* Mobile: sidebar hidden, slide-in on open */
@media (max-width: 767px) {
  .sidebar {
    position: fixed;
    top: 0;
    height: 100vh;
    width: var(--sidebar-width);
    transform: translateX(100%);
    box-shadow: none;
  }
  [dir="ltr"] .sidebar { transform: translateX(-100%); }

  .sidebar.open {
    transform: translateX(0) !important;
    box-shadow: 4px 0 24px rgba(0,0,0,.25);
  }
  .main-wrapper { margin: 0 !important; width: 100%; }
}

/* ══ 3. Topbar ════════════════════════════════════════════ */
.topbar {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 10px;
  position: sticky;
  top: 0;
  z-index: 100;
}

.topbar-title {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-inline-start: auto;
  flex-shrink: 0;
}

#sidebar-toggle {
  display: none;
  flex-shrink: 0;
}

@media (max-width: 1023px) {
  #sidebar-toggle { display: flex !important; }
}

@media (max-width: 767px) {
  .topbar { padding: 0 12px; }
  .topbar-title { font-size: 13px; max-width: 120px; }

  /* Hide search bar on mobile (use search icon instead) */
  #search-wrapper { display: none !important; }

  /* Compact topbar actions */
  .topbar-actions { gap: 4px; }
  .topbar-actions .btn { padding: 6px 8px !important; }
}

/* ══ 4. Page Content ═══════════════════════════════════════ */
.page-content {
  padding: 16px 20px;
  flex: 1;
  padding-bottom: calc(20px + var(--bottom-nav-h));
}

@media (max-width: 767px) {
  .page-content { padding: 12px 12px; }
}

/* ══ 5. Stats Grid ════════════════════════════════════════ */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 12px;
}

@media (max-width: 767px) {
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
  }
  .stat-card { padding: 12px; }
  .stat-value { font-size: 22px; }
  .stat-icon { width: 34px; height: 34px; font-size: 16px; }
}

@media (max-width: 360px) {
  .stats-grid { grid-template-columns: 1fr; }
}

/* ══ 6. Card Grids ════════════════════════════════════════ */
@media (max-width: 767px) {
  /* Single column layout on mobile */
  [style*="grid-template-columns: 1fr 1fr"],
  [style*="grid-template-columns:1fr 1fr"] {
    grid-template-columns: 1fr !important;
  }
  [style*="grid-template-columns: 3fr 2fr"],
  [style*="grid-template-columns:3fr 2fr"] {
    grid-template-columns: 1fr !important;
  }
  [style*="grid-template-columns: 2fr 1fr"],
  [style*="grid-template-columns:2fr 1fr"] {
    grid-template-columns: 1fr !important;
  }
}

/* ══ 7. Tables — Responsive ═══════════════════════════════ */
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: var(--radius);
}

@media (max-width: 767px) {
  /* Auto card-mode for ALL tables inside .table-wrap on phones */
  .table-wrap table { min-width: 0; width: 100%; }
  .table-wrap thead { display: none; }
  .table-wrap tbody { display: block; }
  .table-wrap tr {
    display: block;
    margin-bottom: 10px;
    border: 0.5px solid var(--border);
    border-radius: var(--radius);
    padding: 10px 12px;
    background: var(--bg-card);
  }
  .table-wrap tr:last-child { margin-bottom: 0; }
  .table-wrap td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    padding: 6px 0;
    border: none !important;
    border-bottom: 0.5px solid var(--border) !important;
    font-size: 13px;
    text-align: end;
  }
  .table-wrap td:last-child { border-bottom: none !important; }
  .table-wrap td:empty,
  .table-wrap td:has(> :only-child:empty) { display: none; }
  .table-wrap td::before {
    content: attr(data-label);
    font-size: 11px;
    font-weight: 600;
    color: var(--text-muted);
    flex-shrink: 0;
    text-align: start;
  }
  /* Action columns (no label) — show buttons inline without label */
  .table-wrap td:not([data-label])::before { content: none; }
  .table-wrap td:not([data-label]) { justify-content: flex-end; }
}

/* Opt-out: tables explicitly marked to stay horizontal-scroll */
@media (max-width: 767px) {
  .table-wrap.keep-table table { min-width: 600px; }
  .table-wrap.keep-table thead { display: table-header-group; }
  .table-wrap.keep-table tbody { display: table-row-group; }
  .table-wrap.keep-table tr { display: table-row; border: none; padding: 0; margin: 0; background: none; }
  .table-wrap.keep-table td { display: table-cell; padding: 8px; }
  .table-wrap.keep-table td::before { content: none; }
}

/* ══ 8. Forms ═════════════════════════════════════════════ */
.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}
.form-grid .form-group.full { grid-column: span 2; }

@media (max-width: 767px) {
  .form-grid { grid-template-columns: 1fr; gap: 10px; }
  .form-grid .form-group.full { grid-column: span 1; }
  input, select, textarea { font-size: 16px !important; /* prevent zoom on iOS */ }
}

/* ══ 9. Modals ════════════════════════════════════════════ */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.5);
  z-index: 300;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal-backdrop.open { display: flex; }

.modal {
  background: var(--bg-card);
  border-radius: 14px;
  width: 100%;
  max-width: 560px;
  max-height: 90vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

@media (max-width: 767px) {
  .modal-backdrop {
    align-items: flex-end;
    padding: 0;
  }
  .modal {
    max-width: 100%;
    border-radius: 18px 18px 0 0;
    max-height: 90vh;
    padding-bottom: env(safe-area-inset-bottom, 20px);
  }
  .modal-header {
    padding: 16px 16px 12px;
    position: sticky;
    top: 0;
    background: var(--bg-card);
    z-index: 1;
    border-bottom: 0.5px solid var(--border);
  }
  /* Swipe indicator */
  .modal::before {
    content: '';
    display: block;
    width: 40px;
    height: 4px;
    background: var(--border-strong);
    border-radius: 2px;
    margin: 10px auto 0;
    flex-shrink: 0;
  }
}

/* ══ 10. Buttons — Touch Targets ══════════════════════════ */
@media (max-width: 767px) {
  .btn { min-height: 38px; }
  .btn-sm { min-height: 32px; padding: 5px 10px; }

  /* Floating action button */
  .fab {
    position: fixed;
    bottom: calc(16px + var(--bottom-nav-h));
    inset-inline-end: 16px;
    width: 52px;
    height: 52px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--accent);
    color: white;
    box-shadow: 0 4px 16px rgba(42,120,214,.4);
    border: none;
    cursor: pointer;
    font-size: 22px;
    z-index: 150;
    transition: transform .15s;
  }
  .fab:active { transform: scale(.93); }
}

/* ══ 11. Mobile Bottom Navigation Bar ════════════════════ */
.mobile-bottom-nav {
  display: none;
}

@media (max-width: 767px) {
  .mobile-bottom-nav {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0; right: 0;
    height: var(--bottom-nav-h);
    background: var(--bg-card);
    border-top: 0.5px solid var(--border);
    z-index: 150;
    padding-bottom: env(safe-area-inset-bottom, 0px);
    align-items: stretch;
  }
  .mobile-bottom-nav a,
  .mobile-bottom-nav button {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    text-decoration: none;
    color: var(--text-muted);
    font-size: 10px;
    font-weight: 500;
    background: none;
    border: none;
    cursor: pointer;
    padding: 6px 4px;
    transition: color .1s;
    font-family: inherit;
    position: relative;
  }
  .mobile-bottom-nav a.active,
  .mobile-bottom-nav button.active { color: var(--accent); }
  .mobile-bottom-nav i { font-size: 20px; }
  .mobile-bottom-nav .notif-dot {
    position: absolute;
    top: 4px;
    right: calc(50% - 14px);
    width: 8px; height: 8px;
    background: var(--danger);
    border-radius: 50%;
    border: 2px solid var(--bg-card);
  }
}

/* ══ 12. Sidebar Overlay on Mobile ═══════════════════════ */
#sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.5);
  z-index: 199;
  cursor: pointer;
}
#sidebar-overlay.show { display: block; }

/* ══ 13. Breadcrumb — Compact ════════════════════════════ */
@media (max-width: 767px) {
  .breadcrumb {
    font-size: 11px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    padding: 6px 0;
    margin-bottom: 10px;
  }
}

/* ══ 14. Property Cards / Livestock Cards ════════════════ */
@media (max-width: 767px) {
  /* Single column card grids */
  [style*="auto-fill,minmax(300px"],
  [style*="auto-fill, minmax(300px"],
  [style*="auto-fill,minmax(290px"],
  [style*="auto-fill,minmax(240px"] {
    grid-template-columns: 1fr !important;
  }
  [style*="auto-fill,minmax(180px"] {
    grid-template-columns: repeat(2, 1fr) !important;
  }
  [style*="auto-fill,minmax(160px"],
  [style*="auto-fill,minmax(150px"],
  [style*="auto-fill,minmax(140px"] {
    grid-template-columns: repeat(2, 1fr) !important;
  }
}

/* ══ 15. Tab Navigation — Horizontal Scroll ══════════════ */
@media (max-width: 767px) {
  /* Tab nav scrollable */
  [style*="border-bottom"][style*="flex"] {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    flex-wrap: nowrap !important;
  }
}

/* ══ 16. Typography ═══════════════════════════════════════ */
@media (max-width: 767px) {
  h1[style*="font-size:16px"] { font-size: 15px !important; }
  h1[style*="font-size:17px"] { font-size: 15px !important; }
  h1[style*="font-size:18px"] { font-size: 15px !important; }
}

/* ══ 17. Safe Area (iPhone notch) ════════════════════════ */
@supports (padding: env(safe-area-inset-top)) {
  .topbar { padding-top: env(safe-area-inset-top, 0); }
  .sidebar { padding-top: env(safe-area-inset-top, 0); }
}

/* ══ 18. Dark mode sidebar overlay ══════════════════════ */
[data-theme="dark"] #sidebar-overlay { background: rgba(0,0,0,.7); }

/* ══ 19. Print ═══════════════════════════════════════════ */
@media print {
  .sidebar, .topbar, .mobile-bottom-nav,
  #sidebar-toggle, #sidebar-overlay,
  .breadcrumb, .fab { display: none !important; }
  .main-wrapper { margin: 0 !important; }
  .page-content { padding: 10px !important; }
  .card { box-shadow: none !important; border: 0.5px solid #ddd !important; }
  table { font-size: 11px; }
}

/* ══ 20. Animations ══════════════════════════════════════ */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}


/* ══ 21. Calendar — Mobile Optimization ═══════════════════ */
@media (max-width: 767px) {
  /* Calendar day cells: smaller, scrollable event text */
  div[style*="grid-template-columns:repeat(7,1fr)"] > div {
    min-height: 56px !important;
    padding: 3px !important;
    font-size: 10px;
  }
  div[style*="grid-template-columns:repeat(7,1fr)"] > div > div:first-child {
    width: 18px !important;
    height: 18px !important;
    font-size: 11px !important;
  }
  div[style*="grid-template-columns:repeat(7,1fr)"] a {
    font-size: 8px !important;
    padding: 1px 2px !important;
  }
  /* Calendar header navigation: keep on one line */
  .breadcrumb + div[style*="justify-content:space-between"] {
    flex-wrap: nowrap !important;
    overflow-x: auto;
  }
}

@media (max-width: 400px) {
  div[style*="grid-template-columns:repeat(7,1fr)"] > div {
    min-height: 44px !important;
  }
  div[style*="grid-template-columns:repeat(7,1fr)"] a {
    display: none !important; /* hide event chips, show only dot */
  }
  div[style*="grid-template-columns:repeat(7,1fr)"] > div.has-events::after {
    content: '';
    display: block;
    width: 4px; height: 4px;
    background: var(--accent);
    border-radius: 50%;
    margin: 2px auto 0;
  }
}

/* ══ 22. Sales/Reports Bar Charts — Mobile ═══════════════ */
@media (max-width: 767px) {
  div[style*="align-items:flex-end"][style*="height:120px"],
  div[style*="align-items: flex-end"][style*="height: 120px"] {
    height: 90px !important;
    gap: 3px !important;
  }
}

/* ══ 23. KPI stat cards — prevent text overflow ══════════ */
@media (max-width: 767px) {
  .stat-value { word-break: break-word; }
  .stat-label { font-size: 11px; line-height: 1.3; }
}

/* ══ 24. Sidebar accordion — mobile spacing ═══════════════ */
@media (max-width: 767px) {
  .nav-group-toggle { padding: 11px 16px; }
  .nav-sub-item { padding: 9px 16px 9px 32px; }
  [dir="rtl"] .nav-sub-item { padding: 9px 32px 9px 16px; }
}

/* ══ 25. Tap targets minimum size (accessibility) ═══════ */
@media (max-width: 767px) {
  a, button, input[type="checkbox"], input[type="radio"], select {
    min-height: 36px;
  }
  .badge { min-height: auto; } /* badges stay small */
  .nav-item, .nav-sub-item, .nav-group-toggle { min-height: 40px; }
}


/* ══ 26. Dashboard Widget Grid — Mobile ═══════════════════ */
@media (max-width: 767px) {
  .widget-grid {
    grid-template-columns: 1fr !important;
  }
  .widget[style*="grid-column:span 2"],
  .widget[style*="grid-column: span 2"] {
    grid-column: span 1 !important;
  }
}

/* ══ 27. Dashboard Customizer Panel — Full width on mobile ══ */
@media (max-width: 767px) {
  .dash-customizer { width: 100% !important; }
}

/* ══ 28. Admin Permission Matrix — Horizontal scroll hint ══ */
@media (max-width: 767px) {
  .perm-card { padding: 10px; }
}

/* ══ 29. Quick-nav link grids (module dashboards) ══════════ */
@media (max-width: 767px) {
  div[style*="auto-fill,minmax(190px"],
  div[style*="auto-fill, minmax(190px"],
  div[style*="auto-fill,minmax(180px"],
  div[style*="auto-fill, minmax(180px"],
  div[style*="auto-fill,minmax(200px"] {
    grid-template-columns: repeat(2, 1fr) !important;
  }
}

/* ══ 30. Login Page — Mobile ════════════════════════════════ */
@media (max-width: 480px) {
  .login-card { margin: 16px !important; padding: 28px 20px !important; }
}


/* ══ 31. Autocomplete dropdowns — mobile width safety ════ */
@media (max-width: 767px) {
  .serial-dd, #search-dropdown {
    max-width: calc(100vw - 32px);
    left: 0 !important; right: 0 !important;
  }
}

/* ══ 32. Breeding/Animal pregnancy progress cards ════════ */
@media (max-width: 767px) {
  div[style*="flex-wrap:wrap"][style*="justify-content:space-between"] {
    gap: 8px;
  }
}

/* ══ 33. Hide decorative empty-state icons less aggressively, but shrink ══ */
@media (max-width: 480px) {
  .empty-state i { font-size: 32px !important; }
  .empty-state h3 { font-size: 13px; }
}

/* ══ 34. Notification cards — mobile padding ════════════ */
@media (max-width: 767px) {
  .badge { font-size: 10px; padding: 2px 7px; }
}

/* ══ 35. Sticky table headers don't work well on mobile card-mode — disable ══ */
@media (max-width: 767px) {
  th[style*="position:sticky"] { position: static !important; }
}


/* ══════════════════════════════════════════════════════════
   SMART DASHBOARD — v3 (Insights, Goals, Presets)
   ══════════════════════════════════════════════════════════ */

/* Preset menu button active state */
#preset-btn:focus { outline: none; }

/* Goals widget ring text positioning handled inline via absolute centering */

/* Insight cards subtle hover */
.widget-body [style*="background:var(--warning-bg)"],
.widget-body [style*="background:var(--success-bg)"],
.widget-body [style*="background:var(--accent-bg)"] {
  transition: transform .12s;
}
.widget-body [style*="background:var(--warning-bg)"]:hover,
.widget-body [style*="background:var(--success-bg)"]:hover,
.widget-body [style*="background:var(--accent-bg)"]:hover {
  transform: translateY(-1px);
}

/* Mobile: preset menu full width dropdown */
@media (max-width: 767px) {
  #preset-menu { width: 220px !important; }
  .period-pills { overflow-x: auto; max-width: 100%; -webkit-overflow-scrolling: touch; }
}
