/* ============================
   BACKGAMMON — Refined Dark Luxury
   ============================ */

:root {
  --bg: #0d0a07;
  --panel: #161008;
  --gold: #c9943a;
  --gold-light: #e8b860;
  --gold-dim: #7a5820;
  --ivory: #f2e8d5;
  --ivory-dim: #b5a88a;
  --burgundy: #6b1a1a;
  --teal: #1a4a4a;
  --board-dark: #1c1008;
  --board-felt: #0f2318;
  --pip-dark: #8b3a1a;
  --pip-light: #c8a44a;
  --shadow: rgba(0,0,0,0.7);
  --checker-white: #f5efe0;
  --checker-black: #1a1208;
  --checker-border-w: #e8d0a0;
  --checker-border-b: #3a2810;
  --radius: 8px;
  --font-display: 'Cinzel', serif;
  --font-body: 'Crimson Pro', Georgia, serif;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  background: var(--bg);
  color: var(--ivory);
  font-family: var(--font-body);
  min-height: 100vh;
  overflow: hidden;
  user-select: none;
}

.screen { display: none; width: 100vw; height: 100vh; height: 100dvh; }
.screen.active { display: flex; }

/* ============================
   LOBBY
   ============================ */

#lobby {
  align-items: center;
  justify-content: center;
  position: relative;
}

.lobby-bg {
  position: absolute; inset: 0;
  background:
    radial-gradient(ellipse 80% 60% at 50% 30%, rgba(201,148,58,0.08) 0%, transparent 70%),
    radial-gradient(ellipse 60% 80% at 20% 80%, rgba(107,26,26,0.15) 0%, transparent 60%),
    radial-gradient(ellipse 50% 70% at 80% 70%, rgba(26,74,74,0.12) 0%, transparent 60%);
  background-color: var(--bg);
}

.lobby-content {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 40px;
}

