/* حاسبة الذهب — تصميم للجوال */
:root {
  --bg: #f7f2e7;
  --surface: #ffffff;
  --surface-2: #f3ecdc;
  --line: #e6dcc6;
  --text: #2a2215;
  --muted: #7a6c55;
  --gold: #a87b16;
  --gold-strong: #8a6309;
  --gold-soft: #f6e7bf;
  --on-gold: #ffffff;
  --good: #1f7a4d;
  --good-soft: #e1f3e8;
  --warn: #9a6200;
  --warn-soft: #fff1d1;
  --bad: #b3261e;
  --bad-soft: #fde4e1;
  --shadow: 0 1px 2px rgba(60, 40, 0, .06), 0 4px 14px rgba(60, 40, 0, .06);
  --radius: 16px;
  --tabbar-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16120b; --surface: #211b11; --surface-2: #2a2316; --line: #3a3020;
    --text: #f2e9d6; --muted: #a8977a; --gold: #e0b24a; --gold-strong: #f0c45e; --gold-soft: #3a2e14; --on-gold: #1b1407;
    --good: #5cc98f; --good-soft: #163626; --warn: #f0b04a; --warn-soft: #3a2a0e; --bad: #f2796f; --bad-soft: #3d1a17;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #16120b; --surface: #211b11; --surface-2: #2a2316; --line: #3a3020;
  --text: #f2e9d6; --muted: #a8977a; --gold: #e0b24a; --gold-strong: #f0c45e; --gold-soft: #3a2e14; --on-gold: #1b1407;
  --good: #5cc98f; --good-soft: #163626; --warn: #f0b04a; --warn-soft: #3a2a0e; --bad: #f2796f; --bad-soft: #3d1a17;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0 auto;
  max-width: 520px;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: 'IBM Plex Sans Arabic', system-ui, -apple-system, 'Segoe UI', Tahoma, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
}
body.locked { overflow: hidden; }
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
small { font-size: .78em; }
.muted { color: var(--muted); font-weight: 400; }
.center { text-align: center; }

/* ---------- شريط السعر ---------- */
.price-bar {
  position: sticky; top: 0; z-index: 20;
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.price-label { font-size: 12px; color: var(--muted); }
.price-value { font-size: 28px; font-weight: 700; line-height: 1.15; color: var(--gold-strong); font-variant-numeric: tabular-nums; }
.price-value small { font-size: 14px; font-weight: 500; margin-inline-start: 4px; color: var(--muted); }
.price-sub { font-size: 12px; color: var(--muted); display: flex; gap: 6px; flex-wrap: wrap; }
.price-side { display: flex; flex-direction: column; align-items: center; gap: 4px; flex-shrink: 0; }
.pill {
  font-size: 11px; font-weight: 600; padding: 3px 10px; border-radius: 99px;
  background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.pill[data-state="live"] { background: var(--good-soft); color: var(--good); }
.pill[data-state="live"]::before { content: ''; display: inline-block; width: 6px; height: 6px; border-radius: 50%; background: currentColor; margin-inline-end: 5px; vertical-align: middle; animation: pulse 2s infinite; }
.pill[data-state="stale"], .pill[data-state="manual"], .pill[data-state="closed"] { background: var(--warn-soft); color: var(--warn); }
.pill[data-state="error"] { background: var(--bad-soft); color: var(--bad); }
@keyframes pulse { 50% { opacity: .35; } }
.refresh-btn {
  width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--line);
  background: var(--surface); display: grid; place-items: center; color: var(--gold-strong);
}
.refresh-btn svg { width: 22px; height: 22px; }
.refresh-btn.spin svg { animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(-360deg); } }
.countdown { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; min-height: 16px; }

/* ---------- التنبيه ---------- */
.alert {
  margin: 12px 16px 0; padding: 12px 14px; border-radius: 12px;
  background: var(--warn-soft); color: var(--warn); font-size: 14px;
}
.alert[data-level="bad"] { background: var(--bad-soft); color: var(--bad); }
.manual { margin-top: 10px; color: var(--text); }
.manual label { font-size: 13px; }
.manual-row { display: flex; gap: 8px; margin-top: 6px; }
.manual-row input { flex: 1; min-width: 0; }

