/* Mobile-first reset: without this, padding/border can push elements wider
   than their intended box, causing horizontal overflow on narrow screens. */
*, *::before, *::after {
  box-sizing: border-box;
}

html {
  /* Prevents the whole page from scrolling sideways if anything briefly
     miscalculates its width on a narrow phone screen. */
  overflow-x: hidden;
}

body {
  font-family: sans-serif;
  background: radial-gradient(circle at top, #2a2a2a, #191919 70%);
  background-color: #222;
  color: white;
  text-align: center;
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
}

/* Buttons and clickable divs: kill the gray tap-flash and the ~300ms delay
   mobile browsers add while waiting to see if a tap becomes a double-tap. */
button, .map-cell, .animal-chip, .swarm-slot, .map-trigger, .battle-cell {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* Hover states are meaningless on touch — worse, a tap can leave a button
   looking "stuck" in its hover style until the user taps elsewhere. Only
   apply :hover on devices that actually have a mouse. */
@media (hover: hover) and (pointer: fine) {
  .big-nav-button:hover:not(:disabled) {
    background-color: #555;
  }

  .swarm-slot:hover {
    background-color: #555;
  }

  .map-trigger:hover {
    border-color: #bbb;
    color: white;
  }

  .map-trigger.no-war:hover {
    border-color: #888;
    color: #ccc;
  }

  .map-cell:hover {
    background-color: #f0f0f0;
  }

  .battle-action-button:hover:not(:disabled) {
    background-color: #555;
  }

  .tutorial-button:hover {
    border-color: #ffd54f;
    color: #ffd54f;
  }
}

.hidden {
  display: none !important;
}

#login-form {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-top: 16px;
}

#username-input,
#pin-input {
  padding: 8px;
  font-size: 1rem;
  width: 110px;
}

#login-form button,
.logout-button {
  padding: 8px 16px;
  font-size: 1rem;
  cursor: pointer;
}

.error {
  color: #ff6b6b;
  margin-top: 12px;
}

.welcome-message {
  font-size: 1.1rem;
}

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

.big-nav-buttons {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 12px;
  max-width: 320px;
  margin: 28px auto;
  padding: 0 12px;
}

.big-nav-button {
  background-color: #3a3a3a;
  border: 1px solid #666;
  border-radius: 10px;
  color: white;
  font-size: 1.2rem;
  padding: 20px;
  cursor: pointer;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.25);
  transition: background-color 0.15s;
}

.big-nav-icon {
  margin-right: 8px;
}

button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* Shared toolbar look for the login-less "app bar" header on the home,
   admin, and section screens: title on the left, contextual nav (if any)
   in the middle, tutorial/user actions on the right. */
.app-topbar {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 10px 20px;
  padding: 14px 20px;
  background-color: #2a2a2a;
  border-bottom: 1px solid #444;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.app-topbar-title {
  display: flex;
  align-items: center;
  gap: 8px;
}

.app-topbar-logo {
  font-size: 1.4rem;
  line-height: 1;
}

.app-topbar-title h1 {
  margin: 0;
  font-size: 1.3rem;
}

#app-title {
  cursor: pointer;
}

.app-topbar-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
}

.tutorial-button {
  background: none;
  border: 1px solid #666;
  border-radius: 20px;
  color: #ccc;
  padding: 8px 14px;
  font-size: 0.9rem;
  cursor: pointer;
  white-space: nowrap;
}

.section-nav {
  display: flex;
  gap: 8px;
}

.section-nav-button {
  background: none;
  border: none;
  color: #ccc;
  font-size: 1rem;
  padding: 12px;
  min-height: 44px;
  cursor: pointer;
}

.section-nav-button.active {
  color: white;
  font-weight: bold;
  border-bottom: 2px solid gold;
}

.content-panel {
  padding: 8px;
}

.panel-subtitle {
  color: #999;
  font-size: 0.9rem;
  margin-top: -4px;
}

.swarm-slots {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 12px;
  max-width: 400px;
  margin: 12px auto;
}

.swarm-slot {
  flex: 1 1 100px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  background-color: #444;
  border: 1px solid #888;
  border-radius: 10px;
  padding: 12px;
  color: white;
  cursor: pointer;
}

.swarm-slot-title {
  font-weight: bold;
}

.swarm-slot-summary {
  font-size: 0.8rem;
  color: #ccc;
}

.swarm-slot-locked {
  cursor: not-allowed;
  opacity: 0.6;
  border-color: #a55;
}

#swarm-picker-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}

#swarm-picker-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
}

#swarm-picker-content {
  position: relative;
  z-index: 1;
  background: #333;
  border: 2px solid #555;
  border-radius: 12px;
  padding: 20px;
  max-width: 360px;
  width: 90%;
  max-height: 80vh;
  overflow: auto;
}

.animal-chip.picker-selected {
  outline: 3px solid gold;
}

.animal-chip.picker-unavailable {
  opacity: 0.4;
  cursor: not-allowed;
}

.swarm-picker-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
  margin-top: 12px;
}

.swarm-picker-actions .big-nav-button,
.swarm-picker-actions .back-button-style {
  margin: 0;
  flex: 1;
}

