:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef0f4;
  --border: #dfe3ea;
  --text: #14171f;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-soft: #dbeafe;
  --danger: #dc2626;
  --warn: #d97706;
  --ok: #059669;
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1116;
    --surface: #171b22;
    --surface-2: #1e242e;
    --border: #2a313c;
    --text: #e8eaed;
    --muted: #98a1af;
    --accent: #60a5fa;
    --accent-soft: #1e3a5f;
    --danger: #f87171;
    --warn: #fbbf24;
    --ok: #34d399;
  }
}

* { box-sizing: border-box; }

/* لازمة: display المعرَّف صراحةً يتغلب على خاصية hidden بدون هذه القاعدة. */
[hidden] { display: none !important; }

html {
  /* يمنع iOS من تكبير الخط تلقائيًا في الوضع الأفقي */
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  overflow-x: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Tahoma, sans-serif;
  line-height: 1.6;
}

button { font-family: inherit; cursor: pointer; }
h1, h2, h3 { line-height: 1.3; }
.muted { color: var(--muted); font-size: 0.9rem; }

/* ── شاشة الدخول ─────────────────────────────── */
.login-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 1.5rem;
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2.5rem 2rem;
  width: min(380px, 100%);
  text-align: center;
  box-shadow: 0 10px 40px rgb(0 0 0 / 0.08);
}
.login-icon { font-size: 2.5rem; }
.login-card h1 { margin: 0.5rem 0 0.25rem; font-size: 1.35rem; }
.login-card input {
  width: 100%;
  margin: 1.25rem 0 0.75rem;
  padding: 0.8rem 1rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  text-align: center;
}
.login-card button {
  width: 100%;
  padding: 0.8rem;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-size: 1rem;
  font-weight: 600;
}
.error { color: var(--danger); font-size: 0.9rem; margin: 0.75rem 0 0; }

/* ── الهيكل ──────────────────────────────────── */
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  padding: 1rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand { font-weight: 700; font-size: 1.05rem; }
.header-actions { display: flex; gap: 0.5rem; }
.ghost {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.45rem 0.8rem;
  border-radius: 9px;
  font-size: 0.85rem;
}
.ghost:hover { background: var(--accent-soft); }

main { padding: 0 1.25rem 3rem; max-width: 960px; margin: 0 auto; }

/* ── البطاقات ────────────────────────────────── */
.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 0.75rem;
  padding: 1.25rem;
  max-width: 960px;
  margin: 0 auto;
}
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.9rem 1rem;
}
.card .value { font-size: 1.5rem; font-weight: 700; }
.card .label { color: var(--muted); font-size: 0.8rem; }
.card.alert .value { color: var(--danger); }

/* ── التبويبات ───────────────────────────────── */
.tabs {
  display: flex;
  gap: 0.4rem;
  padding: 0 1.25rem 1rem;
  max-width: 960px;
  margin: 0 auto;
  overflow-x: auto;
}
.tab {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 0.5rem 1rem;
  border-radius: 999px;
  font-size: 0.9rem;
  white-space: nowrap;
}
.tab.active { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }

.panel { display: none; }
.panel.active { display: block; }

.filters { display: flex; gap: 0.5rem; margin-bottom: 1rem; }
.filters select {
  padding: 0.5rem 0.8rem;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
}

