:root {
  --bg: #0f1720;
  --bg-soft: #16212d;
  --panel: rgba(18, 25, 34, 0.92);
  --panel-2: rgba(25, 35, 47, 0.96);
  --line: rgba(255, 255, 255, 0.08);
  --text-main: #edf3ff;
  --text-muted: #92a1b7;
  --accent: #6cc9ff;
  --accent-strong: #2c87f0;
  --success: #7dd96f;
  --warning: #ffcb66;
  --danger: #ff7b7b;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --content-top: 0px;
  --content-bottom: 0px;
  --hud-height: clamp(58px, 11vh, 76px);
  --menu-height: clamp(72px, 12vh, 94px);
  --side-gap: clamp(10px, 3.2vw, 16px);
  --panel-radius: clamp(16px, 4.5vw, 24px);
  --slot-min-height: clamp(88px, 18vw, 120px);
  --card-padding: clamp(10px, 3.2vw, 16px);
  --title-size: clamp(16px, 4.2vw, 22px);
  --body-size: clamp(12px, 3.4vw, 14px);
}

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

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: linear-gradient(180deg, #172231 0%, #0d141d 100%);
  color: var(--text-main);
  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}

button {
  font: inherit;
  color: inherit;
}

.app-shell {
  position: fixed;
  inset: 0;
  overflow: hidden;
  user-select: none;
}

.faction-overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: grid;
  align-items: end;
}

.faction-overlay.is-hidden {
  opacity: 0;
  pointer-events: none;
}

.faction-backdrop {
  position: absolute;
  inset: 0;
  background:
    radial-gradient(circle at top center, color-mix(in srgb, var(--accent) 28%, transparent), transparent 48%),
    rgba(4, 7, 11, 0.7);
  backdrop-filter: blur(10px);
}

.faction-sheet {
  position: relative;
  z-index: 1;
  max-height: min(88dvh, 920px);
  overflow: auto;
  padding: calc(var(--safe-top) + 16px) var(--side-gap) calc(var(--safe-bottom) + 18px);
  border-top-left-radius: clamp(20px, 6vw, 28px);
  border-top-right-radius: clamp(20px, 6vw, 28px);
  background:
    radial-gradient(circle at top center, color-mix(in srgb, var(--accent) 14%, transparent), transparent 46%),
    linear-gradient(180deg, rgba(19, 27, 37, 0.98), rgba(10, 15, 22, 0.98));
  border-top: 1px solid rgba(255, 255, 255, 0.08);
}

