:root {
  color-scheme: light;
  --page: #f7f3ea;
  --panel: #fffdf7;
  --ink: #161514;
  --muted: #706b61;
  --border: rgba(22, 21, 20, 0.14);
  --accent: #2563eb;
  --accent-strong: #1745a4;
  --button-primary: #6c63ff;
  --button-primary-hover: #4a42da;
  --danger: #b42318;
  --track: #1f1f1f;
  --unit: #ffffff;
  --line-symbol: #2563eb;
  --dot-symbol: #dc2626;
  --short-unit-width: clamp(32px, 5vw, 48px);
  --madd-unit-width: clamp(88px, 14vw, 132px);
  --sukoon-dot-size: clamp(8px, 1.2vw, 11px);
  --handle-width: clamp(34px, 4.6vw, 46px);
  --handle-height: clamp(56px, 7vw, 72px);
  --touch-size: 78px;
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  font-family: "Segoe UI", Tahoma, Arial, sans-serif;
  color: var(--ink);
  background:
    linear-gradient(90deg, rgba(37, 99, 235, 0.07) 1px, transparent 1px),
    linear-gradient(180deg, #ffffff 0%, var(--page) 100%);
  background-size: 28px 28px, auto;
  overflow-x: hidden;
}

button,
input {
  font: inherit;
}

.app {
  width: min(1060px, calc(100vw - 32px));
  min-height: 100vh;
  margin: 0 auto;
  padding: 72px 0 36px;
  display: grid;
  align-content: start;
  gap: 10px;
}

.teacher-panel {
  display: grid;
  grid-template-columns: minmax(150px, 30%) auto 52px 1fr;
  gap: 12px;
  align-items: end;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: rgba(255, 253, 247, 0.92);
  box-shadow: 0 20px 48px rgba(22, 21, 20, 0.08);
}

.activity-title {
  grid-column: 1 / -1;
  margin: 0;
  color: var(--ink);
  font-family: "Noto Naskh Arabic", serif;
  font-size: clamp(1.7rem, 4vw, 2.4rem);
  font-weight: 700;
  line-height: 1.1;
  text-align: center;
}

.word-field {
  display: grid;
  gap: 8px;
}

.word-field span {
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 800;
}

.word-field input {
  width: 100%;
  min-height: 52px;
  padding: 8px 14px;
  border: 2px solid rgba(22, 21, 20, 0.16);
  border-radius: 8px;
  background: #ffffff;
  color: var(--ink);
  font-family: "Noto Naskh Arabic", serif;
  font-size: 2rem;
  font-weight: 700;
  line-height: 1.1;
}

.word-field input:focus {
  outline: 3px solid rgba(37, 99, 235, 0.18);
  border-color: var(--accent);
}

.primary-button,
.secondary-button {
  min-height: 52px;
  padding: 0 18px;
  border-radius: 8px;
  font-weight: 800;
  cursor: pointer;
}

.primary-button {
  border: 0;
  color: #ffffff;
  background: var(--button-primary);
}

.primary-button:hover {
  background: var(--button-primary-hover);
}

.secondary-button {
  border: 2px solid rgba(22, 21, 20, 0.16);
  color: var(--ink);
  background: #ffffff;
}

.icon-button {
  width: 52px;
  padding: 0;
  display: inline-grid;
  place-items: center;
  font-size: 1.55rem;
  line-height: 1;
}

.field-help,
.field-error {
  grid-column: 1 / -1;
  margin: 0;
  font-size: 0.9rem;
  line-height: 1.5;
}

.field-help {
  color: var(--muted);
}

.field-error {
  color: var(--danger);
  font-weight: 700;
}

.field-error:empty {
  display: none;
}

.activity-stage {
  position: relative;
  display: grid;
  gap: clamp(8px, 2vw, 18px);
  justify-items: center;
  padding: clamp(8px, 1.5vw, 16px) clamp(18px, 4vw, 40px) clamp(28px, 6vw, 64px);
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 24px 58px rgba(22, 21, 20, 0.1);
}

.word-wrap {
  position: relative;
  display: inline-grid;
  place-items: center;
  min-height: clamp(96px, 16vw, 148px);
  max-width: 100%;
  font-family: "Noto Naskh Arabic", serif;
  font-size: clamp(4.5rem, 12vw, 8.2rem);
  font-weight: 700;
  line-height: 1.2;
  white-space: nowrap;
  letter-spacing: 0;
  user-select: none;
}

.word-base,
.word-overlay {
  grid-area: 1 / 1;
  direction: rtl;
  unicode-bidi: isolate;
  white-space: nowrap;
  letter-spacing: 0;
}

.word-base {
  color: rgba(22, 21, 20, 0.2);
}

.word-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  color: var(--ink);
}