/* ── القوائم ─────────────────────────────────── */
.list { display: flex; flex-direction: column; gap: 0.6rem; }
.item {
  /* الروابط الطويلة تمدّ البطاقة وتكسر التخطيط بدون هذا */
  overflow-wrap: anywhere;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.9rem 1rem;
}
.item-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.75rem;
  margin-bottom: 0.3rem;
  flex-wrap: wrap;
}
.item-sender { font-weight: 600; overflow-wrap: anywhere; }
.item-time { color: var(--muted); font-size: 0.78rem; }
.item-summary { margin: 0.2rem 0; }
.item-body { color: var(--muted); font-size: 0.85rem; margin: 0.35rem 0 0; }
.tags { display: flex; gap: 0.35rem; flex-wrap: wrap; margin-top: 0.5rem; }
.tag {
  font-size: 0.72rem;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  border: 1px solid var(--border);
}
.tag.src-whatsapp { background: #dcfce7; color: #166534; border-color: #bbf7d0; }
.tag.src-email { background: var(--accent-soft); color: var(--accent); }
.tag.u-عاجل { background: #fee2e2; color: #991b1b; border-color: #fecaca; }
.tag.u-مهم { background: #fef3c7; color: #92400e; border-color: #fde68a; }
@media (prefers-color-scheme: dark) {
  .tag.src-whatsapp { background: #14361f; color: #6ee7a8; border-color: #1d4d2c; }
  .tag.u-عاجل { background: #451a1a; color: #fca5a5; border-color: #5c2222; }
  .tag.u-مهم { background: #422f10; color: #fcd34d; border-color: #573f16; }
}

.amount { font-weight: 700; font-variant-numeric: tabular-nums; }
.amount.income { color: var(--ok); }
.amount.expense { color: var(--danger); }

.done-btn {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 0.3rem 0.7rem;
  font-size: 0.8rem;
}

/* ── الرسم البياني ───────────────────────────── */
.bars { display: flex; flex-direction: column; gap: 0.5rem; margin-bottom: 1.5rem; }
.bar-row { display: grid; grid-template-columns: 6.5rem 1fr auto; gap: 0.6rem; align-items: center; }
.bar-label { font-size: 0.85rem; color: var(--muted); }
.bar-track { background: var(--surface-2); border-radius: 999px; height: 0.6rem; overflow: hidden; }
.bar-fill { background: var(--accent); height: 100%; border-radius: 999px; }
.bar-value { font-size: 0.85rem; font-variant-numeric: tabular-nums; }

/* ── المحادثة ────────────────────────────────── */
.chat-panel {
  display: none;
  flex-direction: column;
  /* --chat-h يضبطها app.js من المساحة الحقيقية المتبقية أسفل الشاشة */
  height: var(--chat-h, calc(100vh - 260px));
  min-height: 240px;
}
.chat-panel.active { display: flex; }
.chat-log {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
  padding: 0.5rem 0 1rem;
}
.bubble {
  max-width: 82%;
  padding: 0.7rem 1rem;
  border-radius: var(--radius);
  white-space: pre-wrap;
  word-wrap: break-word;
}
.bubble.user { align-self: flex-start; background: var(--accent); color: #fff; }
.bubble.assistant { align-self: flex-end; background: var(--surface); border: 1px solid var(--border); }
.bubble.error { align-self: flex-end; background: #fee2e2; color: #991b1b; }

.chat-form { display: flex; gap: 0.5rem; padding-top: 0.5rem; border-top: 1px solid var(--border); }
.chat-form input {
  flex: 1;
  padding: 0.75rem 1rem;
  font-size: 16px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  /* أقل من 16px يجعل iOS يقرّب الشاشة عند التركيز */
  font-size: 16px;
}
.chat-form button {
  padding: 0.75rem 1.4rem;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}
.chat-form button:disabled { opacity: 0.5; cursor: not-allowed; }

.empty { color: var(--muted); text-align: center; padding: 2.5rem 1rem; }

.bubble .li { display: block; }

/* ══════════════════════════════════════════════════════
   الجوال — أهداف لمس أكبر، تنقّل في متناول الإبهام
   ══════════════════════════════════════════════════════ */
@media (max-width: 640px) {
  /* dvh يتتبع شريط المتصفح المتغيّر على الجوال، بعكس vh */
  .chat-panel { height: calc(100dvh - 300px); min-height: 260px; }

  header {
    padding: 0.75rem 1rem;
    padding-top: max(0.75rem, env(safe-area-inset-top));
  }
  .brand { font-size: 0.95rem; }
  .ghost { padding: 0.55rem 0.7rem; min-height: 40px; font-size: 0.8rem; }

  .stats {
    grid-template-columns: repeat(2, 1fr);
    gap: 0.6rem;
    padding: 0.9rem 1rem;
  }
  .card { padding: 0.75rem 0.85rem; }
  .card .value { font-size: 1.25rem; }
  .card .label { font-size: 0.72rem; }

  /* شريط التبويبات يلتصق تحت الرأس ويُمرَّر أفقيًا بانسيابية */
  .tabs {
    position: sticky;
    top: 57px;
    z-index: 9;
    background: var(--bg);
    padding: 0.5rem 1rem 0.75rem;
    gap: 0.35rem;
    scroll-snap-type: x proximity;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .tabs::-webkit-scrollbar { display: none; }
  .tab { scroll-snap-align: start; min-height: 40px; padding: 0.5rem 0.9rem; }

  main { padding: 0 1rem 2rem; padding-bottom: max(2rem, env(safe-area-inset-bottom)); }

  .filters select { flex: 1; min-height: 42px; font-size: 0.9rem; }

  .item { padding: 0.8rem 0.9rem; }
  .item-head { gap: 0.4rem; }
  .item-time { font-size: 0.72rem; }
  .item-body { font-size: 0.82rem; }

  /* زر "تم" أكبر ليسهل ضغطه بالإبهام */
  .done-btn { min-height: 40px; min-width: 56px; }

  .bar-row { grid-template-columns: 4.5rem 1fr auto; gap: 0.4rem; }
  .bar-label, .bar-value { font-size: 0.78rem; }

  .bubble { max-width: 90%; font-size: 0.92rem; }
  .chat-form { padding-bottom: max(0.5rem, env(safe-area-inset-bottom)); }
  .chat-form button { min-height: 46px; padding: 0.75rem 1.1rem; }
}

/* شاشات ضيقة جدًا: بطاقة واحدة في الصف */
@media (max-width: 360px) {
  .stats { grid-template-columns: 1fr; }
}

/* أثناء المحادثة على الجوال نخفي البطاقات ليظهر حقل الكتابة بلا تمرير */
@media (max-width: 640px) {
  body.chat-active .stats { display: none; }
  body.chat-active .tabs { top: 0; }
  .chat-panel { height: var(--chat-h, calc(100dvh - 300px)); }
}

.tag.src-sms { background: #fef3c7; color: #92400e; border-color: #fde68a; }
@media (prefers-color-scheme: dark) {
  .tag.src-sms { background: #3a2e12; color: #fcd34d; border-color: #4d3d18; }
}

/* بطاقة تشخيص استقبال الرسائل النصية */
.hook-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.7rem 1rem;
  margin-bottom: 0.9rem;
  font-size: 0.85rem;
}
.hook-box summary { cursor: pointer; color: var(--muted); font-weight: 600; }
.hook-box[open] summary { margin-bottom: 0.6rem; }
.hook-row {
  display: grid;
  grid-template-columns: 1.2rem 6.5rem 1fr;
  gap: 0.4rem;
  align-items: start;
  padding: 0.4rem 0;
  border-top: 1px solid var(--border);
}
.hook-time { color: var(--muted); font-size: 0.75rem; }
.hook-sample {
  grid-column: 1 / -1;
  color: var(--muted);
  font-size: 0.75rem;
  overflow-wrap: anywhere;
  padding-inline-start: 1.6rem;
}
@media (max-width: 640px) {
  .hook-row { grid-template-columns: 1.2rem 1fr; }
  .hook-time { grid-column: 2; }
}
