/* ============================================================
   Jaynstar Chess — layout & board scaling
   Tailwind handles utilities; this file covers structure,
   mobile-first board sizing, overlays, and ad banner.
   ============================================================ */

:root {
  --header-h: 3.25rem;
  --ad-h: 3.5rem;
  --ad-h-safe: calc(var(--ad-h) + env(safe-area-inset-bottom, 0px));
  --stage-pad: 0.75rem;
  --board-max: min(
    100vw - (var(--stage-pad) * 2),
    calc(100dvh - var(--header-h) - var(--ad-h-safe) - 3.5rem),
    560px
  );
  --sq: calc(var(--board-max) / 8);
  --ink-950: #08100c;
  --ink-900: #0f1a14;
  --accent: #c9a227;
  --board-light: #e8d5a8;
  --board-dark: #6b8f71;
  --board-frame: #2a1f14;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--ink-950);
  color: #f4f7f4;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ---------- App shell ---------- */

.app-shell {
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
}

.app-bg {
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background:
    radial-gradient(ellipse 80% 60% at 50% -10%, rgba(107, 143, 113, 0.28), transparent 55%),
    radial-gradient(ellipse 50% 40% at 100% 80%, rgba(201, 162, 39, 0.08), transparent 50%),
    linear-gradient(165deg, #0f1a14 0%, #08100c 45%, #121c16 100%);
}

.app-bg::after {
  content: '';
  position: absolute;
  inset: 0;
  opacity: 0.04;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  background-size: 180px 180px;
}

.app-header,
.app-main,
.ad-banner {
  position: relative;
  z-index: 1;
}

.app-header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  height: var(--header-h);
  padding: 0 0.875rem;
  padding-top: env(safe-area-inset-top, 0px);
  border-bottom: 1px solid rgba(216, 226, 217, 0.08);
  background: rgba(8, 16, 12, 0.55);
  backdrop-filter: blur(10px);
}

.brand-mark {
  flex-shrink: 0;
  width: 1.35rem;
  height: 1.35rem;
  border-radius: 0.2rem;
  background:
    linear-gradient(135deg, var(--board-light) 50%, transparent 50%),
    linear-gradient(135deg, transparent 50%, var(--board-dark) 50%);
  background-size: 50% 50%;
  background-position: 0 0, 100% 100%;
  background-color: var(--board-frame);
  box-shadow: inset 0 0 0 1px rgba(201, 162, 39, 0.35);
}

.mode-chip {
  align-items: center;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--accent);
  background: rgba(201, 162, 39, 0.12);
  border: 1px solid rgba(201, 162, 39, 0.28);
}

.btn-ghost {
  padding: 0.35rem 0.7rem;
  border-radius: 0.4rem;
  font-size: 0.8rem;
  font-weight: 600;
  color: #d8e2d9;
  background: transparent;
  border: 1px solid rgba(216, 226, 217, 0.18);
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.btn-ghost:hover {
  background: rgba(216, 226, 217, 0.08);
  border-color: rgba(216, 226, 217, 0.32);
}

.btn-primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.75rem 1.1rem;
  border-radius: 0.5rem;
  font-size: 0.95rem;
  font-weight: 700;
  color: #0f1a14;
  background: linear-gradient(180deg, #e6c65c 0%, #c9a227 100%);
  border: none;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(201, 162, 39, 0.28);
  transition: transform 0.15s ease, filter 0.15s ease, opacity 0.15s ease;
}

.btn-primary:hover:not(:disabled) {
  filter: brightness(1.05);
  transform: translateY(-1px);
}

.btn-primary:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  box-shadow: none;
}

.btn-back {
  display: inline-flex;
  align-items: center;
  margin-bottom: 0.75rem;
  padding: 0;
  font-size: 0.85rem;
  font-weight: 600;
  color: #8aa390;
  background: none;
  border: none;
  cursor: pointer;
  transition: color 0.15s ease;
}

.btn-back:hover {
  color: #e6c65c;
}

/* ---------- Main / board stage ---------- */

.app-main {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  padding: var(--stage-pad);
  padding-bottom: calc(var(--stage-pad) + 0.25rem);
  overflow: hidden;
}

