:root {

  --stage-background: #FFFFFF;
  --page-background: #090e16;
  --panel-background: rgba(17, 24, 39, 0.92);
  --panel-border: rgba(255, 255, 255, 0.1);
  --text: #f4f7fb;
  --muted: #9eabc0;
  --accent: #73d7ff;
  --head-pivot-x: 50%;
  --head-pivot-y: 58%;
  --head-look-distance: 6px;
  --head-look-angle: 0.7deg;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  background: var(--page-background);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  overflow-x: hidden;
}
button, select, input { font: inherit; }
button { cursor: pointer; }

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(320px, 430px);
}

.stage-area {
  min-height: 100vh;
  display: grid;
  place-items: center;
  position: relative;
  background: var(--stage-background);
  overflow: hidden;
  padding: 2vh 2vw;
}

.loading-status {
  position: absolute;
  z-index: 100;
  inset: auto auto 24px 24px;
  padding: 10px 14px;
  border-radius: 999px;
  background: rgba(0,0,0,.58);
  color: white;
  font-size: 14px;
  backdrop-filter: blur(8px);
  transition: opacity .25s ease;
}
.loading-status.is-hidden { opacity: 0; pointer-events: none; }

.robot-motion {
  width: min(90vw, 90vh, 1000px);
  aspect-ratio: 1 / 1;
  position: relative;
  will-change: transform;
}

.robot-stage {
  position: relative;
  width: 100%;
  height: 100%;
  aspect-ratio: 1 / 1;
  transform-origin: 50% 80%;
  will-change: transform;
}

.robot-layer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  user-select: none;
  -webkit-user-drag: none;
  backface-visibility: hidden;
  transform: translateZ(0);
}

/* Layer stack follows the numeric filename prefix exactly.
   Higher prefix number = visually above lower prefix number.
   02 Head < 03 Eyes < 04 Mouth < 05 Body < 06 Left Hand < 07 Right Hand < 08 Left Leg < 09 Right Leg. */
.layer-head       { z-index: 2; transform-origin: var(--head-pivot-x) var(--head-pivot-y); }
.layer-eyes       { z-index: 3; }
.layer-mouth      { z-index: 4; }
.layer-body       { z-index: 5; }
.layer-left-hand  { z-index: 6; }
.layer-right-hand { z-index: 7; }
.layer-left-leg   { z-index: 8; }
.layer-right-leg  { z-index: 9; }

.robot-stage.idle-active .idle-body {
  animation: idleBody 2.8s ease-in-out infinite;
}
.robot-stage.idle-active .idle-head {
  animation: idleHead 3.4s ease-in-out infinite;
}
.robot-stage.idle-active .idle-hand-left {
  animation: idleLeftHand 3.2s ease-in-out infinite;
}
.robot-stage.idle-active .idle-hand-right {
  animation: idleRightHand 3.5s ease-in-out infinite;
}
.robot-stage.idle-active .idle-limb {
  animation: idleLeg 3.0s ease-in-out infinite;
}

@keyframes idleBody {
  0%,100% { transform: translateY(0); }
  50% { transform: translateY(-3px); }
}
@keyframes idleHead {
  0%,100% { transform: translateY(0) rotate(0); }
  50% { transform: translateY(-2px) rotate(.35deg); }
}
@keyframes idleLeftHand {
  0%,100% { transform: translateY(0) rotate(0); }
  50% { transform: translateY(-2px) rotate(-.35deg); }
}
@keyframes idleRightHand {
  0%,100% { transform: translateY(0) rotate(0); }
  50% { transform: translateY(-2px) rotate(.35deg); }
}
@keyframes idleLeg {
  0%,100% { transform: translateY(0); }
  50% { transform: translateY(1px); }
}

.robot-motion.is-bouncing { animation: robotBounce 600ms cubic-bezier(.2,.8,.2,1); }
@keyframes robotBounce {
  0%,100% { transform: translateY(0) scale(1); }
  42% { transform: translateY(-2.5%) scale(1.01,.99); }
  72% { transform: translateY(.8%) scale(.995,1.005); }
}

