:root {
  --sky: #E7EEF4;
  --water: #9FD8DC;
  --bank: #C9B293;
  --navy: #1B2E4B;
  --amber: #F2B630;
  --amber-line: #D99A1E;
  --sub: #56606C;
  --tray: #DDE3E8;
  --white: #FFFFFF;
  --font: 'Outfit', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --mono: 'JetBrains Mono', ui-monospace, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--sky);
  color: var(--navy);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  touch-action: manipulation; /* no double-tap zoom anywhere */
}

body { display: flex; justify-content: center; }

button { font-family: inherit; cursor: pointer; }
button:focus-visible { outline: 3px solid var(--amber); outline-offset: 2px; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.app {
  position: relative;
  width: 100%;
  max-width: 480px;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--sky);
}

/* Header */
.header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(14px + env(safe-area-inset-top)) 16px 8px 20px;
  position: relative;
  z-index: 3;
}
.logo {
  margin: 0 0 0 -0.035em;
  font-size: 30px;
  font-weight: 800;
  letter-spacing: -0.015em;
  line-height: 1;
}
.keystone {
  background: linear-gradient(var(--amber), var(--amber)) no-repeat;
  background-size: 100% 0.62em;
  background-position: 0 0.36em;
  border-radius: 0.12em;
}
.meta { margin: 4px 0 0; font-size: 12px; font-weight: 500; color: var(--sub); }
.header-actions { display: flex; gap: 8px; }
.round-btn {
  width: 44px; height: 44px;
  border: 0; border-radius: 50%;
  background: var(--navy); color: var(--white);
  display: grid; place-items: center;
  font-size: 18px; font-weight: 700; padding: 0;
}

/* Scene */
.scene { position: relative; flex: 1 1 auto; min-height: 0; }
#sceneCanvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }

.clues {
  position: absolute; left: 16px; right: 16px; top: clamp(16px, calc(8.5vh - 8px), 64px);
  display: flex; flex-direction: column; gap: 10px;
  pointer-events: none;
  z-index: 2;
  transition: transform 0.9s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.7s ease;
}
.clues.leaving { transform: translateY(-70px); opacity: 0; }
.clues.leaving .clue-after { transition-delay: 0.08s; }
.clue {
  margin: 0;
  max-width: 80%;
  padding: 12px 16px;
  background: var(--navy);
  color: var(--white);
  font-size: 16px;
  font-weight: 500;
  line-height: 1.3;
}
.clue-before { align-self: flex-start; border-radius: 16px 16px 16px 4px; }
.clue-after { align-self: flex-end; border-radius: 16px 16px 4px 16px; text-align: right; }

