:root {
  --bg: #0b0b0c;
  --card: #17171a;
  --line: #2a2a2f;
  --text: #f4f4f5;
  --muted: #9a9aa3;
  --accent: #e8262c;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Tahoma, sans-serif; overscroll-behavior: none; }
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; user-select: none; }
input { font: inherit; }

.screen { position: fixed; inset: 0; display: none; flex-direction: column; }
.screen.active { display: flex; }

/* الرئيسية */
#home { align-items: center; justify-content: center; padding: 24px 20px calc(24px + var(--safe-b)); gap: 18px; }
.logo { font-size: 64px; font-weight: 900; letter-spacing: -2px; line-height: 1; }
.logo span { color: var(--accent); }
.sub { margin: 0 0 12px; color: var(--muted); text-align: center; }
.modes { width: 100%; max-width: 420px; display: flex; flex-direction: column; gap: 12px; }
.big { display: grid; grid-template-columns: 52px 1fr; grid-template-rows: auto auto; text-align: right;
  background: var(--card); border: 1px solid var(--line); border-radius: 18px; padding: 16px; column-gap: 12px; }
.big:active { transform: scale(.98); }
.big .ico { grid-row: span 2; display: grid; place-items: center; width: 52px; height: 52px; border-radius: 14px;
  background: var(--accent); font-size: 24px; font-weight: 700; }
.big b { font-size: 18px; }
.big small { color: var(--muted); font-size: 13px; margin-top: 2px; }
.link { color: var(--muted); text-decoration: underline; font-size: 14px; padding: 8px; cursor: pointer; }

