/* وصلة: dark sage green (the only theme), mobile first, right-to-left. */

:root {
  color-scheme: dark;

  --bg: #131A14;
  --surface: #1D261E;
  --surface-2: #253026;
  --line: #303D2F;
  --line-strong: #45563F;
  --ink: #E4EBDE;
  --ink-2: #B3C0AB;
  --ink-3: #8D9A87;

  /* sage, dim to bright */
  --sage-1: #212D21;
  --sage-2: #31432E;
  --sage-3: #4A6243;
  --sage-4: #6E8B62;
  --sage-5: #8EAA80;
  --sage-6: #A9C39B;
  --sage-7: #CADBBE;

  --rail: #3A4A37;
  --key: #29342A;
  --key-shadow: #0C110C;
  --honey: #E1BF7E;
  --honey-bright: #E1BF7E;
  --honey-tint: #342C1D;
  --honey-line: #6A5733;
  --clay: #E3A58F;
  --clay-tint: #3A2620;
  --on-accent: #101610;
  --scrim: rgb(0 0 0 / 0.6);

  --shadow-sm: 0 1px 0 rgb(0 0 0 / 0.3);
  --shadow-lg: 0 12px 40px rgb(0 0 0 / 0.55);

  --font-ui: 'IBM Plex Sans Arabic', system-ui, -apple-system, 'Segoe UI', Tahoma, sans-serif;
  --font-display: 'Cairo', 'IBM Plex Sans Arabic', system-ui, sans-serif;

  --tile: clamp(50px, 15vw, 62px);
  --tile-gap: 8px;
  --rail-pad: 14px;
}

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

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
}

button:focus-visible,
a:focus-visible,
summary:focus-visible {
  outline: 2px solid var(--sage-5);
  outline-offset: 2px;
}

svg {
  display: block;
}

[hidden] {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* ---------- Frame ---------- */

.app {
  max-width: 520px;
  margin-inline: auto;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px 8px 8px;
  padding-inline: 16px 8px;
  border-bottom: 1px solid var(--line);
}

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

.brand__mark {
  width: 30px;
  height: 30px;
  color: var(--sage-5);
}

.brand__name {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 28px;
  line-height: 1.2;
  color: var(--sage-7);
}

.topbar__actions {
  display: flex;
  gap: 2px;
}

.icon-btn {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--ink-2);
}

.icon-btn:hover {
  background: var(--surface-2);
  color: var(--ink);
}

.icon-btn svg {
  width: 23px;
  height: 23px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.preview-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 4px 12px;
  padding: 6px 16px;
  background: var(--honey-tint);
  color: var(--honey);
  font-size: 13px;
  font-weight: 500;
  text-align: center;
}

.preview-bar__nav {
  display: inline-flex;
  gap: 12px;
}

.preview-bar a,
.link-btn {
  border: 0;
  padding: 0;
  background: none;
  color: inherit;
  font-weight: 600;
  text-decoration: underline;
  text-underline-offset: 3px;
}

/* ---------- Game ---------- */

.game {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.meta {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 6px 8px;
  padding: 10px 16px 0;
  font-size: 14px;
  color: var(--ink-2);
}

.meta__no {
  font-weight: 600;
  color: var(--ink);
}

.meta__dot {
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: var(--ink-3);
}

.chip {
  padding: 1px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  background: var(--sage-1);
  color: var(--sage-7);
}

.chip--medium {
  background: var(--honey-tint);
  color: var(--honey);
}

.chip--hard {
  background: var(--clay-tint);
  color: var(--clay);
}

.board {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding-inline: 16px;
}

/* ---------- The ladder ---------- */

.ladder {
  list-style: none;
  margin: 0;
  padding: 16px 0 18px;
  min-height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 10px;
}

.rung {
  --bar-w: calc(3 * var(--tile) + 2 * var(--tile-gap) + 2 * var(--rail-pad));
  display: grid;
  grid-template-columns: 1fr var(--bar-w) 1fr;
  align-items: center;
  column-gap: 10px;
}

/* the joined word, read first (right side), with its caption */
.rung__side {
  justify-self: end;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  min-width: 0;
  white-space: nowrap;
  text-align: end;
}

.rung__word {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: calc(var(--tile) * 0.34);
  line-height: 1.5;
  color: var(--ink-2);
}

.rung__caption {
  font-size: 11px;
  font-weight: 600;
  line-height: 1.3;
  color: var(--ink-3);
}

.rung--goal .rung__word,
.rung--goal .rung__caption {
  color: var(--honey);
}

.rung--start .rung__caption {
  color: var(--sage-6);
}

.rung--input .rung__word {
  color: var(--sage-6);
}

.rung--input .rung__caption {
  opacity: 0.6;
}

.rung.is-lit .rung__word {
  color: var(--sage-6);
}

.rung__badge {
  justify-self: start;
  min-width: 26px;
  height: 26px;
  padding: 0 6px;
  display: grid;
  place-items: center;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink-2);
  font-size: 13px;
  font-weight: 600;
}