/* ---------- البطاقات والحقول ---------- */
main { padding: 12px 16px 16px; }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; margin-bottom: 12px; box-shadow: var(--shadow);
}
.card-title { font-size: 15px; font-weight: 700; margin: 0 0 12px; }
.field { margin-top: 14px; }
.label { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 6px; }
input[type="text"] {
  width: 100%; height: 50px; padding: 0 14px; border-radius: 12px;
  border: 1.5px solid var(--line); background: var(--surface-2);
  font-size: 18px; outline: none; transition: border-color .15s;
}
input[type="text"]:focus { border-color: var(--gold); background: var(--surface); }
input[inputmode="decimal"], input[inputmode="numeric"] { font-variant-numeric: tabular-nums; }
.input-wrap { position: relative; }
.input-wrap input { padding-inline-end: 64px; }
.input-wrap .suffix {
  position: absolute; inset-inline-end: 14px; top: 50%; transform: translateY(-50%);
  font-size: 13px; color: var(--muted); pointer-events: none;
}
.input-wrap.invalid input { border-color: var(--bad); }
.input-row { display: flex; gap: 8px; align-items: stretch; }
.grow { flex: 1; min-width: 0; }
.field-error { color: var(--bad); font-size: 13px; min-height: 0; margin-top: 4px; }
.field-error:empty { display: none; }
.hint { font-size: 13px; color: var(--muted); margin: 6px 0 0; }
.hint:empty { display: none; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }

/* مقاطع الاختيار */
.seg {
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 4px;
  background: var(--surface-2); padding: 4px; border-radius: 12px;
}
.seg button {
  min-height: 42px; border: 0; border-radius: 9px; background: transparent;
  font-size: 14px; font-weight: 600; color: var(--muted); padding: 0 6px;
}
.seg button.on { background: var(--surface); color: var(--gold-strong); box-shadow: var(--shadow); }
.seg-mini { flex: 0 0 110px; }
.seg-mini button { min-height: 42px; }

/* العيارات */
.karats { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.karats button {
  height: 60px; border-radius: 12px; border: 1.5px solid var(--line); background: var(--surface-2);
  display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1.1;
}
.karats button b { font-size: 20px; }
.karats button small { color: var(--muted); font-size: 11px; margin-top: 2px; }
.karats button.on { border-color: var(--gold); background: var(--gold-soft); color: var(--gold-strong); }

/* مفتاح التشغيل */
.switch-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-top: 14px; min-height: 44px; font-size: 15px; }
.card > .switch-row:first-child { margin-top: 0; }
.switch {
  appearance: none; -webkit-appearance: none; flex-shrink: 0;
  width: 52px; height: 32px; border-radius: 99px; background: var(--line); position: relative; margin: 0; transition: background .2s; cursor: pointer;
}
.switch::after {
  content: ''; position: absolute; top: 3px; inset-inline-start: 3px; width: 26px; height: 26px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, .25); transition: inset-inline-start .2s;
}
.switch:checked { background: var(--gold); }
.switch:checked::after { inset-inline-start: 23px; }
.switch:focus-visible, button:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }

/* الأزرار */
.btn {
  min-height: 50px; padding: 0 18px; border-radius: 14px; border: 1.5px solid transparent;
  font-size: 16px; font-weight: 700;
}
.btn-sm { min-height: 50px; padding: 0 16px; background: var(--gold); color: var(--on-gold); border-radius: 12px; }
.btn-primary { background: var(--gold); color: var(--on-gold); }
.btn-primary:active { background: var(--gold-strong); }
.btn:disabled { opacity: .45; cursor: default; }
.btn-ghost { background: var(--surface); border-color: var(--line); }
.btn-danger-ghost { background: var(--surface); border-color: var(--bad-soft); color: var(--bad); }
.btn.block { width: 100%; }
.link-btn { background: none; border: 0; color: var(--gold-strong); font-weight: 700; padding: 10px 8px; min-height: 44px; }
.link-btn.danger { color: var(--bad); }
.icon-btn { width: 44px; height: 44px; border: 0; border-radius: 50%; background: var(--surface-2); font-size: 18px; }

.action-bar {
  position: sticky; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 8px); z-index: 10;
  display: flex; gap: 10px; padding: 10px; margin: 0 -6px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  border-radius: 18px;
}

