@font-face {
  font-family: "Tajawal";
  src: url("./fonts/Tajawal-Arabic-Regular.woff2") format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
  unicode-range: U+0600-06FF, U+0750-077F, U+0870-08FF, U+200C-200E, U+FB50-FDFF, U+FE70-FEFC;
}

@font-face {
  font-family: "Tajawal";
  src: url("./fonts/Tajawal-Arabic-Bold.woff2") format("woff2");
  font-weight: 700;
  font-style: normal;
  font-display: swap;
  unicode-range: U+0600-06FF, U+0750-077F, U+0870-08FF, U+200C-200E, U+FB50-FDFF, U+FE70-FEFC;
}

@font-face {
  font-family: "Tajawal";
  src: url("./fonts/Tajawal-Latin-Regular.woff2") format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2190-2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

@font-face {
  font-family: "Tajawal";
  src: url("./fonts/Tajawal-Latin-Bold.woff2") format("woff2");
  font-weight: 700;
  font-style: normal;
  font-display: swap;
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2190-2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

:root {
  color-scheme: dark;
  --bg: #16171c;
  --surface: #1e2028;
  --border-empty: #3d404a;
  --border-filled: #6b6f7c;
  --text: #ecebe4;
  --muted: #83869a;
  --correct: #4b8c5b;
  --present: #b6903f;
  --absent: #35373f;
  --gold: #c9a15a;
  --overlay: rgba(0,0,0,0.6);
  --hover-bg: #2a2d38;
  --font-ui: "Tajawal", Tahoma, Arial, sans-serif;
}

html[data-theme="light"] {
  color-scheme: light;
  --bg: #f5f2ea;
  --surface: #ffffff;
  --border-empty: #cfc9ba;
  --border-filled: #8a8474;
  --text: #2a2a28;
  --muted: #736d5e;
  --correct: #4f9660;
  --present: #c79a3a;
  --absent: #ddd7c8;
  --gold: #b3823a;
  --overlay: rgba(0,0,0,0.35);
  --hover-bg: #e8e2d2;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  background-image: repeating-linear-gradient(45deg,
      transparent, transparent 40px,
      rgba(201,161,90,0.025) 40px, rgba(201,161,90,0.025) 41px);
  color: var(--text);
  font-family: var(--font-ui);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px 12px 12px;
  -webkit-tap-highlight-color: transparent;
}

header {
  text-align: center;
  margin-bottom: 8px;
  position: relative;
  width: 100%;
  max-width: 560px;
}

h1 {
  font-family: var(--font-ui);
  font-weight: 400;
  font-size: 2.6rem;
  margin: 0;
  letter-spacing: 2px;
  color: var(--text);
  text-shadow: 0 0 18px rgba(201,161,90,0.25);
}

h1::after {
  content: "";
  display: block;
  width: 90px;
  height: 3px;
  margin: 8px auto 0;
  background: var(--gold);
  opacity: 0.6;
  border-radius: 2px;
}

.subtitle {
  color: var(--muted);
  font-size: 0.95rem;
  margin-top: 10px;
}

.iconBtn {
  appearance: none;
  -webkit-appearance: none;
  -webkit-tap-highlight-color: transparent;
  position: absolute;
  top: 4px;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: var(--surface);
  border: 1px solid var(--border-empty);
  color: var(--text);
  font-size: 1.1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  outline: none;
}
.iconBtn:active { color: var(--text); }
.iconBtn:focus-visible { outline: 2px solid var(--gold); outline-offset: 1px; }
.iconBtn:hover { background: var(--hover-bg); }
#themeToggle { left: 0; }
#keyboardToggle { left: 44px; font-size: 0.95rem; }
#settingsToggle { right: 44px; font-size: 1.05rem; }
#statsToggle { right: 0; }

.modalOverlay {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--overlay);
  align-items: center;
  justify-content: center;
  z-index: 10;
}
.modalOverlay.show { display: flex; }

