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

:root {
  --bg: #1a1c2c;
  --panel: #24273a;
  --panel2: #2e3250;
  --text: #e8e8f0;
  --muted: #9a9ab8;
  --accent: #7b6cf6;
  --health: #ef5b6b;
  --hunger: #f0a04a;
  --energy: #f6d55c;
  --happiness: #62c9a3;
  --bond: #6ba7f5;
}

html, body { height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: "Avenir Next", "Segoe UI", system-ui, sans-serif;
  overflow: hidden;
}

#app { display: flex; height: 100vh; }

#game-area { position: relative; flex: 1; min-width: 0; background: #000; }

#game-canvas { width: 100%; height: 100%; display: block; }

/* 3D overlay: projected name tags, speech bubbles, emotes */
#overlay { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.label3d { position: absolute; left: 0; top: 0; width: 0; height: 0; }
.label3d .hidden { display: none; }
.tag3d {
  position: absolute; transform: translate(-50%, 6px); white-space: nowrap;
  background: rgba(20,22,40,0.65); color: #e8e8f0; font-size: 10px; font-weight: 700;
  padding: 2px 7px; border-radius: 8px;
}
.bubble3d {
  position: absolute; transform: translate(-50%, calc(-100% - 34px)); max-width: 200px;
  background: rgba(255,255,255,0.95); color: #22243a; font-size: 12px; line-height: 1.3;
  padding: 6px 10px; border-radius: 10px; text-align: center;
}
.bubble3d::after {
  content: ''; position: absolute; left: 50%; bottom: -6px; transform: translateX(-50%);
  border: 6px solid transparent; border-top-color: rgba(255,255,255,0.95); border-bottom: none;
}
.emote3d {
  position: absolute; transform: translate(10px, -46px); font-size: 20px;
  background: rgba(255,255,255,0.92); border-radius: 50%; padding: 3px 5px;
}

#hud-top {
  position: absolute; top: 10px; left: 10px; right: 10px;
  display: flex; justify-content: space-between; pointer-events: none;
}
#hud-top > div {
  background: rgba(20, 22, 40, 0.75); padding: 6px 12px; border-radius: 20px;
  font-size: 13px; backdrop-filter: blur(4px);
}
#brain-badge.brain-offline { color: var(--muted); }
#brain-badge.brain-online { color: var(--happiness); }
#brain-badge.brain-thinking { color: var(--energy); }
#brain-badge.brain-error { color: var(--health); }

#hud-hint {
  position: absolute; bottom: 8px; left: 50%; transform: translateX(-50%);
  background: rgba(20, 22, 40, 0.6); color: var(--muted);
  font-size: 11px; padding: 4px 12px; border-radius: 12px; pointer-events: none;
  white-space: nowrap;
}

#side-panel {
  width: 340px; background: var(--panel); display: flex; flex-direction: column;
  padding: 12px; gap: 10px; border-left: 2px solid #14162a; overflow: hidden;
}

#agent-chips { display: flex; gap: 6px; flex-wrap: wrap; }
.agent-chip {
  display: flex; align-items: center; gap: 5px; padding: 4px 10px; border-radius: 14px;
  background: var(--panel2); border: 1px solid transparent; cursor: pointer; font-size: 12px;
  color: var(--text);
}
.agent-chip.selected { border-color: var(--accent); background: #383d66; }
.agent-chip .dot { width: 9px; height: 9px; border-radius: 50%; }
.agent-chip .thinking-dot { color: var(--energy); font-weight: 700; }
/* Gameplay F: critical badge */
.agent-chip .chip-badge { font-size: 11px; line-height: 1; }
.agent-chip .chip-badge.critical { animation: criticalPulse 0.9s ease-in-out infinite; }

.panel-header { display: flex; align-items: center; gap: 8px; }
#monster-name { font-size: 20px; font-weight: 700; }
#agent-meta { font-size: 11px; color: var(--muted); }
#mood-emoji { font-size: 20px; }
#settings-btn, #journal-btn, #mute-btn {
  background: var(--panel2); border: none; color: var(--text);
  font-size: 16px; padding: 6px 10px; border-radius: 8px; cursor: pointer;
}
#journal-btn { margin-left: auto; }
#settings-btn:hover, #journal-btn:hover, #mute-btn:hover { background: var(--accent); }

