:root {
  --ink: #4a3b2f;
  --paper: #fffaf0;
  --cream: #fff3e0;
  --green: #5fae5a;
  --green-dark: #3f8a3c;
  --sea: #4fc3e8;
  --coral: #ef8a7a;
  --sun: #ffd166;
  --shadow: 0 4px 0 rgba(74, 59, 47, .25), 0 6px 14px rgba(0, 0, 0, .15);
  --radius: 22px;
  --font: "Trebuchet MS", "Segoe UI", "Helvetica Neue", Arial, system-ui, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: #9fe0f5; color: var(--ink); font-family: var(--font);
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
  overscroll-behavior: none;
}
#game { position: fixed; inset: 0; touch-action: none; }
#game canvas { display: block; width: 100%; height: 100%; touch-action: none; }
.hidden { display: none !important; }

/* ---------- buttons ---------- */
.btn {
  font: inherit; font-weight: 700; font-size: 20px; color: var(--ink);
  min-height: 52px; min-width: 52px; padding: 10px 20px;
  border: 3px solid var(--ink); border-radius: 999px; background: #fff;
  box-shadow: var(--shadow); cursor: pointer; touch-action: manipulation;
  transition: transform .08s ease;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(74,59,47,.25); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--green); color: #fff; }
.btn.danger { background: var(--coral); color: #fff; }
.btn.round { width: 56px; height: 56px; padding: 0; font-size: 26px; line-height: 1; }
.btn.wide { display: block; width: 100%; margin: 10px 0; }
.btn.tiny { font-size: 14px; min-height: 40px; padding: 6px 12px; }
.btn:focus-visible { outline: 4px solid var(--sun); outline-offset: 2px; }

/* ---------- overlays & cards ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 30; display: flex; align-items: center; justify-content: center;
  background: rgba(30, 60, 80, .35); padding: 16px;
}
.card {
  background: var(--paper); border: 4px solid var(--ink); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px 18px; width: min(640px, 100%); max-height: 100%;
  display: flex; flex-direction: column; overflow: hidden;
}
.card.small { width: min(420px, 100%); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 8px; }
.card-head h2 { margin: 0; font-size: 26px; }
.big-emoji { font-size: 56px; text-align: center; }
#loading { background: #9fe0f5; }
#loading p { font-size: 20px; text-align: center; }

.row { display: flex; gap: 10px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.hint { font-size: 16px; opacity: .8; margin: 6px 0 10px; }

/* ---------- start screen ---------- */
#start, #account { position: fixed; inset: 0; z-index: 20; display: flex; pointer-events: none; }
.start-panel {
  pointer-events: auto; background: rgba(255, 250, 240, .96); border: 4px solid var(--ink);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 18px;
  overflow-y: auto; -webkit-overflow-scrolling: touch;
}
@media (orientation: landscape) {
  #start, #account { justify-content: flex-end; align-items: center; padding: 16px; }
  .start-panel { width: min(470px, 52vw); max-height: calc(100vh - 32px); }
}
@media (orientation: portrait) {
  #start, #account { align-items: flex-end; padding: 12px; }
  .start-panel { width: 100%; max-height: 62vh; }
}
.logo {
  margin: 0 0 4px; font-size: 34px; text-align: center; color: #fff; letter-spacing: 1px;
  text-shadow: 0 3px 0 var(--ink), 2px 0 0 var(--ink), -2px 0 0 var(--ink), 0 -2px 0 var(--ink);
}
.step h2 { margin: 6px 0 8px; font-size: 24px; }
.step h3 { margin: 12px 0 6px; font-size: 18px; }
.pick {
  font: inherit; font-weight: 700; font-size: 15px; color: var(--ink);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-width: 70px; min-height: 70px; padding: 6px 8px; background: #fff;
  border: 3px solid #d8c8b4; border-radius: 18px; cursor: pointer;
}
.pick .em { font-size: 30px; line-height: 1; }
.pick[aria-checked="true"] { border-color: var(--ink); background: #fff1c9; box-shadow: 0 0 0 3px var(--sun); }
.swatch {
  width: 52px; height: 52px; border-radius: 50%; border: 3px solid #d8c8b4; cursor: pointer;
}
.swatch[aria-checked="true"] { border-color: var(--ink); box-shadow: 0 0 0 4px var(--sun); }
input[type="text"], input[type="password"] {
  font: inherit; font-size: 22px; font-weight: 700; color: var(--ink);
  width: 100%; min-height: 54px; padding: 8px 14px; border: 3px solid var(--ink);
  border-radius: 16px; background: #fff; -webkit-user-select: text; user-select: text;
}
.step .btn.primary { margin-top: 6px; min-width: 160px; }

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; z-index: 10; pointer-events: none; }
#hud > * { pointer-events: auto; }
#topbar {
  position: absolute; left: 0; right: 0; top: 0; pointer-events: none;
  display: flex; justify-content: space-between; align-items: flex-start; gap: 8px;
  padding: max(10px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 0 max(12px, env(safe-area-inset-left));
}
#topbar > * { pointer-events: auto; }
#island-label {
  background: rgba(255, 250, 240, .92); border: 3px solid var(--ink); border-radius: 18px;
  padding: 6px 12px; font-weight: 700; font-size: 17px; display: flex; flex-direction: column;
  max-width: 50vw; box-shadow: var(--shadow);
}
#hud-island { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#hud-clock { font-size: 14px; opacity: .8; }
.top-buttons { display: flex; gap: 8px; }

