:root {
  --cga-amber: #ffbf00;
  --cga-amber-dim: #8a6700;
  --cga-background: #000000;
  --terminal-font-size: clamp(16px, min(1.28vw + 3px, 1.5vh + 3px), 23px);
  --screen-shell-padding: clamp(8px, 1.5vh, 24px);
  --terminal-frame-padding: clamp(8px, 1.2vh, 14px);
  --panel-width: clamp(220px, 24vw, 320px);
  --panel-gap: clamp(10px, 1.5vw, 18px);
  --desktop-transcript-stage-width: clamp(720px, 60vw, 1120px);
  --mobile-command-bar-height: 52px;
  --visual-viewport-height: 100dvh;
  --mobile-keyboard-offset: 0px;
  --meta-ilex-red: #ff5b47;
}

html,
body {
  min-height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: var(--cga-background);
}

body {
  display: grid;
  place-items: center;
}

#screen-shell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 100dvh;
  height: 100dvh;
  padding: var(--screen-shell-padding);
  box-sizing: border-box;
  overflow: hidden;
}

#terminal-stage {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  gap: var(--panel-gap);
  width: min(100%, 1600px);
  max-width: calc(100vw - (var(--screen-shell-padding) * 2));
  height: calc(var(--visual-viewport-height) - (var(--screen-shell-padding) * 2));
  max-height: calc(var(--visual-viewport-height) - (var(--screen-shell-padding) * 2));
  min-height: 0;
  overflow: hidden;
  transition: width 420ms ease;
}

@media (min-width: 761px) {
  body:not([data-device-theme="mobile"]) #terminal-stage[data-layout="transcript-only"] {
    width: min(100%, var(--desktop-transcript-stage-width));
    max-width: min(var(--desktop-transcript-stage-width), calc(100vw - (var(--screen-shell-padding) * 2)));
  }
}

#status-bar {
  display: none;
}

#terminal-content {
  display: flex;
  flex: 1 1 auto;
  align-items: stretch;
  justify-content: center;
  gap: 0;
  min-height: 0;
  overflow: hidden;
  transition: gap 420ms ease;
}

#terminal-stage[data-layout="with-panels"] #terminal-content {
  gap: var(--panel-gap);
}

.meta-column {
  position: absolute;
  top: var(--screen-shell-padding);
  bottom: var(--screen-shell-padding);
  width: clamp(140px, 20vw, 260px);
  pointer-events: none;
  overflow: visible;
  z-index: 2;
}

#meta-column-left {
  left: var(--screen-shell-padding);
}

#meta-column-right {
  right: var(--screen-shell-padding);
}

.ephemeral-message {
  position: absolute;
  top: 0;
  left: 0;
  color: var(--cga-amber);
  font-family: 'Perfect-DOS-VGA-437-Win', monospace;
  font-size: var(--terminal-font-size);
  line-height: 1;
  white-space: pre-wrap;
  width: 100%;
  opacity: 0.9;
  text-shadow: 0 0 8px rgba(255, 191, 0, 0.25);
  text-align: left;
}

#meta-column-left .ephemeral-message {
  left: 0;
  right: auto;
}

#meta-column-right .ephemeral-message {
  left: auto;
  right: 0;
}

#text-grid-container {
  position: relative;
  flex: 1 1 auto;
  display: flex;
  align-items: stretch;
  justify-content: stretch;
  border: 1px solid var(--cga-amber);
  padding: var(--terminal-frame-padding);
  background-color: var(--cga-background);
  box-shadow: 0 0 24px rgba(255, 191, 0, 0.15);
  min-height: 0;
  max-height: 100%;
  min-width: 0;
  box-sizing: border-box;
  overflow: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
  transform: translateX(0);
  transition: transform 420ms ease, box-shadow 420ms ease;
}

#text-grid-container::-webkit-scrollbar {
  width: 0;
  height: 0;
}

#mobile-meta-overlay {
  display: none;
}

#terminal-stage[data-layout="with-panels"] #text-grid-container {
  transform: translateX(0);
}

#transcript-output {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 0.95rem;
  margin: 0;
  color: var(--cga-amber);
  background-color: var(--cga-background);
  font-family: 'Perfect-DOS-VGA-437-Win', monospace;
  font-size: var(--terminal-font-size);
  line-height: 1.3;
  white-space: pre-wrap;
  letter-spacing: 0;
  overflow-wrap: anywhere;
  word-break: break-word;
  user-select: text;
  width: 100%;
  min-height: 100%;
  min-width: 0;
  box-sizing: border-box;
}

#transcript-layer {
  position: relative;
  width: 100%;
  min-width: 0;
  min-height: 100%;
}

