:root {
  --bg: #070a12;
  --panel: #0d1322;
  --panel2: #131b30;
  --line: #1e2a44;
  --text: #e6ecf7;
  --muted: #8b9bb8;
  --accent: #38bdf8;
  --accent2: #818cf8;
  --topbar-h: 56px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; }
body {
  background: radial-gradient(1200px 700px at 70% -10%, #101a33 0%, var(--bg) 55%);
  background-attachment: fixed;
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  display: flex; flex-direction: column;
  overflow: hidden;
  overscroll-behavior: none;
  height: 100vh;
  height: 100dvh;
}
[dir="rtl"] body { font-family: "Segoe UI", Tahoma, "Noto Sans Arabic", system-ui, sans-serif; }
button { touch-action: manipulation; }
canvas { touch-action: none; }

/* Top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  height: var(--topbar-h);
  padding: 0 16px;
  padding-top: var(--safe-t);
  border-bottom: 1px solid var(--line);
  background: rgba(13, 19, 34, .72);
  backdrop-filter: blur(8px);
  z-index: 40;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.logo { font-size: 24px; filter: drop-shadow(0 0 8px rgba(56, 189, 248, .5)); flex-shrink: 0; }
.brand-text { min-width: 0; }
h1 { font-size: 17px; font-weight: 700; letter-spacing: .3px; white-space: nowrap; }
.tagline { font-size: 11.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-actions { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.badge {
  font-size: 11px; color: #9fe8ff; background: rgba(56,189,248,.12);
  border: 1px solid rgba(56,189,248,.3); padding: 4px 10px; border-radius: 999px;
  white-space: nowrap;
}
.lang-btn {
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: #04121f; border: 0; font-weight: 700; font-size: 13px;
  padding: 7px 14px; border-radius: 999px; cursor: pointer; transition: transform .15s;
  white-space: nowrap;
}
.lang-btn:hover { transform: translateY(-1px); }
.lang-btn:active { transform: scale(.97); }

/* Layout (desktop default) */
.layout { flex: 1; display: flex; min-height: 0; position: relative; }

/* Side panel (desktop) */
.panel {
  width: 300px; min-width: 260px;
  background: var(--panel);
  border-inline-end: 1px solid var(--line);
  display: flex; flex-direction: column;
  padding: 14px;
  gap: 12px;
  z-index: 20;
}
.panel-head { display: none; }
.search {
  width: 100%; padding: 10px 14px;
  background: var(--panel2); color: var(--text);
  border: 1px solid var(--line); border-radius: 10px;
  font-size: 14px; outline: none; transition: border .2s;
  -webkit-appearance: none; appearance: none;
}
.search:focus { border-color: var(--accent); }
.search::placeholder { color: var(--muted); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  font-size: 11px; padding: 5px 11px; border-radius: 999px;
  background: var(--panel2); border: 1px solid var(--line);
  color: var(--muted); cursor: pointer; transition: all .15s; user-select: none;
  white-space: nowrap;
}
.chip.active { background: rgba(56,189,248,.16); border-color: var(--accent); color: #bfe9ff; }
.organ-list {
  flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 4px;
  padding-inline-start: 2px;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}
.organ-list::-webkit-scrollbar { width: 6px; }
.organ-list::-webkit-scrollbar-thumb { background: var(--line); border-radius: 3px; }
.organ-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 11px; border-radius: 9px;
  color: var(--text); cursor: pointer;
  border: 1px solid transparent; transition: background .12s;
  font-size: 14px; min-height: 38px;
  user-select: none; -webkit-tap-highlight-color: transparent;
}
.organ-item:hover { background: var(--panel2); }
.organ-item.active {
  background: rgba(56,189,248,.13); border-color: rgba(56,189,248,.35);
}
.organ-item .dot { width: 9px; height: 9px; border-radius: 50%; flex-shrink: 0; }
.hint { font-size: 12px; color: var(--muted); text-align: center; padding: 6px 0; }

/* Stage */
.stage { flex: 1; position: relative; min-width: 0; }
.viewer { position: absolute; inset: 0; }
.viewer canvas { display: block; outline: none; }

/* Viewer controls: desktop = bottom-center pill */
.viewer-controls {
  position: absolute; bottom: 16px; inset-inline: 0; margin-inline: auto; width: fit-content;
  display: flex; gap: 8px;
  background: rgba(13, 19, 34, .85); backdrop-filter: blur(8px);
  border: 1px solid var(--line); padding: 8px; border-radius: 14px;
  z-index: 10;
}
.ctl {
  background: var(--panel2); color: var(--muted); border: 1px solid var(--line);
  font-size: 12px; padding: 7px 13px; border-radius: 9px; cursor: pointer; transition: all .15s;
  white-space: nowrap;
}
.ctl:hover { color: var(--text); border-color: var(--muted); }
.ctl:active { transform: scale(.96); }
.ctl.active { background: rgba(56,189,248,.18); color: #bfe9ff; border-color: var(--accent); }
.hint.corner {
  position: absolute; top: calc(var(--safe-t) + 12px); inset-inline: 0; margin-inline: auto; width: fit-content;
  font-size: 11px; pointer-events: none; color: var(--muted);
  background: rgba(7,10,18,.5); padding: 3px 12px; border-radius: 999px; z-index: 2;
}

/* Loading */
.loading {
  position: absolute; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 14px;
  background: rgba(7,10,18,.55); backdrop-filter: blur(2px);
  font-size: 14px; color: var(--muted); z-index: 15;
}
.spinner {
  width: 34px; height: 34px; border-radius: 50%;
  border: 3px solid var(--line); border-top-color: var(--accent);
  animation: spin .9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.hidden { display: none !important; }

/* Info card */
.info-card {
  position: absolute; top: calc(var(--safe-t) + 44px); inset-inline-start: 14px; inset-inline-end: auto;
  background: rgba(13,19,34,.85); backdrop-filter: blur(10px);
  border: 1px solid var(--line); border-inline-start: 3px solid var(--accent);
  border-radius: 12px; padding: 12px 16px; z-index: 8;
  max-width: 250px;
  transition: opacity .25s;
}
.card-close {
  position: absolute; top: 6px; inset-inline-end: 6px;
  width: 22px; height: 22px; line-height: 1;
  display: flex; align-items: center; justify-content: center;
  border: none; background: transparent; color: var(--muted);
  font-size: 13px; border-radius: 6px; cursor: pointer; padding: 0;
}
.card-close:hover, .card-close:focus-visible {
  color: #fff; background: rgba(255,255,255,.12); outline: none;
}
.info-card h2 { font-size: 18px; margin-bottom: 2px; }
.sys-label { font-size: 11.5px; color: var(--accent); text-transform: uppercase; letter-spacing: .6px; }
.stats-label { font-size: 11px; color: var(--muted); margin-top: 6px; border-top: 1px dashed var(--line); padding-top: 6px; }

/* Footer (desktop) */
.footer {
  border-top: 1px solid var(--line);
  padding: 8px 20px calc(8px + var(--safe-b));
  background: rgba(13, 19, 34, .72);
  text-align: center;
  font-size: 10.5px; color: var(--muted);
  display: flex; gap: 12px; justify-content: center; flex-wrap: wrap;
  flex-shrink: 0;
}
.footer p { max-width: 640px; }
.disclaimer { color: #d9b26a; }

/* =========================================================
   MOBILE / COMPACT (≤ 880px): full-screen stage +
   collapsible bottom-sheet organ browser
   ========================================================= */
@media (max-width: 880px) {
  .topbar { padding-inline: 12px; }
  .tagline { display: none; }
  h1 { font-size: 16px; }

  .layout { display: block; }
  .stage { position: absolute; inset: 0; }

  .panel {
    position: absolute; inset-inline: 0; bottom: 0; top: auto;
    width: 100%; min-width: 0;
    height: min(68dvh, 520px);
    border-inline-end: 0;
    border-top: 1px solid var(--line);
    border-radius: 18px 18px 0 0;
    background: rgba(13, 19, 34, .92);
    backdrop-filter: blur(14px);
    box-shadow: 0 -12px 40px rgba(0,0,0,.45);
    padding: 8px 14px calc(14px + var(--safe-b));
    gap: 10px;
    transform: translateY(calc(100% - 74px));   /* collapsed: head only */
    transition: transform .3s cubic-bezier(.4, 0, .2, 1);
    will-change: transform;
  }
  .panel.open { transform: translateY(0); }

  .panel-head {
    display: flex; flex-direction: column; align-items: stretch;
    cursor: pointer; user-select: none; -webkit-tap-highlight-color: transparent;
    padding: 4px 2px 6px;
  }
  .handle {
    width: 42px; height: 4px; border-radius: 2px;
    background: var(--line); margin: 2px auto 8px; flex-shrink: 0;
  }
  .head-row { display: flex; align-items: center; gap: 10px; }
  .panel-title { font-size: 15px; font-weight: 700; flex: 1; }
  .head-count { font-size: 11px; color: #9fe8ff; background: rgba(56,189,248,.12); border: 1px solid rgba(56,189,248,.3); padding: 2px 9px; border-radius: 999px; }
  .chev { width: 0; height: 0; border-inline: 6px solid transparent; border-top: 7px solid var(--muted); transition: transform .3s; flex-shrink: 0; }
  .panel.open .chev { transform: rotate(180deg); }
  .head-attrib {
    font-size: 9px; color: var(--muted); margin-top: 5px;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  }
  .panel .search, .panel .chips, .panel .organ-list, .panel .hint { flex-shrink: 0; }
  .panel .organ-list { flex-shrink: 1; }
  .organ-item { min-height: 44px; font-size: 15px; }
  .chips {
    flex-wrap: nowrap; overflow-x: auto; padding-bottom: 2px;
    scrollbar-width: none; -webkit-overflow-scrolling: touch;
  }
  .chips::-webkit-scrollbar { display: none; }
  .chip { font-size: 11.5px; padding: 6px 12px; }

  /* viewer controls: top-end vertical stack (clear of the sheet) */
  .viewer-controls {
    top: calc(var(--safe-t) + 12px); bottom: auto;
    inset-inline: auto 12px;
    flex-direction: column; gap: 6px;
    padding: 6px; border-radius: 12px;
  }
  .ctl { font-size: 11px; padding: 7px 10px; min-width: 84px; text-align: center; }

  /* compact info card, kept clear of the control stack */
  .info-card {
    top: calc(var(--safe-t) + 12px);
    inset-inline-start: 12px;
    inset-inline-end: 138px;
    max-width: none;
    padding: 10px 13px;
  }
  .info-card h2 { font-size: 16px; }
  .hint.corner { display: none; }
  .footer { display: none; }
}

/* Extra-small phones */
@media (max-width: 400px) {
  .badge { display: none; }
  .stats-label { display: none; }
}

/* ===== mode toggle (full-body) ===== */
.mode-btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--panel2);
  color: var(--muted);
  font: 600 12px/1 inherit;
  padding: 6px 10px;
  border-radius: 999px;
  cursor: pointer;
  touch-action: manipulation;
  transition: color .15s, border-color .15s, background .15s;
  white-space: nowrap;
}
.mode-btn:hover { color: var(--text); }
.mode-btn.active {
  color: #04121f;
  background: var(--accent);
  border-color: var(--accent);
}
@media (max-width: 560px) {
  .tagline { display: none; }
  .mode-btn { padding: 5px 9px; font-size: 11px; }
  .brand h1 { font-size: 15px; }
}

/* ===== full-body scan intro ===== */
.scan-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(ellipse at 50% 42%, rgba(13, 19, 34, 0.55), rgba(4, 6, 12, 0.92) 70%);
  cursor: pointer;
  overflow: hidden;
  transition: opacity .55s ease;
}
.scan-overlay.done { opacity: 0; pointer-events: none; }
.scan-overlay::before {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, transparent 0%, rgba(56, 189, 248, 0.10) 42%, rgba(56, 189, 248, 0.16) 50%, rgba(56, 189, 248, 0.10) 58%, transparent 100%);
  height: 34%;
  animation: scanline 2.6s linear infinite;
  border-bottom: 1px solid rgba(56, 189, 248, 0.35);
  box-shadow: 0 0 24px rgba(56, 189, 248, 0.25);
}
@keyframes scanline { 0% { top: -34%; } 100% { top: 100%; } }
.scan-box {
  position: relative;
  text-align: center;
  width: min(420px, 82vw);
  z-index: 1;
}
.scan-title {
  font: 800 17px/1.3 inherit;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: #dcecfa;
  text-shadow: 0 0 18px rgba(56, 189, 248, 0.55);
  margin-bottom: 18px;
}
.scan-track {
  height: 3px;
  border-radius: 99px;
  background: #16203a;
  overflow: hidden;
  box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.6);
}
.scan-bar {
  height: 100%;
  width: 0%;
  border-radius: 99px;
  background: linear-gradient(90deg, #22d3ee, #38bdf8, #818cf8);
  box-shadow: 0 0 12px rgba(56, 189, 248, 0.8);
  transition: width .25s ease;
}
.scan-status {
  margin-top: 14px;
  font-size: 12.5px;
  color: #9fb2cc;
  font-variant-numeric: tabular-nums;
  min-height: 18px;
}
.scan-skip {
  margin-top: 26px;
  font-size: 11px;
  color: #5b6b88;
  letter-spacing: 0.06em;
}

/* Short landscape phones */
@media (max-height: 500px) and (max-width: 880px) {
  .panel { height: min(80dvh, 420px); }
  .organ-item { min-height: 40px; }
}

/* ===== did-you-know fact block ===== */
.fact-block {
  margin-top: 10px;
  padding: 10px 12px;
  border-radius: 12px;
  background: linear-gradient(135deg, rgba(56, 189, 248, 0.10), rgba(129, 140, 248, 0.10));
  border: 1px solid rgba(56, 189, 248, 0.22);
  max-width: 380px;
  cursor: pointer;
  animation: factIn .3s ease;
  transition: border-color .15s;
}
.fact-block:hover { border-color: rgba(56, 189, 248, .5); }
.fact-hint { opacity: .55; font-size: 10.5px; }
@keyframes factIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
.fact-title {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--accent, #38bdf8);
  margin: 0 0 4px;
}
.fact-text {
  margin: 0;
  font-size: 13px;
  line-height: 1.55;
  color: #c7d5e8;
}

/* ===== legal footer ===== */
.footer .legal {
  font-size: 11px;
  color: #5b6b88;
  line-height: 1.5;
  max-width: 760px;
  margin: 8px auto 4px;
  padding: 0 14px;
}
.footer .credit {
  font-size: 11px;
  color: #6b7c99;
  margin: 2px auto 0;
}
.footer .credit a {
  color: #7dd3fc;
  text-decoration: none;
  border-bottom: 1px dotted rgba(125, 211, 252, 0.4);
}
.footer .credit a:hover { color: #bae6fd; }
