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

html, body {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: #fbfbfa;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  color: #1d1d1f;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  touch-action: none;
}

#scene { position: fixed; inset: 0; width: 100%; height: 100%; display: block; }

/* ---------- loading ---------- */
#loading {
  position: fixed; inset: 0; z-index: 30;
  background: #fbfbfa;
  display: flex; align-items: center; justify-content: center;
  transition: opacity 0.6s ease;
}
#loading.done { opacity: 0; pointer-events: none; }
.loading-inner { text-align: center; }
.loading-inner h1 { font-size: 2.2rem; letter-spacing: 0.35em; font-weight: 700; }
.loading-inner p.ahlan { font-size: 1.3rem; letter-spacing: 0; color: #c8102e; animation: none; margin-top: 0.6rem; }
.loading-inner p { margin-top: 0.8rem; color: #86868b; letter-spacing: 0.12em; animation: pulse 1.4s ease infinite; }
@keyframes pulse { 50% { opacity: 0.35; } }

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; z-index: 20; pointer-events: none; }

#hud-top {
  position: absolute;
  top: env(safe-area-inset-top, 0px); left: 0; right: 0;
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 18px;
}
#hud-name { font-size: 13px; letter-spacing: 0.14em; text-transform: uppercase; color: #6e6e73; }
#hud-buttons { display: flex; gap: 8px; pointer-events: auto; }
#hud-buttons button {
  width: 40px; height: 40px; border-radius: 50%;
  border: 1px solid #e3e3e0; background: rgba(255,255,255,0.85);
  font-size: 17px; cursor: pointer; color: #1d1d1f;
  backdrop-filter: blur(6px);
  transition: transform 0.15s ease, background 0.15s ease;
}
#hud-buttons button:hover { transform: scale(1.08); background: #fff; }

#hud-zone {
  position: absolute; top: calc(60px + env(safe-area-inset-top, 0px));
  left: 50%; transform: translateX(-50%);
  font-size: 12px; letter-spacing: 0.3em; text-transform: uppercase;
  color: #b04a17; font-weight: 600;
  opacity: 0; transition: opacity 0.4s ease;
}
#hud-zone.visible { opacity: 1; }

#hud-hint {
  position: absolute; bottom: calc(22px + env(safe-area-inset-bottom, 0px));
  left: 50%; transform: translateX(-50%);
  font-size: 12px; letter-spacing: 0.08em; color: #86868b;
  background: rgba(255,255,255,0.7); padding: 8px 16px; border-radius: 999px;
  border: 1px solid #ececea;
  backdrop-filter: blur(6px);
  white-space: nowrap;
  max-width: 94vw;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: opacity 1s ease;
}
#hud-hint.fade { opacity: 0; }

/* ---------- story card (station info) ---------- */
#story {
  position: fixed; z-index: 18;
  right: 22px; top: 50%;
  width: min(370px, 42vw);
  transform: translateY(-50%) translateX(18px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease, transform 0.3s ease;
}
#story.in { opacity: 1; transform: translateY(-50%) translateX(0); }

#story-card {
  background: rgba(255,255,255,0.92);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid #e8e8e4;
  border-left: 5px solid var(--sc, #c1440e);
  border-radius: 20px;
  padding: 22px 24px 20px;
  box-shadow: 0 18px 50px rgba(29,29,31,0.10);
}
#story-chip {
  display: inline-block;
  background: var(--sc, #c1440e); color: #fff;
  font-size: 11px; font-weight: 800; letter-spacing: 0.12em;
  padding: 5px 12px; border-radius: 999px;
  margin-bottom: 10px;
}
#story-title { font-size: 1.45rem; font-weight: 800; letter-spacing: -0.01em; line-height: 1.15; }
#story-sub { margin-top: 4px; font-size: 0.86rem; font-weight: 600; color: #6e6e73; }
#story-bullets { list-style: none; margin-top: 14px; display: grid; gap: 8px; }
#story-bullets li {
  font-size: 0.88rem; line-height: 1.45; color: #3a3a3c;
  padding-left: 18px; position: relative;
}
#story-bullets li::before {
  content: '—'; position: absolute; left: 0; color: var(--sc, #c1440e); font-weight: 700;
}
#story-metric {
  margin-top: 16px; padding-top: 14px;
  border-top: 1px solid #ececea;
  display: flex; align-items: baseline; gap: 10px;
}
#story-metric-big { font-size: 2rem; font-weight: 800; color: var(--sc, #c1440e); letter-spacing: -0.02em; }
#story-metric-small { font-size: 0.8rem; color: #86868b; line-height: 1.3; }