#transcript-output[data-transitioning="true"] {
  visibility: hidden;
}

#transcript-clear-overlay {
  position: absolute;
  inset: 0;
  display: none;
  color: var(--cga-amber);
  font-family: 'Perfect-DOS-VGA-437-Win', monospace;
  font-size: var(--terminal-font-size);
  line-height: 1.3;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  pointer-events: none;
  z-index: 2;
}

#transcript-clear-overlay[data-visible="true"] {
  display: block;
}

#restart-countdown {
  position: absolute;
  right: 0.75rem;
  bottom: 0.4rem;
  width: fit-content;
  padding: 0.15rem 0.5rem;
  display: none;
  color: #fff4bf;
  background: rgba(0, 0, 0, 0.88);
  border: 1px solid rgba(255, 191, 0, 0.45);
  box-shadow: 0 0 10px rgba(255, 191, 0, 0.12);
  z-index: 3;
}

#restart-countdown[data-visible="true"] {
  display: block;
}

.transcript-entry {
  display: flex;
  flex-direction: column;
  gap: 0.32rem;
}

.transcript-entry-meta {
  gap: 0;
}

.transcript-command,
.transcript-response,
.transcript-system,
.transcript-meta {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.transcript-command {
  color: rgba(255, 191, 0, 0.82);
}

.transcript-response,
.transcript-system {
  color: var(--cga-amber);
}

.transcript-link {
  color: #ffe27a;
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 0.12em;
}

.transcript-link:visited {
  color: #f0d15c;
}

.transcript-link:hover,
.transcript-link:focus-visible {
  color: #fff4bf;
}

.transcript-meta {
  min-height: 1.3lh;
  color: #9dff9f;
  opacity: 0.92;
  text-shadow: 0 0 10px rgba(157, 255, 159, 0.18);
}

.transcript-entry-meta[data-tone="hacker"] .transcript-meta,
.transcript-meta[data-tone="hacker"] {
  color: var(--meta-ilex-red);
  text-shadow: 0 0 10px rgba(255, 91, 71, 0.22);
}

.transcript-entry-meta[data-tone="lackey"] .transcript-meta,
.transcript-meta[data-tone="lackey"] {
  color: #9dff9f;
  text-shadow: 0 0 10px rgba(157, 255, 159, 0.18);
}

.transcript-entry-meta[data-tone="aware"] .transcript-meta,
.transcript-meta[data-tone="aware"] {
  color: #ffffff;
  text-shadow: 0 0 10px rgba(255, 255, 255, 0.24);
}

.transcript-entry-meta[data-cleared="true"] .transcript-meta {
  text-shadow: none;
}

#panel-stack {
  display: flex;
  flex-direction: column;
  gap: var(--panel-gap);
  min-width: 0;
  flex: 0 0 0;
  width: 0;
  max-width: 0;
  opacity: 0;
  overflow: hidden;
  transform: translateX(24px);
  pointer-events: none;
  transition: flex-basis 420ms ease, width 420ms ease, max-width 420ms ease, opacity 240ms ease, transform 420ms ease;
}

#terminal-stage[data-layout="with-panels"] #panel-stack {
  flex-basis: var(--panel-width);
  width: var(--panel-width);
  max-width: var(--panel-width);
  opacity: 1;
  transform: translateX(0);
  pointer-events: auto;
}

.ui-panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 10px 12px;
  border: 1px solid rgba(255, 191, 0, 0.86);
  background: linear-gradient(180deg, rgba(255, 191, 0, 0.05), rgba(255, 191, 0, 0.015));
  box-shadow: inset 0 0 18px rgba(255, 191, 0, 0.06), 0 0 18px rgba(255, 191, 0, 0.06);
  color: var(--cga-amber);
  font-family: 'Perfect-DOS-VGA-437-Win', monospace;
  max-height: 320px;
  overflow: hidden;
  transition: max-height 320ms ease, padding 320ms ease, border-color 320ms ease, opacity 220ms ease, transform 320ms ease, margin 320ms ease;
}

.ui-panel[data-state="locked"] {
  opacity: 0.62;
}

.ui-panel[data-state="degraded"] {
  opacity: 0.78;
}

.ui-panel[data-visible="false"] {
  max-height: 0;
  padding-top: 0;
  padding-bottom: 0;
  border-color: transparent;
  opacity: 0;
  transform: translateY(-8px);
}

.ui-panel[data-visible="false"] .panel-heading,
.ui-panel[data-visible="false"] .panel-body {
  opacity: 0;
}

.ui-panel[data-visible="true"] {
  transform: translateY(0);
}

