:root {
  --gold: #ffd100;
  --gold-dim: #b8962e;
  --text: #ebdcb0;
  --text-muted: #a89a74;
  --error: #ff6a5c;
  --panel-bg: rgba(16, 12, 8, 0.94);
  --field-bg: #0d0a07;
  --field-line: #5a4a28;
  --font: "Segoe UI", Arial, sans-serif;
  --font-mono: 'Lucida Console', Monaco, monospace;
  --size: 13px;
  --size-small: 12px;
  --frame: url(resources/ShellFrame.png);
  --frame-small: url(resources/ShellTooltipFrame.png);
  --tile: url(resources/ShellBackground.png);
  --button-up: url(resources/ShellButtonUp.png);
  --button-down: url(resources/ShellButtonDown.png);
  --button-disabled: url(resources/ShellButtonDisabled.png);
  --status-bar: url(resources/ShellStatusBar.png);
  --quality-dark: rgb(0, 80, 0);
  --quality-bright: rgb(0, 200, 0);
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: black;
  font-family: var(--font);
  font-size: var(--size);
  color: var(--text);
  color-scheme: dark;
}

a { color: var(--gold); }
a:active, a:focus { color: #fff; }

.page { display: none; }
body.page-game #page-game,
body.page-settings #page-settings,
body.page-controls #page-controls,
body.page-log #page-log { display: block; }

#canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  border: 0;
  box-sizing: border-box;
  background: black;
  outline: none;
  image-rendering: smooth;
  object-fit: contain;
}

button, .bar-button {
  box-sizing: border-box;
  border: 8px solid transparent;
  border-image: var(--button-up) 8 10 / 8px 10px stretch;
  border-image-slice: 8 10 fill;
  background: none;
  color: var(--gold);
  font: inherit;
  font-size: var(--size-small);
  padding: 0 6px;
  min-height: 28px;
  cursor: pointer;
  white-space: nowrap;
  text-shadow: 0 1px 1px #000;
  text-decoration: none;
}
button:hover:not(:disabled), .bar-button:hover { filter: brightness(1.3); }
button:active:not(:disabled), button.active, .bar-button:active { border-image-source: var(--button-down); }
button.active { color: #fff; }
button:disabled { border-image-source: var(--button-disabled); color: #8a8a8a; cursor: default; }
button:focus-visible, .bar-button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible { outline: 2px solid var(--gold); outline-offset: 1px; }

.panel {
  box-sizing: border-box;
  border: 32px solid transparent;
  border-image: var(--frame) 32 fill / 32px stretch;
  background: var(--tile), var(--panel-bg);
  background-origin: border-box;
  color: var(--text);
}

#ui-container {
  position: fixed;
  inset: 0;
  z-index: 2;
  display: flex;
  flex-direction: column;
  pointer-events: none;
}

#topbar {
  flex: 0 0 auto;
  align-self: center;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 2px;
  width: max-content;
  max-width: calc(100% - 24px);
  padding: 4px 0 0;
  box-sizing: border-box;
  pointer-events: none;
  opacity: 1;
  transition: opacity 0.25s ease;
}

#topbar > button, #topbar > .bar-button { pointer-events: auto; display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
#topbar > button:focus, #topbar > button:focus-visible, #topbar > .bar-button:focus, #topbar > .bar-button:focus-visible { outline: none; }
#topbar svg { width: 14px; height: 14px; fill: currentColor; flex: 0 0 auto; }

body.page-game.ui-idle:not(.start-pending) #topbar {
  opacity: 0;
  transition: opacity 1.2s ease 2s;
}
body.page-game.ui-idle:not(.start-pending) #topbar > * { pointer-events: none; }
#topbar > .phone-only { display: none; }
body.is-phone #topbar > .phone-only { display: inline-flex; }
body.is-phone.page-game.ui-idle:not(.start-pending) #topbar { opacity: 1; transition: none; }
body.is-phone.page-game.ui-idle:not(.start-pending) #topbar > * { pointer-events: auto; }

#quality {
  flex: 0 0 auto;
  align-self: center;
  box-sizing: border-box;
  height: 16px;
  margin: 2px 0 0;
  border: 3px solid transparent;
  border-image: var(--frame-small) 16 / 3px stretch;
  background: var(--quality-dark) padding-box;
  position: relative;
  pointer-events: none;
  opacity: 1;
  transition: opacity 0.25s ease;
}
#quality[hidden] { display: none; }
#quality-fill {
  width: 0;
  height: 100%;
  background-color: var(--quality-bright);
  background-image: var(--status-bar);
  background-size: 100% 100%;
  background-repeat: no-repeat;
  background-blend-mode: overlay;
  transition: width 0.4s ease;
}
#quality-text {
  position: absolute;
  inset: 0;
  padding: 0 4px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  text-align: center;
  font-size: 10px;
  line-height: 10px;
  color: #fff;
  text-shadow: 0 0 2px #000, 0 1px 1px #000;
}
body.page-game.ui-idle:not(.start-pending) #quality {
  opacity: 0;
  pointer-events: none;
  transition: opacity 1.2s ease 2s;
}

@media (max-width: 720px) {
  #topbar .label { display: none; }
}

#pages {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  pointer-events: none;
}

#pages > .page {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  pointer-events: auto;
}

