/* ==========================================================================
   MEDIEVAL GOTHIC BRUTALISM DESIGN SYSTEM & VARIABLES
   ========================================================================== */
:root {
  /* Dark/Soot atmospheric base colors */
  --bg-deep: #0c0a08;
  --bg-grid: #14110e;
  
  /* Textured tactical surfaces: parchment and raw charcoal iron */
  --panel-stone: #1a1715;
  --panel-stone-hover: #24201d;
  --panel-parchment: #e6d7c3;
  --panel-parchment-dark: #ccb9a1;
  
  --border-iron: 2px solid #3c3530;
  --border-gold: 2px solid #d4af37;
  
  /* Colors representing classes, forces and resources */
  --color-gold: #d4af37;        /* Treasury Antique Gold */
  --color-burgundy: #801818;    /* Royal Crimson / Blood Burgundy */
  --color-iron-grey: #4e4844;   /* Wrought Iron Grey */
  --color-text-dark: #2a231d;   /* Gothic Ink Text */
  --color-text-light: #e6dec9;  /* Light Gold/Ivory text */
  --color-text-muted: #8c8176;  /* Dust Grey */
  
  /* Typography scales */
  --font-display: 'Cinzel Decorative', 'Cinzel', 'MedievalSharp', serif;
  --font-medieval: 'MedievalSharp', serif;
  --font-sans: 'Outfit', sans-serif;
  
  /* Tactical high-contrast sharp shadow offsets (Strictly no neon glows) */
  --shadow-stone: 4px 4px 0px rgba(0, 0, 0, 0.9);
  --shadow-iron: 3px 3px 0px #1a1715;
  --shadow-gold: 0 0 10px rgba(212, 175, 55, 0.4);
  
  --transition-tactile: all 0.15s cubic-bezier(0.4, 0, 0.2, 1);
}

/* ==========================================================================
   GLOBAL RESET & MEDIEVAL CANVAS SETTING
   ========================================================================== */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font-sans);
  background-color: var(--bg-deep);
  color: var(--color-text-light);
  overflow: hidden;
  height: 100vh;
  width: 100vw;
  user-select: none;
}

/* Ash and soot ambient background */
.ambient-bg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: -1;
  background-color: var(--bg-deep);
  background-image: 
    linear-gradient(rgba(40, 32, 24, 0.05) 1px, transparent 1px),
    linear-gradient(90deg, rgba(40, 32, 24, 0.05) 1px, transparent 1px);
  background-size: 60px 60px;
  background-position: center;
  pointer-events: none;
}

/* Flickering candlelight glow overlays */
.candle-glow-left {
  position: fixed;
  top: 0;
  left: 0;
  width: 30vw;
  height: 100vh;
  background: radial-gradient(circle, rgba(212, 175, 55, 0.04) 0%, transparent 70%);
  pointer-events: none;
  animation: flicker 4s infinite alternate;
  z-index: 5;
}
.candle-glow-right {
  position: fixed;
  bottom: 0;
  right: 0;
  width: 35vw;
  height: 100vh;
  background: radial-gradient(circle, rgba(128, 24, 24, 0.04) 0%, transparent 80%);
  pointer-events: none;
  animation: flicker 5s infinite alternate-reverse;
  z-index: 5;
}

@keyframes flicker {
  0% { opacity: 0.8; }
  50% { opacity: 1; }
  100% { opacity: 0.9; }
}

