*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #0b1020;
  --panel: #121a33;
  --panel-2: #0f1730;
  --text: #e7ecff;
  --muted: #8b96b8;
  --accent: #7c9cff;
  --accent-2: #a78bfa;
  --good: #34d399;
  --bad: #fb7185;
  --pinky: #f472b6;
  --ring: #fbbf24;
  --middle: #34d399;
  --index: #60a5fa;
  --thumb: #a78bfa;
  --ui-font: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  --arabic-font: "Noto Naskh Arabic", "Noto Sans Arabic", "KacstNaskh", "Segoe UI", Tahoma, sans-serif;
}

:root[data-lang="ar"] {
  --ui-font: var(--arabic-font);
}

html,
body {
  min-height: 100%;
}

body {
  font-family: var(--ui-font);
  color: var(--text);
  background-color: var(--bg);
  background-image:
    radial-gradient(900px 520px at 12% -12%, rgba(96, 130, 255, 0.22), transparent 60%),
    radial-gradient(820px 520px at 92% -4%, rgba(167, 139, 250, 0.18), transparent 55%),
    radial-gradient(760px 640px at 50% 112%, rgba(52, 211, 153, 0.12), transparent 60%);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
}

button {
  font-family: inherit;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.app {
  max-width: 1080px;
  margin: 0 auto;
  padding: 28px 20px 48px;
}

/* ---------- Top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 22px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 22px;
  font-weight: 800;
  letter-spacing: 0.4px;
}

.logo .dot {
  width: 13px;
  height: 13px;
  border-radius: 4px;
  background: linear-gradient(135deg, var(--accent), var(--thumb));
  box-shadow: 0 0 18px rgba(124, 156, 255, 0.85);
}

.logo .grad {
  background: linear-gradient(90deg, #9db4ff, #a78bfa 60%, #34d399);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.stats {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.lang-btn {
  padding: 9px 15px;
  border-radius: 999px;
  border: 1px solid rgba(124, 156, 255, 0.35);
  background: rgba(124, 156, 255, 0.12);
  color: #cfdaff;
  font-size: 13.5px;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.16s, background 0.16s, border-color 0.16s;
}

.lang-btn:hover {
  background: rgba(124, 156, 255, 0.22);
  border-color: rgba(124, 156, 255, 0.6);
  transform: translateY(-1px);
}

.stat {
  min-width: 80px;
  padding: 8px 12px;
  text-align: center;
  border-radius: 12px;
  background: rgba(18, 26, 51, 0.72);
  border: 1px solid rgba(255, 255, 255, 0.07);
  backdrop-filter: blur(8px);
}

.stat .val {
  font-size: 20px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1.15;
}

.stat .lbl {
  margin-top: 2px;
  font-size: 10px;
  letter-spacing: 1.2px;
  text-transform: uppercase;
  color: var(--muted);
}

/* ---------- Lesson tabs ---------- */

.lessons {
  display: flex;
  gap: 8px;
  margin-bottom: 18px;
  padding-bottom: 6px;
  overflow-x: auto;
  scrollbar-width: thin;
}

.lesson-btn {
  flex: 0 0 auto;
  padding: 9px 16px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(18, 26, 51, 0.6);
  color: var(--muted);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.18s, color 0.18s, border-color 0.18s, background 0.18s;
}

.lesson-btn:hover {
  color: var(--text);
  border-color: rgba(255, 255, 255, 0.22);
  transform: translateY(-1px);
}

.lesson-btn.active {
  color: #0b1020;
  background: linear-gradient(135deg, #9db4ff, #a78bfa);
  border-color: transparent;
  box-shadow: 0 6px 18px rgba(124, 156, 255, 0.35);
}

/* ---------- Practice card ---------- */

.card {
  margin-bottom: 18px;
  padding: 22px;
  border-radius: 20px;
  background: linear-gradient(180deg, rgba(20, 29, 56, 0.92), rgba(14, 21, 42, 0.92));
  border: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
}

.card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.card-title {
  font-size: 18px;
  font-weight: 700;
}

.card-tip {
  max-width: 720px;
  margin-top: 4px;
  color: var(--muted);
  font-size: 13.5px;
  line-height: 1.5;
}

.best {
  flex: 0 0 auto;
  padding: 5px 11px;
  border-radius: 999px;
  border: 1px solid rgba(124, 156, 255, 0.25);
  background: rgba(124, 156, 255, 0.1);
  color: #b9c8ff;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}

.text-wrap {
  position: relative;
  min-height: 130px;
  margin-bottom: 14px;
  padding: 22px 24px;
  border-radius: 16px;
  background: rgba(8, 13, 28, 0.72);
  border: 1px solid rgba(255, 255, 255, 0.06);
  cursor: text;
}

#text {
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  font-size: clamp(17px, 2.2vw, 22px);
  line-height: 1.85;
  letter-spacing: 0.3px;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  text-align: start;
  unicode-bidi: plaintext;
}

/* Arabic script must not use monospace or letter-spacing: they break joining. */
:root[data-lang="ar"] #text {
  font-family: var(--arabic-font);
  font-size: clamp(19px, 2.4vw, 25px);
  line-height: 2.05;
  letter-spacing: 0;
}

.char {
  transition: color 0.08s, background-color 0.08s;
}

.char.pending {
  color: #566180;
}

.char.ok {
  color: var(--text);
}

.char.bad {
  color: var(--bad);
  background: rgba(251, 113, 133, 0.16);
  border-radius: 4px;
  text-decoration: underline wavy rgba(251, 113, 133, 0.75);
  text-underline-offset: 4px;
}

.char.current {
  color: #ffffff;
  background: rgba(124, 156, 255, 0.22);
  border-radius: 5px;
  box-shadow: 0 0 0 1px rgba(124, 156, 255, 0.5);
  animation: pulse 1.3s ease-in-out infinite;
}

@keyframes pulse {
  0%,
  100% {
    box-shadow: 0 0 0 1px rgba(124, 156, 255, 0.45);
  }
  50% {
    box-shadow: 0 0 0 3px rgba(124, 156, 255, 0.16);
  }
}

.progress {
  height: 6px;
  margin-bottom: 14px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.07);
  overflow: hidden;
}

