/* ============================================================
   مسرح الحبر — هوية ورقية مطبوعة
   ============================================================ */
:root{
  --cream:#F2E7CB;
  --cream-2:#EADCB8;
  --ink:#17352A;
  --ink-soft:rgba(23,53,42,.68);
  --sage:#4F7A4A;
  --sage-dark:#2E4B36;
  --copper:#C9A24B;
  --clay:#B2442F;
  --mint:#DCE6CE;
  --teal:#7FA8A0;
  --font-display:'Amiri', 'Times New Roman', serif;
  --font-ui:'Tajawal', system-ui, -apple-system, 'Segoe UI', sans-serif;
}

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

html,body{
  margin:0; padding:0;
  height:100%;
  background:#1B3A2C;
  overflow:hidden;
  font-family:var(--font-ui);
  color:var(--ink);
  -webkit-user-select:none; user-select:none;
  overscroll-behavior:none;
}

.game-root{
  position:relative;
  display:flex;
  flex-direction:column;
  height:100%;
  width:100%;
  background:var(--cream);
  overflow:hidden;
}

/* حبيبات الورق 8% */
.paper-grain{
  position:absolute; inset:0; z-index:60; pointer-events:none;
  opacity:.08;
  mix-blend-mode:multiply;
  background-image:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='140' height='140'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='140' height='140' filter='url(%23n)'/%3E%3C/svg%3E");
  background-size:140px 140px;
}

/* ================= HUD ================= */
.hud{
  display:flex;
  align-items:center;
  gap:6px;
  padding:5px 10px;
  background:var(--sage-dark);
  color:var(--cream);
  border-bottom:3px solid var(--ink);
  flex:0 0 auto;
  min-height:46px;
  font-family:var(--font-ui);
}
.hud-hearts{ display:flex; gap:2px; flex:0 0 auto; }
.heart{ width:17px; height:17px; display:block; }
.heart path{ fill:var(--clay); stroke:var(--ink); stroke-width:1.6; stroke-linejoin:round; }
.heart--empty path{ fill:rgba(242,231,203,.16); stroke:rgba(242,231,203,.55); }

.hud-wave{
  display:flex; align-items:baseline; gap:4px;
  background:var(--copper); color:var(--ink);
  border:2px solid var(--ink);
  border-radius:999px;
  padding:1px 9px 2px;
  font-weight:700;
  font-size:12px;
  flex:0 0 auto;
  white-space:nowrap;
}
.hud-wave-lbl{ font-size:10px; opacity:.85; }
.hud-wave-num{ font-family:var(--font-display); font-size:15px; line-height:1; }

.hud-coins{
  display:flex; align-items:center; gap:4px;
  font-weight:700; font-size:13px;
  flex:0 0 auto;
  padding:1px 7px;
  border-radius:999px;
  background:rgba(242,231,203,.14);
  border:2px solid rgba(242,231,203,.35);
}
.coin-dot{
  width:11px;height:11px;border-radius:50%;
  background:var(--copper);
  border:1.6px solid var(--ink);
  display:block;
}
.hud-score{
  margin-inline-start:auto;
  display:flex; align-items:baseline; gap:3px;
  font-weight:700; font-size:15px;
  color:var(--cream);
  white-space:nowrap;
}
.hud-score-lbl{ font-size:10px; opacity:.72; font-weight:400; }

/* ================= المسرح ================= */
.stage{
  position:relative;
  flex:1 1 auto;
  min-height:0;
  overflow:hidden;
  background:#E4D6B4;
}
.stage canvas{
  display:block;
  width:100%;
  height:100%;
  touch-action:none;
}

/* ================= لوحة التحكم ================= */
.dock{
  flex:0 0 auto;
  display:flex;
  align-items:center;
  justify-content:center;
  gap:18px;
  padding:6px 14px;
  background:var(--cream);
  border-top:3px solid var(--ink);
  min-height:64px;
}

