:root {
  --ink: #2a2118;
  --paper: #f6efdc;
  --accent: #c8553d;
  --panel: #fffaf0;
  --grass: #7fbf5a;
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  font-family: "Trebuchet MS", system-ui, sans-serif;
  color: var(--ink);
  background: #d9c9a3;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  /* Отступы с учётом выреза и home-indicator (нужен viewport-fit=cover). */
  padding:
    max(16px, env(safe-area-inset-top))
    max(16px, env(safe-area-inset-right))
    max(16px, env(safe-area-inset-bottom))
    max(16px, env(safe-area-inset-left));
}

.hidden { display: none !important; }

.panel {
  background: var(--panel);
  border: 3px solid var(--ink);
  border-radius: 6px;
  padding: 16px;
  max-width: 680px;
  width: 100%;
}

h1 { font-size: 28px; margin: 0 0 8px; }

label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 12px;
}

input, select, button {
  font: inherit;
  padding: 8px 10px;
  border: 2px solid var(--ink);
  border-radius: 4px;
  background: var(--paper);
  color: var(--ink);
}

/* 16px и больше: iOS Safari не увеличивает страницу при фокусе на поле. */
input, select { font-size: 16px; }

button { cursor: pointer; }
button:hover:not(:disabled) { background: #fff; }
button:disabled { opacity: 0.5; cursor: default; }
button[name="mode"][value="register"] { background: #e8d9b5; }

.row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin: 10px 0;
}

.error { color: var(--accent); min-height: 1.2em; }
.hint { color: #6b5b3e; font-size: 14px; }

/* ===== Экран игры: двор на весь экран, всё управление поверх него ===== */
#game {
  position: fixed;
  inset: 0;
  z-index: 1;
  background: var(--grass);
  overflow: hidden;
}

#yardWrap {
  position: absolute;
  inset: 0;
  overflow: hidden;
}

#yard {
  position: absolute;
  top: 0;
  left: 0;
  display: block;
  image-rendering: pixelated;
  cursor: pointer;
  background: var(--grass);
  /* Перетаскивание и щипок обрабатывает камера в game.js, поэтому жесты страницы отключены. */
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;
}

/* Круглые кнопки без подписей: общий вид для аватара, инструментов и действий кота. */
#game .round {
  width: 52px;
  min-width: 52px;
  height: 52px;
  min-height: 52px;
  padding: 0;
  display: grid;
  place-items: center;
  border-radius: 50%;
  border: 3px solid var(--ink);
  background: rgba(255, 250, 240, 0.92);
  color: var(--ink);
  box-shadow: 0 3px 0 var(--ink);
  font-size: 22px;
  line-height: 1;
}
#game .round.ok { background: #7bc47f; }
#game .round.active { background: #ffe9a8; }

/* Аватар пользователя: первая буква логина, по нажатию — меню. */
#account {
  position: relative;
  align-self: flex-end;
  pointer-events: auto;
}
.avatar { font-size: 22px; font-weight: bold; }
.menu {
  position: absolute;
  top: 60px;
  right: 0;
  min-width: 200px;
  display: flex;
  flex-direction: column;
  padding: 8px;
  background: var(--panel);
  border: 3px solid var(--ink);
  border-radius: 12px;
  box-shadow: 0 4px 0 var(--ink);
}
.menu-name { font-weight: bold; padding: 6px 8px 8px; }
.menu-item {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0;
  padding: 8px;
  font-size: 16px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  text-align: left;
}
.menu-item:hover:not(:disabled) { background: #f5ecd6; }
.menu-item input { width: 18px; height: 18px; margin: 0; }

/* Попап профиля и настроек поверх двора. */
.dialog-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(42, 33, 24, 0.5);
}
.dialog-panel {
  width: 100%;
  max-width: 360px;
  padding: 20px;
  background: var(--panel);
  border: 3px solid var(--ink);
  border-radius: 12px;
  box-shadow: 0 4px 0 var(--ink);
}
.dialog-panel h2 { font-size: 24px; margin: 0 0 8px; }

