:root{
  --bg: #FAFAF9; --panel: #FFFFFF; --line: #E7E5E2; --text: #1E1E1C;
  --muted: #8A8782; --muted-2: #B7B4AE; --accent: #2F6F5E; --accent-soft: #EAF3F0;
  --amber: #C77D2E; --amber-soft: #FBF1E6; --red: #C24B44; --red-soft: #FBEDEC;
  --blue: #2E6FAD; --blue-soft: #E6F0FA;
  --display: 'Manrope', sans-serif; --body: 'Inter', sans-serif;
}
*{box-sizing:border-box; margin:0; padding:0;}
body{background:var(--bg); color:var(--text); font-family:var(--body); min-height:100vh; display:flex;}
.sidebar{width:210px; background:var(--panel); border-right:1px solid var(--line); display:flex; flex-direction:column; flex-shrink:0; position:sticky; top:0; height:100vh;}
.brand{padding:24px 22px 18px;}
.brand-mark{font-family:var(--display); font-weight:800; font-size:16px; display:flex; align-items:center; gap:8px;}
.brand-mark .dot{width:9px;height:9px;border-radius:50%; background:var(--accent);}
.brand-sub{font-size:11px; color:var(--muted-2); margin-top:3px;}
nav{padding:8px 12px; flex:1;}
.nav-item{display:flex; align-items:center; gap:11px; padding:10px 12px; border-radius:8px; color:var(--muted); font-size:13.5px; font-weight:600; cursor:pointer; margin-bottom:2px;}
.nav-item:hover{background:var(--bg); color:var(--text);}
.nav-item.active{background:var(--accent-soft); color:var(--accent);}
.nav-icon{width:18px; text-align:center; font-size:15px; opacity:.85;}
.sidebar-foot{padding:16px 22px; border-top:1px solid var(--line); font-size:11px; color:var(--muted-2); display:flex; justify-content:space-between; align-items:center;}
.status-live{display:flex; align-items:center; gap:6px;}
.status-live .led{width:6px;height:6px;border-radius:50%;background:var(--accent);}
main{flex:1; display:flex; flex-direction:column; min-width:0;}
.topbar{padding:22px 32px; border-bottom:1px solid var(--line); display:flex; justify-content:space-between; align-items:center; background:var(--panel);}
.topbar h1{font-family:var(--display); font-size:21px; font-weight:800;}
.topbar .path{font-size:12px; color:var(--muted-2); margin-top:2px;}
.content{padding:28px 32px; flex:1; overflow-y:auto;}
.view{display:none;}
.view.active{display:block;}
.grid{display:grid; gap:16px;}
.cols-4{grid-template-columns:repeat(4,1fr);}
.cols-3{grid-template-columns:2fr 1fr;}
.card{background:var(--panel); border:1px solid var(--line); border-radius:12px; padding:18px 20px;}
.stat-label{font-size:12px; color:var(--muted); font-weight:600;}
.stat-value{font-family:var(--display); font-size:26px; font-weight:800; margin-top:8px;}
.stat-delta{font-size:11.5px; margin-top:5px; color:var(--muted-2);}
.up{color:var(--accent);} .down{color:var(--red);}
.card-title{font-family:var(--display); font-weight:700; font-size:15px; margin-bottom:14px; display:flex; justify-content:space-between; align-items:center;}
.card-title .tag{font-size:11.5px; color:var(--muted-2); font-weight:500;}
table{width:100%; border-collapse:collapse; font-size:13px;}
th{text-align:left; font-size:11px; color:var(--muted-2); font-weight:600; padding:9px 10px; border-bottom:1px solid var(--line);}
td{padding:11px 10px; border-bottom:1px solid var(--line); vertical-align:middle;}
tr:last-child td{border-bottom:none;}
tr:hover td{background:var(--bg);}
.mono{color:var(--muted); font-size:12px;}
.serial-tag{display:inline-block; font-size:11px; color:var(--accent); background:var(--accent-soft); border-radius:5px; padding:3px 8px; font-weight:600; cursor:pointer;}
.badge{font-size:11px; padding:4px 10px; border-radius:20px; font-weight:600;}
.badge.ok{background:var(--accent-soft); color:var(--accent);}
.badge.low{background:var(--red-soft); color:var(--red);}
.badge.mid{background:var(--amber-soft); color:var(--amber);}
.btn{background:var(--accent); color:#fff; border:none; border-radius:8px; padding:11px 18px; font-family:var(--display); font-weight:700; font-size:13.5px; cursor:pointer;}
.btn:hover{background:#265A4C;}
.btn.secondary{background:var(--panel); color:var(--text); border:1px solid var(--line);}
.btn.secondary:hover{background:var(--bg);}
.btn.block{width:100%; padding:13px; margin-top:16px;}
.btn.small{padding:8px 14px; font-size:12px;}
.pos-layout{display:grid; grid-template-columns:1fr 340px; gap:18px;}
.product-grid{display:grid; grid-template-columns:repeat(3,1fr); gap:12px;}
.product-card{background:var(--panel); border:1px solid var(--line); border-radius:10px; padding:16px; cursor:pointer;}
.product-card:hover{border-color:var(--accent);}
.product-card .pname{font-weight:700; font-size:13.5px; margin-bottom:4px;}
.product-card .pmeta{font-size:11.5px; color:var(--muted-2); margin-bottom:10px;}
.product-card .pprice{font-family:var(--display); font-weight:800; color:var(--accent); font-size:15.5px;}

/* ---------- shared search box (used in Transactions toolbar) ---------- */
.search-wrap{position:relative; flex:0 0 260px;}
.search-wrap .search-icon{position:absolute; left:14px; top:50%; transform:translateY(-50%); color:var(--muted-2); font-size:15px; pointer-events:none;}
.search-wrap input{width:100%; padding:12px 14px 12px 38px; border:1px solid var(--line); border-radius:10px; font-size:13.5px; background:var(--panel); font-family:var(--body); color:var(--text);}
.search-wrap input:focus{outline:2px solid var(--accent); outline-offset:1px;}

/* ---------- TRANSACTIONS hub: Add Sale / Add Purchase entry points ---------- */
.txn-toolbar{display:flex; align-items:center; gap:16px; flex-wrap:wrap; justify-content:space-between; margin-bottom:18px;}
.btn.sale{background:var(--red); color:#fff;}
.btn.sale:hover{background:#A83E38;}
.btn.purchase{background:var(--blue); color:#fff;}
.btn.purchase:hover{background:#25578A;}

/* ---------- modals (Add Sale / Add Purchase) ---------- */
.modal-overlay{position:fixed; inset:0; background:rgba(20,20,18,.4); opacity:0; pointer-events:none; transition:opacity .2s; z-index:70;}
.modal-overlay.open{opacity:1; pointer-events:auto;}
.modal-box{position:fixed; top:50%; left:50%; transform:translate(-50%,-46%); width:520px; max-width:92vw; max-height:88vh; overflow-y:auto; background:var(--panel); border-radius:14px; box-shadow:0 24px 60px rgba(0,0,0,.25); padding:24px; opacity:0; pointer-events:none; transition:opacity .2s, transform .2s; z-index:71;}
.modal-box.open{opacity:1; pointer-events:auto; transform:translate(-50%,-50%);}
#purchase-modal{width:640px;}
.modal-head{display:flex; justify-content:space-between; align-items:center;}
.modal-close{cursor:pointer; color:var(--muted); font-size:15px; padding:4px 6px; border-radius:6px;}
.modal-close:hover{background:var(--bg); color:var(--text);}
.item-add-row{display:flex; gap:8px; margin-bottom:14px;}
.item-add-row select{flex:1; border:1px solid var(--line); border-radius:8px; padding:10px 12px; font-size:13px; background:var(--bg); color:var(--text); font-family:var(--body);}
.item-add-row input{width:70px; border:1px solid var(--line); border-radius:8px; padding:10px; font-size:13px; text-align:center;}
.cart-items-scroll{max-height:280px; overflow-y:auto; margin-bottom:4px;}

.qty-stepper{display:flex; align-items:center; gap:7px; margin-top:4px;}
.qty-stepper span{width:20px; height:20px; border-radius:5px; background:var(--bg); border:1px solid var(--line); display:inline-flex; align-items:center; justify-content:center; font-size:12px; cursor:pointer; font-weight:700; color:var(--text);}
.qty-stepper span:hover{border-color:var(--accent); color:var(--accent);}
.qty-stepper .qty-val{border:none; background:none; cursor:default; width:auto; min-width:14px;}
.cart{background:var(--panel); border:1px solid var(--line); border-radius:12px; padding:18px; height:fit-content;}
.cart-item{display:flex; justify-content:space-between; padding:10px 0; border-bottom:1px solid var(--line); font-size:13px;}
.cart-item .ciname{font-weight:600;}
.cart-item .ciserial{font-size:11px; color:var(--muted-2); margin-top:2px;}
.cart-item .remove{color:var(--red); cursor:pointer; font-size:11px; margin-left:8px;}
.cart-total-row{display:flex; justify-content:space-between; padding:7px 0; font-size:13px; color:var(--muted);}
.cart-total-row.grand{color:var(--text); font-weight:800; font-family:var(--display); font-size:18px; border-top:1px solid var(--line); margin-top:6px; padding-top:14px;}
.invoice-sheet{background:#fff; border:1px solid var(--line); color:#1C1C1C; border-radius:12px; padding:36px; max-width:640px;}
.inv-head{display:flex; justify-content:space-between; border-bottom:2px solid #1C1C1C; padding-bottom:16px; margin-bottom:16px;}
.inv-head .store{font-family:var(--display); font-weight:800; font-size:20px;}
.inv-meta{font-size:12px; color:#666; text-align:right; line-height:1.6;}
.inv-table{width:100%; font-size:12.5px; border-collapse:collapse;}
.inv-table th{color:#888; border-bottom:1px solid #ddd; text-align:left; padding:8px 4px; font-size:11px; font-weight:600;}
.inv-table td{padding:9px 4px; border-bottom:1px solid #eee;}
.inv-serial{font-size:11px; color:var(--accent); font-weight:600;}
.inv-total{display:flex; justify-content:flex-end; margin-top:14px;}
.inv-total table{width:230px; font-size:13px;}
.inv-total td{padding:5px 0;}
.inv-total tr:last-child td{font-weight:800; font-size:17px; border-top:1px solid #1C1C1C; padding-top:9px; font-family:var(--display);}
.ticket{background:var(--panel); border:1px solid var(--line); border-left:3px solid var(--accent); border-radius:10px; padding:16px 18px; margin-bottom:12px;}
.ticket-top{display:flex; justify-content:space-between; margin-bottom:8px;}
.ticket-id{font-size:11.5px; color:var(--muted-2); font-weight:600;}
.ticket-device{font-weight:700; font-size:14.5px;}
.ticket-issue{font-size:12.5px; color:var(--muted); margin-top:5px;}
.ticket-actions{margin-top:10px; display:flex; gap:8px; align-items:center;}
.form-grid{display:grid; grid-template-columns:1fr 1fr; gap:14px;}
.field{display:flex; flex-direction:column; gap:6px;}
.field.full{grid-column:1/-1;}
.field label{font-size:12px; font-weight:600; color:var(--muted);}
.field input, .field select, .field textarea{border:1px solid var(--line); border-radius:8px; padding:10px 12px; font-size:13px; font-family:var(--body); background:var(--bg); color:var(--text);}
.field input:focus, .field select:focus, .field textarea:focus{outline:2px solid var(--accent); outline-offset:1px; background:#fff;}
.field-hint{font-size:11px; color:var(--muted-2);}
.modal-actions{display:flex; gap:10px; margin-top:20px; justify-content:flex-end;}
.section-head{display:flex; justify-content:space-between; align-items:center; margin-bottom:16px;}
.parties-layout{display:grid; grid-template-columns:300px 1fr; gap:18px;}
.party-list{background:var(--panel); border:1px solid var(--line); border-radius:12px; padding:10px; height:fit-content;}
.party-row{display:flex; justify-content:space-between; align-items:center; padding:12px 12px; border-radius:8px; cursor:pointer; margin-bottom:2px;}
.party-row:hover{background:var(--bg);}
.party-row.active{background:var(--accent-soft);}
.party-name{font-weight:700; font-size:13.5px;}
.party-phone{font-size:11px; color:var(--muted-2); margin-top:2px;}
.party-balance{font-size:12.5px; font-weight:700; text-align:right;}
.party-balance.owe{color:var(--red);}
.party-balance.receive{color:var(--accent);}
.ledger-summary{display:flex; gap:14px; margin-bottom:18px;}
.ledger-stat{flex:1; background:var(--panel); border:1px solid var(--line); border-radius:12px; padding:16px 18px;}
.ledger-stat .l-label{font-size:12px; color:var(--muted); font-weight:600;}
.ledger-stat .l-value{font-family:var(--display); font-size:22px; font-weight:800; margin-top:6px;}
.ledger-entry{display:flex; justify-content:space-between; align-items:center; padding:12px 4px; border-bottom:1px solid var(--line);}
.ledger-entry:last-child{border-bottom:none;}
.le-desc{font-weight:600; font-size:13px;}
.le-date{font-size:11.5px; color:var(--muted-2); margin-top:2px;}
.le-amt{font-family:var(--display); font-weight:700; font-size:14px;}
.le-amt.debit{color:var(--red);}
.le-amt.credit{color:var(--accent);}
.le-running{font-size:11px; color:var(--muted-2); text-align:right; margin-top:2px;}
.empty{color:var(--muted-2); font-size:13px; padding:20px; text-align:center;}
.toast{position:fixed; bottom:24px; right:24px; background:var(--text); color:#fff; padding:12px 18px; border-radius:8px; font-size:13px; font-weight:600; z-index:999; opacity:0; transform:translateY(10px); transition:.2s; pointer-events:none;}
.toast.show{opacity:1; transform:translateY(0);}
.btn.secondary.filter-active{background:var(--accent); color:#fff; border-color:var(--accent);}
.txn-badge{font-size:10.5px; padding:3px 9px; border-radius:20px; font-weight:700; text-transform:uppercase; letter-spacing:.03em;}
.txn-badge.sale{background:var(--accent-soft); color:var(--accent);}
.txn-badge.purchase{background:var(--amber-soft); color:var(--amber);}
.txn-badge.expense{background:var(--red-soft); color:var(--red);}
.txn-badge.return{background:#F0EAF9; color:#7C5CBF;}
.txn-badge.payment{background:#E6F0FA; color:#2E6FAD;}
.txn-amt.in{color:var(--accent); font-weight:700;}
.txn-amt.out{color:var(--red); font-weight:700;}
.sync-badge{display:none; align-items:center; gap:6px; padding:6px 12px; border-radius:20px; background:var(--amber-soft); color:var(--amber); font-size:11.5px; font-weight:700; white-space:nowrap;}
.sync-badge.review{background:var(--red-soft); color:var(--red);}
.sync-summary{display:flex; gap:28px;}
.sync-summary-row{display:flex; flex-direction:column; gap:4px; font-size:12px; color:var(--muted); font-weight:600;}
.sync-summary-row strong{font-family:var(--display); font-size:20px; font-weight:800; color:var(--text);}
.sync-summary-row strong.warn{color:var(--red);}
.sync-review-item{display:flex; justify-content:space-between; align-items:center; padding:12px 4px; border-bottom:1px solid var(--line); gap:12px;}
.sync-review-item:last-child{border-bottom:none;}
.sync-review-actions{display:flex; gap:8px; flex-shrink:0;}

/* ---------- mobile-only nav (hidden on desktop; see the media query below) ---------- */
.bottom-tabbar{display:none;}
.more-sheet-overlay{display:none;}
.more-sheet{display:none;}

/* ---------- auth: first-run setup / sign-in, shown before anything else ---------- */
.auth-overlay{
  display:none; position:fixed; inset:0; z-index:1000; background:var(--bg);
  align-items:center; justify-content:center; padding:24px;
}
.auth-overlay.open{display:flex;}
.auth-box{width:100%; max-width:380px; background:var(--panel); border:1px solid var(--line);
  border-radius:16px; padding:32px 28px; box-shadow:0 24px 60px rgba(0,0,0,.08);}
.auth-brand{font-family:var(--display); font-weight:800; font-size:17px; display:flex; align-items:center; gap:8px; margin-bottom:22px;}
.auth-brand .dot{width:10px; height:10px; border-radius:50%; background:var(--accent);}
.auth-box h2{font-family:var(--display); font-size:19px; font-weight:800; margin-bottom:6px;}
.auth-sub{font-size:12.5px; color:var(--muted); margin-bottom:18px; line-height:1.5;}
.auth-box .field{margin-bottom:12px;}
.auth-error{font-size:12.5px; color:var(--red); min-height:16px; margin-bottom:4px;}

@media print{
  .sidebar, .topbar{display:none;}
  .content{padding:0;}
}

/* ============================================================
   MOBILE (<= 640px) — bottom tab bar, single-column layouts,
   card-style tables, bottom-sheet modals, 44px+ touch targets.
   Desktop rules above are untouched; everything here only
   applies at phone widths.
   ============================================================ */
@media (max-width: 640px){

  /* ---- layout shell: sidebar out, bottom tab bar in ---- */
  .sidebar{display:none;}
  body{display:block;}
  main{width:100%;}
  .topbar{padding:14px 16px; gap:8px;}
  .topbar h1{font-size:17px;}
  .topbar .path{display:none;}
  .content{padding:14px; padding-bottom:calc(80px + env(safe-area-inset-bottom));}

  /* ---- bottom tab bar ---- */
  .bottom-tabbar{
    display:flex; position:fixed; left:0; right:0; bottom:0; z-index:50;
    background:var(--panel); border-top:1px solid var(--line);
    padding:6px 4px calc(6px + env(safe-area-inset-bottom));
  }
  .tab-item{
    flex:1; display:flex; flex-direction:column; align-items:center; justify-content:center;
    gap:3px; min-height:52px; border-radius:10px; color:var(--muted);
    font-size:10.5px; font-weight:700; cursor:pointer; user-select:none;
  }
  .tab-item .tab-icon{font-size:19px; line-height:1;}
  .tab-item.active{color:var(--accent);}

  /* ---- "More" sheet ---- */
  .more-sheet-overlay{
    display:block; position:fixed; inset:0; background:rgba(20,20,18,.4);
    opacity:0; pointer-events:none; transition:opacity .2s; z-index:70;
  }
  .more-sheet-overlay.open{opacity:1; pointer-events:auto;}
  .more-sheet{
    display:flex; flex-direction:column; position:fixed; left:0; right:0; bottom:0;
    background:var(--panel); border-radius:16px 16px 0 0; box-shadow:0 -14px 40px rgba(0,0,0,.18);
    padding:10px 8px calc(16px + env(safe-area-inset-bottom)); z-index:71;
    transform:translateY(100%); transition:transform .25s cubic-bezier(.4,0,.2,1);
  }
  .more-sheet.open{transform:translateY(0);}
  .more-sheet-handle{width:36px; height:4px; border-radius:2px; background:var(--line); margin:2px auto 12px;}
  .more-sheet-title{font-family:var(--display); font-weight:800; font-size:15px; padding:0 10px 8px;}
  .more-sheet-item{
    display:flex; align-items:center; gap:12px; padding:14px 12px; border-radius:10px;
    font-size:14px; font-weight:600; color:var(--text); min-height:44px; cursor:pointer;
  }
  .more-sheet-item:active{background:var(--bg);}
  .more-sheet-item .tab-icon{width:20px; text-align:center; font-size:16px; color:var(--muted); flex-shrink:0;}

  /* ---- grids collapse to one column ---- */
  .cols-4, .cols-3, .pos-layout, .parties-layout, .form-grid, .product-grid{grid-template-columns:1fr;}
  .ledger-summary{flex-direction:column; gap:10px;}
  .section-head{flex-direction:column; align-items:flex-start; gap:8px;}
  .section-head > div[style*="display:flex"]{width:100%;}

  /* ---- tables: Inventory / Transactions / Invoices / Dashboard / Reports / Quotations
     become stacked label:value cards instead of overflowing columns ---- */
  table{font-size:12.5px;}
  th, td{padding:8px 6px;}
  .responsive-table thead{display:none;}
  .responsive-table, .responsive-table tbody{display:block; width:100%;}
  .responsive-table tr{
    display:block; border:1px solid var(--line); border-radius:10px; padding:4px 12px;
    margin-bottom:10px; background:var(--panel);
  }
  .responsive-table tr:last-child{margin-bottom:0;}
  .responsive-table td{
    display:flex; justify-content:space-between; align-items:center; gap:12px;
    padding:9px 0; border-bottom:1px dashed var(--line); text-align:right;
  }
  .responsive-table td:last-child{border-bottom:none;}
  .responsive-table td[data-label]:not([data-label=""])::before{
    content:attr(data-label); font-size:11px; font-weight:700; color:var(--muted-2);
    text-align:left; text-transform:uppercase; letter-spacing:.02em; flex-shrink:0;
  }
  .responsive-table td[data-label=""]{justify-content:flex-end;}
  .responsive-table td.empty{display:block; text-align:center;}

  /* ---- touch targets: buttons, tabs, dropdowns >= ~44px ---- */
  .btn{min-height:44px; padding:11px 16px;}
  .btn.small{min-height:40px; padding:10px 14px; font-size:12.5px;}
  .btn.block{min-height:48px;}
  .field input, .field select, .field textarea,
  .search-wrap input, .item-add-row select, .item-add-row input{
    font-size:16px; min-height:44px; padding:11px 12px; /* 16px avoids iOS auto-zoom-on-focus */
  }
  .party-row, .ledger-entry, .sync-review-item{min-height:44px;}
  .serial-tag{padding:6px 10px;}

  /* ---- Add Sale / Add Purchase become bottom sheets, safely above the tab bar ---- */
  .modal-box, #purchase-modal{
    left:0; right:0; bottom:0; top:auto; width:100%; max-width:100%;
    transform:translateY(100%); border-radius:16px 16px 0 0;
    max-height:85dvh; padding:18px 16px calc(18px + env(safe-area-inset-bottom));
  }
  .modal-box.open, #purchase-modal.open{transform:translateY(0);}
  .item-add-row{flex-wrap:wrap;}
  .item-add-row select{flex:1 1 100%;}
  .item-add-row input{flex:1;}
  .item-add-row button{flex:1;}

  /* ---- Transactions toolbar stacks cleanly ---- */
  .txn-toolbar{flex-direction:column; align-items:stretch;}
  .txn-toolbar > div{width:100%;}
  .search-wrap{flex:none;}
  .txn-toolbar button.btn{width:100%;}

  /* the toast sat bottom-right, which is exactly where the "More" tab now lives —
     move it above the tab bar so it can't silently swallow taps on that corner */
  .toast{
    left:16px; right:16px; bottom:calc(76px + env(safe-area-inset-bottom));
    text-align:center;
  }
}
