:root {
  --ink: #243027;
  --muted: #627263;
  --panel: rgba(255, 255, 246, 0.9);
  --panel-solid: #fffff6;
  --line: rgba(45, 65, 48, 0.16);
  --accent: #2d8f7a;
  --accent-strong: #126c64;
  --danger: #cc4d3b;
  --food: #d79b35;
  --protect: #3477ba;
  --shadow: 0 16px 36px rgba(31, 48, 37, 0.18);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  letter-spacing: 0;
  background: #dfe9c7;
  touch-action: none;
}

button,
a {
  -webkit-tap-highlight-color: transparent;
}

button {
  font: inherit;
}

.game-shell {
  position: fixed;
  inset: 0;
  overflow: hidden;
  background: linear-gradient(180deg, #dfe9c7 0%, #cfdcbb 48%, #b9d3ce 100%);
}

#sceneCanvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  outline: none;
  touch-action: none;
}

.top-hud {
  position: absolute;
  top: calc(12px + var(--safe-top));
  left: 12px;
  right: 12px;
  z-index: 6;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  pointer-events: none;
}

.brand-link,
.stat-chip,
.brain-dock,
.nest-dock,
.journal-panel,
.inspect-panel,
.absence-panel,
.bottom-tools,
.toast {
  backdrop-filter: blur(18px) saturate(145%);
  -webkit-backdrop-filter: blur(18px) saturate(145%);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}

.brand-link {
  min-width: 184px;
  display: inline-flex;
  flex-direction: column;
  gap: 2px;
  padding: 10px 12px;
  color: var(--ink);
  text-decoration: none;
  background: var(--panel);
  border-radius: 8px;
  pointer-events: auto;
}

.brand-main {
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--accent-strong);
}

.brand-sub {
  font-size: 1rem;
  font-weight: 800;
}

.stat-strip {
  display: grid;
  grid-template-columns: repeat(4, minmax(88px, 1fr));
  gap: 8px;
  width: min(650px, 62vw);
  pointer-events: auto;
}

.stat-chip {
  min-height: 56px;
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 2px;
  background: var(--panel);
  border-radius: 8px;
}

.stat-label {
  color: var(--muted);
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
}

.stat-chip strong {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: clamp(0.85rem, 1.6vw, 1.1rem);
}

.brain-dock {
  position: absolute;
  left: 12px;
  top: calc(94px + var(--safe-top));
  z-index: 5;
  width: min(290px, calc(100vw - 24px));
  padding: 10px;
  background: rgba(255, 255, 246, 0.84);
  border-radius: 8px;
  pointer-events: none;
}

.dock-title {
  display: block;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 800;
  text-transform: uppercase;
}

.objective-list {
  display: grid;
  gap: 7px;
}

.objective-item {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 8px;
  padding: 7px 8px;
  background: rgba(47, 138, 117, 0.09);
  border: 1px solid rgba(47, 138, 117, 0.13);
  border-radius: 8px;
  font-size: 0.82rem;
  line-height: 1.2;
}

.objective-weight {
  color: var(--accent-strong);
  font-size: 0.72rem;
  font-weight: 800;
}

.nest-dock {
  position: absolute;
  left: 12px;
  top: calc(258px + var(--safe-top));
  z-index: 5;
  width: min(290px, calc(100vw - 24px));
  padding: 10px;
  background: rgba(255, 255, 246, 0.86);
  border-radius: 8px;
  pointer-events: auto;
}

.nest-dock-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.nest-dock-head .dock-title {
  margin: 0;
}

.nest-dock-head strong {
  color: var(--accent-strong);
  font-size: 0.82rem;
}

.floor-list {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
  margin-top: 8px;
}

.floor-list[hidden] {
  display: none;
}

.floor-btn,
.dig-level-btn {
  min-height: 38px;
  color: var(--ink);
  background: #f9f6ea;
  border: 1px solid rgba(40, 61, 49, 0.18);
  border-radius: 8px;
  cursor: pointer;
  font-weight: 800;
}

.floor-btn {
  padding: 0 6px;
  font-size: 0.72rem;
}

.floor-btn.active {
  color: #fff;
  background: #293a35;
  border-color: #293a35;
}

.floor-btn.planned {
  border-style: dashed;
}

.dig-level-btn {
  width: 100%;
  margin-top: 8px;
  color: #fff;
  background: var(--accent-strong);
}

