/* ===========================================================
   Пиратската карта — styles
   Virtual layout: 1280x720 landscape, fluid scaling, centered.
   =========================================================== */

:root{
  --sand:        #f0d29c;
  --sand-dark:   #c99a56;
  --sea-top:     #7fd4d9;
  --sea-bottom:  #2f9bb0;
  --parchment:   #f3dfae;
  --ink:         #3a2415;
  --gold:        #ffce54;
  --danger:      #d9534f;
  --ok:          #4caf7d;
  --panel-radius: 18px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

*{ box-sizing: border-box; }

html, body{
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background: radial-gradient(ellipse at top, var(--sea-top), var(--sea-bottom) 75%);
  font-family: -apple-system, "Segoe UI Rounded", "Segoe UI", system-ui, "Trebuchet MS", sans-serif;
  color: var(--ink);
  -webkit-text-size-adjust: 100%;
}

img{ -webkit-user-drag: none; user-select: none; pointer-events: none; }

#app{
  position: relative;
  height: 100dvh;
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
}

/* Virtual resolution: 1280x720 (16:9) landscape, scaled to fit either
   the available width or height, whichever is the tighter constraint.
   Width is the single driving dimension; height always follows it via
   aspect-ratio so the box never drifts off a true 16:9 (e.g. once the
   max-width cap binds on very large/wide screens). */
.stage{
  position: relative;
  width: min(100vw - 16px, calc((100dvh - 16px) * 16 / 9), 1040px);
  aspect-ratio: 16 / 9;
  background: var(--sand) url('assets/background/background.png') center / cover no-repeat;
  border-radius: 22px;
  box-shadow: 0 10px 40px rgba(0,0,0,.35);
  overflow: hidden;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

/* ---------------- Screens ---------------- */

.screen{
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  padding:
    calc(10px + var(--safe-top))
    calc(14px + var(--safe-right))
    calc(10px + var(--safe-bottom))
    calc(14px + var(--safe-left));
  opacity: 0;
  transition: opacity .25s ease;
}

.screen.active{
  display: flex;
  opacity: 1;
}

.game-title{
  margin: 0 0 6px;
  font-size: clamp(18px, 4.5vh, 30px);
  font-weight: 800;
  text-align: center;
  color: #5c3418;
  text-shadow: 0 2px 0 #fff6df, 0 4px 6px rgba(0,0,0,.25);
  letter-spacing: .5px;
}

/* ---------------- Loading ---------------- */

#screen-loading{ justify-content: center; }

.loader{
  width: 50%;
  max-width: 260px;
  height: 16px;
  background: rgba(255,255,255,.5);
  border: 2px solid var(--sand-dark);
  border-radius: 10px;
  overflow: hidden;
  margin-top: 18px;
}
.loader-bar{
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--gold), #ffb347);
  transition: width .2s ease;
}
.loader-text{
  margin-top: 10px;
  font-size: 14px;
  font-weight: 600;
  opacity: .85;
}

/* ---------------- Parchment panel ---------------- */

.parchment-panel{
  position: relative;
  width: 100%;
  height: 100%;
  max-width: 900px;
  margin: auto;
  padding: 16px 26px;
  display: flex;
  flex-direction: row;
  align-items: center;
  /* If content is ever too tall for a severely letterboxed viewport
     (e.g. a phone still held in portrait), scroll instead of clipping
     it off. "safe center" keeps the scrolled-to start reachable —
     plain "center" is kept first as a fallback for engines that don't
     understand "safe center" yet. */
  align-items: safe center;
  justify-content: center;
  gap: clamp(10px, 3vw, 34px);
  overflow-y: auto;
  background-color: var(--parchment);
  border: 10px solid #8a5a2b;
  border-image: url('assets/ui/panel_parchment.png') 40 fill stretch;
  border-radius: var(--panel-radius);
  box-shadow: 0 8px 22px rgba(0,0,0,.25);
}

/* ---------------- Start screen (two columns) ---------------- */

/* Smaller than the screen so the corner decorations (ship, starfish,
   palm, shell) sit fully outside it in the surrounding teal margin,
   instead of overlapping its edge. */
.start-panel{
  text-align: center;
  width: min(88%, 820px);
  height: 82%;
  background-color: transparent;
  border: none;
  border-image: none;
  box-shadow: none;
}

