/* ============================================================
   POS System - Main Stylesheet (v2: mobile responsive)
   ============================================================ */

:root {
  --primary: #1a1f36;
  --primary-light: #252d47;
  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --success: #059669;
  --danger: #dc2626;
  --warning: #d97706;
  --info: #0284c7;
  --bg: #f0f2f8;
  --card: #ffffff;
  --text: #1e293b;
  --muted: #64748b;
  --border: #e2e8f0;
  --sidebar-w: 240px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

/* ---- Sidebar ---- */
#sidebar {
  position: fixed;
  top: 0; left: 0;
  width: var(--sidebar-w);
  height: 100vh;
  height: 100dvh;
  background: var(--primary);
  display: flex;
  flex-direction: column;
  z-index: 600;
  transition: transform .28s ease;
}

#sidebar .brand { padding: 20px 18px 16px; border-bottom: 1px solid rgba(255,255,255,.08); }
#sidebar .brand h1 { font-size: 1.1rem; font-weight: 700; color: #fff; letter-spacing: .5px; }
#sidebar .brand span { font-size: .72rem; color: rgba(255,255,255,.5); }

#sidebar nav { flex: 1; padding: 12px 0; overflow-y: auto; -webkit-overflow-scrolling: touch; }

#sidebar nav a {
  display: flex; align-items: center; gap: 11px;
  padding: 12px 18px;
  color: rgba(255,255,255,.72);
  text-decoration: none;
  font-size: .88rem; font-weight: 500;
  transition: background .18s, color .18s;
  border-left: 3px solid transparent;
  min-height: 44px; /* touch target */
}
#sidebar nav a:hover { background: rgba(255,255,255,.07); color: #fff; }
#sidebar nav a.active { background: rgba(79,70,229,.25); color: #fff; border-left-color: var(--accent); }
#sidebar nav a svg { flex-shrink: 0; opacity: .8; }

#sidebar .sidebar-footer { padding: 14px 18px; border-top: 1px solid rgba(255,255,255,.08); font-size: .78rem; color: rgba(255,255,255,.45); }
#sidebar .sidebar-footer strong { color: rgba(255,255,255,.8); display: block; margin-bottom: 2px; }

/* Sidebar overlay for mobile */
#sidebar-overlay {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,.45);
  z-index: 550;
}

/* Hamburger button — hidden on desktop */
#menu-toggle {
  display: none;
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px;
  margin-right: 6px;
  border-radius: 6px;
  color: var(--text);
}
#menu-toggle:hover { background: var(--bg); }

/* ---- Main ---- */
#main { margin-left: var(--sidebar-w); min-height: 100vh; display: flex; flex-direction: column; }

#topbar {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 0 20px;
  height: 58px;
  display: flex; align-items: center; justify-content: space-between;
  position: sticky; top: 0; z-index: 500;
}
#topbar h2 { font-size: 1rem; font-weight: 600; }
#topbar .topbar-left { display: flex; align-items: center; }
#topbar .topbar-right { display: flex; align-items: center; gap: 14px; }

.content { padding: 24px; flex: 1; }

/* ---- Cards ---- */
.card { background: var(--card); border-radius: 10px; border: 1px solid var(--border); padding: 20px; margin-bottom: 20px; }
.card-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; flex-wrap: wrap; gap: 10px; }
.card-header h3 { font-size: .95rem; font-weight: 600; }

