/* ── Reset & base ──────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:          #0f0f1a;
  --surface:     #1a1a2e;
  --surface2:    #16213e;
  --accent:      #e94560;
  --accent2:     #0f3460;
  --text:        #eaeaea;
  --text-muted:  #8888aa;
  --radius:      1rem;
  --shadow:      0 8px 32px rgba(0,0,0,.5);
  --transition:  .2s ease;
  --font-body:   'Nunito', system-ui, sans-serif;
  --font-title:  'Fredoka', 'Nunito', system-ui, sans-serif;
}

body.light {
  --bg:         #f0f2f8;
  --surface:    #ffffff;
  --surface2:   #e4e8f4;
  --accent:     #d63151;
  --accent2:    #1a5276;
  --text:       #1a1a2e;
  --text-muted: #5a5a80;
  --shadow:     0 8px 32px rgba(0,0,0,.12);
}

/* Follow OS preference when the user hasn't made an explicit choice */
@media (prefers-color-scheme: light) {
  body:not(.dark) {
    --bg:         #f0f2f8;
    --surface:    #ffffff;
    --surface2:   #e4e8f4;
    --accent:     #d63151;
    --accent2:    #1a5276;
    --text:       #1a1a2e;
    --text-muted: #5a5a80;
    --shadow:     0 8px 32px rgba(0,0,0,.12);
  }
  body:not(.dark) .coin {
    background: #e8a820;
    box-shadow: 0 2px 6px rgba(0,0,0,.18), inset 0 1px 2px rgba(255,255,255,.4);
  }
}

body {
  font-family: var(--font-body);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  overflow-x: hidden;
}

#app {
  position: relative;
  min-height: 100vh;
  overflow-x: hidden;
}

/* ── Screens ───────────────────────────────────────────────── */
.screen {
  display: flex;
  width: 100%;
  min-height: 100vh;
  align-items: flex-start;
  justify-content: center;
  padding: 2rem 1rem;
  /* animation state */
  opacity: 0;
  pointer-events: none;
  position: absolute;
  top: 0;
  left: 0;
  transform: translateY(12px);
  transition: opacity .22s ease, transform .22s ease;
}
.screen.active {
  position: relative;
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}
.screen.exit {
  opacity: 0;
  transform: translateY(-10px);
}
/* ── Menu card ─────────────────────────────────────────────── */
.menu-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 2.5rem 2rem;
  width: 100%;
  max-width: 480px;
  display: flex;
  flex-direction: column;
  gap: 1.75rem;
}

.title {
  font-size: 2.4rem;
  font-family: var(--font-title);
  font-weight: 700;
  letter-spacing: .03em;
  background: linear-gradient(135deg, #e94560, #f5a623);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  text-align: center;
}

.subtitle {
  text-align: center;
  color: var(--text-muted);
  font-size: .95rem;
  margin-top: -.75rem;
}

/* ── Section headings ──────────────────────────────────────── */
.section { display: flex; flex-direction: column; gap: 1rem; }
.section h2 {
  font-size: 1rem;
  text-transform: uppercase;
  letter-spacing: .1em;
  color: var(--text-muted);
  border-bottom: 1px solid var(--surface2);
  padding-bottom: .4rem;
}
.subsection { display: flex; flex-direction: column; gap: .6rem; }
.section h3 { font-size: .9rem; color: var(--text-muted); }

/* ── Toggle buttons ────────────────────────────────────────── */
.toggle-group {
  display: flex;
  gap: .5rem;
  background: var(--surface2);
  border-radius: .6rem;
  padding: .3rem;
}
.toggle-btn {
  flex: 1;
  padding: .5rem;
  border: none;
  border-radius: .4rem;
  background: transparent;
  color: var(--text-muted);
  font-size: .9rem;
  cursor: pointer;
  transition: background var(--transition), color var(--transition);
}
.toggle-btn.active {
  background: var(--accent);
  color: #fff;
  font-weight: 700;
}

/* ── Difficulty list ───────────────────────────────────────── */
.difficulty-list {
  display: flex;
  flex-direction: column;
  gap: .4rem;
}
.difficulty-list label {
  display: flex;
  align-items: center;
  gap: .6rem;
  cursor: pointer;
  font-size: .92rem;
  color: var(--text);
  padding: .35rem .5rem;
  border-radius: .4rem;
  transition: background var(--transition);
}
.difficulty-list label:hover { background: var(--surface2); }
.difficulty-list input[type="radio"] { accent-color: var(--accent); }

/* ── Number inputs ─────────────────────────────────────────── */
.input-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}
.input-row label { font-size: .95rem; }
.input-row input[type="number"] {
  width: 80px;
  padding: .4rem .6rem;
  background: var(--surface2);
  border: 2px solid transparent;
  border-radius: .5rem;
  color: var(--text);
  font-size: 1rem;
  text-align: center;
  outline: none;
  transition: border-color var(--transition);
}
.input-row input[type="number"]:focus { border-color: var(--accent); }

