:root {
  --bg: #14181c;
  --panel: #1f262d;
  --ink: #e9edf1;
  --muted: #93a0aa;
  --monster: #b3463c;
  --weapon: #3c79b3;
  --potion: #3cab6e;
  --warn: #e0533d;
  --good: #3cab6e;
  --gold: #c9a44c;
  --gold-bright: #e8c773;
  --stone: #2b2722;
  --stone-dark: #181512;
  --blood: #7a1f1f;
  --blood-bright: #c23a3a;
  font-family: "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

body {
  margin: 0;
  background:
    radial-gradient(circle at 50% 0%, rgba(201, 164, 76, 0.06), transparent 60%),
    var(--bg);
  color: var(--ink);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  overscroll-behavior-y: contain;
}

button { touch-action: manipulation; }

#hud {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 10px 16px;
  padding: 12px 20px;
  padding-top: max(12px, env(safe-area-inset-top));
  background: linear-gradient(180deg, rgba(43, 39, 34, 0.88), rgba(24, 21, 18, 0.88));
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  border-bottom: 3px solid var(--gold);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
}

#turn-panel { display: flex; align-items: center; gap: 12px; }

#hud-actions { display: flex; align-items: center; gap: 10px; }

#rules-btn, #new-game-btn {
  background: linear-gradient(180deg, var(--stone) 0%, var(--stone-dark) 100%);
  color: var(--gold-bright);
  border: 2px solid var(--gold);
  padding: 8px 14px;
  min-height: 40px;
  border-radius: 4px;
  font-weight: 700;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  cursor: pointer;
  transition: box-shadow 0.15s ease, color 0.15s ease, transform 0.1s ease;
}

#rules-btn:hover, #new-game-btn:hover {
  color: #fff;
  box-shadow: 0 0 8px rgba(201, 164, 76, 0.6);
}

#rules-btn:active, #new-game-btn:active { transform: translateY(1px); }

#rules-btn:focus-visible, #new-game-btn:focus-visible { outline: 3px solid var(--gold-bright); }

.hud-title {
  font-size: 0.75rem;
  color: var(--gold);
  display: block;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-weight: 700;
}

#health-panel { display: flex; align-items: center; gap: 12px; min-width: 0; }

#health-bar {
  width: min(220px, 42vw);
  min-width: 110px;
  height: 24px;
  background: linear-gradient(180deg, #1a0f0f, #2a1414);
  border-radius: 4px;
  overflow: hidden;
  border: 2px solid var(--stone-dark);
  box-shadow:
    0 0 0 2px var(--gold) inset,
    0 1px 0 rgba(255, 255, 255, 0.08) inset,
    0 2px 4px rgba(0, 0, 0, 0.6);
  position: relative;
}

#health-bar::after {
  content: "";
  position: absolute;
  inset: 0;
  background: repeating-linear-gradient(
    90deg,
    rgba(0, 0, 0, 0.18) 0px,
    rgba(0, 0, 0, 0.18) 1px,
    transparent 1px,
    transparent 11px
  );
  pointer-events: none;
}

#health-fill {
  height: 100%;
  width: 100%;
  background: linear-gradient(180deg, var(--blood-bright), var(--blood) 60%, #4a1212);
  box-shadow: 0 0 8px rgba(194, 58, 58, 0.6) inset;
  transition: width 0.25s ease, background-color 0.25s ease;
}

#health-fill.warn { background: linear-gradient(180deg, #ff8a3d, var(--warn) 60%, #7a2a12); animation: pulse 1s infinite; }
#health-fill.flash-damage { background: #ff6b5e !important; }
#health-fill.flash-heal { background: #6effb0 !important; }

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

#health-text {
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--gold-bright);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
}

#turn-text { color: var(--muted); }

#table {
  flex: 1;
  display: grid;
  grid-template-columns: 160px 1fr 160px;
  grid-template-rows: auto auto;
  gap: 20px;
  padding: 24px;
  align-items: start;
}

#deck-zone, #discard-zone {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.pile-count { font-size: 0.85rem; color: var(--muted); }

#weapon-zone {
  grid-column: 1 / span 3;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  min-height: 120px;
}

.zone-title { font-size: 0.85rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; margin: 0; }

