:root {
  --ink: #2f3e46;
  --fg: #2f3e46;
  --paper: #f7f4ec;
  --soft: rgba(247, 244, 236, 0.88);
  --muted: #6b7a80;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden; position: fixed; inset: 0;
  background: #c4e3de; color: var(--fg);
  font-family: ui-rounded, "SF Pro Rounded", -apple-system, "Avenir Next", system-ui, sans-serif;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; overscroll-behavior: none;
}
button { font: inherit; color: inherit; cursor: pointer; }
.hidden { display: none !important; }
canvas#game { position: fixed; inset: 0; width: 100vw; height: 100vh; height: 100dvh; display: block; touch-action: none; }

/* ── HUD ── */
#hud { position: fixed; inset: 0; pointer-events: none; }
#topbar {
  position: absolute; top: calc(var(--sat) + 10px); left: calc(var(--sal) + 12px); right: calc(var(--sar) + 12px);
  display: flex; justify-content: space-between; align-items: flex-start; gap: 8px;
}
#topbar .right { display: flex; gap: 8px; }
.pill {
  pointer-events: auto; background: var(--soft);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-radius: 999px; padding: 6px 14px 6px 6px; display: flex; align-items: center; gap: 10px;
  box-shadow: 0 2px 12px rgba(47, 62, 70, 0.08); min-height: 44px;
}
#dial { width: 32px; height: 32px; }
.clocktext { display: flex; flex-direction: column; line-height: 1.05; }
.clocktext b { font-size: 15px; }
.clocktext span { font-size: 11px; color: var(--muted); }
.tide { display: flex; align-items: center; gap: 6px; padding-left: 8px; border-left: 1px solid rgba(47,62,70,.12); font-size: 12px; color: var(--muted); }
.meter { width: 8px; height: 24px; border-radius: 4px; background: rgba(47,62,70,.1); position: relative; overflow: hidden; }
#tideFill { position: absolute; left: 0; right: 0; bottom: 0; background: #5aa9a0; border-radius: 4px; transition: height .3s; }
#scorePill { padding: 6px 16px; gap: 8px; }
#scorePill b { font-size: 20px; font-variant-numeric: tabular-nums; }
.pax { width: 18px; height: 18px; }
.badge { font-size: 10px; text-transform: uppercase; letter-spacing: .06em; background: #5aa9a0; color: #fff; border-radius: 8px; padding: 2px 6px; }
.btns { padding: 4px; gap: 2px; }
.btns button { border: 0; background: transparent; width: 40px; height: 36px; border-radius: 18px; font-weight: 600; font-size: 15px; display: grid; place-items: center; }
.btns button svg { width: 18px; height: 18px; }
.btns button:active { background: rgba(47,62,70,.08); }

#tray {
  position: absolute; bottom: calc(var(--sab) + 12px); left: 10px; right: 10px; margin: 0 auto; width: fit-content;
  display: flex; gap: 10px; align-items: center; pointer-events: auto;
  background: var(--soft); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-radius: 30px; padding: 8px 14px; box-shadow: 0 2px 14px rgba(47, 62, 70, 0.1);
  max-width: calc(100vw - 20px); flex-wrap: wrap; justify-content: center;
}
#routeSlots { display: flex; gap: 8px; }
#routeSlots.flash { animation: flash .5s 2; }
@keyframes flash { 50% { transform: scale(1.12); } }
.slot { width: 30px; height: 30px; border-radius: 50%; border: 3.5px solid var(--c); background: transparent; padding: 0; }
.slot.used { background: var(--c); }
.sep { width: 1px; height: 30px; background: rgba(47,62,70,.15); }
.item { position: relative; width: 48px; height: 48px; border-radius: 16px; display: grid; place-items: center; touch-action: none; cursor: grab; }
.item:active { background: rgba(47,62,70,.07); }
.item .icon { width: 36px; height: 36px; display: block; color: var(--fg); }
.item .icon svg, #ghost svg, .choice .ic svg { width: 100%; height: 100%; display: block; }
.item.empty { opacity: .35; }
.count {
  position: absolute; top: 0; right: -2px; background: var(--ink); color: #fff; font-size: 11px; font-weight: 600;
  border-radius: 9px; min-width: 18px; height: 18px; display: grid; place-items: center; padding: 0 4px;
}
#ghost { position: fixed; left: 0; top: 0; width: 52px; height: 52px; pointer-events: none; z-index: 30; color: var(--fg); filter: drop-shadow(0 4px 8px rgba(0,0,0,.2)); }
#toast {
  position: absolute; left: 50%; bottom: calc(var(--sab) + 86px); transform: translate(-50%, 10px);
  background: rgba(47, 62, 70, 0.9); color: #fff; padding: 10px 16px; border-radius: 16px; font-size: 14px;
  max-width: min(440px, calc(100vw - 32px)); text-align: center; opacity: 0; transition: opacity .35s, transform .35s;
  pointer-events: none; line-height: 1.35;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#popover { position: fixed; transform: translate(-50%, -100%); z-index: 25; }
