:root {
  --bg: #f6f7f6;
  --surface: #ffffff;
  --text: #1c2421;
  --muted: #5d6b65;
  --border: #d5dbd8;
  --brand: #1b8354;
  --brand-ink: #ffffff;
  --correct: #1b8354;
  --present: #c9a227;
  --absent: #7a8580;
  --key: #e3e8e5;
  --key-text: #1c2421;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.12);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111614;
    --surface: #1a211e;
    --text: #eef2f0;
    --muted: #9aa8a2;
    --border: #2f3a35;
    --brand: #25a56b;
    --correct: #1f8f5c;
    --present: #b8921f;
    --absent: #3c4642;
    --key: #2c3531;
    --key-text: #eef2f0;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: 'IBM Plex Sans Arabic', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  -webkit-tap-highlight-color: transparent;
}
button { font-family: inherit; cursor: pointer; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }

.bar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
  border-bottom: 1px solid var(--border);
}
.bar h1 { margin: 0; font-size: 1.6rem; font-weight: 700; }
.bar-side { display: flex; gap: 6px; }
.bar-side:last-child { justify-content: flex-end; }
.icon-btn {
  width: 40px; height: 40px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 1.05rem;
  font-weight: 600;
  padding: 0;
}

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 520px;
  margin: 0 auto;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
}

.mode-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 8px 12px;
  border-radius: 12px;
  background: color-mix(in srgb, var(--present) 16%, var(--surface));
  font-size: 0.9rem; font-weight: 600;
}
.mode-bar button {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 99px; padding: 5px 12px; font-weight: 600; font-size: 0.85rem;
}
.secondary {
  border-radius: 12px;
  padding: 12px 20px;
  font-size: 1.05rem;
  font-weight: 600;
  border: none;
  background: var(--key);
  color: var(--key-text);
}
.wide-btn { display: block; width: 100%; margin-top: 10px; }
.center { text-align: center; }
.primary {
  border-radius: 12px;
  padding: 12px 20px;
  font-size: 1.05rem;
  font-weight: 600;
  border: none;
  background: var(--brand);
  color: var(--brand-ink);
}

/* المربعات */
.t {
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 36px;
  border-radius: 8px;
  font-weight: 700; font-size: 1.1rem;
  color: #fff;
}
.t.correct { background: var(--correct); }
.t.present { background: var(--present); }
.t.absent { background: var(--absent); }