/* Экран победы: кадры кота сменяют друг друга по фазе (--i), кнопка «Играть заново» внизу панели. */
.win-overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(42, 33, 24, 0.7);
}
.win-overlay.hidden { display: none; }
.win-panel {
  width: 100%;
  max-width: 380px;
  max-height: calc(100vh - 32px);
  overflow: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 20px;
  text-align: center;
  background: var(--panel);
  border: 3px solid var(--ink);
  border-radius: 12px;
  box-shadow: 0 4px 0 var(--ink);
}
.win-panel h2 { font-size: 28px; margin: 0; }
.win-panel h3 { font-size: 18px; margin: 8px 0 0; }
.win-frames { position: relative; width: 100%; height: 240px; animation: winBob 0.9s ease-in-out infinite; }
.win-frame {
  position: absolute;
  left: 50%;
  top: 0;
  height: 240px;
  width: auto;
  transform: translateX(-50%);
  animation: winFrame 0.9s linear infinite;
  animation-delay: calc(var(--i) * -0.3s);
}
@keyframes winFrame {
  0%, 32% { opacity: 1; }
  33.4%, 100% { opacity: 0; }
}
@keyframes winBob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-8px); }
}
.win-congrats { margin: 0; font-size: 18px; }
.win-ranking { list-style: none; margin: 0; padding: 0; width: 100%; font-size: 16px; }
.win-ranking li { padding: 4px 0; }
.win-ranking li.mine { font-weight: 700; }
.win-again { margin-top: auto; width: 100%; padding: 12px; font-size: 18px; }
.dialog-panel h3 { font-size: 18px; margin: 16px 0 4px; }
.dialog-panel label { flex-direction: row; align-items: center; gap: 8px; padding: 8px 0; }
.dialog-panel input[type="checkbox"] { width: 18px; height: 18px; margin: 0; }
.dialog-panel p { margin: 4px 0 12px; }
.dialog-panel button { display: block; width: 100%; margin-top: 8px; }
.dialog-panel button.danger { background: var(--accent); color: #fff; }
.dialog-panel button.danger:hover:not(:disabled) { background: #d9654b; }

/* Карусель вещей внизу: горизонтальная лента, 4 вещи в кадре, прокрутка по кругу.
   pan-x: горизонтальный свайп листает ленту, вертикальный тянет вещь на двор.
   Под лентой — панель характеристик котов. */
#dock {
  position: absolute;
  left: 0;
  right: 0;
  bottom: max(8px, env(safe-area-inset-bottom));
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 0 8px;
  pointer-events: none;
}
#dock > * { pointer-events: auto; }
/* Панель характеристик котов: по одной карточке на кота, плавные полосы заполнения. */
#stats {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
}
#stats::-webkit-scrollbar { display: none; }
.stat-card {
  flex: 0 0 auto;
  min-width: 116px;
  padding: 6px 8px;
  border-radius: 12px;
  background: rgba(255, 248, 232, 0.9);
  box-shadow: 0 2px 0 rgba(29, 26, 22, 0.25);
  font-size: 12px;
  color: #1d1a16;
  text-align: left;
}
.stat-card.selected { outline: 2px solid #f5c84b; }
.stat-card.locked { opacity: 0.7; }
.stat-name-line { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stat-lock { font-weight: 400; font-size: 11px; color: #a4561e; margin-left: 4px; }
.stat-timer { font-size: 11px; font-weight: 700; color: #2e6b3a; margin-top: 2px; }
.stat-grid { display: grid; grid-template-columns: max-content minmax(70px, 1fr); align-items: center; column-gap: 6px; row-gap: 3px; margin-top: 3px; }
.stat-label { color: #5a4a38; white-space: nowrap; }
.stat-track { display: block; height: 9px; border-radius: 5px; background: rgba(29, 26, 22, 0.15); overflow: hidden; }
.stat-fill { display: block; height: 100%; border-radius: 5px; background: #d9913f; transition: width 0.4s ease; }
.stat-empty { color: #5a4a38; padding: 8px; }
/* Монеты в инвентаре персонажа: монетка и число. */
.coin-line {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 0 0 8px;
  font-size: 18px;
}

/* Кружки действий вокруг кота: появляются над головой, без подписей. */
#petRing {
  position: absolute;
  z-index: 12;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  width: 180px;
  transform: translate(-50%, -100%);
  pointer-events: auto;
}
#petRing .round { width: 46px; min-width: 46px; height: 46px; min-height: 46px; font-size: 20px; }

/* Подсказка над миской или поилкой при наведении: сколько осталось. Не перехватывает указатель. */
#fillTip {
  position: absolute;
  z-index: 11;
  padding: 4px 10px;
  border: 2px solid var(--ink);
  border-radius: 10px;
  background: rgba(255, 250, 240, 0.96);
  color: var(--ink);
  font: inherit;
  white-space: nowrap;
  pointer-events: none;
  transform: translate(-50%, -100%);
}

/* Характеристики кота в руках: висят над ним, пока его тянут. Не перехватывают указатель. */
#catTip {
  position: absolute;
  z-index: 11;
  width: 160px;
  padding: 6px 8px;
  border: 2px solid var(--ink);
  border-radius: 10px;
  background: rgba(255, 250, 240, 0.96);
  color: var(--ink);
  font: inherit;
  font-size: 12px;
  pointer-events: none;
  transform: translate(-50%, -100%);
}

/* Корзина сбоку: появляется при перетаскивании объекта, подсвечивается над ней. */
#trash {
  position: fixed;
  right: max(16px, env(safe-area-inset-right));
  top: 50%;
  z-index: 20;
  display: grid;
  place-items: center;
  width: 64px;
  height: 64px;
  border-radius: 50%;
  border: 3px solid var(--ink);
  background: #f5ecd6;
  color: var(--ink);
  box-shadow: 0 4px 0 var(--ink);
  opacity: 0;
  pointer-events: none;
  transform: translate(120%, -50%) scale(0.8);
  transition: opacity 0.15s, transform 0.2s;
}
#trash.show {
  opacity: 1;
  transform: translate(0, -50%) scale(1);
}
#trash.hot {
  background: #e0604f;
  color: #fff;
  transform: translate(-6px, -50%) scale(1.15);
}

/* Подтверждение удаления: справа по центру. */
#trashConfirm {
  position: absolute;
  right: 84px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  gap: 10px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s;
  z-index: 25;
}
#trashConfirm.show { opacity: 1; pointer-events: auto; }

