* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  width: 100%;
  height: 100%;
  min-height: 100%;
  overflow: hidden;
  background: #0f3b57;              /* deep water, in case the canvas ever lets through */
  touch-action: none;              /* no scroll/zoom on touch devices */
  overscroll-behavior: none;       /* no pull-to-refresh */
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

#game {
  position: fixed;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.run-control {
  position: fixed;
  z-index: 1;
  top: max(0.5rem, calc(env(safe-area-inset-top) + 0.5rem));
  width: 74px;
  min-width: 74px;
  min-height: 44px;
  padding: 0.45rem 0.55rem;
  border: 2px solid rgba(255, 212, 94, 0.9);
  border-radius: 12px;
  background: rgba(6, 24, 36, 0.9);
  color: #ffd45e;
  font: inherit;
  font-weight: 800;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
}
.run-control:hover { background: rgba(15, 59, 87, 0.96); }
.run-control:focus-visible { outline: 4px solid #7ee0ff; outline-offset: 3px; }
.run-control.hidden { display: none; }

#rotate-note {
  display: none;
  position: fixed;
  z-index: 3;
  left: 50%;
  bottom: max(0.5rem, env(safe-area-inset-bottom));
  width: min(calc(100% - 1rem), 22rem);
  transform: translateX(-50%);
  border: 1px solid rgba(255, 212, 94, 0.8);
  border-radius: 999px;
  padding: 0.45rem 0.8rem;
  background: rgba(6, 24, 36, 0.9);
  color: #ffd45e;
  font-size: 0.82rem;
  font-weight: 700;
  text-align: center;
  pointer-events: none;
}
#rotate-note.safe-harbor-hidden { display: none; }

@media (orientation: landscape) and (max-height: 360px) {
  #rotate-note { display: block; }
}

/* Start / game-over screen, drawn over the canvas. */
#overlay {
  position: fixed;
  z-index: 2;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
  padding: 1.25rem 1rem;
  gap: 0.6rem;
  text-align: center;
  background: rgba(6, 24, 36, 0.82);
  color: #eaf2f8;
  user-select: none;
}
#overlay.hidden { display: none; }
#overlay.outcome-mode {
  justify-content: flex-start;
  background: rgba(6, 24, 36, 0.97);
}
#overlay > * { min-width: 0; max-width: 100%; flex-shrink: 0; }
#overlay h1 { font-size: 2.6rem; letter-spacing: 0.02em; }
#overlay h2 { font-size: 1.6rem; color: #ffd45e; }
#overlay p { font-size: 1.05rem; opacity: 0.92; max-width: 32rem; line-height: 1.5; }
#overlay .stat { font-size: 1.15rem; opacity: 0.95; }
#overlay .stat b { color: #ffd45e; }
#overlay .hint { margin-top: 0.4rem; opacity: 0.7; font-size: 0.92rem; }

