* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  width: 100%; height: 100%;
  overflow: hidden;
  background: #111;
  touch-action: none;
  -webkit-touch-callout: none;
}

#game, #dpad, #action-btns, #dialogue-container {
  -webkit-user-select: none;
  user-select: none;
}

/* Loading */
#loading {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: #111; color: #888;
  font-size: 18px; z-index: 20;
  transition: opacity 0.3s;
}
#loading.hidden { opacity: 0; pointer-events: none; }

/* Canvas */
#game {
  display: block;
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

/* Zone Labels */
#zone-labels {
  position: fixed; top: 0; left: 0;
  width: 100%; height: 100%;
  pointer-events: none; z-index: 2;
}
.zone-label {
  position: absolute;
  background: rgba(0,0,0,0.7);
  color: #fff; font-size: 11px; font-family: sans-serif;
  padding: 2px 6px; border-radius: 3px;
  white-space: nowrap;
  transform: translate(-50%, -100%);
}
.remote-label {
  position: absolute;
  background: rgba(0,0,0,0.7);
  color: #fff; font-size: 11px; font-family: sans-serif;
  padding: 2px 6px; border-radius: 3px;
  white-space: nowrap;
  transform: translate(-50%, -100%);
}
.hide-names .remote-label,
.hide-names .npc-label,
.hide-names .local-label { display: none !important; }
.chat-bubble {
  position: absolute;
  background: rgba(255,255,255,0.92);
  color: #111; font-size: 11px; font-family: sans-serif;
  padding: 3px 6px; border-radius: 4px;
  max-width: 120px; word-break: break-word;
  transform: translate(-50%, -100%);
  pointer-events: none;
  transition: opacity 0.3s;
}

/* Overlay */
.overlay {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,0.6);
  z-index: 10;
  transition: opacity 0.25s;
}
.overlay.hidden { display: none; opacity: 0; }
.overlay-close {
  position: absolute; top: 12px; right: 16px;
  font-size: 28px; background: none; border: none;
  color: #fff; cursor: pointer;
}
#overlay-content {
  background: #1a1a2e; color: #eee;
  padding: 24px; border-radius: 8px;
  max-width: 480px; width: 90%;
  max-height: 80vh; overflow-y: auto;
}
#overlay-content h2 { font-size: 18px; }
#overlay-content p { font-size: 14px; line-height: 1.5; }
.overlay-btn {
  display: inline-block; padding: 8px 20px;
  background: #3a7bd5; color: #fff; text-decoration: none;
  border-radius: 4px; font-size: 14px;
}
.overlay-btn:hover { background: #2a5faa; }

/* D-Pad */
#dpad {
  position: fixed; bottom: 20px; left: 20px;
  display: flex; flex-direction: column; align-items: center;
  z-index: 8; opacity: 0.5;
  user-select: none;
}
.dpad-mid { display: flex; gap: 24px; }
.dpad-btn {
  width: 48px; height: 48px;
  font-size: 20px; background: rgba(255,255,255,0.15);
  border: 1px solid rgba(255,255,255,0.3); border-radius: 8px;
  color: #fff; cursor: pointer;
}
.dpad-btn:active { background: rgba(255,255,255,0.35); }

/* Action Buttons */
#action-btns {
  position: fixed; bottom: 32px; right: 20px;
  display: flex; gap: 12px;
  z-index: 8; opacity: 0.5;
  user-select: none;
}
.action-btn {
  width: 48px; height: 48px;
  font-size: 16px; font-weight: bold;
  background: rgba(255,255,255,0.15);
  border: 1px solid rgba(255,255,255,0.3); border-radius: 50%;
  color: #fff; cursor: pointer;
}
.action-btn:active { background: rgba(255,255,255,0.35); }

/* Mobile overlay full-width */
@media (max-width: 600px) {
  #overlay-content {
    max-width: 100%; width: 100%;
    border-radius: 0; max-height: 100vh;
  }
}

