/* ═══════════════════════════════════════════════════════════════
   Countertop Mobile Framework
   Responsive overrides for mobile web (≤768px)
   Include AFTER sidebar-layout.css on every page.
   ═══════════════════════════════════════════════════════════════ */

/* ─── Touch-target sizes ──────────────────────────────────── */
@media (max-width: 768px) {
  :root {
    --touch-min: 44px;       /* Apple HIG minimum */
    --mobile-pad: 12px;
    --bottom-nav-h: 62px;
    --mobile-safe-bottom: env(safe-area-inset-bottom, 0px);
  }

  html, body {
    overflow-x: hidden;
  }

  /* ─── Hide sidebar, show bottom nav ─────────────────────── */
  .sidebar { display: none !important; }
  .sidebar-toggle { display: none !important; }
  .sidebar-overlay { display: none !important; }

  .main-content {
    margin-left: 0 !important;
    padding-bottom: calc(var(--bottom-nav-h) + var(--mobile-safe-bottom) + 10px) !important;
    overflow-x: hidden !important;
  }

  /* ─── Mobile top header ─────────────────────────────────── */
  .mobile-top-bar {
    display: flex !important;
    position: fixed;
    top: 0; left: 0; right: 0;
    height: 52px;
    background: #FEFCFA;
    border-bottom: 1px solid #E8DDD0;
    z-index: 1100;
    align-items: center;
    padding: 0 var(--mobile-pad);
    box-shadow: 0 1px 3px rgba(0,0,0,0.04);
  }
  .mobile-top-bar img { height: 28px; width: auto; }
  .mobile-top-bar .mtb-title {
    font-family: 'Montserrat', sans-serif;
    font-weight: 700;
    font-size: 1rem;
    color: #5A4A42;
    margin-left: 8px;
  }

  .main-content {
    padding-top: 60px !important;
  }

  /* ─── Bottom navigation ─────────────────────────────────── */
  .mobile-bottom-nav {
    display: flex !important;
    position: fixed;
    bottom: 0; left: 0; right: 0;
    height: calc(var(--bottom-nav-h) + var(--mobile-safe-bottom));
    background: #FEFCFA;
    border-top: 1px solid #E8DDD0;
    z-index: 1100;
    justify-content: space-between;
    align-items: center;
    gap: 2px;
    padding: 0 4px var(--mobile-safe-bottom);
    box-shadow: 0 -1px 4px rgba(0,0,0,0.04);
  }
  .mobile-bottom-nav a,
  .mobile-bottom-nav button.mobile-nav-menu-trigger {
    display: flex;
    flex: 1 1 0;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-decoration: none;
    color: #8B7B73;
    font-size: 0.6rem;
    font-weight: 600;
    padding: 4px 2px;
    min-width: 0;
    min-height: var(--touch-min);
    border-radius: 8px;
    transition: all 0.15s;
    text-align: center;
    line-height: 1.1;
    background: transparent;
    border: none;
    appearance: none;
  }
  .mobile-bottom-nav a.active,
  .mobile-bottom-nav button.mobile-nav-menu-trigger.active {
    color: #5A4A42;
    background: #F5E2D0;
    font-weight: 800;
  }
  .mobile-bottom-nav a .nav-emoji,
  .mobile-bottom-nav button.mobile-nav-menu-trigger .nav-emoji {
    font-size: 1.3rem;
    line-height: 1;
    margin-bottom: 2px;
  }

  .mobile-nav-popover {
    position: fixed;
    right: 10px;
    bottom: calc(var(--bottom-nav-h) + var(--mobile-safe-bottom) + 10px);
    background: #fff;
    border: 1px solid #E8DDD0;
    border-radius: 12px;
    box-shadow: 0 8px 24px rgba(0,0,0,0.14);
    min-width: 170px;
    z-index: 1120;
    padding: 6px;
  }
  .mobile-nav-popover a {
    display: flex;
    align-items: center;
    gap: 8px;
    color: #5A4A42;
    text-decoration: none;
    border-radius: 8px;
    padding: 8px 10px;
    font-size: 0.82rem;
    font-weight: 600;
  }
  .mobile-nav-popover a.active {
    background: #F5E2D0;
    color: #3D2518;
  }
  .mobile-nav-popover a .nav-emoji {
    font-size: 1rem;
    margin: 0;
  }

  /* ─── Page headers ──────────────────────────────────────── */
  .page-header {
    padding: 1rem var(--mobile-pad) !important;
    margin-bottom: 1rem !important;
    border-radius: 0 !important;
  }
  .page-header h1 { font-size: 1.5rem !important; }
  .page-header p { font-size: 0.85rem !important; }

  /* ─── Container overrides ───────────────────────────────── */
  .container, .container-fluid {
    padding-left: var(--mobile-pad) !important;
    padding-right: var(--mobile-pad) !important;
  }

  /* ─── Touch-friendly buttons ────────────────────────────── */
  .btn {
    min-height: var(--touch-min);
    padding: 10px 16px !important;
    font-size: 0.9rem !important;
  }
  .btn-sm {
    min-height: 36px;
    padding: 6px 12px !important;
    font-size: 0.8rem !important;
  }

  /* ─── Cards ─────────────────────────────────────────────── */
  .card {
    border-radius: 10px !important;
    margin-bottom: 0.75rem !important;
  }

  /* ─── Settings sections ─────────────────────────────────── */
  .settings-section {
    padding: 1rem !important;
    margin-bottom: 1rem !important;
  }

  /* ─── Info grid → stack on mobile ───────────────────────── */
  .info-grid {
    grid-template-columns: 1fr !important;
  }

  /* ─── Forms ─────────────────────────────────────────────── */
  .form-row {
    flex-direction: column !important;
  }
  input, textarea, select {
    font-size: 16px !important; /* Prevents iOS zoom */
    min-height: var(--touch-min);
  }

  /* ─── Modals ────────────────────────────────────────────── */
  .modal-content {
    margin: 0 !important;
    max-width: 100% !important;
    width: 100% !important;
    min-height: 50vh;
    border-radius: 16px 16px 0 0 !important;
    position: fixed !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    max-height: 90vh !important;
  }

  /* ─── Count cards grid → 2 col on mobile ────────────────── */
  .col-md-6.col-lg-4 {
    flex: 0 0 50% !important;
    max-width: 50% !important;
  }

  /* ─── Count session interface ─ stack columns ────────────── */
  .col-md-3, .col-md-7, .col-md-5, .col-md-8, .col-md-4, .col-md-9 {
    flex: 0 0 100% !important;
    max-width: 100% !important;
  }

  /* ─── Count-session keypad-first mobile layout ──────────── */
  /* Reorder: keypad (order 1) → items (order 2) → group selector (order 3) */
  #count-interface-container .col-md-9 {
    order: 1 !important;
  }
  #count-interface-container .col-md-3 {
    order: 3 !important;
  }
  /* Within the col-md-9, reorder keypad before items */
  #count-interface-container .col-md-9 .row {
    display: flex !important;
    flex-direction: column !important;
  }
  #count-interface-container .col-md-9 .col-md-5 {
    order: -1 !important;
  }
  #count-interface-container .col-md-9 .col-md-7 {
    order: 1 !important;
  }
  /* Make the main row a flex column for reordering */
  #count-interface-container .container-fluid > .row {
    display: flex !important;
    flex-direction: column !important;
  }

  /* Keypad card: compact on mobile */
  #count-interface-container .keypad-custom {
    max-width: 100% !important;
  }
  #count-interface-container .keypad-custom button {
    height: 2.85rem !important;
    min-height: 2.85rem !important;
    font-size: 1rem !important;
  }

  /* Item detail in keypad: compact horizontal layout */
  #count-interface-container .col-md-5 .card {
    max-width: 100% !important;
  }
  #count-interface-container .col-md-5 h5 {
    font-size: 0.95rem !important;
  }

  /* Items list: fixed height, scrollable */
  #count-interface-container .col-md-7 .card {
    max-height: 35vh !important;
    overflow-y: auto !important;
  }

  /* Group selector: compact horizontal scroll */
  #count-interface-container .col-md-3 .card {
    max-height: 25vh !important;
  }
  #count-interface-container .col-md-3 .btn-group {
    flex-wrap: nowrap !important;
    overflow-x: auto !important;
  }

  /* Sort buttons: smaller on mobile */
  #count-interface-container .btn-group .btn {
    font-size: 0.7rem !important;
    padding: 4px 8px !important;
    min-height: 32px !important;
    white-space: nowrap;
  }

  /* Progress bar: compact */
  #count-interface-container .col-md-9 > .card.mb-2 {
    order: -2 !important;
  }

  /* ─── Vendor editor – mobile UX ──────────────────────────── */
  .ve-layout { grid-template-columns: 1fr !important; padding: 0 !important; gap: 0 !important; }
  .ve-list-panel { max-height: none !important; position: static !important; border-radius: 0 !important; border: none !important; }
  .ve-edit-panel { border-radius: 0 !important; border: none !important; position: fixed !important; top: 0; left: 0; right: 0; bottom: 0; z-index: 50; overflow-y: auto; transform: translateX(100%); transition: transform .25s ease; }
  .ve-edit-panel.mobile-open { transform: translateX(0) !important; }
  .ve-back-btn { display: inline-block !important; }
  .ve-save-footer { display: block !important; }
  .ve-edit-header .ve-save-btn { display: none !important; }
  .ve-vendor-item { padding: 14px 16px !important; }
  .ve-vendor-avatar { width: 44px !important; height: 44px !important; font-size: 1.1rem !important; }
  .ve-field-row { flex-direction: column !important; gap: 0 !important; }
  .ve-field.narrow { flex: 1 !important; }
  .ve-edit-body { padding: 16px 16px 100px !important; }
  .ve-quick-add { flex-direction: column !important; }
  .ve-quick-add button { width: 100% !important; }
  .ve-toast { bottom: 80px !important; }

  /* Legacy vendor editor stack (fallback) */
  div[style*="gridTemplateColumns: '340px 1fr'"],
  div[style*="grid-template-columns"] {
    grid-template-columns: 1fr !important;
  }

  /* ─── Hide desktop-only elements on mobile ──────────────── */
  .mobile-header { display: none !important; }

  /* ─── User/store info row ───────────────────────────────── */
  .user-row {
    flex-direction: column;
    align-items: flex-start !important;
    gap: 8px;
  }
  .user-actions {
    width: 100%;
    justify-content: flex-end;
  }

  /* ─── Location rows ─────────────────────────────────────── */
  .location-row {
    flex-direction: column;
    align-items: flex-start !important;
    gap: 6px;
  }

  /* ─── Runs timeline ─────────────────────────────────────── */
  div[style*="gap: '10px'"] {
    flex-direction: column !important;
  }
}

  /* ─── Mobile Count Overview ────────────────────────────────── */
  #calendar-panel { display: none !important; }
  #overview-create-count-section { display: none !important; }
  .page-header { display: none !important; }

  .mobile-count-overview {
    display: flex;
    flex-direction: column;
    height: calc(100vh - 52px - var(--bottom-nav-h));
    overflow: hidden;
    background: #FEFCFA;
  }

  /* Calendar strip */
  .mco-calendar-strip {
    display: flex;
    gap: 6px;
    padding: 8px var(--mobile-pad);
    background: #FEFCFA;
    border-bottom: 1px solid #E8DDD0;
    flex-shrink: 0;
  }
  .mco-cal-day {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 6px 4px 8px;
    border-radius: 12px;
    border: 1.5px solid #E8DDD0;
    background: #fff;
    cursor: pointer;
    transition: all 0.15s;
    -webkit-tap-highlight-color: transparent;
  }
  .mco-cal-day:active { transform: scale(0.96); }
  .mco-cal-today { border-color: #C8B5A0; background: #FAF6F2; }
  .mco-cal-selected {
    border-color: #5A4A42 !important;
    background: #5A4A42 !important;
    color: #fff !important;
  }
  .mco-cal-selected .mco-cal-dayname,
  .mco-cal-selected .mco-cal-month { color: #E8DDD0 !important; }
  .mco-cal-selected .mco-cal-date { color: #fff !important; }
  .mco-cal-dayname {
    font-size: 0.6rem;
    font-weight: 600;
    color: #8B7B73;
    text-transform: uppercase;
    letter-spacing: 0.04em;
  }
  .mco-cal-date {
    font-size: 1.25rem;
    font-weight: 700;
    color: #5A4A42;
    line-height: 1.2;
  }
  .mco-cal-month {
    font-size: 0.55rem;
    color: #A89890;
    font-weight: 500;
  }
  .mco-cal-dots {
    display: flex;
    gap: 3px;
    margin-top: 3px;
    align-items: center;
  }
  .mco-cal-dot {
    width: 5px;
    height: 5px;
    border-radius: 50%;
    display: inline-block;
  }
  .mco-cal-dot-more {
    font-size: 0.5rem;
    color: #8B7B73;
    font-weight: 600;
  }

  /* Controls */
  .mco-controls {
    padding: 6px var(--mobile-pad);
    flex-shrink: 0;
    border-bottom: 1px solid #E8DDD0;
    background: #fff;
  }
  .mco-sort-row {
    display: flex;
    gap: 4px;
    margin-bottom: 6px;
  }
  .mco-sort-btn {
    flex: 1;
    border: 1px solid #D4C4B0;
    background: #fff;
    color: #5A4A42;
    font-size: 0.7rem;
    font-weight: 600;
    padding: 5px 2px;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.15s;
    -webkit-tap-highlight-color: transparent;
  }
  .mco-sort-active {
    background: #C8B5A0;
    border-color: #C8B5A0;
    color: #fff;
  }
  .mco-filter-row {
    display: flex;
    gap: 6px;
  }
  .mco-filter-select,
  .mco-search-input {
    flex: 1;
    border: 1px solid #D4C4B0;
    border-radius: 6px;
    padding: 5px 8px;
    font-size: 0.75rem;
    color: #5A4A42;
    background: #fff;
    min-height: 32px;
  }

  /* Calendar filter label */
  .mco-cal-filter-label {
    padding: 4px var(--mobile-pad);
    font-size: 0.72rem;
    color: #5A4A42;
    background: #FAF6F2;
    border-bottom: 1px solid #E8DDD0;
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
  }
  .mco-cal-clear {
    background: none;
    border: none;
    color: #8B7B73;
    font-size: 0.7rem;
    cursor: pointer;
    text-decoration: underline;
    padding: 0;
  }

  /* Sections */
  .mco-section {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
  }
  .mco-section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px var(--mobile-pad);
    background: #FAF6F2;
    border-bottom: 1px solid #E8DDD0;
    flex-shrink: 0;
  }
  .mco-section-title {
    font-size: 0.8rem;
    font-weight: 700;
    color: #5A4A42;
  }
  .mco-section-count {
    font-size: 0.65rem;
    font-weight: 700;
    background: #C8B5A0;
    color: #fff;
    border-radius: 10px;
    padding: 1px 8px;
    min-width: 22px;
    text-align: center;
  }
  .mco-scroll-area {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 6px var(--mobile-pad);
  }
  .mco-scroll-active {
    max-height: 30vh;
    flex: none;
  }
  .mco-empty {
    text-align: center;
    padding: 1.5rem 0;
    color: #A89890;
    font-size: 0.8rem;
  }

  /* Mobile Count Card */
  .mobile-count-card {
    background: #fff;
    border: 1px solid #E8DDD0;
    border-radius: 10px;
    padding: 10px 12px;
    margin-bottom: 8px;
    position: relative;
    cursor: pointer;
    transition: box-shadow 0.15s;
    -webkit-tap-highlight-color: transparent;
  }
  .mobile-count-card:active { box-shadow: 0 2px 8px rgba(90,74,66,0.15); }
  .mcc-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 4px;
  }
  .mcc-name {
    font-weight: 700;
    font-size: 0.88rem;
    color: #5A4A42;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    margin-right: 8px;
  }
  .mcc-status-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    flex-shrink: 0;
  }
  .mcc-meta {
    font-size: 0.7rem;
    color: #8B7B73;
    margin-bottom: 6px;
    display: flex;
    flex-wrap: wrap;
    gap: 2px;
    align-items: center;
  }
  .mcc-sep { margin: 0 2px; }
  .mcc-progress-track {
    height: 4px;
    background: #F0EBE6;
    border-radius: 2px;
    margin-bottom: 8px;
    overflow: hidden;
  }
  .mcc-progress-fill {
    height: 100%;
    border-radius: 2px;
    transition: width 0.3s;
  }
  .mcc-actions {
    display: flex;
    gap: 4px;
  }
  .mcc-btn {
    border: none;
    border-radius: 6px;
    font-size: 0.68rem;
    font-weight: 600;
    padding: 5px 10px;
    cursor: pointer;
    min-height: 30px;
    transition: opacity 0.15s;
    -webkit-tap-highlight-color: transparent;
  }
  .mcc-btn:active { opacity: 0.75; }
  .mcc-btn-green { background: #28a745; color: #fff; }
  .mcc-btn-orange { background: #FFA726; color: #fff; flex: 1; }
  .mcc-btn-dark { background: #5A4A42; color: #fff; flex: 1; }
  .mcc-btn-light { background: #E8DDD0; color: #5A4A42; }
  .mcc-btn-outline { background: transparent; border: 1px solid #C8B5A0; color: #5A4A42; }
  .mcc-btn-full { flex: 1; }

  /* Card context menu */
  .mcc-menu {
    position: absolute;
    right: 12px;
    bottom: 50px;
    background: #fff;
    border: 1px solid #E8DDD0;
    border-radius: 10px;
    box-shadow: 0 6px 16px rgba(0,0,0,0.12);
    z-index: 200;
    min-width: 150px;
    padding: 4px 0;
  }
  .mcc-menu button {
    display: block;
    width: 100%;
    padding: 8px 14px;
    border: none;
    background: none;
    text-align: left;
    font-size: 0.8rem;
    color: #5A4A42;
    cursor: pointer;
  }
  .mcc-menu button:active { background: #FAF6F2; }
  .mcc-menu-danger { color: #d32f2f !important; }

  /* Preview modal */
  .mco-modal-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.5);
    z-index: 9999;
    display: flex;
    align-items: flex-end;
    justify-content: center;
  }
  .mco-modal {
    background: #FEFCFA;
    border-radius: 16px 16px 0 0;
    padding: 1.25rem;
    width: 100%;
    max-height: 75vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  .mco-modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.75rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid #E8DDD0;
    font-weight: 700;
    color: #5A4A42;
    font-size: 1rem;
  }
  .mco-modal-header button {
    background: none;
    border: none;
    font-size: 1.5rem;
    color: #8B7B73;
    cursor: pointer;
    line-height: 1;
  }
  .mco-modal-meta {
    font-size: 0.78rem;
    color: #8B7B73;
    margin-bottom: 0.5rem;
  }
  .mco-modal-items { }
  .mco-modal-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 4px;
    border-bottom: 1px solid #FAF6F2;
    font-size: 0.82rem;
    color: #5A4A42;
  }
  .mco-modal-item-name {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* ─── Dashboard mobile overrides ──────────────────────────── */
  .dash-container { padding: 8px var(--mobile-pad) !important; }
  .dash-header { margin-bottom: .75rem !important; }
  .dash-title { font-size: 1.2rem !important; }
  .dash-subtitle { font-size: .75rem !important; }

  .dash-stats-row { gap: 6px; margin-bottom: .75rem; }
  .dash-stat { padding: 8px 6px; min-width: 60px; }
  .dash-stat-num { font-size: 1.2rem; }
  .dash-stat-label { font-size: .55rem; }

  .dash-grid { grid-template-columns: 1fr !important; gap: .75rem; }
  .dash-section { padding: .75rem; }
  .dash-section-title { font-size: .85rem; }
  .dash-card { padding: 8px 9px; margin-bottom: 6px; }
  .dash-card-title {
    font-size: .78rem;
    white-space: normal !important;
    overflow-wrap: anywhere;
  }
  .dash-card-meta { font-size: .62rem; gap: 3px 6px; }
  .dash-completed-grid { grid-template-columns: 1fr 1fr !important; gap: 6px; }
}

/* ─── Desktop: hide mobile elements ───────────────────────── */
@media (min-width: 769px) {
  .mobile-top-bar { display: none !important; }
  .mobile-bottom-nav { display: none !important; }
}
