/* ===== Variables ===== */
:root {
  --primary: #1a5f4a;
  --primary-light: #2d8a6e;
  --accent: #e8a317;
  --bg: #f0f4f2;
  --card: #fff;
  --text: #1a1a1a;
  --muted: #6b7280;
  --border: #e5e7eb;
  --success: #059669;
  --danger: #dc2626;
  --admin: #1e3a5f;
  --sales: #7c3aed;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}
body.has-bottom { padding-bottom: calc(80px + var(--safe-bottom)); }

.screen { display: none; min-height: 100vh; }
.screen.active { display: block; }

/* ===== Login ===== */
.login-wrap {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  background: linear-gradient(160deg, #1a5f4a 0%, #0f3d30 50%, #1e3a5f 100%);
}
.login-card {
  background: white;
  border-radius: 16px;
  padding: 1.75rem 1.5rem;
  width: 100%;
  max-width: 380px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.3);
}
.login-card h1 {
  text-align: center;
  font-size: 1.3rem;
  color: var(--primary);
  margin-bottom: 0.3rem;
}
.login-card .sub {
  text-align: center;
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: 1.25rem;
}
.role-tabs { display: flex; gap: 0.5rem; margin-bottom: 1.25rem; }
.role-tab {
  flex: 1;
  padding: 0.7rem;
  border: 2px solid var(--border);
  border-radius: 10px;
  background: #f9fafb;
  font-weight: 700;
  font-size: 0.9rem;
  cursor: pointer;
  text-align: center;
}
.role-tab.active-admin {
  border-color: var(--admin);
  background: #eff6ff;
  color: var(--admin);
}
.role-tab.active-sales {
  border-color: var(--sales);
  background: #f5f3ff;
  color: var(--sales);
}
.login-card label {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--muted);
  display: block;
  margin-bottom: 0.3rem;
}
.login-card input,
.login-card select {
  width: 100%;
  padding: 0.75rem;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  font-size: 1rem;
  margin-bottom: 0.9rem;
}
.login-card input:focus,
.login-card select:focus {
  outline: none;
  border-color: var(--primary);
}
.btn-login {
  width: 100%;
  padding: 0.85rem;
  border: none;
  border-radius: 10px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  color: white;
  min-height: 48px;
}
.btn-login.admin { background: var(--admin); }
.btn-login.sales { background: var(--sales); }

