:root {
  --bg: #1a1410;
  --card: rgba(30, 23, 17, 0.94);
  --ink: #f5ecd8;
  --muted: #cdbf9f;
  --gold: #ffd166;
  --green: #2e7d4f;
  --red: #ff8a80;
  --panel: rgba(20, 15, 10, 0.88);
  --panel-border: #8a6a3a;
  --orange: #ffb070;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, "Segoe UI", Tahoma, Arial, sans-serif;
  user-select: none;
  -webkit-user-select: none;
}

.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: radial-gradient(circle at 50% 30%, rgba(60, 40, 20, 0.6), rgba(10, 8, 6, 0.92));
  overflow-y: auto;
}

.overlay[hidden], #hud[hidden] { display: none; }

.card {
  width: 100%;
  max-width: 440px;
  padding: 28px;
  border-radius: 18px;
  background: var(--card);
  border: 1px solid #5a4630;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

h1 {
  margin: 0;
  font-size: 2.2rem;
}

h1 span { color: var(--gold); }

.tagline { color: var(--muted); margin: 6px 0 20px; }

label {
  display: block;
  margin: 14px 0 6px;
  font-weight: 600;
}

label.check {
  display: flex;
  gap: 8px;
  align-items: center;
  font-weight: 400;
}

input:not([type="checkbox"]) {
  width: 100%;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid #6a5438;
  background: #120d09;
  color: var(--ink);
  font-size: 1.05rem;
}

#room { text-transform: uppercase; letter-spacing: 0.15em; text-align: center; }

.row { display: flex; gap: 8px; }
.row input { flex: 1; min-width: 0; }

button {
  cursor: pointer;
  border: 0;
  border-radius: 10px;
  padding: 12px 16px;
  font-size: 1rem;
  font-weight: 600;
  color: #fff;
  font-family: inherit;
  touch-action: manipulation;
}

button.primary {
  width: 100%;
  margin-top: 20px;
  padding: 14px;
  font-size: 1.1rem;
  background: var(--green);
}

button.primary:hover { background: #379660; }

button.secondary {
  background: #4a3a28;
  white-space: nowrap;
}

button.secondary:hover { background: #5c4a34; }
button.secondary.on { background: var(--green); }

.error { color: var(--red); min-height: 1.2em; margin: 10px 0 0; }

details { margin-top: 12px; color: var(--muted); }
summary { cursor: pointer; color: var(--ink); }
details li { margin: 8px 0; line-height: 1.5; }

/* --- واجهة اللعب --- */

button:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }

#scoreboard {
  position: fixed;
  top: 12px;
  right: 12px;
  z-index: 5;
  min-width: 220px;
  max-width: min(320px, calc(100% - 24px));
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  pointer-events: none;
}

#scoreboard h2 {
  margin: 0 0 4px;
  font-size: 0.95rem;
  color: var(--gold);
}

#scoreboard .progress { margin: 0 0 8px; font-size: 0.9rem; color: var(--muted); }
#scoreboard ol { margin: 0; padding: 0; list-style: none; counter-reset: rang; }

#scoreboard li {
  display: grid;
  grid-template-columns: 1.4em 1fr auto;
  column-gap: 6px;
  padding: 5px 6px;
  border-radius: 8px;
  counter-increment: rang;
  font-size: 1rem;
}

#scoreboard li::before { content: counter(rang); color: var(--muted); font-weight: 700; }
#scoreboard li.me { color: #9fe0b0; }
#scoreboard li.turn { background: rgba(255, 209, 102, 0.16); box-shadow: inset -3px 0 0 var(--gold); }
#scoreboard .score { font-weight: 700; font-variant-numeric: tabular-nums; }
#scoreboard .extra { grid-column: 2 / 4; font-size: 0.875rem; color: var(--muted); }

#hud {
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 5;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
  align-items: center;
  max-width: calc(100% - 360px);
}