/* الكاميرا */
#camera { background: #000; }
#video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.cam-top { position: absolute; top: calc(12px + var(--safe-t)); left: 12px; right: 12px; display: flex; align-items: center; gap: 10px; }
.hint { flex: 1; text-align: center; background: rgba(0,0,0,.55); padding: 8px 12px; border-radius: 12px; font-size: 14px; }
.cam-bottom { position: absolute; bottom: calc(28px + var(--safe-b)); left: 0; right: 0; display: flex; justify-content: space-around; align-items: center; }
.icon-btn { width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center; background: rgba(255,255,255,.14); font-size: 18px; cursor: pointer; }
.icon-btn.ghost { visibility: hidden; }
.shutter { width: 78px; height: 78px; border-radius: 50%; border: 5px solid #fff; background: rgba(255,255,255,.25); }
.shutter:active { background: var(--accent); }
.shutter.rec { background: var(--accent); }
#video.mirror { transform: scaleX(-1); }
.cam-axis { position: absolute; left: 50%; transform: translateX(-50%); bottom: calc(132px + var(--safe-b)); width: min(320px, 86vw);
  background: rgba(0,0,0,.55) !important; border-color: rgba(255,255,255,.15) !important; }
.cam-axis button.on { background: rgba(255,255,255,.22) !important; }
#camera.recording .cam-axis { display: none; }
#camera.axis-v .sweep-arrow { transform: translate(-50%,-50%) rotate(90deg); }
#camera.axis-v.recording .sweep-arrow { animation-name: slide-v; }
@keyframes slide-v { 0% { opacity: 0; transform: translate(-50%,-90%) rotate(90deg); } 40% { opacity: 1; } 100% { opacity: 0; transform: translate(-50%,-10%) rotate(90deg); } }
.hidden { display: none !important; }
.sweep-bar { position: absolute; left: 24px; right: 24px; bottom: calc(124px + var(--safe-b)); height: 4px; border-radius: 2px; background: rgba(255,255,255,.2); overflow: hidden; display: none; }
.sweep-bar div { height: 100%; width: 0; background: var(--accent); }
.sweep-arrow { position: absolute; top: 45%; left: 50%; transform: translate(-50%,-50%); font-size: 72px; font-weight: 900; opacity: 0; text-shadow: 0 2px 12px #000; pointer-events: none; }
#camera.recording .sweep-bar { display: block; }
#camera.recording .sweep-arrow { animation: slide 0.9s ease-in-out infinite; }
@keyframes slide { 0% { opacity: 0; transform: translate(-90%,-50%); } 40% { opacity: 1; } 100% { opacity: 0; transform: translate(-10%,-50%); } }

/* المحرر */
#editor { padding-top: var(--safe-t); }
.ed-top { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px; }
#edTitle { color: var(--muted); font-size: 14px; }
.stage { position: relative; flex: 1; min-height: 120px; margin: 0 12px; }
#preview { position: absolute; inset: 0; margin: auto; max-width: 100%; max-height: 100%; border-radius: 10px; box-shadow: 0 6px 30px rgba(0,0,0,.6); touch-action: manipulation; }
.pivot { position: absolute; width: 28px; height: 28px; margin: -14px 0 0 -14px; border: 2px solid var(--accent); border-radius: 50%;
  box-shadow: 0 0 0 2px rgba(255,255,255,.8); pointer-events: none; opacity: 0; transition: opacity .4s; }
.pivot.show { opacity: 1; }
.tip { text-align: center; color: var(--muted); font-size: 12px; margin: 6px 0 4px; }
.controls { padding: 4px 16px; display: flex; flex-direction: column; gap: 8px; }
.slider { display: grid; grid-template-columns: 80px 1fr; align-items: center; font-size: 14px; }
.slider input { width: 100%; accent-color: var(--accent); }
.seg { display: flex; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 3px; }
.seg button { flex: 1; padding: 8px; border-radius: 9px; font-size: 14px; color: var(--muted); }
.seg button.on { background: #2c2c33; color: var(--text); }
.texts { display: flex; gap: 8px; }
.texts input { flex: 1; min-width: 0; background: var(--card); border: 1px solid var(--line); color: var(--text);
  border-radius: 10px; padding: 9px 10px; font-size: 16px; }
.texts.hidden { display: none; }
.export { display: flex; gap: 10px; padding: 10px 16px calc(14px + var(--safe-b)); }
.export button, .sheet-actions button { flex: 1; padding: 13px; border-radius: 14px; background: var(--card); border: 1px solid var(--line); font-weight: 700; }
button.primary { background: var(--accent) !important; border-color: var(--accent) !important; }
.ghost-btn { background: transparent !important; color: var(--muted) !important; }

/* الرئيسية: أزرار فوق */
.home-top { position: absolute; top: calc(12px + var(--safe-t)); left: 16px; right: 16px; display: flex; justify-content: space-between; }
.home-top .icon-btn { position: relative; width: 46px; height: 46px; }
.badge { position: absolute; top: -4px; left: -4px; min-width: 20px; height: 20px; padding: 0 5px; border-radius: 10px;
  background: var(--accent); color: #fff; font-size: 12px; font-weight: 700; display: grid; place-items: center; }

/* شاشات عامة */
.scroll { flex: 1; overflow-y: auto; padding: 4px 16px calc(24px + var(--safe-b)); }
.scroll h3 { margin: 22px 0 10px; font-size: 16px; }
button.wide { width: 100%; padding: 13px; border-radius: 14px; background: var(--card); border: 1px solid var(--line); font-weight: 700; margin-top: 8px; }
.logo.small { font-size: 40px; }

/* الاسم */
.hello-body { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; padding: 24px; }
.hello-body h2 { margin: 8px 0 0; }
.hello-body input, .add-row input { width: 100%; max-width: 360px; background: var(--card); border: 1px solid var(--line); color: var(--text);
  border-radius: 14px; padding: 14px; font-size: 18px; text-align: center; }
.hello-body .wide { max-width: 360px; }

/* بطاقتي */
.me-card { background: var(--card); border: 1px solid var(--line); border-radius: 20px; padding: 16px; display: flex; flex-direction: column; align-items: center; gap: 10px; }
.me-name { font-size: 20px; font-weight: 800; }
.qr { background: #fff; padding: 10px; border-radius: 12px; line-height: 0; }
.qr img, .qr canvas { width: 180px; height: 180px; }
.me-code { color: var(--muted); }
.me-code b { color: var(--text); font-size: 20px; letter-spacing: 3px; }
.row2 { display: flex; gap: 8px; width: 100%; }
.row2 button { flex: 1; padding: 11px; border-radius: 12px; background: #222227; border: 1px solid var(--line); font-weight: 700; font-size: 14px; }
.add-row { display: flex; gap: 8px; }
.add-row input { flex: 1; font-size: 20px; letter-spacing: 4px; text-transform: uppercase; padding: 12px; }
.add-row button { padding: 0 20px; border-radius: 14px; font-weight: 700; }

/* القوائم */
.list { display: flex; flex-direction: column; gap: 8px; }
.list .item { display: flex; align-items: center; gap: 12px; background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 10px 12px; }
.avatar { width: 40px; height: 40px; border-radius: 50%; background: #2c2c33; display: grid; place-items: center; font-weight: 800; flex: none; }
.item .nm { flex: 1; font-weight: 700; }
.item .x { color: var(--muted); font-size: 13px; padding: 6px; }
.list.pick { max-height: 50vh; overflow-y: auto; }
.list.pick .item { cursor: pointer; }
.list.pick .item .chk { width: 24px; height: 24px; border-radius: 50%; border: 2px solid var(--muted); flex: none; }
.list.pick .item.on .chk { background: var(--accent); border-color: var(--accent); box-shadow: inset 0 0 0 4px var(--card); }
.empty { color: var(--muted); text-align: center; padding: 24px 8px; font-size: 14px; }

/* الستريك والإشعارات */
.streak-chip { display: flex; align-items: center; gap: 8px; background: linear-gradient(135deg, #3a1a0c, #26140c); border: 1px solid #5c2a12;
  color: #ffb36b; padding: 8px 14px; border-radius: 999px; font-size: 14px; }
.streak-chip b { font-size: 18px; color: #ffd29c; }
.streak-chip.risk { animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 6px rgba(255,140,40,.15); } }
.notif-card { width: 100%; max-width: 420px; background: #13202e; border: 1px solid #22405f; color: #cfe6ff; border-radius: 14px; padding: 12px 14px; font-size: 14px; text-align: right; line-height: 1.5; }
.notif-card b { color: #fff; }
.streak { font-weight: 800; color: #ffb36b; font-size: 14px; white-space: nowrap; }
.legend { color: var(--muted); font-size: 12px; margin: -4px 0 10px; }

/* الوارد */
.grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.tile { position: relative; border-radius: 12px; overflow: hidden; background: var(--card); cursor: pointer; aspect-ratio: 9 / 16; }
.tile canvas { width: 100%; height: 100%; object-fit: cover; display: block; }
.tile .cap { position: absolute; left: 0; right: 0; bottom: 0; padding: 18px 8px 6px; font-size: 12px;
  background: linear-gradient(transparent, rgba(0,0,0,.75)); }
.tile .cap b { display: block; font-size: 13px; }
.tile.new::after { content: ''; position: absolute; top: 8px; left: 8px; width: 10px; height: 10px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 0 2px #fff; }

/* الماسح */
#scanner { background: #000; }
#scanVideo { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.scan-frame { position: absolute; top: 50%; left: 50%; width: 64vmin; height: 64vmin; transform: translate(-50%,-50%);
  border: 3px solid #fff; border-radius: 20px; box-shadow: 0 0 0 100vmax rgba(0,0,0,.45); }

#vwCanvas { position: absolute; inset: 0; margin: auto; max-width: 100%; max-height: 100%; border-radius: 10px; }

/* النتيجة */
.sheet { position: fixed; inset: 0; background: rgba(0,0,0,.75); display: none; align-items: flex-end; justify-content: center; z-index: 20; }
.sheet.show { display: flex; }
.sheet-card { width: 100%; max-width: 480px; background: var(--card); border-radius: 22px 22px 0 0; padding: 16px 16px calc(16px + var(--safe-b)); }
.result-media { display: grid; place-items: center; max-height: 60vh; overflow: hidden; margin-bottom: 12px; }
.result-media img, .result-media video { max-width: 100%; max-height: 60vh; border-radius: 10px; }
.sheet-actions { display: flex; gap: 8px; }

/* انتظار */
.busy { position: fixed; inset: 0; background: rgba(11,11,12,.92); display: none; flex-direction: column; align-items: center; justify-content: center; gap: 14px; z-index: 30; padding: 24px; text-align: center; }
.busy.show { display: flex; }
.spinner { width: 42px; height: 42px; border-radius: 50%; border: 4px solid #333; border-top-color: var(--accent); animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.busy .bar { width: 220px; height: 4px; background: #333; border-radius: 2px; overflow: hidden; visibility: hidden; }
.busy .bar.on { visibility: visible; }
.busy .bar div { height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.toast { position: fixed; left: 50%; bottom: calc(200px + var(--safe-b)); transform: translateX(-50%); background: #26262b; padding: 10px 16px;
  border-radius: 12px; font-size: 14px; opacity: 0; transition: opacity .3s; pointer-events: none; z-index: 40; max-width: 90vw; text-align: center; }
.toast.show { opacity: 1; }