/* الشبكة */
.grid {
  flex: 1 1 0;
  min-height: 0;
  max-height: 410px;
  aspect-ratio: 5 / 6;
  max-width: 100%;
  align-self: center;
  display: grid;
  grid-template-rows: repeat(6, 1fr);
  gap: 6px;
  margin: 8px auto 14px;
}
.grid-row { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.grid-row.shake { animation: shake 0.4s; }
.cell {
  min-height: 0;
  display: flex; align-items: center; justify-content: center;
  border: 2px solid var(--border);
  border-radius: 8px;
  font-size: clamp(1.1rem, 4.5vh, 2rem);
  font-weight: 700;
  background: var(--surface);
  max-height: 100%;
}
.cell.filled { border-color: var(--muted); animation: pop 0.1s; }
.cell.correct, .cell.present, .cell.absent { color: #fff; border-color: transparent; }
.cell.correct { background: var(--correct); }
.cell.present { background: var(--present); }
.cell.absent { background: var(--absent); }
.cell.reveal { animation: flip 0.45s ease both; }
.grid-row.win .cell { animation: bounce 0.5s ease both; }

/* الكيبورد */
.keyboard { display: grid; gap: 6px; user-select: none; }
.kb-row { display: flex; gap: 4px; justify-content: center; }
.key {
  flex: 1;
  min-width: 0;
  height: 52px;
  border: none;
  border-radius: 6px;
  background: var(--key);
  color: var(--key-text);
  font-size: 1.1rem;
  font-weight: 600;
  padding: 0;
}
.key.wide { flex: 1.8; font-size: 0.95rem; }
/* الأزرار اللي فيها همزات بالضغط الطويل */
.key { -webkit-touch-callout: none; -webkit-user-select: none; touch-action: manipulation; }
.key.has-variants { position: relative; }
.key.has-variants::after {
  content: attr(data-variants);
  position: absolute; top: 2px; left: 4px;
  font-size: 0.6rem; opacity: 0.55; letter-spacing: 1px;
}
.variants {
  position: fixed; z-index: 20;
  display: flex; gap: 4px; padding: 5px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; box-shadow: var(--shadow);
}
.variants button {
  width: 46px; height: 50px; border: none; border-radius: 7px;
  background: var(--key); color: var(--key-text);
  font-size: 1.3rem; font-weight: 700;
}
.key.correct { background: var(--correct); color: #fff; }
.key.present { background: var(--present); color: #fff; }
.key.absent { background: var(--absent); color: #fff; opacity: 0.8; }

/* رسائل */
.toast {
  position: fixed;
  top: 76px;
  left: 50%;
  transform: translateX(-50%) translateY(-10px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 10px;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 10;
  max-width: calc(100vw - 32px);
  text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* النوافذ */
dialog {
  position: fixed;
  border: none;
  border-radius: 18px;
  padding: 22px 18px 20px;
  width: min(92vw, 420px);
  max-height: 88dvh;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
dialog h2 { margin: 0 0 10px; text-align: center; }
dialog h3 { margin: 16px 0 10px; font-size: 1rem; text-align: center; }
dialog p { line-height: 1.8; margin: 0 0 10px; }
.close {
  position: absolute;
  top: 10px; left: 10px;
  width: 34px; height: 34px;
  border: none; border-radius: 50%;
  background: var(--key); color: var(--text);
  font-size: 0.95rem;
}
#end-box { text-align: center; }
#end-box p { margin: 0; }

.legend { list-style: none; padding: 0; margin: 0 0 12px; display: grid; gap: 8px; }
.legend li { display: flex; align-items: center; gap: 10px; }

.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; text-align: center; }
.stats b { display: block; font-size: 1.7rem; font-weight: 700; }
.stats span { font-size: 0.78rem; color: var(--muted); line-height: 1.3; display: block; }

.dist { display: grid; gap: 4px; }
.dist-row { display: grid; grid-template-columns: 14px 1fr; gap: 8px; align-items: center; font-size: 0.9rem; }
.dist-bar {
  min-width: 26px;
  padding: 2px 8px;
  background: var(--absent);
  color: #fff;
  font-weight: 700;
  border-radius: 4px;
  text-align: end;
  width: fit-content;
}
.dist-bar.now { background: var(--correct); }

.next {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  margin-top: 18px; padding-top: 14px;
  border-top: 1px solid var(--border);
}
.next b { display: block; font-size: 1.5rem; font-variant-numeric: tabular-nums; direction: ltr; text-align: start; }

.setting {
  display: flex; align-items: center; justify-content: space-between; gap: 14px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.setting b { display: block; }
.setting span { font-size: 0.85rem; line-height: 1.6; }
.switch {
  appearance: none; -webkit-appearance: none;
  flex: none;
  width: 48px; height: 28px;
  border-radius: 99px;
  background: var(--key);
  position: relative;
  cursor: pointer;
  transition: background 0.2s;
  margin: 0;
}
.switch::after {
  content: '';
  position: absolute; top: 3px; right: 3px;
  width: 22px; height: 22px; border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  transition: transform 0.2s;
}
.switch:checked { background: var(--brand); }
.switch:checked::after { transform: translateX(-20px); }

@keyframes pop { 50% { transform: scale(1.08); } }
@keyframes flip { 0% { transform: rotateX(90deg); } 100% { transform: rotateX(0); } }
@keyframes bounce { 0%, 100% { transform: translateY(0); } 40% { transform: translateY(-14px); } }
@keyframes shake {
  20%, 60% { transform: translateX(-6px); }
  40%, 80% { transform: translateX(6px); }
}
@media (max-height: 640px) {
  .key { height: 44px; }
  .grid { gap: 4px; margin: 6px auto 10px; }
  .grid-row { gap: 4px; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