#war-status {
  color: #ccc;
}

.map-trigger {
  max-width: 340px;
  margin: 12px auto;
  padding: 20px;
  border: 2px dashed #888;
  border-radius: 10px;
  color: #ccc;
  cursor: pointer;
}

.map-trigger.no-war {
  cursor: default;
  border-style: solid;
}

#war-map-overlay,
#battle-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 16px;
  box-sizing: border-box;
  background-color: #222;
}

#war-map-overlay-heading,
#battle-overlay-heading {
  color: white;
  margin: 0;
}

.turn-status {
  color: #ccc;
  margin: 0;
}

.map-grid {
  display: grid;
  gap: 2px;
  background-color: #ccc;
  border: 4px solid #ccc;
}

#war-map-grid,
#battle-map-grid {
  cursor: grab;
}

#war-map-grid.panning,
#battle-map-grid.panning {
  cursor: grabbing;
}

#war-map-grid {
  max-width: 100%;
  /* dvh accounts for mobile Safari/Chrome's collapsing address bar; vh alone
     can be taller than what's actually visible, cutting content off. The vh
     line is a fallback for browsers that don't support dvh yet. */
  max-height: 65vh;
  max-height: 65dvh;
  overflow: auto;
}

#battle-map-grid {
  width: 92vw;
  height: 70vh;
  height: 70dvh;
  max-width: 900px;
  place-content: start;
  overflow: auto;
}

.map-cell {
  box-sizing: border-box;
  background-color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.battle-cell {
  font-size: 1.4rem;
  cursor: pointer;
}

.reachable-tile {
  background-color: #fff59d !important;
}

.attackable-tile {
  background-color: #ef9a9a !important;
}

.ranged-attackable-tile {
  background-color: #ffcc80 !important;
}

.animal-token.acted {
  opacity: 0.4;
}

.animal-token.ambushing {
  outline: 3px dashed #fff59d;
}

.animal-token.tracked {
  box-shadow: 0 0 0 3px #ff9800;
}

#info-popup-overlay {
  position: fixed;
  inset: 0;
  z-index: 1100;
  display: flex;
  align-items: center;
  justify-content: center;
}

#info-popup-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
}

#info-popup-content {
  position: relative;
  z-index: 1;
  background: #333;
  border: 2px solid #555;
  border-radius: 12px;
  padding: 24px;
  max-width: 340px;
  width: 90%;
  text-align: center;
}

#info-popup-content .big-nav-button {
  margin: 16px 0 0;
}

#battle-action-panel {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  max-width: 500px;
}

.battle-action-button {
  background-color: #444;
  border: 1px solid #888;
  border-radius: 8px;
  color: white;
  padding: 8px 14px;
  cursor: pointer;
}

.swarm-token,
.animal-token {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  font-size: 0.85rem;
  text-transform: uppercase;
}

.owner-user1 {
  background-color: #2196f3;
}

.owner-user2 {
  background-color: #e74c3c;
}

.base-user1 {
  background-color: rgba(33, 150, 243, 0.35);
}

.base-user2 {
  background-color: rgba(231, 76, 60, 0.35);
}

.terrain-forest {
  background-color: #66bb6a;
}

.terrain-water {
  background-color: #64b5f6;
}

.terrain-outpost {
  background-color: #a1887f;
}

.swarm-token.selected,
.animal-token.selected {
  outline: 3px solid gold;
}

.map-overlay-actions {
  display: flex;
  gap: 12px;
}

.map-overlay-actions .back-button-style {
  margin: 0;
}

.back-button-style {
  display: block;
  margin: 24px auto 12px;
  background: none;
  border: 1px solid #666;
  border-radius: 8px;
  color: #ccc;
  padding: 10px 24px;
  font-size: 1rem;
  cursor: pointer;
}

.bucket-heading {
  color: #ccc;
  font-size: 1rem;
  margin: 12px 0 8px;
}

.animal-grid {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin-bottom: 12px;
}

.animal-chip {
  border-radius: 16px;
  padding: 9px 14px;
  border: 1px solid #888;
  text-transform: capitalize;
  cursor: pointer;
}