.logo-wrap {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.logo {
  font-family: var(--font-display);
  font-size: clamp(3rem, 6vw, 5.5rem);
  font-weight: 900;
  letter-spacing: 0.25em;
  color: var(--gold);
  text-shadow:
    0 0 40px rgba(201,148,58,0.4),
    0 2px 4px rgba(0,0,0,0.8);
  line-height: 1;
}

.logo-sub {
  font-family: var(--font-body);
  font-style: italic;
  font-weight: 300;
  color: var(--ivory-dim);
  letter-spacing: 0.15em;
  font-size: 1rem;
  text-transform: uppercase;
}

.logo-ornament {
  color: var(--gold-dim);
  font-size: 1.2rem;
  letter-spacing: 0.5em;
}

.menu-panel {
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: 280px;
}

.match-select {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  font-family: var(--font-display);
  font-size: 0.75rem; letter-spacing: 0.1em; text-transform: uppercase;
  color: var(--gold-dim);
  padding-bottom: 6px;
}
.match-select select {
  background: var(--panel); color: var(--gold);
  border: 1px solid var(--gold-dim); border-radius: 6px;
  padding: 6px 10px; font-family: var(--font-body); font-size: 0.95rem;
}
.match-info {
  font-family: var(--font-display);
  font-size: 0.7rem; letter-spacing: 0.08em; text-transform: uppercase;
  color: var(--gold);
  white-space: nowrap;
}
.match-score { font-family: var(--font-display); color: var(--gold); letter-spacing: 0.08em; margin: 0; }

.menu-btn {
  font-family: var(--font-display);
  font-size: 0.85rem;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  padding: 14px 28px;
  background: transparent;
  border: 1px solid var(--gold-dim);
  color: var(--ivory-dim);
  cursor: pointer;
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  gap: 14px;
  transition: all 0.2s ease;
  position: relative;
  overflow: hidden;
}

.menu-btn::before {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(135deg, rgba(201,148,58,0.05), transparent);
  opacity: 0;
  transition: opacity 0.2s;
}

.menu-btn:hover {
  border-color: var(--gold);
  color: var(--gold-light);
  transform: translateX(4px);
  box-shadow: 0 0 20px rgba(201,148,58,0.15), inset 0 0 20px rgba(201,148,58,0.05);
}
.menu-btn:hover::before { opacity: 1; }

.menu-btn.primary {
  background: linear-gradient(135deg, rgba(201,148,58,0.2), rgba(201,148,58,0.05));
  border-color: var(--gold);
  color: var(--gold-light);
}

.menu-btn.ai {
  border-color: rgba(26,74,74,0.8);
  color: var(--ivory-dim);
}
.menu-btn.ai:hover { border-color: #4a9a9a; color: #7acaca; }

.menu-btn.small { padding: 10px 20px; font-size: 0.75rem; }

.btn-icon { font-size: 1rem; opacity: 0.7; }

.sub-panel {
  background: rgba(255,255,255,0.03);
  border: 1px solid rgba(201,148,58,0.2);
  border-radius: var(--radius);
  padding: 24px;
  width: 280px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}
.sub-panel.hidden { display: none; }

.panel-label {
  font-style: italic;
  color: var(--ivory-dim);
  font-size: 0.9rem;
}

.room-code {
  font-family: var(--font-display);
  font-size: 2.5rem;
  letter-spacing: 0.3em;
  color: var(--gold);
  text-shadow: 0 0 20px rgba(201,148,58,0.5);
}

.waiting-text {
  font-style: italic;
  color: var(--ivory-dim);
  font-size: 0.85rem;
  animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse { 0%,100% { opacity: 0.6; } 50% { opacity: 1; } }

.room-input {
  font-family: var(--font-display);
  font-size: 1.2rem;
  letter-spacing: 0.25em;
  text-transform: uppercase;
  background: rgba(255,255,255,0.05);
  border: 1px solid var(--gold-dim);
  color: var(--gold-light);
  padding: 12px 16px;
  border-radius: 6px;
  width: 100%;
  text-align: center;
  outline: none;
  transition: border-color 0.2s;
}
.room-input:focus { border-color: var(--gold); }
.room-input::placeholder { color: var(--ivory-dim); opacity: 0.4; }

.error-msg {
  color: #e05555;
  font-size: 0.85rem;
  font-style: italic;
}
.hidden { display: none !important; }

/* ============================
   GAME SCREEN
   ============================ */

#gameScreen {
  flex-direction: column;
  align-items: center;
  background: var(--bg);
  padding: 12px;
  gap: 8px;
}

.game-header {
  width: 100%;
  max-width: 920px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  background: rgba(255,255,255,0.03);
  border: 1px solid rgba(201,148,58,0.15);
  border-radius: var(--radius);
}

.player-info {
  display: flex;
  align-items: center;
  gap: 14px;
  font-family: var(--font-display);
  font-size: 0.75rem;
  letter-spacing: 0.1em;
}
.player-info.right { flex-direction: row-reverse; }

.pip-label {
  color: var(--ivory-dim);
  font-size: 0.65rem;
  letter-spacing: 0.2em;
}

.checker-count {
  color: var(--gold);
  font-size: 0.9rem;
}

.bar-info {
  color: #e07050;
  font-size: 0.8rem;
  min-width: 40px;
}

.center-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.game-code {
  font-family: var(--font-display);
  font-size: 0.75rem;
  letter-spacing: 0.2em;
  color: var(--gold-dim);
}

.doubling-cube {
  width: 36px; height: 36px;
  background: var(--ivory);
  border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  font-family: var(--font-display);
  font-size: 1rem;
  font-weight: 900;
  color: var(--board-dark);
  box-shadow: 0 2px 8px rgba(0,0,0,0.5);
  cursor: pointer;
  transition: transform 0.1s;
}
.doubling-cube:hover { transform: scale(1.1); }
.doubling-cube.owned-white { box-shadow: 0 2px 8px rgba(0,0,0,0.5), 0 0 0 2px var(--gold); }
.doubling-cube.owned-black { box-shadow: 0 2px 8px rgba(0,0,0,0.5), 0 0 0 2px var(--gold-dim); }
.doubling-cube.offered { animation: cube-pulse 0.8s ease-in-out infinite alternate; }
@keyframes cube-pulse { to { transform: scale(1.25); } }

.icon-btn {
  background: transparent;
  border: 1px solid rgba(201,148,58,0.2);
  color: var(--ivory-dim);
  padding: 6px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  transition: all 0.2s;
}
.icon-btn:hover { border-color: var(--gold); color: var(--gold); }

/* ============================
   BOARD CANVAS
   ============================ */

.game-area {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

#board {
  border-radius: 10px;
  box-shadow:
    0 0 0 2px var(--gold-dim),
    0 0 0 4px var(--board-dark),
    0 8px 40px rgba(0,0,0,0.8);
  cursor: default;
  max-width: 100%;
  touch-action: none; /* dragging a checker must not scroll or zoom the page */
}

.status-bar {
  font-family: var(--font-display);
  font-size: 0.8rem;
  letter-spacing: 0.15em;
  color: var(--gold);
  text-align: center;
  min-height: 20px;
  text-shadow: 0 0 10px rgba(201,148,58,0.4);
}

.dice-area {
  display: flex;
  align-items: center;
  gap: 16px;
  min-height: 96px;
}

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

.die {
  width: 80px; height: 80px;
  background: var(--ivory);
  border-radius: 14px;
  display: flex; align-items: center; justify-content: center;
  font-family: var(--font-display);
  font-size: 2.8rem;
  font-weight: 900;
  color: var(--board-dark);
  box-shadow: 0 6px 20px rgba(0,0,0,0.6), inset 0 1px 2px rgba(255,255,255,0.3);
  transition: all 0.3s;
  position: relative;
}

.die.used {
  opacity: 0.3;
  transform: scale(0.85);
}

.die.animate {
  animation: dieRoll 0.4s ease;
}

@keyframes dieRoll {
  0% { transform: rotate(0deg) scale(1); }
  25% { transform: rotate(15deg) scale(1.1); }
  50% { transform: rotate(-10deg) scale(0.95); }
  75% { transform: rotate(5deg) scale(1.05); }
  100% { transform: rotate(0deg) scale(1); }
}

.die.black-die {
  background: var(--checker-black);
  color: var(--ivory);
  border: 1px solid var(--checker-border-b);
}

.action-btn {
  font-family: var(--font-display);
  font-size: 0.75rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  padding: 10px 20px;
  background: transparent;
  border: 1px solid var(--gold-dim);
  color: var(--gold);
  cursor: pointer;
  border-radius: 6px;
  transition: all 0.2s;
}
.action-btn:hover {
  border-color: var(--gold);
  background: rgba(201,148,58,0.1);
  box-shadow: 0 0 10px rgba(201,148,58,0.2);
}

/* ============================
   CHAT
   ============================ */

.chat-panel {
  position: fixed;
  bottom: 80px;
  right: 20px;
  width: 280px;
  background: var(--panel);
  border: 1px solid rgba(201,148,58,0.2);
  border-radius: var(--radius);
  overflow: hidden;
  z-index: 10;
  box-shadow: 0 8px 32px rgba(0,0,0,0.6);
}

.history-panel { right: 320px; width: 300px; }
.history-list {
  height: 240px;
  overflow-y: auto;
  padding: 10px 12px;
  font-family: var(--font-body);
  font-size: 0.9rem;
  line-height: 1.5;
}
.history-row { display: grid; grid-template-columns: 2.2em 1.4em 2.6em 1fr; gap: 4px; }
.history-row .n { color: var(--gold-dim); text-align: right; }
.history-row .who { opacity: 0.7; }
.history-row .dice { color: var(--gold); font-weight: 600; }
.history-row.cube { font-style: italic; color: var(--gold); }
.history-empty { opacity: 0.6; font-style: italic; }
.history-actions { display: flex; gap: 8px; padding: 8px 12px; border-top: 1px solid rgba(201,148,58,0.2); }
.history-actions button {
  flex: 1; padding: 6px; cursor: pointer; border-radius: 6px;
  background: transparent; color: var(--gold); border: 1px solid var(--gold-dim);
}
.history-actions button:hover { border-color: var(--gold); }

.chat-messages {
  height: 200px;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.chat-msg {
  font-size: 0.85rem;
  line-height: 1.4;
  padding: 4px 8px;
  border-radius: 4px;
  background: rgba(255,255,255,0.03);
}
.chat-msg .from-white { color: var(--checker-white); }
.chat-msg .from-black { color: var(--gold-light); }

.chat-input-row {
  display: flex;
  border-top: 1px solid rgba(201,148,58,0.1);
}
.chat-input {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--ivory);
  padding: 8px 12px;
  font-family: var(--font-body);
  font-size: 0.85rem;
  outline: none;
}
.chat-input-row button {
  background: rgba(201,148,58,0.1);
  border: none;
  border-left: 1px solid rgba(201,148,58,0.1);
  color: var(--gold);
  padding: 8px 14px;
  cursor: pointer;
  font-family: var(--font-display);
  font-size: 0.7rem;
  letter-spacing: 0.1em;
  transition: background 0.2s;
}
.chat-input-row button:hover { background: rgba(201,148,58,0.2); }

/* ============================
   WIN OVERLAY
   ============================ */

.overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.85);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow-y: auto;
  z-index: 100;
  backdrop-filter: blur(4px);
}

