:root {
  --brick: #c0392b;
  --gem: #2980d9;
  --beast: #27ae60;
  --bg: #14100c;
  --panel: #241c14;
  --panel2: #2f2519;
  --text: #e8dcc0;
  --dim: #a89878;
  color-scheme: dark;
}

:focus-visible { outline: 3px solid #f0c75e; outline-offset: 2px; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
  .card:hover { transform: none; }
}

* { box-sizing: border-box; }

#backdrop {
  position: fixed;
  inset: 0;
  background-size: cover;
  background-position: center;
  filter: brightness(0.45) saturate(0.9);
  z-index: -1;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Georgia, 'Times New Roman', serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px 18px;
  background: rgba(36, 28, 20, 0.85);
  border-bottom: 2px solid #4a3a24;
  backdrop-filter: blur(2px);
}

header h1 { margin: 0; font-size: 26px; letter-spacing: 2px; color: #f0c75e; }

#tavern-bar { display: flex; gap: 12px; align-items: center; }
#tavern-bar select, #tavern-bar button {
  background: var(--panel2);
  color: var(--text);
  border: 1px solid #4a3a24;
  padding: 5px 10px;
  font-family: inherit;
  cursor: pointer;
}

#board {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(260px, 320px) minmax(0, 1fr);
  gap: 12px;
  padding: 12px;
  min-height: 0;
}

.side {
  background: rgba(36, 28, 20, 0.88);
  border: 2px solid #4a3a24;
  border-radius: 8px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  backdrop-filter: blur(2px);
}

.side h2 { margin: 0; font-size: 18px; color: #f0c75e; }

.foe-header { display: flex; gap: 10px; align-items: flex-start; min-height: 96px; }
#foe-portrait, #player-portrait { flex-shrink: 0; line-height: 0; width: 96px; height: 96px; }
#foe-portrait svg, #foe-portrait img, #player-portrait svg, #player-portrait img { border-radius: 50%; width: 96px; height: 96px; object-fit: cover; display: block; }
.header-text { min-width: 0; flex: 1; }
#foe-title, #player-title { font-size: 12px; color: var(--dim); font-style: italic; }
#foe-backstory { font-size: 11px; color: var(--dim); line-height: 1.35; margin-top: 4px; }
#player-portrait-picker { display: block; font-size: 11px; color: var(--dim); margin-top: 4px; }
#player-portrait-picker select {
  background: var(--panel2); color: var(--text);
  border: 1px solid #4a3a24; padding: 2px 6px;
  font-family: inherit; font-size: 11px; max-width: 100%;
}

