:root {
  --red: #ef3340; --green: #0a8f4a; --green-d: #06552c; --black: #141414;
  --sand: #fff6e3; --gold: #f5b400; --gold-2: #ffd766; --sky-1: #bfe6ff; --sky-2: #eaf7ff;
  --ink: #1b2430; --ink-2: #5a6675; --line: #d7e2ea;
  --font: "Baloo Bhaijaan 2", "Segoe UI", system-ui, sans-serif;
  --safe-l: env(safe-area-inset-left, 0px); --safe-r: env(safe-area-inset-right, 0px);
  --safe-t: env(safe-area-inset-top, 0px); --safe-b: env(safe-area-inset-bottom, 0px);
  --pad: clamp(18px, 4vw, 44px);   /* هامش الصفحة */
  --gap: clamp(8px, 1.6vh, 16px);  /* المسافة بين العناصر */
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--sky-2); color: var(--ink); font-family: var(--font); }
body { -webkit-user-select: none; user-select: none; touch-action: manipulation; }
button, input, a { font-family: inherit; font-size: inherit; }
button { cursor: pointer; border: 0; background: none; color: inherit; }
[hidden] { display: none !important; }

/* ---------- الشاشات ---------- */
#app { position: relative; height: 100dvh; width: 100vw; }
.screen { position: absolute; inset: 0; display: none; }
.screen.active { display: block; }
.sky { position: absolute; inset: 0; z-index: 0; background: linear-gradient(180deg, var(--sky-1) 0%, var(--sky-2) 55%, var(--sand) 100%); }
.sky::before, .sky::after { content: ""; position: absolute; border-radius: 50%; background: rgba(255,255,255,.75); filter: blur(2px); }
.sky::before { width: 30vmax; height: 6vmax; top: 10%; left: 6%; box-shadow: 8vmax 2vmax 0 -1vmax rgba(255,255,255,.7); }
.sky::after { width: 22vmax; height: 5vmax; top: 24%; right: 10%; }

