/* ============================================================
   AffsMail — Global Responsive CSS
   Fixes mobile layout for iPhone Safari, Android Chrome, etc.
   ============================================================ */

/* ── 1. Reset & Box Model ────────────────────────────────── */
*, *::before, *::after {
  box-sizing: border-box;
}

/* ── 2. Root / Viewport ──────────────────────────────────── */
html {
  /* Use small viewport height so iPhone Safari address bar doesn't cause overflow */
  height: 100%;
  -webkit-text-size-adjust: 100%; /* Prevent iOS font inflation */
  text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100%;
  /* Fallback for browsers without svh support */
  min-height: 100vh;
  min-height: 100svh;
  overflow-x: hidden;
  /* Prevent rubber-band over-scroll from exposing bg colour gaps */
  overscroll-behavior-y: contain;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ── 3. Prevent Horizontal Overflow ─────────────────────── */
img, video, iframe, table, pre, code {
  max-width: 100%;
}

/* ── 4. Smooth Scrolling (iOS) ───────────────────────────── */
.overflow-y-auto,
main,
[data-scroll] {
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
}

/* ── 5. Full-height Layout Shell ─────────────────────────── */
/* The outer flex wrapper that holds sidebar + main */
.layout-shell {
  display: flex;
  flex: 1 1 0%;
  min-height: 0;
  overflow: hidden;
}

/* ── 6. Header ───────────────────────────────────────────── */
header {
  position: sticky;
  top: 0;
  z-index: 30;
  /* Prevent Safari from blurring behind the header on scroll */
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  /* Extend header into iPhone notch / status bar safe area */
  padding-top: max(0.75rem, env(safe-area-inset-top));
  padding-left: max(1rem, env(safe-area-inset-left));
  padding-right: max(1rem, env(safe-area-inset-right));
}

/* Account selector: never wider than it needs to be on mobile */
header select {
  max-width: 44vw;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── 7. Desktop Sidebar ──────────────────────────────────── */
#sidebar {
  overflow-y: auto;
  flex-shrink: 0;
  height: 100%;
  -webkit-overflow-scrolling: touch;
}

/* ── 8. Mobile Sidebar / Drawer ──────────────────────────── */
#mobile-sidebar {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  /* Respect iOS safe areas */
  padding-bottom: env(safe-area-inset-bottom);
}

/* Ensure the mobile sidebar nav items are large enough to tap */
#mobile-sidebar a,
#mobile-sidebar button {
  min-height: 44px;
  display: flex;
  align-items: center;
}

/* ── 9. Main Content Area ────────────────────────────────── */
main {
  flex: 1 1 0%;
  min-width: 0;       /* prevent flex children from overflowing */
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  /* Bottom safe area (iPhone home indicator) */
  padding-bottom: env(safe-area-inset-bottom);
}

/* ── 10. Sticky Sub-toolbars ─────────────────────────────── */
.toolbar-sticky {
  position: sticky;
  top: 0;
  z-index: 10;
  background: rgba(255, 255, 255, 0.97);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
}

/* ── 11. Email List Items ────────────────────────────────── */
.email-row {
  display: flex;
  align-items: center;
  min-height: 56px;       /* comfortable tap target */
  width: 100%;
  overflow: hidden;
}

.email-row a {
  min-width: 0;           /* allow flex child to shrink and truncate */
}

/* Sender / subject truncation handled by flex min-w-0 now */

/* ── 12. Floating Action Button ─────────────────────────── */
#fab-compose {
  /* Respect iPhone home indicator */
  bottom: calc(1.5rem + env(safe-area-inset-bottom));
  right: calc(1.5rem + env(safe-area-inset-right));
  /* Touch feedback */
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* ── 13. Touch Tap Targets ───────────────────────────────── */
button,
a,
[role="button"],
input[type="checkbox"],
input[type="radio"],
select,
label {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* Minimum 44×44px tap targets for interactive elements */
button,
a.btn,
.nav-link {
  min-height: 44px;
  min-width: 44px;
}

/* ── 14. Forms ───────────────────────────────────────────── */
input,
textarea,
select {
  /* Prevent iOS from zooming in when focusing inputs smaller than 16px */
  font-size: max(16px, 1em);
  max-width: 100%;
}

/* Restore visual size for inputs that are intentionally smaller (toolbars) */
.toolbar-input {
  font-size: 0.875rem; /* 14px — wrapped in container that won't zoom */
  transform-origin: left center;
}

/* ── 15. Tables (overflow-x on mobile) ───────────────────── */
.table-responsive {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

table {
  min-width: max-content; /* let table be its natural width, parent scrolls */
}

/* ── 16. Modals / Dialogs ────────────────────────────────── */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: flex-end;   /* bottom sheet on mobile */
  justify-content: center;
  padding: env(safe-area-inset-bottom) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

@media (min-width: 640px) {
  .modal-backdrop {
    align-items: center;   /* centred on desktop */
  }
}

.modal-box {
  background: #fff;
  border-radius: 1rem 1rem 0 0;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: env(safe-area-inset-bottom);
}

@media (min-width: 640px) {
  .modal-box {
    border-radius: 1rem;
    max-width: 36rem;
    max-height: 85vh;
  }
}

/* ── 17. Dropdowns ───────────────────────────────────────── */
.dropdown-menu {
  position: absolute;
  z-index: 40;
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 0.75rem;
  box-shadow: 0 10px 25px -5px rgba(0,0,0,0.1), 0 8px 10px -6px rgba(0,0,0,0.05);
  min-width: 180px;
  max-width: calc(100vw - 2rem);
  overflow: hidden;
}

/* ── 18. Compose Page ────────────────────────────────────── */
.compose-card {
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
}

@media (max-width: 639px) {
  .compose-card {
    border-radius: 0;
    border-left: none;
    border-right: none;
  }
  /* Full-width compose form on phones */
  .compose-actions {
    flex-wrap: wrap;
    gap: 0.5rem;
  }
}

/* ── 19. Bulk Toolbar ────────────────────────────────────── */
@media (max-width: 639px) {
  #bulk-buttons {
    flex-wrap: wrap;
    gap: 0.25rem;
  }
  #bulk-buttons button {
    font-size: 0.7rem;
    padding: 0.35rem 0.6rem;
  }
}

