* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  width: 100%; height: 100%;
  overflow: hidden;
  font-family: "Segoe UI", Tahoma, Arial, sans-serif;
  background: linear-gradient(160deg, #7ec8ff 0%, #b8a4ff 50%, #ffb8e0 100%);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.screen { position: fixed; inset: 0; }
.hidden { display: none !important; }

/* ===== القائمة الرئيسية ===== */
#menu {
  overflow-y: auto;
  padding: 20px;
  text-align: center;
  touch-action: pan-y;
}

.title {
  font-size: clamp(28px, 6vw, 52px);
  color: #fff;
  text-shadow: 0 4px 0 rgba(0,0,0,0.15);
  margin: 10px 0 4px;
}

.subtitle {
  font-size: clamp(16px, 3vw, 22px);
  color: #fff;
  text-shadow: 0 2px 0 rgba(0,0,0,0.1);
  margin-bottom: 8px;
}

.gamepad-status {
  display: inline-block;
  background: rgba(255,255,255,0.35);
  border-radius: 30px;
  padding: 6px 18px;
  font-size: 15px;
  color: #333;
  margin-bottom: 18px;
}
.gamepad-status.connected { background: #b6ffb6; color: #1a6b1a; }

.game-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 16px;
  max-width: 900px;
  margin: 0 auto 40px;
}