.modalBox {
  background: var(--surface);
  border: 1px solid var(--border-empty);
  border-radius: 10px;
  padding: 20px 24px;
  width: 280px;
  text-align: center;
}
.modalBox h2 {
  font-family: var(--font-ui);
  font-weight: 400;
  margin: 0 0 16px;
  color: var(--gold);
}
.statsGrid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
  margin-bottom: 16px;
}
.statsGrid div { display: flex; flex-direction: column; }
.statsGrid span { font-size: 1.6rem; font-weight: 700; }
.statsGrid label { color: var(--muted); font-size: 0.8rem; margin-top: 2px; }
#closeStats {
  background: var(--gold);
  color: var(--bg);
  border: none;
  border-radius: 6px;
  padding: 8px 20px;
  font-family: var(--font-ui);
  font-weight: 700;
  cursor: pointer;
}

/* ============================================================
   إعدادات طول الكلمة
   ============================================================ */
.settingsBox {
  width: 320px;
  max-width: 90vw;
}
.settingsSection {
  text-align: right;
  margin-bottom: 16px;
}
.settingsSection h3 {
  margin: 0 0 4px;
  font-size: 1.05rem;
  color: var(--gold);
  font-family: var(--font-ui);
  font-weight: 400;
}
.settingsHint {
  margin: 0 0 12px;
  font-size: 0.8rem;
  color: var(--muted);
}
.settingsOptions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.settingsLabel {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 6px;
  background: var(--bg);
  border: 1px solid var(--border-empty);
  transition: background 0.15s;
  direction: rtl;
}
.settingsLabel:hover {
  background: var(--hover-bg);
}
.settingsLabel input[type="checkbox"] {
  width: 20px;
  height: 20px;
  accent-color: var(--gold);
  cursor: pointer;
}
.settingsLabel span {
  font-size: 1rem;
  font-weight: 600;
}
.settingsLabel input:disabled + span {
  color: var(--muted);
}
.randomLabel {
  border-color: var(--gold);
  border-style: dashed;
}
.settingsBtns {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
#applySettings {
  background: var(--gold);
  color: var(--bg);
  border: none;
  border-radius: 6px;
  padding: 10px 20px;
  font-family: var(--font-ui);
  font-weight: 700;
  font-size: 0.95rem;
  cursor: pointer;
}
#applySettings:hover { opacity: 0.9; }
.secondaryBtn {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-empty);
  border-radius: 6px;
  padding: 8px 20px;
  font-family: var(--font-ui);
  font-weight: 600;
  cursor: pointer;
}
.secondaryBtn:hover { background: var(--hover-bg); }

#board {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: clamp(5px, 1.4vh, 8px);
  flex: 1;
  min-height: 0;
  width: 100%;
  margin: 12px 0;
}

.row {
  display: flex;
  direction: rtl;
  gap: clamp(5px, 1.4vw, 8px);
  justify-content: center;
}

.tile {
  width: clamp(42px, min(12vw, 7vh), 66px);
  height: clamp(42px, min(12vw, 7vh), 66px);
  border: 2px solid var(--border-empty);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(1.3rem, min(4.5vw, 3vh), 2rem);
  font-weight: 700;
  background: var(--surface);
  transition: transform 0.15s;
}

.answer-row {
  margin-top: 6px;
  padding-top: 10px;
  border-top: 1px dashed var(--border-empty);
}
.answer-row .tile.answer-tile {
  opacity: 0.9;
}

.tile.filled { border-color: var(--border-filled); }

.tile.flip {
  animation: flip 0.5s ease forwards;
}

@keyframes flip {
  0%   { transform: rotateX(0deg); }
  50%  { transform: rotateX(90deg); }
  51%  { }
  100% { transform: rotateX(0deg); }
}