.panel-heading {
  margin-bottom: 8px;
  padding-bottom: 6px;
  border-bottom: 1px solid rgba(255, 191, 0, 0.45);
  font-size: var(--terminal-font-size);
  line-height: 1;
  letter-spacing: 0.06em;
  transition: opacity 220ms ease;
}

.panel-body {
  margin: 0;
  min-height: 6lh;
  color: inherit;
  font-family: inherit;
  font-size: var(--terminal-font-size);
  line-height: 1.18;
  white-space: pre-wrap;
  word-break: break-word;
  transition: opacity 220ms ease;
}

.map-char-current {
  color: var(--cga-amber);
}

.map-char-dim {
  color: var(--cga-amber-dim);
}

#command-bar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  padding: 0;
  border: none;
  background: transparent;
  box-shadow: none;
  font-family: 'Perfect-DOS-VGA-437-Win', monospace;
  color: var(--cga-amber);
  box-sizing: border-box;
}

#command-input-shell {
  position: relative;
  min-width: 0;
}

#command-prompt {
  position: absolute;
  top: 50%;
  left: 10px;
  transform: translateY(-50%);
  z-index: 1;
  font-size: var(--terminal-font-size);
  line-height: 1;
  pointer-events: none;
}

#cli-input {
  width: 100%;
  min-width: 0;
  height: 38px;
  padding: 0 10px 0 26px;
  border: 1px solid rgba(255, 191, 0, 0.5);
  background-color: rgba(0, 0, 0, 0.94);
  color: var(--cga-amber);
  font-family: 'Perfect-DOS-VGA-437-Win', monospace;
  font-size: var(--terminal-font-size);
  line-height: 1;
  box-sizing: border-box;
  caret-color: var(--cga-amber);
}

#cli-input::placeholder {
  color: rgba(255, 191, 0, 0.5);
}

#cli-input:focus {
  outline: none;
  border-color: var(--cga-amber);
}

#command-submit {
  height: 38px;
  padding: 0 12px;
  border: 1px solid rgba(255, 191, 0, 0.5);
  background-color: rgba(255, 191, 0, 0.08);
  color: var(--cga-amber);
  font-family: 'Perfect-DOS-VGA-437-Win', monospace;
  font-size: var(--terminal-font-size);
  text-transform: uppercase;
}

#mobile-panel-tabs {
  display: none;
}

.mobile-panel-tab {
  border: 1px solid rgba(255, 191, 0, 0.5);
  background: rgba(255, 191, 0, 0.06);
  color: var(--cga-amber);
  font-family: 'Perfect-DOS-VGA-437-Win', monospace;
  font-size: var(--terminal-font-size);
  line-height: 1;
  text-transform: uppercase;
}

body[data-device-theme="mobile"] {
  display: block;
  overflow: auto;
  --terminal-font-size: clamp(15px, 1vw + 0.607rem, 18px);
}

body[data-device-theme="mobile"] #screen-shell {
  align-items: flex-start;
  justify-content: flex-start;
  min-height: var(--visual-viewport-height);
  height: auto;
  padding-top: max(8px, env(safe-area-inset-top));
  padding-right: max(10px, env(safe-area-inset-right));
  padding-bottom: calc(var(--mobile-command-bar-height) + 26px + env(safe-area-inset-bottom) + var(--mobile-keyboard-offset));
  padding-left: max(10px, env(safe-area-inset-left));
}

body[data-device-theme="mobile"] #terminal-stage {
  width: 100%;
  max-width: 100%;
  max-height: none;
  gap: 8px;
  min-height: calc(var(--visual-viewport-height) - max(8px, env(safe-area-inset-top)) - calc(var(--mobile-command-bar-height) + 26px + env(safe-area-inset-bottom) + var(--mobile-keyboard-offset)));
}

body[data-device-theme="mobile"] #status-bar,
body[data-device-theme="mobile"] #terminal-stage[data-layout="transcript-only"] #status-bar,
body[data-device-theme="mobile"] #terminal-stage[data-layout="with-panels"] #status-bar {
  position: sticky;
  top: 0;
  z-index: 3;
  width: 100%;
  align-self: stretch;
  align-items: center;
  justify-content: center;
  padding: 8px 10px;
  box-sizing: border-box;
}

body[data-device-theme="mobile"] .meta-column {
  display: none;
}

body[data-device-theme="mobile"] #mobile-panel-tabs[data-visible="true"] {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

body[data-device-theme="mobile"] .mobile-panel-tab {
  min-height: 34px;
  padding: 8px 10px 7px;
}

