/* ============================================================
   متابعة السكري — التنسيقات العامة (عربي RTL)
   ============================================================ */

:root {
  /* الألوان الأساسية */
  --brand:        #0f766e;
  --brand-dark:   #115e59;
  --brand-light:  #ccfbf1;
  --brand-tint:   #f0fdfa;

  /* ألوان حالة القياس */
  --ok:           #16a34a;
  --ok-bg:        #dcfce7;
  --high:         #f59e0b;
  --high-bg:      #fef3c7;
  --veryhigh:     #dc2626;
  --veryhigh-bg:  #fee2e2;
  --low:          #2563eb;
  --low-bg:       #dbeafe;

  /* محايدة */
  --bg:           #f6f8f9;
  --surface:      #ffffff;
  --border:       #e2e8f0;
  --text:         #0f172a;
  --text-soft:    #64748b;
  --text-faint:   #94a3b8;

  --radius:       14px;
  --radius-sm:    9px;
  --shadow:       0 1px 3px rgba(15, 23, 42, .06), 0 1px 2px rgba(15, 23, 42, .04);
  --shadow-lg:    0 10px 30px rgba(15, 23, 42, .10);
  --nav-h:        64px;
  --header-h:     58px;   /* بيتحدّث من ui.js حسب الارتفاع الفعلي */
}

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

html {
  -webkit-text-size-adjust: 100%;
  /* يمنع تأخير النقرة ويمنع الزوم بالنقر المزدوج على الموبايل */
  touch-action: manipulation;
}

body {
  font-family: 'Cairo', 'Segoe UI', Tahoma, sans-serif;
  background: var(--bg);
  color: var(--text);
  direction: rtl;
  line-height: 1.7;
  font-size: 15px;
  /* مساحة للقائمة السفلية + المنطقة الآمنة في أيفون */
  padding-bottom: calc(var(--nav-h) + 16px + env(safe-area-inset-bottom, 0px));
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: rgba(15, 118, 110, .12);
  overflow-x: hidden;
}

/* أي عنصر قابل للنقر لازم يكون سهل يتضغط بالصباع */
button, .chip, .seg button, .bottom-nav a, a.btn {
  -webkit-tap-highlight-color: rgba(15, 118, 110, .18);
  touch-action: manipulation;
}

/* الأرقام تفضل بالإنجليزي وسهلة القراءة */
.num, input[type="number"], .kpi-value, .reading-value, table td, table th {
  font-variant-numeric: tabular-nums;
}

a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ------------------------------------------------------------ التخطيط */

.container {
  max-width: 960px;
  margin: 0 auto;
  padding: 0 14px;
}

.app-header {
  background: linear-gradient(135deg, var(--brand) 0%, var(--brand-dark) 100%);
  color: #fff;
  padding: 14px 0;
  box-shadow: var(--shadow);
  position: sticky;
  top: 0;
  z-index: 40;
}

.app-header .container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.app-title {
  font-size: 17px;
  font-weight: 800;
  display: flex;
  align-items: center;
  gap: 8px;
}

.app-title .logo { font-size: 22px; line-height: 1; }

.header-user {
  font-size: 12.5px;
  opacity: .92;
  display: flex;
  align-items: center;
  gap: 10px;
}

.header-user > a {
  display: inline-flex;
  align-items: center;
  min-height: 38px;
  padding: 0 4px;
}

.btn-logout {
  background: rgba(255, 255, 255, .18);
  color: #fff;
  border: none;
  border-radius: 9px;
  min-height: 38px;
  padding: 6px 13px;
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.btn-logout:hover, .btn-logout:active { background: rgba(255, 255, 255, .3); }

.page { padding: 18px 0 30px; }

.page-title {
  font-size: 20px;
  font-weight: 800;
  margin-bottom: 4px;
}

.page-sub {
  color: var(--text-soft);
  font-size: 13.5px;
  margin-bottom: 18px;
}

/* ------------------------------------------------------------ التنقل */

.bottom-nav {
  position: fixed;
  bottom: 0; right: 0; left: 0;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px));
  padding-bottom: env(safe-area-inset-bottom, 0px);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  z-index: 50;
  box-shadow: 0 -2px 12px rgba(15, 23, 42, .05);
}

.bottom-nav a {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  min-height: 48px;
  color: var(--text-faint);
  font-size: 11px;
  font-weight: 600;
  text-decoration: none;
  transition: color .15s;
}

.bottom-nav a .ico { font-size: 21px; line-height: 1; }

.bottom-nav a:active { background: var(--brand-tint); }
.bottom-nav a:hover { text-decoration: none; color: var(--brand); }
.bottom-nav a.active { color: var(--brand); }

