:root {
  --theme-frame: url('./loading/913.png'), url('./loading/914.png'), url('./loading/915.png'),
    url('./loading/916.png'), url('./loading/917.png'), url('./loading/918.png'),
    url('./loading/919.png'), url('./loading/920.png');
  --theme-frame-position: left top, right top, left bottom, right bottom, left 9px, 9px top, right 9px, 9px bottom;
  --theme-frame-size: 9px 9px, 9px 9px, 9px 9px, 9px 9px, 9px calc(100% - 18px),
    calc(100% - 18px) 9px, 9px calc(100% - 18px), calc(100% - 18px) 9px;
}

html, body {
  margin: 0;
  height: 100%;
  background: #000;
  color: #d8d2c5;
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

#game-root {
  position: fixed;
  inset: 0;
  display: block;
}

/* The engine's display area: its content box is measured on every frame and the canvas is laid out in it
   (CSS size and offset set by the engine's viewport host; the startup borders fill the rest). */
#game-stage {
  position: absolute;
  top: env(safe-area-inset-top, 0px);
  right: env(safe-area-inset-right, 0px);
  bottom: env(safe-area-inset-bottom, 0px);
  left: env(safe-area-inset-left, 0px);
  overflow: hidden;
  background: #000;
}

#game-canvas {
  position: absolute;
  left: 0;
  top: 0;
  display: block;
  outline: none;
  image-rendering: auto;
}

/* Boot loading overlay (the desktop's Java2D loading screen), driven by the engine's loading pipeline. */
#game-loading {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  pointer-events: none;
  z-index: 2;
}

#game-loading[hidden] { display: none; }

/* Exact copies of cache sprites 913..920; corners remain 9 logical pixels on every host. */
.loading-panel::after, .loading-bar::after {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 1;
  background-image: var(--theme-frame);
  background-position: var(--theme-frame-position);
  background-size: var(--theme-frame-size);
  background-repeat: no-repeat;
}

.loading-panel {
  position: relative;
  width: min(540px, calc(100% - 32px));
  height: min(100%, max(118px, min(340px, calc(100% - 32px))));
  box-sizing: border-box;
  text-align: center;
  isolation: isolate;
}
.loading-panel::before, .loading-bar::before {
  content: '';
  position: absolute;
  inset: 3px;
  background: #252222;
  z-index: -1;
}
.loading-title {
  position: absolute;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  width: min(360px, calc(100% - 48px));
  height: max(0px, min(160px, calc(100% - 146px)));
  object-fit: contain;
  object-position: center top;
}
.loading-bar {
  position: absolute;
  left: min(32px, 12.5%);
  right: min(32px, 12.5%);
  bottom: 64px;
  height: 24px;
  box-sizing: border-box;
  padding: 3px;
  isolation: isolate;
}
.loading-bar::before { background: #181616; }
.loading-fill {
  width: 0;
  height: 100%;
  background: #9b8050;
  box-shadow: inset 0 1px #e4c078;
  transition: width 120ms linear;
}
.loading-text {
  position: absolute;
  left: 16px;
  right: 16px;
  bottom: 37px;
  color: #d8d2c5;
  font: bold 13px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#game-loading::after {
  content: attr(aria-valuenow) '%';
  position: absolute;
  top: calc(50% + min(50%, max(59px, min(170px, (100% - 32px) / 2))) - 34px);
  left: 0;
  right: 0;
  text-align: center;
  color: #e4c078;
  font-size: 11px;
}

#game-status {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  padding: 6px 14px;
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.6);
  pointer-events: none;
  white-space: nowrap;
  z-index: 5;
}

#game-status:empty { display: none; }

#game-error {
  position: fixed;
  inset: 16px;
  margin: 0;
  padding: 16px;
  overflow: auto;
  border: 1px solid #a33;
  background: rgba(20, 0, 0, 0.92);
  color: #f3d0d0;
  font: 12px/1.35 ui-monospace, Consolas, monospace;
  white-space: pre-wrap;
  z-index: 10;
  user-select: text;
  -webkit-user-select: text;
}

.script-notice {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  margin: 0;
}

/* ---- Touch text entry (game.mjs createKeyboardButton): raises the on-screen keyboard for a game text field. ---- */
/* In the stage, which already keeps clear of the safe areas (and is the fullscreen element). */
#game-keyboard {
  position: absolute;
  top: 36px;
  left: 8px;
  z-index: 4;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 1px solid rgba(168, 106, 48, 0.8);
  border-radius: 8px;
  background: rgba(22, 15, 11, 0.75);
  color: #e4c078;
  touch-action: manipulation;
  cursor: pointer;
}