body[data-device-theme="mobile"] .mobile-panel-tab[data-active="true"] {
  background: rgba(255, 191, 0, 0.15);
  box-shadow: inset 0 0 0 1px rgba(255, 191, 0, 0.15);
}

body[data-device-theme="mobile"] #terminal-content,
body[data-device-theme="mobile"] #terminal-stage[data-layout="with-panels"] #terminal-content {
  flex-direction: column;
  gap: 8px;
  min-height: 0;
}

body[data-device-theme="mobile"] #text-grid-container,
body[data-device-theme="mobile"] #terminal-stage[data-layout="with-panels"] #text-grid-container {
  width: 100%;
  max-width: 100%;
  min-height: min(58svh, 520px);
  max-height: min(58svh, 520px);
  justify-content: flex-start;
  align-items: flex-start;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  transform: none;
  box-sizing: border-box;
}

body[data-device-theme="mobile"] #mobile-meta-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 2;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 10px 12px 0;
  pointer-events: none;
}

body[data-device-theme="mobile"] #mobile-meta-overlay[data-visible="true"] {
  background: linear-gradient(180deg, rgba(0, 0, 0, 0.92) 0%, rgba(0, 0, 0, 0.68) 52%, rgba(0, 0, 0, 0) 100%);
}

body[data-device-theme="mobile"] #mobile-meta-line {
  font-size: calc(var(--terminal-font-size) * 0.9);
}

body[data-device-theme="mobile"] #transcript-output {
  width: 100%;
  min-height: 100%;
  margin: 0;
  font-size: clamp(15px, 0.95rem + 0.3vw, 18px);
  line-height: 1.36;
  gap: 1rem;
  padding-top: 1.9rem;
}

body[data-device-theme="mobile"] #panel-stack,
body[data-device-theme="mobile"] #terminal-stage[data-layout="with-panels"] #panel-stack {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex-basis: auto;
  width: 100%;
  max-width: 100%;
  opacity: 0;
  max-height: 0;
  overflow: hidden;
  transform: none;
  pointer-events: auto;
}

body[data-device-theme="mobile"] #terminal-stage[data-mobile-panel-open="true"] #panel-stack {
  opacity: 1;
  max-height: 60svh;
  overflow: visible;
}

body[data-device-theme="mobile"] .ui-panel {
  width: 100%;
  max-height: none;
  padding: 8px 10px;
  box-sizing: border-box;
  background: linear-gradient(180deg, rgba(255, 191, 0, 0.08), rgba(255, 191, 0, 0.02));
}

body[data-device-theme="mobile"] .ui-panel[data-visible="false"] {
  max-height: 0;
  padding-top: 0;
  padding-bottom: 0;
  overflow: hidden;
}

body[data-device-theme="mobile"] .panel-heading {
  margin-bottom: 6px;
  padding-bottom: 5px;
}

body[data-device-theme="mobile"] .panel-body {
  min-height: 0;
  max-height: min(28lh, 44svh);
  overflow: auto;
  font-size: inherit;
  line-height: 1.28;
}

body[data-device-theme="mobile"] #command-bar {
  position: fixed;
  left: max(10px, env(safe-area-inset-left));
  right: max(10px, env(safe-area-inset-right));
  bottom: calc(10px + env(safe-area-inset-bottom) + var(--mobile-keyboard-offset));
  z-index: 5;
}

body[data-device-theme="mobile"] #command-prompt {
  font-size: 16px;
  line-height: 1;
  left: 10px;
}

body[data-device-theme="mobile"] #cli-input {
  height: 36px;
  padding-left: 26px;
  font-size: clamp(16px, 1rem + 0.2vw, 18px);
}

body[data-device-theme="mobile"] #command-submit {
  height: 36px;
  font-size: 14px;
}

body[data-device-theme="mobile"] #command-submit:active {
  background-color: rgba(255, 191, 0, 0.18);
}

body[data-device-theme="mobile"][data-mobile-input-active="true"] #command-bar {
  box-shadow: none;
}

body[data-device-theme="mobile"][data-mobile-input-active="true"] #terminal-stage {
  min-height: calc(var(--visual-viewport-height) - max(8px, env(safe-area-inset-top)) - calc(var(--mobile-command-bar-height) + 26px + env(safe-area-inset-bottom)));
}

body[data-device-theme="mobile"][data-mobile-input-active="true"] #text-grid-container,
body[data-device-theme="mobile"][data-mobile-input-active="true"] #terminal-stage[data-layout="with-panels"] #text-grid-container {
  min-height: min(46dvh, calc((var(--visual-viewport-height) - var(--mobile-command-bar-height) - 32px) * 0.5));
  max-height: min(46dvh, calc((var(--visual-viewport-height) - var(--mobile-command-bar-height) - 32px) * 0.5));
}