.ability-btn{
  position:relative;
  width:68px; height:68px;
  border-radius:50%;
  border:3px solid var(--ink);
  background:var(--copper);
  color:var(--ink);
  display:flex;
  flex-direction:column;
  align-items:center;
  justify-content:center;
  gap:0;
  padding:0;
  cursor:pointer;
  touch-action:manipulation;
  box-shadow:0 3px 0 rgba(23,53,42,.5), inset 0 0 0 3px rgba(242,231,203,.45);
  transition:transform .1s ease, box-shadow .1s ease, background .18s ease;
  overflow:hidden;
}
.ability-btn:active{ transform:translateY(3px); box-shadow:0 0 0 rgba(23,53,42,.5), inset 0 0 0 3px rgba(242,231,203,.45); }
.ability-btn.ready{ background:var(--copper); animation:abilityPulse 1.7s ease-in-out infinite; }
@keyframes abilityPulse{
  0%,100%{ box-shadow:0 3px 0 rgba(23,53,42,.5), inset 0 0 0 3px rgba(242,231,203,.45), 0 0 0 0 rgba(201,162,75,.6); }
  50%{ box-shadow:0 3px 0 rgba(23,53,42,.5), inset 0 0 0 3px rgba(242,231,203,.45), 0 0 0 9px rgba(201,162,75,0); }
}
.ability-btn:disabled{ background:#A89A76; }
.ability-glyph{ display:block; width:26px; height:26px; }
.ability-glyph svg{ width:100%; height:100%; display:block; }
.ability-glyph path{ fill:var(--ink); stroke:var(--ink); stroke-width:1.1; stroke-linejoin:round; }
.ability-txt{ font-family:var(--font-display); font-weight:700; font-size:11px; line-height:1; margin-top:1px; }
.ability-cd{
  position:absolute; inset:0;
  border-radius:50%;
  pointer-events:none;
  background:conic-gradient(rgba(23,53,42,.55) var(--ang,0deg), rgba(23,53,42,0) 0);
}

.dock-pause{
  width:52px; height:52px;
  border-radius:14px 10px 15px 11px / 11px 15px 10px 14px;
  border:3px solid var(--ink);
  background:var(--mint);
  display:flex; align-items:center; justify-content:center;
  cursor:pointer;
  touch-action:manipulation;
  box-shadow:0 3px 0 rgba(23,53,42,.45);
  transition:transform .1s ease, box-shadow .1s ease;
}
.dock-pause:active{ transform:translateY(3px); box-shadow:0 0 0 rgba(23,53,42,.45); }
.dock-pause svg{ width:20px; height:20px; }
.dock-pause rect{ fill:var(--ink); }

/* ================= الأزرار والبطاقات الورقية ================= */
.btn{
  font-family:var(--font-display);
  font-weight:700;
  font-size:17px;
  line-height:1.2;
  color:var(--ink);
  background:var(--copper);
  border:3px solid var(--ink);
  border-radius:14px 9px 15px 11px / 11px 15px 9px 14px;
  padding:11px 20px;
  min-height:48px;
  cursor:pointer;
  box-shadow:0 3px 0 rgba(23,53,42,.5);
  transition:transform .12s ease, box-shadow .12s ease, background .16s ease;
  touch-action:manipulation;
  position:relative;
}
.btn:hover{ background:#D6AF58; }
.btn:active{ transform:translateY(3px); box-shadow:0 0 0 rgba(23,53,42,.5); }
.btn:focus-visible{ outline:3px solid var(--clay); outline-offset:3px; }
.btn--big{ font-size:21px; padding:13px 30px; min-height:56px; width:100%; }
.btn--ghost{
  background:var(--mint);
  font-size:15px;
  min-height:46px;
}
.btn--ghost:hover{ background:#E7EEDC; }
.btn:disabled{ opacity:.45; cursor:not-allowed; transform:none; box-shadow:0 3px 0 rgba(23,53,42,.3); }

/* ================= الطبقات العلوية ================= */
.overlay{
  position:absolute; inset:0; z-index:40;
  display:flex; align-items:center; justify-content:center;
  padding:16px;
  overflow:auto;
  background:rgba(23,53,42,.55);
  animation:fadeIn .18s ease-out;
}
.overlay.is-hidden{ display:none !important; }
@keyframes fadeIn{ from{opacity:0} to{opacity:1} }

.panel{
  position:relative;
  width:100%;
  max-width:420px;
  max-height:100%;
  overflow:auto;
  padding:22px 20px 20px;
  color:var(--ink);
  background:var(--cream);
  border:3px solid var(--ink);
  border-radius:20px 13px 22px 15px / 15px 22px 13px 20px;
  box-shadow:5px 6px 0 rgba(23,53,42,.38);
  animation:popIn .22s cubic-bezier(.2,1.4,.5,1);
}
@keyframes popIn{ from{transform:scale(.94) translateY(8px); opacity:0} to{transform:none; opacity:1} }
.panel--wide{ max-width:560px; }
.panel--narrow{ max-width:360px; }
.panel--title{ max-width:430px; text-align:center; }

/* مسامير نحاسية */
.panel::after{
  content:''; position:absolute; inset:5px; pointer-events:none; border-radius: inherit;
  background-image:
    radial-gradient(circle at 7px 7px, var(--copper) 2.6px, transparent 3.2px),
    radial-gradient(circle at calc(100% - 7px) 7px, var(--copper) 2.6px, transparent 3.2px),
    radial-gradient(circle at 7px calc(100% - 7px), var(--copper) 2.6px, transparent 3.2px),
    radial-gradient(circle at calc(100% - 7px) calc(100% - 7px), var(--copper) 2.6px, transparent 3.2px);
  background-repeat:no-repeat;
  opacity:.75;
}

.kicker{
  margin:0 0 6px;
  font-family:var(--font-ui);
  font-size:12px;
  letter-spacing:0;
  color:var(--sage);
  font-weight:500;
}
.title{
  margin:0 0 10px;
  font-family:var(--font-display);
  font-weight:700;
  font-size:40px;
  line-height:1.1;
  color:var(--ink);
  text-shadow:2px 2px 0 rgba(201,162,75,.45);
}
.panel-title{
  margin:0 0 6px;
  font-family:var(--font-display);
  font-weight:700;
  font-size:26px;
  line-height:1.2;
  color:var(--ink);
}
.panel-sub{
  margin:0 0 16px;
  font-size:13.5px;
  line-height:1.65;
  color:var(--ink-soft);
}
.lead{
  margin:0 0 18px;
  font-size:14.5px;
  line-height:1.7;
  color:var(--ink-soft);
}
.best-line{ margin:14px 0 0; font-size:12.5px; color:var(--sage); font-weight:500; }
.best-line span{ font-family:var(--font-display); font-weight:700; font-size:16px; color:var(--ink); }
.hint-line{ margin:8px 0 0; font-size:11.5px; line-height:1.6; color:rgba(23,53,42,.5); }
.title-actions{ display:flex; gap:10px; }
.stack{ display:flex; flex-direction:column; gap:9px; }

/* ================= بطاقات الشخصيات ================= */
.char-grid{
  display:grid;
  grid-template-columns:repeat(auto-fit, minmax(148px, 1fr));
  gap:10px;
  margin-bottom:14px;
}
.char-card{
  display:block;
  text-align:center;
  cursor:pointer;
  padding:8px 8px 10px;
  background:var(--mint);
  color:var(--ink);
  border:2.5px solid var(--ink);
  border-radius:14px 10px 15px 11px / 11px 15px 10px 14px;
  box-shadow:0 3px 0 rgba(23,53,42,.34);
  transition:transform .12s ease, box-shadow .12s ease, background .16s ease;
  font-family:var(--font-ui);
  min-height:44px;
}
.char-card:hover{ background:#E8EFDD; transform:translateY(-2px); }
.char-card:active{ transform:translateY(2px); box-shadow:0 0 0 rgba(23,53,42,.34); }
.char-card:focus-visible{ outline:3px solid var(--clay); outline-offset:2px; }
.char-img{
  width:100%; aspect-ratio:1/1; object-fit:contain; display:block;
  margin:0 auto 2px; pointer-events:none;
  filter:drop-shadow(0 3px 0 rgba(23,53,42,.18));
}
.char-name{
  font-family:var(--font-display);
  font-weight:700;
  font-size:19px;
  line-height:1.15;
  margin-bottom:1px;
}
.char-sub{ font-size:10.5px; color:var(--sage); margin-bottom:6px; font-weight:500; }
.char-line{
  font-size:10.5px;
  line-height:1.5;
  color:var(--ink-soft);
  text-align:start;
  margin-bottom:2px;
}
.char-line b{ color:var(--ink); font-weight:700; }
.char-stats{ margin-top:6px; display:flex; flex-direction:column; gap:3px; }
.stat{ display:flex; align-items:center; gap:5px; }
.stat span{ font-size:9.5px; width:26px; text-align:start; color:var(--sage); font-weight:700; }
.bar{
  flex:1; height:6px; border-radius:3px;
  background:rgba(23,53,42,.14);
  border:1.4px solid var(--ink);
  overflow:hidden;
}
.bar i{
  display:block; height:100%;
  background:var(--copper);
  border-inline-end:1.5px solid var(--ink);
}

/* ================= الترقيات ================= */
.upg-grid{
  display:grid;
  grid-template-columns:repeat(auto-fit, minmax(130px, 1fr));
  gap:10px;
  margin-bottom:14px;
}
.upg-card{
  position:relative;
  text-align:center;
  padding:12px 10px 12px;
  background:var(--mint);
  color:var(--ink);
  border:2.5px solid var(--ink);
  border-radius:14px 10px 15px 11px / 11px 15px 10px 14px;
  box-shadow:0 3px 0 rgba(23,53,42,.34);
  cursor:pointer;
  font-family:var(--font-ui);
  transition:transform .12s ease, box-shadow .12s ease, background .16s ease, opacity .16s;
  min-height:44px;
}
.upg-card:hover:not(:disabled){ background:#E8EFDD; transform:translateY(-2px); }
.upg-card:active:not(:disabled){ transform:translateY(2px); box-shadow:0 0 0 rgba(23,53,42,.34); }
.upg-card:disabled{ opacity:.42; cursor:not-allowed; }
.upg-card:focus-visible{ outline:3px solid var(--clay); outline-offset:2px; }
.upg-ico{ width:26px; height:26px; margin:0 auto 6px; }
.upg-ico svg{ width:100%; height:100%; }
.upg-ico path{ fill:none; stroke:var(--ink); stroke-width:2; stroke-linecap:round; stroke-linejoin:round; }
.upg-name{ font-family:var(--font-display); font-weight:700; font-size:17px; line-height:1.2; }
.upg-desc{ font-size:11px; line-height:1.5; color:var(--ink-soft); margin-top:3px; }
.upg-cost{
  display:inline-flex; align-items:center; gap:4px;
  margin-top:8px;
  padding:2px 9px;
  border-radius:999px;
  background:var(--copper);
  border:2px solid var(--ink);
  font-weight:700;
  font-size:12px;
}
.upg-cost.poor{ background:#C9BFA2; }

/* ================= نهاية الجولة ================= */
.over-stats{ display:flex; gap:8px; margin:14px 0 10px; }
.stat-box{
  flex:1;
  text-align:center;
  background:var(--mint);
  border:2.5px solid var(--ink);
  border-radius:12px 9px 13px 10px / 10px 13px 9px 12px;
  padding:8px 4px 9px;
}
.stat-box span{ display:block; font-size:10.5px; color:var(--sage); font-weight:500; }
.stat-box b{ display:block; font-family:var(--font-display); font-size:22px; line-height:1.2; }
.over-hero{
  margin:0 0 14px;
  text-align:center;
  font-size:12.5px;
  color:var(--ink-soft);
}

/* ================= الشاشات القصيرة ================= */
@media (max-height:560px){
  .title{ font-size:30px; }
  .panel{ padding:16px 16px 14px; }
  .lead{ font-size:13px; margin-bottom:12px; }
  .char-cv{ width:54px; height:54px; }
  .char-name{ font-size:16px; }
  .hint-line{ display:none; }
}
@media (max-height:420px){
  .panel-title{ font-size:20px; }
  .panel-sub{ margin-bottom:10px; font-size:12px; }
  .char-line{ display:none; }
  .char-grid{ grid-template-columns:repeat(4,1fr); }
  .char-cv{ width:44px; height:44px; }
  .char-stats{ display:none; }
}
@media (max-width:420px){
  .title{ font-size:34px; }
  .hud{ font-size:12px; }
  .heart{ width:15px; height:15px; }
  .hud-wave{ font-size:11px; padding:1px 7px 2px; }
  .hud-wave-num{ font-size:13px; }
  .hud-score{ font-size:13px; }
  .hud-coins{ font-size:12px; padding:1px 6px; }
}

/* ================= طبقة المسرح ثلاثية الأبعاد ================= */
.stage{ position:relative; }
.stage canvas#game{ position:absolute; inset:0; width:100%; height:100%; display:block; touch-action:none; }

.vignette-3d{
  position:absolute; inset:0; pointer-events:none; z-index:2;
  background:radial-gradient(ellipse at 50% 55%, rgba(0,0,0,0) 45%, rgba(6,14,10,.55) 100%);
}

.wave-banner{
  position:absolute; z-index:5; inset-inline:0; top:9%;
  display:flex; align-items:center; justify-content:center;
  pointer-events:none; opacity:0; transform:translateY(-14px) scale(.94);
  transition:opacity .28s ease, transform .28s cubic-bezier(.2,1.5,.5,1);
}
.wave-banner.show{ opacity:1; transform:none; }
.wave-banner span{
  font-family:var(--font-display); font-weight:700; font-size:22px;
  color:var(--cream); background:var(--sage-dark);
  border:3px solid var(--ink); border-radius:14px 9px 15px 11px / 11px 15px 9px 14px;
  padding:7px 22px; box-shadow:0 4px 0 rgba(23,53,42,.55);
  white-space:nowrap; max-width:88vw; overflow:hidden; text-overflow:ellipsis;
}
.wave-banner.boss span{ background:var(--clay); color:var(--cream); }

.boss-bar{
  position:absolute; z-index:5; inset-inline:0; top:10px;
  display:flex; flex-direction:column; align-items:center; gap:3px;
  pointer-events:none; padding:0 22px;
}
.boss-bar.is-hidden{ display:none !important; }
.boss-bar b{ font-size:10.5px; letter-spacing:.04em; color:var(--cream); text-shadow:0 1px 0 rgba(23,53,42,.9); font-weight:700; }
.boss-bar span{
  display:block; width:100%; max-width:280px; height:9px;
  background:rgba(23,53,42,.55); border:2px solid var(--ink); border-radius:999px; overflow:hidden;
}
.boss-bar i{ display:block; height:100%; width:100%; background:var(--clay); border-inline-end:2px solid var(--ink); transition:width .12s linear; }

.hurt-flash{
  position:absolute; inset:0; z-index:4; pointer-events:none; opacity:0;
  background:radial-gradient(ellipse at 50% 50%, rgba(178,68,47,0) 38%, rgba(178,68,47,.55) 100%);
  transition:opacity .16s ease-out;
}
.hurt-flash.on{ opacity:1; transition:opacity .04s ease-out; }

.title-3d{
  font-family:var(--font-ui); font-weight:700; font-size:20px;
  color:var(--sage); vertical-align:super;
  background:var(--copper); border:2.5px solid var(--ink);
  border-radius:8px; padding:1px 7px; margin-inline-start:4px;
  text-shadow:none;
}
.select-status{
  margin:0 0 10px; min-height:16px; text-align:center;
  font-size:12.5px; color:var(--sage); font-weight:500;
}
.char-card.is-busy{ opacity:.55; pointer-events:none; }

@media (max-height:560px){
  .char-img{ width:100%; }
  .wave-banner span{ font-size:17px; padding:5px 16px; }
}
@media (max-height:420px){
  .char-img{ max-height:52px; }
  .wave-banner{ top:4%; }
}

/* ================= الستارة بين الموجات ================= */
.curtain{
  position:absolute; inset:0; z-index:7;
  pointer-events:none; overflow:hidden;
}
.curtain > i{
  position:absolute; top:0; bottom:0; width:51%;
  --cd:620ms;
  transform:translateX(0);
  transition:transform var(--cd) cubic-bezier(.62,.02,.34,1);
  background:
    repeating-linear-gradient(90deg,
      rgba(0,0,0,.34) 0 9px,
      rgba(0,0,0,0) 9px 24px,
      rgba(150,185,155,.18) 24px 33px,
      rgba(0,0,0,.20) 33px 48px),
    linear-gradient(180deg, #35563D 0%, #22392B 62%, #16281E 100%);
  box-shadow:inset 0 -28px 56px rgba(0,0,0,.42);
  will-change:transform;
}
.curtain > i:first-child{ left:0; }
.curtain > i:last-child{ right:0; }
.curtain.is-open > i:first-child{ transform:translateX(-103%); }
.curtain.is-open > i:last-child{ transform:translateX(103%); }

/* ================= العدّ التنازلي ================= */
.countdown{
  position:absolute; inset:0; z-index:6;
  display:flex; align-items:center; justify-content:center;
  pointer-events:none; opacity:0; transition:opacity .16s ease;
}
.countdown.show{ opacity:1; }
.countdown b{
  font-family:var(--font-display);
  font-weight:700; font-size:clamp(64px, 26vw, 148px); line-height:.9;
  color:var(--cream);
  -webkit-text-stroke:3px var(--ink);
  paint-order:stroke fill;
  text-shadow:4px 6px 0 rgba(23,53,42,.55);
}
.countdown.pop b{ animation:countPop .5s cubic-bezier(.2,1.7,.4,1); }
@keyframes countPop{
  0%{ transform:scale(1.6); opacity:.15; }
  55%{ transform:scale(.94); opacity:1; }
  100%{ transform:scale(1); opacity:1; }
}

@media (prefers-reduced-motion:reduce){
  *{ animation-duration:.001ms !important; animation-iteration-count:1 !important; transition-duration:.001ms !important; }
}