.progress .bar {
  width: 0;
  height: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, #7c9cff, #a78bfa);
  transition: width 0.15s ease;
}

.controls {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.btn {
  padding: 10px 18px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.16s, background 0.16s, box-shadow 0.16s;
}

.btn:hover {
  background: rgba(255, 255, 255, 0.1);
  transform: translateY(-1px);
}

.btn.primary {
  border-color: transparent;
  background: linear-gradient(135deg, #7c9cff, #a78bfa);
  color: #0b1020;
}

.btn.primary:hover {
  box-shadow: 0 8px 20px rgba(124, 156, 255, 0.35);
}

.hint {
  margin-inline-start: auto;
  color: var(--muted);
  font-size: 12.5px;
}

/* ---------- Virtual keyboard ---------- */

.keyboard {
  direction: ltr;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 14px;
  border-radius: 18px;
  background: linear-gradient(180deg, rgba(20, 29, 56, 0.9), rgba(12, 18, 36, 0.9));
  border: 1px solid rgba(255, 255, 255, 0.07);
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.35);
}

.krow {
  direction: ltr;
  display: flex;
  gap: 6px;
}

.key {
  position: relative;
  flex: 1 1 0;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 44px;
  border-radius: 9px;
  background: rgba(255, 255, 255, 0.045);
  border: 1px solid rgba(255, 255, 255, 0.07);
  border-bottom-width: 3px;
  color: #c7d0ec;
  font-size: 13px;
  font-weight: 600;
  user-select: none;
  transition: transform 0.05s, background 0.1s, box-shadow 0.15s, color 0.1s;
}

.key .alt {
  position: absolute;
  inset-block-start: 2px;
  inset-inline-start: 5px;
  font-size: 9px;
  font-weight: 500;
  color: var(--muted);
  line-height: 1;
}

:root[data-lang="ar"] .key {
  font-family: var(--arabic-font);
  font-size: 17px;
  line-height: 1;
}

:root[data-lang="ar"] .key.wide,
:root[data-lang="ar"] .key.space {
  font-family: var(--ui-font);
  font-size: 10px;
  letter-spacing: 0;
}

.key.wide {
  flex: 1.6 1 0;
  font-size: 10.5px;
  color: var(--muted);
  letter-spacing: 0.3px;
}

.key.space {
  flex: 1 1 0;
  max-width: 44%;
  margin: 0 auto;
  font-size: 10.5px;
  color: var(--muted);
}

.key.pressed {
  transform: translateY(2px);
  border-bottom-width: 1px;
  background: rgba(255, 255, 255, 0.15);
}

.key.next {
  background: rgba(124, 156, 255, 0.28);
  border-color: rgba(124, 156, 255, 0.8);
  color: #ffffff;
  box-shadow: 0 0 18px rgba(124, 156, 255, 0.45);
  transform: translateY(-1px);
}

.key.next.shift-hint {
  background: rgba(167, 139, 250, 0.3);
  border-color: rgba(167, 139, 250, 0.85);
  box-shadow: 0 0 18px rgba(167, 139, 250, 0.5);
}

.finger-lp,
.finger-rp {
  border-bottom-color: rgba(244, 114, 182, 0.75);
}

.finger-lr,
.finger-rr {
  border-bottom-color: rgba(251, 191, 36, 0.75);
}

.finger-lm,
.finger-rm {
  border-bottom-color: rgba(52, 211, 153, 0.75);
}

.finger-li,
.finger-ri {
  border-bottom-color: rgba(96, 165, 250, 0.75);
}

.finger-thumb {
  border-bottom-color: rgba(167, 139, 250, 0.75);
}

.legend {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 14px;
  margin-top: 12px;
  color: var(--muted);
  font-size: 11.5px;
}

.legend span {
  display: flex;
  align-items: center;
  gap: 6px;
}

.legend i {
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 3px;
}

.kbd-note {
  margin-top: 10px;
  color: var(--muted);
  font-size: 12px;
  text-align: center;
}

:root[data-lang="ar"] .stat .lbl {
  font-size: 11px;
  letter-spacing: 0;
  text-transform: none;
}

:root[data-lang="ar"] .card-tip {
  line-height: 1.75;
}

:root[dir="rtl"] .progress .bar {
  margin-inline-start: auto;
  background: linear-gradient(270deg, #7c9cff, #a78bfa);
}

/* ---------- Result modal ---------- */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(5, 8, 18, 0.72);
  backdrop-filter: blur(6px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}

.overlay.show {
  opacity: 1;
  pointer-events: auto;
}

.modal {
  width: min(440px, 100%);
  padding: 26px;
  border-radius: 22px;
  background: linear-gradient(180deg, #17203d, #0e1530);
  border: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.6);
  text-align: center;
  transform: translateY(10px) scale(0.98);
  transition: transform 0.2s;
}

.overlay.show .modal {
  transform: none;
}

.modal h2 {
  font-size: 20px;
  margin-bottom: 4px;
}

.modal > p {
  margin-bottom: 18px;
  color: var(--muted);
  font-size: 13.5px;
}

.result-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 20px;
}

.result-grid .stat {
  background: rgba(255, 255, 255, 0.04);
  min-width: 0;
}

.modal-actions {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 10px;
}

/* ---------- Mobile ---------- */

.hidden-input {
  position: fixed;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  padding: 0;
  border: 0;
  opacity: 0;
  font-size: 16px;
}

@media (max-width: 640px) {
  .app {
    padding: 18px 12px 40px;
  }

  .card {
    padding: 16px;
  }

  .text-wrap {
    padding: 16px;
    min-height: 110px;
  }

  .stats {
    width: 100%;
  }

  .stat {
    flex: 1;
    min-width: 0;
  }

  .hint {
    display: none;
  }

  .keyboard {
    gap: 4px;
    padding: 10px;
  }

  .krow {
    gap: 4px;
  }

  .key {
    height: 36px;
    border-radius: 7px;
    font-size: 11px;
  }

  .key.wide,
  .key.space {
    font-size: 8.5px;
  }

  :root[data-lang="ar"] .key {
    font-size: 15px;
  }

  .topbar-right {
    width: 100%;
  }

  .lang-btn {
    margin-inline-start: auto;
  }
}