#hud button { min-height: 44px; padding: 8px 14px; font-size: 0.95rem; }

#hud-room {
  padding: 10px 14px;
  border-radius: 10px;
  background: var(--panel);
  font-weight: 700;
}

#hud-status { font-size: 0.9rem; color: var(--muted); }
#hud-status.connected { color: #9fe0b0; }

/* الشريط السفلي: التفاعلات | حالة الدور + اليد | الإجراءات */
#dock {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: 12px;
  z-index: 5;
  display: grid;
  grid-template-columns: 230px minmax(0, 1fr) 230px;
  align-items: end;
  gap: 12px;
  pointer-events: none;
}

#dock > * { pointer-events: auto; }
#center { display: flex; flex-direction: column; align-items: center; gap: 8px; min-width: 0; }

#banner {
  display: flex;
  flex-direction: column;
  align-items: center;
  max-width: 100%;
  padding: 4px 8px;
  background: none;
  border: 0;
  text-align: center;
  pointer-events: none;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.95), 0 0 3px #000;
}

#banner strong { font-size: 1.5rem; color: var(--gold); }
#banner.alert strong { color: var(--orange); }
#banner.flash strong { animation: flashText 0.5s ease-in-out 3; }

@keyframes flashText {
  50% { transform: scale(1.12); color: #fff; }
}

#hand {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 8px;
  max-width: 100%;
  padding: 8px 12px;
  border-radius: 14px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
}

.tile {
  width: 44px;
  height: 88px;
  padding: 0;
  border-radius: 7px;
  background: transparent;
  border: 2px solid rgba(90, 70, 48, 0.7);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.55);
  overflow: hidden;
  touch-action: none;
  transition: transform 0.15s ease, box-shadow 0.15s ease, opacity 0.15s ease;
}

.tile canvas {
  display: block;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.tile.legal {
  border-color: var(--gold);
  transform: translateY(-6px);
  box-shadow: 0 6px 0 #b08a2a, 0 8px 14px rgba(0, 0, 0, 0.6);
}

.tile.dragging {
  z-index: 10;
  transition: none;
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.75);
}

.tile[aria-disabled="true"] { opacity: 0.45; }
.tile:hover { filter: brightness(1.05); }
.tile-side { min-height: 48px; background: var(--green); font-size: 1rem; }

#emotes { display: flex; flex-wrap: wrap; gap: 6px; }
#emotes button { min-width: 44px; min-height: 44px; padding: 6px 10px; font-size: 0.95rem; }

#actions { display: flex; flex-direction: column; gap: 8px; align-items: stretch; }
#actions button { min-height: 48px; font-size: 1.05rem; }
#actions button:disabled { opacity: 0.5; cursor: default; }

#toast {
  position: fixed;
  left: 50%;
  top: 72px;
  z-index: 6;
  transform: translateX(-50%) translateY(-10px);
  max-width: calc(100% - 32px);
  padding: 12px 18px;
  border-radius: 12px;
  background: rgba(20, 16, 12, 0.92);
  color: #9ad0ff;
  font-size: 1rem;
  opacity: 0;
  transition: opacity 0.25s, transform 0.25s;
  pointer-events: none;
}

#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.error { color: var(--orange); }

#scoreboard[hidden], #banner[hidden], #hand[hidden], #dock[hidden], #actions[hidden] { display: none; }

/* شاشة تنبيه تدوير الهاتف للوضع الأفقي */
#rotate-device {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: #140f0b;
  color: var(--ink);
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px;
}

.rotate-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 22px;
}

.phone-icon {
  width: 54px;
  height: 96px;
  border: 4px solid var(--gold);
  border-radius: 12px;
  position: relative;
  animation: rotatePhone 2.2s ease-in-out infinite;
}

.phone-icon::before {
  content: '';
  position: absolute;
  top: 7px;
  left: 50%;
  transform: translateX(-50%);
  width: 16px;
  height: 3px;
  border-radius: 2px;
  background: var(--gold);
}

