* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background: #0b0d08 radial-gradient(circle at 50% 38%, #1e2517 0%, #0a0c07 72%);
  color: #f2e6c8;
  font-family: "Courier New", ui-monospace, monospace;
  user-select: none;
  -webkit-user-select: none;
}

#app {
  position: relative;
  width: min(96vw, 126vh);
  display: flex;
  flex-direction: column;
  background: #0d0f09;
  overflow: hidden;
  container-type: inline-size;
  box-shadow:
    0 0 0 2px #453b27,
    0 0 0 5px #161a10,
    0 24px 70px rgba(0, 0, 0, 0.65);
}

#app.hud-hidden #hud-left,
#app.hud-hidden #hud-right { visibility: hidden; }

/* ---------- Название игры ---------- */

#app { --header-h: clamp(26px, 3.4cqw, 38px); }

#game-header {
  position: relative;
  z-index: 11;
  flex: none;
  height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1.4cqw;
  padding: 0 1.2cqw;
  overflow: hidden;
  background: linear-gradient(#232016, #161a11);
  border-bottom: 0.15cqw solid #4b3f28;
  box-shadow: 0 0.3cqw 1cqw rgba(0, 0, 0, 0.45);
}

#game-name {
  margin: 0;
  white-space: nowrap;
  font-size: clamp(13px, 2.2cqw, 26px);
  letter-spacing: 0.12em;
  color: #f0c96b;
  text-shadow:
    0.16cqw 0.16cqw 0 #5c2b1d,
    0 0 1.6cqw rgba(240, 150, 60, 0.25);
}

#game-map-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: clamp(9px, 0.95cqw, 13px);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: #a99a78;
  opacity: 0.85;
}

#stage {
  position: relative;
  width: 100%;
  background: #0d0f09;
}

canvas#game {
  display: block;
  width: 100%;
  height: auto;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  cursor: crosshair;
}

.hidden { display: none !important; }

/* ---------- Названия городов: DOM поверх канваса ---------- */
/* Текст в канвасе 640×360 растягивается nearest-neighbour и превращается в кашу.
   Подписи живут в DOM: чёткие в нативном разрешении, масштаб — через cqw. */

.map-labels {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

/* На экранах меню, паузы и итогов подписи не нужны — не просвечивают сквозь панель. */
#app.hud-hidden .map-labels { visibility: hidden; }

.map-label {
  position: absolute;
  transform: translate(-50%, -50%);
  padding: 0.06cqw 0.3cqw;
  font-family: "Courier New", ui-monospace, monospace;
  font-weight: bold;
  font-size: clamp(9px, 1.05cqw, 15px);
  line-height: 1.2;
  letter-spacing: 0.01em;
  color: #f1e3bd;
  background: rgba(16, 19, 12, 0.8);
  border: 0.1cqw solid rgba(109, 90, 55, 0.7);
  white-space: nowrap;
  text-shadow: 0 0.1cqw 0 rgba(0, 0, 0, 0.8);
}

/* Длинные имена — на шаг мельче, чтобы плашки не сталкивались. */
.map-label.long { font-size: clamp(8px, 0.92cqw, 13px); }

.map-label.hideout {
  color: #ffd966;
  border-color: rgba(138, 116, 68, 0.85);
}

/* ---------- HUD ---------- */

.hud-side {
  position: relative;
  z-index: 6;
  display: flex;
  gap: 1.2cqw;
  padding: 0.8cqw 1.2cqw;
  background: linear-gradient(#1a1e13, #12140d);
  font-size: clamp(11px, 1.55cqw, 18px);
  line-height: 1.35;
  text-shadow: 0 0.15cqw 0 rgba(0, 0, 0, 0.85);
}

#hud-left {
  flex-direction: row;
  align-items: center;
  border-bottom: 0.15cqw solid #3f3826;
}

#hud-right {
  flex-direction: row;
  align-items: stretch;
  border-top: 0.15cqw solid #3f3826;
}

.side-top {
  order: 2;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.hud-log-wrap { order: 1; }

.panel {
  background: rgba(16, 19, 12, 0.88);
  border: 0.12cqw solid #6d5a37;
  box-shadow: inset 0 0 0 0.1cqw rgba(0, 0, 0, 0.55), 0 0.3cqw 0.8cqw rgba(0, 0, 0, 0.35);
  padding: 0.5cqw 0.7cqw;
}

.gold-panel { width: 21cqw; }

.gold-row {
  display: flex;
  align-items: baseline;
  gap: 0.5cqw;
}

.coin {
  align-self: center;
  width: 0.9em;
  height: 0.9em;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #ffeaa4, #d9a441 55%, #8a6416);
  border: 0.12cqw solid #6b4d10;
  box-shadow: inset -0.15cqw -0.15cqw 0 rgba(0, 0, 0, 0.25);
}

#gold-value {
  font-size: 1.42em;
  font-weight: bold;
  color: #ffd966;
  letter-spacing: 0.02em;
}