.faction-eyebrow,
.city-eyebrow,
.build-menu-kicker,
.building-panel-kicker {
  margin: 0 0 8px;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.faction-title,
.city-title {
  margin: 0 0 8px;
}

.faction-copy {
  margin: 0 0 18px;
  color: #aebbd0;
  font-size: var(--body-size);
  line-height: 1.45;
  text-align: center;
}

.faction-cards,
.build-menu-list {
  display: grid;
  gap: 12px;
}

.build-menu-list {
  margin-top: 14px;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.faction-card,
.build-option {
  width: 100%;
  padding: var(--card-padding);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: clamp(16px, 4vw, 20px);
  background: linear-gradient(180deg, rgba(24, 32, 43, 0.9), rgba(13, 18, 26, 0.9));
  text-align: center;
  cursor: pointer;
  transition:
    transform 150ms ease,
    border-color 150ms ease,
    box-shadow 150ms ease,
    background 150ms ease;
}

.faction-card:hover,
.faction-card:active {
  transform: translateY(-1px);
}

.faction-card.is-selected {
  border-color: color-mix(in srgb, var(--accent) 52%, white 8%);
  box-shadow: 0 0 0 1px color-mix(in srgb, var(--accent) 22%, transparent);
}

.faction-card-head,
.build-menu-head,
.building-panel-head,
.city-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.faction-card-head,
.build-menu-head,
.building-panel-head {
  text-align: center;
}

.faction-card-title,
.build-option-title,
.building-panel-title,
.build-menu-title {
  margin: 0;
  font-size: var(--title-size);
  font-weight: 800;
}

.build-menu-subtitle {
  margin: 6px 0 0;
  color: #abc0df;
  font-size: var(--body-size);
  line-height: 1.35;
}

.faction-card-badge,
.building-panel-badge {
  padding: 5px 9px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08);
  color: #d7e2f4;
  font-size: 11px;
  font-weight: 700;
}

.faction-card[data-faction-id="arcane-empire"] {
  background:
    radial-gradient(circle at top right, rgba(94, 210, 255, 0.20), transparent 38%),
    linear-gradient(180deg, rgba(24, 34, 50, 0.96), rgba(12, 18, 30, 0.96));
}

.faction-card[data-faction-id="arcane-empire"] .faction-card-badge,
body[data-faction="arcane-empire"] .faction-card.is-selected .faction-card-badge {
  background: rgba(94, 210, 255, 0.18);
  color: #dff7ff;
  box-shadow: 0 0 18px rgba(94, 210, 255, 0.18);
}

.faction-card[data-faction-id="undead"] {
  background:
    radial-gradient(circle at top right, rgba(141, 226, 255, 0.16), transparent 36%),
    linear-gradient(180deg, rgba(31, 24, 44, 0.97), rgba(16, 12, 25, 0.97));
}

.faction-card[data-faction-id="undead"] .faction-card-badge,
body[data-faction="undead"] .faction-card.is-selected .faction-card-badge {
  background: rgba(141, 226, 255, 0.14);
  color: #eff6ff;
  box-shadow: 0 0 18px rgba(93, 113, 255, 0.16);
}

.faction-card[data-faction-id="forest-folk"] {
  background:
    radial-gradient(circle at top right, rgba(142, 224, 107, 0.18), transparent 38%),
    linear-gradient(180deg, rgba(22, 33, 22, 0.97), rgba(11, 19, 13, 0.97));
}

.faction-card[data-faction-id="forest-folk"] .faction-card-badge,
body[data-faction="forest-folk"] .faction-card.is-selected .faction-card-badge {
  background: rgba(142, 224, 107, 0.16);
  color: #f1ffe7;
  box-shadow: 0 0 18px rgba(142, 224, 107, 0.16);
}

body[data-faction="arcane-empire"] .faction-card.is-selected {
  box-shadow:
    0 0 0 1px rgba(94, 210, 255, 0.22),
    0 16px 32px rgba(40, 126, 240, 0.16);
}

body[data-faction="undead"] .faction-card.is-selected {
  box-shadow:
    0 0 0 1px rgba(141, 226, 255, 0.18),
    0 16px 32px rgba(93, 113, 255, 0.18);
}

body[data-faction="forest-folk"] .faction-card.is-selected {
  box-shadow:
    0 0 0 1px rgba(142, 224, 107, 0.20),
    0 16px 32px rgba(60, 179, 125, 0.18);
}

.faction-card-summary,
.build-option-description,
.building-panel-status {
  margin: 8px 0 0;
  color: #dbe5f6;
  font-size: var(--body-size);
  line-height: 1.4;
}

.faction-card-meta,
.build-option-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.faction-card-chip,
.build-option-chip {
  padding: 6px 10px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.06);
  color: #abc0df;
  font-size: 12px;
  font-weight: 700;
}

.build-option {
  display: grid;
  grid-template-columns: clamp(56px, 16vw, 72px) minmax(0, 1fr);
  align-items: center;
  gap: clamp(10px, 3vw, 14px);
}

.build-option-content {
  min-width: 0;
  text-align: center;
}

.build-option-icon {
  display: grid;
  place-items: center;
  min-height: clamp(56px, 16vw, 72px);
  border: 1px solid rgba(108, 201, 255, 0.22);
  border-radius: clamp(14px, 4vw, 18px);
  background: linear-gradient(180deg, rgba(49, 87, 132, 0.45), rgba(22, 39, 59, 0.7));
  color: #f2f7ff;
  font-size: clamp(11px, 2.8vw, 12px);
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  text-align: center;
  padding: 8px;
}