.phone-icon::after {
  content: '';
  position: absolute;
  bottom: 7px;
  left: 50%;
  transform: translateX(-50%);
  width: 8px;
  height: 8px;
  border-radius: 50%;
  border: 2px solid var(--gold);
}

.rotate-box p {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--gold);
}

@keyframes rotatePhone {
  0%, 15% { transform: rotate(0deg); }
  50%, 75% { transform: rotate(-90deg); }
  100% { transform: rotate(0deg); }
}

@media (orientation: portrait) and (max-width: 900px) {
  #rotate-device { display: flex; }
}

/* إخفاء أي أزرار واقع افتراضي بشكل كامل */
.a-enter-vr, .a-enter-ar { display: none !important; }

/* اللمس على المشهد ثلاثي الأبعاد للتحريك واللعب */
.a-canvas { touch-action: none; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* تحسين أبعاد الشاشة للهاتف في الوضع الأفقي */
@media (max-height: 560px), (max-width: 950px) and (orientation: landscape) {
  .overlay { padding: 8px; }
  .card {
    max-width: 520px;
    padding: 14px 20px;
    border-radius: 14px;
  }
  h1 { font-size: 1.5rem; }
  .tagline { margin: 2px 0 10px; font-size: 0.9rem; }
  label { margin: 8px 0 4px; font-size: 0.9rem; }
  input:not([type="checkbox"]) { padding: 8px 12px; font-size: 0.95rem; }
  button.primary { margin-top: 12px; padding: 10px; font-size: 1rem; }
  details { display: none; }

  #scoreboard {
    top: 6px;
    right: 6px;
    min-width: 0;
    max-width: 220px;
    padding: 6px 10px;
    border-radius: 10px;
  }
  #scoreboard h2 { font-size: 0.8rem; margin-bottom: 2px; }
  #scoreboard li { font-size: 0.82rem; padding: 2px 4px; }
  #scoreboard .extra { display: none; }

  #hud {
    top: 6px;
    left: 6px;
    gap: 6px;
    max-width: calc(100% - 240px);
  }
  #hud-room { padding: 6px 10px; font-size: 0.82rem; }
  #hud-status { font-size: 0.78rem; }
  #hud button { min-height: 32px; padding: 5px 10px; font-size: 0.8rem; }

  #dock {
    left: 6px;
    right: 6px;
    bottom: 6px;
    grid-template-columns: auto minmax(0, 1fr) auto;
    align-items: end;
    gap: 8px;
  }
  #center { gap: 4px; }
  #banner { padding: 0 4px; }
  #banner strong { font-size: 1.15rem; }
  #hand {
    padding: 5px 8px;
    gap: 5px;
    border-radius: 10px;
    flex-wrap: nowrap;
    overflow-x: auto;
  }
  .tile {
    width: 34px;
    height: 68px;
    border-radius: 5px;
    flex: 0 0 auto;
  }
  .tile.legal { transform: translateY(-4px); box-shadow: 0 4px 0 #b08a2a, 0 6px 10px rgba(0, 0, 0, 0.55); }
  .tile-side { min-height: 36px; padding: 6px 10px; font-size: 0.85rem; }

  #emotes {
    flex-wrap: wrap;
    max-width: 136px;
    gap: 4px;
  }
  #emotes button {
    min-width: 32px;
    min-height: 32px;
    padding: 4px 7px;
    font-size: 0.78rem;
  }

  #actions {
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: flex-end;
    max-width: 200px;
    gap: 4px;
  }
  #actions button {
    min-height: 34px;
    padding: 6px 10px;
    font-size: 0.82rem;
  }

  #toast { top: 44px; padding: 6px 14px; font-size: 0.88rem; }
}

@media (prefers-reduced-motion: reduce) {
  #toast { transition: none; }
  #banner.flash { animation: none; outline: 3px solid var(--gold); }
  .tile.legal { transform: none; }
}