.game-card {
  background: #fff;
  border: none;
  border-radius: 22px;
  padding: 18px 10px 14px;
  cursor: pointer;
  box-shadow: 0 6px 0 rgba(0,0,0,0.12);
  transition: transform 0.12s, box-shadow 0.12s;
  font-family: inherit;
}
.game-card:active, .game-card.focused {
  transform: translateY(4px) scale(0.98);
  box-shadow: 0 2px 0 rgba(0,0,0,0.12);
  outline: 4px solid #ffd400;
}
.game-card .emoji { font-size: 52px; display: block; }
.game-card .name { font-size: 18px; font-weight: bold; color: #444; margin-top: 8px; display: block; }
.game-card .desc { font-size: 12px; color: #999; margin-top: 4px; display: block; }

/* ===== الشريط العلوي ===== */
.top-bar {
  display: flex;
  gap: 10px;
  justify-content: flex-start;
  max-width: 900px;
  margin: 0 auto 6px;
}
.top-bar button {
  background: rgba(255,255,255,0.85);
  border: none;
  border-radius: 20px;
  padding: 8px 16px;
  font-size: 17px;
  font-weight: bold;
  color: #444;
  cursor: pointer;
  font-family: inherit;
  box-shadow: 0 3px 0 rgba(0,0,0,0.15);
}
.top-bar button:active { transform: translateY(2px); }
.top-bar button.off { opacity: 0.45; filter: grayscale(1); }

/* ===== شاشة اللاعبين ===== */
#player-overlay {
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(40, 20, 80, 0.6);
  z-index: 50;
}
#player-box {
  background: #fff;
  border-radius: 28px;
  padding: 24px 30px;
  text-align: center;
  box-shadow: 0 10px 40px rgba(0,0,0,0.4);
  animation: pop-in 0.3s ease-out;
  max-width: 92vw;
  width: 430px;
  max-height: 88vh;
  overflow-y: auto;
}
#player-box h2 { color: #444; margin-bottom: 14px; }
#player-list { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; margin-bottom: 16px; }
.player-item {
  background: #f0f0f8;
  border: 3px solid transparent;
  border-radius: 18px;
  padding: 10px 14px;
  font-size: 17px;
  font-weight: bold;
  color: #444;
  cursor: pointer;
  font-family: inherit;
  display: flex;
  align-items: center;
  gap: 6px;
}
.player-item.active { border-color: #ffb830; background: #fff4dd; }
.player-item .stars-mini { font-size: 12px; color: #f0a500; }
.player-item .del {
  font-size: 13px; color: #bbb; margin-right: 4px; cursor: pointer;
}
.player-item .del:hover { color: #e74c3c; }
#new-player-form {
  border-top: 2px dashed #ddd;
  padding-top: 14px;
  margin-bottom: 12px;
}
.avatar-row { display: flex; gap: 6px; justify-content: center; flex-wrap: wrap; margin-bottom: 10px; }
.avatar-row button {
  font-size: 26px;
  background: #f0f0f8;
  border: 3px solid transparent;
  border-radius: 14px;
  padding: 6px 9px;
  cursor: pointer;
}
.avatar-row button.selected { border-color: #5fb0f0; background: #e3f2ff; }
#player-name-input {
  width: 100%;
  border: 3px solid #d5d5e5;
  border-radius: 14px;
  padding: 10px 14px;
  font-size: 18px;
  font-family: inherit;
  text-align: center;
  margin-bottom: 10px;
  user-select: text;
  -webkit-user-select: text;
}
#player-name-input:focus { outline: none; border-color: #5fb0f0; }
#btn-add-player {
  background: linear-gradient(180deg, #9fd8ff, #5fb0f0);
  border: none;
  border-radius: 14px;
  padding: 10px 22px;
  font-size: 17px;
  font-weight: bold;
  color: #093a63;
  cursor: pointer;
  font-family: inherit;
  box-shadow: 0 3px 0 #3f86c4;
}
#btn-player-close {
  background: linear-gradient(180deg, #ffd95e, #ffb830);
  border: none;
  border-radius: 14px;
  padding: 10px 30px;
  font-size: 18px;
  font-weight: bold;
  color: #6b4500;
  cursor: pointer;
  font-family: inherit;
  box-shadow: 0 3px 0 #d99a1f;
}

/* ===== شاشة اختيار المستوى ===== */
#level-select {
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(40, 20, 80, 0.55);
  z-index: 40;
}
#level-box {
  background: #fff;
  border-radius: 28px;
  padding: 26px 34px;
  text-align: center;
  box-shadow: 0 10px 40px rgba(0,0,0,0.4);
  animation: pop-in 0.3s ease-out;
  max-width: 90vw;
}
#level-game-emoji { font-size: 60px; }
#level-game-name { font-size: 26px; color: #444; margin: 6px 0 16px; }
#level-grid {
  display: flex;
  gap: 14px;
  justify-content: center;
  flex-wrap: wrap;
  margin-bottom: 18px;
}
.level-btn {
  width: 86px; height: 96px;
  border: none;
  border-radius: 18px;
  background: linear-gradient(180deg, #9fd8ff, #5fb0f0);
  cursor: pointer;
  font-family: inherit;
  box-shadow: 0 4px 0 #3f86c4;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  transition: transform 0.1s;
}
.level-btn:active, .level-btn.focused { transform: translateY(3px); outline: 4px solid #ffd400; }
.level-btn .lv-num { font-size: 30px; font-weight: bold; color: #093a63; }
.level-btn .lv-stars { font-size: 13px; letter-spacing: 1px; }
.level-btn.locked {
  background: #d5d5dd;
  box-shadow: 0 4px 0 #aaaab4;
  cursor: not-allowed;
}
.level-btn.locked .lv-num { color: #888; }
#btn-level-back {
  background: linear-gradient(180deg, #ffd95e, #ffb830);
  border: none;
  border-radius: 16px;
  padding: 10px 26px;
  font-size: 18px;
  font-weight: bold;
  color: #6b4500;
  cursor: pointer;
  font-family: inherit;
  box-shadow: 0 4px 0 #d99a1f;
}
#btn-level-back:active { transform: translateY(3px); }
#end-stars { font-size: 34px; min-height: 40px; }
.card-stars { font-size: 12px; color: #f0a500; display: block; margin-top: 3px; }

/* ===== شاشة اللعب ===== */
#play-screen { background: #000; }
#game-canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }

#hud {
  position: absolute;
  top: 0; left: 0; right: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  pointer-events: none;
}
#hud > * { pointer-events: auto; }

#btn-back {
  background: rgba(255,255,255,0.9);
  border: none;
  border-radius: 14px;
  padding: 8px 14px;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  font-family: inherit;
  box-shadow: 0 3px 0 rgba(0,0,0,0.2);
}

#hud-score {
  background: rgba(255,255,255,0.9);
  border-radius: 14px;
  padding: 8px 16px;
  font-size: 18px;
  font-weight: bold;
  color: #e67e00;
  box-shadow: 0 3px 0 rgba(0,0,0,0.2);
}

#hud-info, #hud-time {
  background: rgba(255,255,255,0.9);
  border-radius: 14px;
  padding: 8px 16px;
  font-size: 18px;
  font-weight: bold;
  color: #444;
  box-shadow: 0 3px 0 rgba(0,0,0,0.2);
}
#hud-info:empty, #hud-time:empty { display: none; }
#hud-time.time-low { color: #d32f2f; animation: pulse 0.6s infinite alternate; }
@keyframes pulse { from { transform: scale(1); } to { transform: scale(1.1); } }

#big-message {
  position: absolute;
  top: 38%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: clamp(26px, 6vw, 46px);
  font-weight: bold;
  color: #fff;
  text-shadow: 0 4px 8px rgba(0,0,0,0.4);
  text-align: center;
  pointer-events: none;
  animation: pop-in 0.3s ease-out;
}
@keyframes pop-in {
  from { transform: translate(-50%, -50%) scale(0.3); opacity: 0; }
  to   { transform: translate(-50%, -50%) scale(1); opacity: 1; }
}