.toast {
  position: absolute; left: 50%; top: 180px; /* moved between the clues and the bridge by app.js */
  transform: translate(-50%, -10px);
  opacity: 0;
  width: max-content;
  max-width: calc(100% - 32px);
  padding: 9px 18px;
  border-radius: 999px;
  background: var(--navy); color: var(--white);
  font-size: 14px; font-weight: 600; text-align: center;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.25s ease;
  z-index: 4;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.hint-btn {
  position: absolute; right: 16px; bottom: 10px;
  height: 44px; padding: 0 20px 0 16px;
  border: 0; border-radius: 999px;
  background: var(--navy); color: var(--white);
  display: flex; align-items: center; gap: 8px;
  font-size: 15px; font-weight: 600;
  z-index: 2;
}
.hint-btn:active { transform: translateY(1px); }
.hint-btn[hidden] { display: none; }

/* Keyboard */
.keyboard {
  flex: 0 0 auto;
  background: var(--tray);
  padding: 8px 4px calc(16px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 8px;
  touch-action: manipulation;
  user-select: none; -webkit-user-select: none;
  position: relative; z-index: 3;
}
.kb-row { display: flex; gap: 6px; }
.kb-row.middle { padding: 0 18px; }
.key {
  touch-action: manipulation;
  flex: 1;
  height: 48px;
  border: 0; border-radius: 6px;
  background: var(--white); color: var(--navy);
  font-size: 19px; font-weight: 500;
  padding: 0;
  box-shadow: 0 1px 0 rgba(27, 46, 75, 0.16);
}
.key:active { transform: translateY(1px); background: #F2F5F8; }
.key.wide { flex: 1.6; font-size: 13px; font-weight: 700; letter-spacing: 0.02em; }
.key.enter { background: var(--navy); color: var(--white); }
.key.enter:active { background: #24395C; }
.key.delete { background: var(--bank); display: grid; place-items: center; }

/* Results */
.results {
  position: absolute; left: 0; right: 0; bottom: 0;
  background: var(--white);
  border-radius: 24px 24px 0 0;
  padding: 20px 16px calc(20px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; align-items: center; gap: 12px;
  transform: translateY(105%);
  transition: transform 0.5s cubic-bezier(0.2, 0.8, 0.2, 1);
  z-index: 5;
  box-shadow: 0 -6px 24px rgba(27, 46, 75, 0.08);
}
.results.show { transform: translateY(0); }
.seam-line {
  margin: 0;
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap; justify-content: center;
  font-family: var(--mono); font-size: 20px; font-weight: 700;
}
.seam-line .plus { font-family: var(--font); font-weight: 500; color: var(--sub); }
mark { background: var(--amber); color: var(--navy); padding: 0 2px; border-radius: 3px; }
.stat-row { width: 100%; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
.stat {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 12px 6px; border-radius: 14px; background: var(--sky);
}
.stat-num { font-size: 24px; font-weight: 700; }
.stat-label { font-size: 12px; color: var(--sub); }
.share-btn, .primary-btn {
  width: 100%; height: 52px;
  border: 0; border-radius: 999px;
  background: var(--navy); color: var(--white);
  font-size: 17px; font-weight: 700;
}
.countdown { margin: 0; font-size: 13px; font-weight: 500; color: var(--sub); }
.countdown span { font-weight: 700; color: var(--navy); font-variant-numeric: tabular-nums; }

/* Dialogs */
.sheet {
  width: min(440px, calc(100% - 24px));
  max-height: calc(100dvh - 48px);
  border: 0; border-radius: 24px;
  padding: 20px 20px 24px;
  background: var(--white); color: var(--navy);
  box-shadow: 0 20px 60px rgba(27, 46, 75, 0.25);
}
.sheet::backdrop { background: rgba(27, 46, 75, 0.45); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.sheet h2 { margin: 0; font-size: 22px; font-weight: 800; }
.sheet p, .sheet li { font-size: 15px; line-height: 1.5; }
.sheet ul { padding-left: 20px; margin: 12px 0 20px; }
.sheet li { margin-bottom: 6px; }
.close-btn {
  width: 44px; height: 44px; border: 0; border-radius: 50%;
  background: var(--sky); color: var(--navy);
  display: grid; place-items: center;
}
/* First-visit intro (also reopened by the ? button) */
.intro {
  position: absolute; inset: 0;
  z-index: 10;
  background: var(--sky);
  display: flex; flex-direction: column;
}
.intro[hidden] { display: none; }
.intro-body {
  flex: 1 1 auto; overflow-y: auto;
  padding: calc(28px + env(safe-area-inset-top)) 24px 12px;
}
.intro .intro-logo {
  margin: 0 0 18px -0.035em;
  font-size: 40px; font-weight: 800; letter-spacing: -0.015em; line-height: 1;
}
.intro-title { margin: 0 0 8px; font-size: 24px; font-weight: 800; }
.intro p, .intro li { font-size: 16px; line-height: 1.5; }
.intro p { margin: 0 0 16px; }
.example {
  margin: 4px 0 18px;
  padding: 20px 14px 14px;
  border-radius: 20px;
  background: var(--white);
}
.ex-arch { display: flex; justify-content: center; gap: 3px; height: 62px; align-items: flex-start; }
.ex-tile {
  width: 26px; height: 34px; border-radius: 4px;
  background: var(--navy); color: var(--white);
  display: grid; place-items: center;
  font-family: var(--mono); font-weight: 700; font-size: 15px;
}
.ex-tile.seam { background: var(--amber); color: var(--navy); }
.ex-words {
  margin: 6px 0 0 !important;
  display: flex; justify-content: center; align-items: center; gap: 10px;
  font-family: var(--mono); font-weight: 700; font-size: 16px;
}
.ex-op { font-family: var(--font); font-weight: 500; color: var(--sub); }
.rules { list-style: none; padding: 0; margin: 0 0 16px; display: flex; flex-direction: column; gap: 10px; }
.rules li { padding-left: 18px; position: relative; }
.rules li::before {
  content: ''; position: absolute; left: 0; top: 0.55em;
  width: 8px; height: 8px; border-radius: 2px; background: var(--amber);
}
.intro-foot { color: var(--sub); font-size: 14px !important; }
.intro-actions { flex: 0 0 auto; padding: 12px 20px calc(20px + env(safe-area-inset-bottom)); }
.stats-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; margin-top: 8px; }
.stats-grid .stat { padding: 14px 6px; }
.stats-note { margin: 14px 0 0; font-size: 13px !important; color: var(--sub); text-align: center; }

@media (prefers-reduced-motion: reduce) {
  .results, .toast { transition: none; }
}
