/* Стол: четыре панели-плитки в переключаемой сетке, изменение размеров
   колонок и строк мышью. */

:root {
  --bg: #1d2126;
  --panel: #262b31;
  --line: #3a4149;
  --text: #e8eaed;
  --dim: #97a0a9;
  --gold: #d8a24a;
  --accent: #4a7fc1;
  /* Адаптивные размеры карт вместо фиксированных 104px (§8). */
  --card-w: clamp(88px, 10vw, 150px);
  --card-market: clamp(78px, 8vw, 108px);
  --card-city: clamp(96px, 11vw, 132px);
  --card-rival: clamp(52px, 6.5vw, 84px);
  --toolbar-h: 44px;
}

* { box-sizing: border-box; }

/* Авторское правило display перебивает [hidden] браузера независимо от
   специфичности, поэтому силу атрибуту возвращаем принудительно. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 system-ui, "Segoe UI", Roboto, sans-serif;
}

/* --- Стартовый экран --- */

.screen {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
}

.screen h1 { font-size: 40px; margin: 0; letter-spacing: .02em; }
.screen h2 { margin: 0; font-weight: 600; }
.home-form { display: flex; gap: 8px; width: 320px; }
.home-form input { flex: 1; }

/* --- Список партий на главном экране --- */

.game-list {
  width: min(560px, calc(100vw - 32px));
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.game-list h2 { font-size: 18px; margin: 0; }

.game-list-body {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 40vh;
  overflow: auto;
}

.game-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.game-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.game-info .log { font-size: 12px; }

/* --- Экран ожидания --- */

.lobby-players {
  margin: 0;
  padding: 12px 20px 12px 36px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  min-width: 240px;
}

.lobby-players li { padding: 4px 0; }

.lobby-actions { display: flex; gap: 8px; }

input, button {
  font: inherit;
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
}

button { cursor: pointer; }
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: .35; cursor: default; }
.error { color: #e2756b; }

/* --- Игровой shell --- */

#game-shell {
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.toolbar {
  height: var(--toolbar-h);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 12px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}

.toolbar-title { font-weight: 700; }

.layout-switch { display: flex; gap: 6px; }
.layout-switch button { padding: 4px 10px; border-radius: 6px; }
.layout-switch button.active { border-color: var(--gold); color: var(--gold); }

/* --- Плиточный корень --- */

#table {
  flex: 1;
  min-height: 0;
  position: relative;
  display: grid;
  gap: 0;
  grid-template-areas: var(--areas);
  grid-template-columns: var(--cols);
  grid-template-rows: var(--rows);
  overflow: hidden;
}

/* --- Панели --- */

.panel {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 4px;
  overflow: hidden;
}

#rivals { grid-area: rivals; }
#market { grid-area: market; }
#city { grid-area: city; }
#rail { grid-area: rail; }

.acting-city {
  border-color: var(--gold);
  border-width: 4px;
  box-shadow: inset 0 0 0 1px var(--gold), 0 0 22px rgba(216, 162, 74, .32);
}

.panel-head {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-bottom: 1px solid var(--line);
  font-weight: 700;
  user-select: none;
}

.drag-grip { color: var(--dim); }

.panel-body {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 10px;
}

/* --- Сплиттеры изменения размеров --- */

.splitter {
  position: absolute;
  z-index: 10;
  touch-action: none;
}

.splitter::after {
  content: "";
  position: absolute;
  background: transparent;
  transition: background .12s;
}

.splitter:hover::after,
.splitter.active::after { background: var(--gold); }

.splitter.v {
  top: 0;
  bottom: 0;
  width: 10px;
  margin-left: -5px;
  cursor: col-resize;
}

.splitter.v::after { left: 4px; width: 2px; top: 0; bottom: 0; }

.splitter.h {
  left: 0;
  right: 0;
  height: 10px;
  margin-top: -5px;
  cursor: row-resize;
}

.splitter.h::after { top: 4px; height: 2px; left: 0; right: 0; }

