@import "../shell/shell-surface.css";

/* Engine HUD shell — shared by every title. Title packs only persist player prefs in save.json. */

#hud-shell {
  position: absolute;
  z-index: 14;
  pointer-events: none;
  width: var(--shell-width, 300px);
  max-width: var(--shell-width, 300px);
  box-sizing: border-box;
  overflow: hidden;
  transform: scale(var(--shell-scale, 1));
  transform-origin: top right;
  display: flex;
  flex-direction: column;
  gap: 0;
  user-select: none;
  -webkit-user-select: none;
  contain: layout style paint;
  /* Resize handle is absolutely pinned to the shell bottom (whole-HUD resize). */
  isolation: isolate;
}

#hud-shell[data-shell-height="manual"] {
  height: var(--shell-manual-height);
  max-height: var(--shell-manual-height);
}

/* Manual height: scroll region always consumes space below pinned chrome. */
#hud-shell[data-shell-height="manual"] #shell-scroll-body {
  flex: 1 1 0;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
  pointer-events: auto;
}

#hud-shell[data-shell-height="manual"]:not(.is-drawer-open) #shell-scroll-body {
  overflow: hidden;
}

/* Manual height: allow collapsing through scene title even when a drawer tab is active. */
#hud-shell[data-shell-height="manual"].is-drawer-open #drawer-host.is-open,
#hud-shell[data-shell-height="manual"].is-drawer-open #drawer-host.is-open .drawer-panel,
#hud-shell[data-shell-height="manual"].is-resizing #drawer-host.is-open,
#hud-shell[data-shell-height="manual"].is-resizing #drawer-host.is-open .drawer-panel {
  min-height: 0 !important;
  max-height: 100% !important;
}

/* Scroll region grows when drawer is open or user is resizing the shell. */
#hud-shell.is-drawer-open #shell-scroll-body,
#hud-shell.is-resizing #shell-scroll-body {
  flex: 1 1 0;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
  pointer-events: auto;
}

#hud-shell.is-drawer-open #drawer-host.is-open,
#hud-shell.is-resizing #drawer-host.is-open {
  flex: 1 1 0;
  min-height: 280px;
  max-height: none !important;
  display: flex;
  flex-direction: column;
    transition: opacity var(--shell-drawer-duration, 180ms)
    var(--shell-drawer-easing, cubic-bezier(0.22, 1, 0.36, 1));
}

#hud-shell.is-resizing #drawer-host.is-open,
#hud-shell.is-resizing #drawer-host.is-open .drawer-panel {
  min-height: 0 !important;
}

#hud-shell.is-drawer-open #drawer-host.is-open .drawer-panel.has-shell-stats,
#hud-shell.is-resizing #drawer-host.is-open .drawer-panel.has-shell-stats {
  flex: 1 1 0;
  min-height: 280px;
  height: auto;
  max-height: none !important;
  display: flex;
  flex-direction: column;
  position: relative;
  transform: none;
  transition: none;
}

#hud-shell.is-drawer-open #drawer-host.is-open .drawer-panel[data-active-drawer="menu"] .drawer-iframe.is-active,
#hud-shell.is-resizing #drawer-host.is-open .drawer-panel[data-active-drawer="menu"] .drawer-iframe.is-active {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

#hud-shell.is-resizing #drawer-host,
#hud-shell.is-resizing .drawer-panel {
  transition: none !important;
}

#hud-shell[data-shell-anchor="top-right"] {
  top: calc(var(--shell-anchor-y, 0) * 100vh / var(--shell-scale, 1));
  right: 0;
  left: auto;
  height: calc((1 - var(--shell-anchor-y, 0)) * 100vh / var(--shell-scale, 1));
  max-height: calc((1 - var(--shell-anchor-y, 0)) * 100vh / var(--shell-scale, 1));
  transform-origin: top right;
}

