:root {
  --cb-safe-top: env(safe-area-inset-top, 0px);
  --cb-safe-right: env(safe-area-inset-right, 0px);
  --cb-safe-bottom: env(safe-area-inset-bottom, 0px);
  --cb-safe-left: env(safe-area-inset-left, 0px);
  --cb-viewport-height: 100vh;
  --cb-safe-width: calc(100vw - var(--cb-safe-left) - var(--cb-safe-right));
  --cb-safe-height: calc(var(--cb-viewport-height) - var(--cb-safe-top) - var(--cb-safe-bottom));
  --cb-frame-width: var(--cb-safe-width);
  --cb-frame-height: var(--cb-safe-height);
}

@supports (height: 100dvh) {
  :root { --cb-viewport-height: 100dvh; }
}

html,
body {
  width: 100%;
  min-width: 100%;
  height: 100vh;
  min-height: 100%;
  overflow: hidden;
}

@supports (height: 100dvh) {
  html,
  body { height: 100dvh; }
}

body {
  box-sizing: border-box;
  padding: 0;
}

#stage {
  position: fixed;
  inset: 0;
  display: block;
  width: 100%;
  height: 100vh;
  height: var(--cb-viewport-height);
  overflow: hidden;
  box-sizing: border-box;
  background: #0a0e18;
}

#frame {
  position: absolute;
  left: calc(var(--cb-safe-left) + (var(--cb-safe-width) - var(--cb-frame-width)) / 2);
  top: calc(var(--cb-safe-top) + (var(--cb-safe-height) - var(--cb-frame-height)) / 2);
  width: var(--cb-frame-width);
  height: var(--cb-frame-height);
  max-width: none;
  max-height: none;
  min-width: 0;
  min-height: 0;
  aspect-ratio: auto;
  box-sizing: border-box;
  contain: layout paint;
  isolation: isolate;
}

#render {
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  touch-action: none;
}

#frame button,
#frame [role="button"] {
  box-sizing: border-box;
  max-width: calc(100% - 24px);
  min-width: 44px;
  min-height: 44px;
  touch-action: manipulation;
}

#foldable-secondary {
  position: absolute;
  display: none;
  flex-direction: column;
  justify-content: center;
  gap: clamp(14px, 2.2vmin, 28px);
  box-sizing: border-box;
  overflow: hidden;
  padding: clamp(20px, 4vmin, 54px);
  color: #f4f7ff;
  background:
    radial-gradient(circle at 72% 18%, rgba(255, 210, 63, 0.18), transparent 36%),
    linear-gradient(155deg, rgba(24, 50, 92, 0.96), rgba(5, 14, 35, 0.98));
  border: 1px solid rgba(255, 255, 255, 0.11);
  box-shadow: inset 0 0 80px rgba(2, 8, 22, 0.42);
  font-family: "Arial Black", Impact, system-ui, sans-serif;
  -webkit-user-select: none;
  user-select: none;
}

#stage.is-segmented #foldable-secondary {
  display: flex;
}

.foldable-kicker {
  color: #a9cfff;
  font-size: clamp(11px, 1.55vmin, 18px);
  letter-spacing: 0.24em;
  text-transform: uppercase;
}

.foldable-title {
  margin: 0;
  color: #ffd23f;
  font-size: clamp(32px, 6.2vmin, 86px);
  line-height: 0.88;
  letter-spacing: -0.04em;
  text-shadow: 0 5px 0 #8b4308, 0 10px 30px rgba(255, 153, 0, 0.34);
}

.foldable-stats {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: clamp(8px, 1.5vmin, 18px);
  margin: 0;
}

.foldable-stat {
  min-width: 0;
  padding: clamp(11px, 1.8vmin, 20px);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: clamp(10px, 1.5vmin, 18px);
  background: rgba(0, 0, 0, 0.2);
}