.board-stage {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.65rem;
  width: 100%;
  max-width: var(--board-max);
}

.chessboard {
  display: grid;
  grid-template-columns: repeat(8, var(--sq));
  grid-template-rows: repeat(8, var(--sq));
  width: var(--board-max);
  height: var(--board-max);
  border-radius: 0.35rem;
  overflow: hidden;
  box-shadow:
    0 0 0 3px var(--board-frame),
    0 0 0 4px rgba(201, 162, 39, 0.25),
    0 12px 40px rgba(8, 16, 12, 0.55);
  touch-action: none;
  user-select: none;
}

.square {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: var(--sq);
  height: var(--sq);
  font-size: calc(var(--sq) * 0.72);
  line-height: 1;
  cursor: pointer;
}

.square.light {
  background-color: var(--board-light);
}

.square.dark {
  background-color: var(--board-dark);
}

.square.selected {
  box-shadow: inset 0 0 0 3px rgba(201, 162, 39, 0.9);
}

.square.highlight::after {
  content: '';
  position: absolute;
  width: 28%;
  height: 28%;
  border-radius: 50%;
  background: rgba(15, 26, 20, 0.35);
  pointer-events: none;
}

.square.highlight.capture::after {
  width: auto;
  height: auto;
  inset: 0.12rem;
  border-radius: 0;
  background: transparent;
  border: 3px solid rgba(180, 48, 48, 0.55);
}

.square.last-move {
  box-shadow: inset 0 0 0 999px rgba(201, 162, 39, 0.28);
}

.chessboard.board-locked {
  cursor: wait;
  pointer-events: none;
  filter: saturate(0.92);
}

.chessboard.board-locked .piece {
  opacity: 0.92;
}

.square .coord {
  position: absolute;
  font-size: max(0.55rem, calc(var(--sq) * 0.14));
  font-weight: 600;
  line-height: 1;
  opacity: 0.55;
  pointer-events: none;
}

.square .coord-file {
  right: 0.15rem;
  bottom: 0.1rem;
}

.square .coord-rank {
  left: 0.15rem;
  top: 0.1rem;
}

.square.light .coord {
  color: var(--board-dark);
}

.square.dark .coord {
  color: var(--board-light);
}

.piece {
  display: block;
  font-size: inherit;
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.25));
  transition: transform 0.12s ease;
}

.square:active .piece {
  transform: scale(1.08);
}

.status-bar {
  width: 100%;
  text-align: center;
  font-size: 0.8rem;
  font-weight: 500;
  color: #8aa390;
  min-height: 1.25rem;
}

/* ---------- Overlays ---------- */

.overlay {
  position: absolute;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  padding-bottom: calc(var(--ad-h-safe) + 1rem);
  background: rgba(8, 16, 12, 0.72);
  backdrop-filter: blur(8px);
}

.overlay.hidden {
  display: none;
}

.overlay-panel {
  width: 100%;
  max-width: 22rem;
  padding: 1.35rem 1.25rem 1.4rem;
  border-radius: 0.75rem;
  background:
    linear-gradient(165deg, rgba(44, 61, 49, 0.95) 0%, rgba(15, 26, 20, 0.98) 100%);
  border: 1px solid rgba(201, 162, 39, 0.22);
  box-shadow:
    0 0 0 1px rgba(216, 226, 217, 0.04),
    0 20px 50px rgba(0, 0, 0, 0.45);
}

.overlay-brand {
  margin: 0 0 0.35rem;
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: #f4f7f4;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.25);
}

.overlay-title {
  margin: 0 0 0.4rem;
  font-family: 'Playfair Display', Georgia, serif;
  font-size: 1.15rem;
  font-weight: 600;
  line-height: 1.25;
  color: #f4f7f4;
}

.overlay-sub {
  margin: 0 0 1.15rem;
  font-size: 0.875rem;
  line-height: 1.45;
  color: #8aa390;
}