#toast {
  position: absolute; left: 50%; top: 84px; transform: translateX(-50%);
  background: #fff; border: 3px solid var(--ink); border-radius: 18px; padding: 10px 18px;
  font-size: 19px; font-weight: 700; box-shadow: var(--shadow); text-align: center;
  max-width: min(90vw, 520px); pointer-events: none; animation: pop .25s ease;
}
#toast.new { background: #fff1c9; }
@keyframes pop { from { transform: translateX(-50%) scale(.7); opacity: 0; } to { transform: translateX(-50%) scale(1); opacity: 1; } }

#bite {
  position: absolute; left: 50%; top: 38%; transform: translate(-50%, -50%);
  font-size: 40px; font-weight: 900; color: #fff; pointer-events: none;
  text-shadow: 0 4px 0 var(--ink), 3px 0 0 var(--ink), -3px 0 0 var(--ink), 0 -3px 0 var(--ink);
  animation: wobble .3s ease infinite alternate;
}
@keyframes wobble { from { transform: translate(-50%, -50%) scale(1); } to { transform: translate(-50%, -50%) scale(1.12); } }

#joystick {
  position: absolute; left: 0; bottom: 0; width: 50%; height: 55%; touch-action: none;
  pointer-events: auto;
}
#stick-base {
  position: absolute; left: max(28px, env(safe-area-inset-left)); bottom: max(28px, env(safe-area-inset-bottom));
  width: 140px; height: 140px; border-radius: 50%;
  background: rgba(255, 255, 255, .35); border: 4px solid rgba(74, 59, 47, .5);
}
#stick-knob {
  position: absolute; left: 50%; top: 50%; width: 64px; height: 64px; margin: -32px 0 0 -32px;
  border-radius: 50%; background: #fff; border: 4px solid var(--ink); box-shadow: var(--shadow);
}
body.no-touch #joystick { display: none; }

#btn-action {
  position: absolute; right: max(24px, env(safe-area-inset-right)); bottom: max(32px, env(safe-area-inset-bottom));
  min-width: 132px; min-height: 96px; border-radius: 48px; font-size: 26px;
  background: var(--sun); padding: 10px 22px;
}
#btn-action.idle { background: #f3eadc; color: #a99a8a; border-color: #a99a8a; }
#btn-action.pulse { animation: pulse .5s ease infinite alternate; background: var(--coral); color: #fff; }
@keyframes pulse { from { transform: scale(1); } to { transform: scale(1.08); } }

#fps {
  position: absolute; left: 8px; top: 92px; background: rgba(0,0,0,.6); color: #fff;
  font: 700 14px monospace; padding: 4px 8px; border-radius: 8px; pointer-events: none;
}
#debug-panel { position: absolute; left: 8px; top: 124px; display: flex; flex-direction: column; gap: 6px; }