body[data-device-theme="mobile"][data-mobile-input-active="true"] #panel-stack,
body[data-device-theme="mobile"][data-mobile-input-active="true"] #terminal-stage[data-layout="with-panels"] #panel-stack {
  max-height: min(24dvh, calc((var(--visual-viewport-height) - var(--mobile-command-bar-height) - 40px) * 0.28));
}

@media (max-width: 700px) {
  html,
  body {
    overflow: auto;
  }

  body {
    display: block;
  }

  #terminal-stage[data-layout="transcript-only"] #status-bar,
  #terminal-stage[data-layout="with-panels"] #status-bar {
    width: 100%;
    align-self: stretch;
  }

  #screen-shell {
    align-items: flex-start;
    min-height: 100dvh;
    height: auto;
  }

  #terminal-stage {
    width: 100%;
    max-height: none;
  }

  #terminal-content {
    flex-direction: column;
    gap: var(--panel-gap);
  }

  #text-grid-container {
    width: 100%;
    max-width: 100%;
    justify-content: flex-start;
    align-items: flex-start;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
  }

  #panel-stack {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    width: 100%;
    max-width: 100%;
    transform: translateY(16px);
  }

  #terminal-stage[data-layout="with-panels"] #panel-stack {
    flex-basis: auto;
    width: 100%;
    max-width: 100%;
    transform: translateY(0);
  }

  #terminal-stage[data-layout="with-panels"] #text-grid-container {
    transform: none;
  }

  .meta-column {
    display: none;
  }

  .ui-panel {
    max-height: none;
  }

  .panel-body {
    min-height: 8lh;
    max-height: 18lh;
    overflow: auto;
  }
}

@media (max-width: 760px) {
  body:not([data-device-theme="mobile"]) {
    display: block;
  }

  :root {
    --screen-shell-padding: max(8px, env(safe-area-inset-left));
    --terminal-frame-padding: 8px;
    --panel-gap: 8px;
    --terminal-font-size: clamp(11px, 1.8vw + 0.2875rem, 13px);
  }

  #screen-shell {
    padding-top: max(8px, env(safe-area-inset-top));
    padding-right: max(8px, env(safe-area-inset-right));
    padding-bottom: calc(var(--mobile-command-bar-height) + 16px + env(safe-area-inset-bottom));
    padding-left: max(8px, env(safe-area-inset-left));
  }

  #terminal-stage {
    gap: 8px;
  }

  #status-bar {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
    padding: 8px 10px;
  }

  #status-title-group,
  #status-stats {
    width: 100%;
    justify-content: space-between;
    min-width: 0;
  }

  #status-title-group {
    gap: 6px;
  }

  #game-title,
  #current-location,
  #turn-counter,
  #score-counter {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .status-divider {
    display: none;
  }

  #text-grid-container {
    min-height: min(62dvh, 520px);
    border-width: 1px;
    box-shadow: 0 0 18px rgba(255, 191, 0, 0.12);
  }

  #text-grid {
    min-width: max-content;
    padding-bottom: 2px;
  }

  #panel-stack {
    grid-template-columns: minmax(0, 1fr);
  }

  .ui-panel {
    padding: 8px 10px;
  }

  .panel-heading {
    margin-bottom: 6px;
    padding-bottom: 5px;
  }

  .panel-body {
    min-height: 0;
    max-height: 16lh;
  }

  body:not([data-device-theme="mobile"]) #command-bar {
    grid-template-columns: minmax(0, 1fr);
  }

  body:not([data-device-theme="mobile"]) #command-submit {
    display: none;
  }
}

@media (max-width: 420px) {
  :root {
    --terminal-font-size: clamp(11px, 2vw + 0.2675rem, 12px);
  }

  #status-bar {
    padding: 7px 9px;
  }

  #status-title-group,
  #status-stats {
    gap: 8px;
  }
}

@font-face {
  font-family: 'Perfect-DOS-VGA-437-Win';
  src:url('assets/fnt/Perfect-DOS-VGA-437-Win.ttf.woff') format('woff'),
      url('assets/fnt/Perfect-DOS-VGA-437-Win.ttf.svg#Perfect-DOS-VGA-437-Win') format('svg'),
      url('assets/fnt/Perfect-DOS-VGA-437-Win.ttf.eot'),
      url('assets/fnt/Perfect-DOS-VGA-437-Win.ttf.eot?#iefix') format('embedded-opentype'); 
  font-weight: normal;
  font-style: normal;
}