.tile.correct { background: var(--correct); border-color: var(--correct); }
.tile.present { background: var(--present); border-color: var(--present); }
.tile.absent  { background: var(--absent);  border-color: var(--absent); }
.tile.hint { background: var(--correct); border-color: var(--correct); border-style: dashed; }
.tile.selected {
  border-color: var(--gold);
  box-shadow: 0 0 12px rgba(201, 161, 90, 0.6);
  animation: tileGlow 1s ease-in-out infinite alternate;
  cursor: pointer;
}

@keyframes tileGlow {
  from { box-shadow: 0 0 8px rgba(201, 161, 90, 0.4); }
  to   { box-shadow: 0 0 16px rgba(201, 161, 90, 0.8); }
}

#message {
  min-height: 26px;
  font-weight: 600;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-empty);
  padding: 6px 16px;
  border-radius: 6px;
  opacity: 0;
  transition: opacity 0.25s;
  margin-bottom: 14px;
}
#message.show { opacity: 1; }

#keyboard {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 560px;
  width: 100%;
}
#keyboard.hidden { display: none; }

.kb-row {
  display: flex;
  gap: 5px;
  justify-content: center;
  direction: ltr; /* الكيبورد الحقيقي ثابت الترتيب، مش بينعكس مع RTL */
}

.key {
  appearance: none;
  -webkit-appearance: none;
  -webkit-tap-highlight-color: transparent;
  flex: 1;
  max-width: 38px;
  height: 46px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface);
  border: none;
  border-radius: 5px;
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 1.05rem;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
  outline: none;
  transition: background 0.15s;
}
.key:active { transform: scale(0.94); background: var(--hover-bg); color: var(--text); }
.key:focus-visible { outline: 2px solid var(--gold); outline-offset: -2px; }
.key:hover  { background: var(--hover-bg); }

.key.wide { font-size: 1.1rem; color: var(--muted); max-width: 64px; flex: 1.5; }

.key.correct { background: var(--correct); }
.key.present { background: var(--present); }
.key.absent  { background: var(--absent); color: var(--muted); }

#endActions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-top: 4px;
}

#newGameBtn {
  padding: 10px 22px;
  background: var(--gold);
  color: var(--bg);
  border: none;
  border-radius: 6px;
  font-family: var(--font-ui);
  font-weight: 700;
  font-size: 0.95rem;
  cursor: pointer;
  display: none;
}
#shareBtn, #copyDetailsBtn {
  padding: 10px 18px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-empty);
  border-radius: 6px;
  font-family: var(--font-ui);
  font-weight: 700;
  font-size: 0.95rem;
  cursor: pointer;
  display: none;
}
#newGameBtn.show, #shareBtn.show, #copyDetailsBtn.show { display: inline-block; }

#actionsRow {
  display: flex;
  gap: 8px;
  width: 100%;
  max-width: 560px;
  margin-top: 10px;
}

#submitBtn, #hintBtn, #surrenderBtn, #newGameBtn, #shareBtn, #copyDetailsBtn, #closeStats, #closeSettings, #applySettings {
  appearance: none;
  -webkit-appearance: none;
  -webkit-tap-highlight-color: transparent;
  outline: none;
}
#submitBtn, #hintBtn, #surrenderBtn {
  height: 46px;
  background: var(--surface);
  border: 1px solid var(--border-empty);
  border-radius: 5px;
  color: var(--text);
  font-family: var(--font-ui);
  font-weight: 700;
  font-size: 1rem;
  cursor: pointer;
  transition: background 0.15s;
}
#submitBtn:hover, #hintBtn:hover, #surrenderBtn:hover { background: var(--hover-bg); }
#submitBtn:active, #hintBtn:active, #surrenderBtn:active { transform: scale(0.98); }

#submitBtn { flex: 2; }

#hintBtn   { flex: 1; border-color: var(--gold); color: var(--gold); }
#hintBtn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
  color: var(--muted);
  border-color: var(--border-empty);
}