#game-keyboard[hidden] { display: none; }

/* ---- Portrait notice (orientation-notice.mjs): a phone held upright is asked to turn sideways. ---- */
/* Covers the page and takes its touches while the game keeps running underneath; below the error panel. */
#game-rotate {
  position: fixed;
  inset: 0;
  z-index: 8;
  display: grid;
  place-items: center;
  box-sizing: border-box;
  padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
  background: rgba(8, 5, 3, 0.88);
  color: #d8d2c5;
  text-align: center;
}

#game-rotate[hidden] { display: none; }
#game-rotate .rotate-panel { width: min(320px, calc(100% - 48px)); }
#game-rotate .rotate-icon { color: #e4c078; }
#game-rotate .rotate-title { margin: 14px 0 8px; color: #e4c078; font: bold 24px/1.2 Georgia, "Times New Roman", serif; }
#game-rotate .rotate-text { margin: 0 0 10px; font-size: 16px; line-height: 1.4; }
#game-rotate .rotate-hint { margin: 0; color: #b8ac96; font-size: 14px; line-height: 1.4; }
/* Play fullscreen: only where the browser can lock the screen orientation (Android). */
#game-rotate .rotate-play {
  min-height: 44px;
  margin: 4px 0 10px;
  padding: 8px 20px;
  border: 1px solid #575044;
  border-radius: 4px;
  background: #454035;
  color: #d8d2c5;
  font: bold 16px/1.2 system-ui, -apple-system, "Segoe UI", sans-serif;
  cursor: pointer;
}
#game-rotate .rotate-message { margin: 0 0 10px; color: rgb(255, 170, 140); font-size: 14px; line-height: 1.4; }
#game-rotate .rotate-message[hidden] { display: none; }

/* ---- Saved-data panel (storage-panel.mjs): toggle, condition banner and dialog. ---- */
#storage-toggle {
  position: fixed;
  top: env(safe-area-inset-top, 0px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 6;
  padding: 1px 12px 3px;
  border: 1px solid rgba(168, 106, 48, 0.7);
  border-top: 0;
  border-radius: 0 0 6px 6px;
  background: rgba(22, 15, 11, 0.8);
  color: #e4c078;
  font: 12px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
  opacity: 0.4;
  cursor: pointer;
  transition: opacity 120ms linear;
}

#storage-toggle:hover, #storage-toggle:focus-visible { opacity: 1; }
#storage-toggle.attention { opacity: 1; color: #e4c078; }
#storage-toggle.attention::after { content: " !"; font-weight: bold; }
#storage-toggle[hidden], #storage-banner[hidden], #game-link-notice[hidden] { display: none; }

/* The blocked-link notice (link-notice.mjs) shares the banner's look; it lives in the stage, so it shows in fullscreen. */
#storage-banner, #game-link-notice {
  position: fixed;
  top: calc(env(safe-area-inset-top, 0px) + 28px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 6;
  box-sizing: border-box;
  width: min(560px, calc(100% - 24px));
  padding: 10px 12px;
  border: 1px solid #575044;
  border-radius: 4px;
  background: #252222;
  color: #d8d2c5;
  font: 13px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
  user-select: text;
  -webkit-user-select: text;
}

#storage-banner p, #game-link-notice p { margin: 0 0 8px; }

#storage-dialog {
  box-sizing: border-box;
  width: min(560px, calc(100% - 24px));
  max-height: calc(100% - 24px);
  overflow: auto;
  padding: 18px 20px;
  border: 1px solid #575044;
  border-radius: 4px;
  background: #252222;
  color: #d8d2c5;
  font: 13px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
  user-select: text;
  -webkit-user-select: text;
  touch-action: auto;
}