/* ===== نهاية اللعبة ===== */
#end-overlay {
  position: absolute; inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 30;
}
#end-box {
  background: #fff;
  border-radius: 28px;
  padding: 28px 40px;
  text-align: center;
  box-shadow: 0 10px 40px rgba(0,0,0,0.4);
  animation: pop-in 0.35s ease-out;
}
#end-emoji { font-size: 70px; }
#end-text { font-size: 30px; font-weight: bold; color: #444; margin: 8px 0; }
#end-score { font-size: 22px; color: #e67e00; font-weight: bold; margin-bottom: 18px; }
#end-box button {
  display: block;
  width: 100%;
  margin-top: 10px;
  background: linear-gradient(180deg, #ffd95e, #ffb830);
  border: none;
  border-radius: 16px;
  padding: 12px 24px;
  font-size: 19px;
  font-weight: bold;
  color: #6b4500;
  cursor: pointer;
  font-family: inherit;
  box-shadow: 0 4px 0 #d99a1f;
}
#end-box button:active { transform: translateY(3px); box-shadow: 0 1px 0 #d99a1f; }
#btn-menu { background: linear-gradient(180deg, #9fd8ff, #5fb0f0) !important; color: #093a63 !important; box-shadow: 0 4px 0 #3f86c4 !important; }

/* ===== التحكم باللمس ===== */
#touch-controls { position: absolute; inset: 0; pointer-events: none; z-index: 20; }

#joystick-zone {
  position: absolute;
  bottom: 0; right: 0;            /* RTL: العصا يمين الشاشة */
  width: 45%; height: 55%;
  pointer-events: auto;
}
#joystick-base {
  position: absolute;
  width: 130px; height: 130px;
  border-radius: 50%;
  background: rgba(255,255,255,0.25);
  border: 3px solid rgba(255,255,255,0.5);
  bottom: 30px; right: 30px;
  display: none;
}
#joystick-base.active { display: block; }
#joystick-knob {
  position: absolute;
  width: 56px; height: 56px;
  border-radius: 50%;
  background: rgba(255,255,255,0.85);
  box-shadow: 0 3px 8px rgba(0,0,0,0.3);
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
}

#action-btn {
  position: absolute;
  bottom: 50px; left: 36px;       /* RTL: زر الإجراء يسار الشاشة */
  width: 88px; height: 88px;
  border-radius: 50%;
  border: 4px solid rgba(255,255,255,0.6);
  background: rgba(255, 90, 90, 0.75);
  font-size: 32px;
  pointer-events: auto;
  cursor: pointer;
}
#action-btn:active { background: rgba(255, 60, 60, 0.95); transform: scale(0.92); }

#touch-controls.no-joystick #joystick-zone { display: none; }
#touch-controls.no-action #action-btn { display: none; }

/* مؤشر ذراع التحكم */
#gamepad-cursor {
  position: absolute;
  font-size: 44px;
  z-index: 25;
  pointer-events: none;
  transform: translate(-30%, -10%);
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.5));
}

