/* ==========================================================================
   Marina Escape — styles
   Layout: one fixed full-viewport app; screens are absolutely stacked and
   toggled via [data-screen] on #app. Portrait = stacked HUD/board/actions;
   landscape = side panels around a square board.
   ========================================================================== */

:root {
  --bg: #e4f1f5;
  --bg-stripe: #d8eaf0;
  --ink: #1d2740;
  --ink-soft: #5b6784;
  --card: #ffffff;
  --card-edge: #c9d4e4;
  --primary: #ffc533;
  --primary-edge: #d99a0b;
  --primary-ink: #3a2a00;
  --secondary: #ffffff;
  --secondary-edge: #b9c6da;
  --danger: #ef4b45;
  --danger-edge: #b52b26;
  --red: #e5383b;
  --accent: #1b6fa3;
  --star: #ffbf1f;
  --star-off: #c4cddb;
  --shadow: 0 10px 30px rgba(29, 39, 64, .16);
  --radius: 18px;
  --font: ui-rounded, "SF Pro Rounded", "Nunito", "Varela Round", "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1d2b;
    --bg-stripe: #122334;
    --ink: #eef2fb;
    --ink-soft: #9aa7c4;
    --card: #222c45;
    --card-edge: #10172a;
    --secondary: #2c3857;
    --secondary-edge: #151d33;
    --star-off: #46526f;
    --shadow: 0 10px 30px rgba(0, 0, 0, .45);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
    --bg: #0f1d2b;
    --bg-stripe: #122334;
    --ink: #eef2fb;
    --ink-soft: #9aa7c4;
    --card: #222c45;
    --card-edge: #10172a;
    --secondary: #2c3857;
    --secondary-edge: #151d33;
    --star-off: #46526f;
    --shadow: 0 10px 30px rgba(0, 0, 0, .45);
    color-scheme: dark;
  }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--bg); color: var(--ink);
  font-family: var(--font);
  overscroll-behavior: none;
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
  -webkit-text-size-adjust: 100%;
}
img, canvas { -webkit-user-drag: none; user-drag: none; }

.app {
  position: fixed; inset: 0;
  overflow: hidden;
  background:
    repeating-linear-gradient(135deg, transparent 0 22px, var(--bg-stripe) 22px 44px),
    var(--bg);
}

/* ---------- Screens ---------- */
.screen {
  position: absolute; inset: 0;
  padding: calc(var(--safe-t) + 8px) calc(var(--safe-r) + 12px) calc(var(--safe-b) + 10px) calc(var(--safe-l) + 12px);
  display: none;
  touch-action: none;
}
.app[data-screen="menu"] .screen-menu,
.app[data-screen="levels"] .screen-levels,
.app[data-screen="settings"] .screen-settings,
.app[data-screen="game"] .screen-game { display: flex; animation: screenIn .22s ease-out; }

@keyframes screenIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ---------- Buttons ---------- */
.btn {
  --face: var(--secondary); --edge: var(--secondary-edge); --txt: var(--ink);
  font-family: inherit; font-weight: 800; letter-spacing: .02em;
  color: var(--txt); background: var(--face);
  border: 0; border-radius: 16px;
  box-shadow: 0 5px 0 var(--edge), 0 8px 18px rgba(0,0,0,.12);
  min-height: 48px; min-width: 48px;
  padding: 0 18px;
  display: inline-flex; align-items: center; justify-content: center; gap: 10px;
  cursor: pointer;
  transition: transform .08s ease, box-shadow .08s ease, filter .15s ease;
  touch-action: manipulation;
  position: relative;
}
.btn:hover { filter: brightness(1.05); transform: translateY(-1px); box-shadow: 0 6px 0 var(--edge), 0 10px 22px rgba(0,0,0,.14); }
.btn:active, .btn.pressed { transform: translateY(4px); box-shadow: 0 1px 0 var(--edge), 0 3px 8px rgba(0,0,0,.12); }
.btn:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; }
.btn[disabled] { opacity: .45; pointer-events: none; }
.btn[hidden] { display: none; }