/* ===== Header ===== */
.header {
  color: white;
  padding: 0.7rem 1rem;
  position: sticky;
  top: 0;
  z-index: 50;
  box-shadow: 0 2px 10px rgba(0,0,0,0.18);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.header.admin-h { background: linear-gradient(135deg, var(--admin), #2a5080); }
.header.sales-h { background: linear-gradient(135deg, var(--sales), #6d28d9); }
.header .title-area { flex: 1; text-align: center; }
.header h1 { font-size: 1.1rem; font-weight: 700; }
.header p { font-size: 0.68rem; opacity: 0.9; }
.header .icon-btn {
  background: rgba(255,255,255,0.2);
  border: none;
  color: white;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  font-size: 1.1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.container { max-width: 640px; margin: 0 auto; padding: 0.75rem; }

/* ===== Card ===== */
.card {
  background: var(--card);
  border-radius: 12px;
  padding: 1rem;
  margin-bottom: 0.75rem;
  box-shadow: 0 1px 4px rgba(0,0,0,0.07);
  border: 1px solid var(--border);
}
.card-title {
  font-size: 0.92rem;
  font-weight: 700;
  color: var(--primary);
  margin-bottom: 0.8rem;
  padding-bottom: 0.35rem;
  border-bottom: 2px solid var(--primary);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.card-title.admin-t { color: var(--admin); border-color: var(--admin); }
.card-title.sales-t { color: var(--sales); border-color: var(--sales); }

/* ===== Form ===== */
.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.7rem;
  margin-bottom: 0.7rem;
}
@media (max-width: 420px) {
  .form-row { grid-template-columns: 1fr; }
}
.form-group { display: flex; flex-direction: column; gap: 0.25rem; }
.form-group label {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--muted);
}
.form-group input,
.form-group select {
  padding: 0.6rem 0.7rem;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  background: #fff;
}
.form-group input:focus { outline: none; border-color: var(--primary); }

/* ===== Buttons ===== */
.btn {
  padding: 0.65rem 1rem;
  border: none;
  border-radius: 10px;
  font-size: 0.88rem;
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  touch-action: manipulation;
  min-height: 44px;
}
.btn-primary { background: var(--primary); color: white; }
.btn-accent { background: var(--accent); color: #1a1a1a; }
.btn-outline { background: white; border: 1.5px solid var(--primary); color: var(--primary); }
.btn-danger { background: #fef2f2; color: var(--danger); border: 1.5px solid #fecaca; }
.btn-admin { background: var(--admin); color: white; }
.btn-sales { background: var(--sales); color: white; }
.btn-block { width: 100%; }
.btn-sm { padding: 0.35rem 0.65rem; font-size: 0.78rem; min-height: 34px; }

/* ===== Products (Sales) ===== */
.product-list { display: flex; flex-direction: column; gap: 0.55rem; }
.product-item {
  background: #f8faf9;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  padding: 0.65rem 0.7rem;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.3rem 0.5rem;
  align-items: center;
}
.product-item.has-qty { border-color: #a7f3d0; background: #f0fdf4; }
.product-item .pname { font-weight: 700; font-size: 0.92rem; }
.product-item .price-tag { font-size: 0.78rem; color: var(--muted); }
.product-item .barcode-tag { font-size: 0.65rem; color: var(--muted); font-family: monospace; }
.product-item .fields {
  grid-column: 1 / -1;
  display: flex;
  gap: 0.5rem;
  align-items: center;
}
.line-total {
  font-weight: 800;
  color: var(--primary);
  font-size: 0.95rem;
  min-width: 65px;
  text-align: right;
}
.qty-stepper {
  display: flex;
  align-items: center;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: white;
}
.qty-stepper button {
  width: 36px;
  height: 36px;
  border: none;
  background: #f3f4f6;
  font-size: 1.2rem;
  font-weight: 700;
  cursor: pointer;
  color: var(--primary);
}
.qty-stepper input {
  width: 48px;
  border: none;
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
  height: 36px;
  text-align: center;
  font-weight: 700;
  font-size: 0.95rem;
  padding: 0;
}

/* ===== Totals ===== */
.totals-box {
  background: linear-gradient(135deg, #f0fdf4, #ecfdf5);
  border: 1.5px solid #a7f3d0;
  border-radius: 10px;
  padding: 0.8rem 1rem;
}
.total-row {
  display: flex;
  justify-content: space-between;
  padding: 0.2rem 0;
  font-size: 0.88rem;
}
.total-row.grand {
  font-size: 1.25rem;
  font-weight: 800;
  color: var(--primary);
  border-top: 2px solid var(--primary);
  margin-top: 0.35rem;
  padding-top: 0.5rem;
}

/* ===== Bottom bar ===== */
.bottom-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: white;
  border-top: 1px solid var(--border);
  padding: 0.55rem 0.7rem calc(0.55rem + var(--safe-bottom));
  display: flex;
  gap: 0.45rem;
  z-index: 100;
  box-shadow: 0 -4px 16px rgba(0,0,0,0.1);
}
.bottom-bar .btn { flex: 1; font-size: 0.82rem; padding: 0.65rem 0.3rem; }

/* ===== Scanner ===== */
.scan-section { text-align: center; }
#reader {
  width: 100%;
  max-width: 400px;
  margin: 0.5rem auto 0;
  border-radius: 12px;
  overflow: hidden;
  display: none;
}
.scan-status {
  font-size: 0.82rem;
  color: var(--muted);
  margin-top: 0.4rem;
  min-height: 1.1em;
}
.scan-status.success { color: var(--success); font-weight: 600; }
.scan-status.error { color: var(--danger); }
.manual-barcode { display: flex; gap: 0.45rem; margin-top: 0.65rem; }
.manual-barcode input {
  flex: 1;
  padding: 0.6rem;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  font-size: 0.95rem;
  font-family: monospace;
}
.btn-scan {
  background: #1e3a5f;
  color: white;
  width: 100%;
  font-size: 0.95rem;
  padding: 0.8rem;
}
.btn-scan.active { background: var(--danger); }

.hint { font-size: 0.7rem; color: var(--muted); margin-top: 0.35rem; }

/* ===== Toast ===== */
.toast {
  position: fixed;
  bottom: calc(80px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--primary);
  color: white;
  padding: 0.65rem 1.2rem;
  border-radius: 10px;
  font-size: 0.85rem;
  font-weight: 600;
  opacity: 0;
  transition: all 0.25s;
  z-index: 200;
  box-shadow: 0 4px 16px rgba(0,0,0,0.25);
  max-width: 90%;
  text-align: center;
  pointer-events: none;
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ===== Admin product ===== */
.admin-product {
  background: #f8fafc;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  padding: 0.8rem;
  margin-bottom: 0.55rem;
}
.admin-product .row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.45rem;
  margin-bottom: 0.45rem;
}
@media (max-width: 400px) {
  .admin-product .row { grid-template-columns: 1fr; }
}
.admin-product .row-full { grid-column: 1 / -1; }
.admin-product label {
  font-size: 0.68rem;
  font-weight: 600;
  color: var(--muted);
  display: block;
  margin-bottom: 0.15rem;
}
.admin-product input {
  width: 100%;
  padding: 0.45rem 0.55rem;
  border: 1.5px solid var(--border);
  border-radius: 6px;
  font-size: 0.92rem;
}
.admin-product .actions {
  display: flex;
  gap: 0.35rem;
  margin-top: 0.4rem;
  justify-content: flex-end;
}

/* ===== User card ===== */
.user-card {
  background: #f5f3ff;
  border: 1.5px solid #ddd6fe;
  border-radius: 10px;
  padding: 0.8rem;
  margin-bottom: 0.55rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
}
.user-card .uname { font-weight: 700; font-size: 0.95rem; color: var(--sales); }
.user-card .umeta { font-size: 0.75rem; color: var(--muted); }
.user-card .ustats {
  background: white;
  border-radius: 8px;
  padding: 0.35rem 0.6rem;
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--admin);
  text-align: center;
}

/* ===== History ===== */
.hist-item {
  background: #f8faf9;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.75rem;
  margin-bottom: 0.5rem;
  cursor: pointer;
}
.hist-item:active { background: #f0fdf4; }
.hist-item .h-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 0.5rem;
}
.hist-item .h-no { font-weight: 700; color: var(--primary); font-size: 0.9rem; }
.hist-item .h-amt { font-weight: 800; color: var(--primary); }
.hist-item .h-meta { font-size: 0.75rem; color: var(--muted); margin-top: 0.25rem; }
.hist-item .h-items { font-size: 0.78rem; color: #374151; margin-top: 0.35rem; }

/* ===== Stats ===== */
.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}
.stat-box {
  background: linear-gradient(135deg, #eff6ff, #dbeafe);
  border: 1.5px solid #93c5fd;
  border-radius: 10px;
  padding: 0.75rem;
  text-align: center;
}
.stat-box .s-val { font-size: 1.4rem; font-weight: 800; color: var(--admin); }
.stat-box .s-lbl { font-size: 0.72rem; color: var(--muted); margin-top: 0.15rem; }
.stat-box.green {
  background: linear-gradient(135deg, #f0fdf4, #dcfce7);
  border-color: #86efac;
}
.stat-box.green .s-val { color: var(--primary); }

/* ===== Tabs ===== */
.tabs-row {
  display: flex;
  gap: 0.35rem;
  margin-bottom: 0.75rem;
  flex-wrap: wrap;
}
.tab-btn {
  padding: 0.45rem 0.75rem;
  border-radius: 20px;
  border: 1.5px solid var(--border);
  background: white;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  color: var(--muted);
}
.tab-btn.active {
  background: var(--admin);
  color: white;
  border-color: var(--admin);
}

.empty-hint {
  text-align: center;
  color: var(--muted);
  padding: 1.5rem;
  font-size: 0.88rem;
}
.user-badge {
  display: inline-block;
  background: rgba(255,255,255,0.25);
  padding: 0.15rem 0.5rem;
  border-radius: 6px;
  font-size: 0.7rem;
  margin-top: 0.15rem;
}
.sec-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  margin-top: 0.7rem;
}
.sec-actions .btn { flex: 1; min-width: 95px; font-size: 0.8rem; }

/* ===== Memo print ===== */
#memo-preview { display: none; }
.memo-paper {
  background: white;
  padding: 1.25rem;
  max-width: 700px;
  margin: 0.75rem auto;
  font-size: 0.88rem;
  border: 1px solid #ddd;
  border-radius: 4px;
}
.memo-header {
  text-align: center;
  border-bottom: 2px solid var(--primary);
  padding-bottom: 0.6rem;
  margin-bottom: 0.85rem;
}
.memo-header h2 { color: var(--primary); font-size: 1.3rem; }
.memo-meta {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 0.85rem;
  font-size: 0.85rem;
}
.memo-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 0.75rem;
}
.memo-table th,
.memo-table td {
  border: 1px solid #ccc;
  padding: 0.4rem 0.5rem;
  text-align: left;
}
.memo-table th {
  background: #f0fdf4;
  color: var(--primary);
  font-weight: 600;
}
.memo-table .num { text-align: right; }
.memo-table .center { text-align: center; }
.memo-totals { text-align: right; }
.memo-totals .grand-total {
  font-size: 1.15rem;
  font-weight: 800;
  color: var(--primary);
}
.memo-footer {
  margin-top: 1.25rem;
  padding-top: 0.6rem;
  border-top: 1px dashed #ccc;
  display: flex;
  justify-content: space-between;
  font-size: 0.8rem;
  color: var(--muted);
}

@media print {
  body * { visibility: hidden; }
  #memo-preview,
  #memo-preview * { visibility: visible; }
  #memo-preview {
    display: block !important;
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
  }
  .no-print,
  .bottom-bar,
  .header,
  .toast { display: none !important; }
  .memo-paper { border: none; box-shadow: none; padding: 0; }
}