#surrenderBtn { flex: 1; border-color: #a05050; color: #d08080; }
#surrenderBtn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
  color: var(--muted);
  border-color: var(--border-empty);
}

@media (max-width: 380px) {
  .tile { width: 40px; height: 40px; font-size: 1.3rem; }
  .key  { height: 40px; font-size: 0.9rem; max-width: 32px; }
  .hint-overlay-tile { width: 30px !important; height: 30px !important; font-size: 0.85rem !important; }
  .settingsBox { width: 280px; }
}

/* أي شاشة قصيرة الارتفاع (portrait أو landscape) — تقليص عام معتدل */
@media (max-height: 700px) {
  body { padding: 10px 12px 8px; }
  header { margin-bottom: 2px; }
  h1 { font-size: 1.9rem; }
  h1::after { margin-top: 4px; }
  .subtitle { margin-top: 4px; }
  #board { margin: 6px 0; gap: 4px; }
  .tile { width: clamp(38px, min(12vw, 6.5vh), 66px); height: clamp(38px, min(12vw, 6.5vh), 66px); }
  #message { min-height: 18px; padding: 3px 14px; }
  #keyboard { gap: 4px; }
  .key { height: 38px; }
  #actionsRow { margin-top: 4px; }
  #submitBtn, #hintBtn, #surrenderBtn { height: 38px; }
  #endActions { margin-top: 0; }
}

/* شاشة عريضة وقصيرة جداً (موبايل بوضع landscape) — تقليص إضافي أقوى */
@media (max-height: 500px) and (orientation: landscape) {
  body { padding: 4px 12px; overflow-y: auto; }
  header { margin-bottom: 0; }
  h1 { font-size: 1.25rem; letter-spacing: 1px; }
  h1::after { display: none; }
  .subtitle { margin-top: 0; font-size: 0.72rem; }
  #board { margin: 3px 0; gap: 2px; }
  .row { gap: 2px; }
  .tile { width: clamp(24px, 5vh, 36px); height: clamp(24px, 5vh, 36px); font-size: clamp(0.8rem, 2.6vh, 1.1rem); }
  #message { margin-bottom: 2px; padding: 2px 10px; min-height: 0; font-size: 0.8rem; }
  #keyboard { gap: 3px; }
  .key { height: 28px; font-size: 0.82rem; }
  #actionsRow { margin-top: 3px; }
  #submitBtn, #hintBtn, #surrenderBtn { height: 28px; font-size: 0.78rem; }
  #endActions { margin-top: 0; }
}

/* ============================================================
   شريط التلميح: منفصل عن لوحة المحاولات
   ============================================================ */
#hintPanel {
  display: none;
  width: 100%;
  max-width: 560px;
  justify-content: center;
  margin: -6px 0 8px;
}

#hintPanel.show { display: flex; }

.hint-panel-row {
  display: flex;
  gap: clamp(5px, 1.4vw, 8px);
  justify-content: center;
  animation: hintSlideIn 0.3s ease forwards;
}

@keyframes hintSlideIn {
  from { opacity: 0; transform: translateY(-8px); }
  to   { opacity: 1; transform: translateY(0); }
}

.hint-overlay-tile {
  width: clamp(30px, 9vw, 44px);
  height: clamp(30px, 9vw, 44px);
  font-size: clamp(0.85rem, 3vw, 1.2rem);
  border: 1.5px solid var(--border-empty);
  border-radius: 5px;
  background: var(--surface);
  opacity: 0.92;
}

.hint-overlay-tile.hint-gold {
  background: var(--correct);
  border-color: var(--correct);
  border-style: dashed;
  color: var(--text);
  font-weight: 700;
  box-shadow: 0 0 10px rgba(75, 140, 91, 0.4);
}

.hint-overlay-tile.correct {
  background: var(--correct);
  border-color: var(--correct);
}

.hint-overlay-tile.present {
  background: var(--present);
  border-color: var(--present);
}
