* { box-sizing: border-box; }
[hidden] { display: none !important; }

:root {
  --green: #4a6b2a;
  --green-dark: #3a5520;
  --green-mid: #5a8835;
  --cream: #fffdf5;
  --cream-2: #f3f0e4;
  --border-gold: #b8862c;
  --border-soft: #b8a86c;
  --ink: #3a3320;
  --water: #3aa6ff;
}

body {
  margin: 0;
  min-height: 100vh;
  padding: 22px 12px calc(22px + env(safe-area-inset-bottom));
  display: flex;
  justify-content: center;
  align-items: flex-start;
  background: linear-gradient(180deg, var(--cream-2) 0%, #e7e0c8 100%);
  font-family: Georgia, 'Times New Roman', serif;
  color: var(--ink);
}

.card {
  width: 100%;
  max-width: 720px;
  background: var(--cream);
  border: 1.5px solid var(--border-gold);
  border-radius: 14px;
  padding: 22px 24px 26px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
}

.head { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; }
h1 { margin: 0 0 6px; color: var(--green); font-size: 27px; }
.hint { margin: 0 0 18px; color: #8a7a4a; font-size: 14px; line-height: 1.5; }

.sound-btn {
  flex: none;
  font-size: 20px;
  width: 42px; height: 42px;
  border: 1.5px solid var(--border-soft);
  border-radius: 10px;
  background: #fff; cursor: pointer;
  transition: border-color .15s, background .15s;
}
.sound-btn:hover { border-color: var(--green-mid); background: var(--cream-2); }

/* ---------- Настройки ---------- */
.control { margin-bottom: 16px; }
.control-label { display: block; font-weight: 700; margin-bottom: 8px; color: var(--green); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }

.chip {
  font-family: inherit; font-size: 15px;
  padding: 8px 14px;
  border: 1.5px solid var(--border-soft);
  border-radius: 999px;
  background: #fff; color: #5a4a20;
  cursor: pointer; user-select: none;
  transition: background .15s, border-color .15s, color .15s;
}
.chip:hover { border-color: var(--green-mid); }
.chip[aria-checked="true"] { background: #c8d8a4; border-color: var(--green-mid); color: var(--green-dark); font-weight: 700; }
.chip:focus-visible { outline: 3px solid var(--green-mid); outline-offset: 2px; }
.chip-ico { font-size: 18px; margin-right: 4px; }

.setup-info {
  margin: 6px 0 14px; padding: 12px 14px;
  background: var(--cream-2); border: 1.5px solid var(--border-soft);
  border-radius: 10px; font-size: 14px; line-height: 1.55; color: #5a4a20;
}
.setup-info b { color: var(--green); }

.btn-start {
  margin-top: 6px; width: 100%;
  font-family: inherit; font-size: 17px; font-weight: 700;
  padding: 13px;
  border: 1.5px solid var(--green-mid); border-radius: 10px;
  background: #c8d8a4; color: var(--green-dark);
  cursor: pointer; transition: background .15s;
}
.btn-start:hover { background: #d6e4b6; }
.btn-start:focus-visible { outline: 3px solid var(--green-mid); outline-offset: 2px; }

.btn-secondary {
  font-family: inherit; font-size: 15px;
  padding: 10px 18px;
  border: 1.5px solid var(--border-soft); border-radius: 10px;
  background: #fff; color: var(--green-dark);
  cursor: pointer; transition: border-color .15s, background .15s;
}
.btn-secondary:hover { border-color: var(--green-mid); background: var(--cream-2); }
.btn-secondary:focus-visible { outline: 3px solid var(--green-mid); outline-offset: 2px; }

/* ---------- Карта уровней ---------- */
.map-sub { color: #8a7a4a; font-size: 14px; margin: 0 0 14px; }
.level-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); gap: 10px; margin-bottom: 16px; }
.level-btn {
  aspect-ratio: 1 / 1;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px;
  border: 1.5px solid var(--border-soft); border-radius: 12px;
  background: #fff; cursor: pointer; font-family: inherit;
  transition: border-color .15s, background .15s, transform .1s;
}
.level-btn:hover { border-color: var(--green-mid); transform: translateY(-2px); }
.level-btn.done { background: #eaf3d9; border-color: var(--green-mid); }
.level-btn.locked { background: #f0ece0; cursor: not-allowed; opacity: .7; }
.lv-num { font-size: 20px; font-weight: 700; color: var(--green-dark); }
.lv-stars { font-size: 13px; color: #d9a521; letter-spacing: 1px; }
.lv-lock { font-size: 22px; }

/* ---------- Игра: статистика ---------- */
.stats { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; align-items: center; margin-bottom: 14px; }
.stat {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--cream-2); border: 1.5px solid var(--border-soft);
  border-radius: 10px; padding: 7px 13px; font-size: 15px; color: var(--green-dark);
}
.stat-ico { font-size: 18px; }
.stat-val { font-weight: 700; }

/* ---------- Раскладка поля + боковая панель ---------- */
.play-wrap { display: flex; gap: 16px; align-items: flex-start; flex-wrap: wrap; justify-content: center; }
.board-col { flex: 1 1 340px; min-width: 260px; max-width: 520px; }

.progress { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.progress-track {
  flex: 1; height: 12px; border-radius: 999px;
  background: #e3ddc7; border: 1.5px solid var(--border-soft); overflow: hidden;
}
.progress-track i {
  display: block; height: 100%; width: 0%;
  background: var(--water); border-radius: 999px;
  transition: width .35s ease, background .3s;
}
.progress span { font-size: 13px; color: var(--green-dark); font-weight: 700; white-space: nowrap; }

.board {
  display: grid;
  grid-template-columns: repeat(var(--cols), 1fr);
  grid-template-rows: repeat(var(--rows), 1fr);
  gap: 0;
  width: 100%;
  aspect-ratio: var(--cols) / var(--rows);
  margin: 0 auto 16px;
  background: #2e5b2a;
  border: 4px solid #1f3d1c;
  border-radius: 14px;
  overflow: hidden;
  touch-action: manipulation;
}

.tile {
  position: relative;
  container-type: size;   /* чтобы иконки масштабировались от размера клетки */
  padding: 0; margin: 0;
  border: none;
  background: rgba(255,255,255,0.02);
  box-shadow: inset 0 0 0 0.5px rgba(255,255,255,0.06);
  cursor: pointer;
  font-family: inherit;
  -webkit-tap-highlight-color: transparent;
}
.tile:disabled { cursor: default; }
.tile.slot { cursor: pointer; }
.tile.slot:hover { background: rgba(255,255,255,0.08); }
.tile.blocked { background: rgba(0,0,0,0.28); cursor: default; }
.tile.blocked .rock { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-size: 55cqmin; opacity: .55; }

.pipe-svg {
  position: absolute; inset: 6%;
  width: 88%; height: 88%;
  transition: transform .24s cubic-bezier(.3,.8,.4,1.2);
  overflow: visible;
}
.tile.has-pipe:not(.is-source):not(.is-drain):hover .pipe-svg { filter: brightness(1.08); }

/* корпус трубы: тёмный кант + светлый корпус + блик = объёмная «металлическая» труба */
.rim   { stroke: #5f6771; stroke-linecap: round; stroke-width: 42; fill: none; }
.body  { stroke: #c4ccd4; stroke-linecap: round; stroke-width: 33; fill: none; }
.g-sheen { pointer-events: none; }
.g-sheen .sheen { stroke: #ffffff; stroke-linecap: round; stroke-width: 8; fill: none; opacity: .45; }
.hub-rim   { fill: #5f6771; }
.hub-body  { fill: #c4ccd4; }
.hub-sheen { fill: #ffffff; opacity: .28; }

/* вода */
.g-water { opacity: 0; transition: opacity .2s ease; }
.g-water .water { stroke: var(--water); stroke-linecap: round; stroke-width: 25; fill: none; }
.g-water circle.water { stroke: none; fill: var(--water); }
/* «течение»: бегущая рябь по уже заполненным трубам */
.g-water.wet line.water { stroke-dasharray: 14 10 !important; animation: march 0.8s linear infinite; }
@keyframes march { to { stroke-dashoffset: -24; } }

/* иконки источника/цели */
.endpt {
  position: absolute; z-index: 2;
  font-size: 40cqmin;
  line-height: 1;
  pointer-events: none;
  filter: drop-shadow(0 1px 2px rgba(0,0,0,.5));
}
.endpt-src { left: 2%; top: 50%; transform: translateY(-50%); }
.endpt-dst { right: 2%; top: 50%; transform: translateY(-50%); transition: transform .3s; }
.endpt-dst.happy { animation: pop .5s ease; }
@keyframes pop { 0%{transform:translateY(-50%) scale(1);} 40%{transform:translateY(-50%) scale(1.4);} 100%{transform:translateY(-50%) scale(1.1);} }

.tile.hint-flash { animation: hintflash .5s ease 2; z-index: 3; }
@keyframes hintflash { 0%,100%{box-shadow: inset 0 0 0 0 rgba(255,220,120,0);} 50%{box-shadow: inset 0 0 0 3px #ffe08a, 0 0 8px 2px rgba(255,215,120,.8);} }

.board.solved { animation: boardglow .8s ease; }
@keyframes boardglow { 0%,100%{box-shadow:none;} 50%{box-shadow:0 0 0 4px rgba(255,220,120,.6), 0 0 22px 4px rgba(255,210,90,.5);} }

/* ---------- Боковая панель: лоток / очередь ---------- */
.side { flex: 1 1 200px; min-width: 180px; max-width: 260px; }
.tray-title { font-weight: 700; color: var(--green); margin-bottom: 8px; }
.tray-items { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.tray-chip {
  position: relative;
  width: 66px; height: 66px;
  border: 1.5px solid var(--border-soft); border-radius: 12px;
  background: #eef1e6; cursor: pointer; padding: 4px;
  transition: border-color .15s, transform .1s, background .15s;
}
.tray-chip:hover { border-color: var(--green-mid); }
.tray-chip.picked { border-color: var(--green-mid); background: #d6e4b6; transform: translateY(-2px); box-shadow: 0 3px 8px rgba(0,0,0,.15); }
.tray-chip .pipe-svg { position: absolute; inset: 8px 8px 20px; width: auto; height: auto; }
.tray-chip .rim { stroke-width: 40; } .tray-chip .body { stroke-width: 30; }
.tray-count { position: absolute; right: 5px; top: 3px; font-size: 13px; font-weight: 700; color: var(--green-dark); }
.tray-label { position: absolute; left: 0; right: 0; bottom: 3px; text-align: center; font-size: 11px; color: #6a5a30; }

.queue-items { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.queue-chip {
  position: relative; width: 56px; height: 56px;
  border: 1.5px solid var(--border-soft); border-radius: 10px;
  background: #eef1e6; padding: 4px; opacity: .8;
}
.queue-chip.next { border-color: var(--green-mid); background: #d6e4b6; opacity: 1; box-shadow: 0 2px 6px rgba(0,0,0,.15); }
.queue-chip .pipe-svg { position: absolute; inset: 6px; width: auto; height: auto; }
.queue-chip .rim { stroke-width: 40; } .queue-chip .body { stroke-width: 30; }

.tray-hint { font-size: 13px; color: #8a7a4a; line-height: 1.5; margin-top: 6px; }

.tray-chip, .queue-chip.next { touch-action: none; }

/* «призрак» детали под пальцем/курсором */
.drag-ghost {
  position: fixed; z-index: 40; width: 56px; height: 56px;
  margin: -28px 0 0 -28px; pointer-events: none;
  border-radius: 12px; background: rgba(214,228,182,.9);
  border: 1.5px solid var(--green-mid); box-shadow: 0 6px 16px rgba(0,0,0,.25);
  opacity: .95;
}
.drag-ghost .pipe-svg { position: absolute; inset: 6px; width: auto; height: auto; }
.drag-ghost .rim { stroke-width: 40; } .drag-ghost .body { stroke-width: 30; }

.tile.drop-ok { background: rgba(255,220,120,.28); box-shadow: inset 0 0 0 3px #ffe08a; }

/* ---------- Кнопки игры ---------- */
.game-actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; margin-top: 4px; }
.game-actions .btn-start { width: auto; margin-top: 0; padding: 10px 18px; }

/* ---------- Победа ---------- */
.win-overlay {
  position: fixed; inset: 0;
  background: rgba(58, 51, 32, 0.55);
  display: flex; align-items: center; justify-content: center;
  padding: 20px; z-index: 20; animation: fadein .25s ease;
}
@keyframes fadein { from { opacity: 0; } to { opacity: 1; } }
.win-box {
  background: var(--cream); border: 1.5px solid var(--border-gold);
  border-radius: 16px; padding: 26px 30px; max-width: 380px; width: 100%;
  text-align: center; box-shadow: 0 12px 40px rgba(0,0,0,.25);
  animation: rise .3s ease;
}
@keyframes rise { from { transform: translateY(20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
.win-emoji { font-size: 54px; animation: bounce 1s ease infinite; }
@keyframes bounce { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-10px); } }
.win-box h2 { margin: 8px 0 6px; color: var(--green); font-size: 24px; }
.win-text { margin: 0 0 18px; color: #6a5a30; font-size: 16px; line-height: 1.5; }
.win-text b { color: var(--green); }
.win-stars { font-size: 34px; color: #f0b429; letter-spacing: 4px; margin-bottom: 6px; }
.win-actions { display: flex; flex-direction: column; gap: 10px; }
.win-actions .btn-start, .win-actions .btn-secondary { width: 100%; }

/* конфетти */
.confetti { position: fixed; inset: 0; pointer-events: none; z-index: 30; overflow: hidden; }
.confetti i {
  position: absolute; top: -12px; width: 9px; height: 14px; border-radius: 2px;
  animation: fall linear forwards;
}
@keyframes fall { to { transform: translateY(105vh) rotate(540deg); opacity: .2; } }

@media (prefers-reduced-motion: reduce) {
  .pipe-svg, .g-water { transition: none; }
  .g-water.flow .water { animation: none; }
  .board.solved, .win-emoji, .win-overlay, .win-box, .confetti i, .endpt-dst.happy, .tile.hint-flash { animation: none; }
}

@media (max-width: 560px) {
  .card { padding: 16px 12px 20px; }
  h1 { font-size: 22px; }
  .side { max-width: 100%; }
  .board-col { max-width: 100%; }
}