.mode-choices {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.mode-choice {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.2rem;
  width: 100%;
  padding: 0.9rem 1rem;
  text-align: left;
  border-radius: 0.55rem;
  background: rgba(8, 16, 12, 0.45);
  border: 1px solid rgba(216, 226, 217, 0.12);
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease, box-shadow 0.15s ease;
}

.mode-choice:hover,
.mode-choice:focus-visible {
  outline: none;
  border-color: rgba(201, 162, 39, 0.55);
  background: rgba(201, 162, 39, 0.08);
  box-shadow: 0 0 0 1px rgba(201, 162, 39, 0.2);
}

.mode-choice-title {
  font-size: 0.95rem;
  font-weight: 700;
  color: #f4f7f4;
}

.mode-choice-desc {
  font-size: 0.78rem;
  color: #8aa390;
}

/* ---------- Difficulty selector ---------- */

.difficulty-fieldset {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin: 0;
  padding: 0;
  border: none;
}

.difficulty-option {
  display: block;
  cursor: pointer;
}

.difficulty-option input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.difficulty-card {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  padding: 0.7rem 0.85rem;
  border-radius: 0.5rem;
  background: rgba(8, 16, 12, 0.4);
  border: 1px solid rgba(216, 226, 217, 0.12);
  transition: border-color 0.15s ease, background 0.15s ease, box-shadow 0.15s ease;
}

.difficulty-option input:checked + .difficulty-card {
  border-color: rgba(201, 162, 39, 0.65);
  background: rgba(201, 162, 39, 0.12);
  box-shadow: 0 0 0 1px rgba(201, 162, 39, 0.25);
}

.difficulty-option:hover .difficulty-card {
  border-color: rgba(201, 162, 39, 0.4);
}

.difficulty-name {
  font-size: 0.9rem;
  font-weight: 700;
  color: #f4f7f4;
}

.difficulty-hint {
  font-size: 0.75rem;
  color: #8aa390;
}

/* ---------- Ad banner ---------- */

.ad-banner {
  flex: 0 0 auto;
  width: 100%;
  min-height: var(--ad-h);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  background: rgba(8, 16, 12, 0.9);
  border-top: 1px solid rgba(216, 226, 217, 0.1);
}

.ad-banner-inner {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  max-width: 728px;
  min-height: var(--ad-h);
  margin: 0 auto;
  overflow: hidden;
  background: rgba(15, 26, 20, 0.85);
}

/* Standard AdSense / AdMob container sizing */
.ad-banner .adsbygoogle,
.ad-banner.admob-banner .ad-banner-inner {
  width: 100%;
  min-height: 50px;
  max-height: 90px;
}

.ad-placeholder-label {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: rgba(138, 163, 144, 0.55);
  pointer-events: none;
}

/* Hide placeholder label once a real ad fills the slot */
.ad-banner.has-ad .ad-placeholder-label {
  display: none;
}

/* ---------- Desktop centering ---------- */

@media (min-width: 640px) {
  :root {
    --header-h: 3.5rem;
    --ad-h: 5.625rem; /* ~90px standard leaderboard */
    --stage-pad: 1.25rem;
    --board-max: min(
      100vw - (var(--stage-pad) * 2),
      calc(100dvh - var(--header-h) - var(--ad-h-safe) - 4rem),
      640px
    );
  }

  .overlay-panel {
    max-width: 24rem;
    padding: 1.6rem 1.5rem 1.65rem;
  }

  .overlay-brand {
    font-size: 1.55rem;
  }

  .overlay-title {
    font-size: 1.25rem;
  }
}

@media (min-width: 1024px) {
  :root {
    --board-max: min(
      100vw - (var(--stage-pad) * 2),
      calc(100dvh - var(--header-h) - var(--ad-h-safe) - 4.5rem),
      720px
    );
  }

  .app-header {
    padding-left: 1.5rem;
    padding-right: 1.5rem;
  }
}

/* Very short phones: keep board fully visible, shrink status */
@media (max-height: 620px) {
  :root {
    --board-max: min(
      100vw - (var(--stage-pad) * 2),
      calc(100dvh - var(--header-h) - var(--ad-h-safe) - 2.5rem),
      480px
    );
  }

  .status-bar {
    font-size: 0.72rem;
  }
}

/* Prefer reduced motion */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
