* { box-sizing: border-box; }
html, body {
  margin: 0; min-height: 100%;
  font-family: "Trebuchet MS", "Segoe UI", sans-serif;
  color: #fff;
  background: radial-gradient(ellipse at top, #2a8fc4 0%, #0d4f7a 55%, #072c47 100%) fixed;
}
button {
  font: inherit; cursor: pointer; border: none; border-radius: 10px;
  padding: 8px 14px; background: #ffffff22; color: #fff;
  box-shadow: inset 0 -3px 0 #0003; transition: transform .08s, background .15s;
}
button:hover:not(:disabled) { background: #ffffff38; }
button:active:not(:disabled) { transform: translateY(1px); }
button.primary { background: #3fae49; }
button.primary:hover { background: #4cc457; }
button.ghost { background: transparent; opacity: .7; }
button.selected { background: #ffcc33; color: #222; }
kbd { background: #ffffff25; border-radius: 4px; padding: 0 5px; font-size: .9em; }

#app { max-width: 1060px; margin: 0 auto; padding: 12px; }
.hidden { display: none !important; }

/* ---------- Menu ---------- */
.title-wrap { text-align: center; }
#titleArt { width: min(600px, 100%); height: auto; display: block; margin: 0 auto -10px; }
h1 {
  font-size: 54px; margin: 0; letter-spacing: 1px;
  color: #ffe9a8; text-shadow: 0 4px 0 #8a5a12, 0 8px 18px #0008;
}
h1 span { color: #9cf58a; font-size: .6em; text-shadow: 0 3px 0 #2e6b1f; }
.subtitle { margin: 6px 0 18px; opacity: .9; }
#levelGrid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 12px; }
.level-btn {
  position: relative; text-align: left; padding: 12px; border-radius: 14px; min-height: 120px;
  display: flex; flex-direction: column; gap: 4px;
  box-shadow: inset 0 -4px 0 #0004, 0 4px 10px #0004;
}
.level-btn.seaside { background: linear-gradient(160deg, #f6d48f, #d9a556); color: #4a2f08; }
.level-btn.underwater { background: linear-gradient(160deg, #2fa6c9, #145b8a); color: #eaf8ff; }
.level-btn.seaside:hover:not(:disabled) { background: linear-gradient(160deg, #ffe0a0, #e8b464); }
.level-btn.underwater:hover:not(:disabled) { background: linear-gradient(160deg, #3fbbe0, #1a6ea5); }
.level-btn.night { background: linear-gradient(160deg, #3b3f7a, #1b1f45); color: #e8e6ff; }
.level-btn.trench { background: linear-gradient(160deg, #1d3340, #070f16); color: #b9fff7; box-shadow: inset 0 -4px 0 #0006, 0 0 10px #3ff5ff33; }
.level-btn.night:hover:not(:disabled) { background: linear-gradient(160deg, #4a4f94, #262b5c); }
.level-btn.trench:hover:not(:disabled) { background: linear-gradient(160deg, #26465a, #0c1a24); }
.night .lv-check, .trench .lv-check { color: #9cf58a; }
.world-title {
  grid-column: 1 / -1; margin: 10px 0 -2px; font-weight: bold; font-size: 17px;
  color: #ffe9a8; text-shadow: 0 2px 0 #0006; border-bottom: 2px solid #ffffff22; padding-bottom: 4px;
}
.level-btn.locked { filter: grayscale(.85) brightness(.7); cursor: not-allowed; }
.lv-num { font-size: 26px; font-weight: bold; }
.lv-title { font-weight: bold; font-size: 15px; }
.lv-meta { font-size: 12px; opacity: .85; }
.lv-meta.small { font-size: 11px; opacity: .75; }
.lv-check { position: absolute; top: 8px; right: 10px; font-size: 20px; color: #2e9a3a; text-shadow: 0 1px 0 #fff8; }
.underwater .lv-check { color: #9cf58a; }
.menu-buttons { display: flex; justify-content: center; gap: 12px; margin: 18px 0; }
.howto { background: #00000033; border-radius: 14px; padding: 6px 18px 10px; }
.howto h3 { margin: 8px 0 4px; }
.howto li { margin: 4px 0; }

/* ---------- Almanac ---------- */
.almanac-head { display: flex; align-items: center; gap: 16px; }
.almanac-head h2 { margin: 0; }
.entries { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.entry { display: flex; gap: 12px; align-items: center; background: #00000033; border-radius: 12px; padding: 8px 12px; }
.entry canvas { flex-shrink: 0; background: #ffffff14; border-radius: 10px; }
.entry h4 { margin: 0 0 4px; color: #ffe9a8; }
.entry p { margin: 2px 0; font-size: 13px; opacity: .9; }
.entry.unknown h4 { color: #aaa; }
.cost-line { font-size: 12px !important; color: #bfe8ff; }

/* ---------- Game ---------- */
#topbar {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px;
  background: linear-gradient(#6b4423, #4a2e16); border-radius: 14px 14px 0 0;
  border: 3px solid #3a230f; border-bottom: none;
}
#pearlBox {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  min-width: 70px; height: 78px; background: #f3e3c3; border-radius: 10px; color: #3a230f;
  font-weight: bold; font-size: 20px; box-shadow: inset 0 -3px 0 #0003;
}
#cards { display: flex; gap: 6px; flex: 1; overflow-x: auto; }
.card {
  position: relative; width: 74px; height: 76px; flex-shrink: 0; cursor: pointer; user-select: none;
  background: linear-gradient(#e9f7d8, #b9dc9a); border: 3px solid #7a9a5a; border-radius: 9px; overflow: hidden;
  transition: transform .1s;
}
.card canvas { display: block; margin: 0 auto; }
.card .cost {
  position: absolute; bottom: 0; left: 0; right: 0; text-align: center;
  background: #fffbe8; color: #3a230f; font-weight: bold; font-size: 14px; line-height: 18px;
}
.card .cd { position: absolute; top: 0; left: 0; right: 0; height: 0; background: #0008; pointer-events: none; }
.card .key { position: absolute; top: 2px; left: 4px; font-size: 11px; color: #3a5a1a; font-weight: bold; }
.card.poor { filter: grayscale(.7) brightness(.75); }
.card.selected { transform: translateY(-4px); border-color: #ffcc33; box-shadow: 0 0 12px #ffcc33; }
.card:hover:not(.poor) { transform: translateY(-2px); }
.tool { width: 56px; height: 56px; padding: 0; display: flex; align-items: center; justify-content: center; background: #00000040; }
.controls { display: flex; gap: 6px; }
.controls button { min-width: 42px; height: 40px; padding: 0 8px; }

#stage { position: relative; border: 3px solid #3a230f; border-top: none; border-radius: 0 0 14px 14px; overflow: hidden; }
#game { display: block; width: 100%; height: auto; touch-action: none; }
#stage.placing #game { cursor: crosshair; }

#overlay {
  position: absolute; inset: 0; background: #0009; display: flex; align-items: center; justify-content: center;
}
.panel {
  background: linear-gradient(#fff6dc, #f0dcae); color: #3a230f; border-radius: 18px; padding: 22px 30px;
  max-width: 560px; text-align: center; border: 4px solid #8a5a12; box-shadow: 0 10px 40px #000a;
}
.panel h2 { margin: 0 0 10px; }
.btn-row { display: flex; gap: 10px; justify-content: center; margin-top: 16px; }
.panel button { background: #8a5a12; }
.panel button.primary { background: #3fae49; }
.reward { display: flex; gap: 14px; align-items: center; text-align: left; background: #ffffff80; border-radius: 12px; padding: 10px; }
.reward-title { font-weight: bold; font-size: 17px; }
.reward p { margin: 4px 0; font-size: 14px; }
.reward .cost-line { color: #2a6f8a; }

/* turtle picker */
.pick-info { font-size: 14px; margin: 4px 0 10px; }
.pick-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; justify-items: center; }
.card.pick { width: 100px; height: 96px; opacity: .55; filter: grayscale(.6); }
.card.pick.chosen { opacity: 1; filter: none; border-color: #ffcc33; box-shadow: 0 0 10px #ffcc33aa; }
.card.pick .cost { bottom: 16px; }
.pick-name { position: absolute; bottom: 0; left: 0; right: 0; font-size: 10px; line-height: 16px; background: #3a230f; color: #fff; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 0 3px; }
.pick-count { margin: 8px 0 0; font-weight: bold; }
.world-unlock { margin-top: 10px; padding: 6px 10px; background: #3b3f7a; color: #fff; border-radius: 8px; }

@media (max-width: 1000px) {
  #topbar { flex-wrap: wrap; }
  #cards { order: 3; flex-basis: 100%; flex-wrap: wrap; }
}
@media (max-width: 800px) {
  #levelGrid { grid-template-columns: repeat(2, 1fr); }
  .entries { grid-template-columns: 1fr; }
  h1 { font-size: 38px; }
}

/* ---------- Phones ---------- */
body { touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
.panel { max-height: calc(100% - 16px); overflow-y: auto; }

/* Landscape phones: side columns for cards/controls so the field uses the full height */
@media (orientation: landscape) and (max-height: 540px) {
  #gameScreen:not(.hidden) {
    position: fixed; inset: 0; z-index: 5;
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    grid-template-rows: auto minmax(0, 1fr);
    grid-template-areas: "pearl stage shovel" "cards stage ctrls";
    gap: 4px;
    padding: max(4px, env(safe-area-inset-top)) max(4px, env(safe-area-inset-right)) max(4px, env(safe-area-inset-bottom)) max(4px, env(safe-area-inset-left));
    background: linear-gradient(90deg, #4a2e16, #6b4423 12%, #3a230f 50%, #6b4423 88%, #4a2e16);
    height: 100vh; height: 100dvh;
  }
  #topbar { display: contents; }
  #pearlBox { grid-area: pearl; height: 44px; min-width: 0; flex-direction: row; gap: 4px; font-size: 17px; }
  #pearlIcon { width: 26px; height: 26px; }
  #cards {
    grid-area: cards; display: grid; grid-template-columns: repeat(2, 58px); grid-auto-rows: max-content;
    gap: 4px; overflow-y: auto; flex: none; order: 0; flex-basis: auto; align-content: start;
    height: 100%; max-height: 100%;
  }
  #cards .card { width: 58px; height: auto; max-height: 84px; border-width: 2px; }
  #cards .card canvas { width: 100%; height: auto; max-height: calc(100% - 15px); object-fit: contain; }
  #cards .card .cost { font-size: 12px; line-height: 15px; }
  #cards .card .key { display: none; }
  #shovelBtn { grid-area: shovel; width: 46px; height: 44px; }
  .controls { grid-area: ctrls; flex-direction: column; align-self: start; gap: 5px; }
  .controls button { min-width: 46px; height: 40px; padding: 0; }
  #stage {
    grid-area: stage; border: none; border-radius: 8px; min-height: 0; min-width: 0; height: 100%;
    display: flex; align-items: center; justify-content: center; background: #0006;
  }
  #game { width: auto; height: auto; max-width: 100%; max-height: 100%; border-radius: 6px; }
  .panel { padding: 12px 16px; border-width: 3px; max-width: 92%; }
  .panel h2 { font-size: 20px; margin-bottom: 6px; }
  .panel p { font-size: 13px; margin: 4px 0; }
  .btn-row { margin-top: 10px; }
  #overlay { position: fixed; z-index: 10; }
  .pick-grid { grid-template-columns: repeat(auto-fill, minmax(70px, 1fr)); gap: 5px; }
  .card.pick { width: 70px; height: 74px; }
  .card.pick canvas { width: 66px; height: auto; }
  .pick-info { font-size: 12px !important; }
  /* menu tweaks for short screens */
  h1 { font-size: 34px; }
  #titleArt { width: 360px; }
  .subtitle { margin: 2px 0 10px; font-size: 13px; }
  .level-btn { min-height: 92px; padding: 8px; }
  .lv-num { font-size: 20px; }
}

/* Portrait phones: advise rotating to landscape */
#rotateHint { display: none; }
@media (orientation: portrait) and (max-width: 600px) {
  body:not(.rotate-dismissed) #rotateHint {
    display: flex; position: fixed; inset: 0; z-index: 50;
    flex-direction: column; align-items: center; justify-content: center; gap: 14px;
    padding: 24px; text-align: center;
    background: radial-gradient(ellipse at top, #2a8fc4 0%, #0d4f7a 55%, #072c47 100%);
  }
}
#rotateHint h2 { margin: 0; color: #ffe9a8; text-shadow: 0 3px 0 #8a5a12; font-size: 26px; }
#rotateHint p { margin: 0; max-width: 300px; line-height: 1.4; opacity: .95; }
.rotate-phone {
  width: 70px; height: 120px; border: 5px solid #fff; border-radius: 14px; position: relative;
  animation: rotatePhone 2.4s ease-in-out infinite; margin: 10px 0 20px;
}
.rotate-phone::after { content: '🐢'; position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-size: 30px; }
.rotate-phone::before { content: ''; position: absolute; bottom: 6px; left: 50%; width: 16px; height: 4px; margin-left: -8px; border-radius: 2px; background: #fff; }
@keyframes rotatePhone {
  0%, 20% { transform: rotate(0deg); }
  50%, 80% { transform: rotate(-90deg); }
  100% { transform: rotate(0deg); }
}