@media (max-width: 700px) {
  #story {
    left: 12px; right: 12px; width: auto;
    top: calc(136px + env(safe-area-inset-top, 0px)); /* below the three stacked pills */
    transform: translateY(-10px);
  }
  #story.in { transform: translateY(0); }
  #story-card { padding: 16px 18px 14px; border-radius: 16px; }
  #story-title { font-size: 1.2rem; }
  #story-bullets { gap: 6px; margin-top: 10px; }
  #story-bullets li { font-size: 0.82rem; }
  #story-metric-big { font-size: 1.6rem; }
}

/* ---------- minimap + the big map ---------- */
#minimap-wrap {
  position: absolute; left: 18px; bottom: calc(18px + env(safe-area-inset-bottom, 0px));
  width: 176px; height: 176px; border-radius: 22px;
  border: 1px solid #e3e3e0; background: rgba(255,255,255,0.78);
  box-shadow: 0 10px 28px rgba(29,29,31,0.10);
  overflow: hidden; cursor: pointer; pointer-events: auto;
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
  transition: opacity 0.3s ease, transform 0.15s ease;
}
#minimap-wrap:hover { transform: scale(1.03); }
#minimap { width: 100%; height: 100%; display: block; }
#minimap-n {
  position: absolute; top: 6px; left: 50%; transform: translateX(-50%);
  font-size: 10px; font-weight: 800; letter-spacing: 0.1em; color: #6e6e73;
}
#minimap-expand { position: absolute; right: 9px; bottom: 6px; font-size: 12px; color: #86868b; }
#hud-places { pointer-events: auto; cursor: pointer; }
body.card-open #minimap-wrap, body.map-open #minimap-wrap, body.map-open #hud-hint { opacity: 0; pointer-events: none; }
@media (hover: none) and (pointer: coarse) {
  #minimap-wrap { left: auto; right: 18px; top: calc(66px + env(safe-area-inset-top, 0px)); bottom: auto; width: 104px; height: 104px; border-radius: 18px; }
  #minimap-expand { display: none; }
}
@media (max-width: 700px) and (hover: hover) {
  #minimap-wrap { width: 124px; height: 124px; border-radius: 18px; }
}