/* Start Button + Menu */
#btn-start {
  position: fixed; bottom: 12px; left: 50%;
  transform: translateX(-50%);
  z-index: 5; opacity: 0.5;
  padding: 6px 18px;
  font-size: 11px; font-weight: bold; letter-spacing: 1px;
  background: rgba(255,255,255,0.15);
  border: 1px solid rgba(255,255,255,0.3); border-radius: 10px;
  color: #fff; cursor: pointer;
  user-select: none; -webkit-touch-callout: none;
}
#btn-start:active { background: rgba(255,255,255,0.35); }
#start-menu {
  position: fixed; bottom: 48px; left: 50%;
  transform: translateX(-50%);
  z-index: 8; background: rgba(10,10,30,0.92);
  border: 1px solid rgba(255,255,255,0.25);
  border-radius: 6px; padding: 6px;
}
#start-menu.hidden { display: none; }
.menu-btn {
  display: block; width: 100%; padding: 8px 24px;
  background: none; border: none;
  color: #eee; font-size: 13px; font-family: monospace;
  cursor: pointer; text-align: left;
}
.menu-btn:hover { background: rgba(255,255,255,0.1); }

/* Theme Selector */
#theme-selector {
  position: fixed; top: 12px; right: 12px;
  z-index: 8; background: rgba(0,0,0,0.8);
  padding: 8px; border-radius: 6px;
}
#theme-selector.hidden { display: none; }
.theme-btn {
  display: block; width: 100%; padding: 6px 16px;
  margin-bottom: 4px; background: rgba(255,255,255,0.1);
  border: 1px solid rgba(255,255,255,0.2); border-radius: 4px;
  color: #eee; font-size: 13px; cursor: pointer;
}
.theme-btn:last-child { margin-bottom: 0; }
.theme-btn:hover { background: rgba(255,255,255,0.25); }

/* Dialogue Wrapper */
#dialogue-wrapper {
  position: fixed; bottom: 4%; left: 50%;
  transform: translateX(-50%);
  width: 90%;
  max-width: calc(180px * var(--dlg-scale, 1));
  z-index: 9;
}
#dialogue-wrapper.hidden { display: none; }

/* NPC Name Box */
#portrait-name-box {
  display: inline-block;
  background: rgba(10,10,30,0.92);
  border: calc(2px * var(--dlg-scale, 1)) solid rgba(255,255,255,0.25);
  border-radius: calc(4px * var(--dlg-scale, 1));
  padding: calc(3px * var(--dlg-scale, 1)) calc(8px * var(--dlg-scale, 1));
  margin-bottom: calc(2px * var(--dlg-scale, 1));
}
#portrait-name-box.hidden { display: none; }
#portrait-name {
  color: #fc6; font-family: monospace; font-weight: bold;
  font-size: calc(6px * var(--dlg-scale, 1));
}

/* Choice Box */
#choice-box {
  float: right;
  background: rgba(10,10,30,0.92);
  border: calc(2px * var(--dlg-scale, 1)) solid rgba(255,255,255,0.25);
  border-radius: calc(4px * var(--dlg-scale, 1));
  padding: calc(3px * var(--dlg-scale, 1)) calc(8px * var(--dlg-scale, 1));
  margin-bottom: calc(2px * var(--dlg-scale, 1));
  font-family: monospace;
  font-size: calc(6px * var(--dlg-scale, 1));
  color: #eee;
}
#choice-box.hidden { display: none; }
.choice-item {
  padding: calc(1px * var(--dlg-scale, 1)) 0;
  white-space: nowrap;
  cursor: pointer;
}
.choice-item.selected {
  color: #fc6;
}