/* ---------- أزرار ---------- */
.big { background: var(--green); color: #fff; font-weight: 800; padding: 10px 26px; border-radius: 999px; font-size: clamp(16px, 2.6vmax, 22px); box-shadow: 0 5px 0 var(--green-d); transition: .1s; text-decoration: none; display: inline-block; text-align: center; line-height: 1.4; }
.big:active { transform: translateY(3px); box-shadow: 0 2px 0 var(--green-d); }
.big.alt { background: var(--red); box-shadow: 0 5px 0 #a81f2a; }
.big.alt:active { box-shadow: 0 2px 0 #a81f2a; }
.ghost { color: var(--ink-2); font-weight: 700; padding: 8px 10px; border-radius: 999px; font-size: clamp(13px, 2vmax, 17px); }
.ghost.small { font-size: clamp(12px, 1.7vmax, 14px); opacity: .8; }
.ghost:active { background: rgba(0,0,0,.06); }

/* ---------- شاشة اللغة ---------- */
.lang-wrap { position: relative; z-index: 1; height: 100%; display: grid; grid-template-rows: 1fr auto 1fr; justify-items: center; padding: var(--pad); text-align: center; }
.lang-flag { grid-row: 1; align-self: end; margin-bottom: var(--gap); }
.lang-body { grid-row: 2; display: flex; flex-direction: column; align-items: center; gap: var(--gap); }
.lang-foot { grid-row: 3; align-self: center; padding-top: 48px; }
/* حساب إنستقرام: أيقونة خطية بسيطة بالأسود بدون خلفية */
.ig { display: inline-flex; align-items: center; gap: 8px; height: 20px; line-height: 20px; color: #141414; text-decoration: none; font-weight: 700; font-size: 17px; opacity: .85; direction: ltr; }
.ig svg { width: 17px; height: 17px; display: block; }
.ig span { display: block; line-height: 1; transform: translateY(1px); }

.ig:active { opacity: .6; }
.done-wrap { padding-bottom: calc(var(--pad) + 36px); }
.done-foot { position: absolute; left: 0; right: 0; bottom: calc(var(--pad) * .6 + var(--safe-b)); display: flex; justify-content: center; z-index: 2; }
.lang-flag canvas { width: min(46vw, 34vh); height: auto; }
.bilingual { margin: 0; display: flex; flex-direction: column; gap: 2px; line-height: 1.15; }
.bilingual span:first-child { font-size: clamp(30px, 6vmax, 54px); }
.bilingual span:last-child { font-size: clamp(16px, 2.8vmax, 26px); color: var(--ink-2); font-weight: 700; }
.lang-buttons { display: flex; gap: 14px; margin-top: var(--gap); }
.lang-buttons .big { min-width: 140px; }

/* ---------- شاشة البداية / النهاية (بالعرض) ---------- */
.start-wrap, .done-wrap { position: relative; z-index: 1; height: 100%; display: grid; grid-template-columns: 1fr 1.15fr; align-items: center; gap: var(--pad); padding: var(--pad) max(var(--pad), var(--safe-r)) var(--pad) max(var(--pad), var(--safe-l)); }
.start-flag, .done-flag { display: grid; place-items: center; }
.start-flag canvas, .done-flag canvas { width: min(100%, 66vh); height: auto; }
.start-body, .done-body { display: flex; flex-direction: column; gap: var(--gap); min-width: 0; }
.start-body h1, .done-body h1 { font-size: clamp(30px, 6vmax, 54px); margin: 0; line-height: 1.1; }
.start-body .sub, .done-body p { margin: 0; color: var(--ink-2); font-size: clamp(13px, 2vmax, 18px); line-height: 1.5; }
.tally { display: flex; align-items: baseline; gap: 8px; color: var(--ink-2); font-weight: 700; font-size: clamp(13px, 2vmax, 17px); }
.tally b { color: var(--red); font-size: clamp(22px, 3.6vmax, 34px); font-weight: 800; line-height: 1; font-variant-numeric: tabular-nums; }
.name-row { display: flex; align-items: center; gap: 10px; font-weight: 700; }
.name-row span { white-space: nowrap; }
.name-row input { flex: 1; min-width: 0; padding: 8px 14px; border-radius: 14px; border: 2px solid var(--line); background: #fff; font-weight: 700; outline: none; }
.name-row input:focus { border-color: var(--gold); }
.levels { display: flex; gap: 8px; }
.level { flex: 1; background: #fff; border: 2px solid var(--line); border-radius: 16px; padding: 8px 6px; text-align: center; line-height: 1.2; transition: .15s; display: flex; flex-direction: column; justify-content: center; min-height: 58px; }
.level b { font-size: clamp(14px, 2.4vmax, 20px); }
.level small { font-size: clamp(10px, 1.5vmax, 12px); color: var(--ink-2); white-space: nowrap; text-wrap: balance; }
.level.active { border-color: var(--green); background: #e9f8ef; transform: translateY(-2px); }
.start-actions, .done-actions { display: flex; flex-wrap: wrap; gap: 8px 14px; align-items: center; }
.stars { font-size: clamp(26px, 5vmax, 44px); line-height: 1; letter-spacing: 4px; }
.stars i { font-style: normal; color: #d9dee3; transition: color .3s, transform .3s; display: inline-block; }
.stars i.on { color: var(--gold); transform: scale(1.15); text-shadow: 0 0 16px rgba(245,180,0,.6); }

/* ---------- شاشة اللعب ---------- */
#play { display: none; grid-template-rows: minmax(0, 1fr) auto; }
#play.active { display: grid; }
.top { position: relative; z-index: 1; display: grid; grid-template-columns: minmax(80px, 1fr) 2fr 1fr; min-height: 0; overflow: hidden; gap: 10px; padding: calc(var(--gap) + var(--safe-t)) max(var(--pad), var(--safe-r)) var(--gap) max(var(--pad), var(--safe-l)); }
.side { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.side-buttons { display: flex; flex-direction: column; gap: 6px; }
.chip { background: var(--green); color: #fff; font-weight: 800; padding: 2px 14px; border-radius: 999px; font-size: clamp(12px, 1.9vmax, 16px); white-space: nowrap; }
.counter { font-weight: 800; color: var(--ink-2); font-size: clamp(12px, 1.9vmax, 16px); }
.counter i { font-style: normal; opacity: .6; margin: 0 2px; }
.icon { display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 40px; width: clamp(54px, 8vmax, 76px); padding: 2px 0; border-radius: 14px; line-height: 1.1; }
.icon span { font-size: clamp(16px, 2.8vmax, 26px); }
.icon small { font-size: clamp(9px, 1.3vmax, 12px); font-weight: 700; color: var(--ink-2); white-space: nowrap; }
.icon:active { background: rgba(0,0,0,.06); }
.lyrics { display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; min-width: 0; padding: 0 8px; gap: 2px; }
.line { line-height: 1.45; max-width: 100%; }
.line.cur { font-size: clamp(22px, 6.4vmax, 54px); font-weight: 800; }
.line.next { font-size: clamp(13px, 2.3vmax, 20px); color: var(--ink-2); opacity: .7; font-weight: 700; }
.line.ar { font-size: clamp(14px, 2.6vmax, 24px); color: var(--ink-2); font-weight: 700; display: none; }
html[lang="en"] .line.ar { display: block; }
.line .s { color: #b8c2cc; transition: color .15s; }
.line .s.done { color: var(--ink); }
.line .s.now { color: var(--green); text-shadow: 0 0 14px rgba(245,180,0,.55); }
.flag-zone { position: relative; min-width: 0; }
.flag-zone canvas { position: absolute; inset: 0; width: 100%; height: 100%; }

/* ---------- البيانو ---------- */
.keyboard { position: relative; z-index: 2; height: clamp(150px, 44vh, 320px); margin: 0 max(calc(var(--pad) * .8), var(--safe-r)) calc(var(--pad) * .8 + var(--safe-b)) max(calc(var(--pad) * .8), var(--safe-l)); padding: 0 0 10px; background: #2b2b2b; border-top: 10px solid #1a1a1a; border-radius: 18px; box-shadow: 0 10px 30px rgba(0,0,0,.18); }
.whites { display: flex; height: 100%; gap: 3px; padding: 0 6px; }
.key { position: relative; flex: 1; height: 100%; background: linear-gradient(180deg, #fff 0%, #f1f1f1 85%, #d9d9d9 100%); border-radius: 0 0 9px 9px; box-shadow: inset 0 -5px 0 #cfcfcf; display: flex; align-items: flex-end; justify-content: center; padding-bottom: 10px; transition: transform .05s, background .1s; }
.key .lbl { font-weight: 800; color: #9aa4ae; font-size: clamp(11px, 2vmax, 18px); pointer-events: none; }
.key.pressed { transform: translateY(3px); background: #ececec; box-shadow: inset 0 -2px 0 #cfcfcf; }
.key.hit { background: linear-gradient(180deg, #fff6d6, #ffe59a); }
.key.hint { background: linear-gradient(180deg, #fff9e0 0%, var(--gold-2) 100%); box-shadow: inset 0 -5px 0 #e2a400, 0 0 24px rgba(245,180,0,.8); animation: glow .9s ease-in-out infinite; }
.key.hint .lbl { color: #7a5200; }
.key.wrong { animation: shake .25s; }
@keyframes glow { 0%, 100% { box-shadow: inset 0 -5px 0 #e2a400, 0 0 12px rgba(245,180,0,.5); } 50% { box-shadow: inset 0 -5px 0 #e2a400, 0 0 30px rgba(245,180,0,1); } }
@keyframes shake { 25% { transform: translateX(-3px); } 75% { transform: translateX(3px); } }
.blacks { position: absolute; inset: 0 6px auto 6px; height: 58%; pointer-events: none; }
.bkey { position: absolute; top: 0; width: 5.6%; height: 100%; background: linear-gradient(180deg, #333, #111); border-radius: 0 0 6px 6px; box-shadow: 0 4px 6px rgba(0,0,0,.5); pointer-events: auto; transform: translateX(-50%); }
.bkey.pressed { background: #000; height: 97%; }
.finger { position: absolute; top: 6%; font-size: clamp(28px, 6vmax, 54px); transform: translateX(-50%); pointer-events: none; opacity: 0; transition: left .2s, opacity .2s; animation: bob .7s ease-in-out infinite; filter: drop-shadow(0 4px 6px rgba(0,0,0,.3)); }
.finger.on { opacity: 1; }
@keyframes bob { 50% { margin-top: 10px; } }

/* ---------- التلميح ---------- */
#tip { position: absolute; z-index: 30; left: 50%; top: calc(var(--gap) + var(--safe-t) + 4px); transform: translateX(-50%); width: max-content; max-width: calc(100vw - 2 * var(--pad)); background: rgba(255,255,255,.65); color: var(--ink-2); border-radius: 999px; padding: 4px 14px; display: flex; align-items: center; gap: 6px; font-size: clamp(12px, 1.7vmax, 14px); font-weight: 700; backdrop-filter: blur(6px); transition: opacity .4s; }
#tip.fade { opacity: 0; }
#tip span { font-size: 14px; }
#tip p { margin: 0; min-width: 0; }

/* ---------- النافذة ---------- */
#modal { position: absolute; inset: 0; z-index: 40; background: rgba(10,20,30,.7); display: flex; align-items: center; justify-content: center; padding: 12px; }
.modal-card { position: relative; background: #fff; border-radius: 22px; padding: 14px; display: grid; grid-template-columns: auto 1fr; gap: 16px; align-items: center; max-width: 94vw; max-height: 94dvh; }
.modal-media { height: min(78dvh, 520px); aspect-ratio: 9 / 16; border-radius: 14px; overflow: hidden; background: #000; display: grid; place-items: center; }
.modal-media img, .modal-media video { width: 100%; height: 100%; object-fit: cover; display: block; }
.modal-actions { display: flex; flex-direction: column; gap: 10px; min-width: 150px; }
.modal-progress { position: absolute; inset: 0; display: grid; place-content: center; gap: 10px; background: rgba(255,255,255,.92); border-radius: 22px; font-weight: 800; text-align: center; }
.modal-progress div { width: 220px; height: 10px; background: #e6ecf1; border-radius: 99px; overflow: hidden; }
.modal-progress div i { display: block; height: 100%; width: 0; background: var(--green); transition: width .3s linear; }

/* ---------- آيفون بالعرض (ارتفاع قصير، مع شريط سفاري) ---------- */
@media (orientation: landscape) and (max-height: 440px) {
  :root { --pad: clamp(14px, 3vw, 28px); --gap: 6px; }
  .start-body .sub { display: none; }
  .tally { font-size: 13px; }
  .tally b { font-size: 22px; }
  .start-body h1, .done-body h1 { font-size: clamp(28px, 11vmin, 44px); margin-bottom: 4px; }
  .done-body p { margin-bottom: 4px; }
  .start-actions, .done-actions { margin-top: 4px; }
  .start-flag canvas, .done-flag canvas { width: min(100%, 58vh); }
  .stars { font-size: clamp(22px, 8vmin, 36px); }
  /* اللعب */
  .top { padding-top: calc(8px + var(--safe-t)); padding-bottom: 10px; gap: 6px; grid-template-columns: 26% 1fr 26%; }
  .side { gap: 4px; }
  .side-buttons { display: flex; flex-direction: row; flex-wrap: wrap; justify-content: center; gap: 4px; width: 92px; }
  .icon { width: 44px; min-height: 38px; }
  .icon span { font-size: 20px; }
  .icon small { display: none; }
  .chip { font-size: 12px; padding: 1px 10px; }
  .counter { font-size: 12px; }
  .line.cur { font-size: clamp(20px, 9.5vmin, 40px); line-height: 1.3; }
  .line.next { font-size: clamp(12px, 4.2vmin, 15px); }
  .line.ar { font-size: clamp(12px, 4vmin, 16px); }
  .keyboard { height: clamp(110px, 42dvh, 200px); margin-bottom: calc(6px + var(--safe-b)); border-top-width: 7px; padding-bottom: 6px; border-radius: 14px; }
  .key { padding-bottom: 5px; }
  .blacks { height: 52%; }
  .key .lbl { font-size: 12px; }
  .finger { font-size: 30px; top: 4%; }
}

/* ---------- الوضع الطولي ---------- */
@media (orientation: portrait) {
  .lang-flag canvas { width: min(70vw, 30vh); }
  .start-wrap, .done-wrap { grid-template-columns: 1fr; grid-template-rows: auto auto; align-content: center; gap: calc(var(--gap) * 2); padding-top: calc(var(--pad) + var(--safe-t) + 44px); }
  .start-flag canvas, .done-flag canvas { width: min(60vw, 26vh); }
  .start-body, .done-body { text-align: center; }
  .tally { justify-content: center; }
  .name-row { flex-direction: column; align-items: stretch; gap: 4px; }
  .start-actions, .done-actions { justify-content: center; }
  .level { min-height: 64px; padding: 8px 4px; }
  .level small { white-space: normal; font-size: 11px; }
  /* اللعب: شريط فوق، العلم والكلمات تحته، البيانو تحت */
  .top { grid-template-columns: 30% 1fr; grid-template-rows: auto 1fr; gap: 6px 10px; }
  .side { grid-column: 1 / -1; flex-direction: row; justify-content: space-between; }
  .side-buttons { flex-direction: row; }
  .icon { width: 50px; }
  .flag-zone { grid-row: 2; grid-column: 1; }
  .lyrics { grid-row: 2; grid-column: 2; }
  .line.cur { font-size: clamp(20px, 5.5vw, 30px); }
  .line.next { font-size: 13px; }
  .keyboard { height: 32vh; margin: 0 12px calc(12px + var(--safe-b)); }
  .key .lbl { font-size: 10px; }
  .whites { gap: 2px; padding: 0 3px; }
  .key { padding-bottom: 6px; }
  .modal-card { grid-template-columns: 1fr; justify-items: center; }
  .modal-media { height: min(60dvh, 480px); }
  .modal-actions { flex-direction: row; flex-wrap: wrap; justify-content: center; }
}