.foldable-stat dt {
  margin: 0 0 5px;
  color: #a9cfff;
  font-size: clamp(9px, 1.2vmin, 14px);
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.foldable-stat dd {
  margin: 0;
  overflow: hidden;
  color: #ffffff;
  font-size: clamp(20px, 3.2vmin, 42px);
  line-height: 1;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.foldable-help {
  margin: 0;
  color: #d8e9ff;
  font: 700 clamp(12px, 1.6vmin, 18px)/1.55 system-ui, sans-serif;
}

.foldable-status {
  display: inline-flex;
  align-items: center;
  align-self: flex-start;
  gap: 9px;
  color: #c9d9ef;
  font: 700 clamp(10px, 1.3vmin, 15px)/1 system-ui, sans-serif;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.foldable-status::before {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: #62e08d;
  box-shadow: 0 0 13px rgba(98, 224, 141, 0.78);
  content: "";
}

.foldable-probe {
  position: fixed;
  z-index: -1000;
  visibility: hidden;
  pointer-events: none;
}

/* CSS-only safety net for browsers that expose viewport segments but not the
   legacy JavaScript segment API. The JavaScript layer reads the same env()
   values and replaces these declarations with exact safe-area-aware geometry. */
@media (horizontal-viewport-segments: 2) {
  #stage:not(.is-segmented) #frame {
    left: env(viewport-segment-left 0 0);
    top: env(viewport-segment-top 0 0);
    width: env(viewport-segment-width 0 0);
    height: env(viewport-segment-height 0 0);
  }

  #stage:not(.is-segmented) #foldable-secondary {
    display: flex;
    left: calc(env(viewport-segment-left 1 0) + 12px);
    top: calc(env(viewport-segment-top 1 0) + 12px);
    width: calc(env(viewport-segment-width 1 0) - 24px);
    height: calc(env(viewport-segment-height 1 0) - 24px);
  }
}

@media (vertical-viewport-segments: 2) {
  #stage:not(.is-segmented) #frame {
    left: env(viewport-segment-left 0 0);
    top: env(viewport-segment-top 0 0);
    width: env(viewport-segment-width 0 0);
    height: env(viewport-segment-height 0 0);
  }

  #stage:not(.is-segmented) #foldable-secondary {
    display: flex;
    left: calc(env(viewport-segment-left 0 1) + 12px);
    top: calc(env(viewport-segment-top 0 1) + 12px);
    width: calc(env(viewport-segment-width 0 1) - 24px);
    height: calc(env(viewport-segment-height 0 1) - 24px);
  }
}

@media (prefers-reduced-motion: reduce) {
  #foldable-secondary * {
    scroll-behavior: auto;
    transition-duration: 0.01ms;
  }
}
/* Expanded intermission upgrade shop */
#frame .carboy-upgrade-shop {
  justify-content: flex-start !important;
  gap: 8px !important;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: clamp(10px, 2.2vmin, 22px) !important;
  scrollbar-gutter: stable;
  -webkit-overflow-scrolling: touch;
}

#frame .carboy-upgrade-shop .carboy-upgrade-label {
  margin: 0;
  color: #7fe0a0;
  font-size: clamp(10px, 1.7vmin, 13px) !important;
  letter-spacing: 0.12em !important;
  text-align: center;
}

#frame .carboy-upgrade-shop .carboy-upgrade-cards {
  display: flex !important;
  flex-direction: column !important;
  align-items: stretch !important;
  width: min(100%, 1040px) !important;
  max-width: 1040px !important;
  gap: 12px !important;
  padding-bottom: max(4px, env(safe-area-inset-bottom, 0px));
}

.carboy-upgrade-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 245px), 1fr));
  gap: clamp(8px, 1.4vmin, 14px);
  width: 100%;
}

/* Shared / individual skill sections, and the vehicle picker above them */
.carboy-shop-section {
  display: flex;
  flex-direction: column;
  gap: clamp(6px, 1.1vmin, 10px);
  width: 100%;
}