.start-col{
  display: flex;
  flex-direction: column;
  align-items: center;
}
/* Only the image column is allowed to shrink to 0 in extreme layouts —
   the text column keeps its natural min-content width (at least the
   play button's floor) so it can never collapse to an unreadable
   sliver; the panel scrolls instead if space still runs out. */
.start-col-left{ flex: 0 1 auto; min-width: 0; }
.start-col-right{ flex: 1 1 auto; max-width: 420px; }

.title-sign{
  width: auto;
  height: clamp(50px, 12vh, 92px);
  max-width: 100%;
  object-fit: contain;
  filter: drop-shadow(0 4px 6px rgba(0,0,0,.3));
  margin: 0 0 6px;
}

.pirate-portrait{
  width: auto;
  height: clamp(90px, 40vh, 220px);
  max-width: 32vw;
  object-fit: contain;
  filter: drop-shadow(0 6px 8px rgba(0,0,0,.25));
  margin: 4px 0 6px;
}
.pirate-portrait.small{ height: clamp(70px, 26vh, 130px); }
.pirate-portrait.tall{ height: clamp(110px, 34vh, 190px); margin: 0 0 4px; }

.win-banner{
  width: auto;
  height: clamp(30px, 8vh, 46px);
  object-fit: contain;
  filter: drop-shadow(0 3px 4px rgba(0,0,0,.3));
  margin: 0 0 4px;
}

.prompt-text{
  font-size: clamp(14px, 3.4vh, 19px);
  font-weight: 700;
  margin: 2px 0 8px;
}

.clue-strip{
  width: 100%;
  max-width: 320px;
  height: auto;
  margin: 2px 0 10px;
  filter: drop-shadow(0 3px 4px rgba(0,0,0,.2));
}

.instruction-text{
  font-size: clamp(11px, 2.4vh, 14px);
  opacity: .8;
  margin: 0 0 12px;
  max-width: 320px;
}

.img-button{
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform .12s ease;
}
.img-button img{
  width: clamp(140px, 16vw, 180px);
  height: auto;
  display: block;
  filter: drop-shadow(0 5px 6px rgba(0,0,0,.3));
}
.img-button:active{ transform: scale(.93); }
@media (hover:hover){
  .img-button:hover img{ filter: drop-shadow(0 5px 6px rgba(0,0,0,.3)) brightness(1.06); }
}

.deco{ position: absolute; opacity: .9; pointer-events: none; }
.deco-ship{ width: 8.4%; max-width: 70px; top: 3%; right: 3%; transform: rotate(-6deg); opacity: .85; }
.deco-shell{ width: 8.4%; max-width: 67px; bottom: 4%; left: 2%; }
.deco-starfish{ width: 7.2%; max-width: 58px; bottom: 3%; right: 3%; }
.deco-plant{ width: 7.2%; max-width: 60px; top: 5%; left: 2%; }

/* ---------------- Playing screen ---------------- */

#screen-playing{ padding-top: calc(8px + var(--safe-top)); }

.playing-layout{
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: row;
  align-items: stretch;
  gap: clamp(8px, 1.6vw, 20px);
}

.side-panel{
  flex: 0 0 auto;
  width: clamp(173px, 31.7vw, 302px);
  height: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 10px;
  padding-top: 4px;
  padding-bottom: 4px;
  overflow-y: auto;
}
.attempts-icon{
  width: 100%;
  height: auto;
  max-height: 28.8vh;
  object-fit: contain;
  filter: drop-shadow(0 3px 4px rgba(0,0,0,.25));
}
.mini-clue{
  width: 100%;
  height: auto;
  border-radius: 8px;
  box-shadow: 0 2px 6px rgba(0,0,0,.2);
}

.message-banner{
  width: 100%;
  min-height: 2.6em;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(17px, 3.7vh, 23px);
  font-weight: 700;
  text-align: center;
  color: #4a2c12;
  background: rgba(255,255,255,.55);
  padding: 6px 10px;
  border-radius: 14px;
  transition: opacity .18s ease;
}

.mascot{
  width: clamp(81px, 13vw, 127px);
  height: clamp(81px, 13vw, 127px);
  pointer-events: none;
}
.mascot img{
  width: 100%;
  height: 100%;
  object-fit: contain;
  filter: drop-shadow(0 3px 5px rgba(0,0,0,.3));
  animation: mascot-bob 2.4s ease-in-out infinite;
}
@keyframes mascot-bob{
  0%,100%{ transform: translateY(0); }
  50%{ transform: translateY(-5px); }
}
.mascot.react img{ animation: mascot-react .5s ease; }
@keyframes mascot-react{
  0%{ transform: scale(1); }
  40%{ transform: scale(1.18) translateY(-4px); }
  100%{ transform: scale(1); }
}

