:root{
    box-sizing: border-box;
    padding-top: env(safe-area-inset-top, 0px);
    padding-bottom: env(safe-area-inset-bottom, 0px);
    --bg: #f5faf5;
    --surface: #ffffff;
    --surface-2: #eef8ee;
    --border: #d9ecd9;
    --text: #1c261c;
    --text-dim: #5f7460;
    --accent: #1f9d4a;
    --accent-dim: #e3f6e8;
    --danger: #b3442f;
    --danger-dim: #fbe9e5;
    --warn: #b8860b;
    --radius: 14px;
    --shadow: 0 1px 2px rgba(30,30,20,0.06), 0 4px 16px rgba(30,30,20,0.05);
  }
  :root:not([data-theme="light"]) {
    @media (prefers-color-scheme: dark) {
      --bg: #10160f;
      --surface: #181f16;
      --surface-2: #1e281c;
      --border: #2c3a28;
      --text: #e9f0e6;
      --text-dim: #98a894;
      --accent: #4ade80;
      --accent-dim: #1c3324;
      --danger: #e17b62;
      --danger-dim: #3a2320;
      --warn: #e0b84a;
      --shadow: 0 1px 2px rgba(0,0,0,0.3), 0 4px 20px rgba(0,0,0,0.35);
    }
  }
  :root[data-theme="dark"] {
    --bg: #10160f;
    --surface: #181f16;
    --surface-2: #1e281c;
    --border: #2c3a28;
    --text: #e9f0e6;
    --text-dim: #98a894;
    --accent: #4ade80;
    --accent-dim: #1c3324;
    --danger: #e17b62;
    --danger-dim: #3a2320;
    --warn: #e0b84a;
    --shadow: 0 1px 2px rgba(0,0,0,0.3), 0 4px 20px rgba(0,0,0,0.35);
  }
  html { scroll-padding-top: env(safe-area-inset-top, 0px); height: 100%; }
  * { box-sizing: border-box; }
  body {
    margin: 0;
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
  }
  .wrap {
    max-width: 1120px;
    margin: 0 auto;
    padding: 18px 16px 100px;
  }
  header.top {
    position: sticky;
    top: env(safe-area-inset-top, 0px);
    z-index: 20;
    background: var(--bg);
    padding: 14px 16px 10px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--border);
  }
  header.top h1 {
    font-size: 19px;
    margin: 0;
    display: flex;
    align-items: center;
    gap: 8px;
    letter-spacing: -0.01em;
  }
  header.top h1 .dot { font-size: 22px; }
  .tabs {
    display: flex;
    gap: 6px;
    max-width: 1120px;
    margin: 0 auto;
    padding: 10px 16px 0;
    position: sticky;
    top: calc(env(safe-area-inset-top, 0px) + 52px);
    background: var(--bg);
    z-index: 19;
  }
  .tab-btn {
    flex: 1;
    padding: 9px 8px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text-dim);
    font-size: 13.5px;
    font-weight: 600;
    cursor: pointer;
    transition: all .15s ease;
  }
  .tab-btn.active {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
  }
  section.view { display: none; padding-top: 16px; }
  section.view.active { display: block; }
  @media (min-width: 860px) {
    #view-billing.active {
      display: grid;
      grid-template-columns: minmax(0, 1.3fr) minmax(340px, 1fr);
      align-items: start;
      gap: 14px;
    }
    #view-billing > .card { min-width: 0; margin-bottom: 0; }
  }

  .card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 16px;
    margin-bottom: 14px;
  }
  .card h2 {
    font-size: 14px;
    margin: 0 0 12px;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: .05em;
    font-weight: 700;
  }

  /* Billing: item grid */
  .item-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
    gap: 10px;
  }
  .item-tile {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 12px 10px;
    text-align: left;
    cursor: pointer;
    transition: transform .1s ease, border-color .1s ease;
    display: flex;
    flex-direction: column;
    gap: 3px;
  }
  .item-tile:active { transform: scale(0.97); }
  .item-tile:hover { border-color: var(--accent); }
  .item-tile.highlighted { border-color: var(--accent); border-width: 2px; background: var(--accent-dim); box-shadow: 0 0 0 1px var(--accent); }
  .item-tile .produce-image {
    width: 100%;
    height: 104px;
    object-fit: cover;
    align-self: center;
    margin: 0 0 6px;
    border-radius: 8px;
    background: var(--surface);
  }
  .item-tile .name { font-weight: 600; font-size: 14px; line-height: 1.25; }
  .item-tile .price { font-size: 12.5px; color: var(--accent); font-weight: 600; }
  .item-tile .unit { font-size: 11px; color: var(--text-dim); }
  .item-tile .stockline { font-size: 10.5px; margin-top: 1px; color: var(--text-dim); }
  .item-tile .stockline.low { color: var(--warn); font-weight: 700; }
  .item-tile .stockline.out { color: var(--danger); font-weight: 700; }
  .empty-hint { color: var(--text-dim); font-size: 13.5px; padding: 10px 2px; }

  /* Cart */
  .cart-row {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 9px 0;
    border-bottom: 1px solid var(--border);
  }
  .cart-row:last-child { border-bottom: none; }
  .cart-row .cname { flex: 1; font-size: 14px; font-weight: 500; }
  .cart-row .cunit { font-size: 11px; color: var(--text-dim); }
  .qty-box { display: flex; align-items: center; gap: 6px; }
  .qty-box button {
    width: 26px; height: 26px; border-radius: 8px;
    border: 1px solid var(--border); background: var(--surface-2);
    color: var(--text); font-size: 15px; cursor: pointer; line-height: 1;
  }
  .qty-box input {
    width: 48px; text-align: center; border: 1px solid var(--border);
    border-radius: 8px; padding: 4px 2px; background: var(--surface);
    color: var(--text); font-size: 13px;
  }
  .cart-row .sub { width: 68px; text-align: right; font-weight: 600; font-size: 13.5px; }
  .cart-row .rm { background: none; border: none; color: var(--danger); font-size: 16px; cursor: pointer; padding: 2px 4px; }

  .total-line {
    display: flex; justify-content: space-between; align-items: baseline;
    padding-top: 12px; margin-top: 4px; border-top: 1px dashed var(--border);
  }
  .total-line .label { color: var(--text-dim); font-size: 14px; }
  .total-line .amt { font-size: 24px; font-weight: 700; color: var(--accent); }

  button.primary {
    width: 100%;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 12px;
    padding: 13px;
    font-size: 15px;
    font-weight: 700;
    cursor: pointer;
    margin-top: 12px;
  }
  button.primary:disabled { opacity: .5; cursor: not-allowed; }
  button.secondary {
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 9px 14px;
    font-size: 13.5px;
    font-weight: 600;
    cursor: pointer;
  }
  button.danger-outline {
    background: var(--danger-dim);
    color: var(--danger);
    border: 1px solid var(--danger);
    border-radius: 8px;
    padding: 5px 9px;
    font-size: 12px;
    cursor: pointer;
  }

  /* Items management */
  .item-form {
    display: grid;
    grid-template-columns: minmax(140px, 1.5fr) repeat(2, minmax(88px, 0.8fr)) repeat(2, minmax(115px, 0.9fr)) minmax(150px, 1.2fr) auto;
    gap: 8px;
    margin-bottom: 14px;
  }
  .item-form input, .item-form select {
    border: 1px solid var(--border);
    border-radius: 9px;
    padding: 9px 10px;
    font-size: 13.5px;
    background: var(--surface);
    color: var(--text);
    width: 100%;
  }
  .item-list-row-wrap {
    padding: 10px 0;
    border-bottom: 1px solid var(--border);
  }
  .item-list-row-wrap:last-child { border-bottom: none; }
  .ilr-top { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
  .ilr-top .iname { flex: 1; font-size: 14px; font-weight: 500; }
  .ilr-top .iunit { font-size: 11.5px; color: var(--text-dim); background: var(--surface-2); padding: 2px 7px; border-radius: 6px; }
  .ilr-top select.iunit { border: 1px solid var(--border); padding: 3px 6px; cursor: pointer; }
  .ilr-bottom { display: flex; align-items: center; gap: 7px; flex-wrap: wrap; }
  .ilr-bottom select.item-category-input {
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 5px 8px;
    background: var(--surface-2);
    color: var(--text);
    font-size: 12px;
  }
  .photo-upload { position: relative; overflow: hidden; }
  .photo-upload-input { display: none; }
  .stock-badge {
    font-size: 11.5px; padding: 4px 9px; border-radius: 8px;
    background: var(--surface-2); color: var(--text-dim); border: 1px solid var(--border);
    font-weight: 600; white-space: nowrap;
  }
  .stock-badge.low { background: var(--warn); color: #fff; border-color: var(--warn); opacity: .92; }
  .stock-badge.out { background: var(--danger-dim); color: var(--danger); border-color: var(--danger); }
  .mini-btn {
    border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
    border-radius: 8px; padding: 5px 10px; font-size: 12px; font-weight: 600; cursor: pointer; white-space: nowrap;
  }
  .mini-btn.waste { background: var(--danger-dim); color: var(--danger); border-color: var(--danger); }
  .price-edit {
    display: flex; align-items: center; gap: 3px;
    border: 1px solid var(--border); border-radius: 8px; padding: 4px 8px;
    background: var(--surface-2);
  }
  .price-edit span { font-size: 12.5px; color: var(--text-dim); }
  .price-edit .price-unit { white-space: nowrap; }
  .price-edit input {
    width: 62px; border: none; background: none; color: var(--text);
    font-size: 13.5px; font-weight: 600; text-align: right;
  }
  .price-edit input:focus { outline: none; }

  /* Sales summary */
  .stat-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-bottom: 4px;
  }
  .stat {
    background: var(--surface-2);
    border-radius: 12px;
    padding: 12px;
    border: 1px solid var(--border);
  }
  .stat .k { font-size: 11.5px; color: var(--text-dim); text-transform: uppercase; letter-spacing: .04em; }
  .stat .v { font-size: 20px; font-weight: 700; margin-top: 2px; }
  .rank-row {
    display: flex; justify-content: space-between; padding: 8px 0;
    border-bottom: 1px solid var(--border); font-size: 13.5px;
  }
  .rank-row:last-child { border-bottom: none; }
  .rank-row .rn { font-weight: 500; }
  .rank-row .rv { color: var(--text-dim); }
  .sale-entry {
    padding: 10px 0; border-bottom: 1px solid var(--border);
  }
  .sale-entry:last-child { border-bottom: none; }
  .sale-entry .se-top {
    display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 3px;
  }
  .sale-entry .se-time { font-size: 12px; color: var(--text-dim); }
  .sale-entry .se-total { font-weight: 700; color: var(--accent); font-size: 14.5px; }
  .sale-entry .se-items { font-size: 12.5px; color: var(--text-dim); }

  .toast {
    position: fixed;
    bottom: calc(24px + env(safe-area-inset-bottom, 0px));
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    background: var(--text);
    color: var(--bg);
    padding: 10px 18px;
    border-radius: 30px;
    font-size: 13.5px;
    font-weight: 600;
    opacity: 0;
    transition: all .25s ease;
    pointer-events: none;
    z-index: 50;
    white-space: nowrap;
  }
  .toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

  .filter-row { display: flex; gap: 6px; margin-bottom: 12px; }
  .filter-row button {
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--text-dim);
    border-radius: 8px;
    padding: 6px 12px;
    font-size: 12.5px;
    font-weight: 600;
    cursor: pointer;
  }
  .filter-row button.active { background: var(--accent); color: white; border-color: var(--accent); }

  ::placeholder { color: var(--text-dim); opacity: .7; }

  /* Modals */
  .modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(20,20,14,0.45);
    z-index: 100;
    align-items: flex-end;
    justify-content: center;
    padding: 0;
  }
  .modal-overlay.show { display: flex; }
  .modal-card {
    background: var(--surface);
    border-radius: 18px 18px 0 0;
    padding: 20px 18px calc(20px + env(safe-area-inset-bottom, 0px));
    width: 100%;
    max-width: 480px;
    box-shadow: var(--shadow);
    animation: slideUp .18s ease;
  }
  @media (min-width: 520px) {
    .modal-overlay { align-items: center; }
    .modal-card { border-radius: 18px; }
  }
  @keyframes slideUp { from { transform: translateY(24px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
  .modal-card h3 { margin: 0 0 4px; font-size: 17px; }
  .modal-card .sub { color: var(--text-dim); font-size: 13px; margin-bottom: 14px; }
  .payment-card { max-width: 420px; }
  .payment-choice-row { display: flex; gap: 10px; }
  .payment-choice { flex: 1; margin-top: 4px !important; }
  .payment-cancel { width: 100%; margin-top: 10px; }
  .upi-payment-panel { display: none; }
  .upi-id-label { display: block; margin-bottom: 5px; font-size: 12px; font-weight: 600; color: var(--text-dim); }
  #merchantUpiId {
    width: 100%; padding: 10px 12px; border: 1px solid var(--border);
    border-radius: 9px; background: var(--surface-2); color: var(--text); font-size: 14px;
  }
  .upi-qr-wrap {
    min-height: 210px; display: flex; flex-direction: column; align-items: center;
    justify-content: center; margin: 12px 0; padding: 10px;
    border: 1px solid var(--border); border-radius: 10px; background: #fff; color: #444;
  }
  #upiQrCode { display: none; width: 190px; height: 190px; object-fit: contain; }
  #upiQrHint { max-width: 240px; text-align: center; font-size: 12px; }
  .upi-instruction { text-align: center; }
  .receipt-payment { margin: -6px 0 12px; font-size: 13px; color: var(--text-dim); }
  .weight-input-row {
    display: flex; align-items: center; gap: 10px; margin-bottom: 16px;
  }
  .weight-input-row input {
    flex: 1;
    font-size: 26px;
    font-weight: 700;
    padding: 12px 14px;
    border-radius: 12px;
    border: 1.5px solid var(--border);
    background: var(--surface-2);
    color: var(--text);
    text-align: center;
  }
  .weight-input-row input:focus { outline: none; border-color: var(--accent); }
  .weight-input-row .wunit { font-size: 14px; color: var(--text-dim); font-weight: 600; min-width: 44px; }
  .quick-qty { display: flex; gap: 7px; margin-bottom: 16px; flex-wrap: wrap; }
  .quick-qty button {
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--text);
    border-radius: 9px;
    padding: 7px 12px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
  }
  .modal-btn-row { display: flex; gap: 10px; }
  .modal-btn-row button.secondary { flex: 1; margin: 0; }
  .modal-btn-row button.primary { flex: 2; margin: 0; }

  #printArea { display: none; }
  @media print {
    @page { size: 80mm auto; margin: 3mm; }
    body * { visibility: hidden; }
    #printArea, #printArea * { visibility: visible; }
    #printArea {
      display: block !important;
      position: absolute; top: 0; left: 0; width: 74mm;
      padding: 2mm; color: #000; background: #fff; font-family: 'Courier New', monospace;
    }
    #printArea h2 { text-align: center; margin: 0 0 2px; font-size: 15px; }
    #printArea .p-meta { text-align: center; font-size: 10.5px; margin-bottom: 8px; }
    #printArea table { width: 100%; border-collapse: collapse; font-size: 11.5px; }
    #printArea th, #printArea td { text-align: left; padding: 2px 0; }
    #printArea th:last-child, #printArea td:last-child { text-align: right; }
    #printArea .p-total-row td { border-top: 1px dashed #000; font-weight: 700; padding-top: 5px; }
    #printArea .p-thanks { text-align: center; margin-top: 10px; font-size: 11px; }
  }

  .brand-badge {
    font-size: 10.5px;
    font-weight: 700;
    color: var(--accent);
    background: var(--accent-dim);
    padding: 2px 7px;
    border-radius: 999px;
    letter-spacing: .04em;
    vertical-align: 2px;
    margin-left: 2px;
  }
  .header-tools { display: flex; align-items: center; gap: 8px; }
  .lock-btn {
    display: none;
    background: none;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 4px 9px;
    font-size: 12px;
    color: var(--text-dim);
    cursor: pointer;
  }
  .db-status { font-size: 11px; color: var(--text-dim); }
  .billing-search {
    width: 100%;
    padding: 10px 12px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--text);
    font-size: 14px;
    margin-bottom: 12px;
  }
  .billing-categories { display: flex; flex-wrap: wrap; gap: 7px; margin-bottom: 14px; }
  .category-btn {
    border: 1px solid var(--border);
    border-radius: 9px;
    padding: 7px 11px;
    background: var(--surface-2);
    color: var(--text-dim);
    font-size: 12.5px;
    font-weight: 600;
    cursor: pointer;
  }
  .category-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }
  .billing-category-group + .billing-category-group { margin-top: 18px; }
  .billing-category-group h3 { margin: 0 0 9px; font-size: 13px; color: var(--text-dim); }
  #billingEmptyHint, #billingNoMatchHint, #itemsEmptyHint,
  #topItemsEmptyHint, #recentSalesEmptyHint, #wasteTopEmptyHint,
  #wasteRecentEmptyHint { display: none; }
  .item-section-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2px;
  }
  .item-section-title { margin: 0 !important; }
  .change-pin-btn { padding: 6px 11px; font-size: 12px; }
  .item-form-start { margin-top: 12px; }
  .waste-value, .waste-rank-value, .waste-total { color: var(--danger) !important; }
  .pin-input { text-align: center; letter-spacing: 6px; font-size: 20px; }
  #pinInput2Row, .pin-error, #wasteReasonRow { display: none; }
  .pin-error { color: var(--danger); font-size: 12.5px; margin-bottom: 10px; }
  .waste-reason-input { font-size: 13.5px; font-weight: 500; text-align: left; }
  .receipt-preview { max-height: 40vh; overflow-y: auto; margin-bottom: 14px; }
  .receipt-table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
  .receipt-table-head { color: var(--text-dim); text-align: left; }
  .receipt-table th, .receipt-row td { padding: 4px 0; }
  .receipt-row { border-top: 1px solid var(--border); }
  .receipt-row td { padding-top: 6px; padding-bottom: 6px; }
  .receipt-amount { text-align: right; }
  .receipt-total { margin-top: 8px; }
  .past-sale-print { margin-top: 6px; padding: 5px 10px; font-size: 11.5px; }