.gold-goal { opacity: 0.65; font-size: 0.87em; }

.goal-bar {
  margin-top: 0.4cqw;
  height: 0.7cqw;
  background: #241f14;
  border: 0.1cqw solid #4b3f28;
}

#goal-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #c9a227, #ffe066);
  transition: width 0.18s linear;
}

.top-buttons { display: flex; gap: 0.6cqw; }

.hud-btn {
  pointer-events: auto;
  font-family: inherit;
  font-weight: bold;
  font-size: 0.9em;
  color: #f2e6c8;
  background: #2a2618;
  border: 0.12cqw solid #6d5a37;
  padding: 0.35cqw 0.8cqw;
  cursor: pointer;
  text-transform: uppercase;
  text-shadow: inherit;
}

.hud-btn:hover { background: #3d3621; }
.hud-btn:active { background: #4a4128; }

.hud-gang {
  width: 22cqw;
  flex: none;
  display: flex;
  flex-direction: column;
}

.hud-log-wrap {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 0.45cqw;
}

.panel-title {
  display: flex;
  justify-content: space-between;
  font-size: 0.84em;
  letter-spacing: 0.08em;
  color: #ffd966;
  margin-bottom: 0.4cqw;
}

#gang-list {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 0.25cqw;
  margin-bottom: 0.5cqw;
}

.pip {
  height: 1.5cqw;
  background: #1a1a10;
  border: 0.1cqw solid #3f3826;
  padding: 0.12cqw;
}

.pip-fill {
  height: 100%;
  width: 0%;
  background: #7fbf5a;
}

.pip.empty { border-style: dashed; opacity: 0.45; }

.hire-btn {
  width: 100%;
  margin-top: auto;
  font-size: 0.77em;
  padding: 0.45cqw 0.3cqw;
  letter-spacing: 0.04em;
}

.hire-btn:disabled { opacity: 0.4; cursor: default; }
.hire-btn:disabled:hover { background: #2a2618; }

.hud-log {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 0.2cqw;
  height: 6.2cqw;
  overflow: hidden;
}

.log-line {
  background: rgba(14, 17, 10, 0.72);
  border-left: 0.18cqw solid #6d5a37;
  padding: 0.14cqw 0.5cqw;
  font-size: 0.79em;
  color: #ddd0ad;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  animation: log-in 0.25s ease-out;
}

.log-line:last-child { color: #fff2c4; }

@keyframes log-in {
  from { opacity: 0; transform: translateX(0.5cqw); }
  to { opacity: 1; transform: none; }
}

.hud-hint {
  width: 100%;
  padding: 0.3cqw 0.8cqw;
  background: rgba(12, 14, 9, 0.74);
  border: 0.1cqw solid #4b3f28;
  font-size: 0.81em;
  line-height: 1.35;
  color: #d8cbaa;
  white-space: normal;
  overflow-wrap: break-word;
}

/* ---------- Широкий экран: панели по бокам карты ---------- */

@media (min-aspect-ratio: 3/2) {
  #app {
    display: grid;
    grid-template-columns: minmax(150px, 12cqw) minmax(0, 1fr) minmax(160px, 14cqw);
    grid-template-rows: auto minmax(0, 1fr);
    grid-template-areas:
      "head head head"
      "left map right";
    width: min(96vw, 214vh);
  }

  #game-header { grid-area: head; }

  #hud-left {
    grid-area: left;
    flex-direction: column;
    align-items: stretch;
    border-bottom: none;
    border-right: 0.15cqw solid #3f3826;
  }

  #stage { grid-area: map; }

  #hud-right {
    grid-area: right;
    flex-direction: column;
    align-items: stretch;
    border-top: none;
    border-left: 0.15cqw solid #3f3826;
  }

  .gold-panel,
  .hud-gang { width: 100%; }

  .side-top { order: 1; }
  .hud-log-wrap { order: 2; flex: 1; }
  .hud-log { flex: 1; height: auto; min-height: 6.2cqw; }
  .log-line { white-space: normal; }
  .hud-hint { flex: none; }
}

/* ---------- Screens ---------- */

.screen {
  position: absolute;
  inset: var(--header-h) 0 0 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at 50% 45%, rgba(18, 23, 14, 0.55), rgba(7, 9, 5, 0.9));
}

.screen-inner {
  text-align: center;
  padding: 2cqw 3cqw;
  max-width: 76cqw;
  animation: screen-in 0.3s ease-out;
}

/* Меню: заголовок уже в шапке игры, здесь — правила, выбор страны и старт. */
.menu-inner {
  max-width: 62cqw;
  max-height: 100%;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 1.6cqw 2.2cqw;
  background: rgba(10, 13, 8, 0.82);
  border: 0.12cqw solid #4b3f28;
  box-shadow:
    inset 0 0 0 0.1cqw rgba(0, 0, 0, 0.5),
    0 1cqw 3cqw rgba(0, 0, 0, 0.55);
}