#hud-shell[data-shell-anchor="top-left"] {
  top: calc(var(--shell-anchor-y, 0) * 100vh / var(--shell-scale, 1));
  left: 0;
  right: auto;
  height: calc((1 - var(--shell-anchor-y, 0)) * 100vh / var(--shell-scale, 1));
  max-height: calc((1 - var(--shell-anchor-y, 0)) * 100vh / var(--shell-scale, 1));
  transform-origin: top left;
}

#hud-shell[data-shell-anchor="top-right"] #shell-scroll-body,
#hud-shell[data-shell-anchor="top-left"] #shell-scroll-body {
  flex: 1 1 0;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
  pointer-events: auto;
}

#hud-shell[data-shell-position="manual"] {
  right: auto !important;
  transform-origin: top left;
}

#shell-scroll-body {
  position: relative;
  flex: 1 1 auto;
  width: 100%;
  max-width: 100%;
  min-height: 0;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
}

#shell-pinned {
  flex: 0 0 auto;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  pointer-events: auto;
}

#shell-hud-title {
  flex: 0 0 auto;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  margin: 0 0 4px;
  background: transparent;
  min-height: 1px;
}

.shell-hud-title-img {
  display: block;
  width: 100%;
  max-width: 100%;
  height: auto;
  object-fit: contain;
  object-position: top center;
  opacity: 0.55;
  pointer-events: none;
  user-select: none;
  -webkit-user-drag: none;
}

#shell-scene-title {
  flex: 0 0 auto;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  height: 64px;
  min-height: 64px;
  max-height: 64px;
  margin-top: 4px;
  border: 1px solid rgba(120, 128, 148, 0.35);
  border-radius: 4px;
  background: var(--shell-surface-bg);
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 8px;
}

.shell-scene-title-text {
  width: 100%;
  font-family: var(--score-font, system-ui, sans-serif);
  font-weight: 700;
  font-size: clamp(0.72rem, 3.2vw, 1rem);
  line-height: 1;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  text-align: center;
  color: #eef1f6;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.shell-scene-title-text.is-multiline {
  white-space: pre-line;
  line-height: 1.05;
  text-overflow: clip;
}

.shell-drag-handle {
  flex: 0 0 18px;
  flex-shrink: 0;
  height: 18px;
  margin-bottom: 2px;
  border-radius: 4px;
  pointer-events: auto;
  cursor: grab;
  touch-action: none;
  background: linear-gradient(
    180deg,
    rgba(139, 149, 171, 0.22) 0%,
    rgba(139, 149, 171, 0.08) 100%
  );
  border: 1px solid rgba(120, 128, 148, 0.28);
  position: relative;
  box-sizing: border-box;
}

.shell-drag-grip {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  gap: 3px;
  width: 28px;
  pointer-events: none;
}

.shell-drag-grip span {
  display: block;
  width: 28px;
  height: 2px;
  flex: 0 0 2px;
  border-radius: 999px;
  background: rgba(139, 149, 171, 0.55);
}

.shell-drag-handle::after {
  content: none;
}

#hud-shell.is-dragging .shell-drag-handle {
  cursor: grabbing;
  background: linear-gradient(
    180deg,
    rgba(72, 196, 255, 0.28) 0%,
    rgba(72, 196, 255, 0.1) 100%
  );
  border-color: rgba(72, 196, 255, 0.45);
}

#hud-shell.is-dragging {
  user-select: none;
}

#hud-shell .shell-drag-handle {
  pointer-events: auto;
}

.shell-resize-handle {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  height: 14px;
  border-radius: 0 0 4px 4px;
  pointer-events: auto;
  cursor: ns-resize;
  touch-action: none;
  background: linear-gradient(
    180deg,
    rgba(18, 22, 30, 0.35) 0%,
    rgba(139, 149, 171, 0.55) 100%
  );
  border: 1px solid rgba(120, 128, 148, 0.45);
  border-top-color: rgba(72, 196, 255, 0.35);
  box-shadow: 0 -3px 10px rgba(0, 0, 0, 0.35);
}

.shell-resize-handle::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 28px;
  height: 4px;
  transform: translate(-50%, -50%);
  border-radius: 999px;
  background: rgba(139, 149, 171, 0.55);
}