/* ---------- grids (pocket & book) ---------- */
.grid {
  display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; overflow-y: auto;
  padding: 4px; -webkit-overflow-scrolling: touch;
}
.slot {
  position: relative; aspect-ratio: 1; min-height: 52px; background: #f6ecd9;
  border: 3px solid #d8c8b4; border-radius: 14px; display: flex; align-items: center; justify-content: center;
  cursor: pointer; padding: 0; font: inherit;
}
.slot img { width: 78%; height: 78%; image-rendering: auto; pointer-events: none; }
.slot .count {
  position: absolute; right: 3px; bottom: 1px; font-weight: 900; font-size: 16px; color: var(--ink);
  text-shadow: 0 0 3px #fff, 0 0 3px #fff;
}
.slot.sel { border-color: var(--ink); box-shadow: 0 0 0 3px var(--sun); }
.slot.empty { cursor: default; }
.detail { min-height: 64px; margin-top: 10px; font-size: 18px; display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.detail b { margin-right: 6px; }

.tabs { flex-wrap: wrap; margin: 4px 0 6px; }
.tabs .btn { font-size: 17px; padding: 8px 14px; min-height: 48px; }
.tabs .btn.on { background: var(--green); color: #fff; }
.grid.book { grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); }
.entry {
  background: #fff; border: 3px solid #d8c8b4; border-radius: 14px; padding: 6px;
  display: flex; flex-direction: column; align-items: center; text-align: center; font-size: 14px; font-weight: 700;
}
.entry img { width: 64px; height: 64px; }
.entry.unknown { background: #efe6d6; color: #a99a8a; }
.entry .sub { font-weight: 400; font-size: 12px; opacity: .8; }

.confirm { margin-top: 8px; padding: 10px; border: 3px dashed var(--coral); border-radius: 16px; }
.confirm p { margin: 0 0 8px; font-size: 18px; }
.help-list { font-size: 18px; line-height: 1.5; padding-left: 20px; margin: 0; }

/* ---------- confetti ---------- */
#confetti { position: fixed; inset: 0; pointer-events: none; z-index: 40; overflow: hidden; }
.cf { position: absolute; top: -20px; width: 12px; height: 16px; border-radius: 3px; animation: fall 1.8s ease-in forwards; }
@keyframes fall { to { transform: translateY(105vh) rotate(720deg); opacity: .8; } }

/* ---------- small screens ---------- */
@media (max-width: 430px) {
  .grid { grid-template-columns: repeat(4, 1fr); }
  .card-head h2 { font-size: 22px; }
  #btn-action { min-width: 116px; min-height: 88px; font-size: 23px; }
  #stick-base { width: 124px; height: 124px; }
  .pick { min-width: 62px; }
}
@media (max-height: 500px) {
  .btn.round { width: 50px; height: 50px; font-size: 22px; }
  #stick-base { width: 116px; height: 116px; }
}

/* ---------- island picker ---------- */
.island-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin: 6px 0 8px; }
.island-card {
  font: inherit; color: var(--ink); background: #fff; border: 3px solid #d8c8b4; border-radius: 16px;
  padding: 6px; display: flex; flex-direction: column; align-items: center; gap: 2px; cursor: pointer; min-height: 48px;
}
.island-card img { width: 100%; max-width: 160px; aspect-ratio: 150 / 110; border-radius: 10px; }
.island-card b { font-size: 16px; }
.island-card span { font-size: 13px; opacity: .8; text-align: center; }
.island-card[aria-checked="true"] { border-color: var(--ink); background: #fff1c9; box-shadow: 0 0 0 3px var(--sun); }
#btn-reroll { margin-bottom: 4px; }
#hud-shells { font-size: 14px; }
@media (orientation: portrait) and (max-width: 430px) {
  .island-card img { max-width: 120px; }
  .island-card span { display: none; }
}

/* ---------- volume sliders ---------- */
.vol { display: flex; align-items: center; gap: 12px; font-size: 19px; font-weight: 700; margin: 10px 4px; }
.vol input[type="range"] { flex: 1; height: 44px; accent-color: var(--green); cursor: pointer; }
#bite { white-space: nowrap; }
@media (max-width: 430px) { #bite { font-size: 30px; } }

/* ---------- accounts ---------- */
.field { display: block; font-weight: 700; font-size: 18px; margin: 10px 0 4px; }
.field input { margin-top: 4px; }
.pw { position: relative; display: block; }
.pw input { padding-right: 64px; }
.eye {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%); margin-top: 2px;
  width: 52px; height: 46px; font-size: 24px; border: 0; background: transparent; cursor: pointer;
}
.check { display: flex; align-items: center; gap: 10px; font-size: 17px; font-weight: 700; margin: 12px 0 4px; min-height: 44px; }
.check input { width: 28px; height: 28px; accent-color: var(--green); }
.msg { min-height: 0; margin: 4px 0; font-size: 17px; font-weight: 700; }
.msg:empty { display: none; }
.msg.bad { color: #c0392b; }
.msg.warn { color: #9a6b00; }
.msg.info { color: var(--green-dark); }
.btn.local { display: block; margin: 14px auto 2px; background: transparent; box-shadow: none; border-style: dashed; }
#acc-welcome .btn.wide { margin: 12px 0; }
.acc-row { display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; font-weight: 700; font-size: 16px; margin: 4px 0 6px; }
#conflict { z-index: 40; }
#conflict small { font-weight: 400; font-size: 15px; }
#conflict p { font-size: 18px; }
.hint.bad { color: #c0392b; opacity: 1; font-weight: 700; }