.menu-sub { margin-bottom: 0.9cqw; }

.menu-label {
  margin: 0 0 0.55cqw;
  font-size: 1.15cqw;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: #a99a78;
}

@keyframes screen-in {
  from { opacity: 0; transform: translateY(0.8cqw); }
  to { opacity: 1; transform: none; }
}

.game-title {
  margin: 0 0 0.5cqw;
  font-size: 7cqw;
  line-height: 0.95;
  letter-spacing: 0.06em;
  color: #f0c96b;
  text-shadow:
    0.35cqw 0.35cqw 0 #5c2b1d,
    0 0 3cqw rgba(240, 150, 60, 0.28);
}

.game-title.small { font-size: 5cqw; margin-bottom: 0.4cqw; }

#end-title.win { color: #ffe08a; }
#end-title.lose { color: #e0604a; text-shadow: 0.35cqw 0.35cqw 0 #41140d; }

.screen-sub {
  margin: 0 0 1.6cqw;
  color: #b9a98a;
  font-size: 1.6cqw;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.menu-rules {
  color: #e6d8b4;
  font-size: 1.65cqw;
  line-height: 1.55;
  margin-bottom: 2.2cqw;
}

.menu-rules p { margin: 0.15cqw 0; }
.menu-rules b { color: #ffd966; }

.map-picker {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  justify-content: center;
  gap: 0.4cqw;
  margin: 0 0 0.6cqw;
  width: 100%;
}

.map-card {
  pointer-events: auto;
  font-family: inherit;
  font-size: 1.02cqw;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  color: #d9cba6;
  background: rgba(26, 31, 20, 0.92);
  border: 0.15cqw solid #5f5036;
  padding: 0.42cqw 0.3cqw;
  cursor: pointer;
  text-shadow: inherit;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.map-card:hover { background: #2c2a1a; border-color: #a08a55; }

.map-card.selected {
  color: #ffe08a;
  border-color: #f0c96b;
  background: #3a3320;
  box-shadow: 0 0 0 0.1cqw #f0c96b inset;
}

.map-desc {
  margin: 0 0 1.1cqw;
  min-height: 2.6em;
  max-width: 50cqw;
  color: #b9a98a;
  font-size: 1.25cqw;
  line-height: 1.4;
}

.btn-row {
  display: flex;
  gap: 1.2cqw;
  justify-content: center;
}

.btn {
  pointer-events: auto;
  font-family: inherit;
  font-weight: bold;
  font-size: 1.9cqw;
  letter-spacing: 0.08em;
  padding: 0.9cqw 2.4cqw;
  color: #201708;
  background: #f0c96b;
  border: 0.2cqw solid #fff0b8;
  box-shadow: 0 0.4cqw 0 #8a6416, 0 0.7cqw 1.2cqw rgba(0, 0, 0, 0.5);
  cursor: pointer;
  text-transform: uppercase;
}

.btn:hover { background: #ffe08a; transform: translateY(0.1cqw); }
.btn:active { transform: translateY(0.4cqw); box-shadow: 0 0 0 #8a6416, 0 0.2cqw 0.4cqw rgba(0, 0, 0, 0.5); }

.btn.secondary {
  background: #3a3323;
  color: #f0e2c0;
  border-color: #6d5a37;
  box-shadow: 0 0.4cqw 0 #1c1910, 0 0.7cqw 1.2cqw rgba(0, 0, 0, 0.5);
}

.btn.secondary:hover { background: #4a4128; }

.end-stats {
  margin: 1cqw 0 2.2cqw;
  font-size: 1.75cqw;
  line-height: 1.75;
  color: #e6d8b4;
}

.end-stats b { color: #ffd966; }

button:focus-visible { outline: 0.2cqw solid #fff0b8; outline-offset: 0.2cqw; }

/* ---------- отладка (?debug=1) ---------- */

#debug-panel {
  position: absolute;
  top: 10.5cqw;
  right: 1.2cqw;
  z-index: 8;
  width: 27cqw;
  padding: 0.6cqw;
  background: rgba(0, 0, 0, 0.86);
  border: 0.1cqw solid #8a7444;
  color: #a6e08a;
  font-size: 1.05cqw;
  line-height: 1.4;
  white-space: pre-wrap;
  pointer-events: auto;
}

#debug-state { margin-bottom: 0.4cqw; }

#debug-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3cqw;
}

#debug-buttons button {
  font-family: inherit;
  font-size: 1cqw;
  padding: 0.25cqw 0.45cqw;
  background: #2a2618;
  color: #eee6d0;
  border: 0.1cqw solid #6d5a37;
  cursor: pointer;
}

#debug-buttons button:hover { background: #3d3621; }