.win-panel {
  background: var(--panel);
  border: 1px solid var(--gold-dim);
  border-radius: 16px;
  padding: 60px 80px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  box-shadow:
    0 0 0 1px rgba(201,148,58,0.1),
    0 20px 80px rgba(0,0,0,0.8),
    0 0 80px rgba(201,148,58,0.1);
}

.win-ornament {
  color: var(--gold);
  font-size: 2rem;
  animation: spin 4s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

#winTitle {
  font-family: var(--font-display);
  font-size: 3rem;
  color: var(--gold-light);
  text-shadow: 0 0 40px rgba(201,148,58,0.5);
}
#winMsg {
  color: var(--ivory-dim);
  font-style: italic;
  font-size: 1.1rem;
}

/* Scrollbar */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--gold-dim); border-radius: 2px; }

/* Active player highlight */
.player-active .pip-label {
  color: var(--gold) !important;
}


/* ============================
   PHONES AND NARROW WINDOWS
   ============================ */

@media (max-width: 640px) {
  /* Lobby */
  .lobby-content { gap: 22px; padding: 0 16px; width: 100%; }
  .logo { font-size: clamp(1.5rem, 8.5vw, 2.6rem); letter-spacing: 0.12em; }
  .logo-sub { font-size: 0.7rem; letter-spacing: 0.08em; }
  .logo-ornament { font-size: 1rem; }
  .menu-panel { width: 100%; max-width: 340px; }
  .menu-btn { padding: 14px 18px; }
  .sub-panel { width: 100%; max-width: 340px; padding: 18px; }
  .room-code { font-size: 1.8rem; letter-spacing: 0.2em; }

  /* Game screen: header wraps into two rows (players, then room and tools) */
  #gameScreen { padding: 6px; gap: 6px; overflow-y: auto; justify-content: flex-start; }
  .game-header { flex-wrap: wrap; row-gap: 6px; padding: 6px 10px; }
  .player-info { gap: 8px; }
  .center-info { order: 3; width: 100%; flex-wrap: wrap; justify-content: center; gap: 8px; }
  .game-code { font-size: 0.65rem; letter-spacing: 0.12em; }
  .match-info { white-space: normal; text-align: center; font-size: 0.6rem; flex-basis: 100%; order: -1; }
  .icon-btn { padding: 8px 12px; }

  .game-area { width: 100%; }
  .status-bar { font-size: 0.7rem; letter-spacing: 0.06em; padding: 0 6px; min-height: 32px; }
  .dice-area { flex-wrap: wrap; justify-content: center; gap: 10px; min-height: 64px; }
  .dice-wrap { gap: 8px; }
  .die { width: 52px; height: 52px; font-size: 1.9rem; border-radius: 10px; }
  .action-btn { padding: 12px 18px; font-size: 0.7rem; }

  /* Side panels become bottom sheets, one at a time (see toggleChat/toggleHistory) */
  .chat-panel, .history-panel { left: 8px; right: 8px; bottom: 8px; width: auto; }
  .chat-messages { height: 160px; }
  .history-list { height: 200px; }

  /* End-of-game panel */
  .win-panel { padding: 28px 20px; width: calc(100vw - 32px); gap: 14px; }
  #winTitle { font-size: 1.8rem; }
}