.rung__badge.is-empty {
  visibility: hidden;
}

/* rails and the rung behind the tiles */
.rung__bar {
  position: relative;
  width: var(--bar-w);
  padding: 6px var(--rail-pad);
  background: linear-gradient(var(--rail), var(--rail)) center / 100% 4px no-repeat;
}

.rung__bar::before,
.rung__bar::after {
  content: '';
  position: absolute;
  top: -6px;
  bottom: -6px;
  width: 4px;
  border-radius: 4px;
  background: var(--rail);
}

.rung__bar::before {
  inset-inline-start: 3px;
}

.rung__bar::after {
  inset-inline-end: 3px;
}

/* the part still to climb */
.rung--gap {
  flex: 1 1 auto;
  min-height: 40px;
  align-items: stretch;
}

.rung--gap .rung__bar {
  display: grid;
  place-items: center;
  padding: 0 var(--rail-pad);
  background: none;
}

.rung--gap .rung__bar::before,
.rung--gap .rung__bar::after {
  background: repeating-linear-gradient(to bottom, var(--rail) 0 6px, transparent 6px 13px);
}

.gap-hint {
  position: relative;
  z-index: 1;
  padding: 3px 12px;
  border-radius: 999px;
  border: 1px solid var(--honey-line);
  background: var(--honey-tint);
  color: var(--honey);
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
}

.rung--goal {
  position: sticky;
  top: 0;
  z-index: 3;
  margin-top: -16px;
  padding-top: 16px;
  background: var(--bg);
}

.rung--input {
  scroll-margin-top: calc(var(--tile) + 48px);
  scroll-margin-bottom: 16px;
}

/* ---------- Tiles ---------- */

.tiles {
  position: relative;
  z-index: 1;
  display: flex;
  justify-content: center;
  gap: var(--tile-gap);
}

.tile {
  width: var(--tile);
  height: var(--tile);
  display: grid;
  place-items: center;
  border-radius: 12px;
  border: 1.5px solid var(--line);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  font-family: var(--font-display);
  font-weight: 700;
  font-size: calc(var(--tile) * 0.46);
  line-height: 1;
  padding-bottom: calc(var(--tile) * 0.06);
  color: var(--ink);
  user-select: none;
  transition: background-color 0.35s ease, border-color 0.35s ease, color 0.35s ease, transform 0.35s ease;
}

.tile--changed {
  background: var(--sage-2);
  border-color: var(--sage-3);
  color: var(--sage-7);
}

.rung--start .tile {
  background: var(--sage-1);
  border-color: var(--sage-2);
}

.rung--goal .tile {
  background: var(--honey-tint);
  border-color: var(--honey-line);
}

.rung--input .tile {
  border: 2px solid var(--line-strong);
  box-shadow: none;
}

.rung--input .tile--filled {
  border-color: var(--sage-4);
}

.rung--input .tile--active {
  border-color: var(--sage-5);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--sage-4) 28%, transparent);
}

.tile--pop {
  animation: pop 0.16s ease-out;
}

.rung--new .tiles {
  animation: rise 0.28s ease-out;
}

.rung--input.is-shaking .tiles {
  animation: shake 0.36s ease-in-out;
}

/* solved */
.rung.is-lit .tile {
  background: var(--sage-2);
  border-color: var(--sage-3);
  color: var(--sage-7);
}

.rung.is-lit .tile--changed {
  background: var(--sage-5);
  border-color: var(--sage-5);
  color: var(--on-accent);
}

.rung--goal.is-lit .tile {
  background: var(--sage-6);
  border-color: var(--sage-6);
  color: var(--on-accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--honey-bright) 55%, transparent);
}

.rung.is-lit .tiles {
  animation: lift 0.4s ease-out;
}

.rung.is-lit .rung__bar {
  --rail: var(--sage-4);
}

.rung--goal.is-lit .rung__caption::before {
  content: '✓ ';
}

@keyframes pop {
  0% { transform: scale(0.88); }
  60% { transform: scale(1.06); }
  100% { transform: scale(1); }
}

@keyframes rise {
  from { opacity: 0; transform: translateY(-14px); }
  to { opacity: 1; transform: none; }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-7px); }
  40% { transform: translateX(7px); }
  60% { transform: translateX(-5px); }
  80% { transform: translateX(4px); }
}

@keyframes lift {
  0% { transform: none; }
  40% { transform: translateY(-5px); }
  100% { transform: none; }
}

/* ---------- Toolbar ---------- */

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 12px;
  border-top: 1px solid var(--line);
}