/* --- Перекрывающие слои (наполняют модули превью/анимаций/confirm) --- */

.overlay { position: fixed; z-index: 100; }
#card-preview { z-index: 120; pointer-events: none; }
#dice-overlay { z-index: 130; }
#confirm-dialog { z-index: 140; }
.swap-toast { z-index: 150; }

.turn-toast {
  position: fixed;
  left: 50%;
  top: 50%;
  z-index: 150;
  transform: translate(-50%, -50%);
  pointer-events: none;
  padding: 18px 34px;
  border-radius: 999px;
  background: rgba(29, 33, 38, .88);
  border: 2px solid var(--gold);
  color: var(--gold);
  font-size: clamp(34px, 6vw, 72px);
  font-weight: 900;
  letter-spacing: .03em;
  text-shadow: 0 3px 16px rgba(0, 0, 0, .55);
  animation: turn-melt 2.35s ease-out forwards;
}

.victory-toast {
  pointer-events: none;
  text-align: center;
  animation: victory-rise .65s ease-out forwards;
}

.swap-toast {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: min(760px, calc(100vw - 32px));
  padding: 18px;
  border: 2px solid var(--gold);
  border-radius: 24px;
  background: rgba(29, 33, 38, .94);
  box-shadow: 0 24px 80px rgba(0, 0, 0, .55);
  color: var(--paper);
  text-align: center;
  pointer-events: none;
  animation: swap-hold .25s ease-out forwards;
}

.swap-toast-content {
  display: grid;
  grid-template-columns: minmax(120px, 1fr) auto minmax(120px, 1fr);
  align-items: center;
  gap: 16px;
}

.swap-card {
  display: grid;
  gap: 8px;
  justify-items: center;
}

.swap-card b { color: var(--gold); font-size: clamp(18px, 3vw, 28px); }
.swap-card img { width: min(180px, 28vw); border-radius: 10px; box-shadow: 0 10px 26px rgba(0, 0, 0, .4); }
.swap-card span { font-weight: 700; }
.swap-arrows {
  display: grid;
  gap: 4px;
  color: var(--gold);
  font-size: clamp(34px, 7vw, 64px);
  line-height: .8;
}

.card-preview {
  max-width: 560px;
  display: grid;
  grid-template-columns: 240px 1fr;
  gap: 12px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(29, 33, 38, .96);
  box-shadow: 0 18px 50px rgba(0, 0, 0, .45);
}

.card-preview img,
.confirm-card img { width: 100%; border-radius: 8px; display: block; }
.preview-text { display: flex; flex-direction: column; gap: 6px; }
.preview-text p { margin: 0; }

.confirm-backdrop {
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(0, 0, 0, .55);
}

.confirm-box {
  width: min(620px, 100%);
  position: relative;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
  box-shadow: 0 22px 70px rgba(0, 0, 0, .5);
}

.confirm-box h2 { margin: 0 38px 14px 0; }
.confirm-close { position: absolute; top: 10px; right: 10px; }
.confirm-card { display: grid; grid-template-columns: 160px 1fr; gap: 14px; align-items: start; }
.confirm-card p { margin: 0 0 8px; }
.confirm-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

.dice-overlay {
  inset: 0;
  display: grid;
  place-items: center;
  pointer-events: none;
  background: radial-gradient(circle, rgba(29, 33, 38, .62), rgba(29, 33, 38, .08));
}

.dice-box {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 24px 30px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: rgba(29, 33, 38, .95);
  box-shadow: 0 18px 50px rgba(0, 0, 0, .45);
  perspective: 600px;
}

/* Кубик — светлая грань с точками, как у настоящей игральной кости. */
.die {
  width: 96px;
  height: 96px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  place-items: center;
  padding: 16px;
  border-radius: 18px;
  background: #f4f6f8;
  border: 2px solid #cdd3da;
  box-shadow: inset 0 -5px 0 rgba(0, 0, 0, .12), 0 10px 22px rgba(0, 0, 0, .42);
}