/* ------------------------------------------------------------ الكروت */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  margin-bottom: 14px;
}

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.card-title {
  font-size: 15.5px;
  font-weight: 800;
  display: flex;
  align-items: center;
  gap: 7px;
}

.card-hint {
  font-size: 12.5px;
  color: var(--text-soft);
  margin-top: -6px;
  margin-bottom: 12px;
}

/* ------------------------------------------------------------ النماذج */

.field { margin-bottom: 14px; }

label, .field-label {
  display: block;
  font-size: 13.5px;
  font-weight: 700;
  margin-bottom: 6px;
  color: var(--text);
}

.field-note {
  font-size: 12px;
  color: var(--text-soft);
  font-weight: 400;
  margin-right: 4px;
}

/*
   ملاحظة مهمة للموبايل: حجم الخط لازم يكون 16px على الأقل،
   لأن سفاري في الأيفون بيعمل زوم تلقائي لأي حقل خطه أصغر من كده.
*/
input[type="text"], input[type="tel"], input[type="password"],
input[type="number"], input[type="date"], input[type="time"],
input[type="email"], input[type="search"], input[type="url"],
input[type="month"], input[type="datetime-local"], input:not([type]),
select, textarea {
  width: 100%;
  min-height: 46px;
  padding: 11px 12px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 16px;
  color: var(--text);
  background: #fff;
  transition: border-color .15s, box-shadow .15s;
  appearance: none;
  -webkit-appearance: none;
}

/* سهم القائمة المنسدلة (اتشال بسبب appearance:none) */
select {
  background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2.5' stroke-linecap='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: left 12px center;
  background-size: 17px;
  padding-left: 38px;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 3px var(--brand-light);
}

textarea { resize: vertical; min-height: 68px; }

input[type="date"], input[type="time"] { direction: ltr; text-align: right; }

/* شيل أسهم الزيادة والنقصان من حقول الأرقام — بتلخبط على الموبايل */
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
input[type="number"] { -moz-appearance: textfield; }

.input-big {
  font-size: 30px !important;
  font-weight: 800;
  text-align: center;
  padding: 14px !important;
  letter-spacing: 1px;
}

.row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.row-3 {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}

/* أزرار الاختيار (segmented) */
.seg {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
}

.seg button, .seg .opt {
  flex: 1 1 auto;
  min-width: 82px;
  min-height: 44px;
  padding: 10px;
  border: 1.5px solid var(--border);
  background: #fff;
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-soft);
  cursor: pointer;
  transition: all .15s;
  text-align: center;
}

.seg button:hover, .seg .opt:hover {
  border-color: var(--brand);
  color: var(--brand);
}

.seg button.on, .seg .opt.on {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
}

/* اختيار متعدد على شكل شرائح */
.chips { display: flex; flex-wrap: wrap; gap: 7px; }

.chip {
  min-height: 40px;
  padding: 8px 15px;
  border: 1.5px solid var(--border);
  border-radius: 999px;
  background: #fff;
  font-family: inherit;
  font-size: 13.5px;
  font-weight: 600;
  color: var(--text-soft);
  cursor: pointer;
  transition: all .15s;
}
.chip:hover { border-color: var(--brand); color: var(--brand); }
.chip.on { background: var(--brand-light); border-color: var(--brand); color: var(--brand-dark); }

/* ------------------------------------------------------------ الأزرار */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 48px;
  padding: 12px 20px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--brand);
  color: #fff;
  font-family: inherit;
  font-size: 15.5px;
  font-weight: 700;
  cursor: pointer;
  transition: background .15s, transform .05s;
}
.btn:hover { background: var(--brand-dark); text-decoration: none; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .55; cursor: not-allowed; }

.btn-block { width: 100%; }
.btn-lg { min-height: 54px; padding: 15px 24px; font-size: 16.5px; }

/*
   زرار الحفظ يفضل في الشاشة وانت بتنزل في نموذج طويل على الموبايل.
   على الشاشات الكبيرة بيرجع عادي في مكانه.
*/
.sticky-save {
  position: sticky;
  bottom: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px) + 10px);
  z-index: 30;
  box-shadow: 0 6px 20px rgba(15, 118, 110, .35);
}

.btn-ghost {
  background: transparent;
  color: var(--brand);
  border: 1.5px solid var(--brand);
}
.btn-ghost:hover { background: var(--brand-tint); }

.btn-soft {
  background: var(--brand-tint);
  color: var(--brand-dark);
  border: 1.5px solid var(--brand-light);
}
.btn-soft:hover { background: var(--brand-light); }