#bigmap {
  position: fixed; inset: 0; z-index: 30;
  background: rgba(238,243,246,0.72); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  display: flex; align-items: center; justify-content: center;
  padding: env(safe-area-inset-top, 0px) 0 env(safe-area-inset-bottom, 0px);
}
#bigmap[hidden] { display: none; }
#bigmap-card {
  display: flex; gap: 22px; padding: 18px;
  background: rgba(255,255,255,0.94); border: 1px solid #e3e3e0; border-radius: 26px;
  box-shadow: 0 24px 60px rgba(29,29,31,0.16);
  max-height: 94vh; max-width: 94vw;
}
#bigmap-map { height: min(84vh, 980px); width: calc(min(84vh, 980px) * 0.3512); display: flex; align-items: center; justify-content: center; }
#bigmap-canvas { border-radius: 14px; border: 1px solid #e8e6e0; background: #f1efe9; }
#bigmap-side { width: 260px; display: flex; flex-direction: column; min-height: 0; }
#bigmap-head { display: flex; justify-content: space-between; align-items: center; }
#bigmap-head h3 { font-size: 14px; letter-spacing: 0.16em; text-transform: uppercase; color: #1d1d1f; margin: 0; }
#bigmap-head h3 span { letter-spacing: 0; margin-left: 8px; text-transform: none; font-weight: 700; color: #b04a17; }
#bigmap-close {
  width: 32px; height: 32px; border-radius: 50%; border: 1px solid #e3e3e0; background: #fff;
  cursor: pointer; font-size: 18px; line-height: 1; color: #1d1d1f;
}
#bigmap-close:hover { background: #f4f3ef; }
#bigmap-stats { display: flex; gap: 6px; flex-wrap: wrap; margin: 12px 0 10px; }
#bigmap-stats span { background: #f4f3ef; border-radius: 999px; padding: 4px 10px; font-size: 11.5px; font-weight: 700; color: #6b4428; }
#bigmap-list { list-style: none; margin: 0; padding: 0 4px 0 0; overflow: auto; flex: 1; min-height: 0; font-size: 12.5px; }
#bigmap-list li { padding: 4px 0; color: #a1a1a6; display: flex; justify-content: space-between; gap: 8px; border-bottom: 1px dashed #efede8; }
#bigmap-list li.visited { color: #1d1d1f; font-weight: 600; }
#bigmap-list li.cv { color: #c1440e; font-weight: 700; }
#bigmap-list li .ar { color: #b04a17; font-weight: 700; font-size: 12px; }
#bigmap-tip { font-size: 11px; color: #86868b; margin: 10px 0 0; line-height: 1.4; }
@media (max-width: 760px) {
  #bigmap-card { flex-direction: column; gap: 10px; padding: 12px; overflow: auto; }
  #bigmap-map { height: min(58vh, 900px); width: calc(min(58vh, 900px) * 0.3512); margin: 0 auto; }
  #bigmap-side { width: min(84vw, 340px); }
  #bigmap-list { max-height: 20vh; }
}

/* ---------- philosopher chat ---------- */
#chat {
  position: fixed; z-index: 19;
  left: 50%; bottom: calc(64px + env(safe-area-inset-bottom, 0px));
  width: min(560px, 92vw);
  transform: translate(-50%, 16px);
  opacity: 0;
  transition: opacity 0.3s ease, transform 0.3s ease;
  pointer-events: none;
}
#chat.in { opacity: 1; transform: translate(-50%, 0); }
#chat-card {
  background: rgba(255,255,255,0.94);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid #e8e8e4;
  border-top: 5px solid var(--sc, #1d4f91);
  border-radius: 20px;
  padding: 16px 20px 16px;
  box-shadow: 0 18px 50px rgba(29,29,31,0.12);
}
#chat-who { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
#chat-name { font-size: 1.05rem; font-weight: 800; color: var(--sc, #1d4f91); }
#chat-era { font-size: 0.75rem; font-weight: 600; color: #86868b; }
#chat-line { margin-top: 8px; font-size: 0.95rem; line-height: 1.45; color: #1d1d1f; font-style: italic; }
#chat-options { margin-top: 12px; display: grid; gap: 8px; }
#chat-options button {
  pointer-events: auto;
  display: flex; align-items: center; gap: 10px;
  text-align: left;
  font: inherit; font-size: 0.88rem; font-weight: 600; color: #1d1d1f;
  background: #f5f5f3; border: 1px solid #e3e3df; border-radius: 12px;
  padding: 9px 12px;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}