/* ---------- النتائج ---------- */
.results .empty, .empty { color: var(--muted); text-align: center; padding: 16px 8px; font-size: 15px; }
.total-box {
  display: flex; flex-direction: column; align-items: center; text-align: center;
  background: var(--gold-soft); border-radius: 12px; padding: 14px; margin-bottom: 12px;
}
.total-box > span:first-child { font-size: 13px; color: var(--muted); }
.total-box strong { font-size: 32px; color: var(--gold-strong); line-height: 1.2; font-variant-numeric: tabular-nums; }
.total-box strong small { font-size: 15px; }
.total-box .muted { font-size: 13px; }
.table { font-size: 14px; }
.table .group { font-size: 12px; font-weight: 700; color: var(--gold-strong); margin: 12px 0 2px; }
.table .r { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; padding: 8px 0; border-bottom: 1px dashed var(--line); }
.table .r span { color: var(--muted); }
.table .r b { font-weight: 600; font-variant-numeric: tabular-nums; text-align: end; white-space: nowrap; }
.table .r b small { color: var(--muted); font-weight: 400; }
.table .r.strong span, .table .r.strong b { color: var(--text); font-weight: 700; }
.table .r.sub { padding-top: 0; }
.table .r.sub span, .table .r.sub b { font-size: 12px; }
.table .r.neg b { color: var(--bad); }
.reverse { margin-top: 16px; padding: 14px; border-radius: 12px; background: var(--surface-2); }
.reverse.bad { background: var(--bad-soft); }
.reverse h3 { margin: 0 0 6px; font-size: 15px; }
.warn-line { color: var(--bad); font-size: 13px; font-weight: 600; margin: 6px 0; }

/* ---------- الجولة ---------- */
.tour-head { display: flex; align-items: baseline; justify-content: space-between; margin: 4px 0 12px; }
.tour-head h2 { margin: 0; font-size: 20px; }
.tour-list { margin-top: 12px; }
.empty-tour h3 { margin: 0 0 8px; }
.empty-tour ol { margin: 0; padding-inline-start: 20px; color: var(--muted); font-size: 14px; }
.empty-tour li { margin-bottom: 6px; }
.offer-cols { display: flex; justify-content: space-between; font-size: 11px; color: var(--muted); padding: 0 12px 4px; }
.offer-cols span:last-child { margin-inline-end: 40px; }
.offers { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; overflow: hidden; }
.offer { display: flex; align-items: stretch; border-bottom: 1px solid var(--line); border-inline-start: 4px solid transparent; }
.offer:last-child { border-bottom: 0; }
.offer.best { border-inline-start-color: var(--good); }
.offer.worst { border-inline-start-color: var(--bad); }
.offer-open { flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; padding: 7px 8px; background: none; border: 0; text-align: start; min-height: 52px; }
.offer-open:active { background: var(--surface-2); }
.offer-title, .offer-nums { display: flex; flex-direction: column; line-height: 1.3; }
.offer-title { flex: 1; min-width: 0; }
.offer-title b { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.offer-title small, .offer-nums small { font-size: 11px; color: var(--muted); white-space: nowrap; }
.offer-nums { align-items: flex-end; font-variant-numeric: tabular-nums; }
.offer-nums b { font-size: 15px; }
.offer.best .offer-nums small { color: var(--good); font-weight: 700; }
.offer-x { width: 44px; flex-shrink: 0; background: none; border: 0; color: var(--bad); font-size: 15px; }
.rank {
  flex-shrink: 0; width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center;
  background: var(--gold-soft); color: var(--gold-strong); font-weight: 700; font-size: 14px;
}
.offer-note { margin: 10px 0 0; font-size: 13px; color: var(--muted); overflow-wrap: anywhere; }
.tour-tools { margin-top: 12px; }

/* ---------- صفحة التفاصيل ---------- */
.page {
  position: fixed; top: 0; bottom: 0; left: 50%; transform: translateX(-50%); z-index: 35;
  width: 100%; max-width: 520px; display: flex; flex-direction: column; background: var(--bg);
}
.page-head { display: flex; align-items: center; gap: 10px; padding: calc(10px + env(safe-area-inset-top)) 16px 10px; border-bottom: 1px solid var(--line); }
.page-head h2 { margin: 0; font-size: 18px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.page-head .icon-btn { flex-shrink: 0; }
.page-body { flex: 1; overflow-y: auto; padding: 12px 16px calc(24px + env(safe-area-inset-bottom)); -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.page-body .rv-item { margin-bottom: 12px; }
.sub-title { font-size: 14px; margin: 18px 0 0; }
.grid-2 .full { grid-column: 1 / -1; }

/* ---------- الأسعار ---------- */
.ktable { display: flex; flex-direction: column; }
.krow { display: grid; grid-template-columns: 1fr auto 1.4fr; gap: 10px; align-items: center; padding: 12px 8px; border-bottom: 1px solid var(--line); }
.krow span { color: var(--muted); font-size: 13px; }
.krow strong { text-align: end; font-size: 18px; font-variant-numeric: tabular-nums; }
.krow strong small { color: var(--muted); font-weight: 400; font-size: 12px; }
.krow.on { background: var(--gold-soft); border-radius: 10px; border-color: transparent; }
.krow.oz { border-bottom: 0; }
.formula { background: var(--surface-2); border-radius: 10px; padding: 12px; font-size: 14px; font-weight: 600; }
.facts { padding-inline-start: 18px; font-size: 14px; color: var(--muted); }
.facts li { margin-bottom: 6px; }
.facts strong { color: var(--text); }
.settings-tools { display: flex; flex-direction: column; gap: 8px; }

/* ---------- شريط التبويبات ---------- */
.tabbar {
  position: fixed; bottom: 0; left: 50%; transform: translateX(-50%); z-index: 30;
  width: 100%; max-width: 520px;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--line);
}
.tabbar button {
  position: relative; border: 0; background: none; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: 11px; font-weight: 600;
}
.tabbar svg { width: 24px; height: 24px; }
.tabbar button.active { color: var(--gold-strong); }
.badge {
  position: absolute; top: 6px; inset-inline-start: calc(50% + 6px);
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 99px;
  background: var(--bad); color: #fff; font-size: 11px; display: grid; place-items: center;
}

/* ---------- النوافذ السفلية ---------- */
.backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, .4); z-index: 40; opacity: 0; transition: opacity .2s; }
.backdrop.open { opacity: 1; }
.sheet {
  position: fixed; bottom: 0; left: 50%; z-index: 50; width: 100%; max-width: 520px;
  max-height: 88dvh; display: flex; flex-direction: column;
  background: var(--bg); border-radius: 22px 22px 0 0;
  transform: translate(-50%, 100%); transition: transform .22s ease-out;
  padding-bottom: env(safe-area-inset-bottom);
}
.sheet.open { transform: translate(-50%, 0); }
.sheet-handle { width: 40px; height: 5px; border-radius: 99px; background: var(--line); margin: 8px auto 0; }
.sheet-head { display: flex; justify-content: space-between; align-items: center; padding: 8px 16px; }
.sheet-head h2 { margin: 0; font-size: 18px; }
.sheet-body { overflow-y: auto; padding: 0 16px 20px; -webkit-overflow-scrolling: touch; }
.sheet-body .field:first-child { margin-top: 0; }
.sheet-body .btn.block { margin-top: 14px; }

.summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.summary div { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px; display: flex; flex-direction: column; text-align: center; }
.summary span { font-size: 11px; color: var(--muted); }
.summary b { font-size: 17px; font-variant-numeric: tabular-nums; }
.summary b small { font-size: 10px; color: var(--muted); font-weight: 400; }
.headline { margin: 12px 0; padding: 12px 14px; border-radius: 12px; background: var(--good-soft); color: var(--text); font-size: 15px; }
.rv-item { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 12px; margin-bottom: 10px; }
.rv-item.best { border-color: var(--good); }
.rv-item.worst { border-color: var(--bad); }
.rv-head { display: flex; align-items: center; gap: 8px; }
.rv-head .muted { margin-inline-start: auto; font-size: 12px; }
.rv-item ul, .tips ul { list-style: none; margin: 8px 0 0; padding: 0; }
.rv-item li, .tips li { font-size: 14px; padding: 4px 0; }
.lv-bad { color: var(--bad); }
.lv-good { color: var(--good); }
.lv-warn { color: var(--warn); }
.tips { margin-top: 12px; }
.tips h3 { font-size: 15px; margin: 0; }
.criteria { margin-top: 12px; font-size: 12px; color: var(--muted); background: var(--surface-2); border-radius: 10px; padding: 10px 12px; }
.disclaimer { margin: 12px 0 0; padding: 10px 12px; border-radius: 10px; border: 1px dashed var(--warn); color: var(--warn); font-size: 13px; font-weight: 600; text-align: center; }

/* ---------- الإشعار ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 80px); z-index: 60;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 99px; font-size: 14px; font-weight: 600;
  transition: opacity .2s, transform .2s; max-width: calc(100% - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- الطباعة ---------- */
.print-only { display: none; }
@media print {
  body { max-width: none; padding: 0; background: #fff; color: #000; }
  body > *:not(#printArea) { display: none !important; }
  #printArea { display: block; padding: 16px; }
  #printArea table { width: 100%; border-collapse: collapse; font-size: 12px; }
  #printArea th, #printArea td { border: 1px solid #999; padding: 4px 6px; text-align: start; }
  #printArea tr, #printArea .rv-item { break-inside: avoid; box-shadow: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