#hud-shell.is-resizing .shell-resize-handle {
  cursor: ns-resize;
  background: linear-gradient(
    180deg,
    rgba(72, 196, 255, 0.22) 0%,
    rgba(72, 196, 255, 0.45) 100%
  );
  border-color: rgba(72, 196, 255, 0.55);
  border-top-color: rgba(72, 196, 255, 0.65);
}

#hud-shell.is-resizing {
  user-select: none;
}

#hud-shell .shell-resize-handle {
  pointer-events: auto;
}

#hud-shell.shell-hidden {
  visibility: hidden;
  opacity: 0;
  overflow: hidden;
  clip-path: inset(100%);
  pointer-events: none !important;
}

#hud-shell.shell-hidden * {
  pointer-events: none !important;
}

#hud-shell.shell-hidden #drawer-host,
#hud-shell.shell-hidden .drawer-panel,
#hud-shell.shell-hidden .drawer-iframe {
  visibility: hidden !important;
  opacity: 0 !important;
}

#hud-shell:not(.shell-hidden) {
  visibility: visible;
}

.shell-close-btn {
  position: absolute;
  top: 0;
  bottom: 0;
  right: 4px;
  z-index: 12;
  width: 16px;
  height: 16px;
  margin-block: auto;
  padding: 0;
  border: none;
  border-radius: 2px;
  background: transparent;
  color: rgba(220, 226, 236, 0.82);
  font-size: 14px;
  line-height: 1;
  font-family: system-ui, -apple-system, sans-serif;
  cursor: pointer;
  pointer-events: auto;
  display: flex;
  align-items: center;
  justify-content: center;
}

.shell-close-btn:hover {
  color: #fff;
  background: rgba(72, 196, 255, 0.18);
}

.shell-close-btn:focus-visible {
  outline: 1px solid #fff;
  outline-offset: 1px;
}

.shell-restore-toggle {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 52;
  width: 36px;
  height: 36px;
  padding: 0;
  border: 1px solid rgba(120, 128, 148, 0.45);
  border-radius: 4px;
  background: rgba(28, 32, 40, 0.9);
  color: #fff;
  cursor: pointer;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.4);
  display: none;
  align-items: center;
  justify-content: center;
}

.shell-restore-toggle.is-visible {
  display: flex;
}

.shell-restore-toggle:hover {
  background: rgba(42, 48, 58, 0.96);
  border-color: rgba(160, 168, 184, 0.55);
}

.shell-restore-toggle:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
}

.shell-restore-icon {
  display: flex;
  flex-direction: column;
  gap: 3px;
  width: 16px;
}

.shell-restore-icon span {
  display: block;
  height: 2px;
  border-radius: 999px;
  background: #fff;
}

#hud-shell #shell-pinned,
#hud-shell #shell-scroll-body,
#hud-shell #score-module,
#hud-shell #shell-hud-title,
#hud-shell #shell-chrome,
#hud-shell #shell-scene-title,
#hud-shell #drawer-host.is-open {
  pointer-events: auto;
}

#score-module {
  flex: 0 0 auto;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
}

#shell-chrome {
  flex: 0 0 auto;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  display: flex;
  align-items: stretch;
  justify-content: stretch;
  gap: 0;
  margin-top: 4px;
  border: 1px solid rgba(120, 128, 148, 0.35);
  border-radius: 4px;
  background: var(--shell-surface-bg);
  overflow: hidden;
}

.shell-tab {
  flex: 1 1 0;
  min-width: 0;
  padding: 7px 4px;
  border: none;
  border-right: 1px solid rgba(120, 128, 148, 0.25);
  background: transparent;
  font-family: var(--score-font, system-ui, sans-serif);
  font-size: calc(11px * 1.3);
  font-weight: 800;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--score-muted, #8b95ab);
  cursor: pointer;
  line-height: 1.3;
  white-space: normal;
}

.shell-tab:last-child {
  border-right: none;
}