#chat-options button:hover { background: #fff; border-color: var(--sc, #1d4f91); }
#chat-options kbd {
  flex: none;
  display: inline-grid; place-items: center;
  width: 22px; height: 22px; border-radius: 6px;
  background: var(--sc, #1d4f91); color: #fff;
  font: 800 12px/1 -apple-system, "Segoe UI", Helvetica, Arial, sans-serif;
}
@media (hover: none) and (pointer: coarse) { #chat-options kbd { display: none; } }
@media (max-width: 700px) {
  #chat { top: calc(136px + env(safe-area-inset-top, 0px)); bottom: auto; transform: translate(-50%, -10px); } /* below the three stacked pills */
  #chat.in { transform: translate(-50%, 0); }
  #chat-card { padding: 12px 14px; border-radius: 16px; }
  #chat-line { font-size: 0.86rem; }
  #chat-options button { font-size: 0.82rem; padding: 8px 10px; }
}

/* ---------- touch controls: WASD-style ---------- */
#touch-controls { position: fixed; inset: 0; z-index: 16; pointer-events: none; }
#touch-controls button {
  pointer-events: auto;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none; user-select: none;
  border: 1.5px solid rgba(29,29,31,0.15);
  background: rgba(255,255,255,0.72);
  color: #1d1d1f;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  font-weight: 800;
  transition: transform 0.06s ease, background 0.06s ease;
}
#touch-controls button.held { background: #c1440e; color: #fff; border-color: #c1440e; transform: scale(0.94); }
#pad-steer {
  position: absolute;
  left: calc(16px + env(safe-area-inset-left, 0px));
  bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  display: flex; gap: 14px;
}
#pad-steer button, #pad-pedals button {
  width: 76px; height: 76px; border-radius: 50%;
  font-size: 26px;
}
#pad-drive {
  position: absolute;
  right: calc(16px + env(safe-area-inset-right, 0px));
  bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  display: flex; align-items: flex-end; gap: 12px;
}
#pad-pedals { display: flex; flex-direction: column; gap: 12px; }
#pad-drift {
  width: 70px; height: 44px; border-radius: 999px;
  font-size: 12px; letter-spacing: 0.1em;
  color: #c1440e !important; border-color: rgba(193,68,14,0.4) !important;
  margin-bottom: 16px;
}
#pad-drift.held { color: #fff !important; }
@media (max-height: 500px) {
  /* phone in landscape */
  #pad-steer button, #pad-pedals button { width: 64px; height: 64px; font-size: 22px; }
  #pad-pedals { gap: 8px; }
}

/* ---------- ka'ak counter, toasts, Arabic ---------- */
#hud-left { display: flex; flex-direction: column; gap: 6px; }
#hud-collect { display: flex; gap: 6px; flex-wrap: wrap; }
#hud-kaak, #hud-maamoul {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; font-weight: 700; letter-spacing: 0.06em; color: #6b4428;
  background: rgba(255,255,255,0.8); border: 1px solid #ece3d3;
  padding: 4px 10px 4px 6px; border-radius: 999px;
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
  width: max-content;
}
.kaak-icon {
  width: 14px; height: 14px; border-radius: 50%;
  border: 4px solid #c98a45; box-sizing: border-box;
}
#toast {
  position: absolute; left: 50%; bottom: calc(70px + env(safe-area-inset-bottom, 0px));
  transform: translate(-50%, 10px);
  background: rgba(29,29,31,0.86); color: #fff;
  font-size: 13px; font-weight: 600; padding: 9px 16px; border-radius: 999px;
  opacity: 0; transition: opacity 0.3s ease, transform 0.3s ease;
  white-space: nowrap; max-width: 92vw; overflow: hidden; text-overflow: ellipsis;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#hud-zone .ar { letter-spacing: 0; margin-left: 10px; text-transform: none; font-weight: 700; }
@media (max-width: 700px) {
  #toast { bottom: calc(150px + env(safe-area-inset-bottom, 0px)); font-size: 12px; }
}

.maamoul-icon {
  width: 14px; height: 11px; border-radius: 50% 50% 35% 35% / 70% 70% 30% 30%;
  background: repeating-linear-gradient(90deg, #e2bd84 0 2px, #c9995a 2px 3px);
  box-shadow: inset 0 2px 0 rgba(255,255,255,0.8);
}
#hud-maamoul { color: #8a5a3a; }