.hud { position: absolute; }

/* Правый верхний угол под кнопкой профиля: персонаж с вещами в руках и список задач. Клики проходят мимо, кроме панели. */
#sideHud {
  top: max(12px, env(safe-area-inset-top));
  right: max(12px, env(safe-area-inset-right));
  z-index: 9;
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 190px;
  max-width: calc(100% - 24px);
  pointer-events: none;
}
#handsPanel {
  pointer-events: auto;
  padding: 6px 8px;
  font-size: 13px;
  background: rgba(255, 250, 240, 0.95);
  border: 3px solid var(--ink);
  border-radius: 12px;
}
.hands-head { display: flex; align-items: center; gap: 6px; }
#handsName { font-size: 14px; line-height: 1.2; }
#handsPanel .coin-line { font-size: 14px; margin: 0 0 4px; gap: 4px; }
#handsPanel .coin-line svg { width: 16px; height: 16px; }
#handsPortrait { width: 36px; height: 36px; object-fit: contain; flex: 0 0 auto; }
.hands-slots { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; min-height: 8px; }
.hand-slot {
  position: relative;
  width: 40px;
  height: 40px;
  padding: 2px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
  border: 2px solid var(--ink);
  border-radius: 8px;
}
.hand-slot[aria-pressed="true"] { background: #ffe27a; }
.hand-slot .board-thumb { width: 30px; height: 30px; }
.hand-slot.is-empty .board-thumb { opacity: 0.35; }
.hand-badge {
  position: absolute;
  left: 50%;
  bottom: -7px;
  transform: translateX(-50%);
  padding: 0 4px;
  font-size: 11px;
  line-height: 14px;
  white-space: nowrap;
  color: #fff;
  background: #1d1a16;
  border-radius: 6px;
}
.hands-hint { margin: 4px 0 0; font-size: 12px; line-height: 1.25; }
#taskList {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 8px 10px;
  background: rgba(255, 250, 240, 0.92);
  border: 3px solid var(--ink);
  border-radius: 12px;
  font-size: 14px;
  pointer-events: none;
}
#taskList:empty { display: none; }
#taskList .task { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#taskList .task.done { opacity: 0.6; }
#taskList .task.failed { color: #b3261e; }