.shell-tab.is-active {
  background: rgba(139, 149, 171, 0.12);
  color: var(--score-text, #e8ecf4);
}

.shell-tab:focus-visible {
  outline: 2px solid rgba(139, 149, 171, 0.75);
  outline-offset: -2px;
}

#drawer-host {
  position: relative;
  flex: 0 0 auto;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  overflow: hidden;
  max-height: 0;
  opacity: 0;
  pointer-events: none;
  transition:
    max-height var(--shell-drawer-duration, 180ms) var(--shell-drawer-easing, cubic-bezier(0.22, 1, 0.36, 1)),
    opacity var(--shell-drawer-duration, 180ms) var(--shell-drawer-easing, cubic-bezier(0.22, 1, 0.36, 1));
}

#drawer-host.is-open {
  flex: 1 1 0;
  min-height: 0;
  max-height: none !important;
  opacity: 1;
  pointer-events: auto;
  margin-top: 4px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#drawer-host.is-closing {
  pointer-events: none;
}

.drawer-panel {
  position: relative;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  flex: 1 1 0;
  min-height: 0;
  height: auto;
  max-height: min(var(--shell-drawer-max-height, 85vh), 720px);
  background: var(--shell-surface-bg);
  border: 1px solid rgba(120, 128, 148, 0.35);
  border-radius: 4px;
  overflow: hidden;
  transform: translateY(-100%);
  transition: transform var(--shell-drawer-duration, 180ms)
    var(--shell-drawer-easing, cubic-bezier(0.22, 1, 0.36, 1));
}

#hud-shell[data-shell-anchor] .drawer-panel {
  max-height: none !important;
}

.drawer-panel.has-shell-stats {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Stats tab — shell stats drawer fills panel (same as legacy iframe). */
.drawer-panel[data-active-drawer="stats"] #stats-drawer-root {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.drawer-panel[data-active-drawer="stats"] #stats-scroll {
  flex: 1 1 0;
  min-height: 0;
  height: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px;
  box-sizing: border-box;
}

.drawer-panel[data-active-drawer="stats"] #stats-panel {
  flex: 0 0 auto;
}

.drawer-panel[data-active-drawer="stats"] .drawer-iframe[data-drawer-id="stats"] {
  display: none;
}

.drawer-panel[data-active-drawer="menu"] #stats-drawer-root {
  display: none;
}

.drawer-panel[data-active-drawer="menu"] .drawer-iframe.is-active {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

#drawer-host.is-open .drawer-panel {
  transform: translateY(0);
}

/* Preload iframes at full drawer width while host is collapsed — clipped, not visible. */
#drawer-host:not(.is-open) {
  content-visibility: hidden;
  contain: strict;
}

#drawer-host:not(.is-open) .drawer-panel {
  position: absolute;
  top: 0;
  left: 0;
  visibility: hidden;
  pointer-events: none;
}

.drawer-iframe {
  display: block;
  position: absolute;
  inset: 0;
  width: 100%;
  max-width: 100%;
  height: 100%;
  border: none;
  background: transparent;
  pointer-events: none;
  visibility: hidden;
  box-sizing: border-box;
}

.drawer-iframe.is-active {
  visibility: visible;
  pointer-events: auto;
  z-index: 1;
}

#pause-dim {
  position: fixed;
  inset: 0;
  /* Below #scoring-hud (12) so PAUSE/RESUME chrome stays clickable while dimmed. */
  z-index: 11;
  pointer-events: none;
  background: rgba(0, 0, 0, var(--shell-pause-dim-opacity, 0.45));
  opacity: 0;
  transition: opacity 120ms ease;
}

#pause-dim.is-active {
  opacity: 1;
  /* Block clicks on gameplay area only — shell chrome remains above this layer. */
  pointer-events: auto;
}

#hud-shell.is-scene-loading #score-module::after {
  content: "Switching level…";
  display: block;
  font-family: var(--score-font, system-ui, sans-serif);
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--score-cyan, #48c4ff);
  text-align: center;
  padding: 4px 0 2px;
  animation: scene-loading-pulse 1.2s ease-in-out infinite;
}