.pill-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 36px;
  padding: 0 14px 0 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink-2);
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
}

.pill-btn svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.pill-btn:hover:not(:disabled) {
  border-color: var(--line-strong);
  color: var(--ink);
}

.pill-btn:disabled {
  opacity: 0.45;
  cursor: default;
}

.pill-btn[aria-pressed='true'] {
  background: var(--honey-tint);
  border-color: var(--honey-line);
  color: var(--honey);
}

.counter {
  margin: 0;
  font-size: 14px;
  color: var(--ink-2);
  text-align: center;
  white-space: nowrap;
}

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

.counter span + span {
  padding-inline-start: 10px;
  border-inline-start: 1px solid var(--line-strong);
}

.counter b {
  color: var(--ink);
  font-weight: 700;
}

/* ---------- Keyboard ---------- */

.keyboard {
  display: grid;
  gap: 7px;
  padding: 4px 5px calc(8px + env(safe-area-inset-bottom));
  user-select: none;
}

/* Left to right, like a real Arabic keyboard (ض at the top left). Without this the
   page's right-to-left direction would mirror every row. */
.kb-row {
  display: flex;
  gap: 4px;
  direction: ltr;
}

.kb-space {
  flex: 0.5 1 0;
}

.key {
  flex: 1 1 0;
  min-width: 0;
  height: 52px;
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: var(--key);
  box-shadow: 0 1.5px 0 var(--key-shadow);
  color: var(--ink);
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 21px;
  line-height: 1;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

.key:active {
  background: var(--sage-2);
  transform: translateY(1px);
  box-shadow: none;
}

.key--wide {
  flex-grow: 2.6;
  font-family: var(--font-ui);
  font-size: 15px;
  font-weight: 600;
}

.key--enter {
  background: var(--sage-6);
  color: var(--on-accent);
  box-shadow: 0 1.5px 0 var(--sage-7);
}

.key--enter:active {
  background: var(--sage-5);
}

.key--back svg {
  width: 26px;
  height: 26px;
  margin: auto;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---------- Finished ---------- */

.done-panel {
  padding: 14px 16px calc(16px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  text-align: center;
}

.done-panel__line {
  margin: 0 0 12px;
  font-weight: 600;
  color: var(--ink);
}

.done-panel__actions {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 10px;
}

.done-panel__next {
  margin: 12px 0 0;
  font-size: 13px;
  color: var(--ink-2);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 46px;
  padding: 0 20px;
  border-radius: 12px;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--ink);
  font-size: 15px;
  font-weight: 600;
}

.btn:hover {
  border-color: var(--sage-4);
}

.btn svg {
  width: 19px;
  height: 19px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.btn--primary {
  background: var(--sage-6);
  border-color: var(--sage-6);
  color: var(--on-accent);
}

.btn--primary:hover {
  background: var(--sage-7);
  border-color: var(--sage-7);
}

.btn--block {
  width: 100%;
}

.countdown {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: var(--ink);
}

/* ---------- Screens (before launch, errors) ---------- */

.screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 24px 24px 48px;
  text-align: center;
}

.screen__mark {
  width: 76px;
  height: 76px;
  margin-bottom: 8px;
  color: var(--sage-5);
}

.screen__title {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 26px;
  line-height: 1.4;
  color: var(--sage-7);
}

.screen__text {
  margin: 0;
  max-width: 30ch;
  color: var(--ink-2);
}

.screen__count {
  margin: 4px 0 14px;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 30px;
  line-height: 1.4;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

.screen__count:empty {
  display: none;
}

/* ---------- Toasts ---------- */

.toasts {
  position: fixed;
  top: calc(70px + env(safe-area-inset-top));
  inset-inline: 0;
  z-index: 60;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}

.toast {
  max-width: calc(100vw - 32px);
  padding: 9px 18px;
  border-radius: 12px;
  background: var(--ink);
  color: var(--bg);
  font-size: 15px;
  font-weight: 600;
  box-shadow: var(--shadow-lg);
  animation: toast-in 0.18s ease-out;
}

.toast.is-leaving {
  opacity: 0;
  transform: translateY(-6px);
  transition: opacity 0.25s, transform 0.25s;
}

@keyframes toast-in {
  from { opacity: 0; transform: translateY(-8px) scale(0.97); }
  to { opacity: 1; transform: none; }
}

/* ---------- Dialogs ---------- */

.sheet {
  width: min(440px, calc(100vw - 24px));
  max-height: calc(100dvh - 32px);
  padding: 22px 20px 20px;
  border: 0;
  border-radius: 20px;
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow-lg);
  overscroll-behavior: contain;
}

.sheet:focus {
  outline: none;
}

.sheet[open] {
  animation: sheet-in 0.2s ease-out;
}

.sheet::backdrop {
  background: var(--scrim);
}

@keyframes sheet-in {
  from { opacity: 0; transform: translateY(12px) scale(0.98); }
  to { opacity: 1; transform: none; }
}

.sheet__close {
  position: absolute;
  top: 10px;
  inset-inline-end: 10px;
}

.sheet h2 {
  margin: 0 0 6px;
  padding-inline-end: 40px;
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 24px;
  line-height: 1.4;
  color: var(--sage-7);
}

.sheet h3 {
  margin: 22px 0 10px;
  font-size: 14px;
  font-weight: 600;
  color: var(--ink-2);
}

.lead {
  margin: 0 0 6px;
  font-size: 17px;
}

.rules {
  margin: 0;
  padding-inline-start: 20px;
  color: var(--ink-2);
}

.rules li {
  margin: 4px 0;
}

.rules li::marker {
  color: var(--sage-4);
}

.rules b,
.lead b {
  color: var(--ink);
}

.rules--small {
  font-size: 14px;
}

.example {
  margin: 14px 0;
  padding: 6px 8px 10px;
  border-radius: 16px;
  background: var(--bg);
  --tile: 38px;
  --tile-gap: 5px;
  --rail-pad: 9px;
}

.example .ladder {
  min-height: 0;
  gap: 7px;
  padding: 10px 0 6px;
}

.example .rung--goal {
  position: static;
  margin: 0;
  padding: 0;
  background: none;
}

.example .tile {
  border-radius: 9px;
}

.example figcaption {
  font-size: 13px;
  color: var(--ink-2);
  text-align: center;
}

.note {
  margin: 14px 0 0;
  padding: 10px 14px;
  border-radius: 12px;
  background: var(--sage-1);
  color: var(--sage-7);
  font-size: 14px;
  font-weight: 500;
}

.credits {
  margin-top: 14px;
  font-size: 13px;
  color: var(--ink-3);
}

.credits summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--ink-2);
}