#storage-dialog::backdrop { background: rgba(0, 0, 0, 0.55); }
#storage-dialog h2 { margin: 0 0 4px; color: #e4c078; font: bold 20px/1.2 Georgia, "Times New Roman", serif; }
#storage-dialog h3 { margin: 16px 0 6px; color: #e4c078; font-size: 14px; }
#storage-dialog .storage-lead { margin: 0 0 12px; color: #b8ac96; }
#storage-conditions { margin: 0 0 12px; padding: 8px 12px 8px 28px; border: 1px solid rgb(150, 70, 40); background: rgb(40, 20, 12); }
#storage-conditions[hidden] { display: none; }
#storage-conditions li + li { margin-top: 6px; }
#storage-dialog dl { display: grid; grid-template-columns: max-content 1fr; gap: 4px 14px; margin: 0; }
#storage-dialog dt { color: #b8ac96; }
#storage-dialog dd { margin: 0; overflow-wrap: anywhere; }
#storage-message { min-height: 1.45em; margin: 12px 0 0; }
#storage-message[data-kind="error"] { color: rgb(255, 170, 140); }
#storage-confirm { margin-top: 12px; padding: 10px 12px; border: 1px solid rgb(150, 70, 40); background: rgb(40, 20, 12); }
#storage-confirm[hidden] { display: none; }
#storage-confirm p { margin: 4px 0 10px; }
.storage-actions, .link-notice-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.storage-actions.storage-footer { justify-content: flex-end; margin-top: 16px; }
.storage-actions.storage-persist-row { margin-top: 12px; }

#storage-banner button, #storage-dialog button, #game-link-notice button, #game-link-notice .link-notice-open {
  padding: 5px 12px;
  border: 1px solid #575044;
  border-radius: 3px;
  background: #454035;
  color: #d8d2c5;
  font: inherit;
  cursor: pointer;
}

#storage-banner button:hover, #storage-dialog button:hover:not(:disabled), #game-link-notice button:hover,
#game-link-notice .link-notice-open:hover { --theme-fill: #655d4b; }
#game-link-notice .link-notice-open { display: inline-block; font-weight: bold; text-decoration: none; }
#storage-dialog button:disabled { opacity: 0.5; cursor: default; }
#storage-dialog button.storage-danger { border-color: rgb(170, 70, 50); }
#storage-dialog button[hidden] { display: none; }