/* Dialogue Box — scales with canvas via --dlg-scale custom property set by engine.js */
#dialogue-container {
  display: flex; align-items: flex-start;
  gap: calc(4px * var(--dlg-scale, 1));
  background: rgba(10,10,30,0.92);
  border: calc(2px * var(--dlg-scale, 1)) solid rgba(255,255,255,0.25);
  border-radius: calc(4px * var(--dlg-scale, 1));
  padding: calc(4px * var(--dlg-scale, 1));
  width: 100%;
  color: #eee;
  font-family: monospace;
  font-size: calc(7px * var(--dlg-scale, 1));
  image-rendering: pixelated;
  position: relative;
}
#dialogue-container.hidden { display: none; }
@media (max-width: 600px) {
  #dialogue-wrapper { width: 90%; }
}
#portrait-frame {
  flex-shrink: 0;
  border: calc(1px * var(--dlg-scale, 1)) solid rgba(255,255,255,0.25);
  border-radius: calc(2px * var(--dlg-scale, 1));
  padding: calc(1px * var(--dlg-scale, 1));
  background: rgba(0,0,0,0.3);
}
#portrait-frame canvas {
  display: block;
  width: calc(32px * var(--dlg-scale, 1));
  height: calc(32px * var(--dlg-scale, 1));
  image-rendering: pixelated;
}
#portrait-frame.hidden { display: none; }
#dialogue-body { flex: 1; min-width: 0; }
#dialogue-text {
  line-height: 1.4;
  height: 4.2em;
  overflow: hidden;
  white-space: pre-wrap; word-break: break-word;
}
#dialogue-advance {
  position: absolute;
  bottom: calc(4px * var(--dlg-scale, 1));
  right: calc(6px * var(--dlg-scale, 1));
  font-size: calc(5px * var(--dlg-scale, 1));
  color: rgba(255,255,255,0.5);
  animation: pulse-advance 1s ease-in-out infinite;
  opacity: 0;
}
#dialogue-advance.visible { opacity: 1; }
@keyframes pulse-advance {
  0%, 100% { opacity: 0.4; }
  50% { opacity: 1; }
}

/* Iframe Overlay */
#iframe-overlay {
  position: fixed; inset: 0;
  background: #000;
  z-index: 15;
  display: flex; flex-direction: column;
}
#iframe-overlay.hidden { display: none; }
.iframe-close {
  position: absolute; top: 8px; right: 12px;
  font-size: 32px; background: rgba(0,0,0,0.6);
  border: none; color: #fff; cursor: pointer;
  z-index: 16; padding: 4px 12px; border-radius: 4px;
}
.iframe-close:hover { background: rgba(255,255,255,0.2); }
#iframe-content {
  flex: 1; width: 100%; border: none;
}

/* Chat UI */
#chat-ui { position: fixed; bottom: 90px; right: 20px; z-index: 7; }
.chat-icon {
  width: 36px; height: 36px; border-radius: 50%;
  background: rgba(255,255,255,0.15); border: 1px solid rgba(255,255,255,0.3);
  color: #fff; font-size: 18px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  user-select: none; -webkit-touch-callout: none;
}
.chat-icon:active { background: rgba(255,255,255,0.35); }
.chat-panel {
  width: 220px; background: rgba(10,10,30,0.92);
  border: 1px solid rgba(255,255,255,0.25); border-radius: 6px;
  display: flex; flex-direction: column; overflow: hidden;
}
.chat-panel.hidden { display: none; }
.chat-panel-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 6px 8px; font-size: 12px; font-weight: bold;
  color: #eee; font-family: monospace;
  border-bottom: 1px solid rgba(255,255,255,0.15);
}
.chat-collapse {
  background: none; border: none; color: #aaa; font-size: 14px; cursor: pointer;
}
.chat-messages {
  height: 140px; overflow-y: auto; padding: 4px 8px;
  font-size: 11px; font-family: monospace; color: #ddd;
}
.chat-msg { margin-bottom: 3px; line-height: 1.3; word-break: break-word; }
.chat-msg-name { color: #6cf; font-weight: bold; }
.chat-system { color: #999; font-style: italic; }
.chat-input-row {
  display: flex; padding: 4px; gap: 4px;
  border-top: 1px solid rgba(255,255,255,0.15);
}
#chat-input {
  flex: 1; background: #1a1a2e; color: #eee; border: 1px solid #444;
  border-radius: 3px; padding: 4px 6px; font-size: 11px; font-family: monospace;
  outline: none;
}
.chat-send {
  background: #3a7bd5; color: #fff; border: none; border-radius: 3px;
  padding: 4px 8px; font-size: 11px; cursor: pointer;
}
.chat-send:active { background: #2a5faa; }