.faction-actions {
  margin-top: 18px;
}

.faction-confirm,
.claim-button,
.building-action,
.build-menu-close {
  min-height: clamp(42px, 9vw, 46px);
  padding: 0 clamp(12px, 3vw, 16px);
  border: 1px solid color-mix(in srgb, var(--accent) 34%, white 8%);
  border-radius: clamp(14px, 3.8vw, 16px);
  background:
    linear-gradient(
      180deg,
      color-mix(in srgb, var(--accent) 34%, transparent),
      color-mix(in srgb, var(--accent-strong) 34%, transparent)
    );
  color: #eef4ff;
  font-weight: 800;
}

.faction-confirm {
  width: 100%;
  min-height: clamp(48px, 11vw, 54px);
}

.hud {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 20;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: clamp(8px, 2.5vw, 10px);
  padding: calc(var(--safe-top) + 10px) var(--side-gap) 10px;
  isolation: isolate;
  justify-items: center;
  align-items: center;
}

.hud::before {
  content: "";
  position: absolute;
  inset: calc(var(--safe-top) + 4px) var(--side-gap) 4px;
  z-index: -1;
  border: 1px solid rgba(255, 255, 255, 0.05);
  border-radius: 22px;
  background:
    linear-gradient(180deg, rgba(20, 28, 39, 0.9), rgba(10, 15, 22, 0.78));
  box-shadow: 0 14px 28px rgba(4, 8, 14, 0.24);
  backdrop-filter: blur(12px);
}

.resource-card {
  position: relative;
  display: grid;
  justify-items: center;
  align-content: center;
  gap: 2px;
  min-height: clamp(28px, 8vw, 42px);
  padding: clamp(6px, 2.4vw, 10px) clamp(8px, 2.8vw, 12px);
  border: 1px solid var(--line);
  border-radius: clamp(14px, 4.5vw, 18px);
  background: linear-gradient(180deg, rgba(22, 29, 40, 0.84), rgba(9, 13, 20, 0.88));
  min-width: 0;
  overflow: hidden;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05);
}

.resource-card::before {
  content: "";
  position: absolute;
  inset: 0 auto 0 0;
  width: 4px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.78), rgba(255, 255, 255, 0.1));
  opacity: 0.95;
}

