:root {
  color-scheme: light;
  --green: #0c6b45;
  --green-dark: #084e32;
  --green-soft: #e7f6ef;
  --ink: #172033;
  --muted: #5d6b7c;
  --line: #e4e9ef;
  --bg: #eef2f5;
  --card: #ffffff;
  --amber: #8a5a00;
  --amber-bg: #fff6e5;
  --danger: #b42318;
  --danger-bg: #fdeceb;
  --shadow: 0 12px 32px rgba(23, 32, 51, 0.06);
  --radius: 16px;
  font-family: "Cairo", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--ink); }
body { min-height: 100vh; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
img { max-width: 100%; }

.app { min-height: 100vh; }
.side {
  display: none;
  background: var(--green-dark);
  color: #fff;
  padding: 22px 16px;
}
.brand { display: flex; gap: 10px; align-items: center; margin-bottom: 22px; }
.brand-mark {
  width: 42px; height: 42px; border-radius: 12px; background: #fff;
  color: var(--green); display: grid; place-items: center; font-weight: 800;
}
.brand h1 { margin: 0; font-size: 18px; }
.brand p { margin: 0; color: #d5eee4; font-size: 13px; }
.nav { display: flex; flex-direction: column; gap: 6px; }
.nav button, .tabbar button, .btn, .btn-line, .btn-danger {
  border: 0; border-radius: 12px; min-height: 44px; padding: 8px 14px;
}
.nav button { background: transparent; color: #fff; text-align: right; font-weight: 600; }
.nav button.active { background: rgba(255,255,255,.14); }
.main { padding: 16px 16px 96px; }
.top { display: flex; justify-content: space-between; gap: 12px; align-items: center; margin-bottom: 14px; }
.top h2 { margin: 0; font-size: 22px; }
.who { color: var(--muted); font-size: 14px; }
.trial { background: var(--amber-bg); color: var(--amber); border-radius: 999px; padding: 4px 10px; font-size: 13px; font-weight: 700; }

.card, .ticket, .stat, .modal-card, .auth-card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
}
.card { padding: 16px; margin-bottom: 14px; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 14px; }
.stat { padding: 12px; text-align: right; border: 1px solid transparent; }
.stat span, .muted { color: var(--muted); }
.stat strong { display: block; font-size: 18px; margin-top: 4px; }
.stat em { font-style: normal; color: var(--danger); font-size: 12px; font-weight: 700; }
.stat.warn { background: var(--amber-bg); }

.sale-layout { display: grid; gap: 14px; }
.sale-layout > * { min-width: 0; }
.scan-row { display: flex; gap: 8px; }
.scan-row input, .field input, .field select, .field textarea, .line input {
  width: 100%; border: 1px solid var(--line); border-radius: 12px; min-height: 48px; padding: 8px 12px; background: #fff;
}
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; font-size: 14px; }
.field textarea { min-height: 80px; }
.num { direction: ltr; text-align: right; font-variant-numeric: tabular-nums; }
.btn { background: var(--green); color: #fff; font-weight: 700; }
.btn:disabled { opacity: .5; cursor: default; }
.btn-line { background: #fff; border: 1px solid var(--line) !important; }
.btn-danger { background: var(--danger-bg); color: var(--danger); }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-row > * { flex: 1; }
.keys { display: none; color: var(--muted); font-size: 13px; margin-top: 8px; }

.search-results { display: flex; flex-direction: column; gap: 6px; margin: 8px 0; }
.search-results button, .list-btn {
  display: flex; justify-content: space-between; gap: 8px; width: 100%;
  background: #fff; border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; text-align: right;
}
.cart-line {
  display: grid; grid-template-columns: minmax(0, 1fr); gap: 8px;
  padding: 12px 0; border-bottom: 1px solid var(--line);
}
.line-name { font-weight: 700; }
.line-controls { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto; gap: 8px; align-items: end; min-width: 0; }
.line-controls .btn-danger { white-space: nowrap; }
.line label { font-size: 12px; color: var(--muted); display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.line-total { font-weight: 800; }
.ticket { padding: 16px; }
.ticket-meta { display: grid; gap: 0; }
.ticket-secondary { margin-bottom: 8px; }
.paybar .btn { width: 100%; margin-top: 8px; }
.total-due { font-size: 32px; margin: 4px 0 8px; font-variant-numeric: tabular-nums; }
.row-between { display: flex; justify-content: space-between; gap: 8px; margin: 4px 0; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 8px; border-bottom: 1px solid var(--line); text-align: right; white-space: nowrap; }
th { color: var(--muted); font-size: 13px; font-weight: 600; }
.badge { display: inline-block; border-radius: 999px; padding: 2px 8px; font-size: 12px; font-weight: 700; background: var(--green-soft); color: var(--green-dark); }
.badge.bad { background: var(--danger-bg); color: var(--danger); }
.badge.warn { background: var(--amber-bg); color: var(--amber); }
.low { color: var(--danger); font-weight: 700; }

.auth {
  min-height: 100vh; display: grid; place-items: center; padding: 24px;
}
.auth-card { width: min(440px, 100%); padding: 24px; }
.auth-card h2 { margin: 0 0 8px; }
.cashier-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.cashier-list button.active { border-color: var(--green); background: var(--green-soft); }

.tabbar {
  position: fixed; inset-inline: 0; bottom: 0; display: flex; gap: 2px;
  background: #fff; border-top: 1px solid var(--line); padding: 4px 4px calc(4px + env(safe-area-inset-bottom));
  z-index: 5; overflow: hidden;
}
.tabbar button {
  flex: 1 1 0; min-width: 0; background: transparent; color: var(--muted);
  font-size: 11px; font-weight: 700; line-height: 1.2; padding: 6px 1px; white-space: nowrap;
}
.tabbar button.active { color: var(--green); background: var(--green-soft); }

.modal { position: fixed; inset: 0; background: rgba(23,32,51,.45); display: grid; place-items: end center; z-index: 20; padding: 12px; }
.modal-card { width: min(520px, 100%); padding: 16px; max-height: min(92vh, 760px); overflow: auto; }
.toast {
  position: fixed; inset-inline: 16px; top: 16px; background: var(--ink); color: #fff;
  border-radius: 12px; padding: 12px 14px; z-index: 40; pointer-events: none;
  width: min(420px, calc(100% - 32px)); margin-inline: auto;
}
.empty { color: var(--muted); padding: 18px 0; }
.check { display: flex; align-items: center; gap: 8px; margin: 8px 0; }
#qr-reader { border-radius: 12px; overflow: hidden; }
.logo-preview { width: 72px; height: 72px; object-fit: contain; border-radius: 12px; background: #fff; }

#print-root { display: none; }
.receipt { width: 80mm; color: #111; font-size: 12px; }
.receipt.paper-58 { width: 58mm; }
.receipt h3, .receipt p { margin: 0 0 4px; }
.receipt table { width: 100%; }
.receipt td, .receipt th { border: 0; padding: 2px 0; white-space: normal; text-align: right; }
.statement-print table { width: 100%; border-collapse: collapse; }
.statement-print th, .statement-print td { border-bottom: 1px solid #ccc; padding: 4px 6px; }
.receipt .center { text-align: center; }
.receipt hr { border: 0; border-top: 1px dashed #999; margin: 6px 0; }

@media (min-width: 980px) {
  .app { display: grid; grid-template-columns: 232px 1fr; }
  .side { display: flex; flex-direction: column; min-height: 100vh; position: sticky; top: 0; }
  .tabbar { display: none; }
  .main { padding: 22px 28px 32px; }
  .sale-layout { grid-template-columns: minmax(0, 1fr) 300px; align-items: start; }
  .ticket { position: sticky; top: 18px; }
  .keys { display: block; }
  .modal { place-items: center; }
}
@media (max-width: 979px) {
  .main { padding: 12px 12px 0; }
  .top { margin-bottom: 8px; }
  .top h2 { font-size: 18px; }
  .sale-screen { padding-bottom: calc(168px + env(safe-area-inset-bottom)); }
  .sale-screen .stats { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; margin-bottom: 8px; }
  .sale-screen .stat { padding: 8px 6px; }
  .sale-screen .stat span { font-size: 11px; }
  .sale-screen .stat strong { font-size: 14px; margin-top: 2px; }
  .sale-screen .stat em { display: block; font-size: 10px; }
  .sale-screen .card { padding: 12px; margin-bottom: 0; }
  .sale-screen .ticket {
    position: fixed; z-index: 6; inset-inline: 0; bottom: calc(48px + env(safe-area-inset-bottom));
    margin: 0; border-radius: 16px 16px 0 0; padding: 8px 12px 10px;
    box-shadow: 0 -8px 24px rgba(23, 32, 51, 0.12);
  }
  .sale-screen .ticket-meta { grid-template-columns: 1fr 1fr; gap: 8px; }
  .sale-screen .ticket-meta .field { margin-bottom: 0; }
  .sale-screen .ticket-meta .field input,
  .sale-screen .ticket-meta .field select { min-height: 40px; }
  .sale-screen .ticket-secondary { margin: 8px 0; }
  .sale-screen .ticket-secondary .btn-line { min-height: 36px; padding: 4px 6px; font-size: 13px; }
  .sale-screen .paybar {
    display: flex; align-items: center; justify-content: space-between; gap: 10px;
    position: sticky; bottom: 0; background: #fff;
  }
  .sale-screen .total-due { font-size: 24px; margin: 0; }
  .sale-screen .paybar .btn { width: auto; min-width: 112px; min-height: 48px; margin: 0; font-size: 18px; }
}
@media (max-width: 640px) {
  .stats { grid-template-columns: 1fr; }
  .line-controls { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto; }
}

@media print {
  body * { visibility: hidden; }
  #print-root, #print-root * { visibility: visible; }
  #print-root {
    display: block; position: absolute; top: 0; right: 0; left: 0; width: auto;
    background: #fff; direction: rtl;
  }
  .tabbar, .toast, .modal { display: none !important; }
  .statement-print { width: 100%; max-width: none; }
  .statement-print th, .statement-print td { white-space: normal; }
  @page { margin: 8mm; }
}