#output {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 8px;
  border: 0;
  box-sizing: border-box;
  background: black;
  color: lightgray;
  font-family: var(--font-mono);
  font-size: var(--size);
  resize: none;
  outline: none;
}

.settings-panel {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  margin: 0;
  padding: 16px;
  background: var(--tile), #060403;
  color: var(--text);
  overflow-y: auto;
}

.settings-panel fieldset {
  box-sizing: border-box;
  border: 8px solid transparent;
  border-image: var(--frame-small) 16 / 8px stretch;
  background: var(--panel-bg);
  background-origin: border-box;
  margin: 0 0 12px;
  padding: 4px 12px 12px;
  max-width: 560px;
}

.settings-panel legend {
  padding: 0 6px;
  color: var(--gold);
  font-weight: bold;
}

.settings-table { width: 100%; border-collapse: collapse; }
.settings-table td { padding: 4px 0; vertical-align: middle; }
.settings-table td:first-child:not([colspan]) { padding-right: 12px; white-space: nowrap; width: 1%; }
.settings-table td:last-child:not(:first-child) { width: 99%; }

.settings-control {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.settings-label {
  flex: 1 1 100%;
  color: var(--text);
}
.settings-table td:first-child:not([colspan]) .settings-label { display: block; }

.settings-control select,
.settings-control textarea,
.settings-control input {
  flex: 0 0 auto;
  width: 160px;
  box-sizing: border-box;
  background-color: var(--field-bg);
  color: var(--text);
  border: 1px solid var(--field-line);
  border-radius: 2px;
  box-shadow: inset 0 1px 3px #000;
  padding: 3px 5px;
  font: inherit;
}

.settings-control textarea { width: 260px; resize: vertical; }

.settings-shard-generate { flex: 0 0 auto; }
.settings-shard-warning { flex: 1 0 100%; color: var(--gold); font-size: 90%; }
.settings-shard-warning:empty { display: none; }

.settings-control select option { background-color: var(--field-bg); color: var(--text); }

.settings-control select:disabled,
.settings-control textarea:disabled,
.settings-control input:disabled { opacity: 0.5; }

.settings-control input::placeholder { color: var(--text-muted); }

.settings-error {
  flex: 1 1 100%;
  color: var(--error);
  font-size: 0.9em;
}

.settings-about {
  margin: 8px 0 0;
  font-size: 0.85em;
  color: var(--text-muted);
}

.controls-row { display: flex; align-items: baseline; gap: 8px; padding: 4px 0; }
.controls-key { flex: 0 0 auto; min-width: 170px; color: var(--gold); }
.controls-desc { flex: 1 1 auto; color: var(--text-muted); }

#start-overlay {
  position: fixed;
  inset: 0;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 48px 12px 12px;
  box-sizing: border-box;
  background: black;
}

#start-overlay.hidden { display: none; }

#start-window {
  width: 600px;
  max-width: 100%;
  max-height: calc(100% - 110px);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
  padding: 0;
}

#btn-start {
  align-self: center;
  min-width: 160px;
  min-height: 40px;
  padding: 0 24px;
  font-size: 20px;
}

#start-tabs { display: flex; flex-wrap: wrap; justify-content: center; gap: 2px; }
.start-tab { font-size: var(--size-small); }
.start-tab:not(.active) { color: var(--text); }

#start-rows .settings-control input[type=text], #start-rows .settings-control input[type=url], #start-rows .settings-control textarea { flex: 1 1 200px; width: auto; min-width: 0; }

#shell-message {
  box-sizing: border-box;
  max-width: min(520px, calc(100% - 24px));
  padding: 6px 14px;
  text-align: center;
  border: 8px solid transparent;
  border-image: var(--frame-small) 16 fill / 8px stretch;
  background: var(--panel-bg);
  background-origin: border-box;
  color: var(--gold);
  text-shadow: 0 1px 2px #000;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.5s ease;
}
#shell-message.has-text { opacity: 1; }
#start-overlay > #shell-message { margin-top: 10px; }
#ui-container > #shell-message { position: absolute; left: 50%; bottom: 12px; transform: translateX(-50%); }
#status-text:empty { display: none; }
#status-text.error { color: var(--error); }

#shell-progress { display: flex; align-items: center; gap: 8px; margin-top: 4px; font-size: var(--size-small); color: var(--text); }
#shell-progress[hidden] { display: none; }
#shell-progress-bar { flex: 1 1 auto; min-width: 120px; height: 8px; border: 1px solid var(--field-line); background: var(--field-bg); overflow: hidden; }
#shell-progress-fill { height: 100%; width: 0; background: linear-gradient(#ffd100, #b87800); }
#shell-progress.unknown #shell-progress-fill { width: 30%; animation: shell-progress-slide 1.2s linear infinite; }
@keyframes shell-progress-slide { from { margin-left: -30%; } to { margin-left: 100%; } }

#video-overlay {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: black;
  z-index: 1;
  opacity: 1;
  visibility: hidden;
  cursor: none;
  image-rendering: smooth;
}

body.show-video #video-overlay { visibility: visible; }
body.show-video #canvas { visibility: hidden; }
body.show-video.fading #canvas { visibility: visible; }
body.fading #video-overlay { pointer-events: none; }