.resource-card[data-resource="gold"]::before {
  background: linear-gradient(180deg, #ffe28a, #b68720);
}

.resource-card[data-resource="food"]::before {
  background: linear-gradient(180deg, #93f07d, #348a3a);
}

.resource-card[data-resource="wood"]::before {
  background: linear-gradient(180deg, #f0c17b, #9c5c2d);
}

.resource-card[data-resource="stone"]::before {
  background: linear-gradient(180deg, #d9e3f6, #7990aa);
}

.resource-label {
  color: var(--text-muted);
  font-size: clamp(9px, 2.4vw, 11px);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.resource-value {
  font-size: clamp(13px, 3.8vw, 16px);
  line-height: 1.05;
}

.game-area {
  position: fixed;
  top: calc(var(--safe-top) + var(--hud-height));
  right: 0;
  bottom: calc(var(--safe-bottom) + var(--menu-height));
  left: 0;
  overflow: auto;
  padding: 0 var(--side-gap) 14px;
}

.city-shell {
  min-height: 100%;
  padding: clamp(12px, 3.5vw, 16px);
  border: 1px solid var(--line);
  border-radius: var(--panel-radius);
  background: linear-gradient(180deg, rgba(18, 26, 36, 0.96), rgba(10, 15, 22, 0.96));
}

.city-head {
  text-align: center;
  justify-content: center;
  align-items: center;
}

.city-head > div {
  flex: 1;
  display: grid;
  justify-items: center;
}

.city-board {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: clamp(8px, 2.8vw, 12px);
  margin-top: clamp(12px, 3.5vw, 14px);
}

.city-slot {
  min-height: var(--slot-min-height);
  padding: clamp(10px, 3vw, 12px);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: clamp(14px, 4vw, 18px);
  background: linear-gradient(180deg, rgba(26, 35, 46, 0.96), rgba(16, 22, 31, 0.96));
  text-align: center;
  cursor: pointer;
}

.city-slot.is-empty {
  border-style: dashed;
  border-color: rgba(108, 201, 255, 0.28);
}

.city-slot.is-building {
  border-color: rgba(255, 203, 102, 0.38);
}

.city-slot.is-occupied {
  border-color: rgba(125, 217, 111, 0.28);
}

.city-slot.is-selected {
  box-shadow: 0 0 0 2px rgba(108, 201, 255, 0.28);
}

.city-slot-role {
  margin: 0 0 10px;
  color: var(--text-muted);
  font-size: clamp(10px, 2.7vw, 11px);
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.city-slot-name {
  margin: 0 0 6px;
  font-size: clamp(14px, 4vw, 16px);
  font-weight: 800;
}

.city-slot-status {
  margin: 0;
  color: #c6d1e1;
  font-size: clamp(12px, 3.2vw, 13px);
  line-height: 1.35;
}

.build-menu-overlay {
  position: fixed;
  inset: 0;
  z-index: 34;
  background:
    radial-gradient(circle at center, rgba(36, 58, 88, 0.16), transparent 42%),
    rgba(4, 7, 11, 0.68);
  backdrop-filter: blur(6px);
  opacity: 1;
  transition: opacity 180ms ease;
}

.build-menu-overlay.is-hidden {
  opacity: 0;
  pointer-events: none;
}

.building-panel,
.build-menu {
  padding: clamp(12px, 3.5vw, 14px) clamp(14px, 4vw, 16px);
  background: var(--panel);
  backdrop-filter: blur(16px);
}

.building-panel.is-hidden,
.build-menu.is-hidden {
  display: none;
}

.building-panel {
  position: fixed;
  top: 50%;
  left: 50%;
  width: min(440px, calc(100vw - (var(--side-gap) * 2)));
  max-height: min(72dvh, 520px);
  transform: translate(-50%, -50%);
  z-index: 40;
  border: 1px solid rgba(180, 210, 255, 0.14);
  border-radius: clamp(22px, 6vw, 28px);
  box-shadow:
    0 26px 60px rgba(0, 0, 0, 0.42),
    inset 0 1px 0 rgba(255, 255, 255, 0.05);
  text-align: center;
}

.building-panel-actions {
  margin-top: 12px;
}

.building-progress {
  margin-top: 14px;
  text-align: center;
}

.building-progress.is-hidden {
  display: none;
}

.building-progress-track {
  overflow: hidden;
  height: 12px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.05);
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.3);
}

.building-progress-fill {
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, #7dd96f, #c9f36d);
  box-shadow: 0 0 16px rgba(125, 217, 111, 0.32);
  transition: width 320ms linear;
}

.building-progress-text {
  margin: 8px 0 0;
  color: #d9e7f6;
  font-size: 12px;
  font-weight: 700;
}

.build-menu {
  position: fixed;
  left: var(--side-gap);
  right: var(--side-gap);
  top: max(calc(var(--safe-top) + 18px), 72px);
  bottom: calc(var(--safe-bottom) + 18px);
  max-height: none;
  overflow: hidden;
  background: var(--panel-2);
  z-index: 40;
  border-radius: clamp(22px, 6vw, 28px);
  border: 1px solid rgba(180, 210, 255, 0.14);
  box-shadow:
    0 26px 60px rgba(0, 0, 0, 0.42),
    inset 0 1px 0 rgba(255, 255, 255, 0.05);
  text-align: center;
}

.build-menu-head {
  position: sticky;
  top: 0;
  z-index: 1;
  align-items: flex-start;
  padding-bottom: 12px;
  background: linear-gradient(180deg, rgba(25, 35, 47, 0.98), rgba(25, 35, 47, 0.88));
}

.build-menu-list {
  max-height: calc(100% - 92px);
  overflow: auto;
  padding-right: 2px;
  overscroll-behavior: contain;
}

.build-menu-empty {
  padding: 18px 16px;
  border: 1px dashed rgba(255, 255, 255, 0.14);
  border-radius: 18px;
  color: #c6d1e1;
  font-size: 14px;
  line-height: 1.4;
  background: rgba(255, 255, 255, 0.03);
}

.bottom-menu {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 20;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: clamp(8px, 2.5vw, 10px);
  padding: 10px var(--side-gap) calc(var(--safe-bottom) + 12px);
  isolation: isolate;
  justify-items: center;
  align-items: center;
}

.bottom-menu::before {
  content: "";
  position: absolute;
  inset: 4px var(--side-gap) calc(var(--safe-bottom) + 4px);
  z-index: -1;
  border: 1px solid rgba(255, 255, 255, 0.06);
  border-radius: 24px;
  background:
    radial-gradient(circle at top center, rgba(108, 201, 255, 0.14), transparent 52%),
    linear-gradient(180deg, rgba(18, 24, 34, 0.94), rgba(8, 12, 18, 0.94));
  box-shadow: 0 -10px 24px rgba(2, 6, 10, 0.34);
  backdrop-filter: blur(16px);
}

.menu-button {
  position: relative;
  width: 100%;
  min-height: clamp(50px, 11vw, 58px);
  border: 1px solid var(--line);
  border-radius: clamp(14px, 4vw, 18px);
  background: linear-gradient(180deg, rgba(22, 29, 40, 0.8), rgba(9, 13, 20, 0.84));
  font-weight: 700;
  font-size: clamp(13px, 3.5vw, 15px);
  color: #c6d6ec;
  overflow: hidden;
  text-align: center;
  transition:
    transform 140ms ease,
    border-color 140ms ease,
    background 140ms ease,
    color 140ms ease;
}

.menu-button::before {
  content: "";
  position: absolute;
  inset: auto 16px 8px;
  height: 3px;
  border-radius: 999px;
  background: linear-gradient(90deg, rgba(108, 201, 255, 0), rgba(108, 201, 255, 0.85), rgba(108, 201, 255, 0));
  opacity: 0;
  transform: translateY(6px);
  transition:
    opacity 160ms ease,
    transform 160ms ease;
}

.menu-button:active {
  transform: translateY(1px) scale(0.99);
}

.menu-button.is-active {
  border-color: rgba(141, 181, 255, 0.45);
  background:
    radial-gradient(circle at top center, rgba(138, 193, 255, 0.2), transparent 60%),
    linear-gradient(180deg, rgba(96, 148, 255, 0.32), rgba(41, 82, 154, 0.32));
  color: #f3f8ff;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.09),
    0 10px 18px rgba(24, 74, 143, 0.22);
}

.menu-button.is-active::before {
  opacity: 1;
  transform: translateY(0);
}

@media (max-width: 420px) {
  :root {
    --hud-height: clamp(54px, 11vh, 68px);
    --menu-height: clamp(76px, 13vh, 92px);
    --slot-min-height: clamp(82px, 24vw, 104px);
  }

  .city-head,
  .build-menu-head,
  .building-panel-head {
    align-items: flex-start;
    flex-direction: column;
  }

  .city-head {
    align-items: center;
  }

  .city-board {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .build-menu-list {
    grid-template-columns: minmax(0, 1fr);
  }

  .build-option {
    grid-template-columns: 56px minmax(0, 1fr);
  }

  .build-menu {
    left: 8px;
    right: 8px;
    top: max(calc(var(--safe-top) + 10px), 56px);
    bottom: calc(var(--safe-bottom) + 10px);
  }

  .building-panel {
    width: calc(100vw - 16px);
    max-height: min(70dvh, 460px);
  }

  .hud {
    gap: 6px;
  }

  .resource-card {
    padding: 6px 8px;
    border-radius: 14px;
  }

  .resource-value {
    font-size: 13px;
  }
}
