:root {
  --cream: #fbf7f0;
  --paper: #fffdf9;
  --ink: #24302f;
  --muted: #7a807c;
  --coral: #e8734a;
  --teal: #1f9e9a;
  --line: #ece4d6;
  --good: #2bae66;
  --bad: #e04848;
  --shadow: 0 10px 30px rgba(30, 50, 50, .15);
}
* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%; overflow: hidden; background: #e4eee9; color: var(--ink);
  font-family: 'Noto Sans TC', -apple-system, 'PingFang TC', system-ui, sans-serif;
  -webkit-user-select: none; user-select: none;
}
canvas#game { position: fixed; inset: 0; width: 100%; height: 100%; display: block; }
.hidden { display: none !important; }
.card { background: var(--paper); border-radius: 18px; box-shadow: var(--shadow); }
.kicker { font-size: 12px; letter-spacing: 3px; color: var(--coral); font-weight: 700; text-transform: uppercase; }
.muted { color: var(--muted); }
.bad { color: var(--bad); }
kbd {
  display: inline-block; background: #fff; border: 1px solid var(--line); border-bottom-width: 3px;
  border-radius: 6px; padding: 1px 7px; font-family: inherit; font-size: 12px; font-weight: 700;
}

/* ---------- 標題卡 ---------- */
#brand { position: fixed; top: 20px; left: 20px; display: flex; gap: 16px; align-items: center; padding: 14px 20px; z-index: 5; }
.brand-mark { display: flex; align-items: center; gap: 10px; }
.brand-mark .zh { font-size: 32px; font-weight: 900; color: var(--coral); letter-spacing: 2px; line-height: 1; }
.brand-mark .en { font-size: 13px; font-weight: 900; line-height: 1.05; color: var(--teal); letter-spacing: 1px; }
.brand-sub { border-left: 1px solid var(--line); padding-left: 14px; display: flex; flex-direction: column; }
.brand-sub b { font-size: 15px; }
.brand-sub small { color: var(--muted); font-size: 11px; letter-spacing: 1px; }
.brand-lohas { width: 48px; height: 48px; border-left: 1px solid var(--line); padding-left: 12px; box-sizing: content-box; }
#sponsor {
  position: fixed; left: 50%; bottom: 18px; transform: translateX(-50%); z-index: 31;
  display: flex; align-items: center; gap: 10px; background: rgba(255, 253, 249, .92);
  border-radius: 999px; padding: 6px 18px 6px 6px; box-shadow: var(--shadow);
}
#sponsor img { width: 46px; height: 46px; }
#sponsor span { display: flex; flex-direction: column; line-height: 1.25; }
#sponsor b { font-size: 15px; }
#sponsor small { font-size: 11px; color: var(--muted); }
.title-sponsor { display: flex; align-items: center; gap: 8px; margin-top: 14px; font-size: 13px; color: var(--muted); }
.title-sponsor img { width: 34px; height: 34px; }

/* ---------- 右側關卡清單 ---------- */
#panel {
  position: fixed; top: 0; right: 0; bottom: 0; width: 360px; background: var(--cream);
  padding: 28px 24px; overflow-y: auto; border-left: 1px solid var(--line); z-index: 5;
}
#panel h2 { margin: 8px 0 6px; font-size: 28px; font-weight: 900; }
#panel p { margin: 0 0 16px; font-size: 14px; }
.player-card {
  display: grid; grid-template-columns: 1fr 1fr; gap: 10px; background: #fff3e8;
  border: 1px solid #f6d9c3; border-radius: 14px; padding: 12px 14px; margin-bottom: 10px;
}
.player-card small { display: block; color: var(--muted); font-size: 12px; }
.player-card b { font-size: 20px; }
#level-list { list-style: none; margin: 0; padding: 0; }
#level-list li {
  display: grid; grid-template-columns: 46px 1fr auto; align-items: center; gap: 6px;
  padding: 14px 6px; border-bottom: 1px solid var(--line); cursor: pointer; transition: background .15s;
}
#level-list li:hover { background: #fff; }
#level-list .num { font-family: Georgia, serif; font-style: italic; font-size: 26px; color: var(--coral); }
#level-list .lname { font-weight: 700; font-size: 16px; display: flex; flex-direction: column; }
#level-list .lname small { font-weight: 400; color: var(--muted); font-size: 11px; }
#level-list .meta { font-size: 13px; color: var(--muted); }
#level-list li.cleared .num, #level-list li.cleared .meta { color: var(--good); font-weight: 700; }
#level-list li.locked { opacity: .45; cursor: not-allowed; }
.panel-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 18px; }
.panel-actions .primary { grid-column: span 2; }