.weapon-slot {
  display: flex;
  position: relative;
  min-height: 150px;
}

.weapon-threshold { font-size: 0.85rem; color: var(--muted); }

#room-zone {
  grid-column: 1 / span 3;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

.room-cards { display: flex; gap: 20px; min-height: 150px; flex-wrap: wrap; justify-content: center; }

.card {
  width: 110px;
  height: 150px;
  border-radius: 8px;
  background: linear-gradient(160deg, #e9dcb8, #d8c79a);
  border: 3px solid var(--stone-dark);
  padding: 0;
  cursor: pointer;
  position: relative;
  user-select: none;
  transition: transform 0.15s ease;
  overflow: visible;
  box-shadow: 0 3px 6px rgba(0, 0, 0, 0.45);
}

.card-face {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 5px;
  pointer-events: none;
}

.card:hover { transform: translateY(-4px); }
.card:active:not(.disabled) { transform: scale(0.96); }
.card:focus-visible { outline: 3px solid #ffd24a; }

.card.disabled { opacity: 0.45; cursor: default; pointer-events: none; }
.card.carried { box-shadow: 0 0 0 3px #ffd24a; }

.card.type-monster { border-color: var(--monster); }
.card.type-weapon { border-color: var(--weapon); }
.card.type-potion { border-color: var(--potion); }

.card-fallback {
  display: none;
  position: absolute;
  inset: 0;
  align-items: center;
  justify-content: center;
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--ink);
  background: linear-gradient(160deg, #e9dcb8, #d8c79a);
  border-radius: 5px;
}

.card.art-failed .card-face { display: none; }
.card.art-failed .card-fallback { display: flex; }

.card-value-badge {
  position: absolute;
  top: -8px;
  left: -8px;
  background: #14181c;
  color: var(--ink);
  border: 1px solid #000;
  border-radius: 50%;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  font-weight: 700;
}

.carried-tag {
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  background: #ffd24a;
  color: #14181c;
  font-size: 0.6rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 2px 8px;
  border-radius: 10px;
  white-space: nowrap;
}

.weapon-ineligible-badge {
  position: absolute;
  top: -8px;
  right: -8px;
  background: #2a3138;
  border-radius: 50%;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.8rem;
  border: 1px solid var(--warn);
  color: var(--warn);
}

.weapon-stack-card { position: relative; }
.weapon-stack-card + .weapon-stack-card { margin-left: -55px; }

.pile {
  width: 110px;
  height: 150px;
  border-radius: 8px;
  background-size: cover;
  border: 3px solid #000;
}
.card-back { background-image: url("../../assets/cards/back.webp"); }

#avoid-btn {
  background: linear-gradient(180deg, var(--stone) 0%, var(--stone-dark) 100%);
  color: var(--gold-bright);
  border: 2px solid var(--gold);
  padding: 10px 24px;
  min-height: 44px;
  border-radius: 4px;
  font-weight: 700;
  font-size: 0.9rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  cursor: pointer;
  box-shadow:
    0 1px 0 rgba(255, 255, 255, 0.08) inset,
    0 3px 6px rgba(0, 0, 0, 0.5);
  transition: transform 0.1s ease, box-shadow 0.15s ease, color 0.15s ease;
}

#avoid-btn:hover:not(:disabled) {
  color: #fff;
  box-shadow:
    0 0 10px rgba(201, 164, 76, 0.6),
    0 1px 0 rgba(255, 255, 255, 0.1) inset,
    0 3px 6px rgba(0, 0, 0, 0.5);
}

#avoid-btn:active:not(:disabled) { transform: translateY(1px); }

#avoid-btn:disabled {
  background: var(--stone-dark);
  color: var(--muted);
  border-color: #4a443a;
  cursor: not-allowed;
  box-shadow: none;
}

#avoid-btn:focus-visible { outline: 3px solid var(--gold-bright); }

.avoid-reason { font-size: 0.8rem; color: var(--muted); }

#toast {
  position: fixed;
  bottom: max(24px, calc(env(safe-area-inset-bottom) + 12px));
  left: 50%;
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  text-align: center;
  background: linear-gradient(180deg, var(--stone), var(--stone-dark));
  color: var(--gold-bright);
  font-weight: 600;
  padding: 10px 18px;
  border-radius: 6px;
  border: 2px solid var(--gold);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.6);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

