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

:root {
  --bg-primary: hsla(0, 0%, 0%, 1);
  --bg-secondary: hsla(0, 0%, 4%, 1);
  --text-primary: hsla(0, 0%, 100%, 1);
  --text-secondary: hsla(0, 0%, 53%, 1);
  --border-color: hsla(0, 0%, 20%, 1);
  --border-highlight: hsla(0, 0%, 40%, 1);
  --radius: 3px;

  /* Accent colors for events */
  --green: hsla(128, 49%, 49%, 1);
  --yellow: hsla(41, 72%, 48%, 1);
  --red: hsla(3, 92%, 63%, 1);
  --purple: hsla(262, 89%, 71%, 1);
  --accent: hsla(212, 100%, 67%, 1);
}

body {
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
  line-height: 1.6;
}

/* ============================================
   Layout
   ============================================ */

.app {
  display: flex;
  flex-direction: column;
  height: auto;
  min-height: 100vh;
  overflow: visible;
}

@media (min-width: 768px) {
  .app {
    height: 100vh;
    overflow: hidden;
  }
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

header h1 {
  font-size: 1.1rem;
  letter-spacing: 0.05em;
}

@media (min-width: 768px) {
  header {
    padding: 1rem 2rem;
  }

  header h1 {
    font-size: 1.5rem;
  }
}

.status {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.75rem;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

.dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--red);
  transition: background 0.3s;
}

.dot.connected {
  background: var(--green);
}

main {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
  padding: 1rem;
  flex: 1;
  min-height: 0;
}

@media (min-width: 768px) {
  main {
    grid-template-columns: 3fr 1fr;
    gap: 2rem;
    padding: 2rem;
  }
}

/* ============================================
   World
   ============================================ */

.world {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  min-height: 0;
  overflow: hidden;
}

/* ============================================
   HUD
   ============================================ */

.hud {
  flex-shrink: 0;
  overflow: auto;
}

@media (min-width: 768px) {
  .thought {
    overflow: initial;
  }
}

/* ============================================
   Thinking (AI options)
   ============================================ */

.hud > .thinking {
  display: flex;
  gap: 0.5rem;
  flex-shrink: 0;
  min-height: 4.5rem;
  align-items: flex-start;
  flex-wrap: nowrap;
}

.hud > .thinking.empty {
  align-items: center;
  font-size: 0.75rem;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  visibility: hidden;
}

.hud > .thinking.empty::before {
  content: "Thinking...";
  visibility: visible;
}

.thought {
  flex: 0 0 auto;
  width: 160px;
  max-width: 150px;
  font-size: 0.8rem;
  padding: 0.6rem;
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  transition: border-color 0.2s ease;
}

@media (min-width: 768px) {
  .thought {
    flex: 0 1 150px;
    width: auto;
  }
}

.thought.top-choice {
  border-color: var(--border-highlight);
  background: rgba(255, 255, 255, 0.02);
}

/* When exactly 2 thoughts (NPC choices), allow more width */
.thought:first-child:nth-last-child(2),
.thought:first-child:nth-last-child(2) ~ .thought {
  flex: 0 1 300px;
  max-width: 300px;
}

.thought-label {
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.thought-score {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.score-bar {
  flex: 1;
  height: 2px;
  background: var(--border-color);
  border-radius: 1px;
  overflow: hidden;
}

.score-fill {
  height: 100%;
  background: var(--text-secondary);
  border-radius: 1px;
  transition: width 0.2s ease-out;
}

.score-fill.negative {
  background: var(--red);
  opacity: 0.5;
}

.thought.top-choice .score-fill {
  background: var(--text-primary);
}

.score-value {
  font-size: 0.75rem;
  color: var(--text-secondary);
  min-width: 32px;
  text-align: right;
}

/* Event log styles for choice events */
.event-log li.choice_presented {
  border-left-color: var(--purple);
  color: var(--text-primary);
}

.event-log li.choice_made {
  border-left-color: var(--green);
  color: var(--text-primary);
}

.event-log li.choice_response {
  border-left-color: var(--purple);
  color: var(--text-primary);
}

/* ============================================
   Viewport (room display)
   ============================================ */

.viewport {
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 1rem;
  overflow: hidden;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  min-height: 640px;
}

@media (min-width: 768px) {
  .viewport {
    min-height: 0;
  }
}

.viewport-hud,
.viewport-footer {
  flex-shrink: 0;
  min-height: 1.3rem;
  font-size: 0.8rem;
  line-height: 1.3rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.hovered-entity {
  display: block;
  color: var(--text-primary);
}

.hovered-entity.empty {
  visibility: hidden;
  opacity: 0;
}

@media (pointer: fine) {
  .hovered-entity.empty {
    visibility: visible;
    opacity: 0.35;
  }
}

.viewport-footer {
  display: flex;
  align-items: baseline;
  gap: 1.5rem;
  color: var(--text-primary);
}

.viewport-footer label {
  color: var(--text-secondary);
  margin-right: 0.25rem;
}

.viewport-content {
  flex: 1;
  min-height: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  container-type: inline-size;
  container-name: viewport;
  position: relative;
}

@media (min-width: 768px) {
  .viewport-content {
    container-type: size;
  }
}

.viewport-label {
  display: none;
}

@media (min-width: 768px) {
  .viewport-label {
    display: inline;
  }
}

/* ============================================
   Tilemap
   ============================================ */

.tilemap {
  --cell-by-width: calc((100cqw - 16px) / var(--cols));
  --cell-by-height: calc((100cqh - 16px) / var(--rows));
  --cell-size: min(var(--cell-by-width), var(--cell-by-height));

  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell-size));
  grid-template-rows: repeat(var(--rows), var(--cell-size));
  gap: 0;
  line-height: 1;
}

@media (min-width: 768px) {
  .tilemap {
    --cell-by-width: calc((100cqw - 64px) / var(--cols));
    --cell-by-height: calc((100cqh - 64px) / var(--rows));
  }
}

.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--cell-size) * 0.85);
  position: relative;
  background: rgba(0, 0, 0, 0.01);
  cursor: pointer;
}

