:root {
  --bg: #f4f1ea;
  --card: #ffffff;
  --ink: #1d2733;
  --muted: #6b7380;
  --line: #e2ddd2;
  --accent: #1d6fd6;
  --ok: #1f9d55;
  --warn: #b7791f;
  --bad: #d4342b;
  --top: #1d2733;
  --top-ink: #f4f1ea;
  --sea: #c5dfea;
  --tabbar-h: 56px;
  --sheet-min: 68px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

/* Страница не скроллится и не «пружинит»: двигается только карта и списки внутри */
html, body {
  margin: 0; height: 100%; overflow: hidden;
  overscroll-behavior: none;
  position: fixed; inset: 0; width: 100%;
}
body {
  background: var(--bg); color: var(--ink);
  font: 16px/1.35 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  display: flex; flex-direction: column;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
}
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
button:disabled { cursor: default; }

/* --- Шапка (под вырезом iPhone) --- */
.top {
  flex: none; display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(8px + var(--safe-top)) max(16px, env(safe-area-inset-right)) 8px max(16px, env(safe-area-inset-left));
  background: var(--top); color: var(--top-ink);
}
.who { display: flex; align-items: center; gap: 8px; font-size: 17px; min-width: 0; }
.top-right { text-align: right; }
.phase { font-weight: 600; font-size: 15px; }
.timer { font-variant-numeric: tabular-nums; font-size: 14px; opacity: .85; }
.timer.late { color: #ffb4a8; opacity: 1; }
.dot { display: inline-block; width: 12px; height: 12px; border-radius: 50%; border: 1.5px solid rgba(0,0,0,.35); flex: none; }

/* --- Сцена: карта, шторка, страницы вкладок --- */
.stage { flex: 1; position: relative; min-height: 0; overflow: hidden; }
.map-wrap { position: absolute; inset: 0; background: var(--sea); }
.map { position: absolute; inset: 0; touch-action: none; overflow: hidden; overscroll-behavior: none; }
.map svg { width: 100%; height: 100%; display: block; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.page {
  position: absolute; inset: 0; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  padding: 12px 16px 24px; touch-action: pan-y; display: none;
}

body[data-tab="orders"] .map-wrap { visibility: hidden; }
body[data-tab="orders"] .page { display: block; }
body[data-tab="orders"] .sheet, body[data-tab="results"] .sheet { display: none; }
body[data-tab="results"] .map-wrap { bottom: auto; height: 44%; }
body[data-tab="results"] .page { display: block; top: 44%; border-top: 1px solid var(--line); background: var(--bg); }
body[data-tab="results"] .zoom { bottom: 8px; }
body[data-tab="talks"] .map-wrap, body[data-tab="talks"] .sheet { display: none; }
body[data-tab="talks"] .page { display: flex; flex-direction: column; padding: 0; overflow: hidden; }

.zoom { position: absolute; right: 10px; bottom: calc(var(--sheet-min) + 12px); display: flex; flex-direction: column; gap: 6px; }
.zoom button {
  width: 40px; height: 40px; border-radius: 10px; border: 1px solid var(--line);
  background: rgba(255,255,255,.92); font-size: 20px; line-height: 1; box-shadow: 0 1px 3px rgba(0,0,0,.15);
}

/* --- Подсветка на карте --- */
.map path.sel { fill: #ffd23f !important; }
.map path.hl { fill: #7fe3ff !important; animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 50% { fill-opacity: .55; } }
.map .unit.ghost { opacity: .55; }
.map .coast-badge { font: 700 22px -apple-system, sans-serif; fill: #111; stroke: #fff; stroke-width: 5px; paint-order: stroke; }

/* --- Шторка --- */
.sheet {
  position: absolute; left: 0; right: 0; bottom: 0;
  height: var(--sheet-h, var(--sheet-min)); max-height: 60%;
  background: var(--card); border-radius: 16px 16px 0 0; box-shadow: 0 -4px 18px rgba(0,0,0,.18);
  display: flex; flex-direction: column; transition: height .22s ease;
}
.sheet.dragging { transition: none; }
.sheet[data-state="action"] { height: auto; max-height: 55%; }
.sheet-grip { flex: none; height: 18px; display: flex; justify-content: center; align-items: center; touch-action: none; cursor: grab; }
.sheet-grip span { width: 40px; height: 5px; border-radius: 3px; background: #cfc9bd; }
.sheet-body { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 0 16px 12px; touch-action: pan-y; }
.sheet-bar { display: flex; align-items: center; gap: 10px; min-height: 44px; touch-action: none; }
.sheet-bar .count { flex: 1; font-weight: 700; background: none; border: 0; text-align: left; padding: 0; min-height: 44px; }
.sheet-bar .count small { font-weight: 400; color: var(--muted); }

/* --- Таб-бар (над полоской «домой») --- */
.tabbar {
  flex: none; display: flex; background: var(--card); border-top: 1px solid var(--line);
  padding: 0 env(safe-area-inset-right) var(--safe-bottom) env(safe-area-inset-left);
}
.tabbar button {
  flex: 1; height: var(--tabbar-h); border: 0; background: none; position: relative;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 11px; color: var(--muted);
}
.tabbar svg { width: 24px; height: 24px; fill: currentColor; }
.tabbar button.on { color: var(--accent); font-weight: 600; }
.tabbar button:disabled { opacity: .35; }
.badge {
  position: absolute; top: 5px; left: calc(50% + 8px); min-width: 18px; padding: 0 5px; height: 18px; border-radius: 9px;
  background: var(--accent); color: #fff; font-size: 11px; line-height: 18px; font-weight: 700;
}
.badge.done { background: var(--ok); }

/* --- Общие элементы панелей --- */
.section-title { font-weight: 700; margin: 12px 0 6px; }
.muted { color: var(--muted); }
.small { font-size: 14px; }
.rows { display: flex; flex-direction: column; gap: 6px; }
.row { display: flex; align-items: stretch; background: var(--card); border: 1px solid var(--line); border-radius: 12px; }
.sheet .row { background: #faf9f6; }
.row.active { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(29,111,214,.2); }
.row.off { opacity: .45; }
.row-main {
  flex: 1; display: flex; flex-direction: column; align-items: flex-start; gap: 2px;
  padding: 10px 12px; min-height: 52px; border: 0; background: none; text-align: left;
}
.row-unit { font-weight: 600; }
.row-order { font-size: 15px; }
.row-order.warn { color: var(--warn); }
.row-side { display: flex; align-items: center; gap: 6px; padding-right: 8px; flex-wrap: wrap; justify-content: flex-end; }
.ghost { border: 0; background: none; min-width: 44px; min-height: 44px; font-size: 18px; color: var(--muted); }

.step-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.step-head b { font-size: 17px; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 6px 0; }
.targets { display: flex; flex-wrap: wrap; gap: 8px; margin: 6px 0 4px; max-height: 30vh; overflow-y: auto; }
/* В панели действий на карте — по одной строке с горизонтальной прокруткой: панель низкая, карта видна */
.sheet .actions, .sheet .targets {
  flex-wrap: nowrap; overflow-x: auto; overflow-y: hidden; max-height: none;
  margin-left: -16px; margin-right: -16px; padding: 0 16px; scrollbar-width: none; touch-action: pan-x;
  overscroll-behavior-x: contain;
}
.sheet .actions::-webkit-scrollbar, .sheet .targets::-webkit-scrollbar { display: none; }
.sheet .chip-btn { flex: none; }
.chip-btn {
  min-height: 44px; padding: 8px 14px; border-radius: 22px; border: 1px solid var(--line); background: #f7f6f2; white-space: nowrap;
}
.chip-btn.on { background: var(--accent); color: #fff; border-color: var(--accent); }
.chip-btn.target { background: #e8f8fd; border-color: #8fd5ea; }
.chip-btn.target.hold { background: #eef8f1; border-color: #9fd3b0; font-weight: 600; }
.chip-btn:disabled { opacity: .4; }

.ready-strip { margin-top: 14px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 14px; background: var(--card); border: 1px solid var(--line); font-size: 14px; }
.chip.on { border-color: var(--ok); color: var(--ok); font-weight: 600; }

.ready {
  min-height: 52px; border: 0; border-radius: 14px; padding: 0 18px;
  background: var(--accent); color: #fff; font-size: 17px; font-weight: 700;
}
.ready.on { background: var(--ok); }
.ready.small { min-height: 40px; font-size: 15px; padding: 0 14px; border-radius: 12px; }
.page .ready.wide { width: 100%; margin-top: 14px; }
.ready:disabled { opacity: .6; }

/* Сводка «что будет при „Готов“» */
.summary { margin-top: 12px; padding: 10px 12px; border-radius: 12px; background: #fff8e8; border: 1px solid #f0d9a8; font-size: 15px; }
.summary.ok { background: #eef8f1; border-color: #bfe3cb; }
.summary p { margin: 2px 0; }

/* Зимний экран */
.counter { display: inline-block; padding: 2px 10px; border-radius: 12px; background: var(--ink); color: #fff; font-size: 14px; font-weight: 700; margin-left: 6px; }
.counter.full { background: var(--ok); }

/* Итоги */
.res-nav { display: flex; align-items: center; justify-content: space-between; }
.res-nav .section-title { margin: 4px 0; text-align: center; flex: 1; }
.res-nav .ghost { font-size: 28px; }
.res-nav .ghost:disabled { opacity: .25; }
.res-power { display: flex; align-items: center; gap: 8px; font-weight: 700; margin: 12px 0 4px; }
.res-row { display: flex; justify-content: space-between; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--line); font-size: 15px; }
.res-row.bad .res-code { color: var(--bad); font-weight: 600; }
.res-row.auto .res-code { color: var(--warn); font-weight: 600; }
.res-code { color: var(--ok); white-space: nowrap; }
.legend { margin-bottom: 4px; }
.note { margin: 10px 0; padding: 10px 12px; border-radius: 12px; background: var(--card); border: 1px solid var(--line); }

/* Модальное подтверждение */
.modal { position: fixed; inset: 0; z-index: 20; background: rgba(15,20,28,.45); display: flex; align-items: flex-end; }
.modal[hidden] { display: none; }
.modal-box {
  width: 100%; background: var(--card); border-radius: 16px 16px 0 0;
  padding: 16px 16px calc(16px + var(--safe-bottom));
}
.modal-box h3 { margin: 0 0 8px; font-size: 18px; }
.modal-box ul { margin: 6px 0 12px; padding-left: 20px; }
.modal-actions { display: flex; gap: 10px; }
.modal-actions button { flex: 1; min-height: 50px; border-radius: 12px; border: 1px solid var(--line); background: #f7f6f2; font-weight: 600; }
.modal-actions .go { background: var(--accent); color: #fff; border-color: var(--accent); }

.toast {
  position: fixed; left: 16px; right: 16px; bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 84px);
  padding: 12px 14px; border-radius: 12px; background: var(--ink); color: #fff; white-space: pre-line;
  opacity: 0; transform: translateY(10px); transition: .2s; pointer-events: none; z-index: 30;
}
.toast.show { opacity: 1; transform: none; }
.toast.error { background: var(--bad); }

/* Лобби Mini App: обычная прокручиваемая страница без таб-бара */
body.lobby .page { display: block; }
.row-unit .dot { vertical-align: -1px; }

/* Переключатель держав (игрок с несколькими державами) */
.powerbar { display: none; }
body.multi .powerbar {
  flex: none; display: flex; gap: 6px; overflow-x: auto; padding: 6px 12px; background: #263241;
  scrollbar-width: none; touch-action: pan-x;
}
.powerbar::-webkit-scrollbar { display: none; }
.power-tab {
  flex: none; display: inline-flex; align-items: center; gap: 6px; min-height: 36px; padding: 4px 12px;
  border-radius: 18px; border: 1px solid rgba(255,255,255,.18); background: transparent; color: #e8edf3; font-size: 14px;
}
.power-tab small { opacity: .75; font-variant-numeric: tabular-nums; }
.power-tab.on { background: #f4f1ea; color: var(--ink); border-color: #f4f1ea; font-weight: 600; }

/* Закрытая нейтральная территория (игра на двоих до 1915 года) */
.map path.closed { fill: #d9d4cb !important; stroke-dasharray: 6 4; }

/* «Мои партии» */
.game-card {
  display: flex; flex-direction: column; gap: 4px; width: 100%; text-align: left; padding: 12px 14px;
  border-radius: 14px; border: 1px solid var(--line); background: var(--card);
}
.game-card.mine { border: 2px solid var(--accent); box-shadow: 0 0 0 3px rgba(29,111,214,.12); }
.game-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.game-phase { display: flex; justify-content: space-between; gap: 8px; font-size: 15px; }
.timer-mini { font-variant-numeric: tabular-nums; color: var(--muted); white-space: nowrap; }
.status { font-size: 13px; font-weight: 600; padding: 2px 8px; border-radius: 10px; white-space: nowrap; }
.status.bad { background: #fde8e6; color: var(--bad); }
.status.warn { background: #fff3dc; color: var(--warn); }
.status.ok { background: #e3f4e9; color: var(--ok); }
.status.muted { background: #efede7; color: var(--muted); }
.bot-mark { font-size: 12px; margin-left: 2px; }

/* --- Переговоры --- */
.talk-scroll { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 4px 16px 24px; touch-action: pan-y; }
.talk-head { flex: none; display: flex; align-items: center; gap: 10px; padding: 8px 16px; min-height: 52px;
  background: var(--card); border-bottom: 1px solid var(--line); }
.talk-scroll .talk-head { background: none; border: 0; padding: 4px 0; }
.talk-title { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-row { display: flex; align-items: center; gap: 10px; width: 100%; text-align: left; min-height: 56px; padding: 8px 12px;
  background: var(--card); border: 1px solid var(--line); border-radius: 12px; }
.chat-row.unread .chat-title { font-weight: 700; }
.chat-row.picked { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(29,111,214,.2); }
.chat-dots { display: flex; gap: 2px; flex: none; }
.chat-icon { font-size: 18px; }
.chat-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.chat-title { font-weight: 600; }
.chat-last { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-unread { flex: none; min-width: 22px; height: 22px; border-radius: 11px; padding: 0 6px; background: var(--accent); color: #fff;
  font-size: 12px; line-height: 22px; text-align: center; }
.check { flex: none; width: 22px; color: var(--accent); font-weight: 700; }
.msgs { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 8px 12px; touch-action: pan-y;
  display: flex; flex-direction: column; gap: 4px; }
.msgs-empty { text-align: center; margin: 24px 16px; }
.phase-divider { align-self: center; font-size: 12px; color: var(--muted); background: var(--line); border-radius: 10px;
  padding: 2px 10px; margin: 8px 0 4px; }
.msg { max-width: 82%; align-self: flex-start; background: var(--card); border: 1px solid var(--line); border-radius: 14px 14px 14px 4px;
  padding: 6px 10px 4px; }
.msg.mine { align-self: flex-end; background: #dcebff; border-color: #c4dbfa; border-radius: 14px 14px 4px 14px; }
.msg-from { font-size: 13px; font-weight: 600; display: flex; align-items: center; gap: 4px; margin-bottom: 2px; }
.msg-from .dot { width: 9px; height: 9px; }
.msg-text { white-space: pre-wrap; overflow-wrap: anywhere; }
.msg-time { font-size: 11px; color: var(--muted); text-align: right; }
.composer { flex: none; display: flex; align-items: flex-end; gap: 8px; padding: 8px 12px; background: var(--card);
  border-top: 1px solid var(--line); }
.composer-input { flex: 1; resize: none; font: inherit; font-size: 16px; padding: 9px 12px; border: 1px solid var(--line);
  border-radius: 18px; max-height: 120px; background: #faf9f6; color: var(--ink); }
.send { flex: none; width: 42px; height: 42px; border-radius: 50%; border: 0; background: var(--accent); color: #fff; font-size: 18px; }
.composer-off { flex: none; text-align: center; padding: 12px; }
.tabbar button[hidden] { display: none; }

/* --- Заглавная страница сайта --- */
body.landing { position: static; overflow: auto; height: auto; min-height: 100%; display: block; }
.landing-main { max-width: 640px; margin: 0 auto; padding: 32px 16px 48px; text-align: center; }
.pigs { width: 100%; height: auto; display: block; border-radius: 24px; box-shadow: 0 6px 24px rgba(0,0,0,.12); }
.landing h1 { font-size: 40px; margin: 24px 0 8px; letter-spacing: -.5px; }
.tagline { font-size: 18px; color: var(--muted); margin: 0 auto 24px; max-width: 480px; }
.login-title { font-weight: 700; margin: 0 0 10px; }
.login-box { min-height: 48px; display: flex; justify-content: center; }
.error-text { color: var(--bad); }
.shelf { display: flex; flex-direction: column; gap: 6px; }
.shelf .chip-btn { align-self: flex-start; }
.user-name { opacity: .8; margin-left: 4px; }
.top .who .chip-btn { color: var(--ink); }

/* --- Партия: итоговая таблица, диалоги, уровни ботов --- */
.standings { width: 100%; border-collapse: collapse; margin: 8px 0; font-size: 14px; }
.standings th, .standings td { text-align: left; padding: 5px 6px; border-bottom: 1px solid var(--line); }
.standings th { color: var(--muted); font-weight: 600; font-size: 12px; }
.wide-box { max-width: 520px; }
.confirm-input { width: 100%; font: inherit; font-size: 16px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; margin: 4px 0 12px; }
.modal-actions .danger, .chip-btn.danger { color: var(--bad); }
.modal-actions .go.danger { background: var(--bad); color: #fff; }
.game-panel { margin-top: 18px; padding-top: 6px; border-top: 1px dashed var(--line); }
.link-btn { display: block; text-align: center; text-decoration: none; line-height: 44px; margin: 12px 0; }
.level-pick { display: flex; flex-direction: column; gap: 6px; width: 100%; }
.level-btn { display: flex; flex-direction: column; align-items: flex-start; text-align: left; gap: 1px; padding: 8px 10px;
  border: 1px solid var(--line); border-radius: 10px; background: #faf9f6; }
.level-btn:hover { border-color: var(--accent); }
.row.picking { flex-direction: column; }
.row.picking .row-side { padding: 0 10px 10px; justify-content: flex-start; }
.hotkeys { margin-top: 16px; }
.side { display: none; }
.tab-orders { display: none; }
.map path.nb { stroke: #1d6fd6; stroke-width: 4px; }
.map path.hov { stroke: #ffb000; stroke-width: 6px; }

/* --- Десктоп: карта слева на всю высоту, справа панель с вкладками, «Мои партии» сбоку --- */
@media (min-width: 900px) {
  body:not(.lobby):not(.landing) {
    display: grid;
    grid-template-columns: 240px minmax(0, 1fr) 420px;
    grid-template-rows: auto auto auto minmax(0, 1fr);
    grid-template-areas: "top top top" "side bar bar" "side map tabs" "side map panel";
  }
  .top { grid-area: top; }
  .powerbar { grid-area: bar; }
  .side { display: flex; flex-direction: column; gap: 4px; grid-area: side; min-height: 0; overflow-y: auto;
    background: var(--card); border-right: 1px solid var(--line); padding: 10px 8px; }
  .side-head { display: flex; align-items: center; justify-content: space-between; padding: 4px 6px 8px; font-weight: 700; }
  .side-head a { color: var(--ink); text-decoration: none; }
  .side-game { display: flex; flex-direction: column; padding: 8px 10px; border-radius: 10px; color: var(--ink); text-decoration: none; }
  .side-game:hover { background: #f1eee6; }
  .side-game.on { background: #e5effc; }
  .side-game.mine b::after { content: ' •'; color: var(--bad); }
  .stage { display: contents; }
  .map-wrap, body[data-tab="orders"] .map-wrap, body[data-tab="results"] .map-wrap, body[data-tab="talks"] .map-wrap {
    grid-area: map; position: relative; inset: auto; height: auto; bottom: auto; visibility: visible; display: block; min-height: 0; }
  .tabbar { grid-area: tabs; border-top: 0; border-left: 1px solid var(--line); border-bottom: 1px solid var(--line); padding: 0; }
  .tabbar button { height: 46px; flex-direction: row; gap: 6px; font-size: 14px; }
  .tabbar svg { width: 18px; height: 18px; }
  .tabbar button[data-tab="orders"] { display: none; }
  .tab-map { display: none; }
  .tab-orders { display: inline; }
  .badge { position: static; margin-left: 2px; }
  .sheet, .page, body[data-tab="results"] .page, body[data-tab="talks"] .page {
    grid-area: panel; position: relative; inset: auto; top: auto; min-height: 0; border-left: 1px solid var(--line); border-top: 0; }
  .sheet { height: auto !important; max-height: none; border-radius: 0; box-shadow: none; transition: none; }
  .sheet[data-state="action"] { height: auto; max-height: none; }
  .sheet-grip { display: none; }
  .sheet-body { padding-top: 12px; }
  body[data-tab="map"] .page { display: none; }
  body:not([data-tab="map"]) .sheet { display: none; }
  .zoom { bottom: 12px; }
  body.lobby .page { left: calc(50% - 360px); right: auto; width: 720px; }
}
@media (min-width: 900px) { .modal { align-items: center; justify-content: center; } }
@media (max-width: 480px) { .user-name { display: none; } }