/* ── Action buttons ────────────────────────────────────────── */
.menu-actions { display: flex; gap: .75rem; flex-direction: column; }

.btn-primary {
  padding: .85rem;
  background: linear-gradient(135deg, #e94560, #c0392b);
  border: none;
  border-radius: .7rem;
  color: #fff;
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: .04em;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(233,69,96,.35);
  transition: transform var(--transition), box-shadow var(--transition);
}
.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(233,69,96,.5);
}
.btn-primary:active { transform: translateY(0); }

.btn-secondary {
  padding: .7rem;
  background: transparent;
  border: 2px solid var(--surface2);
  border-radius: .7rem;
  color: var(--text-muted);
  font-size: .95rem;
  cursor: pointer;
  transition: border-color var(--transition), color var(--transition);
}
.btn-secondary:hover { border-color: var(--accent); color: var(--text); }

/* ── Modal ─────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 100;
  backdrop-filter: blur(4px);
}
.modal-overlay.hidden { display: none; }

.error-card {
  max-width: 340px;
  text-align: center;
  animation: pop .25s cubic-bezier(.175,.885,.32,1.275);
}
.error-card h2 {
  font-size: 1.2rem;
  color: var(--accent);
  -webkit-text-fill-color: unset;
  background: none;
}

.modal-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 2rem 1.75rem;
  max-width: 420px;
  max-height: 90vh;
  overflow-y: auto;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.modal-card a { color: var(--accent); }
.modal-card h2 {
  font-size: 1.4rem;
  background: linear-gradient(135deg, #e94560, #f5a623);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.modal-card p { color: var(--text-muted); line-height: 1.6; }
.modal-card strong, .modal-card em { color: var(--text); }

/* ── Game screen ───────────────────────────────────────────── */
#screen-game {
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding: 2rem 1rem;
  gap: 1.5rem;
}

.game-header {
  width: 100%;
  max-width: 800px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.game-header h2 { font-size: 1.2rem; color: var(--text-muted); }
.turn-label {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--accent);
  transition: color var(--transition);
  margin-right: auto;
}

/* ── Board ─────────────────────────────────────────────────── */
.board-wrapper {
  width: 100%;
  max-width: 800px;
  overflow-x: auto;
}

.board {
  display: grid;
  gap: .4rem;
  padding: 1.5rem 1rem;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  /* grid-template-columns set dynamically by JS */
}

/* Turn corners are regular .square elements, no extra rules needed */

.square {
  aspect-ratio: 1;
  min-width: 0;
  border-radius: .5rem;
  background: var(--surface2);
  border: 2px solid transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: .7rem;
  color: var(--text-muted);
  position: relative;
  cursor: default;
  transition: border-color var(--transition), background var(--transition);
  user-select: none;
}
.square .sq-label {
  position: absolute;
  bottom: 3px;
  right: 5px;
  font-size: .6rem;
  color: var(--text-muted);
  opacity: .4;
}

.square.highlight {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  cursor: pointer;
}
.square.highlight:hover { background: color-mix(in srgb, var(--accent) 22%, transparent); }

/* ── Coin ──────────────────────────────────────────────────── */
.coin {
  width: 75%;
  height: 75%;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #f5a623, #c07000);
  box-shadow: 0 4px 10px rgba(0,0,0,.5), inset 0 2px 4px rgba(255,255,255,.25);
  cursor: grab;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: .65rem;
  font-weight: 700;
  color: #7a4500;
  transition: transform var(--transition), box-shadow var(--transition);
  position: relative;
  z-index: 1;
}
body.light .coin {
  background: #e8a820;
  box-shadow: 0 2px 6px rgba(0,0,0,.18), inset 0 1px 2px rgba(255,255,255,.4);
}
.coin:active { cursor: grabbing; }
.coin.selected {
  transform: scale(1.15);
  box-shadow: 0 0 0 3px var(--accent), 0 6px 16px rgba(0,0,0,.6);
}
.coin.dragging {
  opacity: .5;
  transform: scale(1.05);
}

/* Touch drag ghost — fixed, follows the finger */
.coin.touch-ghost {
  position: fixed;
  pointer-events: none;
  /* width/height set explicitly by JS from getBoundingClientRect */
  transform: translate(-50%, -50%) scale(1.2);
  z-index: 999;
  opacity: .85;
}

/* ── Placement phase info ──────────────────────────────────── */
.placement-info {
  font-size: 1rem;
  color: var(--text-muted);
  text-align: center;
}