/* ---- Stat Cards ---- */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 16px; margin-bottom: 24px; }
.stat-card { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 18px; display: flex; align-items: center; gap: 14px; }
.stat-icon { width: 46px; height: 46px; border-radius: 10px; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.stat-icon.blue   { background: #eff6ff; color: var(--info); }
.stat-icon.green  { background: #f0fdf4; color: var(--success); }
.stat-icon.orange { background: #fffbeb; color: var(--warning); }
.stat-icon.purple { background: #f5f3ff; color: var(--accent); }
.stat-icon.red    { background: #fef2f2; color: var(--danger); }
.stat-info .value { font-size: 1.4rem; font-weight: 700; }
.stat-info .label { font-size: .76rem; color: var(--muted); margin-top: 2px; }

/* ---- Tables ---- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: .87rem; min-width: 480px; }
thead th { background: var(--bg); padding: 10px 12px; text-align: left; font-weight: 600; color: var(--muted); font-size: .76rem; text-transform: uppercase; letter-spacing: .5px; border-bottom: 1px solid var(--border); white-space: nowrap; }
tbody td { padding: 11px 12px; border-bottom: 1px solid var(--border); }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #f8faff; }

/* ---- Badges ---- */
.badge { display: inline-block; padding: 3px 9px; border-radius: 20px; font-size: .71rem; font-weight: 600; text-transform: uppercase; letter-spacing: .4px; white-space: nowrap; }
.badge-success { background: #d1fae5; color: #065f46; }
.badge-danger  { background: #fee2e2; color: #991b1b; }
.badge-warning { background: #fef3c7; color: #92400e; }
.badge-info    { background: #e0f2fe; color: #075985; }
.badge-purple  { background: #ede9fe; color: #5b21b6; }

/* ---- Buttons ---- */
.btn { display: inline-flex; align-items: center; gap: 7px; padding: 9px 16px; border-radius: 7px; font-size: .85rem; font-weight: 600; cursor: pointer; border: none; transition: all .18s; text-decoration: none; min-height: 38px; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-success { background: var(--success); color: #fff; }
.btn-success:hover { background: #047857; }
.btn-danger  { background: var(--danger); color: #fff; }
.btn-danger:hover  { background: #b91c1c; }
.btn-warning { background: var(--warning); color: #fff; }
.btn-secondary { background: var(--bg); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--border); }
.btn-sm { padding: 6px 11px; font-size: .78rem; min-height: 32px; }
.btn-lg { padding: 13px 24px; font-size: 1rem; }

/* ---- Forms ---- */
.form-group { margin-bottom: 15px; }
.form-group label { display: block; font-size: .83rem; font-weight: 600; margin-bottom: 6px; }
.form-control { width: 100%; padding: 10px 13px; border: 1px solid var(--border); border-radius: 7px; font-size: .9rem; background: #fff; color: var(--text); transition: border .18s; outline: none; min-height: 42px; }
.form-control:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(79,70,229,.1); }
select.form-control { cursor: pointer; }

/* ---- Alerts ---- */
.alert { padding: 12px 16px; border-radius: 8px; font-size: .88rem; margin-bottom: 16px; display: flex; align-items: flex-start; gap: 10px; }
.alert-success { background: #d1fae5; color: #065f46; border: 1px solid #6ee7b7; }
.alert-danger  { background: #fee2e2; color: #991b1b; border: 1px solid #fca5a5; }
.alert-warning { background: #fef3c7; color: #92400e; border: 1px solid #fde68a; }

/* ---- Modals ---- */
.modal-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 900; align-items: center; justify-content: center; padding: 14px; }
.modal-overlay.open { display: flex; }
.modal { background: #fff; border-radius: 12px; padding: 24px; width: 100%; max-width: 540px; max-height: 92vh; max-height: 92dvh; overflow-y: auto; box-shadow: 0 20px 60px rgba(0,0,0,.2); }
.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 18px; }
.modal-header h3 { font-size: 1rem; font-weight: 700; }
.modal-close { background: none; border: none; cursor: pointer; font-size: 1.4rem; color: var(--muted); padding: 4px 8px; }

/* ---- POS Layout ---- */
#pos-layout {
  display: grid;
  grid-template-columns: 1fr 370px;
  gap: 16px;
  height: calc(100vh - 58px);
  height: calc(100dvh - 58px);
  padding: 14px;
}
#pos-left, #pos-right {
  background: var(--card);
  border-radius: 10px;
  border: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
}

#pos-search-bar { padding: 12px; border-bottom: 1px solid var(--border); display: flex; gap: 8px; flex-wrap: wrap; }
#barcode-input { flex: 1; min-width: 160px; padding: 11px 13px; font-size: 1rem; border: 2px solid var(--accent); border-radius: 8px; outline: none; background: #f5f3ff; }
#product-search { flex: 1; min-width: 150px; padding: 11px 13px; font-size: .9rem; border: 1px solid var(--border); border-radius: 8px; outline: none; }
#product-search:focus, #barcode-input:focus { box-shadow: 0 0 0 3px rgba(79,70,229,.15); }

#product-grid {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(128px, 1fr));
  gap: 10px;
  align-content: start;
}
.product-card { border: 1px solid var(--border); border-radius: 8px; padding: 12px 9px; cursor: pointer; text-align: center; transition: all .15s; background: #fff; position: relative; }
.product-card:active { transform: scale(.97); }
.product-card:hover { border-color: var(--accent); background: #f5f3ff; box-shadow: 0 4px 12px rgba(79,70,229,.15); }
.product-card .pname { font-size: .82rem; font-weight: 600; margin-bottom: 4px; line-height: 1.3; }
.product-card .pprice { font-size: .95rem; font-weight: 700; color: var(--accent); }
.product-card .pstock { font-size: .69rem; color: var(--muted); margin-top: 3px; }
.product-card .pexpiry { position: absolute; top: 4px; right: 4px; font-size: .6rem; padding: 2px 5px; border-radius: 4px; font-weight: 700; }
.pexpiry.expired  { background: #fee2e2; color: #991b1b; }
.pexpiry.expiring { background: #fef3c7; color: #92400e; }

/* Cart */
#cart-header { padding: 13px 15px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; }
#cart-header h3 { font-size: .95rem; font-weight: 700; }
#cart-items { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; min-height: 60px; }

.cart-item { display: grid; grid-template-columns: 1fr auto; gap: 6px; padding: 10px 14px; border-bottom: 1px solid var(--border); align-items: center; }
.cart-item:last-child { border-bottom: none; }
.cart-item .item-name { font-size: .85rem; font-weight: 600; }
.cart-item .item-price { font-size: .77rem; color: var(--muted); }
.cart-item .item-total { font-weight: 700; font-size: .9rem; text-align: right; }
.qty-controls { display: flex; align-items: center; gap: 6px; }
.qty-btn { width: 32px; height: 32px; border-radius: 7px; border: 1px solid var(--border); background: var(--bg); cursor: pointer; font-size: 1.05rem; font-weight: 700; display: flex; align-items: center; justify-content: center; transition: all .12s; }
.qty-btn:hover { background: var(--accent); color: #fff; border-color: var(--accent); }
.qty-btn:active { transform: scale(.92); }
.qty-display { font-weight: 700; min-width: 26px; text-align: center; font-size: .92rem; }
.remove-item { background: none; border: none; color: var(--danger); cursor: pointer; font-size: .85rem; padding: 6px 7px; border-radius: 5px; }
.remove-item:hover { background: #fee2e2; }

#cart-footer { padding: 12px 15px; border-top: 1px solid var(--border); background: #fafbff; }
.totals-row { display: flex; justify-content: space-between; font-size: .88rem; margin-bottom: 6px; }
.totals-row.grand { font-weight: 700; font-size: 1.05rem; border-top: 1px solid var(--border); padding-top: 8px; margin-top: 6px; }
.discount-row { display: flex; align-items: center; gap: 8px; margin: 8px 0; }
.discount-row label { font-size: .83rem; font-weight: 600; white-space: nowrap; }
.discount-row input { flex: 1; padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; font-size: .9rem; min-height: 38px; }

.payment-method { display: flex; gap: 8px; margin: 10px 0; }
.pay-btn { flex: 1; padding: 11px; border: 2px solid var(--border); border-radius: 8px; background: #fff; cursor: pointer; font-size: .85rem; font-weight: 600; transition: all .15s; min-height: 44px; }
.pay-btn.active { border-color: var(--accent); background: #f5f3ff; color: var(--accent); }

#btn-checkout { width: 100%; padding: 14px; font-size: 1rem; font-weight: 700; background: var(--success); color: #fff; border: none; border-radius: 8px; cursor: pointer; margin-top: 10px; transition: background .18s; min-height: 48px; }
#btn-checkout:hover { background: #047857; }
#btn-checkout:disabled { background: var(--muted); cursor: not-allowed; }
#btn-save-draft { width: 100%; padding: 10px; font-size: .85rem; font-weight: 600; background: #fff; color: var(--warning); border: 1px solid #fde68a; border-radius: 8px; cursor: pointer; margin-top: 6px; min-height: 42px; }
#btn-save-draft:hover { background: #fffbeb; }
#btn-clear-cart { width: 100%; padding: 10px; font-size: .85rem; font-weight: 600; background: #fff; color: var(--danger); border: 1px solid #fca5a5; border-radius: 8px; cursor: pointer; margin-top: 6px; min-height: 42px; }
#btn-clear-cart:hover { background: #fee2e2; }

.drafts-bar { padding: 8px 12px; background: #fffbeb; border-bottom: 1px solid #fde68a; font-size: .8rem; }
.draft-chip { display: inline-block; background: #fef3c7; border: 1px solid #fde68a; border-radius: 5px; padding: 6px 10px; margin: 2px; cursor: pointer; font-size: .78rem; transition: background .15s; }
.draft-chip:hover { background: #fde68a; }
.draft-chip .del-draft { color: var(--danger); margin-left: 4px; font-weight: 700; }

/* Receipt modal (screen preview only — print CSS lives in pos.php) */
#receipt-modal .modal { max-width: 380px; }

/* ---- Misc ---- */
.empty-state { text-align: center; padding: 36px 18px; color: var(--muted); }
.empty-state svg { opacity: .3; margin-bottom: 10px; }
.empty-state p { font-size: .9rem; }

.search-results-dropdown { position: absolute; background: #fff; border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.12); z-index: 400; max-height: 260px; overflow-y: auto; min-width: 240px; }
.search-result-item { padding: 12px 14px; cursor: pointer; font-size: .88rem; display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.search-result-item:hover { background: var(--bg); }
.search-result-item .sname { font-weight: 600; }
.search-result-item .sprice { color: var(--accent); font-weight: 700; white-space: nowrap; }

.spinner { width: 18px; height: 18px; border: 2px solid rgba(255,255,255,.4); border-top-color: #fff; border-radius: 50%; animation: spin .6s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }

.low-stock-badge { color: var(--danger); font-weight: 700; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
.grid-form-list { display: grid; grid-template-columns: 340px 1fr; gap: 18px; }

/* ============================================================
   MOBILE RESPONSIVE
   ============================================================ */

/* Tablets and below */
@media (max-width: 1024px) {
  #pos-layout { grid-template-columns: 1fr 320px; }
  .grid-2 { grid-template-columns: 1fr; }
  .grid-form-list { grid-template-columns: 1fr; }
}

/* Mobile */
@media (max-width: 820px) {
  /* Sidebar becomes off-canvas drawer */
  #sidebar { transform: translateX(-100%); box-shadow: 4px 0 24px rgba(0,0,0,.25); }
  #sidebar.open { transform: translateX(0); }
  #sidebar-overlay.show { display: block; }

  #menu-toggle { display: inline-flex; align-items: center; }

  #main { margin-left: 0; }
  #topbar { padding: 0 12px; }
  #topbar h2 { font-size: .92rem; }
  .topbar-right span { display: none; } /* hide date on small screens */

  .content { padding: 14px; }
  .card { padding: 15px; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .stat-card { padding: 13px; gap: 10px; }
  .stat-icon { width: 38px; height: 38px; }
  .stat-info .value { font-size: 1.1rem; }

  /* POS: stack vertically — products on top, cart below.
     FIX for mobile scroll: the layout itself scrolls the page, and each
     panel sizes to its content. #pos-left keeps a capped, independently
     scrolling product grid; #pos-right (cart) flows naturally below so the
     Complete Sale button is always reachable by scrolling the page. */
  #pos-layout {
    display: flex;
    flex-direction: column;
    height: auto;                 /* was fixed dvh — caused trapped scroll */
    min-height: calc(100dvh - 58px);
    padding: 8px;
    gap: 8px;
    overflow: visible;
  }
  #pos-left {
    flex: none;
    height: 46vh;                 /* products panel: fixed height, scrolls inside */
    min-height: 260px;
  }
  #pos-right {
    flex: none;
    height: auto;                 /* cart grows with content, page scrolls */
    overflow: visible;
  }
  #cart-items { max-height: none; overflow: visible; }
  #product-grid { grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); gap: 8px; padding: 8px; overflow-y: auto; -webkit-overflow-scrolling: touch; }
  .product-card { padding: 9px 6px; }
  .product-card .pname { font-size: .74rem; }
  .product-card .pprice { font-size: .85rem; }

  #pos-search-bar { padding: 8px; gap: 6px; }
  #barcode-input, #product-search { min-width: 0; font-size: 16px; } /* 16px stops iOS zoom */

  .modal { padding: 18px; border-radius: 12px 12px 0 0; max-width: none; align-self: flex-end; }
  .modal-overlay { padding: 0; align-items: flex-end; }
  #receipt-modal .modal { max-width: none; }

  table { font-size: .8rem; }
  thead th, tbody td { padding: 8px 9px; }
}

/* Small phones */
@media (max-width: 400px) {
  .stats-grid { grid-template-columns: 1fr; }
  #product-grid { grid-template-columns: repeat(3, 1fr); }
  .payment-method { flex-direction: row; }
}