.stats {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 10px;
  font-size: 14px;
}
.stats .label { color: var(--dim); }
.stats .label b.tower-name, .stats .label b.wall-name { color: #f0c75e; font-size: 15px; letter-spacing: 0.5px; }
.stats .label:has(b.tower-name), .stats .label:has(b.wall-name) { color: #f0c75e; }
.stats span:has(+ .bar) { font-size: 15px; }
.stats > span:nth-child(3), .stats > span:nth-child(4) { font-size: 15px; font-weight: bold; }
.stats .label.income-pulse .res-dot { animation: dot-pulse 1.2s ease-out; }
@keyframes dot-pulse {
  0% { box-shadow: 0 0 0 0 rgba(240, 199, 94, 0.8); transform: scale(1); }
  40% { box-shadow: 0 0 8px 3px rgba(240, 199, 94, 0.55); transform: scale(1.5); }
  100% { box-shadow: 0 0 0 0 rgba(240, 199, 94, 0); transform: scale(1); }
}
.stats .label .res-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 4px; }
.stats .label .res-dot.brick { background: var(--brick); }
.stats .label .res-dot.gem { background: var(--gem); }
.stats .label .res-dot.beast { background: var(--beast); }
.stats .bar { height: 10px; background: #000; border: 1px solid #4a3a24; border-radius: 5px; overflow: hidden; align-self: center; min-width: 40px; }
.stats .bar > div { height: 100%; background: #f0c75e; transition: width 0.8s ease; }
.stats span { white-space: nowrap; }
.stats .delta { font-size: 12px; margin-left: 5px; font-weight: bold; animation: delta-fade 2.2s forwards; }
.stats .delta.up { color: #7fe0a3; }
.stats .delta.down { color: #ff9d8f; }
.stats span.flash-up { animation: flash-up 1.6s ease-out; background-color: rgba(127, 224, 163, 0.22); border-radius: 4px; }
.stats span.flash-down { animation: flash-down 1.6s ease-out; background-color: rgba(255, 157, 143, 0.22); border-radius: 4px; }
.stats .bar.flash-up > div { background: #7fe0a3; }
.stats .bar.flash-down > div { background: #ff9d8f; }
@keyframes delta-fade { 0% { opacity: 1; transform: translateY(0); } 70% { opacity: 1; } 100% { opacity: 0; transform: translateY(-4px); } }
@keyframes flash-up { 0% { color: #7fe0a3; } 30% { color: #7fe0a3; } 100% { color: inherit; } }
@keyframes flash-down { 0% { color: #ff9d8f; } 30% { color: #ff9d8f; } 100% { color: inherit; } }

#log .turn-header { color: #f0c75e; text-align: center; margin: 4px 0; font-size: 11px; letter-spacing: 1px; }

.hand {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-content: flex-start;
}

.card {
  width: 128px;
  min-height: 178px;
  border-radius: 8px;
  border: 2px solid #000;
  padding: 5px;
  font-size: 11px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 4px;
  transition: transform 0.1s, box-shadow 0.1s;
  user-select: none;
}
.card:hover { transform: translateY(-4px); box-shadow: 0 4px 10px rgba(0,0,0,0.6); }
.card.brick { background: linear-gradient(160deg, #7e2a1e, #4a150d); border-color: var(--brick); }
.card.gem { background: linear-gradient(160deg, #1d5a9e, #0d2f57); border-color: var(--gem); }
.card.beast { background: linear-gradient(160deg, #1e7a44, #0e3d22); border-color: var(--beast); }
.card.unaffordable { opacity: 0.65; filter: saturate(0.35); cursor: not-allowed; }
.card.unaffordable:hover { transform: none; box-shadow: none; }
.card.unaffordable .name::after { content: ' (not enough resources)'; font-weight: normal; font-size: 9px; color: var(--dim); }
.card:hover, .card:focus-visible { z-index: 2; }

.hand.discard-mode .card { border-color: var(--brick); cursor: crosshair; }
.hand.draw-discard-mode .card { border-color: var(--gem); cursor: crosshair; }

#turn-controls { display: flex; gap: 8px; margin-top: auto; }
#turn-controls button {
  background: var(--panel2);
  color: var(--text);
  border: 1px solid #4a3a24;
  padding: 6px 10px;
  font-family: inherit;
  cursor: pointer;
  flex: 1;
}
#turn-controls button:disabled { opacity: 0.5; cursor: not-allowed; }
.card .art {
  position: relative;
  border: 1px solid rgba(0,0,0,0.6);
  border-radius: 5px;
  overflow: hidden;
  line-height: 0;
  aspect-ratio: 118 / 64;
}
.card .art svg, .card .art img { width: 100%; height: 100%; object-fit: cover; display: block; }
.card .art::after {
  content: '';
  position: absolute;
  inset: 0;
  border: 2px solid rgba(240,199,94,0.55);
  border-radius: 4px;
  box-shadow: inset 0 0 12px rgba(0,0,0,0.55);
  pointer-events: none;
}
.card .name { font-weight: bold; font-size: 12px; text-align: center; }
.card .text {
  flex: 1;
  line-height: 1.25;
  background: rgba(0,0,0,0.35);
  border-radius: 4px;
  padding: 3px 4px;
}
.card .bottom { display: flex; align-items: center; justify-content: space-between; min-height: 24px; }
.card .cost {
  background: rgba(0,0,0,0.55);
  border-radius: 50%;
  width: 24px; height: 24px;
  display: flex; align-items: center; justify-content: center;
  font-weight: bold; font-size: 13px;
  flex-shrink: 0;
  border: 2px solid transparent;
}
.card .cost.brick { color: #ff8a75; border-color: var(--brick); }
.card .cost.gem { color: #8fc3ff; border-color: var(--gem); }
.card .cost.beast { color: #7fe0a3; border-color: var(--beast); }
.card .tag { font-size: 9px; color: var(--dim); text-transform: uppercase; text-align: right; }

.hidden-cards { display: flex; gap: 8px; flex-wrap: wrap; align-content: flex-start; }
.card-back {
  width: 128px; height: 178px;
  border-radius: 8px;
  border: 2px solid #000;
  line-height: 0;
  transition: transform 0.15s;
  position: relative;
  overflow: hidden;
  background: #241a10;
}
.card-back:hover { transform: translateY(-3px); }
/* Mirror the front card's art treatment: 1px dark inner line, then the gold
   frame + inset vignette from .card .art::after, so backs and fronts read as
   the same deck. */
.card-back svg { width: 100%; height: 100%; display: block; border-radius: 6px; }
.card-back img {
  width: 100%; height: 100%;
  display: block;
  border-radius: 6px;
  object-fit: cover;
  border: 1px solid rgba(0,0,0,0.6);
}
.card-back::after {
  content: '';
  position: absolute;
  inset: 5px;
  border: 2px solid rgba(240,199,94,0.6);
  border-radius: 5px;
  box-shadow: inset 0 0 14px rgba(0,0,0,0.65);
  pointer-events: none;
}

#center {
  background: rgba(36, 28, 20, 0.88);
  border: 2px solid #4a3a24;
  border-radius: 8px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow: hidden;
  backdrop-filter: blur(2px);
}

.side.active-turn { border-color: #f0c75e; box-shadow: 0 0 14px rgba(240, 199, 94, 0.35); }
#foe-side.active-turn { border-color: #ff9d8f; box-shadow: 0 0 16px rgba(255, 157, 143, 0.4); }
#foe-side.active-turn h2 { color: #ff9d8f; }
#foe-hand { transition: filter 0.4s; }
#foe-side.active-turn #foe-hand { filter: drop-shadow(0 0 6px rgba(255, 157, 143, 0.55)); }
#foe-side.active-turn .card-back { filter: drop-shadow(0 0 5px rgba(255, 157, 143, 0.7)); }

#played-slot {
  display: flex;
  justify-content: center;
  min-height: 0;
  height: 0;
  overflow: visible;
  pointer-events: none;
}
#played-slot .card, #played-slot .card-back.big {
  width: 150px;
  min-height: 210px;
  font-size: 12px;
  opacity: 0;
  transform-origin: center;
  box-shadow: 0 10px 30px rgba(0,0,0,0.7);
}
#played-slot .card .name { font-size: 15px; }
#played-slot .card .text { font-size: 12px; }
#played-slot .card .cost { width: 30px; height: 30px; font-size: 16px; }
#played-slot .card-back.big { width: 150px; height: 210px; }
#played-slot.foe .card { border-color: #ff9d8f; box-shadow: 0 0 24px rgba(255, 157, 143, 0.45), 0 10px 30px rgba(0,0,0,0.7); }
#played-slot.foe::after {
  content: 'OPPONENT';
  position: absolute;
  top: -8px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 9px;
  letter-spacing: 2px;
  color: #ff9d8f;
  background: rgba(36, 28, 20, 0.9);
  padding: 1px 6px;
  border-radius: 4px;
  border: 1px solid rgba(255, 157, 143, 0.5);
}
#played-slot { position: relative; }
#played-slot.show .card, #played-slot.show .card-back {
  animation: slot-pop 2.6s forwards;
}
#played-slot.burn .card, #played-slot.burn .card-back {
  animation: slot-burn 1.4s forwards;
}
@keyframes slot-pop {
  0% { opacity: 0; transform: scale(0.5) translateY(24px); }
  10% { opacity: 1; transform: scale(1.12) translateY(0); }
  18% { transform: scale(1.04) translateY(-2px); }
  24% { transform: scale(1) translateY(0); }
  75% { opacity: 1; transform: scale(1) translateY(0); }
  100% { opacity: 0; transform: scale(0.96) translateY(-8px); }
}

#thinking {
  text-align: center;
  color: var(--dim);
  font-style: italic;
  font-size: 13px;
  padding: 2px 0;
}
#thinking.hidden { display: none; }
#thinking .dots i {
  display: inline-block;
  animation: think-dot 1.2s infinite;
  font-style: normal;
}
#thinking .dots i:nth-child(2) { animation-delay: 0.2s; }
#thinking .dots i:nth-child(3) { animation-delay: 0.4s; }
@keyframes think-dot {
  0%, 60%, 100% { opacity: 0.2; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-3px); }
}
@keyframes slot-burn {
  0% { opacity: 1; filter: none; transform: scale(1); }
  45% { opacity: 0.95; filter: brightness(2.4) saturate(0.3); transform: scale(1.03) rotate(-2deg); }
  100% { opacity: 0; filter: brightness(0.15) sepia(1) hue-rotate(-45deg); transform: scale(0.65) translateY(16px) rotate(4deg); }
}

#turn-info {
  font-size: 13px;
  color: var(--dim);
  display: flex;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
}
#turn-info .danger { color: #ff9d8f; font-weight: bold; }

#goal-strip {
  font-size: 11px;
  color: var(--dim);
  border-top: 1px solid #4a3a24;
  padding-top: 6px;
  line-height: 1.4;
}
#goal-strip b { color: #f0c75e; font-weight: normal; }

#message { font-size: 15px; color: #f0c75e; min-height: 22px; text-align: center; }

#log {
  flex: 1;
  overflow-y: auto;
  font-size: 12px;
  line-height: 1.5;
  color: var(--dim);
}
#log .you { color: #8fd0ff; }
#log .foe { color: #ff9d8f; }
#log .sys { color: #f0c75e; }

#overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.75);
  display: flex; align-items: center; justify-content: center;
}
#overlay.hidden { display: none; }
#overlay-box {
  background: var(--panel);
  border: 3px solid #f0c75e;
  border-radius: 10px;
  padding: 30px 40px;
  text-align: center;
  max-width: min(90vw, 520px);
}
#confirm-modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center;
  z-index: 10;
}
#confirm-modal.hidden { display: none; }
#confirm-box {
  background: var(--panel);
  border: 2px solid #f0c75e;
  border-radius: 10px;
  padding: 20px 28px;
  text-align: center;
  max-width: min(90vw, 420px);
}
#confirm-box h2 { color: #f0c75e; margin: 0 0 8px; font-size: 18px; }
#confirm-box p { margin: 0 0 14px; color: var(--text); font-size: 14px; }
#confirm-box button {
  background: var(--panel2); color: var(--text);
  border: 1px solid #f0c75e; padding: 7px 16px;
  font-family: inherit; font-size: 14px; cursor: pointer;
  margin: 0 4px;
}
#confirm-box #confirm-no { border-color: #4a3a24; }

.card.playing { animation: card-play 0.6s forwards; }
@keyframes card-play {
  0% { transform: translateY(0) scale(1); opacity: 1; }
  100% { transform: translateY(-46px) scale(1.12); opacity: 0; }
}

#overlay-box button { margin: 6px 4px 0; }
#overlay-stats {
  margin: 12px auto 4px;
  font-size: 13px;
  color: var(--dim);
  display: grid;
  grid-template-columns: auto auto auto;
  gap: 3px 14px;
  text-align: left;
  max-width: 380px;
}
#overlay-stats b { color: var(--text); font-weight: normal; }

@media (max-width: 980px) {
  #board {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto minmax(200px, 40vh) auto;
  }
  #center { order: 2; }
  #foe-side { order: 1; }
  #player-side { order: 3; }
  .foe-header { align-items: center; }
  #foe-backstory { max-width: none; }
}
#overlay-box #overlay-close {
  background: var(--panel2); color: var(--text);
  border: 1px solid #4a3a24; padding: 8px 18px;
  font-family: inherit; font-size: 15px; cursor: pointer;
}
#overlay-box h2 { color: #f0c75e; margin-top: 0; }
#overlay-box button {
  background: var(--panel2); color: var(--text);
  border: 1px solid #f0c75e; padding: 8px 18px;
  font-family: inherit; font-size: 15px; cursor: pointer;
}