/* ---------- 地圖標籤 ---------- */
#labels { position: fixed; inset: 0; pointer-events: none; z-index: 4; overflow: hidden; }
.pin {
  position: absolute; left: 0; top: 0; pointer-events: auto; border: none; cursor: pointer;
  background: #fff; border-radius: 999px; padding: 5px 12px 5px 6px; display: flex; gap: 6px; align-items: center;
  font: 700 13px 'Noto Sans TC', sans-serif; color: var(--ink); white-space: nowrap; box-shadow: 0 4px 14px rgba(0, 0, 0, .15);
}
.pin:hover { transform-origin: center; filter: brightness(1.03); box-shadow: 0 6px 18px rgba(0, 0, 0, .25); }
.pin-num { background: var(--coral); color: #fff; border-radius: 999px; padding: 1px 7px; font-size: 12px; }
.pin.locked { color: #9a9a9a; }
.pin.locked .pin-num { background: #bbb; }
.pin.cleared .pin-num { background: var(--good); }
.pin-state:empty { display: none; }

/* ---------- 按鈕與視窗 ---------- */
.btn {
  border: none; border-radius: 12px; padding: 11px 18px; font: 700 15px 'Noto Sans TC', sans-serif;
  cursor: pointer; transition: transform .1s, filter .15s;
}
.btn:hover { filter: brightness(1.05); }
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--ink); color: #fff; }
.btn.ghost { background: #fff; border: 1px solid var(--line); color: var(--ink); }
#modal-wrap {
  position: fixed; inset: 0; background: rgba(220, 232, 228, .5); backdrop-filter: blur(6px);
  display: flex; align-items: center; justify-content: center; z-index: 30; padding: 16px;
}
.modal { width: min(460px, 100%); max-height: calc(100vh - 32px); overflow-y: auto; padding: 26px 28px; }
.modal.wide { width: min(640px, 100%); }
.modal h2 { margin: 8px 0 10px; font-size: 26px; font-weight: 900; }
.modal p { line-height: 1.7; font-size: 15px; margin: 0 0 12px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; flex-wrap: wrap; }
.modal-actions .btn:first-child:not(:last-child) { margin-right: auto; }
#modal-wrap.photo { background: linear-gradient(rgba(0, 40, 60, .25), rgba(0, 40, 60, .45)), url(../assets/ui/loading.jpg) center / cover; backdrop-filter: none; }
.title-hero { display: flex; gap: 18px; align-items: center; }
.title-diver { height: 340px; width: 200px; object-fit: contain; flex-shrink: 0; }
.title-main { flex: 1; min-width: 0; }
.title-diver:not([src]), .title-logo:not([src]) { visibility: hidden; }
.title-logo { width: 100%; max-width: 320px; display: block; margin: -10px auto 0; }
.title-main h2 { margin: 0 0 8px; display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.title-main h2 small { font-size: 14px; color: var(--teal); font-weight: 700; }
.level-thumb { display: block; width: 100%; aspect-ratio: 16 / 9; object-fit: cover; border-radius: 12px; margin: 4px 0 12px; }
.dive-card .level-thumb { margin-top: 0; }
.field { display: block; margin-top: 8px; }
.field span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.field input {
  width: 100%; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line);
  font: 500 16px 'Noto Sans TC', sans-serif; background: #fff; outline: none; -webkit-user-select: text; user-select: text;
}
.field input:focus { border-color: var(--coral); }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 12px 0; }
.stats div { background: var(--cream); border-radius: 12px; padding: 10px; text-align: center; }
.stats small { display: block; color: var(--muted); font-size: 12px; }
.stats b { font-size: 20px; }
.help { padding-left: 18px; margin: 0; line-height: 1.8; }
.help li { margin-bottom: 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
.chip { background: var(--cream); border-radius: 999px; padding: 2px 10px; font-size: 13px; }
.keys { display: flex; flex-wrap: wrap; gap: 8px 16px; margin-top: 12px; font-size: 14px; }
.credits { margin-top: 16px !important; font-size: 12px !important; color: var(--muted); line-height: 1.6 !important; }
.board { width: 100%; border-collapse: collapse; font-size: 15px; }
.board th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 500; padding: 6px 8px; border-bottom: 1px solid var(--line); }
.board td { padding: 9px 8px; border-bottom: 1px solid var(--line); }
.board td:first-child { font-family: Georgia, serif; font-style: italic; color: var(--coral); font-size: 18px; }
.board tr.me td { background: #fff3e8; font-weight: 700; }

/* ---------- 潛水關卡 HUD ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; z-index: 6; }
.hud-label { font-size: 12px; color: var(--muted); font-weight: 700; letter-spacing: 1px; }
.hud-tl { position: absolute; top: 16px; left: 16px; padding: 12px 16px; width: 220px; }
.hud-score b { font-size: 34px; font-weight: 900; color: var(--coral); }
.hud-score span { color: var(--muted); margin-left: 4px; }
.bar { height: 8px; border-radius: 99px; background: #eee5d8; overflow: hidden; margin-top: 4px; }
.bar i { display: block; height: 100%; width: 0; background: var(--coral); border-radius: 99px; transition: width .2s; }
.bar i.ok { background: var(--good); }
.bar.stam i { background: var(--teal); transition: none; }
.hud-tc { position: absolute; top: 16px; left: 50%; transform: translateX(-50%); display: flex; gap: 8px; }
.hud-time { padding: 10px 18px; font: 900 26px Georgia, serif; }
.hud-time.low { color: var(--bad); animation: pulse .8s infinite; }
.hud-radar { padding: 10px 14px; display: flex; align-items: center; gap: 8px; font-weight: 700; }
.hud-radar .arrow { display: inline-block; color: var(--coral); font-size: 18px; transition: transform .1s; }
.hud-tr { position: absolute; top: 16px; right: 16px; padding: 12px 16px; width: 180px; }
.hearts { font-size: 22px; color: var(--bad); letter-spacing: 2px; margin-bottom: 4px; }
#tool {
  position: absolute; right: -7vh; bottom: -12vh; height: 72vh; transform-origin: 85% 100%;
  filter: brightness(.92) saturate(.95) drop-shadow(0 6px 18px rgba(0, 20, 40, .35));
}
#tool:not([src]) { display: none; }
#crosshair {
  position: absolute; left: 50%; top: 50%; width: 24px; height: 24px; margin: -12px 0 0 -12px;
  border: 2px solid rgba(255, 255, 255, .85); border-radius: 50%; transition: transform .1s, border-color .1s;
}
#crosshair::after { content: ''; position: absolute; left: 50%; top: 50%; width: 4px; height: 4px; margin: -2px; background: #fff; border-radius: 50%; }
#crosshair.on { border-color: #7dffb0; transform: scale(1.35); }
#h-shark {
  position: absolute; left: 50%; top: 50%; display: flex; flex-direction: column; align-items: center; gap: 2px;
  color: #fff; font-weight: 900; font-size: 14px; text-shadow: 0 1px 4px rgba(0, 0, 0, .6);
}
#h-shark .sa { color: #ff4040; font-size: 34px; line-height: 1; animation: pulse .5s infinite; }
#h-shark .sd { background: rgba(200, 30, 30, .85); padding: 2px 10px; border-radius: 99px; text-shadow: none; }
#h-shark.onscreen .sa { display: none; }
#h-hint {
  position: absolute; left: 50%; top: calc(50% + 30px); transform: translateX(-50%);
  color: #fff; font-weight: 700; text-shadow: 0 1px 4px rgba(0, 0, 0, .6); font-size: 15px;
}
#h-keys {
  position: absolute; left: 16px; bottom: 14px; color: rgba(255, 255, 255, .85); font-size: 12px;
  text-shadow: 0 1px 3px rgba(0, 0, 0, .5);
}
#vignette { position: fixed; inset: 0; pointer-events: none; z-index: 5; box-shadow: inset 0 0 180px rgba(0, 25, 45, .55); }
#vignette.danger { box-shadow: inset 0 0 220px rgba(220, 30, 30, .7); animation: pulse .9s infinite; }
#flash { position: fixed; inset: 0; pointer-events: none; z-index: 7; opacity: 0; transition: opacity .5s; }
#flash.bad { background: rgba(255, 40, 40, .4); opacity: 1; transition: none; }
#flash.warn { background: rgba(255, 190, 40, .35); opacity: 1; transition: none; }
@keyframes pulse { 50% { opacity: .55; } }

#dive-overlay {
  position: fixed; inset: 0; z-index: 20; display: flex; align-items: center; justify-content: center;
  background: rgba(0, 30, 50, .35); cursor: pointer; padding: 16px;
}
.dive-card { padding: 26px 30px; width: min(520px, 100%); text-align: center; }
.dive-card h2 { margin: 6px 0 4px; font-size: 32px; font-weight: 900; }
.dive-card .keys { justify-content: center; }
.dive-cta {
  margin-top: 20px; background: var(--ink); color: #fff; border-radius: 12px; padding: 12px; font-weight: 700;
  animation: pulse 1.6s infinite;
}

/* ---------- 浮動文字與提示 ---------- */
#popups { position: fixed; left: 50%; top: 40%; z-index: 8; pointer-events: none; }
.popup {
  position: absolute; transform: translateX(-50%); white-space: nowrap; font-size: 22px; font-weight: 900;
  text-shadow: 0 2px 6px rgba(0, 0, 0, .4); animation: rise 1.2s ease-out forwards;
}
.popup.good { color: #7dffb0; }
.popup.bad { color: #ff8080; }
@keyframes rise { from { opacity: 1; translate: 0 0; } to { opacity: 0; translate: 0 -70px; } }
#toast {
  position: fixed; left: 50%; bottom: 60px; transform: translate(-50%, 20px); z-index: 40;
  background: var(--ink); color: #fff; padding: 12px 20px; border-radius: 12px; font-weight: 700; font-size: 15px;
  opacity: 0; pointer-events: none; transition: opacity .25s, transform .25s; max-width: calc(100vw - 32px); text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#btn-mute {
  position: fixed; left: 20px; bottom: 20px; z-index: 35; width: 44px; height: 44px; border-radius: 50%;
  border: none; background: var(--paper); box-shadow: var(--shadow); font-size: 18px; cursor: pointer;
}
#hud:not(.hidden) ~ #btn-mute { bottom: auto; top: 140px; left: 16px; }

@media (max-width: 860px) {
  #panel { top: auto; width: 100%; height: 44vh; border-left: none; border-top: 1px solid var(--line); padding: 18px 16px; }
  #brand { top: 12px; left: 12px; padding: 10px 14px; }
  .brand-mark .zh { font-size: 24px; }
  .brand-sub { display: none; }
  .stats { grid-template-columns: repeat(2, 1fr); }
  .title-diver { display: none; }
  #tool { height: 50vh; }
}

/* ---------- 手機／平板觸控 ---------- */
.touch-only { display: none; }
body.touch .touch-only { display: flex; }
body.touch span.touch-only { display: inline; }
body.touch .desk-only { display: none !important; }
#touch-ui { display: none; }
body.touch #hud:not(.hidden) #touch-ui { display: block; }
canvas#game { touch-action: none; }
#joy {
  position: absolute; left: calc(24px + env(safe-area-inset-left)); bottom: calc(28px + env(safe-area-inset-bottom));
  width: 130px; height: 130px; border-radius: 50%; pointer-events: auto; touch-action: none;
  background: rgba(255, 255, 255, .18); border: 2px solid rgba(255, 255, 255, .5);
}
#joy .knob {
  position: absolute; left: 50%; top: 50%; width: 56px; height: 56px; margin: -28px; border-radius: 50%;
  background: rgba(255, 255, 255, .85); box-shadow: 0 2px 8px rgba(0, 0, 0, .25);
}
.tbtns {
  position: absolute; right: calc(16px + env(safe-area-inset-right)); bottom: calc(24px + env(safe-area-inset-bottom));
  display: grid; grid-template-columns: 62px 80px; grid-auto-rows: auto; gap: 10px; align-items: center; justify-items: center;
}
.tbtn {
  pointer-events: auto; touch-action: none; border: 2px solid rgba(255, 255, 255, .6); border-radius: 50%;
  width: 58px; height: 58px; background: rgba(20, 50, 60, .45); color: #fff;
  font: 700 14px 'Noto Sans TC', sans-serif; -webkit-tap-highlight-color: transparent; user-select: none; -webkit-user-select: none;
}
.tbtn.on, .tbtn:active { background: rgba(232, 115, 74, .8); }
.tbtn.big { width: 80px; height: 80px; font-size: 20px; background: rgba(232, 115, 74, .75); }
.tbtn.wide { grid-column: 1 / span 2; width: 152px; height: 44px; border-radius: 22px; }
#t-surface { grid-row: 1; background: rgba(43, 174, 102, .85); }
#t-up { grid-column: 1; grid-row: 2; }
#t-down { grid-column: 1; grid-row: 3; }
#t-grab { grid-column: 2; grid-row: 2 / span 2; }
#t-sprint { grid-row: 4; }
.tbtn.small { position: absolute; top: calc(116px + env(safe-area-inset-top)); left: 16px; width: 44px; height: 44px; font-size: 12px; }
body.touch #h-keys, body.touch #btn-mute { }
body.touch #h-keys { display: none; }
body.touch #tool { height: 46vh; right: -4vh; bottom: -8vh; opacity: .9; }
body.touch .hud-tl { width: 150px; padding: 8px 12px; top: 10px; left: 10px; }
body.touch .hud-score b { font-size: 24px; }
body.touch .hud-tr { width: 128px; padding: 8px 12px; top: 10px; right: 10px; }
body.touch .hearts { font-size: 18px; }
body.touch .hud-tc { top: 10px; }
body.touch .hud-time { font-size: 20px; padding: 6px 12px; }
body.touch .hud-radar { padding: 6px 10px; }
body.touch #hud:not(.hidden) ~ #btn-mute { top: calc(116px + env(safe-area-inset-top)); left: 68px; width: 44px; height: 44px; }
@media (max-width: 600px) {
  body.touch .hud-tc { top: 96px; }
  .modal { padding: 20px 18px; }
  .modal h2 { font-size: 22px; }
  #sponsor { bottom: 10px; padding: 4px 14px 4px 4px; }
  #sponsor img { width: 38px; height: 38px; }
}

.lang-btn {
  position: absolute; top: 14px; right: 14px; border: 1px solid var(--line); background: #fff; border-radius: 999px;
  padding: 6px 14px; font: 700 13px 'Noto Sans TC', sans-serif; cursor: pointer; color: var(--teal);
}
.modal { position: relative; }
body.lang-en .brand-mark .zh { font-size: 26px; letter-spacing: 0; }
body.lang-en .brand-mark .en { display: none; }
@media (max-height: 560px) {
  .title-logo { max-width: 170px; margin-top: -14px; }
  .title-main h2 { font-size: 20px; margin-bottom: 4px; }
  .modal p { font-size: 13px; line-height: 1.55; margin-bottom: 8px; }
  .modal { padding: 16px 20px; }
  .modal-actions { margin-top: 12px; }
  .field input { padding: 9px 12px; }
  #sponsor { display: none !important; }
  .level-thumb { aspect-ratio: 21 / 9; }
  .dive-card { padding: 14px 18px; }
  .dive-card h2 { font-size: 22px; }
  .stats b { font-size: 16px; }
  .stats div { padding: 6px; }
}

/* 手機只留搖桿：上浮／下潛跟著視線方向、輕點畫面撿、搖桿推到底加速 */
#t-up, #t-down, #t-grab, #t-sprint { display: none !important; }
#joy.sprint { border-color: #ff9b6a; background: rgba(232, 115, 74, .25); }
#joy.sprint .knob { background: #ffb38a; }
#joy::after {
  content: attr(data-label); position: absolute; left: 50%; bottom: -22px; transform: translateX(-50%);
  white-space: nowrap; color: #fff; font-size: 11px; font-weight: 700; text-shadow: 0 1px 3px rgba(0,0,0,.6);
}

.back-site {
  position: absolute; top: 14px; left: 14px; border: none; background: none; padding: 6px 4px;
  font: 700 13px 'Noto Sans TC', sans-serif; color: var(--muted); cursor: pointer;
}
.back-site:hover { color: var(--ink); }
.panel-actions .exit { grid-column: span 2; color: var(--muted); }