.robot-stage.look-left .layer-head { transform: translateX(calc(-1 * var(--head-look-distance))) rotate(calc(-1 * var(--head-look-angle))); }
.robot-stage.look-right .layer-head { transform: translateX(var(--head-look-distance)) rotate(var(--head-look-angle)); }
.robot-stage.look-center .layer-head { transform: translateX(0) rotate(0); }

.robot-stage.dance-sway { animation: danceSway 700ms ease-in-out infinite alternate; }
.robot-stage.dance-sway .layer-head { animation: danceHead 600ms ease-in-out infinite alternate; }
@keyframes danceSway {
  from { transform: translateX(-1.3%) rotate(-1deg); }
  to   { transform: translateX(1.3%) rotate(1deg); }
}
@keyframes danceHead {
  from { transform: rotate(-.8deg); }
  to   { transform: rotate(.8deg); }
}

.control-panel {
  min-height: 100vh;
  background: var(--panel-background);
  border-left: 1px solid var(--panel-border);
  padding: 24px;
  overflow: auto;
}
.panel-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
h1 { font-size: 22px; margin: 0 0 5px; }
.panel-heading p { color: var(--muted); margin: 0; font-size: 13px; }
.state-badge { font-size: 11px; letter-spacing: .08em; border: 1px solid var(--panel-border); border-radius: 999px; padding: 7px 9px; color: var(--accent); }
.panel-section { border-top: 1px solid var(--panel-border); margin-top: 22px; padding-top: 18px; }
.panel-section h2 { font-size: 13px; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); margin: 0 0 12px; }
.button-grid { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 8px; }
button {
  min-height: 38px;
  color: var(--text);
  background: rgba(255,255,255,.055);
  border: 1px solid var(--panel-border);
  border-radius: 9px;
  padding: 8px 10px;
  transition: background .15s ease, border-color .15s ease, transform .1s ease;
}
button:hover { background: rgba(255,255,255,.1); border-color: rgba(115,215,255,.45); }
button:active { transform: translateY(1px); }
button:disabled { opacity: .45; cursor: not-allowed; }
.speed-controls { display: grid; grid-template-columns: 1fr auto; gap: 12px; align-items: center; }
.speed-controls output { min-width: 62px; color: var(--accent); text-align: right; }
.speed-presets { display: grid; grid-template-columns: repeat(5,1fr); gap: 6px; margin-top: 10px; }
.speed-presets button { min-height: 32px; padding: 5px; font-size: 12px; }
.inspector-grid { display: grid; gap: 10px; }
.inspector-grid label { display: grid; grid-template-columns: 1fr 110px; align-items: center; gap: 10px; font-size: 13px; color: var(--muted); }
select { width: 100%; border-radius: 8px; padding: 8px; color: var(--text); background: #111827; border: 1px solid var(--panel-border); }
.note { color: var(--muted); font-size: 13px; line-height: 1.5; }
code { color: var(--accent); }

body.tv-mode { overflow: hidden; cursor: none; }
body.tv-mode .app-shell { display: block; }
body.tv-mode .stage-area { width: 100vw; height: 100vh; min-height: 100vh; padding: 0; }
body.tv-mode .control-panel { display: none; }
body.tv-mode .robot-motion { width: min(94vw, 94vh, 1000px); }
body.tv-mode .loading-status { inset: auto auto 18px 18px; }

@media (max-width: 980px) {
  .app-shell { grid-template-columns: 1fr; }
  .stage-area { min-height: 70vh; }
  .control-panel { min-height: auto; border-left: 0; border-top: 1px solid var(--panel-border); }
  .robot-motion { width: min(94vw, 66vh, 1000px); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 1ms !important; animation-iteration-count: 1 !important; scroll-behavior: auto !important; transition-duration: 1ms !important; }
}


/* ==================== BOOKIE V2 SCREEN PLACEMENT ==================== */
.stage-area { position: relative; }

.robot-viewport {
  --position-duration: 1400ms;
  --bookie-scale: 1;
  position: absolute;
  z-index: 20;
  width: min(52vmin, 720px);
  aspect-ratio: 1 / 1;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%) scale(var(--bookie-scale));
  transition:
    left var(--position-duration) cubic-bezier(.22,.78,.2,1),
    top var(--position-duration) cubic-bezier(.22,.78,.2,1),
    width 700ms cubic-bezier(.22,.78,.2,1);
  will-change: left, top, width;
}
.robot-viewport.no-position-transition { transition: none !important; }
.robot-viewport.size-small  { width: min(30vmin, 420px); }
.robot-viewport.size-medium { width: min(52vmin, 720px); }
.robot-viewport.size-large  { width: min(82vmin, 1000px); }