.carboy-section-title {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin: 0;
  padding: 0 2px;
  color: #ffd23f;
  font: 900 clamp(12px, 1.8vmin, 15px)/1.1 "Arial Black", Impact, system-ui, sans-serif;
  letter-spacing: 0.1em;
}

.carboy-section-note {
  color: rgba(255, 255, 255, 0.62);
  font: 700 clamp(9px, 1.3vmin, 11px)/1.2 system-ui, sans-serif;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.carboy-vehicle-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 168px), 1fr));
  gap: clamp(8px, 1.4vmin, 14px);
  width: 100%;
}

#frame .carboy-vehicle-card {
  display: flex;
  flex-direction: column;
  gap: 5px;
  width: 100%;
  max-width: none !important;
  min-height: 92px;
  padding: 11px 13px;
  border: 2px solid transparent;
  border-radius: 12px;
  color: #18253a;
  background: linear-gradient(180deg, #ffffff, #e2e8f2);
  box-shadow: 0 5px 0 #8d97a8, 0 9px 18px rgba(0, 0, 0, 0.32), inset 0 2px 0 #ffffff;
  cursor: pointer;
  font: 700 11px/1.3 system-ui, sans-serif;
  text-align: left;
}

#frame .carboy-vehicle-card:not(:disabled):hover {
  background: linear-gradient(180deg, #fff9d8, #ffe985);
}

#frame .carboy-vehicle-card:not(:disabled):active {
  transform: translateY(4px);
  box-shadow: 0 1px 0 #8d97a8, 0 4px 10px rgba(0, 0, 0, 0.35), inset 0 2px 0 #ffffff;
}

#frame .carboy-vehicle-card.is-active {
  border-color: #ffd23f;
  background: linear-gradient(180deg, #fff4c4, #ffd85c);
  box-shadow: 0 5px 0 #8b4308, 0 9px 18px rgba(0, 0, 0, 0.38), inset 0 2px 0 #fff8d6;
  cursor: default;
  opacity: 1;
  filter: none;
}

#frame .carboy-vehicle-card.is-locked {
  background: linear-gradient(180deg, #dfe3ea, #b9c1cf);
}

#frame .carboy-vehicle-card:disabled {
  cursor: not-allowed;
}

#frame .carboy-vehicle-card.is-locked:disabled {
  opacity: 0.62;
  filter: saturate(0.35);
}

.carboy-vehicle-name {
  color: #12305c;
  font: 900 clamp(13px, 1.9vmin, 16px)/1.1 "Arial Black", Impact, system-ui, sans-serif;
}

.carboy-vehicle-state {
  align-self: flex-start;
  padding: 3px 7px;
  border-radius: 999px;
  color: #ffffff;
  background: #183c68;
  font-size: 9px;
  font-weight: 900;
  letter-spacing: 0.09em;
}

.carboy-vehicle-card.is-active .carboy-vehicle-state {
  background: #8b4308;
}

.carboy-vehicle-blurb {
  color: #56647a;
}

#frame .carboy-upgrade-card {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 7px;
  width: 100%;
  max-width: none !important;
  min-height: 124px;
  padding: 12px 14px;
  border: 0;
  border-radius: 12px;
  color: #18253a;
  background: linear-gradient(180deg, #ffffff, #e8edf5);
  box-shadow: 0 5px 0 #8d97a8, 0 9px 18px rgba(0, 0, 0, 0.32), inset 0 2px 0 #ffffff;
  cursor: pointer;
  font: 700 12px/1.35 system-ui, sans-serif;
  text-align: left;
}

#frame .carboy-upgrade-card:not(:disabled):hover {
  background: linear-gradient(180deg, #fff9d8, #ffe985);
}