.board-wrap{
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  container-type: size;
  container-name: boardwrap;
}

.board{
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-template-rows: repeat(5, 1fr);
  width: min(100cqw, 100cqh);
  height: min(100cqw, 100cqh);
  aspect-ratio: 1 / 1;
  gap: 3px;
  padding: 6px;
  background: #a97a3f;
  border: 4px solid #7a5327;
  border-radius: 14px;
  box-shadow: inset 0 0 12px rgba(0,0,0,.35), 0 6px 14px rgba(0,0,0,.3);
}
/* Fallback for browsers without size container queries: cap by height,
   which is the tighter constraint in a 16:9 layout. */
@supports not (container-type: size){
  .board{ width: min(100%, 78vh); height: min(100%, 78vh); }
}
.board.shake{ animation: board-shake .5s ease; }
@keyframes board-shake{
  0%,100%{ transform: translateX(0); }
  20%{ transform: translateX(-6px) rotate(-.6deg); }
  40%{ transform: translateX(5px) rotate(.6deg); }
  60%{ transform: translateX(-4px); }
  80%{ transform: translateX(3px); }
}

.cell{
  position: relative;
  border: none;
  padding: 0;
  border-radius: 6px;
  overflow: hidden;
  background: var(--sand);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.cell img{
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.cell .landmark-overlay{
  object-fit: contain;
  padding: 8%;
  filter: drop-shadow(0 2px 3px rgba(0,0,0,.3));
}

.cell.cell-dig{
  box-shadow: inset 0 0 0 2px rgba(255,206,84,.55), 0 0 6px 1px rgba(255,206,84,.4);
  animation: dig-glow 1.8s ease-in-out infinite;
}
@keyframes dig-glow{
  0%,100%{ box-shadow: inset 0 0 0 2px rgba(255,206,84,.55), 0 0 6px 1px rgba(255,206,84,.4); }
  50%{ box-shadow: inset 0 0 0 3px rgba(255,206,84,.95), 0 0 12px 3px rgba(255,206,84,.75); }
}
.cell.cell-dig.opened{ animation: none; }

.cell:active:not(.opened){ filter: brightness(.9); }

.cell.nudge{ animation: cell-nudge .3s ease; }
@keyframes cell-nudge{
  0%,100%{ transform: scale(1); }
  50%{ transform: scale(.95); }
}

.cell.pop{ animation: cell-pop .35s ease; }
@keyframes cell-pop{
  0%{ transform: scale(1); }
  40%{ transform: scale(.82); }
  100%{ transform: scale(1); }
}

.dig-result{
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  padding: 10%;
  opacity: 0;
  transform: scale(.5);
  animation: result-pop .4s cubic-bezier(.34,1.56,.64,1) forwards;
}
@keyframes result-pop{
  to{ opacity: 1; transform: scale(1); }
}

.dust-puff{
  position: absolute;
  inset: 0;
  width: 60%;
  height: 60%;
  margin: auto;
  object-fit: contain;
  opacity: .95;
  animation: dust-rise .6s ease forwards;
  pointer-events: none;
}
@keyframes dust-rise{
  0%{ opacity: .95; transform: translateY(0) scale(.6); }
  100%{ opacity: 0; transform: translateY(-18%) scale(1.3); }
}

.pulse-ring{
  position: absolute;
  inset: 0;
  border-radius: 6px;
  pointer-events: none;
  box-shadow: 0 0 0 0 rgba(255,255,255,.9);
  animation: pulse-ring .8s ease-out 2;
}
@keyframes pulse-ring{
  0%{ box-shadow: 0 0 0 0 rgba(255,255,255,.8); }
  100%{ box-shadow: 0 0 0 18px rgba(255,255,255,0); }
}

.treasure-cell-glow{
  position: absolute;
  inset: -30%;
  width: 160%;
  height: 160%;
  object-fit: contain;
  opacity: 0;
  animation: treasure-teach 1.4s ease-in-out;
  pointer-events: none;
}
@keyframes treasure-teach{
  0%{ opacity: 0; transform: scale(.6); }
  35%{ opacity: 1; transform: scale(1.05); }
  70%{ opacity: 1; transform: scale(1); }
  100%{ opacity: 0; transform: scale(1.1); }
}

/* ---------------- Result screens (two columns) ---------------- */

.result-panel{ text-align: center; }

/* Victory and failure result panels — fully transparent and borderless,
   matching the start screen treatment. */
#screen-victory .parchment-panel,
#screen-failure .parchment-panel{
  background-color: transparent;
  border: none;
  border-image: none;
  box-shadow: none;
}

.result-col{
  display: flex;
  flex-direction: column;
  align-items: center;
}
.result-col-left{ flex: 0 1 auto; min-width: 0; }
.result-col-right{ flex: 1 1 auto; max-width: 380px; }

.treasure-reveal{
  position: relative;
  width: clamp(140px, 26vh, 230px);
  aspect-ratio: 1/1;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 4px;
}
.treasure-glow{
  position: absolute;
  inset: -20%;
  width: 140%;
  height: 140%;
  object-fit: contain;
  animation: glow-pulse 2.2s ease-in-out infinite;
}
@keyframes glow-pulse{
  0%,100%{ opacity: .7; transform: scale(1) rotate(0deg); }
  50%{ opacity: 1; transform: scale(1.08) rotate(6deg); }
}
.treasure-chest{
  position: relative;
  width: 78%;
  height: auto;
  filter: drop-shadow(0 8px 10px rgba(0,0,0,.3));
  animation: chest-bounce .7s cubic-bezier(.34,1.56,.64,1);
}
@keyframes chest-bounce{
  0%{ transform: scale(.3) translateY(30px); opacity: 0; }
  60%{ transform: scale(1.1) translateY(-6px); opacity: 1; }
  100%{ transform: scale(1) translateY(0); }
}
.sparkles{
  position: absolute;
  width: 34%;
  height: auto;
  animation: sparkle-twinkle 1.4s ease-in-out infinite;
}
.sparkles-a{ top: -4%; left: -6%; animation-delay: 0s; }
.sparkles-b{ bottom: -2%; right: -8%; width: 26%; animation-delay: .5s; }
@keyframes sparkle-twinkle{
  0%,100%{ opacity: .4; transform: scale(.85) rotate(0deg); }
  50%{ opacity: 1; transform: scale(1.15) rotate(15deg); }
}

.stars{
  display: flex;
  gap: 6px;
  margin: 4px 0 2px;
  justify-content: center;
}
.stars img{
  width: clamp(24px, 4vh, 36px);
  height: clamp(24px, 4vh, 36px);
  filter: drop-shadow(0 2px 3px rgba(0,0,0,.3));
  animation: star-in .4s ease backwards;
}
.stars img.dim{
  opacity: .3;
  filter: grayscale(1);
  animation: none;
}
.stars img:nth-child(1){ animation-delay: .05s; }
.stars img:nth-child(2){ animation-delay: .2s; }
.stars img:nth-child(3){ animation-delay: .35s; }
@keyframes star-in{
  0%{ transform: scale(0) rotate(-40deg); opacity: 0; }
  70%{ transform: scale(1.25) rotate(10deg); opacity: 1; }
  100%{ transform: scale(1) rotate(0deg); }
}

.result-title{
  font-size: clamp(15px, 3.6vh, 21px);
  font-weight: 800;
  margin: 6px 0 2px;
}
.result-sub{
  font-size: clamp(12px, 2.8vh, 15px);
  margin: 0 0 8px;
  opacity: .85;
}
.best-line{
  font-size: 12px;
  opacity: .7;
  margin: -2px 0 8px;
  min-height: 1em;
}

/* ---------------- Rotate-device hint (portrait phones) ---------------- */

.rotate-hint{
  display: none;
  position: fixed;
  left: 50%;
  bottom: calc(10px + var(--safe-bottom));
  transform: translateX(-50%);
  margin: 0;
  padding: 6px 14px;
  font-size: 12px;
  font-weight: 600;
  color: #fff;
  background: rgba(0,0,0,.45);
  border-radius: 999px;
  pointer-events: none;
  z-index: 5;
}
@media (orientation: portrait){
  .rotate-hint{ display: block; }
}

.home-btn{
  position: absolute;
  top: 12px;
  left: 12px;
  z-index: 20;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: rgba(58, 36, 21, 0.75);
  border: 2px solid var(--gold);
  color: #fff;
  font-size: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  cursor: pointer;
  box-shadow: 0 4px 10px rgba(0,0,0,.35);
  pointer-events: auto;
}

.home-btn:hover{
  filter: brightness(1.15);
}

@media (min-width: 700px){
  .stage{ border-radius: 26px; }
}