.cell.exit {
  background: rgba(255, 255, 255, 0.05);
}

.tile.floor {
  opacity: 0.35;
}

.tile.wall {
  opacity: 0.7;
}

.cell.exit .tile {
  opacity: 1;
}

.entity {
  position: absolute;
  z-index: 10;
  text-shadow: 0 0 20px currentColor;
}

.player {
  z-index: 100;
  animation: pulse 3s ease-in-out infinite;
  text-shadow: 0 0 8px hsla(0, 0%, 100%, 1);
  opacity: 1 !important;
}

.player.eating {
  animation: eating-pulse 1s ease-in-out infinite;
  text-shadow: 0 0 12px currentColor;
}

.player.sleeping {
  animation: sleeping-fade 2s ease-in-out infinite;
  text-shadow: 0 0 20px currentColor;
}

@keyframes pulse {
  0%,
  100% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.1);
    opacity: 0.85;
  }
}

@keyframes eating-pulse {
  0%,
  100% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.15);
    opacity: 0.9;
  }
}

@keyframes sleeping-fade {
  0%,
  100% {
    transform: scale(1);
    opacity: 0.6;
  }
  50% {
    transform: scale(0.95);
    opacity: 0.4;
  }
}

.entity.npc {
  animation: breathe 4s ease-in-out infinite;
}

@keyframes breathe {
  0%,
  100% {
    transform: scale(1);
    opacity: 0.9;
  }
  50% {
    transform: scale(1.05);
    opacity: 1;
  }
}

.entity.animal {
  animation: breathe-animal 2.5s ease-in-out infinite;
}

@keyframes breathe-animal {
  0%,
  100% {
    transform: scale(1);
    opacity: 0.85;
  }
  40% {
    transform: scale(1.08);
    opacity: 1;
  }
  60% {
    transform: scale(1.06);
    opacity: 0.95;
  }
}

.entity.coin {
  text-shadow: 0 0 6px currentColor;
  animation: coin-gleam 2s ease-in-out infinite;
}

@keyframes coin-gleam {
  0%,
  100% {
    transform: translateX(0);
    opacity: 0.9;
  }
  50% {
    transform: translateX(2px);
    opacity: 1;
  }
}

/* ============================================
   Sidebar
   ============================================ */

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  min-height: 0;
}

/* ============================================
   Panels (shared)
   ============================================ */

.panel {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 1rem;
}

.panel h3 {
  font-size: 0.8rem;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  flex-shrink: 0;
}

.time-played {
  font-variant-numeric: tabular-nums;
  color: var(--text-primary);
  margin-left: 0.5em;
}

/* ============================================
   Journey Panel
   ============================================ */

.journey {
  flex-shrink: 0;
}

/* ============================================
   Energy Bar
   ============================================ */

