
/* =============================================================================
   MOBILE RESPONSIVENESS — FULL APPLICATION
   BizMaker v2 · 2026
   Covers: Topbar, Nav, Calculator, Dashboard, Materials, Settings, Modals, Toasts
   ============================================================================= */

/* ─── MOBILE MENU BACKDROP ─── */
.mobile-menu-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 999;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s ease;
}
.mobile-menu-backdrop.active {
  opacity: 1;
  visibility: visible;
}

/* ─── CARD-VIEW TABLES (used on materials table, etc.) ─── */
@media (max-width: 768px) {

  .table-mobile-cards {
    border: 0 !important;
    background: transparent !important;
    display: block !important;
    overflow: visible !important;
    white-space: normal !important;
  }

  .table-mobile-cards thead {
    display: none !important;
  }

  .table-mobile-cards tbody {
    display: block;
  }

  .table-mobile-cards tr {
    display: grid !important;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 10px;
    margin-bottom: 10px;
    padding: 14px;
  }

  .table-mobile-cards td {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    padding: 0 !important;
    border: 0 !important;
    text-align: left;
    white-space: normal !important;
  }

  /* Label above each field */
  .table-mobile-cards td::before {
    content: attr(data-label);
    font-weight: 700;
    text-transform: uppercase;
    font-size: 10px;
    color: var(--muted);
    letter-spacing: 0.5px;
    margin-bottom: 4px;
    line-height: 1;
  }
  .table-mobile-cards td[data-label=""]::before {
    display: none;
  }

  /* Inputs fill available space */
  .table-mobile-cards td input,
  .table-mobile-cards td select {
    width: 100% !important;
    max-width: 100% !important;
    box-sizing: border-box;
  }
  .table-mobile-cards td .input-group {
    width: 100%;
  }

  /* Buttons in cards */
  .table-mobile-cards td button {
    width: 100%;
    text-align: center;
    justify-content: center;
  }

  /* ── Materials Table: 2-col grid ── */
  #materialsTable.table-mobile-cards td:nth-child(1) { grid-column: 1 / -1; } /* Name */
  #materialsTable.table-mobile-cards td:nth-child(2) { grid-column: 1 / -1; } /* Variant */
  #materialsTable.table-mobile-cards td:nth-child(3) { grid-column: 1 / 2; } /* Qty */
  #materialsTable.table-mobile-cards td:nth-child(4) { grid-column: 2 / 3; } /* Unit */
  #materialsTable.table-mobile-cards td:nth-child(5) { grid-column: 1 / -1; } /* Cost */
  #materialsTable.table-mobile-cards td:nth-child(6) { grid-column: 1 / -1; margin-top: 6px; } /* Actions */

  /* ── Machine Extras: single-col stack ── */
  .machine-extras-table.table-mobile-cards td { grid-column: 1 / -1; }
}


/* =============================================================================
   MAIN MOBILE BREAKPOINT — ALL PAGES
   ============================================================================= */