.btn-primary { --face: var(--primary); --edge: var(--primary-edge); --txt: var(--primary-ink); }
.btn-secondary { --face: var(--secondary); --edge: var(--secondary-edge); }
.btn-danger { --face: var(--danger); --edge: var(--danger-edge); --txt: #fff; }

.btn-xl { min-height: 74px; font-size: 26px; border-radius: 22px; width: 100%; }
.btn-lg { min-height: 58px; font-size: 20px; width: 100%; }
.btn-md { min-height: 50px; font-size: 16px; }
.btn-round { width: 48px; height: 48px; padding: 0; border-radius: 15px; font-size: 22px; }
.btn-icon { font-size: 1.05em; line-height: 1; }
.btn-label { display: flex; flex-direction: column; align-items: center; line-height: 1.05; }
.btn-label small { font-size: 13px; font-weight: 700; opacity: .7; letter-spacing: .04em; margin-top: 3px; }

/* ---------- Menu ---------- */
.screen-menu { align-items: center; justify-content: center; }
.menu-inner {
  width: min(420px, 100%); max-height: 100%;
  display: flex; flex-direction: column; align-items: center; gap: clamp(10px, 2.6vh, 22px);
}
.logo { display: flex; align-items: center; gap: 14px; }
.logo-sign {
  width: clamp(58px, 9vh, 78px); aspect-ratio: 1; border-radius: 16px;
  background: var(--accent); color: #fff;
  display: grid; place-items: center;
  font-size: clamp(38px, 6vh, 52px); font-weight: 900;
  box-shadow: inset 0 0 0 4px rgba(255,255,255,.9), 0 6px 0 #0f4f78, var(--shadow);
  transform: rotate(-6deg);
}
.logo-text { margin: 0; display: flex; flex-direction: column; line-height: .9; font-size: clamp(34px, 6.2vh, 50px); font-weight: 900; letter-spacing: .01em; }
.logo-escape { color: var(--red); text-shadow: 0 3px 0 rgba(0,0,0,.12); }
.menu-preview { width: min(300px, 78vw, 34vh); aspect-ratio: 1.109; filter: drop-shadow(0 10px 18px rgba(29,39,64,.22)); }
.menu-buttons { width: 100%; display: flex; flex-direction: column; gap: 14px; }
.menu-progress { margin: 0; color: var(--ink-soft); font-weight: 700; font-size: 14px; }

/* ---------- Top bars ---------- */
.topbar { display: flex; align-items: center; gap: 12px; width: 100%; max-width: 760px; margin: 0 auto; }
.topbar-title { flex: 1; margin: 0; font-size: 24px; font-weight: 900; }
.topbar-spacer { width: 48px; }
.topbar-chip { background: var(--card); padding: 10px 14px; border-radius: 999px; font-weight: 800; box-shadow: var(--shadow); color: var(--ink); }
.topbar-chip::first-letter { color: var(--star); }

/* ---------- Level select ---------- */
.screen-levels, .screen-settings { flex-direction: column; gap: 12px; }
.levels-scroll {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  touch-action: pan-y; overscroll-behavior: contain;
  width: 100%; max-width: 760px; margin: 0 auto; padding: 4px 4px 24px;
}
.level-group { margin-bottom: 18px; }
.level-group h3 { margin: 6px 4px 10px; font-size: 15px; font-weight: 900; letter-spacing: .08em; text-transform: uppercase; color: var(--ink-soft); display: flex; align-items: center; gap: 8px; }
.level-group h3 i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.level-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); gap: 12px; }
.level-btn {
  aspect-ratio: 1; min-height: 64px; padding: 4px; border-radius: 16px;
  flex-direction: column; gap: 2px; font-size: 22px;
}
.level-btn .lv-stars { font-size: 13px; letter-spacing: 1px; color: var(--star-off); }
.level-btn .lv-stars .on { color: var(--star); }
.level-btn.current { --face: var(--primary); --edge: var(--primary-edge); --txt: var(--primary-ink); }
.level-btn.locked { --face: var(--card); opacity: .55; font-size: 18px; cursor: default; }
.level-btn.locked:hover { transform: none; }