.robot-viewport.position-center       { left: 50%; top: 50%; }
.robot-viewport.position-top-left     { left: 18%; top: 20%; }
.robot-viewport.position-top-right    { left: 82%; top: 20%; }
.robot-viewport.position-bottom-left  { left: 18%; top: 80%; }
.robot-viewport.position-bottom-right { left: 82%; top: 80%; }

.robot-viewport .robot-motion {
  width: 100%;
  height: 100%;
}

body.tv-mode .robot-viewport .robot-motion { width: 100%; }

.robot-motion.is-running { animation: runBob 260ms ease-in-out infinite alternate; }
@keyframes runBob {
  from { transform: translateY(0) rotate(-.5deg); }
  to   { transform: translateY(-2.2%) rotate(.5deg); }
}

.robot-stage.talking-visual .layer-mouth {
  animation: talkPulse 150ms ease-in-out infinite alternate;
  transform-origin: 50% 50%;
}
@keyframes talkPulse {
  from { transform: scaleY(.96); }
  to   { transform: scaleY(1.05); }
}

@media (max-width: 700px) {
  .robot-viewport.size-small  { width: min(36vmin, 360px); }
  .robot-viewport.size-medium { width: min(58vmin, 620px); }
  .robot-viewport.size-large  { width: min(88vmin, 900px); }
  .robot-viewport.position-top-left     { left: 22%; top: 22%; }
  .robot-viewport.position-top-right    { left: 78%; top: 22%; }
  .robot-viewport.position-bottom-left  { left: 22%; top: 78%; }
  .robot-viewport.position-bottom-right { left: 78%; top: 78%; }
}


/* URL background support. When --stage-background / --page-background are set to
   transparent by script.js, the whole Bookie canvas remains transparent. */
html { background: var(--page-background); }
.app-shell { background: var(--page-background); }

/* ==================== DISPLAY / URL SETTINGS PANEL ==================== */
.display-controls { display: grid; gap: 12px; }
.display-row { display: grid; grid-template-columns: 105px 1fr; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.display-inline { display: grid; grid-template-columns: 1fr 58px; align-items: center; gap: 10px; }
.display-inline output { color: var(--accent); text-align: right; font-variant-numeric: tabular-nums; }
.scale-presets { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.scale-presets button { min-height: 32px; padding: 5px; font-size: 12px; }
.custom-background-row input[type="color"] { width: 100%; min-height: 38px; padding: 3px; border-radius: 8px; border: 1px solid var(--panel-border); background: #111827; }
.url-preview-wrap { display: grid; gap: 6px; margin-top: 2px; padding: 10px; border-radius: 8px; border: 1px solid var(--panel-border); background: rgba(255,255,255,.03); }
.url-preview-wrap span { color: var(--muted); font-size: 12px; }
.url-preview-wrap code { overflow-wrap: anywhere; word-break: break-word; }

/* ==================== DISPLAY / URL SETTINGS PANEL ==================== */
.display-controls { display: grid; gap: 12px; }
.display-row { display: grid; grid-template-columns: 105px 1fr; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.display-inline { display: grid; grid-template-columns: 1fr 58px; align-items: center; gap: 10px; }
.display-inline output { color: var(--accent); text-align: right; font-variant-numeric: tabular-nums; }
.scale-presets { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.scale-presets button { min-height: 32px; padding: 5px; font-size: 12px; }
.custom-background-row input[type="color"] { width: 100%; min-height: 38px; padding: 3px; border-radius: 8px; border: 1px solid var(--panel-border); background: #111827; }
.url-preview-wrap { display: grid; gap: 6px; margin-top: 2px; padding: 10px; border-radius: 8px; border: 1px solid var(--panel-border); background: rgba(255,255,255,.03); }
.url-preview-wrap span { color: var(--muted); font-size: 12px; }
.url-preview-wrap code { overflow-wrap: anywhere; word-break: break-word; }