/* ── 20. Pagination ──────────────────────────────────────── */
@media (max-width: 639px) {
  .pagination {
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
    text-align: center;
  }
}

/* ── 21. Settings / Forms ────────────────────────────────── */
@media (max-width: 639px) {
  .settings-grid {
    grid-template-columns: 1fr !important;
  }
  .settings-section {
    padding: 1rem !important;
  }
}

/* ── 22. Read/View Email Page ────────────────────────────── */
.email-body-content {
  word-break: break-word;
  overflow-wrap: anywhere;
}

.email-body-content img {
  height: auto;
  max-width: 100%;
}

@media (max-width: 639px) {
  .email-meta-row {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.25rem;
  }
  .email-action-buttons {
    flex-wrap: wrap;
    gap: 0.5rem;
  }
  .email-action-buttons a,
  .email-action-buttons button {
    flex: 1 1 auto;
    text-align: center;
    justify-content: center;
  }
}

/* ── 23. Spam / Trash / Sent Table Layout ────────────────── */
@media (max-width: 767px) {
  /* Convert table rows to card-like blocks on small screens */
  .email-table-mobile thead {
    display: none;
  }
  .email-table-mobile tr {
    display: flex;
    flex-direction: column;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid #f3f4f6;
  }
  .email-table-mobile td {
    display: block;
    padding: 0;
    border: none;
    font-size: 0.875rem;
  }
  .email-table-mobile td[data-label]::before {
    content: attr(data-label) ": ";
    font-weight: 600;
    font-size: 0.7rem;
    color: #9ca3af;
    text-transform: uppercase;
    letter-spacing: 0.05em;
  }
  .email-table-mobile td.actions-cell {
    margin-top: 0.5rem;
  }
}

/* ── 24. No Horizontal Scroll Anywhere ───────────────────── */
.no-overflow {
  overflow-x: hidden;
}

/* ── 25. Login Page ──────────────────────────────────────── */
@media (max-width: 479px) {
  .login-card {
    padding: 1.5rem 1.25rem !important;
    border-radius: 1rem !important;
  }
}

/* ── 26. Utility: Safe-area Padding ─────────────────────── */
.safe-bottom {
  padding-bottom: env(safe-area-inset-bottom);
}
.safe-top {
  padding-top: env(safe-area-inset-top);
}

/* ── 27. Textarea / Body Editor ──────────────────────────── */
textarea {
  resize: vertical;
  min-height: 120px;
}

@media (max-width: 639px) {
  textarea[name="body"] {
    min-height: 200px;
  }
}

/* ── 28. Scrollbar Thin (Chrome/Edge) ────────────────────── */
::-webkit-scrollbar {
  width: 4px;
  height: 4px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: #d1d5db;
  border-radius: 4px;
}

/* ── 29. Focus visible (keyboard nav) ───────────────────── */
:focus-visible {
  outline: 2px solid #3b82f6;
  outline-offset: 2px;
}

/* ── 30. Animation Performance ───────────────────────────── */
.transition,
[class*="transition"] {
  will-change: auto;
}

/* Mobile sidebar slide transition */
#mobile-sidebar {
  transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  will-change: transform;
}

/* ── 31. Prevent Content from Hiding Behind FAB ──────────── */
@media (max-width: 767px) {
  main {
    /* Extra padding so the last email row isn't hidden by FAB */
    padding-bottom: calc(5rem + env(safe-area-inset-bottom));
  }
  /* But NOT on compose page — no FAB there */
  .no-fab main {
    padding-bottom: env(safe-area-inset-bottom);
  }
}

/* ── 32. Toolbar search full-width on xs ─────────────────── */
@media (max-width: 479px) {
  .toolbar-search {
    width: 100% !important;
  }
}

/* ── 33. Card / Panel Responsive Padding ─────────────────── */
@media (max-width: 639px) {
  .card-padded {
    padding: 1rem !important;
  }
  .section-header {
    font-size: 0.95rem !important;
  }
}