.dig-level-btn:disabled {
  color: var(--muted);
  background: rgba(36, 48, 39, 0.08);
  cursor: default;
}

.dig-progress {
  min-height: 18px;
  margin-top: 7px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
  line-height: 1.25;
}

.journal-toggle {
  position: absolute;
  top: calc(94px + var(--safe-top));
  right: 12px;
  z-index: 7;
  min-height: 44px;
  padding: 0 16px;
  color: #fff;
  background: #293a35;
  border: 0;
  border-radius: 8px;
  box-shadow: var(--shadow);
  cursor: pointer;
}

.journal-panel,
.inspect-panel,
.absence-panel {
  position: absolute;
  z-index: 10;
  color: var(--ink);
  background: var(--panel-solid);
  border-radius: 8px;
  pointer-events: auto;
}

.journal-panel {
  top: calc(12px + var(--safe-top));
  right: 12px;
  bottom: calc(94px + var(--safe-bottom));
  width: min(390px, calc(100vw - 24px));
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.inspect-panel {
  left: 12px;
  top: calc(256px + var(--safe-top));
  bottom: calc(94px + var(--safe-bottom));
  width: min(360px, calc(100vw - 24px));
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px;
  border-bottom: 1px solid var(--line);
}

.panel-head h1,
.panel-head h2 {
  margin: 0;
  font-size: 1rem;
  line-height: 1.2;
}

.icon-btn {
  width: 36px;
  height: 36px;
  display: inline-grid;
  place-items: center;
  color: var(--ink);
  background: rgba(33, 48, 42, 0.08);
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
}

.current-thought {
  margin: 12px;
  padding: 11px;
  color: #fdfaf0;
  background: #2e4d45;
  border-radius: 8px;
  font-size: 0.9rem;
  line-height: 1.35;
}

.journal-list {
  margin: 0;
  padding: 0 12px 14px 32px;
  overflow: auto;
}

.journal-list li {
  margin: 0 0 10px;
  padding-left: 4px;
  color: var(--ink);
  font-size: 0.88rem;
  line-height: 1.35;
}

.inspect-body {
  display: grid;
  gap: 10px;
  padding: 12px;
  overflow: auto;
}

.inspect-section {
  display: grid;
  gap: 7px;
  padding: 10px;
  background: rgba(47, 138, 117, 0.08);
  border: 1px solid rgba(47, 138, 117, 0.14);
  border-radius: 8px;
}

.inspect-section h3 {
  margin: 0;
  color: var(--accent-strong);
  font-size: 0.76rem;
  text-transform: uppercase;
}

.inspect-row {
  display: grid;
  grid-template-columns: minmax(86px, 1fr) minmax(110px, 1.4fr) 38px;
  align-items: center;
  gap: 8px;
  font-size: 0.78rem;
}

.inspect-meter {
  height: 7px;
  overflow: hidden;
  background: rgba(36, 48, 39, 0.12);
  border-radius: 999px;
}

.inspect-meter span {
  display: block;
  height: 100%;
  background: var(--accent);
  border-radius: inherit;
}

.meter-0 { width: 0%; }
.meter-5 { width: 5%; }
.meter-10 { width: 10%; }
.meter-15 { width: 15%; }
.meter-20 { width: 20%; }
.meter-25 { width: 25%; }
.meter-30 { width: 30%; }
.meter-35 { width: 35%; }
.meter-40 { width: 40%; }
.meter-45 { width: 45%; }
.meter-50 { width: 50%; }
.meter-55 { width: 55%; }
.meter-60 { width: 60%; }
.meter-65 { width: 65%; }
.meter-70 { width: 70%; }
.meter-75 { width: 75%; }
.meter-80 { width: 80%; }
.meter-85 { width: 85%; }
.meter-90 { width: 90%; }
.meter-95 { width: 95%; }
.meter-100 { width: 100%; }

.inspect-meter.warn span {
  background: var(--food);
}

.inspect-meter.danger span {
  background: var(--danger);
}

.inspect-list {
  margin: 0;
  padding-left: 18px;
  display: grid;
  gap: 5px;
  font-size: 0.78rem;
  line-height: 1.25;
}

.inspect-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.inspect-tag {
  padding: 4px 7px;
  background: rgba(36, 48, 39, 0.08);
  border: 1px solid var(--line);
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 700;
}

.journal-time {
  display: block;
  color: var(--muted);
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
}

.absence-panel {
  left: 50%;
  top: calc(78px + var(--safe-top));
  width: min(420px, calc(100vw - 28px));
  transform: translateX(-50%);
  overflow: hidden;
}

.absence-panel ul {
  margin: 0;
  padding: 12px 18px 16px 30px;
}

.absence-panel li {
  margin-bottom: 8px;
  line-height: 1.35;
}

.toast {
  position: absolute;
  left: 50%;
  bottom: calc(108px + var(--safe-bottom));
  z-index: 11;
  min-width: min(360px, calc(100vw - 32px));
  max-width: min(520px, calc(100vw - 32px));
  padding: 11px 14px;
  color: #fff;
  text-align: center;
  background: rgba(38, 58, 52, 0.92);
  border-radius: 8px;
  opacity: 0;
  transform: translate(-50%, 10px);
  pointer-events: none;
  transition: opacity 160ms ease, transform 160ms ease;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

.bottom-tools {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: calc(12px + var(--safe-bottom));
  z-index: 7;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 10px;
  min-height: 64px;
  padding: 10px;
  background: rgba(255, 255, 246, 0.88);
  border-radius: 8px;
  pointer-events: auto;
}

.camera-tools {
  display: grid;
  grid-template-columns: 44px 46px 44px;
  align-items: center;
  gap: 6px;
}

.camera-speed {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
  text-align: center;
}

.tool-btn,
.action-btn,
.radial-action {
  min-height: 48px;
  color: var(--ink);
  background: #f9f6ea;
  border: 1px solid rgba(40, 61, 49, 0.18);
  border-radius: 8px;
  cursor: pointer;
  transition: transform 120ms ease, background 120ms ease, border-color 120ms ease;
}

.tool-btn:active,
.action-btn:active,
.radial-action:active {
  transform: translateY(1px) scale(0.99);
}

.tool-btn.small {
  min-width: 44px;
  font-size: 1.2rem;
  font-weight: 900;
}

.action-main {
  display: none;
  min-width: 104px;
  padding: 0 16px;
  color: #fff;
  background: var(--accent-strong);
  border-color: rgba(18, 108, 100, 0.36);
  font-weight: 800;
}

.action-bar {
  display: grid;
  grid-template-columns: repeat(3, minmax(104px, 1fr));
  gap: 8px;
  overflow: visible;
  scrollbar-width: none;
}

.action-bar::-webkit-scrollbar {
  display: none;
}

.action-btn {
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  justify-items: start;
  gap: 8px;
  min-width: 0;
  padding: 0 14px;
  font-size: 0.88rem;
  font-weight: 800;
}

.action-btn .btn-icon,
.radial-action .btn-icon {
  width: 26px;
  height: 26px;
  display: inline-grid;
  place-items: center;
  color: #fff;
  background: var(--accent);
  border-radius: 50%;
  font-size: 0.82rem;
  font-weight: 900;
}

.action-btn[data-action="attack"] .btn-icon,
.action-btn[data-action="danger"] .btn-icon,
.radial-action[data-action="attack"] .btn-icon,
.radial-action[data-action="danger"] .btn-icon {
  background: var(--danger);
}

.action-btn[data-action="give_food"] .btn-icon,
.action-btn[data-action="remove_food"] .btn-icon,
.action-btn[data-action="move_food"] .btn-icon,
.radial-action[data-action="give_food"] .btn-icon,
.radial-action[data-action="remove_food"] .btn-icon,
.radial-action[data-action="move_food"] .btn-icon {
  background: var(--food);
}

.action-btn[data-action="protect"] .btn-icon,
.radial-action[data-action="protect"] .btn-icon {
  background: var(--protect);
}

.action-btn.active,
.radial-action.active {
  color: #fff;
  background: #293a35;
  border-color: #293a35;
}

.radial-menu {
  position: absolute;
  left: 50%;
  bottom: calc(82px + var(--safe-bottom));
  z-index: 8;
  width: 220px;
  height: 220px;
  opacity: 0;
  pointer-events: none;
  transform: translateX(-50%) scale(0.9);
  transition: opacity 140ms ease, transform 140ms ease;
}

.radial-menu.open {
  opacity: 1;
  pointer-events: auto;
  transform: translateX(-50%) scale(1);
}

.radial-action {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 76px;
  min-height: 58px;
  display: grid;
  place-items: center;
  gap: 3px;
  padding: 7px;
  font-size: 0.68rem;
  font-weight: 800;
  line-height: 1.05;
  transform: translate(-50%, -50%) rotate(var(--angle)) translate(88px) rotate(var(--counter-angle));
  box-shadow: var(--shadow);
}

.radial-action .btn-icon {
  width: 24px;
  height: 24px;
}

@media (max-width: 820px) {
  .top-hud {
    align-items: stretch;
    flex-direction: column;
  }

  .brand-link {
    width: fit-content;
    max-width: calc(100vw - 24px);
  }

  .stat-strip {
    width: 100%;
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .stat-chip {
    min-height: 48px;
    padding: 7px;
  }

  .stat-label {
    font-size: 0.57rem;
  }

  .brain-dock {
    top: auto;
    left: 12px;
    right: 12px;
    bottom: calc(176px + var(--safe-bottom));
    width: auto;
    max-height: 118px;
    overflow: hidden;
  }

  .nest-dock {
    top: auto;
    left: 12px;
    right: 12px;
    bottom: calc(92px + var(--safe-bottom));
    width: auto;
    display: grid;
    grid-template-columns: minmax(90px, auto) minmax(0, 1fr) minmax(122px, auto);
    align-items: center;
    gap: 8px;
  }

  .floor-list {
    margin-top: 0;
  }

  .dig-level-btn {
    margin-top: 0;
  }

  .dig-progress {
    display: none;
  }

  .objective-list {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .objective-item {
    min-width: 0;
    display: block;
    font-size: 0.72rem;
  }

  .objective-weight {
    display: block;
    margin-top: 4px;
  }

  .journal-toggle {
    top: calc(154px + var(--safe-top));
  }

  .bottom-tools {
    grid-template-columns: auto minmax(0, 1fr);
    min-height: 72px;
  }

  .action-bar {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .action-btn {
    grid-template-columns: 1fr;
    justify-items: center;
    gap: 4px;
    padding: 5px 7px;
    font-size: 0.68rem;
    text-align: center;
  }

  .camera-tools {
    grid-template-columns: 38px 38px 38px;
  }

  .tool-btn.small {
    min-width: 38px;
  }
}

@media (max-width: 560px) {
  .brand-main {
    font-size: 0.64rem;
  }

  .brand-sub {
    font-size: 0.88rem;
  }

  .stat-strip {
    gap: 6px;
  }

  .stat-chip strong {
    font-size: 0.78rem;
  }

  .journal-panel {
    left: 8px;
    right: 8px;
    top: calc(8px + var(--safe-top));
    bottom: calc(86px + var(--safe-bottom));
    width: auto;
  }

  .inspect-panel {
    left: 8px;
    right: 8px;
    top: calc(206px + var(--safe-top));
    bottom: calc(86px + var(--safe-bottom));
    width: auto;
  }

  .bottom-tools {
    left: 8px;
    right: 8px;
    bottom: calc(8px + var(--safe-bottom));
    gap: 7px;
    padding: 8px;
  }

  .action-main {
    min-width: 86px;
    padding: 0 10px;
  }

  .brain-dock {
    left: 8px;
    right: 8px;
    bottom: calc(168px + var(--safe-bottom));
  }

  .nest-dock {
    left: 8px;
    right: 8px;
    bottom: calc(84px + var(--safe-bottom));
    grid-template-columns: minmax(0, 1fr);
  }

  .floor-list {
    grid-template-columns: repeat(5, minmax(0, 1fr));
  }

  .radial-menu {
    bottom: calc(78px + var(--safe-bottom));
  }
}

@media (orientation: landscape) and (max-height: 520px) {
  .top-hud {
    right: auto;
    width: min(560px, 58vw);
  }

  .brand-link {
    display: none;
  }

  .stat-strip {
    width: 100%;
  }

  .brain-dock {
    top: 76px;
    bottom: auto;
    width: min(280px, 36vw);
    max-height: calc(100vh - 160px);
  }

  .nest-dock {
    top: 76px;
    left: auto;
    right: 12px;
    bottom: auto;
    width: min(300px, 38vw);
    display: block;
  }

  .nest-dock .dig-progress {
    display: block;
  }

  .journal-toggle {
    top: calc(12px + var(--safe-top));
  }

  .journal-panel {
    bottom: calc(78px + var(--safe-bottom));
  }
}
