/* ══════════════════════════════════════════════════════════════
   layout.css  —  RetailPOS Kenya
   Mobile-first responsive layout fixes
   ══════════════════════════════════════════════════════════════ */

/* ── BASE RESET ── */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  overflow-x: hidden;
}

/* ── APP LAYOUT ── */
.app-layout {
  display: flex;
  min-height: 100vh;
  overflow-x: hidden;
}

/* ══════════════════════════════════════════════════════════════
   SIDEBAR
   ══════════════════════════════════════════════════════════════ */
.sidebar {
  width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 200;
  transition: transform 0.25s ease;
  overflow: hidden;
}

.sidebar-brand {
  padding: 20px 20px 16px;
  border-bottom: 1px solid var(--border-light);
  flex-shrink: 0;
}

.brand-logo {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-icon {
  width: 36px;
  height: 36px;
  background: var(--accent);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 18px;
  flex-shrink: 0;
}

.brand-name {
  font-size: 15px;
  font-weight: 600;
}

.brand-sub {
  font-size: 11px;
  color: var(--text-3);
}

.shop-switcher {
  margin: 12px 14px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  transition: background var(--transition);
  flex-shrink: 0;
}

.shop-switcher:hover { background: var(--border-light); }

.shop-info { flex: 1; min-width: 0; }

.shop-name {
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.shop-loc { font-size: 11px; color: var(--text-3); }

.sidebar-nav {
  flex: 1;
  padding: 8px 10px;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
}

.nav-section { margin-bottom: 20px; }

.nav-label {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  color: var(--text-3);
  padding: 0 10px;
  margin-bottom: 4px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  color: var(--text-2);
  text-decoration: none;
  font-size: 14px;
  transition: all var(--transition);
  width: 100%;
}

.nav-item:hover { background: var(--bg); color: var(--text); }
.nav-item.active { background: var(--accent-light); color: var(--accent); font-weight: 500; }
.nav-item .icon { width: 20px; text-align: center; flex-shrink: 0; }

.nav-badge {
  margin-left: auto;
  background: var(--red);
  color: white;
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 10px;
  flex-shrink: 0;
}

.sidebar-footer {
  padding: 14px;
  border-top: 1px solid var(--border-light);
  flex-shrink: 0;
}

.user-card { display: flex; align-items: center; gap: 10px; }

.user-avatar {
  width: 32px;
  height: 32px;
  background: var(--accent);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 12px;
  font-weight: 600;
  flex-shrink: 0;
}

.user-info { flex: 1; min-width: 0; }
.user-name { font-size: 13px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-role { font-size: 11px; color: var(--text-3); }

.btn-logout { color: var(--text-3); text-decoration: none; font-size: 16px; flex-shrink: 0; }
.btn-logout:hover { color: var(--red); }

/* ══════════════════════════════════════════════════════════════
   MAIN CONTENT
   ══════════════════════════════════════════════════════════════ */
.main-content {
  position: relative;
  margin-left: var(--sidebar-w);
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow-x: hidden;
  z-index: 1;
}

.top-bar {
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 24px;
  gap: 16px;
  position: sticky;
  top: 0;
  z-index: 50;
  flex-shrink: 0;
}

.page-title {
  font-size: 17px;
  font-weight: 600;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.top-bar-actions {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-shrink: 0;
}

/* Hamburger toggle — hidden on desktop */
.menu-toggle {
  display: none;
  background: none;
  border: none;
  font-size: 22px;
  cursor: pointer;
  color: var(--text);
  padding: 6px;
  border-radius: var(--radius-sm);
  min-width: 44px;
  min-height: 44px;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background var(--transition);
  line-height: 1;
}
.menu-toggle:hover { background: var(--bg); }

/* Toast container */
#toast-container {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 9999;
  max-width: calc(100vw - 40px);
}

/* ══════════════════════════════════════════════════════════════
   SIDEBAR OVERLAY
   CRITICAL: Default = invisible + no pointer-events.
   Only becomes interactive when sidebar is open on mobile.
   Uses class "open" to match app.js.
   z-index 150 = above main content (50) below sidebar (200).
   ══════════════════════════════════════════════════════════════ */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 150;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.25s ease;
  cursor: pointer;
}

/* ══════════════════════════════════════════════════════════════
   MODAL OVERLAY (FIXED)
   Prevents invisible layers from blocking clicks
   ══════════════════════════════════════════════════════════════ */
.modal-overlay {
  position: fixed;
  inset: 0;
  display: none;
  pointer-events: none;   /* ✅ no blocking when hidden */
  z-index: 300;           /* above everything */
}

.modal-overlay.open {
  display: flex;
  pointer-events: all;    /* ✅ only active when open */
}

/* ══════════════════════════════════════════════════════════════
   TABLES — RESPONSIVE SCROLL
   ══════════════════════════════════════════════════════════════ */
.table-wrap {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.table-wrap table {
  min-width: 560px;
}

/* ══════════════════════════════════════════════════════════════
   MODALS — MOBILE SAFE
   ══════════════════════════════════════════════════════════════ */
@media (max-width: 768px) {
  .modal-overlay {
    padding: 12px;
    align-items: flex-end;
  }

  .modal {
    max-width: 100% !important;
    width: 100%;
    margin: 0 !important;
    max-height: 85vh;
    border-radius: var(--radius-xl) var(--radius-xl) var(--radius-sm) var(--radius-sm);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
}

@media (max-width: 480px) {
  .modal {
    max-height: 90vh;
    border-radius: 16px 16px 4px 4px;
  }

  .modal-body {
    padding: 14px !important;
  }

  .modal-header {
    padding: 12px 14px;
  }
}

/* ══════════════════════════════════════════════════════════════
   BREAKPOINT: 768px (tablets / mobile landscape)
   ══════════════════════════════════════════════════════════════ */
@media (max-width: 768px) {
  /* Sidebar slides off-canvas */
  .sidebar {
    transform: translateX(-100%);
    box-shadow: none;
  }
  .sidebar.open {
    transform: translateX(0);
    box-shadow: var(--shadow-lg);
  }

  /* Overlay: exist in DOM but invisible until sidebar opens */
  .sidebar-overlay {
    display: none;
    pointer-events: none;
    opacity: 0;
  }
  .sidebar-overlay.open {
    display: block;
    pointer-events: all;
    opacity: 1;
  }

  /* Show hamburger */
  .menu-toggle { display: flex; }

  /* Remove sidebar offset */
  .main-content { margin-left: 0; }

  /* Page padding */
  .page-body { padding: 14px; }
  .top-bar { padding: 0 14px; gap: 10px; }

  /* Stat cards 2-col */
  .stat-cards { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .stat-value { font-size: 18px; }

  /* Forms stack */
  .form-row-2, .form-row-3 { grid-template-columns: 1fr; }

  /* Shops grid: single col */
  #shops-grid { grid-template-columns: 1fr !important; }

  /* Dashboard grid: single col */
  .dash-grid { grid-template-columns: 1fr !important; }

  /* POS layout: vertical stack */
  .pos-layout {
    grid-template-columns: 1fr;
    height: auto;
    min-height: 100dvh;
  }
  .pos-cart {
    border-left: none;
    border-top: 2px solid var(--border);
    max-height: none;
  }
  .pos-products { min-height: 300px; }
  .product-grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); }
}

/* ══════════════════════════════════════════════════════════════
   BREAKPOINT: 480px (small phones)
   ══════════════════════════════════════════════════════════════ */
@media (max-width: 480px) {
  .stat-cards { grid-template-columns: 1fr; }
  .page-body { padding: 10px; }
  .top-bar { padding: 0 10px; }
  .page-title { font-size: 15px; }
  .product-grid { grid-template-columns: repeat(2, 1fr); }
  .payment-methods .pay-method { font-size: 11px; padding: 7px 4px; }

  /* Buttons in top bar */
  .top-bar-actions .btn {
    padding: 7px 10px;
    font-size: 12px;
  }
}

/* ══════════════════════════════════════════════════════════════
   INVENTORY / PRODUCTS FILTER ROWS
   ══════════════════════════════════════════════════════════════ */
.inv-filter-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.inv-filter-row .search-wrap {
  flex: 1;
  min-width: 180px;
  max-width: 320px;
}

.inv-filter-row select { width: 140px; flex-shrink: 0; }

.products-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.products-header-row .search-wrap {
  flex: 1;
  min-width: 180px;
  max-width: 320px;
}

@media (max-width: 480px) {
  .inv-filter-row {
    flex-direction: column;
    align-items: stretch;
  }
  .inv-filter-row .search-wrap,
  .inv-filter-row select { max-width: 100%; width: 100%; }

  .products-header-row {
    flex-direction: column;
    align-items: stretch;
  }
  .products-header-row .search-wrap { max-width: 100%; }
  .products-header-row .btn { width: 100%; justify-content: center; }
}

/* ══════════════════════════════════════════════════════════════
   TABLE CARD-MODE on very small screens
   Rows become stacked cards; data-label attrs label each cell.
   ══════════════════════════════════════════════════════════════ */
@media (max-width: 560px) {
  .table-card-mode .table-wrap {
    overflow-x: visible;
  }

  .table-card-mode table,
  .table-card-mode thead,
  .table-card-mode tbody,
  .table-card-mode th,
  .table-card-mode td,
  .table-card-mode tr {
    display: block;
  }

  .table-card-mode .table-wrap table {
    min-width: unset;
    width: 100%;
  }

  .table-card-mode thead tr {
    position: absolute;
    top: -9999px;
    left: -9999px;
  }

  .table-card-mode tbody tr {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: 10px;
    padding: 12px 14px;
    display: flex;
    flex-wrap: wrap;
    gap: 8px 16px;
    align-items: flex-start;
  }

  .table-card-mode tbody td {
    flex: 1 1 45%;
    min-width: 0;
    padding: 0;
  }

  .table-card-mode tbody td::before {
    content: attr(data-label);
    display: block;
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    color: var(--text-3);
    margin-bottom: 3px;
    letter-spacing: 0.05em;
  }

  /* Actions cell always full-width */
  .table-card-mode tbody td[data-label="Actions"],
  .table-card-mode tbody td:last-child {
    flex: 1 1 100%;
    padding-top: 8px;
    border-top: 1px solid var(--border-light);
    margin-top: 4px;
  }

  .table-card-mode tbody td[data-label="Actions"]::before,
  .table-card-mode tbody td:last-child::before {
    display: none;
  }
}

/* ══════════════════════════════════════════════════════════════
   UNIT ROW in product modal
   ══════════════════════════════════════════════════════════════ */
.unit-row {
  display: grid;
  grid-template-columns: 1fr auto auto 28px;
  gap: 8px;
  align-items: center;
  background: var(--bg);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  margin-bottom: 6px;
}

@media (max-width: 560px) {
  .unit-row {
    grid-template-columns: 1fr 28px;
    grid-template-rows: auto auto auto;
    gap: 6px;
  }
  .unit-row > *:not(:last-child) { grid-column: 1; }
  .unit-row > *:last-child { grid-column: 2; grid-row: 1; }
}

/* ══════════════════════════════════════════════════════════════
   SAFE AREA INSETS (notch / home bar on iOS)
   ══════════════════════════════════════════════════════════════ */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  .sidebar-footer {
    padding-bottom: calc(14px + env(safe-area-inset-bottom));
  }
  .cart-footer {
    padding-bottom: calc(14px + env(safe-area-inset-bottom));
  }
}