#popover button { border: 0; background: var(--paper); border-left: 6px solid var(--c); padding: 10px 14px; border-radius: 14px; box-shadow: 0 4px 18px rgba(47,62,70,.2); font-weight: 600; white-space: nowrap; }

/* ── Title ── */
.screen {
  position: fixed; inset: 0; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: calc(var(--sat) + 32px) calc(var(--sar) + 20px) calc(var(--sab) + 32px) calc(var(--sal) + 20px);
  background: linear-gradient(rgba(247,244,236,.25), rgba(247,244,236,.7));
  display: flex; justify-content: center;
}
.title-wrap { width: min(900px, 100%); }
header h1 { font-size: clamp(44px, 8vw, 76px); font-weight: 650; letter-spacing: -0.03em; margin: 0; line-height: 1; }
header p { margin: 8px 0 22px; color: var(--muted); font-size: 17px; }
.continue {
  width: 100%; border: 0; border-radius: 20px; background: var(--ink); color: #fff; padding: 16px 20px;
  display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 18px; text-align: left;
}
.continue b { font-size: 18px; }
.continue span { opacity: .8; font-size: 14px; }
.maps { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 14px; }
.card { border: 0; background: var(--soft); border-radius: 22px; padding: 8px; text-align: left; box-shadow: 0 2px 12px rgba(47,62,70,.07); transition: transform .15s; }
.card:active { transform: scale(.98); }
.card canvas { width: 100%; aspect-ratio: 4 / 3; border-radius: 16px; display: block; }
.card .info { padding: 10px 8px 6px; display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; }
.card .info b { font-size: 16px; }
.card .info small { grid-column: 1 / -1; color: var(--muted); font-size: 13px; line-height: 1.35; }
.stars { color: #e3a83b; letter-spacing: 1px; }
.card.locked canvas { filter: grayscale(.7) blur(1.5px); opacity: .55; }
.card.locked { cursor: default; }
footer { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 16px; margin-top: 22px; padding: 14px 16px; background: var(--soft); border-radius: 20px; }
.switch { display: flex; align-items: center; gap: 10px; cursor: pointer; }
.switch input { display: none; }
.switch .track { width: 44px; height: 26px; border-radius: 13px; background: rgba(47,62,70,.2); position: relative; transition: background .2s; flex: none; }
.switch .track::after { content: ''; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; transition: transform .2s; box-shadow: 0 1px 3px rgba(0,0,0,.2); }
.switch input:checked + .track { background: #5aa9a0; }
.switch input:checked + .track::after { transform: translateX(18px); }
.lbl { display: flex; flex-direction: column; font-weight: 600; }
.lbl small { font-weight: 400; color: var(--muted); font-size: 12px; }
.vol { display: flex; gap: 18px; flex-wrap: wrap; }
.vol label { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--muted); }
input[type=range] { width: 110px; accent-color: var(--ink); }

/* ── Modal ── */
.modal { position: fixed; inset: 0; display: grid; place-items: center; background: rgba(47,62,70,.2); backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px); z-index: 20; padding: 20px; }
.panel { background: var(--paper); border-radius: 26px; padding: 24px; width: min(480px, 100%); max-height: calc(100dvh - 40px); overflow-y: auto; box-shadow: 0 10px 40px rgba(47,62,70,.25); animation: rise .35s ease-out; }
@keyframes rise { from { transform: translateY(14px); opacity: 0; } }
.panel h2 { margin: 0 0 6px; font-size: 24px; letter-spacing: -0.01em; }
.panel .body { color: var(--fg); line-height: 1.4; }
.muted { color: var(--muted); }
.choices { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 16px; }
.choice { border: 0; background: #fff; border-radius: 20px; padding: 16px 14px; text-align: left; display: flex; flex-direction: column; gap: 4px; box-shadow: 0 2px 10px rgba(47,62,70,.08); transition: transform .15s; }
.choice:active { transform: scale(.97); }
.choice .ic { width: 44px; height: 44px; color: var(--fg); margin-bottom: 4px; }
.choice b { font-size: 16px; }
.choice small { color: var(--muted); font-size: 13px; line-height: 1.35; }
.btnrow { display: flex; gap: 10px; justify-content: flex-end; margin-top: 20px; flex-wrap: wrap; }
.btnrow button { border: 0; border-radius: 16px; padding: 12px 18px; background: rgba(47,62,70,.08); font-weight: 600; }
.btnrow button.primary { background: var(--ink); color: #fff; }
.final { text-align: center; padding: 8px 0; display: grid; gap: 4px; }
.final .big { font-size: 56px; font-weight: 700; line-height: 1; }
.final .stars { font-size: 26px; margin-top: 6px; }
.tag { justify-self: center; background: #e3a83b; color: #fff; border-radius: 10px; padding: 3px 10px; font-weight: 600; font-size: 13px; }

/* ── Daily voyage, sections, settings ── */
.daily {
  width: 100%; border: 0; background: var(--soft); border-radius: 22px; padding: 8px; margin-bottom: 8px;
  display: flex; align-items: center; gap: 14px; text-align: left; box-shadow: 0 2px 12px rgba(47,62,70,.07);
}
.daily canvas { width: 132px; aspect-ratio: 4 / 3; border-radius: 16px; flex: none; }
.dinfo { display: flex; flex-direction: column; gap: 3px; flex: 1; min-width: 0; }
.dinfo small { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .05em; }
.dinfo b { font-size: 18px; }
.dinfo > span { color: var(--muted); font-size: 14px; }
.daily .go { background: var(--ink); color: #fff; border-radius: 14px; padding: 10px 16px; font-weight: 600; margin-right: 8px; flex: none; }
h3.section { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 18px 4px 10px; }
.ghostbtn { border: 0; background: rgba(47,62,70,.08); border-radius: 14px; padding: 10px 16px; font-weight: 600; }
.settings { display: grid; gap: 16px; margin-top: 14px; }
.settings .range { display: flex; align-items: center; justify-content: space-between; gap: 12px; font-weight: 600; }
.settings .range input { width: 60%; }
.danger { border: 0; background: transparent; color: #c8553d; font-weight: 600; text-align: left; padding: 4px 0; }
.btnrow button.danger-btn { background: #c8553d; color: #fff; }
.item.info { cursor: help; }

/* ── End-of-voyage card ── */
.snap { width: 100%; border-radius: 16px; margin: 6px 0 10px; display: block; box-shadow: 0 2px 10px rgba(47,62,70,.12); }
.statrow { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 12px 0 4px; }
.statrow span { display: flex; flex-direction: column; font-size: 12px; color: var(--muted); }
.statrow b { font-size: 15px; color: var(--fg); }
.lstats { display: grid; gap: 6px; margin-top: 10px; text-align: left; }
.lstat { display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: center; }
.lstat .bar { height: 10px; border-radius: 5px; background: rgba(47,62,70,.08); overflow: hidden; }
.lstat .bar i { display: block; height: 100%; border-radius: 5px; }
.lstat b { font-size: 13px; min-width: 28px; text-align: right; }

/* ── Night sea ── */
body.dark { --paper: #24343d; --soft: rgba(30, 44, 52, 0.9); --muted: #9fb0b6; --fg: #e8eef0; background: #22333d; }
body.dark .screen { background: linear-gradient(rgba(20,30,36,.25), rgba(20,30,36,.6)); }
body.dark .count, body.dark .continue, body.dark .daily .go, body.dark .btnrow button.primary { background: #e8eef0; color: #22333d; }
body.dark .choice { background: #2e424c; }
body.dark .btnrow button, body.dark .ghostbtn { background: rgba(232,238,240,.1); }
body.dark .lstat .bar { background: rgba(232,238,240,.1); }
body.dark .tide { border-left-color: rgba(232,238,240,.15); }
body.dark .meter { background: rgba(232,238,240,.12); }
body.dark .sep { background: rgba(232,238,240,.15); }
body.dark input[type=range] { accent-color: #e8eef0; }
body.dark .switch .track { background: rgba(232,238,240,.2); }
body.dark .switch input:checked + .track { background: #5aa9a0; }

/* ── Reduced motion ── */
body.rm *, body.rm *::before, body.rm *::after { animation: none !important; transition: none !important; }

/* ── Narrow (folded / cover screen) ── */
@media (max-width: 520px) {
  .tide span { display: none; }
  .pill { gap: 8px; }
  #tray { gap: 6px; padding: 6px 10px; }
  #routeSlots { gap: 5px; }
  .slot { width: 24px; height: 24px; border-width: 3px; }
  .item { width: 40px; height: 40px; }
  .item .icon { width: 30px; height: 30px; }
  .choices { grid-template-columns: 1fr; }
  .continue { flex-direction: column; align-items: flex-start; gap: 2px; }
  .daily canvas { width: 96px; }
  .daily .go { display: none; }
}