/* Короткие сообщения (ошибки, подтверждения) внизу поля. */
#message.toast {
  position: absolute;
  left: 50%;
  bottom: calc(max(8px, env(safe-area-inset-bottom)) + 112px);
  transform: translateX(-50%);
  z-index: 15;
  width: max-content;
  max-width: calc(100% - 24px);
  margin: 0;
  padding: 8px 14px;
  background: rgba(42, 33, 24, 0.85);
  color: #fff;
  border-radius: 12px;
  font-size: 15px;
  text-align: center;
  pointer-events: none;
  min-height: 0;
}
#message.toast:empty { display: none; }

/* Телефоны: крупные цели для пальца (не меньше 44px по рекомендациям Apple). */
@media (pointer: coarse), (max-width: 600px) {
  input, select, button { min-height: 44px; padding: 10px 12px; }
  #game .round { min-height: 52px; }
  #petRing .round { min-height: 46px; }
}

/* Телефоны: инвентарь справа под кнопкой профиля, прозрачный и узкий; касания проходят к двору, кроме слотов вещей. */
@media (max-width: 600px) {
  #sideHud { width: 150px; gap: 4px; }
  #handsPanel {
    padding: 0;
    background: transparent;
    border: 0;
    box-shadow: none;
    pointer-events: none;
    text-shadow: 0 0 3px #fffaf0, 0 0 3px #fffaf0;
  }
  #handsPanel .hand-slot { pointer-events: auto; background: rgba(255, 255, 255, 0.6); }
  #handsPortrait { width: 32px; height: 32px; }
  #handsName { font-size: 13px; }
  #handsPanel .coin-line { font-size: 13px; margin: 0 0 2px; }
  #handsPanel .coin-line svg { width: 14px; height: 14px; }
  .hands-slots { gap: 3px; margin-top: 2px; }
  .hand-slot { width: 34px; height: 34px; }
  .hand-slot .board-thumb { width: 24px; height: 24px; }
  .hands-hint { display: none; }
  #taskList { padding: 4px 6px; font-size: 12px; background: rgba(255, 250, 240, 0.6); border-width: 2px; }
}

/* Настольные экраны: поля и кнопки крупнее. */
@media (min-width: 1024px) {
  h1 { font-size: 36px; }
  input, select, button { font-size: 20px; min-height: 52px; padding: 10px 16px; }
  .hint, #message { font-size: 18px; }
}

.round.done { opacity: 0.5; }

