:root {
  --bg-0: #07070d;
  --bg-1: #11111c;
  --bg-2: #1a1a2b;
  --accent: #ffcf5c;
  --accent-2: #5cd1ff;
  --danger: #ff5d6c;
  --good: #6cffa3;
  --text: #f3f3ff;
  --muted: #9da0c0;
  --border: rgba(255, 255, 255, 0.12);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  background: var(--bg-0);
  color: var(--text);
  font-family: 'Tajawal', 'Segoe UI', system-ui, -apple-system, sans-serif;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

#game-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  outline: none;
}

.overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at center, rgba(20, 20, 40, 0.82), rgba(0,0,0,0.95));
  backdrop-filter: blur(8px);
  padding: 16px;
}

.overlay.hidden, .hidden { display: none !important; }

.panel {
  width: min(440px, 96vw);
  max-height: 92vh;
  overflow-y: auto;
  background: linear-gradient(180deg, var(--bg-1), var(--bg-2));
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 28px 26px 22px;
  box-shadow: 0 30px 80px rgba(0,0,0,0.6), 0 2px 0 rgba(255,255,255,0.04) inset;
  text-align: center;
}

.panel.admin-panel {
  width: min(720px, 96vw);
}

.panel h1 {
  margin: 0 0 6px;
  font-size: 26px;
  letter-spacing: 0.5px;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.panel .lead {
  margin: 0 0 22px;
  color: var(--muted);
  line-height: 1.7;
  font-size: 14px;
}

#register-form { display: flex; flex-direction: column; gap: 12px; }

#register-form label {
  display: flex;
  flex-direction: column;
  text-align: right;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
}

#register-form input {
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: rgba(0,0,0,0.4);
  color: var(--text);
  font-size: 16px;
  text-align: right;
}

#register-form input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(255, 207, 92, 0.18);
}

button {
  margin-top: 6px;
  padding: 12px 18px;
  border-radius: 10px;
  border: 0;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  color: #0a0a14;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.12s ease, opacity 0.12s ease;
}
button:hover { transform: translateY(-1px); box-shadow: 0 8px 20px rgba(255, 207, 92, 0.25); }
button:disabled { opacity: 0.5; cursor: not-allowed; transform: none; box-shadow: none; }

.hint {
  font-size: 12px;
  color: var(--muted);
  margin: 6px 0 0;
  min-height: 16px;
}
.hint.error { color: var(--danger); }
.hint.ok { color: var(--good); }

/* Admin dashboard */
#admin-stats {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: center;
  margin-bottom: 14px;
}
.admin-stat {
  background: rgba(255,255,255,0.05);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 14px;
  font-size: 13px;
  color: var(--muted);
}
.admin-stat b { color: var(--text); font-size: 18px; display: block; }
#admin-list {
  text-align: right;
  max-height: 50vh;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 12px;
  margin-bottom: 12px;
}
.admin-row {
  display: grid;
  grid-template-columns: 1.4fr 1fr 1fr 1fr;
  gap: 8px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  align-items: center;
}
.admin-row.head { font-weight: 700; color: var(--muted); background: rgba(255,255,255,0.03); }
.admin-row:last-child { border-bottom: 0; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 6px; font-size: 11px; font-weight: 700; }
.badge.win { background: rgba(108, 255, 163, 0.16); color: var(--good); }
.badge.lose { background: rgba(255, 93, 108, 0.18); color: var(--danger); }
.badge.in-progress { background: rgba(92, 209, 255, 0.18); color: var(--accent-2); }

/* HUD */
#hud {
  position: fixed;
  inset: 0;
  z-index: 50;
  pointer-events: none;
  font-size: 14px;
}

#hud-top {
  position: absolute;
  top: 14px;
  left: 14px;
  right: 14px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

#player-info {
  background: rgba(10,10,20,0.65);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 8px 14px;
  pointer-events: auto;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.dot { color: var(--muted); }

#timer-wrap {
  background: rgba(255, 93, 108, 0.15);
  border: 1px solid rgba(255, 93, 108, 0.5);
  color: var(--danger);
  border-radius: 999px;
  padding: 8px 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  pointer-events: auto;
}
.timer-label { font-size: 11px; opacity: 0.85; }
#timer { font-size: 18px; font-weight: 700; letter-spacing: 1px; }
#timer.warn { color: #fff; background: var(--danger); padding: 2px 10px; border-radius: 6px; }

