@import url('https://fonts.googleapis.com/css2?family=Cairo:wght@400;600;700;800&display=swap');

:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --text: #1c1f26;
  --muted: #6b7280;
  --border: #e5e7eb;
  --primary: #0f766e;
  --primary-light: #ccfbf1;
  --primary-dark: #0b544e;
  --debt: #dc2626;
  --debt-bg: #fef2f2;
  --debt-border: #fecaca;
  --payment: #16a34a;
  --payment-bg: #f0fdf4;
  --payment-border: #bbf7d0;
  --wa: #16a34a;
  --wa-bg: #e7f9ee;
  --shadow-sm: 0 1px 2px rgba(20, 20, 30, 0.04), 0 1px 1px rgba(20, 20, 30, 0.03);
  --shadow-md: 0 6px 20px rgba(20, 20, 30, 0.08);
  --overlay: rgba(15, 17, 21, 0.5);
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --card: #1d2025;
    --text: #f3f4f6;
    --muted: #9199a6;
    --border: #2c3038;
    --primary: #2dd4bf;
    --primary-light: #0f3d38;
    --primary-dark: #5eead4;
    --debt: #f87171;
    --debt-bg: #3a1618;
    --debt-border: #5c2226;
    --payment: #4ade80;
    --payment-bg: #12301c;
    --payment-border: #1e4a2a;
    --wa: #4ade80;
    --wa-bg: #12301c;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 10px 26px rgba(0, 0, 0, 0.45);
    --overlay: rgba(0, 0, 0, 0.65);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'Cairo', Tahoma, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body { min-height: 100vh; }

.hidden { display: none !important; }

.screen {
  display: none;
  min-height: 100vh;
  max-width: 480px;
  margin: 0 auto;
  padding-bottom: 90px;
}

.screen.active { display: block; animation: screen-in 0.28s ease both; }

@keyframes screen-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

@media (prefers-reduced-motion: reduce) {
  .screen.active { animation: none; }
}

input, button { font-family: inherit; font-size: 16px; }

label {
  display: block;
  margin: 14px 0 6px;
  font-size: 14px;
  color: var(--muted);
  font-weight: 600;
}

input[type="text"], input[type="tel"], input[type="password"], input[type="number"], input[type="search"] {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--card);
  color: var(--text);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 20%, transparent); }

.btn {
  border: none;
  border-radius: 10px;
  padding: 13px 16px;
  font-weight: 700;
  cursor: pointer;
  width: 100%;
  transition: transform 0.12s ease, opacity 0.12s ease, background 0.12s ease;
  position: relative;
}

.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.65; cursor: default; transform: none; }