/* Доска объявлений и выбор аватара: списки строк с кнопками справа. */
.board-list { display: flex; flex-direction: column; gap: 6px; }
.board-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.board-row span { flex: 1; min-width: 0; }
.board-thumb { width: 36px; height: 36px; flex: none; object-fit: contain; image-rendering: pixelated; }
.board-thumb-icon { display: inline-flex; align-items: center; justify-content: center; font-size: 24px; }
.board-price { font-weight: bold; white-space: nowrap; background: #fff8e1; border: 1px dashed #8a6d3b; border-radius: 4px; padding: 2px 6px; }
.board-row button { width: auto; }
#board .dialog-panel, #houseBoard .dialog-panel { max-height: 90vh; overflow-y: auto; }
#board .dialog-panel, #houseBoard .dialog-panel { max-width: 560px; position: relative; }

/* Доска: товары, заказы, ворота и склад — карточки в два столбца. Блок персонажа остаётся списком. */
.board-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.board-grid > .hint { grid-column: 1 / -1; margin: 0; }
.board-grid .board-row {
  flex-direction: column;
  justify-content: flex-start;
  gap: 6px;
  padding: 10px 8px;
  text-align: center;
  background: #fffdf5;
  border: 2px solid var(--ink);
  border-radius: 10px;
  box-shadow: 0 3px 0 var(--ink);
}
.board-grid .board-row span { flex: none; font-weight: bold; }
.board-grid .board-thumb { width: 56px; height: 56px; }
.board-grid .board-thumb-icon { font-size: 36px; }
.board-grid .board-row button { width: 100%; margin: 0; }
.board-grid button.stock-card { width: 100%; margin: 0; cursor: pointer; font: inherit; color: inherit; }
.board-grid button.stock-card:disabled { cursor: default; }

/* Магазин: 4 карточки в ряд (по числу товаров первой необходимости), картинка 56 px, сама карточка — кнопка покупки. */
.board-shop { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.board-shop .shop-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  width: 100%;
  margin: 0;
  padding: 6px 4px;
  font-size: 12px;
  line-height: 1.2;
  background: #fffdf5;
  border: 2px solid var(--ink);
  border-radius: 8px;
  box-shadow: 0 2px 0 var(--ink);
  color: inherit;
}
.board-shop .shop-card:hover:not(.is-off) { background: #fff; }
.board-shop .shop-card.is-off { opacity: 0.5; cursor: not-allowed; box-shadow: none; }
.board-shop .shop-card .board-thumb { width: 56px; height: 56px; }
.board-shop .shop-card .board-thumb-icon { font-size: 36px; }
.board-shop .shop-name { font-weight: bold; text-align: center; }
.board-shop .shop-card small { color: #666; text-align: center; }
.board-shop .shop-card .board-price { font-size: 12px; }

/* Диалог спонсора во дворе. */
.chat-bubble {
  position: absolute;
  z-index: 14;
  max-width: min(280px, calc(100% - 32px));
  padding: 12px 14px;
  border: 2px solid var(--ink);
  border-radius: 16px;
  background: var(--panel);
  color: var(--ink);
  box-shadow: 0 3px 0 var(--ink);
}
.chat-bubble::after {
  content: "";
  position: absolute;
  left: 24px;
  bottom: -12px;
  width: 18px;
  height: 18px;
  background: var(--panel);
  border-right: 2px solid var(--ink);
  border-bottom: 2px solid var(--ink);
  transform: rotate(45deg);
}
.chat-answer {
  margin: 8px 6px 0 0;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--paper);
  font-weight: bold;
}
.chat-answer:hover:not(:disabled) { background: #ffe9a8; }
.sponsor-btn {
  position: absolute;
  left: max(8px, env(safe-area-inset-left));
  top: max(8px, env(safe-area-inset-top));
  z-index: 12;
  display: grid;
  place-items: center;
  width: 96px;
  min-width: 96px;
  height: 96px;
  padding: 0;
  overflow: hidden;
  border: 0;
  border-radius: 12px;
  background: transparent;
  cursor: pointer;
}
.sponsor-btn img, .sponsor-btn svg {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

/* Монета, которая летит от спонсора в инвентарь (см. flyCoin в game.js). */
.flying-coin {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 60;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid var(--ink);
  background: radial-gradient(circle at 35% 35%, #fff3b0, #f7c948 55%, #c98f12);
  pointer-events: none;
  will-change: transform;
}