.credits p {
  margin: 6px 0 0;
}

.credits a {
  color: var(--sage-6);
}

/* result + stats */

.result {
  text-align: center;
}

.result__kicker {
  margin: 0;
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-3);
}

.result h2 {
  padding: 0;
  margin: 0;
  font-size: 30px;
}

.result__line {
  margin: 2px 0 14px;
  color: var(--ink-2);
}

.path {
  margin: 0 0 12px;
  padding: 10px 12px;
  border-radius: 14px;
  background: var(--bg);
}

.path__label {
  margin: 0 0 6px;
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-3);
}

.chain {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 4px 6px;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 18px;
  line-height: 1.5;
}

.chain__arrow {
  color: var(--sage-4);
  font-family: var(--font-ui);
  font-weight: 400;
  font-size: 15px;
}

.chain__word--goal {
  color: var(--sage-6);
}

.legend {
  margin: 8px 0 0;
  font-size: 12px;
  color: var(--ink-3);
}

.kpis {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-top: 18px;
}

.kpi {
  padding: 10px 4px 8px;
  border-radius: 14px;
  background: var(--bg);
  text-align: center;
}

.kpi__value {
  display: block;
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 26px;
  line-height: 1.3;
  color: var(--ink);
}

.kpi__label {
  display: block;
  font-size: 12px;
  line-height: 1.4;
  color: var(--ink-2);
}

.dist {
  display: grid;
  gap: 6px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.dist__row {
  display: grid;
  grid-template-columns: 76px 1fr;
  align-items: center;
  gap: 10px;
}

.dist__label {
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-2);
  text-align: start;
}

.dist__track {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.dist__bar {
  height: 22px;
  min-width: 4px;
  border-start-end-radius: 4px;
  border-end-end-radius: 4px;
  background: var(--sage-3);
}

.dist__row.is-today .dist__bar {
  background: var(--sage-6);
}

.dist__count {
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-2);
  font-variant-numeric: tabular-nums;
}

.empty {
  margin: 0;
  padding: 14px;
  border-radius: 14px;
  background: var(--bg);
  color: var(--ink-2);
  font-size: 14px;
  text-align: center;
}

.stats-next {
  margin: 18px 0 0;
  text-align: center;
  font-size: 14px;
  color: var(--ink-2);
}

.stats-note {
  margin: 10px 0 0;
  font-size: 12px;
  color: var(--honey);
  text-align: center;
}

/* ---------- Confetti ---------- */

.confetti {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 40;
}

/* ---------- Small and large screens ---------- */

@media (max-height: 640px) {
  :root {
    --tile: clamp(44px, 13vw, 54px);
  }

  .key {
    height: 46px;
  }

  .keyboard {
    gap: 6px;
  }
}

@media (min-width: 560px) {
  .app {
    border-inline: 1px solid var(--line);
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