#frame .carboy-upgrade-card:not(:disabled):active {
  transform: translateY(4px);
  box-shadow: 0 1px 0 #8d97a8, 0 4px 10px rgba(0, 0, 0, 0.35), inset 0 2px 0 #ffffff;
}

#frame .carboy-upgrade-card:disabled {
  opacity: 0.58;
  cursor: not-allowed;
  filter: saturate(0.45);
}

.carboy-upgrade-heading,
.carboy-upgrade-footer {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.carboy-upgrade-name {
  color: #12305c;
  font: 900 clamp(14px, 2vmin, 18px)/1.1 "Arial Black", Impact, system-ui, sans-serif;
}

.carboy-upgrade-level {
  flex: 0 0 auto;
  padding: 4px 7px;
  border-radius: 999px;
  color: #ffffff;
  background: #183c68;
  font-size: 10px;
  letter-spacing: 0.08em;
}

.carboy-upgrade-blurb {
  flex: 1;
  color: #56647a;
}

.carboy-upgrade-effect {
  color: #0f7a43;
  font-weight: 900;
}

.carboy-upgrade-price {
  color: #8b4308;
  font-size: 10px;
  font-weight: 900;
  letter-spacing: 0.04em;
  text-align: right;
}

#frame .carboy-next-day {
  position: sticky;
  bottom: 0;
  z-index: 2;
  width: min(100%, 420px);
  max-width: 420px !important;
  min-height: 58px;
  align-self: center;
  padding: 12px 20px;
  border: 0;
  border-radius: 12px;
  color: #18253a;
  background: #ffd23f;
  box-shadow: 0 5px 0 #8b4308, 0 10px 22px rgba(0, 0, 0, 0.42), inset 0 2px 0 #fff2a8;
  cursor: pointer;
  font: 900 15px/1 "Arial Black", Impact, system-ui, sans-serif;
  letter-spacing: 0.08em;
}

#frame .carboy-next-day:active {
  transform: translateY(4px);
  box-shadow: 0 1px 0 #8b4308, 0 5px 12px rgba(0, 0, 0, 0.4), inset 0 2px 0 #fff2a8;
}

@media (max-height: 560px) {
  #frame .carboy-upgrade-shop {
    align-items: stretch !important;
  }

  #frame .carboy-upgrade-shop .carboy-upgrade-cards {
    align-self: center;
  }

  #frame .carboy-upgrade-card {
    min-height: 110px;
  }
}
/* Autosave and new-game controls */
.carboy-save-summary {
  margin-top: 8px;
  color: #cfe6ff;
  font: 800 clamp(10px, 1.7vmin, 13px)/1.3 system-ui, sans-serif;
  letter-spacing: 0.1em;
  text-align: center;
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.7);
}

#frame .carboy-new-game {
  min-height: 48px;
  border: 1px solid rgba(255, 255, 255, 0.22);
  border-radius: 10px;
  color: #eaf2ff;
  background: rgba(10, 22, 43, 0.88);
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.42);
  cursor: pointer;
  font: 900 12px/1 "Arial Black", Impact, system-ui, sans-serif;
  letter-spacing: 0.08em;
  touch-action: manipulation;
}

#frame .carboy-new-game:hover {
  background: rgba(28, 53, 89, 0.94);
}

#frame .carboy-new-game:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.42);
}

#frame .carboy-new-game.is-confirming {
  border-color: #ff8d7a;
  color: #ffffff;
  background: #a52f24;
}

#frame .carboy-new-game-title {
  width: min(72vw, 260px);
  max-width: 260px !important;
  margin-top: 7px;
  padding: 11px 16px;
}

#frame .carboy-new-game-pause {
  width: 100%;
  max-width: none !important;
  padding: 10px 12px;
}

#frame .carboy-new-game-upgrade {
  width: min(100%, 300px);
  max-width: 300px !important;
  min-height: 44px;
  align-self: center;
  padding: 9px 14px;
  background: rgba(10, 22, 43, 0.96);
}