.start-btn {
  font: inherit;
  font-weight: 700;
  font-size: 1.15rem;
  color: #06202f;
  background: #ffd45e;
  border: none;
  border-radius: 12px;
  padding: 0.7rem 1.6rem;
  margin-top: 0.5rem;
  min-height: 44px; cursor: pointer; touch-action: manipulation;
}
.start-btn:hover { background: #ffe08a; }
.start-btn:focus-visible { outline: 4px solid #7ee0ff; outline-offset: 3px; }
.start-btn:disabled { opacity: 0.4; cursor: default; }
.launchers {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 0.5rem;
  width: min(100%, 36rem);
}
.launchers .start-btn { margin-top: 0; }


/* Profile picker (#14) */
#overlay h3.who { margin-top: 0.3rem; font-size: 1.02rem; opacity: 0.8; }
.profiles { width: min(100%, 34rem); min-width: 0; display: flex; flex-wrap: wrap; gap: 0.5rem; justify-content: center; }
.chip {
  font: inherit; display: flex; flex-direction: column; align-items: center; gap: 0.15rem;
  padding: 0.5rem 0.9rem; border-radius: 12px; border: 2px solid rgba(255, 255, 255, 0.25);
  min-width: 0; min-height: 44px; max-width: 100%; background: rgba(255, 255, 255, 0.06); color: #eaf2f8; cursor: pointer; touch-action: manipulation;
}
.chip.sel { border-color: #ffd45e; background: rgba(255, 212, 94, 0.12); }
.chip .sub { font-size: 0.78rem; opacity: 0.75; }
.chip .profile-level-best { opacity: 0.92; }
.chip .profile-level-best b { color: #b8f2ff; }
.chip.sel .profile-level-best b { color: #ffd45e; }
.dot { display: inline-block; width: 0.8em; height: 0.8em; border-radius: 50%; margin-right: 0.35em; }
.guest-dot { box-sizing: border-box; background: transparent; border: 2px solid #c9d4dc; }
.mini-btn {
  font: inherit; font-size: 0.95rem; color: #eaf2f8; background: rgba(255, 255, 255, 0.1);
  min-height: 44px; border: 1px solid rgba(255, 255, 255, 0.3); border-radius: 10px; padding: 0.35rem 0.9rem; cursor: pointer; touch-action: manipulation;
}
.mini-btn.gold { background: #ffd45e; color: #06202f; font-weight: 700; border: none; }
.new-form {
  display: flex; flex-direction: column; gap: 0.55rem; align-items: center;
  background: rgba(255, 255, 255, 0.05); border-radius: 14px; padding: 0.8rem 1rem;
}
.new-form.hidden { display: none; }
.new-form input {
  font: inherit; padding: 0.4rem 0.7rem; border-radius: 8px; text-align: center;
  border: 1px solid rgba(255, 255, 255, 0.35); background: rgba(6, 24, 36, 0.6); color: #eaf2f8;
}
.form-error { width: 100%; min-height: 1.2em; margin: 0; color: #ffd4d4; font-size: 0.88rem; }
.form-error:empty, .start-status:empty { display: none; }
.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;
}
.colors { display: flex; flex-wrap: wrap; justify-content: center; max-width: 100%; gap: 0.45rem; }
.color-dot { width: 2.75rem; height: 2.75rem; border-radius: 50%; border: 2px solid transparent; cursor: pointer; touch-action: manipulation; }
.color-dot.sel { border-color: #fff; }
.tiers { width: 100%; min-width: 0; display: flex; flex-wrap: wrap; gap: 0.4rem; justify-content: center; }
.tiers.small { gap: 0.3rem; align-items: center; }
.tiers.small .tier-btn { min-height: 44px; padding: 0.25rem 0.55rem; font-size: 0.85rem; }
.tier-lbl { font-size: 0.85rem; opacity: 0.75; margin-right: 0.2rem; }
.tier-btn {
  font: inherit; display: flex; flex-direction: column; padding: 0.4rem 0.7rem; border-radius: 10px;
  min-width: 44px; min-height: 44px; border: 2px solid rgba(255, 255, 255, 0.25); background: rgba(255, 255, 255, 0.06); color: #eaf2f8; cursor: pointer; touch-action: manipulation;
}
.tier-btn.sel { border-color: #7ee0ff; background: rgba(126, 224, 255, 0.12); }
.tier-btn span { font-size: 0.72rem; opacity: 0.7; }
.tier-btn:focus-visible, .mini-btn:focus-visible, .chip:focus-visible,
.color-dot:focus-visible, .data-link:focus-visible {
  outline: 4px solid #7ee0ff; outline-offset: 3px;
}
.datalinks { font-size: 0.8rem; opacity: 0.65; }
.data-link {
  appearance: none; display: inline-flex; align-items: center; justify-content: center;
  min-width: 44px; min-height: 44px; padding: 0 0.25rem; border: 0;
  font: inherit; color: #7ee0ff;
  background: transparent; cursor: pointer; text-decoration: underline;
}
#overlay .stat.sub { font-size: 0.95rem; opacity: 0.75; }
.guest-settings {
  width: min(100%, 36rem);
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
  padding: 0.55rem;
  border: 1px solid rgba(126, 224, 255, 0.3);
  border-radius: 12px;
  background: rgba(6, 24, 36, 0.38);
}
#overlay .guest-note { margin: 0; font-size: 0.8rem; opacity: 0.72; }

/* Between-wave build choice: three real buttons cover pointer, touch, and keyboard. */
.upgrade-dialog {
  width: min(100%, 56rem);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.65rem;
}
.intermission-dialog {
  width: min(100%, 32rem);
  min-height: min(20rem, 100%);
  margin-block: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.65rem;
}
.harbor-dialog { width: min(100%, 38rem); }
.intermission-dialog .start-btn { min-width: 9rem; }
#overlay .chapter-review-gate {
  max-width: none;
  margin: 0.1rem 0 -0.1rem;
  color: #ffd45e;
  font-size: 0.84rem;
  font-weight: 800;
  line-height: 1.3;
  text-align: center;
}
#overlay .chapter-review-gate.is-unlocked { color: #8df0a0; }
.chapter-review {
  width: 100%;
  min-width: 0;
  border: 1px solid rgba(126, 224, 255, 0.45);
  border-radius: 14px;
  background: rgba(6, 24, 36, 0.72);
  color: #eaf2f8;
  text-align: left;
}
.chapter-review-toggle {
  min-height: 44px;
  min-width: 0;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 0.5rem;
  padding: 0.65rem 0.8rem;
  color: #ffd45e;
  cursor: pointer;
  touch-action: manipulation;
  overflow-wrap: anywhere;
}
.chapter-review-toggle { list-style: none; }
.chapter-review-toggle::-webkit-details-marker { display: none; }
.chapter-review-toggle::before {
  content: "\25B8";
  color: #7ee0ff;
  font-size: 1.2rem;
  line-height: 1;
  transition: transform 120ms ease-out;
}
.chapter-review[open] .chapter-review-toggle::before { transform: rotate(90deg); }
.chapter-review-toggle:focus-visible { outline: 4px solid #7ee0ff; outline-offset: 3px; }
.chapter-review-title { font-weight: 800; }
.chapter-review-summary { color: #b8f2ff; font-size: 0.78rem; font-weight: 700; text-align: right; }
#overlay .chapter-review-intro {
  max-width: none;
  margin: 0;
  padding: 0.55rem 0.8rem 0.65rem;
  font-size: 0.82rem;
  opacity: 0.78;
}
.chapter-review-list {
  width: 100%;
  min-width: 0;
  display: grid;
  gap: 0.55rem;
  padding: 0 0.65rem 0.7rem;
  list-style: none;
}
.chapter-review-item {
  min-width: 0;
  padding: 0.65rem;
  border: 2px solid rgba(184, 242, 255, 0.42);
  border-radius: 12px;
  background: rgba(15, 59, 87, 0.78);
  overflow-wrap: anywhere;
}
.chapter-review-item.is-right { border-color: rgba(141, 240, 160, 0.78); }
.chapter-review-item.is-wrong { border-color: rgba(255, 154, 154, 0.88); }
.chapter-review-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.5rem;
  font-size: 0.82rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.035em;
}
.chapter-review-identity { color: #b8f2ff; }
.chapter-review-item.is-right .chapter-review-status { color: #8df0a0; }
.chapter-review-item.is-wrong .chapter-review-status { color: #ffb3b3; }
#overlay .chapter-review-prompt {
  max-width: none;
  margin: 0.45rem 0;
  color: #fff;
  font-size: 1rem;
  font-weight: 800;
  line-height: 1.3;
}
.chapter-review-answers {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.45rem;
  margin: 0;
}
.chapter-review-answers > div {
  min-width: 0;
  padding: 0.45rem;
  border-radius: 9px;
  background: rgba(6, 24, 36, 0.48);
}
.chapter-review-answers dt {
  color: #c9d4dc;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
}
.chapter-review-answers dd {
  margin-top: 0.12rem;
  color: #fff;
  font-size: 0.9rem;
  font-weight: 800;
}
#overlay .chapter-review-explanation,
#overlay .chapter-review-link {
  max-width: none;
  margin: 0.45rem 0 0;
  font-size: 0.82rem;
  line-height: 1.35;
}
#overlay .chapter-review-explanation b { color: #ffd45e; }
#overlay .chapter-review-link {
  padding-top: 0.4rem;
  border-top: 1px solid rgba(184, 242, 255, 0.22);
  color: #b8f2ff;
  font-weight: 700;
}
.upgrade-help { margin: 0; }
.upgrade-grid {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.75rem;
}
.upgrade-grid.cards-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); max-width: 38rem; }
.upgrade-grid.cards-1 { grid-template-columns: minmax(0, 19rem); justify-content: center; }
.upgrade-card {
  min-width: 0;
  min-height: 9.5rem;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.5rem;
  padding: 0.85rem;
  border: 2px solid rgba(126, 224, 255, 0.7);
  border-radius: 14px;
  background: rgba(15, 59, 87, 0.94);
  color: #eaf2f8;
  text-align: left;
  font: inherit;
  cursor: pointer;
  touch-action: manipulation;
}
.upgrade-card:hover { border-color: #ffd45e; background: rgba(22, 78, 111, 0.98); }
.upgrade-card:focus-visible { outline: 4px solid #ffd45e; outline-offset: 3px; }
.upgrade-meta {
  width: 100%; display: flex; justify-content: space-between; align-items: center;
  color: #b8f2ff; font-size: 0.78rem; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.04em;
}
.upgrade-key {
  display: inline-grid; place-items: center; width: 1.55rem; height: 1.55rem;
  border-radius: 50%; background: #ffd45e; color: #06202f;
}
.upgrade-title { color: #ffd45e; font-size: 1.2rem; line-height: 1.1; }
.upgrade-rank-label {
  color: #b8f2ff; font-size: 0.74rem; font-weight: 800; text-transform: uppercase;
  letter-spacing: 0.04em;
}
.upgrade-consequence { font-size: 0.98rem; line-height: 1.3; }
.upgrade-stack { margin-top: auto; color: #b8f2ff; font-size: 0.82rem; font-weight: 700; }
.build-complete { color: #8df0a0; }
.danger-btn { border-color: rgba(255, 154, 154, 0.8); color: #ffd4d4; }

.outcome-report {
  width: min(100%, 46rem);
  min-width: 0;
  margin-block: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.65rem;
}
.outcome-report h3 { margin-top: 0.25rem; color: #b8f2ff; font-size: 1.05rem; }
.outcome-answer-grid {
  width: min(100%, 38rem);
  display: grid;
  gap: 0.4rem;
}
.outcome-answer-row {
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(7rem, 0.8fr) minmax(0, 2fr);
  gap: 0.7rem;
  align-items: baseline;
  padding: 0.55rem 0.7rem;
  border: 1px solid rgba(126, 224, 255, 0.32);
  border-radius: 10px;
  background: rgba(6, 24, 36, 0.5);
  text-align: left;
}
.outcome-answer-row dt { color: #ffd45e; font-weight: 800; }
.outcome-answer-row dd { min-width: 0; margin: 0; overflow-wrap: anywhere; }
.outcome-group, .outcome-insight, .outcome-disclosure {
  width: min(100%, 42rem);
  min-width: 0;
}
.outcome-group {
  border: 1px solid rgba(126, 224, 255, 0.28);
  border-radius: 12px;
  background: rgba(6, 24, 36, 0.42);
  text-align: left;
}
.outcome-group-toggle {
  min-height: 44px;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 0.55rem;
  padding: 0.55rem 0.7rem;
  color: #b8f2ff;
  font-size: 1.05rem;
  font-weight: 800;
  cursor: pointer;
  touch-action: manipulation;
  list-style: none;
}
.outcome-group-toggle::-webkit-details-marker { display: none; }
.outcome-group-toggle::before {
  content: "\25B8";
  color: #ffd45e;
  transition: transform 120ms ease;
}
.outcome-group[open] > .outcome-group-toggle::before { transform: rotate(90deg); }
.outcome-group-toggle:focus-visible { outline: 3px solid #ffd45e; outline-offset: 3px; }
.outcome-group-count {
  justify-self: end;
  color: rgba(232, 248, 255, 0.72);
  font-size: 0.76rem;
  font-weight: 700;
}
.outcome-group-body { padding: 0 0.55rem 0.55rem; }
#overlay .outcome-table-key,
#overlay .outcome-zero-revenge {
  max-width: none;
  margin: 0 0.15rem 0.35rem;
  font-size: 0.76rem;
  line-height: 1.25;
}
#overlay .outcome-table-key { color: rgba(232, 248, 255, 0.78); }
#overlay .outcome-zero-revenge {
  color: #b8f2ff;
  font-weight: 700;
}
.outcome-table {
  width: 100%;
  table-layout: fixed;
  border-collapse: separate;
  border-spacing: 0.3rem;
  color: #e8f8ff;
}
.outcome-table th,
.outcome-table td { min-width: 0; overflow-wrap: anywhere; }
.outcome-table thead th {
  padding: 0.15rem 0.25rem;
  color: #b8f2ff;
  font-size: 0.72rem;
  line-height: 1.1;
  text-align: center;
}
.outcome-table thead th:first-child { width: 28%; text-align: left; }
.outcome-table tbody th {
  padding: 0.45rem 0.35rem;
  color: #ffd45e;
  font-size: 0.82rem;
  line-height: 1.15;
  text-align: left;
}
.outcome-table tbody tr > * {
  border-top: 1px solid rgba(126, 224, 255, 0.18);
}
.outcome-metric {
  padding: 0.35rem;
  border-radius: 8px;
  background: rgba(15, 59, 87, 0.72);
  text-align: center;
}
.outcome-metric.is-strong { background: rgba(32, 107, 84, 0.64); }
.outcome-metric.is-growing { background: rgba(121, 88, 25, 0.58); }
.outcome-metric.is-practice { background: rgba(116, 49, 61, 0.62); }
.outcome-metric.is-empty { background: rgba(45, 61, 72, 0.72); }
.outcome-metric-content { display: flex; flex-direction: column; align-items: center; gap: 0.05rem; }
.outcome-metric-top { display: flex; align-items: baseline; justify-content: center; gap: 0.3rem; }
.outcome-rate {
  padding: 0.05rem 0.25rem;
  border-radius: 999px;
  background: rgba(6, 24, 36, 0.5);
  font-size: 0.7rem;
  font-weight: 900;
}
.outcome-ratio { color: #e8f8ff; font-size: 0.9rem; font-weight: 700; white-space: nowrap; }
.outcome-ratio b { color: #8df0a0; font-size: 1.05rem; }
.outcome-ratio-key { color: rgba(232, 248, 255, 0.68); font-size: 0.58rem; line-height: 1; }
.outcome-missed {
  margin-top: 0.1rem;
  font-size: 0.68rem;
  font-weight: 800;
  white-space: nowrap;
}
.outcome-missed.is-clear { color: #8df0a0; }
.outcome-missed.has-misses { color: #ffd4d4; }
.outcome-insight {
  padding: 0.55rem 0.7rem;
  border: 1px solid rgba(184, 242, 255, 0.28);
  border-radius: 10px;
  background: rgba(6, 24, 36, 0.42);
}
#overlay .outcome-insight p, #overlay .outcome-disclosure p {
  max-width: none; margin-top: 0.2rem; font-size: 0.9rem;
}
.outcome-disclosure { opacity: 0.75; }

@media (max-width: 480px), (max-height: 520px) {
  #overlay {
    justify-content: flex-start;
    padding:
      max(0.75rem, env(safe-area-inset-top))
      max(0.75rem, env(safe-area-inset-right))
      max(0.75rem, env(safe-area-inset-bottom))
      max(0.75rem, env(safe-area-inset-left));
    gap: 0.45rem;
  }
  #overlay h1 { font-size: clamp(1.8rem, 9vw, 2.35rem); }
  #overlay h2 { font-size: clamp(1.25rem, 6vw, 1.55rem); }
  #overlay p { font-size: 0.95rem; line-height: 1.35; }
  .start-btn { font-size: 1.05rem; padding: 0.6rem 1.3rem; }
  .upgrade-dialog { gap: 0.4rem; }
  .intermission-dialog { min-height: 0; margin-block: auto; gap: 0.45rem; }
  .upgrade-grid { gap: 0.45rem; }
  .upgrade-card { min-height: 7.75rem; gap: 0.3rem; padding: 0.55rem; }
  .upgrade-title { font-size: 1rem; }
  .upgrade-consequence { font-size: 0.82rem; line-height: 1.2; }
  .upgrade-stack, .upgrade-meta { font-size: 0.7rem; }
  .outcome-report { gap: 0.45rem; }
}

@media (max-width: 480px) and (min-height: 521px) {
  .upgrade-grid { grid-template-columns: 1fr; }
  .upgrade-card { min-height: 7.25rem; }
}

@media (max-width: 420px) {
  .chapter-review-toggle { grid-template-columns: auto minmax(0, 1fr); gap: 0.1rem 0.45rem; }
  .chapter-review-summary { grid-column: 2; text-align: left; }
  .outcome-group-body { padding-inline: 0.4rem; }
  .outcome-table { display: block; border-spacing: 0; }
  .outcome-table thead {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
  }
  .outcome-table tbody { display: grid; gap: 0.4rem; }
  .outcome-table tr {
    min-width: 0;
    display: grid;
    gap: 0.25rem;
    padding: 0.4rem;
    border-radius: 9px;
    background: rgba(15, 59, 87, 0.5);
  }
  .outcome-table.has-revenge tr { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .outcome-table.no-revenge tr { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .outcome-table tbody th {
    grid-column: 1 / -1;
    padding: 0 0.1rem 0.15rem;
    border-top: 0;
    font-size: 0.9rem;
  }
  .outcome-table tbody td { border-top: 0; }
  .outcome-metric { padding: 0.3rem 0.2rem; }
  .outcome-metric::before {
    content: attr(data-label);
    display: block;
    margin-bottom: 0.15rem;
    color: #b8f2ff;
    font-size: 0.62rem;
    font-weight: 800;
  }
  .outcome-metric-top { flex-direction: column; align-items: center; gap: 0; }
}

@media (max-width: 340px) {
  .chapter-review-answers { grid-template-columns: 1fr; }
  .outcome-answer-row { grid-template-columns: 1fr; gap: 0.15rem; }
}

@media (prefers-reduced-motion: reduce) {
  .chapter-review-toggle::before, .outcome-group-toggle::before { transition: none; }
}