#live-equation {
  position: absolute;
  bottom: 140px; /* تم رفع المعادلة لأعلى */
    left: 50%;
  transform: translateX(-50%);
  /* Always read left-to-right regardless of the page's RTL direction. */
  direction: ltr;
  display: flex;
  flex-direction: row;
  gap: 12px;
  align-items: center;
  background: rgba(10,10,20,0.72);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 8px 16px;
  pointer-events: none;
  font-size: 22px;
  letter-spacing: 4px;
  max-width: 90vw;
  unicode-bidi: isolate;
}
.le-label {
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 0;
  /* Keep the Arabic label readable RTL even though the parent is LTR. */
  direction: rtl;
  unicode-bidi: isolate;
}
#le-tokens {
  display: inline-flex;
  flex-direction: row;
  gap: 6px;
  flex-wrap: wrap;
  font-weight: 700;
  direction: ltr;
  unicode-bidi: isolate;
}
#le-tokens .le-tok {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 6px;
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.12);
}
.ltr-eq {
  display: inline-block;
  direction: ltr;
  unicode-bidi: isolate;
  font-weight: 700;
  letter-spacing: 1px;
}
#le-tokens .le-tok small {
  display: block;
  font-size: 9px;
  color: var(--muted);
  letter-spacing: 0;
  font-weight: 400;
}

.prompt {
  position: absolute;
  bottom: 50%;
  left: 50%;
  transform: translate(-50%, 80px);
  background: rgba(10,10,20,0.85);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 14px;
  pointer-events: none;
  white-space: nowrap;
}
.prompt b { color: var(--accent); }

.big-notice {
  position: absolute;
  top: 22%;
  left: 50%;
  transform: translateX(-50%);
  background: linear-gradient(180deg, rgba(255,93,108,0.95), rgba(180,40,60,0.95));
  border: 2px solid #fff;
  color: #fff;
  border-radius: 14px;
  padding: 16px 22px;
  font-size: 18px;
  font-weight: 800;
  text-align: center;
  pointer-events: none;
  box-shadow: 0 18px 42px rgba(0,0,0,0.55);
  max-width: 86vw;
  line-height: 1.6;
  animation: bigNotice 0.6s ease;
}
@keyframes bigNotice {
  from { transform: translateX(-50%) scale(0.85); opacity: 0; }
  to   { transform: translateX(-50%) scale(1); opacity: 1; }
}

#crosshair {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  font-size: 18px;
  color: rgba(255,255,255,0.7);
  pointer-events: none;
}

/* Touch UI */
#touch-ui {
  position: absolute;
  inset: 0;
  pointer-events: none;
}
#joystick-zone {
  position: absolute;
  bottom: 28px;
  left: 28px; /* تم التغيير إلى اليسار */
    width: 140px;
   height: 140px;
  border-radius: 50%;
  background: rgba(20, 20, 30, 0.45);
  border: 2px solid rgba(255,255,255,0.18);
  pointer-events: auto;
  touch-action: none;
}
#joystick-thumb {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.55);
  transform: translate(-50%, -50%);
  pointer-events: none;
}
.touch-btn {
  position: absolute;
  bottom: 36px;
  width: 76px;
  height: 76px;
  padding: 0;
  border-radius: 50%;
  background: linear-gradient(180deg, rgba(255,255,255,0.95), rgba(220,220,240,0.95));
  color: #0a0a14;
  font-size: 22px;
  font-weight: 800;
  border: 2px solid rgba(255,255,255,0.5);
  pointer-events: auto;
  margin: 0;
}
#touch-interact { right: 36px; left: auto; } /* تم النقل لليمين */
#touch-jump { right: 130px; left: auto; bottom: 110px; } /* تم النقل لليمين */

/* Result overlay treasure styling */
.result-panel.win {
  background: linear-gradient(180deg, #2a210a, #4d3a0e 60%, #654a0f);
  border: 2px solid #ffcf5c;
  box-shadow: 0 0 60px rgba(255,207,92,0.4), 0 30px 80px rgba(0,0,0,0.6);
}
.result-panel.win h1 {
  font-size: 36px;
  -webkit-text-fill-color: transparent;
  background: linear-gradient(90deg, #ffe57a, #fff7c4, #ffcf5c);
  -webkit-background-clip: text;
}
.result-panel.lose {
  background: linear-gradient(180deg, #2a0a14, #4a0e1c);
  border: 2px solid #ff5d6c;
}

/* Underwater visual tint when player head is submerged */
body.submerged #game-canvas {
  filter: hue-rotate(-25deg) saturate(1.25) brightness(0.85);
}
body.submerged::after {
  content: '';
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(ellipse at 50% 30%, rgba(0,180,220,0.0) 30%, rgba(0,40,80,0.55) 100%),
    linear-gradient(180deg, rgba(0,80,140,0.18), rgba(0,30,60,0.32));
  z-index: 40;
}
body.submerged #crosshair { color: rgba(180,230,255,0.9); }

@media (max-width: 720px), (pointer: coarse) {
  .prompt { font-size: 13px; bottom: 55%; }
  #live-equation { font-size: 16px; padding: 6px 10px; bottom: 140px; } 
  #player-info { font-size: 12px; padding: 6px 10px; }
  #timer { font-size: 15px; }
  .big-notice { font-size: 15px; padding: 12px 16px; top: 16%; }
}
