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

* { 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, #game {
  background: var(--panel);
  border: 3px solid var(--ink);
  border-radius: 6px;
  padding: 16px;
  max-width: 680px;
  width: 100%;
}

header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
}

header h1 { margin: 0 auto 0 0; font-size: 24px; }

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;
}

/* Двор: ширина по контейнеру; листание и масштаб делает камера в game.js. */
#yardWrap {
  max-width: 100%;
  overflow: hidden;
  border: 3px solid var(--ink);
  border-radius: 4px;
}
/* Корзина сбоку: появляется при перетаскивании объекта, подсвечивается над ней. */
#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);
}
#yard {
  display: block;
  height: auto;
  image-rendering: pixelated;
  cursor: pointer;
  background: #7fbf5a;
  /* Перетаскивание и щипок обрабатывает камера в game.js, поэтому жесты страницы отключены. */
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;
}

.error { color: var(--accent); min-height: 1.2em; }
.hint, #message { color: #6b5b3e; font-size: 14px; }
.goal { font-weight: bold; color: #5a3a12; }
#message { min-height: 1.2em; }
#selected { font-weight: bold; }

#yardControls { margin-top: 8px; }
.scroll { display: flex; align-items: center; gap: 4px; }
.scroll input { width: 120px; }
.decor-item span { flex: 1; }
.picked { background: #ffe9a8; border-radius: 4px; }

/* Телефоны: крупные цели для пальца (не меньше 44px по рекомендациям Apple). */
@media (pointer: coarse), (max-width: 600px) {
  input, select, button {
    min-height: 44px;
    padding: 10px 12px;
  }
  .row { gap: 10px; }
  header h1 { font-size: 22px; }
}

/* Планшеты (iPad, 720px и шире): панель и поле крупнее, шрифты и кнопки удобнее для пальца. */
@media (min-width: 720px) {
  body { padding: 24px; }
  .panel, #game { max-width: 760px; padding: 20px 24px; }
  h1 { font-size: 34px; }
  header h1 { font-size: 30px; }
  header span { font-size: 18px; }
  #selected { font-size: 18px; }
  .hint, #message { font-size: 16px; }
  input, select, button { font-size: 18px; min-height: 48px; padding: 10px 14px; }
  .row { gap: 12px; }
}

/* Альбомная ориентация телефона: шапка компактнее, кнопки ниже. */
@media (orientation: landscape) and (max-height: 500px) {
  body { padding-top: 8px; padding-bottom: 8px; }
  h1 { font-size: 22px; margin-bottom: 4px; }
  #game { padding: 10px; }
  .row { margin: 6px 0; }
}

/* Меню кота: модальное окно поверх двора. */
dialog {
  width: min(92vw, 540px);
  max-height: 88vh;
  max-height: 88dvh;
  overflow: auto;
  padding: 16px 20px;
  border: 3px solid var(--ink);
  border-radius: 8px;
  background: var(--panel);
  color: var(--ink);
}
dialog::backdrop { background: rgba(42, 33, 24, 0.5); }
dialog h3 { margin: 14px 0 4px; font-size: 18px; }
.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.dialog-head h2 { margin: 0; font-size: 26px; }
#closeDialog { min-width: 44px; }
#placed .row { margin: 6px 0; }
#placed .row span { flex: 1 1 100%; font-weight: bold; }
#dlgMessage { color: #6b5b3e; min-height: 1.2em; }

/* Телефон: меню выезжает снизу, как шторка. */
@media (max-width: 600px) {
  dialog {
    position: fixed;
    inset: auto 0 0 0;
    margin: 0;
    width: 100%;
    max-width: 100%;
    max-height: 85vh;
    max-height: 85dvh;
    border-radius: 12px 12px 0 0;
  }
}

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

/* ===== Один экран: панель управления сверху, поле под ней на всю оставшуюся высоту ===== */
#game {
  position: fixed;
  inset: 0;
  z-index: 1;
  width: auto;
  max-width: none;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: #7fbf5a;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
#bar {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: max(8px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) 8px max(10px, env(safe-area-inset-left));
  background: var(--panel);
  border-bottom: 3px solid var(--ink);
}
.bar-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}
#bar h1 { font-size: 20px; margin: 0 6px 0 0; }
#bar .chip {
  padding: 4px 10px;
  background: #f5ecd6;
  border-radius: 999px;
  font-size: 15px;
}
#user:empty { display: none; }
#bar button { min-height: 40px; padding: 4px 12px; font-size: 15px; }
.spacer { flex: 1; }
.round {
  width: 44px;
  min-width: 44px;
  height: 44px;
  min-height: 44px;
  padding: 0;
  display: grid;
  place-items: center;
  border-radius: 50%;
  border: 3px solid var(--ink);
  background: var(--panel);
  color: var(--ink);
}
.round.ok { background: #7bc47f; }
#yardControls {
  display: flex;
  gap: 8px;
  margin: 0;
}
#yardControls .scroll { display: none; }
.dock-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  padding: 6px 16px;
  font-size: 16px;
  font-weight: bold;
  color: var(--ink);
  background: var(--panel);
  border: 3px solid var(--ink);
  border-radius: 999px;
}
.dock-btn[aria-expanded="true"] { background: #ffe9a8; }

/* Магазин раскрывается между панелью и полем и сдвигает поле вниз. */
#panel.sheet {
  flex: 0 1 auto;
  max-height: 34vh;
  overflow-y: auto;
  padding: 10px max(12px, env(safe-area-inset-right)) 10px max(12px, env(safe-area-inset-left));
  background: var(--panel);
  border-bottom: 3px solid var(--ink);
  display: none;
}
#panel.sheet.open { display: block; }
#panel.sheet .goal { margin: 0 0 6px; }
#panel.sheet h3 { margin: 10px 0 4px; }

/* Поле занимает всё оставшееся место; оверлеи лежат поверх него. */
#yardWrap {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  max-width: none;
  border: 0;
  border-radius: 0;
  overflow: hidden;
}
#yard {
  position: absolute;
  top: 0;
  left: 0;
  background: transparent;
}

/* Корзина: поверх поля справа по центру. */
#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; }
#placeHint {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 15;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px;
  max-width: calc(100% - 24px);
  padding: 8px 12px;
  background: var(--panel);
  border: 3px solid var(--ink);
  border-radius: 14px;
  pointer-events: auto;
}
#placeHint span { font-size: 15px; }
#placeHint button { min-height: 40px; padding: 6px 12px; font-size: 15px; }
#message.toast {
  position: absolute;
  left: 50%;
  bottom: 12px;
  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; }
