:root {
  --bg: var(--tg-theme-bg-color, #0d0f1a);
  --text: var(--tg-theme-text-color, #eef0ff);
  --hint: var(--tg-theme-hint-color, #9aa0c0);
  --accent: var(--tg-theme-button-color, #7c5cff);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --panel: rgba(16, 18, 34, 0.92);
  --radius: 16px;
}

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

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans Arabic", sans-serif;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

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

.hidden { display: none !important; }
.muted { color: var(--hint); font-size: 0.9rem; }

.icon { width: 24px; height: 24px; display: block; }
.icon-xl { width: 56px; height: 56px; margin: 0 auto 12px; color: var(--accent); }
.glow { filter: drop-shadow(0 0 12px var(--accent)); }

/* ---------- Overlays ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: center; justify-content: center;
  background: radial-gradient(ellipse at center, rgba(20, 16, 48, 0.85), rgba(8, 8, 20, 0.97));
  backdrop-filter: blur(6px);
  padding: 20px;
}

.panel {
  background: var(--panel);
  border: 1px solid rgba(124, 92, 255, 0.35);
  border-radius: var(--radius);
  padding: 28px 24px;
  max-width: 340px;
  width: 100%;
  text-align: center;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.6);
}
.panel.wide { max-width: 420px; }
.panel h2 { font-size: 1.25rem; margin-bottom: 10px; }
.panel p { line-height: 1.45; }

/* ---------- Buttons / inputs ---------- */
.btn {
  border: 0; border-radius: 12px; padding: 12px 20px;
  font-size: 1rem; font-weight: 600; cursor: pointer;
  background: rgba(255, 255, 255, 0.08); color: var(--text);
  transition: transform 0.1s ease, filter 0.15s ease;
}
.btn:active { transform: scale(0.96); }
.btn.primary { background: var(--accent); color: var(--accent-text); }

.row { display: flex; gap: 8px; margin-top: 14px; }
.row input {
  flex: 1; min-width: 0; border-radius: 12px; padding: 12px;
  border: 1px solid rgba(124, 92, 255, 0.35);
  background: rgba(255, 255, 255, 0.06); color: var(--text); font-size: 0.95rem;
  outline: none;
}
.row input:focus { border-color: var(--accent); }

/* ---------- Theme picker ---------- */
.theme-grid {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-top: 16px;
}
.theme-card {
  border: 1px solid rgba(124, 92, 255, 0.3); border-radius: 12px;
  padding: 14px 10px; cursor: pointer; text-align: center;
  background: linear-gradient(160deg, rgba(124, 92, 255, 0.12), rgba(20, 20, 40, 0.4));
  font-size: 0.92rem; font-weight: 600;
  transition: transform 0.1s ease, border-color 0.15s ease;
}
.theme-card:active { transform: scale(0.95); border-color: var(--accent); }
.theme-card .icon { margin: 0 auto 6px; color: var(--accent); }

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

.hud-top {
  position: absolute; top: calc(env(safe-area-inset-top, 0px) + 10px);
  left: 12px; right: 12px;
  display: flex; align-items: center; gap: 8px;
}
.hud-chip {
  background: rgba(10, 10, 26, 0.7); border: 1px solid rgba(124, 92, 255, 0.4);
  border-radius: 20px; padding: 6px 14px; font-size: 0.85rem; font-weight: 600;
  max-width: 55%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.hud-chip.small { font-size: 0.75rem; color: var(--hint); }
.hud-btn {
  margin-inline-start: auto;
  background: rgba(10, 10, 26, 0.7); border: 1px solid rgba(124, 92, 255, 0.4);
  border-radius: 50%; width: 40px; height: 40px; color: var(--text);
  display: flex; align-items: center; justify-content: center; cursor: pointer;
}

.hud-objective {
  position: absolute; top: calc(env(safe-area-inset-top, 0px) + 58px);
  left: 50%; transform: translateX(-50%);
  background: rgba(10, 10, 26, 0.55); border-radius: 14px;
  padding: 4px 14px; font-size: 0.8rem; color: var(--hint); white-space: nowrap;
}

/* ---------- Touch controls ---------- */
.joystick {
  position: absolute; bottom: calc(env(safe-area-inset-bottom, 0px) + 28px); left: 24px;
  width: 120px; height: 120px; border-radius: 50%;
  background: rgba(255, 255, 255, 0.06);
  border: 1.5px solid rgba(124, 92, 255, 0.35);
}
.joystick-knob {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  width: 52px; height: 52px; border-radius: 50%;
  background: rgba(124, 92, 255, 0.55);
  border: 1.5px solid rgba(255, 255, 255, 0.4);
}
.action-btn {
  position: absolute; bottom: calc(env(safe-area-inset-bottom, 0px) + 44px); right: 28px;
  width: 74px; height: 74px; border-radius: 50%;
  background: rgba(124, 92, 255, 0.4);
  border: 1.5px solid rgba(255, 255, 255, 0.35);
  color: #fff; display: flex; align-items: center; justify-content: center;
  cursor: pointer;
}
.action-btn:active { background: rgba(124, 92, 255, 0.7); }
.action-btn .icon { width: 30px; height: 30px; }

/* ---------- New HUD elements ---------- */
.hud-bars {
  position: absolute; top: calc(env(safe-area-inset-top, 0px) + 58px);
  left: 12px; width: 42%; max-width: 220px;
}
.hp-bar {
  height: 10px; border-radius: 6px; overflow: hidden;
  background: rgba(10, 10, 26, 0.65); border: 1px solid rgba(255, 90, 90, 0.4);
}
#hp-fill {
  height: 100%; width: 100%; border-radius: 6px;
  background: linear-gradient(90deg, #e0484f, #ff7a6b);
  transition: width 0.2s ease;
}
.hud-meta { display: flex; gap: 6px; margin-top: 6px; }
.meta-chip {
  display: inline-flex; align-items: center; gap: 4px;
  background: rgba(10, 10, 26, 0.65); border: 1px solid rgba(124, 92, 255, 0.35);
  border-radius: 14px; padding: 3px 10px; font-size: 0.78rem;
}
.icon-s { width: 14px; height: 14px; color: var(--accent); }

.hud-side {
  position: absolute; top: calc(env(safe-area-inset-top, 0px) + 110px);
  right: 12px; display: flex; flex-direction: column; gap: 10px;
}

.action-btn.attack {
  bottom: calc(env(safe-area-inset-bottom, 0px) + 132px);
  right: 44px;
  width: 64px; height: 64px;
  background: rgba(224, 72, 79, 0.45);
}
.action-btn.attack:active { background: rgba(224, 72, 79, 0.75); }

#vignette {
  position: fixed; inset: 0; z-index: 40; pointer-events: none; opacity: 0;
  background: radial-gradient(ellipse at center, transparent 55%, rgba(200, 30, 30, 0.55));
  transition: opacity 0.15s ease;
}
#vignette.hurt { opacity: 1; }

#toast {
  position: fixed; top: calc(env(safe-area-inset-top, 0px) + 150px);
  left: 50%; transform: translateX(-50%); z-index: 45;
  background: rgba(10, 10, 26, 0.85); border: 1px solid rgba(124, 92, 255, 0.5);
  border-radius: 14px; padding: 8px 18px; font-size: 0.85rem; font-weight: 600;
  pointer-events: none; white-space: nowrap;
}

/* ---------- Panels: character / build / studio ---------- */
.section-label {
  font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.08em;
  color: var(--hint); margin: 14px 0 8px; text-align: start;
}
.stat-rows { margin: 10px 0; }
.stat-row {
  display: flex; justify-content: space-between; padding: 6px 2px; font-size: 0.95rem;
}
.xp-bar {
  height: 8px; border-radius: 5px; overflow: hidden;
  background: rgba(255, 255, 255, 0.08); margin: 2px 0 6px;
}
#c-xpbar { height: 100%; background: var(--accent); width: 0; border-radius: 5px; }

.upgrade-card {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  border: 1px solid rgba(124, 92, 255, 0.3); border-radius: 12px;
  padding: 10px 12px; margin-top: 10px;
  background: rgba(255, 255, 255, 0.04);
}
.upgrade-info { display: flex; align-items: center; gap: 10px; text-align: start; }
.upgrade-info .icon { color: var(--accent); flex-shrink: 0; }
.upgrade-info small { display: block; color: var(--hint); font-size: 0.75rem; }
.btn.small { padding: 8px 12px; font-size: 0.85rem; white-space: nowrap; }
.btn.ghost { background: transparent; border: 1px solid rgba(124, 92, 255, 0.35); margin-top: 14px; }
.btn.danger { background: rgba(224, 72, 79, 0.25); }

textarea {
  width: 100%; border-radius: 12px; padding: 12px; resize: none;
  border: 1px solid rgba(124, 92, 255, 0.35);
  background: rgba(255, 255, 255, 0.06); color: var(--text); font-size: 0.95rem;
  font-family: inherit; outline: none; margin-top: 12px;
}
textarea:focus { border-color: var(--accent); }

.featured-list { display: flex; flex-direction: column; gap: 8px; }
.featured-card {
  border: 1px solid rgba(255, 200, 90, 0.45); border-radius: 12px;
  padding: 10px 14px; cursor: pointer; text-align: start;
  background: linear-gradient(160deg, rgba(255, 190, 80, 0.12), rgba(20, 20, 40, 0.4));
}
.featured-card b { display: block; font-size: 0.95rem; }
.featured-card small { color: var(--hint); font-size: 0.75rem; }
.featured-card .timer { color: #ffc85a; }

.studio-list { display: flex; flex-direction: column; gap: 8px; margin-top: 14px; max-height: 30vh; overflow-y: auto; }
.studio-item {
  display: flex; align-items: center; justify-content: space-between; gap: 6px;
  border: 1px solid rgba(124, 92, 255, 0.3); border-radius: 12px; padding: 8px 12px;
  background: rgba(255, 255, 255, 0.04); font-size: 0.85rem;
}
.studio-item .actions { display: flex; gap: 6px; }
.studio-item .actions button {
  border: 0; border-radius: 8px; padding: 6px 10px; cursor: pointer;
  font-size: 0.75rem; font-weight: 600; color: var(--text);
  background: rgba(124, 92, 255, 0.3);
}
.studio-item .actions button.danger { background: rgba(224, 72, 79, 0.35); }
.badge {
  font-size: 0.68rem; border-radius: 8px; padding: 2px 8px; font-weight: 700;
  background: rgba(90, 200, 120, 0.25); color: #7ee2a0;
}
.badge.draft { background: rgba(255, 200, 90, 0.2); color: #ffc85a; }

.build-card {
  border: 1px solid rgba(124, 92, 255, 0.3); border-radius: 12px;
  padding: 12px 8px; cursor: pointer; text-align: center;
  background: linear-gradient(160deg, rgba(124, 92, 255, 0.1), rgba(20, 20, 40, 0.4));
  font-size: 0.85rem; font-weight: 600;
}
.build-card:active { transform: scale(0.95); }
.build-card small { display: block; color: var(--hint); font-weight: 400; margin-top: 3px; }
.build-card.disabled { opacity: 0.45; }

[dir="rtl"] .action-btn.attack { right: auto; left: 44px; }
[dir="rtl"] .hud-bars { left: auto; right: 12px; }
[dir="rtl"] .hud-side { right: auto; left: 12px; }

/* ---------- Portal spinner ---------- */
.portal-spinner { position: relative; width: 72px; height: 72px; margin: 0 auto 18px; }
.portal-spinner div {
  position: absolute; inset: 0; border-radius: 50%;
  border: 3px solid transparent; border-top-color: var(--accent);
  animation: spin 1.2s linear infinite;
}
.portal-spinner div:nth-child(2) { inset: 10px; border-top-color: #4ecdc4; animation-duration: 0.9s; animation-direction: reverse; }
.portal-spinner div:nth-child(3) { inset: 20px; border-top-color: #ff6b9d; animation-duration: 0.7s; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- RTL ---------- */
[dir="rtl"] .hud-objective { direction: rtl; }
[dir="rtl"] .joystick { left: auto; right: 24px; }
[dir="rtl"] .action-btn { right: auto; left: 28px; }
