:root {
  --page-bg: linear-gradient(180deg, #eef6fb 0%, #f8fbfd 100%);
  --panel-bg: #ffffff;
  --border: #d4dbe3;
  --border-strong: #b8c6d3;
  --text: #1f2a35;
  --muted: #667482;
  --blue: #4d88d8;
  --blue-dark: #356fbe;
  --blue-soft: #9fd3ec;
  --blue-pale: #d8edf8;
  --soft-blue: #eef7fd;
  --green: #2f8a4c;
  --red: #c84545;
  --shadow: 0 10px 24px rgba(48, 93, 129, .12);
}

* { box-sizing: border-box; }
html { min-height: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  background: var(--page-bg);
  color: var(--text);
  font-family: Arial, Tahoma, "Segoe UI", sans-serif;
  line-height: 1.5;
}
button, select, textarea { font: inherit; }
button, select { cursor: pointer; }

.page-shell {
  width: min(100% - 24px, 1100px);
  margin: 18px auto 30px;
  background: rgba(255,255,255,.72);
  backdrop-filter: blur(4px);
  border: 1px solid rgba(184,198,211,.6);
  border-radius: 24px;
  box-shadow: 0 18px 40px rgba(67, 119, 160, .15);
  overflow: hidden;
}

.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 22px;
  border-bottom: 1px solid rgba(184,198,211,.7);
  background: linear-gradient(180deg, rgba(255,255,255,.95), rgba(247,251,255,.95));
}
.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--text);
  text-decoration: none;
}
.brand-mark {
  width: 46px;
  height: 46px;
  display: grid;
  place-items: center;
  background: linear-gradient(180deg, #63a3f1, #3f80d3);
  color: #fff;
  border-radius: 12px;
  font-size: 22px;
  box-shadow: 0 8px 18px rgba(63,128,211,.25);
}
.brand strong, .brand small { display: block; }
.brand strong { font-size: 18px; }
.brand small { color: var(--muted); font-size: 12px; }

.language-switch {
  display: flex;
  border: 1px solid rgba(150,176,203,.9);
  border-radius: 999px;
  overflow: hidden;
  background: #fff;
}
.language-button {
  border: 0;
  background: #fff;
  padding: 10px 16px;
  color: #385168;
  font-weight: 700;
}
.language-button + .language-button { border-left: 1px solid rgba(150,176,203,.9); }
.language-button.active {
  background: linear-gradient(180deg, #5f97e3, #437fd2);
  color: #fff;
}
html[dir="rtl"] .language-button + .language-button {
  border-left: 0;
  border-right: 1px solid rgba(150,176,203,.9);
}

.intro-panel {
  display: grid;
  row-gap: 6px;
  justify-items: end;
  align-content: start;
  text-align: end;
  padding: 18px 22px 14px;
}
html[dir="ltr"] .intro-panel {
  justify-items: start;
  text-align: start;
}
.intro-panel h1 {
  margin: 0;
  max-width: min(100%, 700px);
  color: #244b74;
  font-size: clamp(24px, 4vw, 34px);
  font-weight: 700;
  line-height: 1.2;
}
.intro-panel p {
  margin: 0;
  max-width: min(100%, 700px);
  color: var(--muted);
  font-size: 15px;
}

.test-panel { padding: 16px 22px 24px; }
.card-surface {
  background: rgba(255,255,255,.88);
  border: 1px solid rgba(184,198,211,.75);
  border-radius: 18px;
  box-shadow: var(--shadow);
}

.test-options {
  display: flex;
  align-items: end;
  gap: 14px;
  flex-wrap: wrap;
  margin-bottom: 18px;
  padding: 16px;
}
.option-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.option-group label {
  font-weight: 700;
  color: #41576d;
}
.option-actions { margin-inline-start: auto; }
select {
  min-width: 130px;
  padding: 9px 12px;
  border: 1px solid rgba(150,176,203,.95);
  border-radius: 12px;
  background: #fff;
  color: #244159;
}
.primary-button, .secondary-button {
  border-radius: 12px;
  padding: 10px 16px;
  font-weight: 700;
}
.primary-button {
  background: linear-gradient(180deg, #5f97e3, #437fd2);
  color: white;
  border: 1px solid #376db5;
  box-shadow: 0 8px 16px rgba(67,127,210,.22);
}
.primary-button:hover { filter: brightness(.98); }
.secondary-button {
  background: #f9fcff;
  color: #27445d;
  border: 1px solid rgba(150,176,203,.95);
}
.secondary-button:hover { background: #f0f7ff; }

.typing-card {
  padding: 14px;
  border-radius: 24px;
  background: linear-gradient(180deg, #a9d8ef 0%, #9fcfe8 100%);
  border: 1px solid #90c3de;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.45), 0 10px 24px rgba(74, 131, 171, .18);
}
.test-badges {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}
.status-badge,
.segment-indicator {
  padding: 6px 12px;
  border-radius: 999px;
  background: rgba(255,255,255,.75);
  color: #3b5871;
  font-size: 13px;
  font-weight: 700;
  border: 1px solid rgba(136,177,205,.8);
}
.status-badge.running {
  background: #e8f7ed;
  color: var(--green);
}
.status-badge.finished {
  background: var(--soft-blue);
  color: var(--blue-dark);
}
.segment-progress {
  height: 8px;
  background: rgba(255,255,255,.45);
  border-radius: 999px;
  overflow: hidden;
  border: 1px solid rgba(136,177,205,.6);
  margin-bottom: 12px;
}
.segment-progress-bar {
  width: 0;
  height: 100%;
  background: linear-gradient(90deg, #4b8fe0, #61b6e3);
  transition: width .2s ease;
}

.passage {
  margin-bottom: 14px;
}
.passage-card {
  border: 1px solid #b7c5ce;
  border-radius: 10px;
  background: #f7f7f7;
  overflow: hidden;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.75);
}
.passage-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: linear-gradient(180deg, #eef6fb, #d7e9f4);
  border-bottom: 1px solid #c5d5df;
  color: #355269;
  font-size: 13px;
  font-weight: 700;
}
.passage-content {
  min-height: 145px;
  padding: 20px 28px;
  background: #fbfbfb;
  color: #171717;
  font-size: 22px;
  line-height: 2;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
html[lang="ar"] .passage-content,
html[lang="ar"] #typingInput,
html[lang="ar"] .key-main,
html[lang="ar"] .key-shift {
  font-family: Tahoma, Arial, "Segoe UI", sans-serif;
}
html[lang="ar"] .passage-content {
  font-size: 34px;
  line-height: 1.95;
}
html[lang="ar"] #typingInput {
  font-size: 24px;
}
.char { border-radius: 4px; }
.char.correct { color: #2e7a49; background: #eaf8ee; }
.char.wrong { color: #fff; background: #c84545; }
.char.current { background: #ddd; box-shadow: none; }

.input-timer-row {
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr) 136px;
  gap: 0;
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid rgba(87,131,207,.65);
  background: rgba(80, 126, 201, .24);
  box-shadow: inset 0 1px 0 rgba(255,255,255,.35);
}
.input-box-wrap {
  padding: 6px;
  background: rgba(102, 149, 223, .18);
}
#typingInput {
  display: block;
  width: 100%;
  min-height: 68px;
  height: 68px;
  margin: 0;
  padding: 14px 18px;
  resize: none;
  overflow-y: auto;
  border: 5px solid #4f88e3;
  border-radius: 12px;
  background: #f7f7f7;
  outline: none;
  color: #1d1d1d;
  font-size: 20px;
  line-height: 1.45;
}
html[lang="en"] #typingInput {
  direction: ltr;
  text-align: left;
}
html[lang="ar"] #typingInput {
  direction: rtl;
  text-align: right;
  unicode-bidi: plaintext;
}
#typingInput:focus {
  border-color: #4276c7;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.6), 0 0 0 4px rgba(70,120,200,.08);
}
#typingInput:disabled { background: #f3f3f3; color: #888; }

.timer-panel {
  display: grid;
  place-items: center;
  background: linear-gradient(180deg, #6ea7e7, #5388d0);
  border-inline-end: 1px solid rgba(66,118,199,.9);
  min-height: 108px;
}
.inline-start-button {
  min-height: 108px;
  min-width: 120px;
  border-radius: 0 12px 12px 0;
  box-shadow: none;
}
html[dir="rtl"] .inline-start-button {
  border-radius: 12px 0 0 12px;
}

.timer {
  font-size: clamp(40px, 5vw, 56px);
  line-height: 1;
  font-weight: 300;
  color: #fff;
  text-shadow: 0 1px 0 rgba(0,0,0,.06);
  font-variant-numeric: tabular-nums;
}
.action-row {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  padding: 10px 6px 2px;
}
html[dir="rtl"] .action-row {
  justify-content: flex-start;
}

.start-hint {
  color: #3d596f;
  font-size: 13px;
  font-weight: 600;
}


.speed-panel {
  margin-top: 16px;
  padding: 18px 18px 16px;
  background: #f5f5f5;
  border: 1px solid rgba(184,198,211,.75);
  border-radius: 14px;
  box-shadow: var(--shadow);
}
.speed-panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
  color: #1f3650;
}
.speed-panel-head strong {
  font-size: 20px;
}
.speed-category-text {
  font-weight: 700;
  color: #1e5ea0;
}
.speed-bar-wrap {
  position: relative;
  padding-top: 36px;
}
.speed-marker {
  position: absolute;
  top: 0;
  transform: translateX(-50%);
  z-index: 2;
}
.speed-marker span {
  position: relative;
  display: inline-grid;
  place-items: center;
  min-width: 48px;
  height: 48px;
  padding: 0 10px;
  border-radius: 999px;
  background: linear-gradient(180deg, #156dc3, #0c4f92);
  color: #fff;
  font-size: 28px;
  font-weight: 700;
  box-shadow: 0 8px 18px rgba(10,76,140,.25);
}
.speed-marker span::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -10px;
  transform: translateX(-50%);
  border-left: 10px solid transparent;
  border-right: 10px solid transparent;
  border-top: 12px solid #0c4f92;
}
.speed-scale-top {
  height: 14px;
  background: linear-gradient(90deg, #ff1f1f 0%, #ff1f1f 8%, #045ba9 8%, #045ba9 100%);
  border-top-left-radius: 4px;
  border-top-right-radius: 4px;
}
html[dir="rtl"] .speed-scale-top {
  background: linear-gradient(90deg, #045ba9 0%, #045ba9 92%, #ff1f1f 92%, #ff1f1f 100%);
}
html[dir="rtl"] .speed-marker {
  transform: translateX(50%);
}

.speed-scale-track {
  position: relative;
  height: 38px;
  background: linear-gradient(180deg, #0e6cc0, #0a4d8c);
  border-top: 4px solid #0a0a0a;
  border-bottom: 4px solid #0a0a0a;
  overflow: hidden;
}
.speed-scale-track::after {
  content: "";
  position: absolute;
  top: 0;
  right: -22px;
  width: 0;
  height: 0;
  border-top: 19px solid transparent;
  border-bottom: 19px solid transparent;
  border-left: 22px solid #0a0a0a;
}
html[dir="rtl"] .speed-scale-track::after {
  right: auto;
  left: -22px;
  border-left: 0;
  border-right: 22px solid #0a0a0a;
}
.tick {
  position: absolute;
  top: 0;
  bottom: 0;
  transform: translateX(-50%);
}
.tick::before {
  content: "";
  position: absolute;
  top: 2px;
  left: 50%;
  transform: translateX(-50%);
  width: 4px;
  height: 20px;
  background: #f68026;
  box-shadow: 8px 0 0 #adc9e8;
}
html[dir="rtl"] .tick::before {
  box-shadow: -8px 0 0 #adc9e8;
}
.tick b {
  position: absolute;
  left: 50%;
  bottom: 3px;
  transform: translateX(-50%);
  color: #fff;
  font-size: 17px;
}
.speed-scale-labels {
  display: grid;
  grid-template-columns: 20% 30% 50%;
  background: linear-gradient(180deg, #d6e7f7, #a7c3df);
  border-left: 1px solid #8db0cd;
  border-right: 1px solid #8db0cd;
  border-bottom: 1px solid #8db0cd;
}
.speed-label {
  padding: 12px 10px;
  text-align: center;
  font-size: 20px;
  font-weight: 700;
  color: #0d5697;
  border-right: 1px solid #8db0cd;
}
.speed-label:last-child { border-right: 0; }
.speed-label.active {
  background: linear-gradient(180deg, rgba(255,255,255,.45), rgba(255,255,255,.08));
  color: #073f73;
}
html[dir="rtl"] .speed-label { border-right: 0; border-left: 1px solid #8db0cd; }
html[dir="rtl"] .speed-label:last-child { border-left: 0; }

.results-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  margin-top: 16px;
  border: 1px solid rgba(184,198,211,.75);
  border-radius: 18px;
  overflow: hidden;
  box-shadow: var(--shadow);
}
.metric {
  min-height: 112px;
  padding: 14px 10px;
  text-align: center;
  border-right: 1px solid rgba(212,219,227,.95);
  background: linear-gradient(180deg, #ffffff, #f8fbfd);
}
.metric:last-child { border-right: 0; }
.metric-label {
  display: block;
  min-height: 42px;
  margin-bottom: 8px;
  color: #617080;
  font-size: 13px;
}
.metric strong {
  display: block;
  font-size: 30px;
  line-height: 1.1;
  color: #203244;
}
.metric small {
  display: block;
  margin-top: 5px;
  color: #7b8b99;
}
html[dir="rtl"] .metric { border-right: 0; border-left: 1px solid rgba(212,219,227,.95); }
html[dir="rtl"] .metric:last-child { border-left: 0; }

.keyboard-note {
  margin-top: 16px;
  padding: 16px;
}
.keyboard-note p {
  margin: 0 0 10px;
  color: #4d6175;
}
.toggle-row {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  font-weight: 600;
  color: #324e66;
}
.toggle-row input { position: absolute; opacity: 0; pointer-events: none; }
.switch {
  width: 42px;
  height: 24px;
  border-radius: 999px;
  background: #bfc7ce;
  position: relative;
  transition: .2s;
}
.switch::after {
  content: "";
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  position: absolute;
  top: 3px;
  left: 3px;
  box-shadow: 0 1px 3px rgba(0,0,0,.25);
  transition: .2s;
}
.toggle-row input:checked + .switch { background: #4f88e3; }
.toggle-row input:checked + .switch::after { transform: translateX(18px); }
html[dir="rtl"] .switch::after { left: auto; right: 3px; }
html[dir="rtl"] .toggle-row input:checked + .switch::after { transform: translateX(-18px); }

.virtual-keyboard {
  margin-top: 12px;
  padding: 10px;
  border: 1px solid #bcbcbc;
  border-radius: 18px;
  background: #dfe0e3;
  box-shadow: var(--shadow);
  user-select: none;
}
.keyboard-frame {
  background: #dcdde0;
  border: 1px solid #bababa;
  border-radius: 12px;
  padding: 8px;
}
.keyboard-row {
  display: flex;
  justify-content: flex-start;
  gap: 2px;
  margin-bottom: 2px;
}
.keyboard-row:last-child { margin-bottom: 0; }
.key {
  position: relative;
  min-width: 56px;
  height: 60px;
  padding: 8px 6px;
  border: 1px solid #b8b8b8;
  border-radius: 2px;
  background: linear-gradient(#fbfbfb, #e6e6e6);
  box-shadow: inset 0 1px 0 rgba(255,255,255,.95), 0 1px 1px rgba(0,0,0,.08);
  color: #111;
  display: flex;
  align-items: center;
  justify-content: center;
}
.key:hover { background: linear-gradient(#ffffff, #ececec); }
.key:active, .key.active-key {
  transform: translateY(1px);
  background: linear-gradient(#ececec, #ffffff);
}
.key-main { font-size: 17px; line-height: 1; }
.key.has-dual .key-main { font-size: 18px; }
.key-shift {
  position: absolute;
  top: 6px;
  right: 8px;
  color: #17a629;
  font-size: 12px;
  line-height: 1;
}
html[dir="rtl"] .key-shift { right: auto; left: 8px; }
.key-label {
  justify-content: flex-start;
  color: #666;
  font-size: 16px;
  padding-inline-start: 12px;
}
.key-tab { min-width: 76px; }
.key-caps { min-width: 98px; }
.key-shift-left, .key-shift-right { min-width: 104px; }
.key-backspace { min-width: 90px; }
.key-enter { min-width: 98px; }
.key-space { min-width: min(540px, 48vw); justify-content: center; }
.key-ctrl, .key-alt { min-width: 88px; }
.keyboard-footer {
  display: flex;
  justify-content: flex-end;
  gap: 6px;
  padding-top: 6px;
}
.keyboard-select {
  min-width: 160px;
  height: 28px;
  padding: 2px 8px;
  border: 1px solid #4e4e4e;
  border-radius: 0;
  background: #f7f7f7;
}
.keyboard-layout-select { min-width: 180px; }

.help-panel {
  border-top: 1px solid rgba(184,198,211,.7);
  padding: 18px 22px;
  background: rgba(248,251,253,.85);
}
.help-panel h2 { margin: 0 0 4px; font-size: 18px; color: #2f4b62; }
.help-panel p { margin: 0; color: var(--muted); }
footer {
  padding: 14px 20px 20px;
  border-top: 1px solid rgba(184,198,211,.7);
  color: #768594;
  text-align: center;
  font-size: 12px;
  background: rgba(255,255,255,.55);
}

@media (max-width: 920px) {
  .speed-label { font-size: 18px; }
  .tick b { font-size: 15px; }
  .results-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .metric { border-bottom: 1px solid rgba(212,219,227,.95); }
  .metric:nth-last-child(-n+1) { grid-column: 1 / -1; }
  .input-timer-row { grid-template-columns: 190px minmax(0, 1fr) 120px; }
  .key { min-width: 44px; height: 50px; }
  .key-tab { min-width: 62px; }
  .key-caps { min-width: 80px; }
  .key-shift-left, .key-shift-right { min-width: 84px; }
  .key-backspace { min-width: 72px; }
  .key-enter { min-width: 84px; }
  .key-ctrl, .key-alt { min-width: 64px; }
}

@media (max-width: 760px) {
  .page-shell { width: 100%; margin: 0; border-radius: 0; border-left: 0; border-right: 0; }
  .site-header { align-items: flex-start; }
  .brand small { display: none; }
  .option-actions { margin-inline-start: 0; }
  .input-timer-row { grid-template-columns: 1fr; }
  #typingInput { min-height: 62px; height: 62px; }
  .inline-start-button { min-height: 58px; border-radius: 12px; }
  .timer-panel {
    border-inline-start: 0;
    border-top: 1px solid rgba(66,118,199,.9);
    min-height: 90px;
  }
  .passage-content { font-size: 20px; min-height: 130px; }
  html[lang="ar"] .passage-content { font-size: 26px; }
  html[lang="ar"] #typingInput { font-size: 22px; }
}

@media (max-width: 520px) {
  .site-header { flex-direction: column; }
  .speed-panel { padding: 14px 10px; }
  .speed-panel-head { flex-direction: column; align-items: flex-start; }
  .speed-marker span { min-width: 42px; height: 42px; font-size: 22px; }
  .tick::before { height: 16px; width: 3px; box-shadow: 6px 0 0 #adc9e8; }
  html[dir="rtl"] .tick::before { box-shadow: -6px 0 0 #adc9e8; }
  .tick b { font-size: 12px; }
  .speed-label { font-size: 15px; padding: 10px 4px; }
  .language-switch { width: 100%; }
  .language-button { flex: 1; }
  .intro-panel, .test-panel { padding-left: 14px; padding-right: 14px; }
  .test-options { align-items: stretch; }
  .option-group, .option-actions { width: 100%; }
  .option-actions .secondary-button { width: 100%; }
  .results-grid { grid-template-columns: 1fr; }
  .metric, html[dir="rtl"] .metric {
    border-right: 0;
    border-left: 0;
    border-bottom: 1px solid rgba(212,219,227,.95);
  }
  .metric:last-child { grid-column: auto; border-bottom: 0; }
  .metric-label { min-height: auto; }
  .action-row { align-items: flex-start; flex-direction: column; }
  .passage-meta { flex-direction: column; align-items: flex-start; }
  .key { min-width: 28px; height: 42px; padding: 6px 4px; }
  .key-main { font-size: 13px; }
  .key-shift { font-size: 9px; top: 4px; right: 4px; }
  .key-tab { min-width: 48px; }
  .key-caps { min-width: 62px; }
  .key-shift-left, .key-shift-right { min-width: 68px; }
  .key-backspace { min-width: 52px; }
  .key-enter { min-width: 58px; }
  .key-ctrl, .key-alt { min-width: 42px; }
  .key-space { min-width: min(220px, 38vw); }
  .keyboard-footer { flex-direction: column; align-items: stretch; }
  .keyboard-select, .keyboard-layout-select { min-width: 0; width: 100%; }
}