/* ---- Browser-settings advisor (platform/performance-advisor.mjs): non-modal popup with fixes per browser. ---- */
/* Opened as a manual popover (top layer) where supported; the z-index is the fallback's. */
.perf-advisor {
  position: fixed;
  inset: auto;
  top: calc(env(safe-area-inset-top, 0px) + 24px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 2147483646;
  box-sizing: border-box;
  width: min(620px, calc(100% - 32px));
  max-height: calc(100% - 48px);
  overflow: auto;
  margin: 0;
  padding: 18px 20px 16px;
  border: 1px solid #575044;
  border-radius: 4px;
  background: #252222;
  color: #d8d2c5;
  font: 13px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
  user-select: text;
  -webkit-user-select: text;
  touch-action: auto;
}

.perf-advisor h2 { margin: 0 0 10px; color: #e4c078; font: bold 20px/1.2 Georgia, "Times New Roman", serif; }
.perf-advisor h3 { margin: 0 0 4px; color: #e4c078; font-size: 14px; }
.perf-advisor-condition { margin: 0 0 12px; padding: 10px 12px; border: 1px solid #575044; background: #181616; }
.perf-advisor-why { margin: 0 0 6px; color: #d8d2c5; overflow-wrap: anywhere; }
.perf-advisor-steps { margin: 0; padding-left: 22px; }
.perf-advisor-steps li + li { margin-top: 5px; }
.perf-advisor-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
.perf-advisor-link { display: inline-block; margin-top: 8px; color: #e4c078; }
.perf-advisor-note { margin: 4px 0 12px; color: #b8ac96; }
.perf-advisor-copy-field { position: fixed; top: -1000px; left: -1000px; opacity: 0; }

.perf-advisor-button, .perf-advisor-close {
  display: inline-block;
  padding: 4px 10px;
  border: 1px solid #575044;
  border-radius: 3px;
  background: #454035;
  color: #d8d2c5;
  font: inherit;
  text-decoration: none;
  cursor: pointer;
}

.perf-advisor-button:hover, .perf-advisor-close:hover { --theme-fill: #655d4b; }
.perf-advisor-close { float: right; }

/* Native saved-account frame on host notices, recovery reports and their controls. */
#game-error, #game-status, .script-notice, #game-rotate .rotate-panel,
#storage-banner, #game-link-notice, #storage-dialog, .perf-advisor, .velheim-warning-dialog,
#game-rotate .rotate-play, #storage-banner button, #storage-dialog button,
#game-link-notice button, #game-link-notice .link-notice-open,
.perf-advisor-button, .perf-advisor-close, .velheim-warning-dialog button {
  --theme-fill: #252222;
  border: 0;
  border-radius: 0;
  background-color: transparent;
  background-image: var(--theme-frame), linear-gradient(var(--theme-fill), var(--theme-fill));
  background-position: var(--theme-frame-position), center;
  background-size: var(--theme-frame-size), calc(100% - 6px) calc(100% - 6px);
  background-repeat: no-repeat;
  color: #d8d2c5;
}
#game-error {
  padding: 20px;
  max-width: 1100px;
  margin: auto;
  touch-action: auto;
  white-space: normal;
  font: 15px/1.5 Arial, sans-serif;
  z-index: 2147483647;
}
#game-error h2 { color: #d0ac70; margin-top: 0; }
#game-error pre { white-space: pre-wrap; overflow-wrap: anywhere; font: 12px/1.4 ui-monospace, Consolas, monospace; }
#game-error summary { cursor: pointer; margin-top: 16px; }
[data-velheim-unsupported] #game-error { white-space: pre-wrap; }
#game-error .crash-controls { display: flex; flex-wrap: wrap; gap: 8px; }
#game-error button { padding: 10px 16px; color: #d8d2c5; background: #454035; border: 1px solid #9b8050; cursor: pointer; }
#game-error, #storage-dialog, .velheim-warning-dialog {
  scrollbar-color: #9b8050 #181616;
  scrollbar-width: thin;
}
#game-error::before, .perf-advisor::before, .velheim-warning-dialog::before {
  content: '';
  display: block;
  width: min(240px, 100%);
  height: 100px;
  margin: 0 auto 16px;
  background: url('./loading/logo.webp') center / contain no-repeat;
}
#game-status { max-width: calc(100% - 48px); padding: 10px 16px; overflow: hidden; text-overflow: ellipsis; }
.script-notice { inset: auto; top: 50%; left: 50%; transform: translate(-50%, -50%); width: min(520px, calc(100% - 48px)); padding: 24px; box-sizing: border-box; }
#game-rotate { background: #000; }
#game-rotate .rotate-panel { padding: 24px; box-sizing: border-box; }
#game-rotate .rotate-play, #storage-banner button, #storage-dialog button,
#game-link-notice button, #game-link-notice .link-notice-open,
.perf-advisor-button, .perf-advisor-close, .velheim-warning-dialog button {
  --theme-fill: #454035;
  padding: 10px 16px;
}
#game-rotate .rotate-play:hover, #storage-banner button:hover, #storage-dialog button:hover:not(:disabled),
#game-link-notice button:hover, #game-link-notice .link-notice-open:hover,
.perf-advisor-button:hover, .perf-advisor-close:hover, .velheim-warning-dialog button:hover {
  --theme-fill: #655d4b;
  background-color: transparent;
}
#game-rotate .rotate-play:focus-visible, #storage-banner button:focus-visible, #storage-dialog button:focus-visible,
#game-link-notice button:focus-visible, #game-link-notice .link-notice-open:focus-visible,
.perf-advisor-button:focus-visible, .perf-advisor-close:focus-visible, .velheim-warning-dialog button:focus-visible {
  outline: 2px solid #e4c078;
  outline-offset: 2px;
}
.velheim-warning-dialog { width: min(520px, calc(100% - 32px)); max-height: calc(100% - 32px); padding: 20px; box-sizing: border-box; overflow: auto; touch-action: auto; user-select: text; -webkit-user-select: text; }
.velheim-warning-dialog::backdrop { background: rgba(0, 0, 0, 0.8); }
.velheim-warning-dialog h2 { color: #e4c078; font-size: 20px; }
.velheim-warning-dialog form { text-align: right; }
#game-error a, .velheim-warning-dialog a { color: #e4c078; display: inline-block; margin: 8px 12px 8px 0; }
#game-error p { white-space: pre-line; }
@media (max-height: 117px) {
  .loading-title { display: none; }
  .loading-bar { top: 8px; bottom: auto; }
  .loading-text { bottom: 24px; }
  #game-loading::after { top: auto; bottom: 1px; }
}
@media (max-height: 95px) { .loading-bar { display: none; } }
@media (max-height: 43px) {
  #game-loading::after { display: none; }
  .loading-text { top: 0; bottom: auto; }
}
