:root {
  color-scheme: dark;
  --bg0: #0f1419;
  --bg1: #15202b;
  --surface: #1c2938;
  --border: #2f4055;
  --text: #e7ecf3;
  --muted: #8b9bb0;
  --accent: #3d8bfd;
  --ok: #2f9e44;
  --warn: #e8590c;
  --danger: #e03131;
  --ready: #2f9e44;
  --sug: #f1c40f;
  --bad: #e03131;
  --page-pad: 14px;
  --content-max: 720px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  overflow-x: hidden;
  background:
    radial-gradient(1200px 500px at 100% -10%, #1b3a5f 0%, transparent 55%),
    radial-gradient(900px 400px at 0% 100%, #163028 0%, transparent 50%),
    var(--bg0);
  color: var(--text);
  font-family: "Segoe UI", Tahoma, sans-serif;
  direction: rtl;
}

/* شريط اسم الموقع — منزّل تحت منطقة الكاميرا / النوتش */
.site-header {
  position: sticky;
  top: 0;
  z-index: 40;
  background: linear-gradient(180deg, #122033 0%, #15202b 100%);
  border-bottom: 1px solid var(--border);
  /* safe-area + مسافة إضافية بعيداً عن الكاميرا */
  padding-top: calc(env(safe-area-inset-top, 0px) + 18px);
  padding-left: max(var(--page-pad), env(safe-area-inset-left, 0px));
  padding-right: max(var(--page-pad), env(safe-area-inset-right, 0px));
  padding-bottom: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
}

.site-header-inner {
  max-width: var(--content-max);
  margin: 0 auto;
  text-align: center;
}

.site-title {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  color: #f0f5fb;
}

.pair-lead {
  text-align: center;
  color: var(--muted);
  margin: 8px 0 16px;
  font-size: 0.95rem;
}

.screen {
  max-width: var(--content-max);
  margin: 0 auto;
  padding: 14px var(--page-pad) 40px;
  width: 100%;
  overflow-x: clip;
}

#tabInvoices,
#tabTransfer {
  width: 100%;
  max-width: 100%;
  min-width: 0;
}

.brand {
  text-align: center;
  padding: 28px 8px 18px;
}

.brand h1 {
  margin: 0;
  font-size: 2rem;
  letter-spacing: 0.02em;
}

.brand p {
  margin: 8px 0 0;
  color: var(--muted);
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px;
  display: grid;
  gap: 10px;
  width: 100%;
  max-width: 100%;
  min-width: 0;
}

label {
  font-size: 0.9rem;
  color: var(--muted);
}

input, .search {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  border: 1px solid var(--border);
  background: var(--bg1);
  color: var(--text);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 1rem;
}

#pinInput {
  font-size: 1.5rem;
  letter-spacing: 0.2em;
  text-align: center;
  font-weight: 700;
}

.hint {
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.5;
  margin-top: 14px;
}

.btn {
  border: none;
  border-radius: 10px;
  padding: 11px 14px;
  font-weight: 700;
  color: #fff;
  cursor: pointer;
  font-size: 0.95rem;
}

.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary { background: var(--accent); }
.btn-ok { background: var(--ok); }
.btn-warn { background: #d4a017; color: #1a1a1a; }
.btn-danger { background: var(--danger); }
.btn-ghost { background: transparent; border: 1px solid var(--border); color: var(--text); }
.btn-watch {
  background: #1a3d5c;
  border: 1px solid #00b4d8;
  color: #7ee8fa;
}
.btn-watch.active {
  background: #00b4d8;
  color: #041820;
  box-shadow: 0 0 0 2px rgba(0, 180, 216, 0.35);
}
.watch-status {
  font-size: 0.85rem;
  color: var(--muted);
  margin: 2px 2px 8px;
}
.watch-status.on {
  color: #7ee8fa;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.conn-status {
  font-size: 0.85rem;
  color: var(--muted);
  margin-top: 4px;
}
.conn-status.ok { color: #69db7c; }
.conn-status.warn { color: #ffc078; }
.conn-status.err { color: #ff8787; }

.tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 12px;
  width: 100%;
  max-width: 100%;
  min-width: 0;
}

.tab {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 999px;
  padding: 10px;
  font-weight: 700;
}
.tab.active {
  color: #fff;
  background: #243447;
  border-color: var(--accent);
}

.stats {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  margin-bottom: 10px;
  width: 100%;
  max-width: 100%;
  min-width: 0;
}
.stat-chip {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 6px;
  text-align: center;
  color: var(--text);
  cursor: pointer;
  min-width: 0;
}
.stat-chip span { display: block; font-size: 1.15rem; font-weight: 800; }
.stat-chip small { color: var(--muted); }
.stat-chip.active {
  outline: 2px solid var(--accent);
  background: #243447;
}
.stat-ready span { color: #51cf66; }
.stat-sug span { color: #ffd43b; }
.stat-bad span { color: #ff6b6b; }
.stat-dup span { color: #ff922b; }

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 10px;
  width: 100%;
  max-width: 100%;
  min-width: 0;
}

.search { margin-bottom: 10px; width: 100%; max-width: 100%; }

.fetch-panel {
  margin-bottom: 12px;
}
.fetch-title {
  font-weight: 800;
  font-size: 1.05rem;
  margin-bottom: 4px;
  color: #d0ebff;
}
.fetch-panel select,
.fetch-panel #fetchChannel {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--bg1);
  color: var(--text);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 1rem;
}
.fetch-dates {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.fetch-dates input {
  padding: 10px 12px;
}
.fetch-actions {
  margin-top: 4px;
  margin-bottom: 0;
}
.progress-error {
  border-color: rgba(224, 49, 49, 0.7);
  color: #ffc9c9;
  background: rgba(224, 49, 49, 0.15);
}

.invoice-list {
  display: grid;
  gap: 12px;
  width: 100%;
  max-width: 100%;
  min-width: 0;
}

.inv-section {
  display: grid;
  gap: 8px;
  width: 100%;
  max-width: 100%;
  min-width: 0;
}
.inv-section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
  color: var(--muted);
  padding: 0 2px;
}
.inv-section-title span {
  background: rgba(255,255,255,0.08);
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 0.75rem;
}
.inv-section-toggle {
  width: 100%;
  max-width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  border: 1px solid var(--border);
  background: linear-gradient(90deg, rgba(47, 158, 68, 0.16), var(--surface) 55%);
  color: var(--text);
  border-radius: 12px;
  padding: 12px 14px;
  cursor: pointer;
  font-size: 0.95rem;
}
.inv-section-toggle .chev {
  color: #69db7c;
  font-size: 1.1rem;
}
.inv-section-body {
  display: grid;
  gap: 10px;
  width: 100%;
  max-width: 100%;
  min-width: 0;
}

.inv-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-inline-start: 4px solid var(--border);
  border-radius: 14px;
  padding: 12px;
  width: 100%;
  max-width: 100%;
  min-width: 0;
  overflow: hidden;
}
.inv-card.collapsed {
  padding-bottom: 10px;
}
.inv-card.st-ready {
  border-inline-start-color: var(--ready);
  background: linear-gradient(90deg, rgba(47, 158, 68, 0.12), var(--surface) 40%);
}
.inv-card.st-sug {
  border-inline-start-color: var(--sug);
  background: linear-gradient(90deg, rgba(241, 196, 15, 0.14), var(--surface) 40%);
}
.inv-card.st-bad {
  border-inline-start-color: var(--bad);
  background: linear-gradient(90deg, rgba(224, 49, 49, 0.14), var(--surface) 40%);
}

.inv-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 0;
}
.inv-card:not(.collapsed) .inv-top {
  margin-bottom: 10px;
}
.inv-head-btn {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  border: none;
  background: transparent;
  color: inherit;
  padding: 0;
  cursor: pointer;
  text-align: right;
}
.inv-head-summary {
  font-size: 0.8rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}
.inv-top-actions {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-shrink: 0;
}
.btn-toggle-card,
.btn-close-held {
  border: 1px solid var(--border);
  background: #243447;
  color: #d0ebff;
  border-radius: 8px;
  padding: 4px 10px;
  font-size: 0.8rem;
  cursor: pointer;
}
.btn-close-held {
  border-color: rgba(47, 158, 68, 0.55);
  color: #b2f2bb;
  background: rgba(47, 158, 68, 0.18);
}

.badge {
  font-size: 0.8rem;
  font-weight: 700;
  border-radius: 999px;
  padding: 4px 10px;
}
.inv-card.st-dup {
  box-shadow: inset 0 0 0 1px rgba(255, 146, 43, 0.35);
}
.badge-dup {
  background: rgba(255, 146, 43, 0.22) !important;
  color: #ff922b !important;
}

.sug-title-ok {
  color: #69db7c !important;
}
.chip-ok {
  border: 1px solid rgba(47, 158, 68, 0.55) !important;
  background: rgba(47, 158, 68, 0.18) !important;
  color: #b2f2bb !important;
}

.btn-del {
  border: 1px solid var(--border);
  background: transparent;
  color: #ff8787;
  border-radius: 8px;
  padding: 4px 10px;
  font-size: 0.8rem;
  cursor: pointer;
}

.inv-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  width: 100%;
  max-width: 100%;
  min-width: 0;
}

.inv-field {
  display: grid;
  gap: 4px;
  margin: 0;
  min-width: 0;
  max-width: 100%;
}
.inv-field > span {
  font-size: 0.75rem;
  color: var(--muted);
}
.inv-field-row {
  display: flex;
  gap: 4px;
  align-items: stretch;
  min-width: 0;
  max-width: 100%;
}
.inv-input {
  flex: 1;
  min-width: 0;
  max-width: 100%;
  border: 1px solid var(--border);
  background: var(--bg1);
  color: var(--text);
  border-radius: 8px;
  padding: 9px 10px;
  font-size: 0.9rem;
}
.inv-field.fld-ok .inv-input { border-color: rgba(47, 158, 68, 0.55); }
.inv-field.fld-sug .inv-input { border-color: rgba(241, 196, 15, 0.75); background: rgba(241, 196, 15, 0.08); }
.inv-field.fld-bad .inv-input { border-color: rgba(224, 49, 49, 0.7); background: rgba(224, 49, 49, 0.08); }
.inv-field[data-field="note"] { grid-column: 1 / -1; }

.btn-sug {
  border: 1px solid var(--border);
  background: #243447;
  color: #ffd43b;
  border-radius: 8px;
  min-width: 36px;
  cursor: pointer;
  font-weight: 700;
}

.sug-inline {
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px dashed var(--border);
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  max-height: 220px;
  overflow-y: auto;
}
.sug-title {
  width: 100%;
  font-size: 0.8rem;
  color: #ffd43b;
  margin-bottom: 2px;
}
.chip {
  border: 1px solid rgba(241, 196, 15, 0.45);
  background: rgba(241, 196, 15, 0.12);
  color: var(--text);
  border-radius: 999px;
  padding: 8px 12px;
  cursor: pointer;
  font-size: 0.9rem;
}

.transfer-panel {
  grid-template-columns: repeat(3, 1fr);
  display: grid;
  margin-bottom: 10px;
}
.metric { text-align: center; }
.metric span { display: block; font-weight: 800; font-size: 1.1rem; }
.metric small { color: var(--muted); }
.metric .ok { color: #69db7c; }
.metric .err { color: #ff8787; }

.progress {
  background: #243447;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 10px;
}

.log {
  background: #0b121a;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  min-height: 220px;
  max-height: 45vh;
  overflow: auto;
  white-space: pre-wrap;
  font-size: 0.85rem;
  color: #c5d0dc;
}

.muted { color: var(--muted); }

.toast {
  position: fixed;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 50;
  background: #243447;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 999px;
  max-width: 90vw;
}
.toast-error {
  background: #4a1820;
  border-color: #a61e4d;
}

@media (max-width: 520px) {
  .inv-grid { grid-template-columns: 1fr; }
  .stats { grid-template-columns: repeat(3, 1fr); }
  .fetch-dates { grid-template-columns: 1fr; }
}