@media (max-width: 768px) {

  /* ─── GLOBAL ─── */
  html { -webkit-text-size-adjust: 100%; }

  .container {
    padding: 10px 12px;
  }

  h2 {
    font-size: 20px;
    margin-top: 4px;
    margin-bottom: 12px;
  }

  /* ─── TOPBAR ─── */
  .topbar-inner {
    flex-wrap: wrap;
    padding: 10px 14px;
    min-height: 56px;
  }

  .brand {
    flex: 1;
    min-width: 0; /* allow text truncation */
  }
  .title {
    font-size: 16px;
  }
  .subtitle {
    font-size: 11px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 55vw;
  }

  /* Hamburger button — always visible, large touch target */
  .icon-btn {
    display: inline-flex !important;
    width: 44px;
    height: 44px;
    border-radius: 12px;
    flex-shrink: 0;
  }

  /* ─── MOBILE DROPDOWN MENU ─── */
  .topbar-right {
    display: flex !important;
    visibility: hidden;
    opacity: 0;
    flex-direction: column;
    align-items: stretch;
    gap: 0;
    background: rgba(7, 10, 18, 0.97);
    backdrop-filter: blur(24px);
    -webkit-backdrop-filter: blur(24px);
    border-bottom: 1px solid var(--border);
    border-radius: 0 0 20px 20px;
    padding: 12px 16px 20px;
    position: fixed;
    top: 56px;
    left: 0;
    right: 0;
    z-index: 1001;
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.5);
    transform: translateY(-12px);
    transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
    pointer-events: none;
    max-height: calc(100dvh - 60px);
    overflow-y: auto;
    overscroll-behavior: contain;
  }
  .topbar-right.is-open {
    visibility: visible;
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
  }

  /* Light theme menu */
  html.light-theme .topbar-right {
    background: rgba(255, 255, 255, 0.97);
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.12);
  }

  /* Nav links — large touch targets */
  .nav {
    flex-direction: column;
    width: 100%;
    align-items: stretch;
    gap: 2px;
    margin-bottom: 12px;
  }
  .nav .pill,
  .nav .icon-link {
    width: 100%;
    justify-content: flex-start;
    padding: 14px 16px;
    border-radius: 12px;
    border: 1px solid transparent;
    background: transparent;
    font-size: 15px;
    font-weight: 500;
    height: auto;
    min-height: 48px; /* Touch-friendly min-height */
  }
  .nav .pill:hover, .nav .icon-link:hover,
  .nav .pill.active, .nav .icon-link.active {
    background: rgba(96, 165, 250, 0.08);
    border-color: rgba(96, 165, 250, 0.2);
    color: var(--primary);
  }
  .icon-link .mobile-text {
    display: inline-block;
    margin-left: 12px;
  }
  .icon-link .icon-gear {
    font-size: 20px;
  }

  /* Lang/user section in menu */
  .lang-switch {
    width: 100%;
    flex-direction: column;
    gap: 6px;
    border-top: 1px solid var(--border);
    padding-top: 12px;
  }
  .lang-switch .pill {
    width: 100%;
    justify-content: center;
    padding: 12px;
    min-height: 44px;
  }
  .lang-switch span.pill {
    background: transparent;
    border: none;
    color: var(--muted);
    font-size: 13px;
    padding: 4px 0;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    pointer-events: none;
    min-height: auto;
  }


  /* ─── GRID SYSTEMS — stack on mobile ─── */
  .grid2,
  .grid3,
  .grid4 {
    grid-template-columns: 1fr !important;
    gap: 10px;
  }
  .grid2-bias-left {
    grid-template-columns: 1fr !important;
  }


  /* ─── PREVENT iOS INPUT ZOOM (16px minimum) ─── */
  input, select, textarea, .textarea, .form-control {
    font-size: 16px !important;
  }


  /* ─── CARDS ─── */
  .card {
    padding: 14px;
    border-radius: 14px;
    margin-top: 12px;
  }


  /* ─── BUTTONS — touch-friendly ─── */
  .btn {
    min-height: 44px;
    padding: 10px 14px;
    font-size: 14px;
  }
  .actions {
    flex-direction: column;
    width: 100%;
    align-items: stretch;
    gap: 8px;
  }
  .actions .btn {
    width: 100%;
    text-align: center;
    justify-content: center;
  }


  /* ─── TABLES — horizontal scroll fallback ─── */
  .table:not(.table-mobile-cards) {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
    border-radius: 10px;
  }
  .table:not(.table-mobile-cards) th,
  .table:not(.table-mobile-cards) td {
    padding: 10px 12px;
    font-size: 13px;
  }


  /* ─── TABS — horizontal scroll, no wrap ─── */
  .tabs {
    gap: 6px;
    justify-content: flex-start;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 4px;
    scrollbar-width: none;
  }
  .tabs::-webkit-scrollbar { display: none; }
  .tab-btn {
    white-space: nowrap;
    flex-shrink: 0;
    min-height: 40px;
    padding: 8px 14px;
    font-size: 13px;
  }


  /* ─── KPI CARDS ─── */
  .kpi {
    padding: 14px;
  }
  .kpi-value {
    font-size: 20px;
  }


  /* ─── STATS GRID (dashboard) — 2 cols ─── */
  .stats {
    grid-template-columns: 1fr 1fr !important;
  }
  .stat-value {
    font-size: 16px;
  }


  /* ─── CHART WRAP ─── */
  .chart-wrap {
    padding: 8px;
  }
  .chart-wrap canvas {
    max-height: 200px;
  }


  /* ─── STOCK ADD (calculator page) ─── */
  .stock-add {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }
  .stock-add label {
    width: 100%;
  }
  .stock-add-actions {
    width: 100%;
    display: flex;
    gap: 10px;
  }
  .stock-add-actions .btn,
  .stock-add-actions .pill {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
    min-height: 44px;
  }


  /* ─── MACHINE CARDS (calculator) ─── */
  .machine-card {
    padding: 14px !important;
    border-radius: 14px !important;
  }
  .machine-header {
    flex-direction: column;
    align-items: stretch !important;
    gap: 10px;
  }
  .machine-top-row {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }
  .machine-header-actions {
    flex-wrap: wrap;
    gap: 8px;
  }
  .machine-header-actions .btn {
    flex: 1;
    min-width: 0;
    justify-content: center;
  }
  .machine-header-actions > div {
    flex: 1;
    min-width: 0;
  }
  .machine-header-actions select {
    width: 100%;
  }
  .machine-fields.grid3 {
    grid-template-columns: 1fr 1fr !important;
  }
  .machine-fields.grid2 {
    grid-template-columns: 1fr !important;
  }
  .machine-toolbar {
    flex-direction: column;
    gap: 8px;
    align-items: stretch;
  }
  .machine-toolbar .btn {
    width: 100%;
    justify-content: center;
  }


  /* ─── PRICING MODE ROW (calculator) ─── */
  .pricing-mode-row {
    display: flex !important;
    flex-direction: column !important;
    gap: 10px !important;
  }
  #marginModeField,
  #priceModefield {
    width: 100%;
  }
  #pricingModeBtn {
    width: 100%;
    justify-content: center;
  }


  /* ─── RESULTS SECTION ─── */
  .results-header {
    flex-direction: column;
    gap: 12px;
    align-items: stretch;
  }
  .results-header .btn {
    width: 100%;
    text-align: center;
  }
  .breakdown li {
    font-size: 13px;
    padding: 8px 4px;
    gap: 8px;
  }


  /* ─── INLINE CHECKBOX GROUPS ─── */
  .inline {
    padding-top: 8px !important;
    flex-wrap: wrap;
  }


  /* ─── SEARCH BOX ─── */
  .table-tools {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }
  .search-box {
    max-width: 100% !important;
  }


  /* ─── SETTINGS PAGE ─── */
  #settingsPage .card {
    padding: 16px;
  }
  .settings-section {
    padding-bottom: 16px;
    margin-bottom: 16px;
  }
  .setting-item select,
  .setting-item .form-control {
    max-width: 100% !important;
    width: 100%;
  }
  .radio-group label,
  .checkbox-group label {
    padding: 10px 0;
    min-height: 44px;
    align-items: center;
  }
  .radio-group input[type="radio"],
  .checkbox-group input[type="checkbox"] {
    width: 22px;
    height: 22px;
  }
  #settingsPage .btn {
    width: 100%;
    justify-content: center;
    min-height: 44px;
  }
  #settingsPage fieldset .btn + .btn {
    margin-top: 8px;
  }


  /* ─── RAW MATERIALS PAGE ─── */
  #rawMaterialsPage .form .grid3 {
    grid-template-columns: 1fr !important;
  }
  #rawMaterialsPage .actions {
    margin-top: 12px;
  }
  #rmPropsList {
    display: flex;
    flex-direction: column;
    gap: 6px;
  }


  /* ─── MODALS ─── */
  .bizmaker-modal-overlay,
  .modal-overlay {
    align-items: flex-end; /* slide-up from bottom on mobile */
    padding: 0;
  }
  .bizmaker-modal,
  .modal {
    width: 100% !important;
    max-width: 100% !important;
    max-height: 85dvh;
    border-radius: 20px 20px 0 0 !important;
    margin: 0 !important;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 20px 16px 24px !important;
  }
  .bizmaker-modal-actions {
    flex-direction: column;
    gap: 8px;
  }
  .bizmaker-modal-actions .btn {
    width: 100%;
    min-width: unset !important;
  }

  /* Price History modal */
  #priceHistoryModal .modal-content {
    width: 95% !important;
    max-width: 100% !important;
    margin: 3vh auto !important;
    padding: 16px !important;
    border-radius: 16px !important;
    max-height: 90dvh;
    overflow-y: auto;
  }
  #priceHistoryChart {
    height: 200px !important;
  }


  /* ─── TOASTS — bottom center ─── */
  .toast-wrap {
    right: 8px;
    left: 8px;
    bottom: 8px;
    max-width: 100%;
  }
  .toast {
    font-size: 13px;
    padding: 12px;
    border-radius: 14px;
  }


  /* ─── INPUT GROUP (currency symbol) ─── */
  .input-group {
    width: 100%;
  }
  .input-group input {
    width: 100%;
  }


  /* ─── FORM ROW (auth pages) ─── */
  .form-row {
    flex-direction: column;
    align-items: stretch;
  }
  .form-row label {
    width: 100%;
    margin-bottom: 4px;
  }


  /* ─── ACTIVITY LIST (dashboard) ─── */
  .activity-list {
    max-height: 300px;
  }
  .activity-item {
    padding: 8px 0;
    gap: 8px;
  }
  .activity-title { font-size: 12px; }
  .activity-meta { font-size: 10px; }


  /* ─── ADMIN PAGES ─── */
  .table-responsive {
    border-radius: 12px;
  }
  .search-input-group {
    flex-direction: column;
  }
  .search-input-group input {
    border-radius: 10px !important;
    border-right: 1px solid var(--border) !important;
  }
  .search-input-group .btn {
    border-radius: 10px !important;
    width: 100%;
  }
  .pagination {
    flex-wrap: wrap;
    justify-content: center;
  }


  /* ─── SECTION TITLE ─── */
  .section-title {
    font-size: 12px;
    margin: 12px 0 8px;
  }


  /* ─── SAFE AREA BOTTOM (notch devices) ─── */
  main.container {
    padding-bottom: max(16px, env(safe-area-inset-bottom));
  }
}


/* =============================================================================
   EXTRA-SMALL SCREENS (≤ 400px)
   ============================================================================= */
@media (max-width: 400px) {
  .container {
    padding: 8px;
  }
  .card {
    padding: 10px;
    border-radius: 12px;
  }
  .title {
    font-size: 14px;
  }
  .subtitle {
    display: none; /* hide subtitle on very small screens */
  }
  .stats {
    grid-template-columns: 1fr !important; /* single column on very small */
  }
  .kpi-value {
    font-size: 18px;
  }
  .tab-btn {
    font-size: 12px;
    padding: 6px 10px;
  }
  .machine-card .grid3 {
    grid-template-columns: 1fr !important; /* single column on tiny screens */
  }
}


/* =============================================================================
   LANDSCAPE PHONE (short height)
   ============================================================================= */
@media (max-height: 500px) and (max-width: 900px) {
  .topbar-right {
    max-height: calc(100dvh - 50px);
    padding: 8px 12px 12px;
  }
  .nav .pill,
  .nav .icon-link {
    padding: 10px 14px;
    min-height: 40px;
    font-size: 14px;
  }
}