.rarity-common { color: #b0b0b0; border-color: #b0b0b0; background-color: rgba(176, 176, 176, 0.25); }
.rarity-uncommon { color: #4caf50; border-color: #4caf50; background-color: rgba(76, 175, 80, 0.25); }
.rarity-rare { color: #2196f3; border-color: #2196f3; background-color: rgba(33, 150, 243, 0.25); }
.rarity-epic { color: #9c27b0; border-color: #9c27b0; background-color: rgba(156, 39, 176, 0.25); }
.rarity-legendary { color: #ffa500; border-color: #ffa500; background-color: rgba(255, 165, 0, 0.25); }
.rarity-variant { color: #e91e63; border-color: #e91e63; background-color: rgba(233, 30, 99, 0.25); }

.animal-chip.locked {
  opacity: 0.85;
}

.injured-cross {
  color: #e74c3c;
  font-weight: bold;
}

.animal-chip.injured {
  border-color: #e74c3c;
}

.card-injured-note {
  margin-top: auto;
  color: #e74c3c;
  font-size: 0.9rem;
}

#card-modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}

#card-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
}

#card-scene-wrapper {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

#card-close-button {
  align-self: flex-end;
  background: rgba(255, 255, 255, 0.15);
  border: none;
  color: white;
  font-size: 18px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  cursor: pointer;
}

.card-scene {
  /* Scales down on narrow phones instead of a rigid 280x400 that can
     overflow on the smallest screens; aspect-ratio keeps the same 7:10
     card proportions at any size instead of a rigid pixel height. */
  width: min(280px, 85vw);
  aspect-ratio: 280 / 400;
  perspective: 1000px;
}

.flip-card {
  width: 100%;
  height: 100%;
  position: relative;
  transform-style: preserve-3d;
  transition: transform 0.5s;
}

.flip-card.flipped {
  transform: rotateY(180deg);
}

.card-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  border-radius: 14px;
  background: #333;
  border: 2px solid #555;
  padding: 18px;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  box-shadow: 0 14px 40px rgba(0, 0, 0, 0.5);
}

.card-back {
  transform: rotateY(180deg);
}

.card-header-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}

.card-name {
  flex: 1;
  font-size: 16px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.card-level-badge {
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.5px;
  color: white;
  white-space: nowrap;
  background: rgba(255, 255, 255, 0.15);
}

.card-rarity-badge {
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: white;
  white-space: nowrap;
}

.card-rarity-badge.rarity-common { background: #888888; }
.card-rarity-badge.rarity-uncommon { background: #2ecc71; }
.card-rarity-badge.rarity-rare { background: #3498db; }
.card-rarity-badge.rarity-epic { background: #9b59b6; }
.card-rarity-badge.rarity-legendary { background: #d4af37; color: #2a2200; }
.card-rarity-badge.rarity-variant { background: #e74c3c; }

.card-picture-placeholder {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 10px;
  border: 1px dashed rgba(255, 255, 255, 0.2);
  color: rgba(255, 255, 255, 0.4);
  font-style: italic;
  text-align: center;
  margin-bottom: 12px;
}

.card-picture-placeholder.has-photo {
  border-style: solid;
  border-color: rgba(255, 255, 255, 0.3);
}

.card-photo {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.card-flip-button {
  padding: 10px;
  border: 2px solid rgba(255, 255, 255, 0.4);
  background: rgba(255, 255, 255, 0.1);
  color: white;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 700;
}

.card-locked-note {
  margin-top: auto;
  color: #ffb74d;
  font-size: 0.9rem;
}

.card-class-row {
  text-align: center;
  text-transform: uppercase;
  letter-spacing: 1px;
  font-size: 12px;
  font-weight: 700;
  color: rgba(255, 255, 255, 0.65);
  margin-bottom: 8px;
}

.card-back-sightings {
  display: flex;
  gap: 10px;
  margin-bottom: 12px;
}

.card-sighting-block {
  flex: 1;
  text-align: center;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  padding: 8px 4px;
}

.card-sighting-label {
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: rgba(255, 255, 255, 0.6);
  margin-bottom: 2px;
}

.card-sighting-val {
  font-size: 26px;
  font-weight: 800;
}

.card-sighting-maxlevel .card-sighting-val {
  font-size: 15px;
  letter-spacing: 0.5px;
  color: #ffd54f;
}

.card-attrs-title {
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: rgba(255, 255, 255, 0.5);
  margin-bottom: 5px;
}

.card-attr-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.07);
  font-size: 12px;
}

.card-attr-name {
  color: rgba(255, 255, 255, 0.85);
  flex: 0 0 52px;
}

.card-attr-bar-wrap {
  position: relative;
  flex: 1;
  height: 5px;
  background: rgba(255, 255, 255, 0.12);
  border-radius: 3px;
  overflow: hidden;
}

.card-attr-bar-fill {
  height: 100%;
  background: #3ddc6f;
}

.card-attr-cap-line {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 2px;
  background: #ffd54f;
}

.card-attr-val {
  font-weight: 700;
  min-width: 44px;
  text-align: right;
  font-size: 11px;
  color: rgba(255, 255, 255, 0.8);
}

#rules-modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 1200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

#rules-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
}

#rules-modal-content {
  position: relative;
  z-index: 1;
  background: #2a2a2a;
  border: 2px solid #555;
  border-radius: 12px;
  padding: 30px 24px 24px;
  max-width: 560px;
  width: 100%;
  max-height: 85vh;
  max-height: 85dvh;
  overflow-y: auto;
  text-align: left;
  box-shadow: 0 14px 40px rgba(0, 0, 0, 0.5);
}

#rules-modal-content h2 {
  margin-top: 0;
  text-align: center;
}

#rules-modal-content h3 {
  color: #ffd54f;
  margin-bottom: 4px;
}

#rules-modal-content p {
  color: #ddd;
  margin-top: 0;
  line-height: 1.5;
}

#rules-modal-close-button {
  position: absolute;
  top: 12px;
  right: 12px;
  background: rgba(255, 255, 255, 0.15);
  border: none;
  color: white;
  font-size: 16px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
}