.word-overlay-clip {
  position: absolute;
  overflow: hidden;
}

.word-overlay-clip span {
  position: absolute;
  display: grid;
  place-items: center;
  direction: rtl;
  unicode-bidi: isolate;
  white-space: nowrap;
}

.track-wrap {
  width: fit-content;
  max-width: 100%;
  padding: 0;
  margin-top: -18px;
}

.track {
  position: relative;
  width: 100%;
  --track-progress: 0;
  min-height: 50px;
  min-width: 96px;
  padding: 5px 0;
  display: flex;
  align-items: center;
  border: 0;
  border-radius: 18px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.96), rgba(247, 243, 234, 0.98));
  box-shadow:
    inset 0 0 0 1px rgba(31, 31, 31, 0.08),
    0 12px 22px rgba(22, 21, 20, 0.07);
  touch-action: none;
  user-select: none;
}

.track::before {
  content: "";
  position: absolute;
  inset: 4px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.42);
  pointer-events: none;
}

.track::after {
  content: "";
  position: absolute;
  top: 4px;
  right: 4px;
  bottom: 4px;
  z-index: 0;
  width: calc((100% - 8px) * var(--track-progress));
  border-radius: 12px;
  background: var(--track);
  pointer-events: none;
  transition: width 120ms ease;
}

.track-units {
  position: absolute;
  inset: 4px 0;
  z-index: 2;
  width: 100%;
  direction: rtl;
  pointer-events: none;
}

.track-unit {
  position: absolute;
  top: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 54px;
  flex: 0 0 auto;
  transform: translate(-50%, -50%);
}

.track-unit::before {
  content: "";
  display: block;
  border: 3px solid currentColor;
  background: var(--unit);
  transition:
    opacity 140ms ease;
}

.track-unit[data-type="short"] {
  width: var(--symbol-width, var(--short-unit-width));
  color: var(--line-symbol);
}

.track-unit[data-type="madd"] {
  width: var(--symbol-width, var(--madd-unit-width));
  color: var(--line-symbol);
}

.track-unit[data-type="short"]::before,
.track-unit[data-type="madd"]::before {
  width: 100%;
  height: 12px;
  background: var(--line-symbol);
  border-radius: 999px;
}

.track-unit[data-type="sukoon"] {
  width: 28px;
  color: var(--dot-symbol);
}

.track-unit[data-type="sukoon"]::before {
  width: var(--sukoon-dot-size);
  height: var(--sukoon-dot-size);
  background: var(--dot-symbol);
  border-radius: 999px;
}

.track-unit.is-inactive::before {
  opacity: 1;
}

.blend-handle {
  position: absolute;
  top: calc(100% - 4px);
  left: 100%;
  z-index: 4;
  width: var(--touch-size);
  height: var(--touch-size);
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  cursor: grab;
  transform: translateX(-50%);
  touch-action: none;
}

.blend-handle::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 0;
  width: var(--handle-width);
  height: var(--handle-height);
  background: var(--accent);
  clip-path: polygon(38% 26%, 50% 0, 62% 26%, 100% 26%, 100% 100%, 0 100%, 0 26%);
  box-shadow:
    0 12px 22px rgba(37, 99, 235, 0.28),
    0 0 0 2px rgba(23, 69, 164, 0.28);
  transform: translateX(-50%);
}

.blend-handle::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 21px;
  width: 4px;
  height: calc(var(--handle-height) - 30px);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.88);
  box-shadow: none;
  transform: translateX(-50%);
}

.blend-handle:hover::before {
  background: var(--accent-strong);
}

.blend-handle:active {
  cursor: grabbing;
}

.blend-handle:focus-visible {
  outline: 3px solid rgba(37, 99, 235, 0.3);
  outline-offset: 4px;
}

@media (max-width: 720px) {
  .app {
    width: calc(100vw - 20px);
    padding-top: 64px;
  }

  .teacher-panel {
    grid-template-columns: minmax(150px, 1fr) auto 52px;
  }

  .word-field {
    grid-column: auto;
  }
}

@media (max-width: 520px) {
  .track-units {
    gap: 12px;
  }

  .teacher-panel {
    grid-template-columns: 1fr;
  }

  .icon-button {
    width: 100%;
  }
}