/* ============================================================
   تحسينات شاشات الجوال: الحواف الآمنة (النوتش) + التجاوب
   ============================================================ */

/* احترام الحواف الآمنة (النوتش/الزوايا المنحنية) في الأجهزة الحديثة */
#menu {
  padding-top: max(20px, env(safe-area-inset-top));
  padding-left: max(20px, env(safe-area-inset-left));
  padding-right: max(20px, env(safe-area-inset-right));
  padding-bottom: max(20px, env(safe-area-inset-bottom));
}

/* الشريط العلوي للعب يبتعد عن النوتش */
#hud {
  padding-top: max(10px, env(safe-area-inset-top));
  padding-left: max(14px, env(safe-area-inset-left));
  padding-right: max(14px, env(safe-area-inset-right));
}

/* أزرار اللمس تبتعد عن حواف الشاشة الآمنة */
#joystick-base {
  bottom: max(30px, calc(env(safe-area-inset-bottom) + 14px));
  right: max(30px, calc(env(safe-area-inset-right) + 14px));
}
#action-btn {
  bottom: max(50px, calc(env(safe-area-inset-bottom) + 24px));
  left: max(36px, calc(env(safe-area-inset-left) + 18px));
}

/* ===== تلميح "أدر الهاتف أفقياً" ===== */
#rotate-hint {
  display: none;
  position: absolute;
  inset: 0;
  z-index: 60;
  background: linear-gradient(160deg, #2a2150, #4a2d6b);
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  text-align: center;
  padding: 24px;
}
#rotate-hint .rotate-icon {
  font-size: 90px;
  animation: rotate-phone 1.8s ease-in-out infinite;
}
#rotate-hint .rotate-text {
  font-size: 26px;
  font-weight: bold;
  color: #fff;
  text-shadow: 0 2px 6px rgba(0,0,0,0.4);
}
@keyframes rotate-phone {
  0%, 40% { transform: rotate(0deg); }
  60%, 100% { transform: rotate(90deg); }
}

/* يظهر التلميح فقط عند اللعب والهاتف عمودي (الألعاب مصممة للوضع الأفقي) */
@media (orientation: portrait) {
  #play-screen:not(.hidden) #rotate-hint { display: flex; }
}

/* ===== تجاوب الشاشات الصغيرة (هواتف صغيرة / وضع أفقي قصير) ===== */
@media (max-width: 700px) {
  .game-grid {
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    gap: 12px;
  }
  .game-card { padding: 14px 8px 12px; border-radius: 18px; }
  .game-card .emoji { font-size: 42px; }
  .game-card .name { font-size: 15px; margin-top: 6px; }
  .game-card .desc { font-size: 11px; }
}

/* الوضع الأفقي قصير الارتفاع: قائمة مدمجة + أزرار لمس أصغر */
@media (orientation: landscape) and (max-height: 480px) {
  .title { font-size: clamp(22px, 5vh, 32px); margin: 4px 0 2px; }
  .subtitle { font-size: clamp(13px, 3vh, 17px); margin-bottom: 4px; }
  .gamepad-status { margin-bottom: 8px; padding: 4px 14px; font-size: 13px; }
  .game-grid {
    grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
    gap: 10px;
  }
  .game-card { padding: 10px 6px 8px; }
  .game-card .emoji { font-size: 36px; }
  .game-card .name { font-size: 13px; margin-top: 4px; }
  .game-card .desc { display: none; }

  /* أزرار اللمس أصغر قليلاً لتوفير مساحة الرؤية */
  #joystick-base { width: 110px; height: 110px; }
  #joystick-knob { width: 48px; height: 48px; }
  #action-btn { width: 74px; height: 74px; font-size: 26px; }
}

/* شاشات صغيرة جداً: تصغير شريط معلومات اللعب */
@media (max-width: 480px) {
  #hud { gap: 8px; }
  #btn-back { padding: 6px 10px; font-size: 14px; }
  #hud-score, #hud-info, #hud-time { padding: 6px 10px; font-size: 15px; }
}