#vitals { display: flex; flex-direction: column; gap: 5px; }
.vital { display: flex; align-items: center; gap: 8px; font-size: 12px; }
.vital label { width: 78px; color: var(--muted); }
.vital span { width: 28px; text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
.bar { flex: 1; height: 10px; background: #171930; border-radius: 5px; overflow: hidden; }
.fill { height: 100%; border-radius: 5px; transition: width 0.4s; }
.fill.health { background: var(--health); }
.fill.hunger { background: var(--hunger); }
.fill.energy { background: var(--energy); }
.fill.happiness { background: var(--happiness); }
.fill.bond { background: var(--bond); }

/* Gameplay D: critical pulse animation when vital < 20 */
@keyframes criticalPulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}
.fill.critical {
  animation: criticalPulse 0.9s ease-in-out infinite;
  filter: brightness(1.25) saturate(1.5);
}

#thought-box, #quest-box {
  background: var(--panel2); border-radius: 10px; padding: 8px 10px;
}
#thought-box { min-height: 52px; }
#quest-text { font-size: 12px; color: #f0e2b8; line-height: 1.4; }
.box-title { font-size: 11px; color: var(--muted); margin-bottom: 4px; }
#thought-text { font-size: 12px; font-style: italic; color: #c9c5f2; line-height: 1.4; }

#chat-log {
  flex: 1; min-height: 80px; overflow-y: auto; background: var(--panel2);
  border-radius: 10px; padding: 8px; display: flex; flex-direction: column; gap: 6px;
}
.msg { max-width: 88%; padding: 6px 10px; border-radius: 12px; font-size: 13px; line-height: 1.35; }
.msg.player { align-self: flex-end; background: var(--accent); border-bottom-right-radius: 3px; }
.msg.monster { align-self: flex-start; background: #3a3f63; border-bottom-left-radius: 3px; }
.msg.system { align-self: center; background: none; color: var(--muted); font-size: 11px; padding: 2px; }

/* Gameplay E: typing bubble in chat */
.typing-bubble {
  opacity: 0.85;
  font-style: italic;
}
.typing-name { font-weight: 600; color: #a8b0e8; }
.typing-dots span {
  display: inline-block;
  animation: typingBounce 1.2s ease-in-out infinite;
}
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typingBounce {
  0%, 100% { transform: translateY(0); opacity: 0.4; }
  50% { transform: translateY(-3px); opacity: 1; }
}

#chat-input-row { display: flex; gap: 6px; }
#chat-input {
  flex: 1; background: var(--panel2); border: 1px solid #3a3f63; color: var(--text);
  padding: 8px 10px; border-radius: 8px; font-size: 13px; outline: none;
}
#chat-input:focus { border-color: var(--accent); }
#chat-send, #action-buttons button {
  background: var(--panel2); border: none; color: var(--text); padding: 8px 10px;
  border-radius: 8px; cursor: pointer; font-size: 13px;
}
#chat-send:hover, #action-buttons button:hover { background: var(--accent); }

#action-buttons { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
#action-buttons button { padding: 7px 4px; font-size: 12px; }

#event-log { max-height: 120px; overflow-y: auto; background: var(--panel2); border-radius: 10px; padding: 8px; }
#event-list { display: flex; flex-direction: column-reverse; gap: 3px; }
#event-list div { font-size: 11px; color: var(--muted); line-height: 1.3; }