/* ---------- Settings ---------- */
.settings-body { width: 100%; max-width: 520px; margin: 0 auto; display: flex; flex-direction: column; gap: 12px; overflow-y: auto; touch-action: pan-y; padding-bottom: 20px; }
.setting-row, .setting-card { background: var(--card); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.setting-row { display: flex; align-items: center; gap: 12px; min-height: 64px; cursor: pointer; }
.setting-text { flex: 1; display: flex; flex-direction: column; }
.setting-text small { color: var(--ink-soft); font-weight: 600; }
.how-to ul { margin: 8px 0 0; padding-left: 20px; line-height: 1.5; color: var(--ink-soft); font-weight: 600; }
.how-to li { margin-bottom: 4px; }
.red-word { color: var(--red); font-weight: 900; }
kbd { background: var(--bg); border-radius: 6px; padding: 1px 6px; font-family: inherit; font-weight: 800; color: var(--ink); border: 1px solid var(--card-edge); }
.version { text-align: center; color: var(--ink-soft); font-size: 12px; margin: 4px; }

.toggle {
  appearance: none; -webkit-appearance: none; flex: none;
  width: 58px; height: 34px; border-radius: 999px; background: var(--star-off);
  position: relative; cursor: pointer; transition: background .2s;
}
.toggle::after {
  content: ""; position: absolute; top: 4px; left: 4px; width: 26px; height: 26px; border-radius: 50%;
  background: #fff; box-shadow: 0 2px 4px rgba(0,0,0,.25); transition: transform .2s;
}
.toggle:checked { background: #33b864; }
.toggle:checked::after { transform: translateX(24px); }
.toggle:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; }

/* ---------- Gameplay (portrait default) ---------- */
.screen-game {
  display: none;
  flex-direction: column; align-items: stretch; gap: 8px;
}
.app[data-screen="game"] .screen-game { display: grid; grid-template-rows: auto auto 1fr auto; grid-template-columns: 100%; }

.hud { display: flex; align-items: center; gap: 10px; width: 100%; max-width: 560px; margin: 0 auto; }
.hud-center { flex: 1; display: flex; flex-direction: column; align-items: center; line-height: 1; }
.hud-level { font-size: 22px; font-weight: 900; }
.hud-stars { font-size: 20px; letter-spacing: 2px; margin-top: 3px; }
.star { color: var(--star-off); transition: color .25s, transform .25s; display: inline-block; }
.star.on { color: var(--star); text-shadow: 0 1px 0 rgba(0,0,0,.15); }
.star.lost { animation: starLost .4s ease; }
@keyframes starLost { 50% { transform: scale(1.4) rotate(-15deg); } }

.hud-stats { display: flex; justify-content: center; gap: 8px; width: 100%; max-width: 560px; margin: 0 auto; }
.stat {
  flex: 1; background: var(--card); border-radius: 14px; padding: 6px 8px;
  display: flex; flex-direction: column; align-items: center; box-shadow: 0 3px 0 var(--card-edge);
}
.stat-label { font-size: 11px; font-weight: 800; text-transform: uppercase; letter-spacing: .08em; color: var(--ink-soft); }
.stat-value { font-size: 20px; font-weight: 900; font-variant-numeric: tabular-nums; }
.stat-value.bump { animation: bump .2s ease; }
@keyframes bump { 50% { transform: scale(1.25); } }

.board-wrap { position: relative; min-height: 0; min-width: 0; display: flex; align-items: center; justify-content: center; }
.board { display: block; touch-action: none; outline: none; border-radius: 20px; }
.board:focus-visible { box-shadow: 0 0 0 3px var(--accent); }

.tutorial {
  position: absolute; left: 50%; bottom: 4px; transform: translateX(-50%);
  background: rgba(29,39,64,.92); color: #fff; font-weight: 800; font-size: 15px;
  padding: 10px 18px; border-radius: 18px; pointer-events: none; text-align: center; text-wrap: balance;
  box-shadow: var(--shadow); animation: tutIn .35s ease-out;
  width: max-content; max-width: min(92%, 420px);
}
@keyframes tutIn { from { opacity: 0; transform: translate(-50%, 10px); } }

.actions { display: flex; gap: 10px; width: 100%; max-width: 560px; margin: 0 auto; }
.btn-action { flex: 1; min-height: 58px; font-size: 17px; flex-direction: row; }
.btn-action .btn-icon { font-size: 22px; }
.btn-hint { --face: var(--primary); --edge: var(--primary-edge); --txt: var(--primary-ink); }
.badge {
  position: absolute; top: -8px; right: -6px; min-width: 24px; height: 24px; padding: 0 6px;
  border-radius: 999px; background: var(--red); color: #fff; font-size: 13px; font-weight: 900;
  display: grid; place-items: center; box-shadow: 0 2px 0 rgba(0,0,0,.25);
}
.badge.plus { background: #33b864; }
.badge.empty { background: var(--star-off); }

/* Rewarded "Skip level": appears only for stuck players, quieter than Hint */
.btn-skip { --face: var(--card); --edge: var(--card-edge); }
.btn-skip.arrive { animation: skipIn .45s cubic-bezier(.2, 1.5, .4, 1); }
@keyframes skipIn { from { transform: scale(.6); opacity: 0; } }

/* While an ad is on screen the game ignores input */
.app.ad-playing { pointer-events: none; }

.level-btn.skipped .lv-stars::after { content: " ⏭"; color: var(--ink-soft); letter-spacing: 0; }

/* ---------- Landscape gameplay: panels beside the board ---------- */
@media (min-aspect-ratio: 5/4) {
  .app[data-screen="game"] .screen-game {
    grid-template-columns: minmax(150px, 1fr) minmax(0, calc((100vh - 20px) * 1.12)) minmax(150px, 1fr);
    grid-template-columns: minmax(150px, 1fr) minmax(0, calc((100dvh - 20px) * 1.12)) minmax(150px, 1fr);
    grid-template-rows: auto 1fr auto;
    grid-template-areas: "hud board actions" "stats board actions" ". board actions";
    column-gap: clamp(12px, 3vw, 48px);
  }
  .hud { grid-area: hud; flex-wrap: wrap; justify-content: center; max-width: 260px; justify-self: end; }
  .hud-center { order: -1; flex-basis: 100%; margin-bottom: 8px; }
  .hud-level { font-size: 28px; }
  .hud-stars { font-size: 26px; }
  .hud-stats { grid-area: stats; flex-direction: column; max-width: 260px; justify-self: end; width: 100%; align-self: start; margin-top: 8px; }
  .stat { flex: none; flex-direction: row; justify-content: space-between; padding: 10px 14px; }
  .board-wrap { grid-area: board; }
  .actions { grid-area: actions; flex-direction: column; justify-content: center; max-width: 220px; justify-self: start; width: 100%; }
  .btn-action { flex: none; min-height: 60px; }
  .tutorial {
    position: fixed; left: calc(var(--safe-l) + 16px); bottom: calc(var(--safe-b) + 16px); transform: none;
    white-space: normal; max-width: min(260px, calc(50vw - 40px)); border-radius: 16px; animation: none;
  }
}
/* very short landscape phones: tighten */
@media (min-aspect-ratio: 5/4) and (max-height: 500px) {
  .hud-level { font-size: 20px; } .hud-stars { font-size: 18px; }
  .hud-center { margin-bottom: 4px; }
  .stat { padding: 4px 10px; } .stat-value { font-size: 17px; }
  .btn-action { min-height: 48px; font-size: 15px; }
  .actions { gap: 8px; }
}
@media (max-aspect-ratio: 5/4) and (max-width: 430px) {
  .actions:has(.btn-skip:not([hidden])) .btn-action { padding: 0 8px; gap: 6px; font-size: 15px; }
}
/* small portrait phones */
@media (max-aspect-ratio: 5/4) and (max-height: 700px) {
  .stat { padding: 3px 6px; } .stat-value { font-size: 17px; }
  .btn-action { min-height: 52px; }
}

/* ---------- Modals ---------- */
.modal {
  position: absolute; inset: 0; z-index: 20;
  display: grid; place-items: center; padding: 16px;
  background: rgba(13, 19, 34, .55);
  backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px);
  animation: fadeIn .2s ease-out;
  touch-action: none;
}
.modal[hidden] { display: none; }
@keyframes fadeIn { from { opacity: 0; } }
.modal-card {
  width: min(360px, 100%); max-height: 100%; overflow: auto;
  background: var(--card); border-radius: 26px; padding: 24px 20px 22px;
  box-shadow: 0 8px 0 var(--card-edge), 0 24px 60px rgba(0,0,0,.35);
  display: flex; flex-direction: column; align-items: center; gap: 14px;
  animation: popIn .32s cubic-bezier(.2, 1.4, .4, 1);
}
@keyframes popIn { from { transform: scale(.7); opacity: 0; } }
.modal-title { margin: 0; font-size: 26px; font-weight: 900; }
.modal-text { margin: 0; text-align: center; color: var(--ink-soft); font-weight: 600; }
.modal-buttons { display: flex; flex-direction: column; gap: 12px; width: 100%; }
.modal-row { display: flex; gap: 10px; width: 100%; }
.modal-row .btn { flex: 1; }

.win-card { padding-top: 0; overflow: visible; margin-top: 24px; }
.win-ribbon {
  margin-top: -24px;
  background: var(--red); color: #fff; font-weight: 900; font-size: 34px; letter-spacing: .06em;
  padding: 8px 34px; border-radius: 16px;
  box-shadow: 0 5px 0 #a8211f, 0 10px 20px rgba(0,0,0,.2);
  transform: rotate(-3deg);
}
.win-stars { display: flex; gap: 6px; font-size: 58px; line-height: 1; margin-top: 4px; }
.wstar { color: var(--star-off); transform: scale(0); display: inline-block; }
.wstar:nth-child(2) { transform: scale(0) translateY(-10px); font-size: 1.2em; }
.wstar.show { animation: starPop .45s cubic-bezier(.2, 1.6, .4, 1) forwards; }
.wstar.earned { color: var(--star); text-shadow: 0 3px 0 #d99a0b, 0 0 18px rgba(255,191,31,.6); }
.wstar:nth-child(2).show { animation-name: starPopMid; }
@keyframes starPop { from { transform: scale(0) rotate(-40deg); } to { transform: scale(1) rotate(0); } }
@keyframes starPopMid { from { transform: scale(0) translateY(-10px) rotate(-40deg); } to { transform: scale(1) translateY(-10px); } }
.win-stats { display: flex; gap: 10px; width: 100%; }
.win-stats div { flex: 1; background: var(--bg); border-radius: 14px; padding: 8px; display: flex; flex-direction: column; align-items: center; }
.win-stats span { font-size: 11px; font-weight: 800; text-transform: uppercase; letter-spacing: .08em; color: var(--ink-soft); }
.win-stats b { font-size: 22px; font-variant-numeric: tabular-nums; }
.win-note { margin: 0; min-height: 1.2em; font-weight: 700; color: var(--ink-soft); text-align: center; font-size: 14px; }

/* ---------- Toast ---------- */
.toast {
  position: absolute; left: 50%; top: calc(var(--safe-t) + 14px); z-index: 40;
  transform: translate(-50%, calc(-100% - 40px)); opacity: 0;
  background: var(--ink); color: var(--bg); font-weight: 800;
  padding: 12px 18px; border-radius: 14px; box-shadow: var(--shadow);
  transition: transform .25s ease, opacity .25s; pointer-events: none; max-width: 92vw; text-align: center;
}
.toast.show { transform: translate(-50%, 0); opacity: 1; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
  .wstar { transform: scale(1) !important; }
}