.btn-sm { min-height: 38px; padding: 7px 13px; font-size: 13px; }

.btn-danger { background: var(--veryhigh); }
.btn-danger:hover { background: #b91c1c; }

.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 40px;
  min-height: 40px;
  background: transparent;
  border: none;
  color: var(--text-faint);
  cursor: pointer;
  font-size: 17px;
  border-radius: 9px;
}
.btn-icon:hover, .btn-icon:active { background: var(--bg); color: var(--text); }

/* ------------------------------------------------------------ التنبيهات */

.alert {
  padding: 11px 14px;
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  font-weight: 600;
  margin-bottom: 14px;
  border-right: 4px solid;
}
.alert-error   { background: var(--veryhigh-bg); color: #991b1b; border-color: var(--veryhigh); }
.alert-success { background: var(--ok-bg);       color: #14532d; border-color: var(--ok); }
.alert-warn    { background: var(--high-bg);     color: #92400e; border-color: var(--high); }
.alert-info    { background: var(--low-bg);      color: #1e3a8a; border-color: var(--low); }
.alert.hidden  { display: none; }

/* ------------------------------------------------------------ مؤشرات KPI */

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 11px;
}

.kpi {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 13px 14px;
  box-shadow: var(--shadow);
}

.kpi-label {
  font-size: 12px;
  color: var(--text-soft);
  font-weight: 600;
  margin-bottom: 3px;
}

.kpi-value {
  font-size: 25px;
  font-weight: 800;
  line-height: 1.25;
}

.kpi-unit {
  font-size: 12px;
  color: var(--text-faint);
  font-weight: 600;
  margin-right: 3px;
}

.kpi-sub {
  font-size: 11.5px;
  color: var(--text-soft);
  margin-top: 2px;
}

.kpi-delta { font-size: 12px; font-weight: 700; margin-top: 3px; }
.delta-good { color: var(--ok); }
.delta-bad  { color: var(--veryhigh); }
.delta-flat { color: var(--text-faint); }

/* ------------------------------------------------------------ ألوان القياس */

.v-ok        { color: var(--ok); }
.v-high      { color: var(--high); }
.v-veryhigh  { color: var(--veryhigh); }
.v-low       { color: var(--low); }

.bg-ok       { background: var(--ok-bg);       color: #14532d; }
.bg-high     { background: var(--high-bg);     color: #92400e; }
.bg-veryhigh { background: var(--veryhigh-bg); color: #991b1b; }
.bg-low      { background: var(--low-bg);      color: #1e3a8a; }

/* ------------------------------------------------------------ قائمة القياسات */

.reading-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}
.reading-item:last-child { border-bottom: none; }

.reading-value {
  min-width: 72px;
  text-align: center;
  font-size: 21px;
  font-weight: 800;
  padding: 7px 4px;
  border-radius: var(--radius-sm);
  line-height: 1.15;
}

.reading-value small {
  display: block;
  font-size: 9.5px;
  font-weight: 600;
  opacity: .75;
}

.reading-body { flex: 1; min-width: 0; }

.reading-main {
  font-size: 13.5px;
  font-weight: 700;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

.reading-meta {
  font-size: 12px;
  color: var(--text-soft);
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tag {
  display: inline-block;
  font-size: 10.5px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--bg);
  color: var(--text-soft);
  border: 1px solid var(--border);
}

.tag-delta { background: var(--high-bg); color: #92400e; border-color: #fcd34d; }
.tag-delta.good { background: var(--ok-bg); color: #14532d; border-color: #86efac; }

.reading-actions { display: flex; gap: 2px; }

/* ------------------------------------------------------------ خطة القياس */

.plan-item {
  display: flex;
  align-items: flex-start;
  gap: 11px;
  padding: 11px 0;
  border-bottom: 1px solid var(--border);
}
.plan-item:last-child { border-bottom: none; }

.plan-item.tappable {
  cursor: pointer;
  border-radius: 10px;
  padding: 11px 8px;
  margin: 0 -8px;
  transition: background .15s;
}
.plan-item.tappable:hover,
.plan-item.tappable:active { background: var(--brand-tint); }

.plan-ico {
  width: 38px; height: 38px;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 19px;
  background: var(--bg);
  border-radius: 11px;
}

.plan-body { flex: 1; min-width: 0; }

.plan-title {
  font-size: 14px;
  font-weight: 800;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 7px;
}

.plan-why {
  font-size: 12.5px;
  color: var(--text-soft);
  line-height: 1.6;
  margin-top: 2px;
}

.plan-time {
  font-size: 12.5px;
  font-weight: 800;
  color: var(--text-soft);
  white-space: nowrap;
  padding-top: 2px;
}

.pill {
  font-size: 10.5px;
  font-weight: 800;
  padding: 2px 9px;
  border-radius: 999px;
  white-space: nowrap;
}

/* ------------------------------------------------------------ الجداول */

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13.5px;
  min-width: 460px;
}

th {
  text-align: right;
  font-weight: 700;
  color: var(--text-soft);
  font-size: 12px;
  padding: 9px 8px;
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
}

td {
  padding: 10px 8px;
  border-bottom: 1px solid var(--border);
}

tr:last-child td { border-bottom: none; }

/* ------------------------------------------------------------ الرسوم */

.chart-box {
  position: relative;
  height: 300px;
  width: 100%;
}

.chart-box.short { height: 230px; }

/* ------------------------------------------------------------ فترات التاريخ */

.range-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  align-items: end;
}

.range-bar .field { margin-bottom: 0; flex: 1 1 130px; }

/* ------------------------------------------------------------ متنوعات */

.empty {
  text-align: center;
  padding: 34px 16px;
  color: var(--text-faint);
}
.empty .ico { font-size: 40px; display: block; margin-bottom: 8px; opacity: .55; }

.loading {
  text-align: center;
  padding: 34px;
  color: var(--text-soft);
  font-size: 14px;
}

.spinner {
  display: inline-block;
  width: 17px; height: 17px;
  border: 2.5px solid var(--brand-light);
  border-top-color: var(--brand);
  border-radius: 50%;
  animation: spin .7s linear infinite;
  vertical-align: middle;
  margin-left: 7px;
}
@keyframes spin { to { transform: rotate(360deg); } }

.hidden { display: none !important; }

.divider { height: 1px; background: var(--border); margin: 16px 0; }

.muted { color: var(--text-soft); font-size: 13px; }
.small { font-size: 12px; }
.center { text-align: center; }
.mt { margin-top: 14px; }
.mb { margin-bottom: 14px; }

.disclaimer {
  background: #fffbeb;
  border: 1px solid #fde68a;
  border-radius: var(--radius-sm);
  padding: 11px 13px;
  font-size: 12.5px;
  color: #78350f;
  line-height: 1.65;
  margin-top: 18px;
}

/* ------------------------------------------------------------ صفحة الدخول */

.auth-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 22px 14px;
  background: linear-gradient(160deg, var(--brand) 0%, var(--brand-dark) 55%, #0b3d38 100%);
}

.auth-card {
  background: #fff;
  border-radius: 20px;
  padding: 28px 24px;
  width: 100%;
  max-width: 400px;
  box-shadow: var(--shadow-lg);
}

.auth-logo {
  text-align: center;
  margin-bottom: 20px;
}
.auth-logo .ico { font-size: 44px; display: block; }
.auth-logo h1 { font-size: 21px; font-weight: 800; margin-top: 6px; }
.auth-logo p { font-size: 13px; color: var(--text-soft); }

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

.auth-tabs button {
  flex: 1;
  padding: 9px;
  border: none;
  background: transparent;
  border-radius: 8px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  color: var(--text-soft);
  cursor: pointer;
}
.auth-tabs button.on { background: #fff; color: var(--brand); box-shadow: var(--shadow); }

/* ------------------------------------------------------------ شاشات أكبر */

@media (min-width: 760px) {
  body { padding-bottom: 24px; font-size: 15.5px; }

  .bottom-nav {
    position: sticky;
    top: var(--header-h);
    bottom: auto;
    height: auto;
    max-width: 960px;
    margin: 14px auto 0;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
  }

  .bottom-nav a {
    flex-direction: row;
    gap: 7px;
    padding: 12px 6px;
    font-size: 13.5px;
  }

  .bottom-nav a .ico { font-size: 16px; }
  .bottom-nav a.active { background: var(--brand-tint); }

  .sticky-save { position: static; box-shadow: none; }

  .row-3 { grid-template-columns: repeat(3, 1fr); }
  .chart-box { height: 340px; }
}

@media (max-width: 400px) {
  .row { grid-template-columns: 1fr; }
  .row-3 { grid-template-columns: 1fr 1fr; }
  .kpi-value { font-size: 22px; }
}

/* ------------------------------------------------------------ تقليل الحركة */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
    scroll-behavior: auto !important;
  }
}

/* ------------------------------------------------------------ الطباعة */

@media print {
  .bottom-nav, .app-header, .no-print, .btn { display: none !important; }
  body { background: #fff; padding: 0; }
  .card { break-inside: avoid; box-shadow: none; border: 1px solid #ccc; }
  .page { padding: 0; }
}