.pip {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background: transparent;
}

.pip.on {
  background: #1d2126;
  box-shadow: inset 0 2px 3px rgba(0, 0, 0, .5);
}

.die.rolling { animation: die-tumble .5s linear infinite; }

.dice-box.settled .die { animation: die-settle .32s ease-out; }

.dice-sum { font-size: 42px; font-weight: 700; }

@keyframes die-tumble {
  0%   { transform: rotate(0deg) rotateX(0deg) translateY(0); }
  20%  { transform: rotate(72deg) rotateX(26deg) translateY(-12px); }
  40%  { transform: rotate(144deg) rotateX(0deg) translateY(0); }
  60%  { transform: rotate(216deg) rotateX(-26deg) translateY(-8px); }
  80%  { transform: rotate(288deg) rotateX(0deg) translateY(0); }
  100% { transform: rotate(360deg) rotateX(0deg) translateY(0); }
}

@keyframes die-settle {
  0%   { transform: translateY(0) scale(1); }
  40%  { transform: translateY(-12px) scale(1.07); }
  100% { transform: translateY(0) scale(1); }
}
.coin-host { position: relative; }
.coin-gain { color: #7ddc8a; }
.coin-loss { color: #f08a80; }
.coin-delta {
  position: absolute;
  margin-left: 6px;
  color: var(--gold);
  font-weight: 700;
  animation: coin-pop 2.5s ease-out forwards;
}

.player-coin-burst {
  position: fixed;
  z-index: 135;
  transform: translate(-50%, -50%);
  padding: 10px 18px;
  border-radius: 999px;
  background: rgba(29, 33, 38, .9);
  border: 2px solid currentColor;
  font-size: 44px;
  font-weight: 900;
  pointer-events: none;
  animation: coin-pop 2.5s ease-out forwards;
}

.player-coin-burst.gain { color: #7ddc8a; }
.player-coin-burst.loss { color: #f08a80; }

@keyframes coin-pop {
  from { opacity: 0; transform: translateY(4px); }
  20% { opacity: 1; }
  to { opacity: 0; transform: translateY(-16px); }
}

@keyframes turn-melt {
  0% {
    opacity: 0;
    filter: blur(0);
    transform: translate(-50%, -44%) scale(.86);
  }
  15% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
  }
  65% {
    opacity: .92;
    filter: blur(.4px);
  }
  100% {
    opacity: 0;
    filter: blur(13px);
    transform: translate(-50%, -72%) scale(1.22);
  }
}

@keyframes victory-rise {
  from {
    opacity: 0;
    filter: blur(0);
    transform: translate(-50%, -44%) scale(.86);
  }
  to {
    opacity: 1;
    filter: blur(0);
    transform: translate(-50%, -50%) scale(1);
  }
}

@keyframes swap-hold {
  from { opacity: 0; transform: translate(-50%, -44%) scale(.92); }
  to { opacity: 1; transform: translate(-50%, -50%) scale(1); }
}

/* --- Соперники --- */

#rivals .panel-body {
  display: flex;
  gap: 10px;
  align-items: flex-start;
}

.rival {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0 0 8px;
  min-width: 160px;
  transform-origin: top left;
  transition: transform .15s, box-shadow .15s, opacity .15s;
  position: relative;
  z-index: 1;
  cursor: grab;
}

.rival.acting {
  border-color: var(--gold);
  border-width: 4px;
  box-shadow: inset 0 0 0 1px var(--gold), 0 0 18px rgba(216, 162, 74, .28);
}
.rival:hover:not(.dragging) {
  transform: scale(1.18);
  z-index: 20;
  box-shadow: 0 12px 34px rgba(0, 0, 0, .38);
}

.rival.dragging {
  opacity: .45;
  cursor: grabbing;
}

.rival.drop-before::before,
.rival.drop-after::after {
  content: "";
  position: absolute;
  top: 6px;
  bottom: 6px;
  width: 3px;
  border-radius: 999px;
  background: var(--gold);
}

.rival.drop-before::before { left: -7px; }
.rival.drop-after::after { right: -7px; }

.rival-head {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 9px;
  border-bottom: 1px solid var(--line);
  border-radius: 10px 10px 0 0;
  background: rgba(255, 255, 255, .025);
  font-weight: 700;
}

.rival-head .coin-wrap { margin-left: auto; }
.rival .landmark-dots,
.rival .row.mini { margin-left: 10px; margin-right: 10px; }
.rival .landmark-dots { margin-top: 7px; }

/* Свёрнутый блок соперника: предприятия видны без наведения, мельче. */
.rival .row.mini { flex-wrap: wrap; gap: 4px; margin: 6px 0 0; }
.rival .pile.mini-pile { width: 40px; }
.rival .pile.mini-pile .count { font-size: 10px; padding: 0 4px; right: -2px; bottom: -2px; }

.landmark-dots { display: flex; gap: 4px; }
.dot { width: 12px; height: 12px; border: 1px solid var(--gold); border-radius: 3px; }
.dot.built { background: var(--gold); }

/* --- Рынок и город --- */

.row { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.row:last-child { margin-bottom: 0; }

.pile { position: relative; width: var(--card-w); cursor: default; }
.pile img { width: 100%; border-radius: 6px; display: block; }
.pile.empty { opacity: .25; }
.pile.available { outline: 2px solid var(--gold); border-radius: 8px; cursor: pointer; }
.pile.available[draggable="true"] { cursor: grab; }
.pile.available[draggable="true"]:active { cursor: grabbing; }
.pile.fired { outline: 3px solid #f2d06b; border-radius: 8px; }
.panel-body.drop-target { outline: 2px dashed var(--gold); outline-offset: -6px; }

/* Рынок — фиксированная сетка 5×3: все карты должны помещаться в панель. */
[data-panel="market"] .panel-body {
  display: grid;
  grid-template-rows: repeat(3, minmax(0, 1fr));
  gap: 6px;
  overflow: hidden;
  padding: 10px 12px;
}

[data-panel="market"] .panel-body .row {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 6px;
  min-height: 0;
  margin: 0;
}

[data-panel="market"] .panel-body .pile {
  width: 100%;
  max-height: 100%;
  min-width: 0;
  align-self: center;
  justify-self: center;
  aspect-ratio: 405 / 630;
}

[data-panel="market"] .panel-body .pile img {
  width: 100%;
  height: auto;
  max-height: 100%;
  object-fit: contain;
}

/* Карты своего города крупнее, у соперников — компактный обзор. */
[data-panel="city"] .panel-body .pile { width: var(--card-city); }
[data-panel="rivals"] .panel-body .pile:not(.mini-pile) { width: var(--card-rival); }

.count {
  position: absolute;
  right: -4px; bottom: -4px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 1px 7px;
  font-size: 12px;
}

.coin-wrap {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  white-space: nowrap;
}

.coins { font-weight: 700; }
.coin-icon {
  width: .9em;
  height: .9em;
  display: inline-grid;
  place-items: center;
  border-radius: 50%;
  background: var(--gold);
  border: 1px solid #f2d06b;
  color: transparent;
  box-shadow: inset 0 -1px 0 rgba(0, 0, 0, .25);
}

/* --- Управление и журнал --- */

#rail .panel-body { padding: 12px; }
.dice { font-size: 32px; letter-spacing: .1em; }
.actions { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0; }
.log { font-size: 12px; color: var(--dim); }
.log div { padding: 2px 0; border-bottom: 1px solid var(--line); }

/* --- Узкий fallback: одна колонка, без resize --- */

@media (max-width: 720px) {
  #table { display: block; overflow: auto; }
  .panel { margin-bottom: 8px; }
  .splitter { display: none; }
  .card-preview { grid-template-columns: 160px 1fr; max-width: calc(100vw - 28px); }
  .confirm-card { grid-template-columns: 120px 1fr; }
}

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