#speaker-chips { display: flex; align-items: center; gap: 6px; }
#speaker-chips.hidden { display: none; }
.speaker-chip {
  background: var(--panel2); border: 1px solid transparent; color: var(--text);
  padding: 3px 12px; border-radius: 12px; cursor: pointer; font-size: 12px;
}
.speaker-chip.selected { border-color: var(--accent); background: #383d66; }

#region-badge { color: #b8e0c0; }

/* Settings + journal + help modals */
#settings-modal, #journal-modal, #help-modal {
  position: fixed; inset: 0; background: rgba(0,0,0,0.6); display: flex;
  align-items: center; justify-content: center; z-index: 50;
}
#settings-modal.hidden, #journal-modal.hidden, #help-modal.hidden { display: none; }

#help-card {
  background: var(--panel); border-radius: 14px; padding: 20px; width: 560px; max-width: 94vw;
  max-height: 88vh; overflow-y: auto; display: flex; flex-direction: column; gap: 10px;
}
#help-card h2 { font-size: 17px; }
#help-content { display: flex; flex-direction: column; gap: 10px; }
.help-section { background: var(--panel2); border-radius: 10px; padding: 12px; }
.help-section h4 { font-size: 12px; color: #f0e2b8; text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 6px; }
.help-section p, .help-section li { font-size: 12.5px; line-height: 1.55; color: var(--text); }
.help-section ul { padding-left: 18px; }
.help-section table { width: 100%; border-collapse: collapse; font-size: 12px; margin-top: 6px; }
.help-section th, .help-section td { text-align: left; padding: 4px 6px; border-bottom: 1px solid #3a3f63; }
.help-section th { color: var(--muted); font-weight: 600; }
.evo-species { font-weight: 700; margin-top: 8px; color: #c9c5f2; }

#journal-card {
  background: var(--panel); border-radius: 14px; padding: 20px; width: 460px; max-width: 92vw;
  max-height: 84vh; overflow-y: auto; display: flex; flex-direction: column; gap: 10px;
}
#journal-card h2 { font-size: 17px; }
#journal-content { display: flex; flex-direction: column; gap: 10px; }
.journal-section { background: var(--panel2); border-radius: 10px; padding: 10px; }
.journal-section h4 { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 6px; }
.journal-section div { font-size: 12px; line-height: 1.5; }
.journal-section .belief { color: #c9c5f2; font-style: italic; }
.skill-row { display: flex; align-items: center; gap: 8px; margin-bottom: 3px; }
.skill-row label { width: 80px; color: var(--muted); font-size: 12px; }
.skill-pips { letter-spacing: 2px; }

.row2 { display: flex; gap: 10px; }
.row2 > label { flex: 1; }
.check-row { flex-direction: row !important; align-items: center; gap: 8px !important; }
.check-row input { width: auto !important; }
.world-buttons { display: flex; gap: 6px; align-items: flex-end; }
.world-buttons button {
  background: var(--panel2); border: none; color: var(--text); padding: 8px 10px;
  border-radius: 8px; cursor: pointer; font-size: 12px;
}
.world-buttons button.danger { background: #5c2438; }
#settings-card select {
  background: var(--panel2); border: 1px solid #3a3f63; color: var(--text);
  padding: 8px; border-radius: 8px; font-size: 13px; width: 100%;
}
#settings-card {
  background: var(--panel); border-radius: 14px; padding: 20px; width: 520px; max-width: 92vw;
  max-height: 88vh; overflow-y: auto;
  display: flex; flex-direction: column; gap: 10px;
}
#settings-card h2 { font-size: 17px; }
#settings-card h3 { font-size: 13px; color: var(--muted); margin-top: 6px; text-transform: uppercase; letter-spacing: 0.5px; }

.creature-row {
  background: var(--panel2); border-radius: 10px; padding: 10px; display: flex;
  flex-direction: column; gap: 8px;
}
.creature-row .row { display: flex; gap: 8px; align-items: flex-end; }
.creature-row .row label { flex: 1; }
.creature-row .row label.small { flex: 0 0 90px; }
.creature-row select, .creature-row input[type="color"] {
  background: var(--panel); border: 1px solid #3a3f63; color: var(--text);
  padding: 7px 8px; border-radius: 8px; font-size: 13px; width: 100%;
}
.creature-row input[type="color"] { height: 34px; padding: 2px; }
.creature-row .remove-btn {
  background: #5c2438; border: none; color: var(--text); border-radius: 8px;
  padding: 7px 10px; cursor: pointer; align-self: flex-end;
}
.creature-row .custom-api { display: flex; flex-direction: column; gap: 8px; }
.creature-row .custom-api.hidden { display: none; }
.settings-note { font-size: 12px; color: var(--muted); }
#settings-card label { font-size: 12px; color: var(--muted); display: flex; flex-direction: column; gap: 4px; }
#settings-card input {
  background: var(--panel2); border: 1px solid #3a3f63; color: var(--text);
  padding: 8px 10px; border-radius: 8px; font-size: 13px; outline: none;
}
#settings-card input:focus { border-color: var(--accent); }
#settings-test-result { font-size: 12px; min-height: 16px; }
#settings-test-result.ok { color: var(--happiness); }
#settings-test-result.err { color: var(--health); }
.settings-buttons { display: flex; gap: 8px; }
.settings-buttons button {
  flex: 1; background: var(--panel2); border: none; color: var(--text);
  padding: 9px 0; border-radius: 8px; cursor: pointer; font-size: 13px;
}
.settings-buttons button.primary { background: var(--accent); }
.settings-buttons button.danger { background: #5c2438; }
.settings-buttons button:hover { filter: brightness(1.2); }

@media (max-width: 800px) {
  #app { flex-direction: column; }
  #side-panel { width: 100%; height: 45vh; border-left: none; border-top: 2px solid #14162a; }
}

/* Gameplay J: weather overlay effects */
.weather-overlay {
  position: absolute; inset: 0; pointer-events: none; z-index: 5;
  transition: opacity 2s ease;
}

/* Rain: animated diagonal streaks */
.weather-rain::before {
  content: '';
  position: absolute; inset: -20px;
  background-image: repeating-linear-gradient(
    105deg,
    transparent 0px,
    transparent 6px,
    rgba(150,190,255,0.18) 6px,
    rgba(150,190,255,0.18) 7px
  );
  animation: rainFall 0.5s linear infinite;
}
@keyframes rainFall {
  from { transform: translateY(-30px); }
  to   { transform: translateY(0px); }
}

/* Fog: animated radial gradient overlay */
.weather-fog {
  background: radial-gradient(ellipse at 50% 60%, transparent 20%, rgba(180,190,210,0.35) 100%);
  animation: fogDrift 8s ease-in-out infinite alternate;
}
@keyframes fogDrift {
  from { opacity: 0.7; }
  to   { opacity: 1; }
}

/* Weather badge in HUD */
.weather-badge {
  font-size: 18px; padding: 4px 6px; pointer-events: none;
}

/* Gameplay C: welcome banner */
.welcome-banner {
  position: fixed; inset: 0; background: rgba(0,0,0,0.72);
  display: flex; align-items: center; justify-content: center; z-index: 100;
}
.welcome-banner.hidden { display: none; }
.welcome-inner {
  background: var(--panel); border-radius: 18px; padding: 28px 32px;
  width: 460px; max-width: 94vw; display: flex; flex-direction: column;
  gap: 16px; box-shadow: 0 8px 40px rgba(0,0,0,0.6);
  border: 1px solid rgba(123,108,246,0.4);
}
.welcome-title { font-size: 22px; font-weight: 800; color: var(--accent); }
.welcome-controls {
  display: grid; grid-template-columns: 1fr 1fr; gap: 8px;
}
.welcome-controls span {
  background: var(--panel2); border-radius: 8px; padding: 7px 10px;
  font-size: 12px; color: var(--text); line-height: 1.4;
}
.welcome-ok {
  background: var(--accent); border: none; color: #fff; border-radius: 10px;
  padding: 10px 0; font-size: 15px; font-weight: 700; cursor: pointer;
  transition: filter 0.15s;
}
.welcome-ok:hover { filter: brightness(1.15); }