/* Medieval high-contrast color classes */
.text-burgundy { color: #ec7063; text-shadow: 1px 1px 0px #000; }
.text-gold { color: var(--color-gold); text-shadow: 1px 1px 0px #000; }
.text-center { text-align: center; }
.font-gothic { font-family: var(--font-medieval); }

/* ==========================================================================
   TACTILE MEDIEVAL LAYOUT SURFACES
   ========================================================================== */
/* Aged Scroll Parchment */
.parchment-card {
  background: var(--panel-parchment);
  border: 4px solid var(--color-text-dark);
  border-radius: 2px;
  padding: 40px;
  color: var(--color-text-dark);
  box-shadow: var(--shadow-stone), inset 0 0 20px rgba(0, 0, 0, 0.15);
  position: relative;
  transition: var(--transition-tactile);
}

.stone-panel {
  background: var(--panel-stone);
  border: var(--border-iron);
  border-radius: 2px;
  box-shadow: var(--shadow-iron);
}

/* ==========================================================================
   IRON BUTTONS & SCRIBED INPUTS
   ========================================================================== */
.btn {
  font-family: var(--font-medieval);
  font-weight: bold;
  font-size: 0.95rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  padding: 10px 20px;
  border-radius: 2px;
  border: 2px solid var(--color-text-dark);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  transition: var(--transition-tactile);
}

/* Primary buttons are gold sealed */
.btn-primary {
  background: var(--color-gold);
  color: #120e0a;
  border: 2px solid #000;
  box-shadow: 3px 3px 0px #000;
}

.btn-primary:hover {
  transform: translate(-1px, -1px);
  box-shadow: 4px 4px 0px #000;
}

.btn-primary:active {
  transform: translate(2px, 2px);
  box-shadow: 0px 0px 0px #000;
}

/* Secondary buttons are cold iron */
.btn-secondary {
  background: var(--panel-stone-hover);
  border: var(--border-iron);
  color: var(--color-text-light);
}

.btn-secondary:hover {
  background: var(--color-iron-grey);
  border-color: var(--color-text-light);
}

.btn-icon .icon {
  width: 18px;
  height: 18px;
}

input[type="text"] {
  font-family: var(--font-sans);
  background: rgba(0, 0, 0, 0.8);
  border: 2px solid var(--color-text-dark);
  color: var(--color-text-light);
  padding: 12px 16px;
  border-radius: 2px;
  outline: none;
  font-size: 1rem;
  transition: var(--transition-tactile);
}

input[type="text"]:focus {
  border-color: var(--color-gold);
  background: #000;
}

/* ==========================================================================
   1. LANDING & LOBBY VIEW
   ========================================================================== */
.overlay-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 100;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(8, 6, 5, 0.95);
  padding: 20px;
}

.overlay-container.active {
  display: flex;
}

.main-lobby-card {
  width: 100%;
  max-width: 600px;
  display: flex;
  flex-direction: column;
  gap: 25px;
}

.gothic-title {
  font-family: var(--font-display);
  font-size: 3.8rem;
  font-weight: 900;
  letter-spacing: 0.12em;
  color: var(--color-burgundy);
  text-shadow: 2px 2px 0px #000, 4px 4px 0px var(--color-gold);
  text-align: center;
}

.subtitle {
  font-family: var(--font-medieval);
  font-size: 0.95rem;
  letter-spacing: 0.25em;
  text-transform: uppercase;
  color: var(--color-text-dark);
  opacity: 0.8;
  text-align: center;
  margin-top: 5px;
}

.heraldic-divider {
  height: 4px;
  background: linear-gradient(90deg, transparent, var(--color-burgundy), transparent);
  margin-top: 15px;
}

.nickname-section .input-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.nickname-section label {
  font-family: var(--font-medieval);
  text-transform: uppercase;
  font-size: 0.85rem;
  letter-spacing: 0.1em;
  color: var(--color-text-dark);
  font-weight: bold;
}

.rooms-section {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.section-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.section-title h2 {
  font-family: var(--font-medieval);
  font-size: 1.3rem;
  letter-spacing: 0.05em;
  color: var(--color-burgundy);
}

/* Scribed tables within stone */
.stone-wrapper {
  background: rgba(0, 0, 0, 0.4);
  border: var(--border-iron);
  border-radius: 2px;
  max-height: 180px;
  overflow-y: auto;
}

.rooms-table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
  font-size: 0.95rem;
}

.rooms-table th, .rooms-table td {
  padding: 12px 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.rooms-table th {
  font-family: var(--font-medieval);
  text-transform: uppercase;
  font-size: 0.78rem;
  letter-spacing: 0.08em;
  color: var(--color-text-muted);
  background: rgba(0, 0, 0, 0.5);
  position: sticky;
  top: 0;
}

.rooms-table tbody tr {
  cursor: pointer;
  transition: var(--transition-tactile);
}

.rooms-table tbody tr:hover {
  background: rgba(212, 175, 55, 0.06);
}

.rooms-table tbody tr.selected {
  background: rgba(212, 175, 55, 0.12);
  border-left: 4px solid var(--color-gold);
}

.empty-row {
  text-align: center;
  color: var(--color-text-muted);
  font-style: italic;
}

.lobby-footer .create-room-group {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
}

/* ==========================================================================
   2. GAME FIELD & CONTROLLER HUD VIEWS
   ========================================================================== */
.game-container {
  display: none;
  grid-template-columns: 1fr 320px;
  grid-template-rows: 60px 1fr;
  height: 100vh;
  width: 100vw;
  background-color: var(--bg-deep);
  padding: 8px;
  gap: 8px;
}

/* Header Info Panel */
.hud-header {
  grid-column: 1 / 3;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}

.hud-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.hud-item .label {
  font-family: var(--font-medieval);
  font-size: 0.78rem;
  letter-spacing: 0.1em;
  color: var(--color-text-muted);
}

.hud-item .value {
  font-family: var(--font-medieval);
  font-size: 1.25rem;
  font-weight: bold;
}

.gold-box {
  background: rgba(212, 175, 55, 0.08);
  border: 1px solid rgba(212, 175, 55, 0.4);
  padding: 4px 12px;
  border-radius: 20px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.gold-box .value {
  color: var(--color-gold);
}

.sound-btn {
  padding: 6px 12px;
  font-size: 0.8rem;
  height: 34px;
}

/* Canvas Viewport */
.canvas-wrapper {
  position: relative;
  border-radius: 2px;
  overflow: hidden;
  border: var(--border-iron);
  box-shadow: inset 0 0 15px rgba(0, 0, 0, 0.8);
}

#gameCanvas {
  display: block;
  width: 100%;
  height: 100%;
  background-color: #0b0a08; /* Earth-black terrain canvas backdrop */
  cursor: crosshair;
}

/* Translucent Chat Corner */
.chat-container {
  position: absolute;
  bottom: 12px;
  left: 12px;
  width: 320px;
  height: 150px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  background: rgba(26, 23, 21, 0.85);
  border: var(--border-iron);
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  font-size: 0.82rem;
  line-height: 1.45;
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding-right: 5px;
}

.chat-messages .msg {
  color: var(--color-text-light);
}

.chat-messages .msg.system {
  color: var(--color-gold);
  font-weight: bold;
  font-family: var(--font-sans);
}

/* Translucent floating Legend */
.controls-legend {
  position: absolute;
  top: 12px;
  left: 12px;
  padding: 10px 14px;
  font-size: 0.75rem;
  line-height: 1.6;
  color: var(--color-text-muted);
  background: rgba(26, 23, 21, 0.9);
}

.controls-legend strong {
  color: var(--color-gold);
}

.legend-divider {
  height: 1px;
  background: rgba(255, 255, 255, 0.05);
  margin: 6px 0;
}

/* ==========================================================================
   3. RIGHT SIDEBAR - RECRUITMENT TENT & SHOP
   ========================================================================== */
.shop-sidebar {
  display: flex;
  flex-direction: column;
  padding: 16px;
  gap: 12px;
  overflow-y: auto;
}

.sidebar-title {
  font-family: var(--font-medieval);
  font-size: 1.15rem;
  color: var(--color-gold);
  letter-spacing: 0.05em;
  margin-bottom: 2px;
}

.sidebar-desc {
  font-size: 0.78rem;
  color: var(--color-text-muted);
  line-height: 1.4;
}

.shop-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.shop-card {
  background: rgba(0, 0, 0, 0.4);
  border: 1px solid rgba(255, 255, 255, 0.04);
  border-radius: 2px;
  padding: 10px 12px;
  cursor: pointer;
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 6px;
  color: var(--color-text-light);
  transition: var(--transition-tactile);
}

.shop-card:hover:not([disabled]) {
  background: var(--panel-stone-hover);
  border-color: var(--color-gold);
  transform: scale(1.01);
}

.shop-card.disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.shop-card-header {
  display: flex;
  align-items: center;
  gap: 10px;
}

.unit-icon {
  font-size: 1.4rem;
}

.unit-title-group {
  display: flex;
  flex-direction: column;
}

.unit-name {
  font-family: var(--font-medieval);
  font-weight: bold;
  font-size: 0.95rem;
}

.unit-tag {
  font-size: 0.65rem;
  text-transform: uppercase;
  font-weight: 800;
  letter-spacing: 0.05em;
}

.tag-infantry { color: #a9cce3; }
.tag-archer { color: #f9e79f; }
.tag-cavalry { color: #f1948a; }

.unit-details {
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-size: 0.72rem;
  color: var(--color-text-muted);
  padding: 4px 0;
  border-top: 1px solid rgba(255, 255, 255, 0.04);
}

.detail-row {
  display: flex;
  justify-content: space-between;
}

.shop-card-footer {
  display: flex;
  justify-content: flex-end;
}

.price-tag {
  font-size: 0.78rem;
  font-weight: bold;
  color: var(--color-gold);
  background: rgba(212, 175, 55, 0.15);
  padding: 2px 6px;
  border-radius: 20px;
}

/* Selected unit stats overlay box */
.selections-box {
  border-top: 1px solid rgba(255, 255, 255, 0.05);
  padding-top: 12px;
  margin-top: 4px;
}

.box-title {
  font-family: var(--font-medieval);
  font-size: 0.8rem;
  letter-spacing: 0.08em;
  color: var(--color-text-muted);
  text-transform: uppercase;
  margin-bottom: 8px;
}

.selection-stats {
  font-size: 0.78rem;
}

.no-selection {
  color: var(--color-text-muted);
  font-style: italic;
  text-align: center;
  padding: 8px;
}

.selection-row {
  display: flex;
  justify-content: space-between;
  padding: 5px 8px;
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.02);
  margin-bottom: 5px;
}

/* ==========================================================================
   4. HUD OVERLAYS (SPECTATORS & VICTORY)
   ========================================================================== */
.hud-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 90;
  background: rgba(8, 6, 5, 0.94);
  display: none;
  align-items: center;
  justify-content: center;
}

/* Victory banner stylings */
.victory-banner {
  background: #1a1715;
  border: 4px solid var(--color-gold);
  border-radius: 2px;
  padding: 40px 60px;
  text-align: center;
  box-shadow: var(--shadow-stone);
  max-width: 600px;
  width: 90%;
  animation: bannerSlideIn 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes bannerSlideIn {
  from { transform: scale(0.85); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.victory-title {
  font-family: var(--font-display);
  font-size: 3.5rem;
  letter-spacing: 0.15em;
  margin-bottom: 8px;
  font-weight: bold;
}

.winner-announcement {
  text-transform: uppercase;
  font-size: 0.8rem;
  letter-spacing: 0.12em;
  color: var(--color-text-muted);
  margin-bottom: 4px;
}

.winner-name {
  font-family: var(--font-display);
  font-size: 2rem;
  color: #fff;
  text-shadow: 2px 2px 0px #000;
  margin-bottom: 20px;
}

.victory-divider {
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--color-gold), transparent);
  margin-bottom: 20px;
}

.cooldown-msg {
  font-size: 0.85rem;
  color: var(--color-text-muted);
}

.blur-bg {
  padding: 30px;
  border-radius: 2px;
  border: 2px solid var(--color-text-dark);
}

.leaderboard-section {
  margin-top: 15px;
  text-align: left;
}

.leaderboard-section h3 {
  font-family: var(--font-medieval);
  color: var(--color-gold);
  margin-bottom: 8px;
  text-transform: uppercase;
  font-size: 1.1rem;
}

.sort-controls {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.sort-controls button {
  padding: 5px 10px;
  font-size: 0.78rem;
}

.sort-controls button.active {
  background: var(--color-gold);
  color: #000;
  border-color: #000;
}

.leaderboard-table {
  width: 100%;
  font-size: 0.85rem;
}

.leaderboard-table th, .leaderboard-table td {
  padding: 6px 10px;
}

.leaderboard-table th {
  color: var(--color-text-muted);
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.leaderboard-table .highlight-row {
  background: rgba(212, 175, 55, 0.12);
  font-weight: bold;
}

/* ==========================================================================
   5. DYNAMIC MOBILE RESPONSIVENESS SUITE (Touch & Tablet support)
   ========================================================================== */
.mobile-only-overlay {
  display: none; /* Auto displayed in coarse touch layouts */
}

/* Detect mobile screens / tablet overlays */
@media (max-width: 900px) {
  .game-container {
    grid-template-columns: 1fr;
    grid-template-rows: 50px 1fr;
    padding: 4px;
    gap: 4px;
  }

  .hud-header {
    grid-column: 1 / 2;
    padding: 0 10px;
  }

  .hud-header .hud-item.room-info,
  .hud-header .hud-item.sound-control {
    display: none; /* Save space for critical values on mobile */
  }

  /* Hide raw sidebar components on mobile; it is overlayed or expanded on demand */
  .shop-sidebar {
    display: none !important; 
  }

  .desktop-only {
    display: none !important;
  }

  /* Canvas layout scales full viewport width */
  .canvas-wrapper {
    height: 100%;
  }

  /* MOBILE FLOATING HUD OVERLAY & BUTTON BANNER DESIGNS */
  .mobile-only-overlay {
    display: flex;
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    pointer-events: none; /* Pass clicks to canvas below unless direct overlay targets hit */
    z-index: 10;
    flex-direction: column;
    justify-content: space-between;
    padding: 12px;
  }

  /* Hanging Crest Banners (Top/Right corner group selections) */
  .mobile-hotkeys-bar {
    display: flex;
    pointer-events: auto;
    align-self: flex-end;
    gap: 6px;
    background: rgba(26, 23, 21, 0.85);
    border: 1px solid rgba(212, 175, 55, 0.3);
    padding: 4px;
    border-radius: 4px;
  }

  .btn-mobile-banner {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: #2a231d;
    border: 2px solid #3c3530;
    width: 46px;
    height: 48px;
    cursor: pointer;
    transition: var(--transition-tactile);
    color: var(--color-text-light);
  }

  .btn-mobile-banner .banner-crest {
    font-size: 1.1rem;
  }

  .btn-mobile-banner .banner-label {
    font-size: 0.58rem;
    font-family: var(--font-medieval);
    font-weight: bold;
    margin-top: 2px;
  }

  .btn-mobile-banner.active {
    background: var(--color-gold);
    color: #120e0a;
    border-color: #000;
    box-shadow: 0px 0px 8px var(--color-gold);
  }

  /* Panning / Gesture toggling mobile switches */
  .mobile-mode-toggle {
    display: flex;
    pointer-events: auto;
    align-self: center;
    gap: 8px;
    background: rgba(0, 0, 0, 0.8);
    border: 2px solid #3c3530;
    padding: 4px;
    border-radius: 30px;
    margin-bottom: 12px;
  }

  .btn-mobile-mode {
    display: flex;
    align-items: center;
    gap: 6px;
    background: transparent;
    border: 1px solid transparent;
    padding: 8px 16px;
    border-radius: 20px;
    color: var(--color-text-muted);
    font-family: var(--font-medieval);
    font-weight: bold;
    font-size: 0.82rem;
    cursor: pointer;
    transition: var(--transition-tactile);
    min-height: 44px; /* Touch target standard */
  }

  .btn-mobile-mode.active {
    background: var(--color-burgundy);
    color: #fff;
    border-color: var(--color-burgundy);
    box-shadow: inset 0px 1px 2px rgba(0,0,0,0.5);
  }

  .chat-container {
    bottom: 8px;
    left: 8px;
    width: 250px;
    height: 90px;
  }
}

/* General touch target accessibility rules for coarser devices */
@media (pointer: coarse) {
  .btn, .shop-card, input[type="text"] {
    min-height: 44px; /* Finger tap layout */
  }
}