/* Portrait phones: the canvas is drawn on its side (2:3). Keep it within the
   screen height next to the header, dice and buttons. */
body[data-layout="portrait"] #board {
  width: min(100%, calc((100dvh - 215px) / 1.5));
  height: auto;
}

/* Landscape phones: board on the left at full height, everything else in a
   column on the right. */
@media (orientation: landscape) and (max-height: 500px) {
  #lobby { overflow-y: auto; align-items: flex-start; }
  .lobby-content { gap: 10px; padding: 12px 16px; }
  .logo-wrap { gap: 4px; }
  .logo { font-size: 1.8rem; }
  .logo-ornament { display: none; }
  .menu-panel { flex-direction: row; flex-wrap: wrap; justify-content: center; max-width: 640px; }
  .menu-btn { padding: 10px 14px; font-size: 0.75rem; }
  .match-select { flex-basis: 100%; }

  #gameScreen.active {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 210px;
    grid-template-rows: auto auto 1fr;
    column-gap: 8px;
    row-gap: 6px;
    align-items: start;
    padding: 6px;
    overflow: hidden;
  }
  /* status bar, dice and buttons become grid items next to the board */
  .game-area { display: contents; }
  #board {
    grid-column: 1;
    grid-row: 1 / 4;
    justify-self: center;
    width: min(100%, calc((100dvh - 14px) * 1.5));
    height: auto;
  }
  .game-header { grid-column: 2; grid-row: 1; max-width: none; flex-direction: column; align-items: stretch; gap: 6px; }
  .game-header .player-info, .game-header .player-info.right { flex-direction: row; justify-content: space-between; }
  .center-info { order: 0; flex-wrap: wrap; justify-content: center; }
  .match-info { flex-basis: 100%; order: -1; white-space: normal; text-align: center; }
  .status-bar { grid-column: 2; grid-row: 2; font-size: 0.65rem; letter-spacing: 0.04em; min-height: 0; }
  .dice-area { grid-column: 2; grid-row: 3; flex-wrap: wrap; justify-content: center; min-height: 0; align-self: start; }
  .die { width: 46px; height: 46px; font-size: 1.6rem; border-radius: 9px; }
  .win-panel { padding: 18px 24px; gap: 8px; }
  .chat-messages { height: 110px; }
  .history-list { height: 130px; }
  .chat-panel, .history-panel { width: 260px; left: auto; right: 8px; bottom: 8px; }
}


/* ============================
   AD SLOTS (filled by https://oyunmatik.net/ads.js)
   Empty slots take no space, so nothing shows until an ad is loaded. Slots
   are labelled and set apart from the content; they only exist in the lobby
   and on the end-of-game screen, never next to the board, dice or buttons.
   ============================ */

[data-oyunmatik-ad] { width: 100%; max-width: 340px; }

[data-oyunmatik-ad]:not(:empty) {
  margin-top: 28px;
  padding-top: 14px;
  border-top: 1px solid rgba(201,148,58,0.15);
}

[data-oyunmatik-ad]:not(:empty)::before {
  content: 'Reklam';
  display: block;
  text-align: center;
  font-family: var(--font-display);
  font-size: 0.6rem;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--gold-dim);
  margin-bottom: 8px;
}

/* Below the result panel, with a clear gap from the Next game / lobby buttons */
.overlay > [data-oyunmatik-ad]:not(:empty) { margin-top: 36px; margin-bottom: 16px; }

/* No room for ads on short landscape screens */
@media (orientation: landscape) and (max-height: 500px) {
  [data-oyunmatik-ad] { display: none !important; }
}