/* ── End-of-game overlay ───────────────────────────────────── */
.winner-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.75);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  backdrop-filter: blur(6px);
}
.winner-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 2.5rem 2rem;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  max-width: 360px;
  width: 90%;
  animation: pop .35s cubic-bezier(.175,.885,.32,1.275);
}
.winner-card h2 {
  font-size: 1.8rem;
  font-weight: 900;
  background: linear-gradient(135deg, #e94560, #f5a623);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.winner-card p { color: var(--text-muted); }
.winner-actions { display: flex; gap: .75rem; flex-direction: column; }

@keyframes pop {
  from { transform: scale(.8); opacity: 0; }
  to   { transform: scale(1);  opacity: 1; }
}

/* ── Coin drop animation ───────────────────────────────────── */
@keyframes coinLand {
  0%   { transform: scale(1); }
  30%  { transform: scale(1.18); }
  65%  { transform: scale(.93); }
  100% { transform: scale(1); }
}
.coin.landing { animation: coinLand .28s ease-out forwards; }

/* ── Analysis modal ────────────────────────────────────────── */
.analysis-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 200;
  backdrop-filter: blur(6px);
}

.analysis-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  width: 100%;
  max-width: 480px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  animation: pop .35s cubic-bezier(.175,.885,.32,1.275);
  overflow: hidden;
}

.analysis-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.25rem 1.5rem 1rem;
  border-bottom: 1px solid var(--surface2);
  flex-shrink: 0;
}
.analysis-header h2 {
  font-size: 1.2rem;
  font-weight: 800;
  background: linear-gradient(135deg, #e94560, #f5a623);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.analysis-close {
  padding: .25rem .6rem;
  font-size: .9rem;
  border-radius: .4rem;
}

.analysis-body {
  overflow-y: auto;
  padding: 1rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: .75rem;
  flex: 1;
}

.a-initial {
  display: flex;
  align-items: center;
  gap: .6rem;
  flex-wrap: wrap;
  padding-bottom: .75rem;
  border-bottom: 1px solid var(--surface2);
}
.a-label {
  font-size: .8rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .08em;
}

/* Tags */
.atag {
  font-size: .72rem;
  font-weight: 700;
  padding: .15rem .5rem;
  border-radius: .3rem;
  letter-spacing: .04em;
}
.atag.winning  { background: color-mix(in srgb, #4ade80 15%, var(--surface)); color: #16a34a; }
.atag.losing   { background: color-mix(in srgb, #f87171 15%, var(--surface)); color: #dc2626; }
.atag.blunder  { background: color-mix(in srgb, #fb923c 15%, var(--surface)); color: #ea580c; }
body:not(.light) .atag.winning { color: #4ade80; }
body:not(.light) .atag.losing  { color: #f87171; }
body:not(.light) .atag.blunder { color: #fb923c; }

/* Timeline */
.a-timeline {
  display: flex;
  flex-direction: column;
}

.a-divider {
  width: 2px;
  height: 1rem;
  background: var(--surface2);
  margin-left: 1.25rem;
  flex-shrink: 0;
}

.a-move {
  display: grid;
  grid-template-columns: 5.5rem 4rem 1fr;
  align-items: center;
  gap: .5rem;
  padding: .55rem .75rem;
  border-radius: .5rem;
  border-left: 3px solid transparent;
}
.a-move.p1 { border-left-color: #e94560; }
.a-move.p2 { border-left-color: var(--accent2); }
.a-move.good    { background: color-mix(in srgb, #4ade80 10%, var(--surface)); }
.a-move.blunder { background: color-mix(in srgb, #fb923c 10%, var(--surface)); }

.a-move-who {
  font-size: .8rem;
  font-weight: 700;
  color: var(--text-muted);
  white-space: nowrap;
}
.a-move-arrow {
  font-size: .95rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  white-space: nowrap;
}
.a-move-tags {
  display: flex;
  flex-wrap: wrap;
  gap: .3rem;
  justify-content: flex-end;
}

.a-outcome {
  text-align: center;
  font-size: 1.1rem;
  font-weight: 800;
  padding: .6rem;
  border-radius: .5rem;
  background: color-mix(in srgb, var(--accent) 10%, var(--surface));
  color: var(--text);
  border-top: 1px solid var(--surface2);
  margin-top: .25rem;
}

.analysis-footer {
  display: flex;
  gap: .75rem;
  padding: 1rem 1.5rem;
  border-top: 1px solid var(--surface2);
  flex-shrink: 0;
}
.analysis-footer .btn-primary   { flex: 1; }
.analysis-footer .btn-secondary { flex: 1; }

/* ── Responsive tweaks ─────────────────────────────────────── */
@media (max-width: 480px) {
  .menu-card { padding: 1.75rem 1.25rem; }
  .title { font-size: 1.9rem; }
}