.energy-container {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.energy-bar {
  flex: 1;
  height: 2px;
  background: var(--border-color);
  border-radius: 1px;
  overflow: hidden;
}

.energy-fill {
  height: 100%;
  background: var(--text-secondary);
  border-radius: 1px;
  transition: width 0.2s ease-out;
}

.energy-fill.low {
  background: var(--red);
  opacity: 0.7;
}

.energy-container.eating .energy-fill {
  background: var(--green);
}

.energy-container.sleeping .energy-fill {
  background: var(--text-primary);
  opacity: 0.5;
}

.energy-text {
  font-size: 0.75rem;
  font-variant-numeric: tabular-nums;
  color: var(--text-secondary);
  min-width: 32px;
  text-align: right;
}

.counters {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.5rem;
}

@media (min-width: 768px) {
  .counters {
    grid-template-columns: repeat(3, 1fr);
  }
}

.counters div {
  text-align: center;
  padding: 0.75rem;
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  font-size: 0.8rem;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.padded-number {
  color: var(--text-primary);
}

.padded-number .pad {
  color: hsla(0, 0%, 50%, 1);
}

.collectibles {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.5rem;
}

.collectible {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.35rem;
  padding: 0.6rem;
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  cursor: pointer;
  transition: border-color 300ms ease;
}

.collectible span {
  letter-spacing: 0.02em;
}

.collectible:hover {
  border-color: var(--border-highlight);
}

.collectible .icon {
  font-size: 1.3rem;
  line-height: 1;
  opacity: 0.8;
}

.collectible .icon.coin {
  color: hsla(51, 100%, 50%, 1);
}
.collectible .icon.diamond {
  color: hsla(190, 100%, 50%, 1);
}
.collectible .icon.wildflower {
  color: hsla(330, 100%, 71%, 1);
}
.collectible .icon.fossil {
  color: hsla(30, 59%, 53%, 1);
}
.collectible .icon.key {
  color: hsla(51, 100%, 50%, 1);
}
.collectible .icon.apple {
  color: hsla(340, 80%, 50%, 1);
}

/* ============================================
   Activity Panel
   ============================================ */

.activity {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.event-log {
  list-style: none;
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  min-height: 0;
  max-height: 200px;
}

@media (min-width: 768px) {
  .event-log {
    max-height: none;
  }
}

.event-log li {
  font-size: 0.85rem;
  padding: 0.25rem 0.5rem;
  border-left: 1px solid var(--border-color);
  color: var(--text-secondary);
  animation: fadeIn 0.2s ease-out;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.event-time {
  color: var(--text-muted, hsla(0, 0%, 33%, 1));
  font-size: 0.65rem;
  font-family: monospace;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.event-message {
  line-height: 1.3;
}

.event-count {
  margin-left: 0.4rem;
  font-size: 0.75rem;
  opacity: 0.6;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

/* Event type colors */
.event-log li.item_found,
.event-log li.item_collected {
  border-left-color: var(--green);
  color: var(--text-primary);
}

.event-log li.npc_met {
  border-left-color: var(--purple);
  color: var(--text-primary);
}

.event-log li.npc_greeted {
  border-left-color: var(--purple);
}

.event-log li.animal_helped {
  border-left-color: var(--yellow);
  color: var(--text-primary);
}

.event-log li.animal_noticed {
  border-left-color: var(--yellow);
}

.event-log li.room_entered {
  border-left-color: var(--accent);
  color: var(--text-primary);
}

.event-log li.exit_found {
  border-left-color: var(--accent);
}

.event-log li.container_searching {
  font-style: italic;
}

.event-log li.container_empty,
.event-log li.container_searched {
  opacity: 0.7;
}

.event-log li.player_moved,
.event-log li.player_waited {
  opacity: 0.7;
}

.event-log li.thinking {
  border-left-color: var(--yellow);
  font-style: italic;
}

.event-log li.decided {
  border-left-color: var(--accent);
}

.event-log li.chest_opened {
  border-left-color: hsla(51, 100%, 50%, 1);
  color: var(--text-primary);
}

.event-log li.chest_locked {
  border-left-color: hsla(46, 68%, 47%, 1);
  font-style: italic;
}

/* ============================================
   Empty State
   ============================================ */

.empty-state {
  color: var(--text-secondary);
  font-size: 0.75rem;
  text-align: center;
  padding: 1.5rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

/* ============================================
   About Button
   ============================================ */

.about-button {
  margin-left: auto;
  cursor: pointer;
  color: var(--text-secondary);
  transition: color 0.2s ease;
}

.about-button:hover {
  color: var(--text-primary);
}

/* ============================================
   About Dialog
   ============================================ */

.about-dialog {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  z-index: 1000;
  animation: fadeIn 0.2s ease-out;
}

.about-dialog-content {
  position: relative;
  max-width: 480px;
  padding: 2rem;
}

.about-dialog-content h2 {
  font-size: min(4.5cqw, 20px);
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  margin-bottom: 1.5rem;
  color: var(--text-primary);
}

.about-dialog-content p {
  font-size: min(3.5cqw, 15px);
  line-height: 1.7;
  color: var(--text-secondary);
  margin-bottom: 1rem;
}

.about-dialog-content p:last-of-type {
  margin-bottom: 0;
}

.about-close {
  position: absolute;
  top: 0;
  right: 0;
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  color: var(--text-secondary);
  font-size: 0.9rem;
  cursor: pointer;
  transition:
    border-color 0.2s ease,
    color 0.2s ease;
}

.about-close:hover {
  border-color: var(--border-highlight);
  color: var(--text-primary);
}