#toast.visible { opacity: 1; }

.overlay {
  position: fixed;
  inset: 0;
  background: radial-gradient(circle at 50% 40%, rgba(0, 0, 0, 0.55), rgba(0, 0, 0, 0.85));
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.overlay.hidden { display: none; }

#modal, #end-card {
  background:
    radial-gradient(circle at 50% 0%, rgba(201, 164, 76, 0.08), transparent 65%),
    linear-gradient(165deg, var(--stone) 0%, var(--stone-dark) 100%);
  border-radius: 6px;
  padding: 26px 28px;
  min-width: 280px;
  max-width: 90vw;
  max-height: 85vh;
  overflow-y: auto;
  border: 2px solid var(--gold);
  box-shadow:
    0 0 0 5px var(--stone-dark) inset,
    0 0 0 6px var(--gold-bright) inset,
    0 10px 40px rgba(0, 0, 0, 0.7);
}

#modal h3, #end-card h2 {
  margin: 0 0 14px;
  padding-bottom: 10px;
  color: var(--gold-bright);
  text-align: center;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-size: 1.1rem;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
  border-bottom: 1px solid rgba(201, 164, 76, 0.4);
}

#end-card p { color: var(--ink); text-align: center; }

.rules-section-title {
  margin: 16px 0 4px;
  color: var(--gold-bright);
  font-size: 0.9rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.rules-section-title:first-of-type { margin-top: 0; }

#modal p { margin: 0 0 8px; color: var(--ink); line-height: 1.4; }

.modal-actions, .end-actions { display: flex; gap: 10px; margin-top: 18px; flex-wrap: wrap; }

.modal-actions button, .end-actions button {
  flex: 1;
  padding: 10px;
  min-height: 44px;
  border-radius: 4px;
  border: 2px solid var(--gold);
  background: linear-gradient(180deg, var(--stone) 0%, var(--stone-dark) 100%);
  color: var(--gold-bright);
  cursor: pointer;
  font-weight: 700;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  transition: box-shadow 0.15s ease, color 0.15s ease, transform 0.1s ease;
}

.modal-actions button:hover:not(:disabled), .end-actions button:hover:not(:disabled) {
  color: #fff;
  box-shadow: 0 0 10px rgba(201, 164, 76, 0.6);
}

.modal-actions button:active:not(:disabled), .end-actions button:active:not(:disabled) {
  transform: translateY(1px);
}

.modal-actions button:disabled, .end-actions button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  box-shadow: none;
}

.monster-list {
  list-style: none;
  padding: 0;
  margin: 8px 0;
  max-height: 200px;
  overflow-y: auto;
  border-top: 1px solid rgba(201, 164, 76, 0.25);
}

.monster-list li {
  padding: 6px 2px;
  color: var(--ink);
  border-bottom: 1px solid rgba(201, 164, 76, 0.25);
}

@media (max-width: 720px) {
  #hud { padding: 10px 14px; justify-content: center; }
  #health-panel { order: 1; flex-basis: 100%; justify-content: center; }
  #turn-panel { order: 2; }
  #hud-actions { order: 3; }

  /* Weapon and room come first (the cards you act on); the deck/discard
     piles are reference info, so they move to the bottom, side by side,
     to keep the interactive area close to the HUD. */
  #table { grid-template-columns: 1fr 1fr; padding: 16px; gap: 16px; }
  #weapon-zone { grid-column: 1 / -1; order: 1; }
  #room-zone { grid-column: 1 / -1; order: 2; }
  #deck-zone { order: 3; }
  #discard-zone { order: 4; }
  .room-cards { gap: 12px; }
}

@media (max-width: 400px) {
  .card, .pile {
    width: 92px;
    height: 125px;
  }
  .room-cards { gap: 10px; }
  .weapon-stack-card + .weapon-stack-card { margin-left: -46px; }
  #avoid-btn { padding: 10px 18px; font-size: 0.8rem; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

@media (hover: none) {
  .card:hover { transform: none; }
  #avoid-btn:hover:not(:disabled), #rules-btn:hover, #new-game-btn:hover { box-shadow: none; color: var(--gold-bright); }
}