.btn.primary { background: var(--primary); color: #fff; }
.btn.primary:active:not(:disabled) { background: var(--primary-dark); }
.btn.ghost { background: var(--border); color: var(--text); }
.btn.outline { background: var(--card); color: var(--text); border: 1px solid var(--border); }
.btn.debt { background: var(--debt-bg); color: var(--debt); border: 1px solid var(--debt-border); }
.btn.payment { background: var(--payment-bg); color: var(--payment); border: 1px solid var(--payment-border); }
.btn.danger { background: var(--debt); color: #fff; }

@media (prefers-color-scheme: dark) {
  .btn.primary { color: #04140f; }
}

.btn .spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid currentColor;
  border-left-color: transparent;
  border-radius: 50%;
  vertical-align: -3px;
  margin-inline-end: 8px;
  animation: spin 0.7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ===== شاشة الدخول ===== */
.auth-box {
  padding: 40px 24px;
}

.brand-mark {
  display: block;
  margin: 0 auto 8px;
}

.brand-mark-bg { fill: var(--primary); }
.brand-mark-check { stroke: #fff; }

@media (prefers-color-scheme: dark) {
  .brand-mark-check { stroke: #04140f; }
}

.brand {
  text-align: center;
  font-size: 40px;
  margin: 0;
  color: var(--primary);
  font-weight: 800;
}

.tagline {
  text-align: center;
  color: var(--muted);
  margin: 4px 0 28px;
}

.tabs {
  display: flex;
  background: var(--border);
  border-radius: 10px;
  padding: 4px;
  margin-bottom: 20px;
}

.tab {
  flex: 1;
  border: none;
  background: transparent;
  padding: 10px;
  border-radius: 8px;
  font-weight: 700;
  color: var(--muted);
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.tab.active { background: var(--card); color: var(--primary); box-shadow: var(--shadow-sm); }

.tab-panel { display: none; }
.tab-panel.active { display: block; animation: fade-in 0.2s ease both; }

@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

.error {
  color: var(--debt);
  font-size: 14px;
  min-height: 18px;
  margin: 8px 0 0;
}

/* ===== الهيدر ===== */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 18px 16px 14px;
}

.shop-name { font-weight: 800; font-size: 18px; }
.total-due { color: var(--muted); font-size: 14px; margin-top: 2px; }
.total-due b { color: var(--debt); font-size: 16px; transition: color 0.2s ease; }
.total-due b.all-clear { color: var(--payment); }

.icon-btn {
  background: var(--card);
  border: 1px solid var(--border);
  width: 40px;
  height: 40px;
  border-radius: 50%;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  box-shadow: var(--shadow-sm);
  transition: transform 0.12s ease;
}

.icon-btn:active { transform: scale(0.92); }

.search-bar { padding: 0 16px 12px; }

/* ===== شريط الأوفلاين ===== */
.offline-banner {
  margin: 0 16px 12px;
  background: #fffbeb;
  color: #92400e;
  border: 1px solid #fde68a;
  border-radius: 10px;
  padding: 9px 14px;
  font-size: 13px;
  text-align: center;
  font-weight: 600;
}

@media (prefers-color-scheme: dark) {
  .offline-banner { background: #3a2e0e; color: #fbbf24; border-color: #5c4a12; }
}

/* ===== قائمة العملاء ===== */
.customers-list {
  list-style: none;
  margin: 0;
  padding: 0 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.customer-row {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: transform 0.12s ease, box-shadow 0.12s ease;
  animation: row-in 0.25s ease both;
}

@keyframes row-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.customer-row:active { transform: scale(0.99); }

.avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 15px;
  flex-shrink: 0;
}

.customer-row .info { flex: 1; min-width: 0; }
.customer-row .cname { font-weight: 700; font-size: 16px; }
.customer-row .cmeta { color: var(--muted); font-size: 12.5px; margin-top: 2px; display: flex; gap: 6px; align-items: center; }
.customer-row .cmeta .dot { opacity: 0.6; }

.customer-row .balance-wrap { text-align: left; }

.customer-row .balance {
  font-weight: 800;
  font-size: 16px;
  white-space: nowrap;
}

.customer-row .balance.owes { color: var(--debt); }
.customer-row .balance.settled { color: var(--payment); }

.wa-quick-btn {
  border: none;
  background: var(--wa-bg);
  color: var(--wa);
  width: 38px;
  height: 38px;
  border-radius: 50%;
  font-size: 16px;
  flex-shrink: 0;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.12s ease;
}

.wa-quick-btn:active { transform: scale(0.9); }

.empty-state {
  text-align: center;
  color: var(--muted);
  margin-top: 50px;
  padding: 0 24px;
}

.empty-state svg { opacity: 0.5; margin-bottom: 10px; }
.empty-state p { margin: 0; }

.fab {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 60px;
  border-radius: 50%;
  border: none;
  background: var(--primary);
  color: #fff;
  font-size: 30px;
  box-shadow: 0 6px 18px rgba(15, 118, 110, 0.4);
  cursor: pointer;
  max-width: 480px;
  transition: transform 0.15s ease;
}

.fab:active { transform: translateX(-50%) scale(0.92); }

body.modal-open .fab { display: none; }

@media (prefers-color-scheme: dark) {
  .fab { color: #04140f; }
}

/* ===== شاشة العميل ===== */
.customer-header-info { flex: 1; text-align: center; min-width: 0; }
.customer-name { font-weight: 800; font-size: 18px; }
.customer-phone { color: var(--muted); font-size: 13px; }

.balance-card {
  margin: 4px 16px 18px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  text-align: center;
  box-shadow: var(--shadow-sm);
  transition: box-shadow 0.3s ease;
}

.balance-card.celebrate { animation: celebrate-pulse 0.7s ease; }

@keyframes celebrate-pulse {
  0% { box-shadow: var(--shadow-sm); }
  40% { box-shadow: 0 0 0 8px var(--payment-bg); }
  100% { box-shadow: var(--shadow-sm); }
}

.balance-label { color: var(--muted); font-size: 14px; }
.balance-amount { font-size: 42px; font-weight: 800; margin: 4px 0; }
.balance-amount.owes { color: var(--debt); }
.balance-amount.settled { color: var(--payment); }
.balance-sub { color: var(--muted); font-size: 13px; }

.action-row {
  display: flex;
  gap: 10px;
  padding: 0 16px 10px;
}

.action-row.secondary .btn { font-size: 14px; padding: 11px; }

.section-title {
  padding: 8px 16px 4px;
  font-size: 15px;
  color: var(--muted);
}

.entries-list {
  list-style: none;
  margin: 0;
  padding: 0 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.entry-row {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  box-shadow: var(--shadow-sm);
  animation: row-in 0.25s ease both;
}

.entry-row.voided { opacity: 0.5; }
.entry-row.pending { border-style: dashed; opacity: 0.85; }

.pending-badge {
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
}
.entry-row .edesc { font-size: 13px; color: var(--muted); margin-top: 2px; }
.entry-row .edate { font-size: 12px; color: var(--muted); margin-top: 2px; }
.entry-row .eamount { font-weight: 800; font-size: 15px; }
.entry-row .eamount.debt { color: var(--debt); }
.entry-row .eamount.payment { color: var(--payment); }

.void-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  text-decoration: underline;
  cursor: pointer;
  padding: 4px;
}

/* ===== النوافذ المنبثقة ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: var(--overlay);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 50;
  animation: overlay-in 0.18s ease both;
}

@keyframes overlay-in { from { opacity: 0; } to { opacity: 1; } }

.modal-box {
  background: var(--card);
  width: 100%;
  max-width: 480px;
  border-radius: 18px 18px 0 0;
  padding: 20px 20px 28px;
  animation: sheet-in 0.22s cubic-bezier(0.22, 1, 0.36, 1) both;
}

@keyframes sheet-in {
  from { opacity: 0; transform: translateY(24px); }
  to { opacity: 1; transform: translateY(0); }
}

@media (prefers-reduced-motion: reduce) {
  .modal-overlay, .modal-box, .screen.active, .customer-row, .entry-row { animation: none; }
}

.modal-box h3 { margin: 0 0 4px; }
.modal-box p.modal-message { color: var(--muted); margin: 8px 0 0; line-height: 1.6; }

.modal-actions {
  display: flex;
  gap: 10px;
  margin-top: 20px;
}

.modal-secondary-action { margin-top: 10px; }

#share-link-input { direction: ltr; text-align: right; font-size: 14px; color: var(--muted); }

@media (min-width: 480px) {
  .modal-overlay { align-items: center; }
  .modal-box { border-radius: 18px; animation: dialog-in 0.18s ease both; }
}

@keyframes dialog-in {
  from { opacity: 0; transform: scale(0.96); }
  to { opacity: 1; transform: scale(1); }
}

/* ===== توست ===== */
.toast-container {
  position: fixed;
  top: max(16px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 200;
  width: calc(100% - 32px);
  max-width: 420px;
  pointer-events: none;
}

.toast {
  background: var(--text);
  color: var(--bg);
  padding: 12px 16px;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 600;
  text-align: center;
  box-shadow: var(--shadow-md);
  animation: toast-in 0.25s cubic-bezier(0.22, 1, 0.36, 1) both;
}

.toast.leaving { animation: toast-out 0.2s ease both; }

.toast.success { background: var(--payment); color: #08260f; }
.toast.error { background: var(--debt); color: #fff; }

@keyframes toast-in {
  from { opacity: 0; transform: translateY(-10px) scale(0.95); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes toast-out {
  to { opacity: 0; transform: translateY(-6px) scale(0.97); }
}
