:root {
  --cream: #f7f1e8;
  --paper: #fbf7f0;
  --ink: #3d3830;
  --muted: #7d786e;
  --sage: #8a9a7a;
  --sage-deep: #5f6f56;
  --terracotta: #e8b4a0;
  --terracotta-deep: #c88974;
  --fog: rgba(55, 50, 42, 0.38);
  --shadow: 0 18px 50px rgba(70, 58, 42, 0.16);
  --radius: 22px;
  --dock-h: 74px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --font: "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", "Source Han Sans SC",
    system-ui, sans-serif;
  --serif: "Noto Serif SC", "Songti SC", "Hiragino Mincho ProN", "Source Han Serif SC",
    "Georgia", serif;
  --cute: "ZCOOL KuaiLe", "YouYuan", "Yuanti SC", "STYuanti", "Yuppy SC",
    "Wawati SC", "Hiragino Maru Gothic ProN", "PingFang SC", sans-serif;
}

@font-face {
  font-family: "ZCOOL KuaiLe";
  src: url("fonts/kuaile.woff2") format("woff2");
  font-style: normal;
  font-weight: 400;
  font-display: swap;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

html, body {
  height: 100%;
  overflow: hidden;
  font-family: var(--font);
  color: var(--ink);
  background: #d9cfc0;
  -webkit-font-smoothing: antialiased;
}

button, input, textarea { font-family: inherit; }

#app {
  position: relative;
  width: 100%;
  height: 100%;
  min-height: 100dvh;
  background:
    radial-gradient(1200px 700px at 50% 0%, #efe6d6 0%, #d9e0d0 55%, #c9d0c0 100%);
}

#canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  z-index: 0;
}

#app::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 5;
  pointer-events: none;
  background: radial-gradient(ellipse 75% 70% at 50% 42%, transparent 55%, rgba(70, 58, 42, 0.16) 100%);
}

/* ---------- WebGL fallback garden ---------- */
#fallback {
  display: none;
  position: absolute;
  inset: 0;
  z-index: 0;
  overflow: hidden;
  background: linear-gradient(#efe6d6 0%, #dce5d4 48%, #c5d0b8 100%);
}
#fallback.visible { display: block; }
#fallback svg { width: 100%; height: 100%; display: block; }
.fallback-note {
  position: absolute;
  left: 50%;
  top: auto;
  bottom: calc(var(--dock-h) + 28px + var(--safe-bottom));
  transform: translateX(-50%);
  width: min(420px, calc(100% - 40px));
  text-align: center;
  font-size: 13px;
  line-height: 1.6;
  color: var(--sage-deep);
  background: rgba(251, 247, 240, 0.72);
  border-radius: 14px;
  padding: 10px 14px;
  backdrop-filter: blur(8px);
}

/* ---------- Top bar ---------- */
.topbar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top, 0px)) 16px 10px;
  pointer-events: none;
  background: linear-gradient(to bottom, rgba(247, 241, 232, 0.55), rgba(247, 241, 232, 0));
}
.topbar > * { pointer-events: auto; }

.wordmark {
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.wordmark strong {
  font-family: var(--serif);
  font-weight: 600;
  font-size: 18px;
  letter-spacing: 0.02em;
  color: var(--ink);
}
.wordmark span {
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 0.08em;
}

.top-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.langs {
  display: flex;
  background: rgba(251, 247, 240, 0.86);
  border-radius: 999px;
  padding: 3px;
  box-shadow: 0 4px 16px rgba(70, 58, 42, 0.08);
  backdrop-filter: blur(10px);
}
.langs button {
  border: none;
  background: transparent;
  color: var(--muted);
  padding: 5px 10px;
  border-radius: 999px;
  cursor: pointer;
  font-size: 12px;
}
.langs button.active {
  background: var(--terracotta);
  color: #fff;
}

.auth-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(251, 247, 240, 0.86);
  border-radius: 999px;
  padding: 4px 4px 4px 12px;
  box-shadow: 0 4px 16px rgba(70, 58, 42, 0.08);
  backdrop-filter: blur(10px);
  min-height: 34px;
}
.auth-chip .who {
  font-size: 12px;
  color: var(--ink);
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.auth-chip button,
.btn-login {
  border: none;
  background: var(--terracotta);
  color: #fff;
  border-radius: 999px;
  padding: 6px 12px;
  cursor: pointer;
  font-size: 12px;
}
.btn-login {
  box-shadow: 0 4px 16px rgba(70, 58, 42, 0.08);
  padding: 8px 14px;
}
.auth-chip button.ghost,
button.ghost {
  background: transparent;
  color: var(--muted);
}
.auth-chip button:hover,
.btn-login:hover { background: var(--terracotta-deep); }
.auth-chip button.ghost:hover { background: rgba(0,0,0,0.05); color: var(--ink); }

/* ---------- Scene caption ---------- */
.caption {
  position: absolute;
  top: calc(68px + env(safe-area-inset-top, 0px));
  left: 50%;
  transform: translateX(-50%);
  z-index: 15;
  text-align: center;
  width: min(520px, calc(100% - 32px));
  pointer-events: none;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.4);
}
.caption h1 {
  font-family: var(--serif);
  font-weight: 500;
  font-size: clamp(20px, 3.4vw, 30px);
  letter-spacing: 0.06em;
  color: var(--ink);
  line-height: 1.35;
}
.caption p {
  margin-top: 6px;
  font-size: 13px;
  color: var(--sage-deep);
  line-height: 1.6;
}

/* ---------- WASD hint ---------- */
.hint-wasd {
  position: absolute;
  right: 18px;
  bottom: calc(var(--dock-h) + 28px + var(--safe-bottom));
  z-index: 15;
  font-size: 12px;
  color: var(--sage-deep);
  background: rgba(251, 247, 240, 0.72);
  border-radius: 999px;
  padding: 6px 12px;
  pointer-events: none;
  backdrop-filter: blur(8px);
}
@media (max-width: 768px) {
  .hint-wasd { display: none; }
}

/* ---------- Proximity card ---------- */
.proximity {
  position: absolute;
  left: 50%;
  bottom: calc(var(--dock-h) + 24px + var(--safe-bottom));
  transform: translateX(-50%) translateY(8px);
  z-index: 16;
  width: min(300px, calc(100% - 28px));
  padding: 10px 10px 9px;
  border-radius: 22px;
  background:
    linear-gradient(180deg, rgba(255, 252, 248, 0.97) 0%, rgba(251, 243, 232, 0.96) 100%);
  border: 2px solid rgba(232, 180, 160, 0.6);
  box-shadow: 0 10px 26px rgba(70, 58, 42, 0.14), inset 0 0 0 3px rgba(255, 255, 255, 0.7);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.22s ease, transform 0.22s ease;
  backdrop-filter: blur(14px);
  font-family: var(--cute);
}
.proximity.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
  pointer-events: auto;
}
.prox-close {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 26px;
  height: 26px;
  border: none;
  border-radius: 50%;
  background: #fff6ee;
  color: var(--terracotta-deep);
  font-family: var(--cute);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  z-index: 2;
  box-shadow: 0 2px 6px rgba(70, 58, 42, 0.12);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}
.prox-close:hover,
.prox-close:focus {
  background: #fde4d8;
  outline: none;
}
.prox-card {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding-right: 20px;
}
.prox-photo-wrap {
  flex: 0 0 68px;
  width: 68px;
  height: 68px;
  margin: 0;
  border-radius: 16px;
  overflow: hidden;
  background: #eee6da;
  border: 3px solid #fff;
  box-shadow: 0 2px 8px rgba(70, 58, 42, 0.12);
}
.prox-photo-wrap[hidden] { display: none !important; }
.prox-photo-wrap img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  cursor: zoom-in;
}
.prox-copy { min-width: 0; flex: 1; }
.photo-lightbox {
  position: absolute;
  inset: 0;
  z-index: 40;
  background: rgba(30, 26, 22, 0.84);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
  cursor: zoom-out;
}
.photo-lightbox:not([hidden]) { display: flex; }
.photo-lightbox img {
  max-width: min(100%, 720px);
  max-height: 100%;
  object-fit: contain;
  border-radius: 14px;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
}
.proximity .row {
  display: flex;
  align-items: baseline;
  gap: 6px;
  flex-wrap: wrap;
}
.proximity h2 {
  font-family: var(--cute);
  font-size: 16px;
  font-weight: 400;
  letter-spacing: 0.02em;
  line-height: 1.25;
}
.badge {
  display: inline-block;
  font-size: 10px;
  letter-spacing: 0.06em;
  background: var(--sage);
  color: #fff;
  border-radius: 999px;
  padding: 2px 8px;
  vertical-align: middle;
}
.proximity .dates {
  font-size: 10px;
  color: var(--muted);
  margin-top: 1px;
}
.proximity .note,
.proximity .letter {
  margin-top: 3px;
  font-size: 12px;
  line-height: 1.4;
  color: var(--sage-deep);
  font-family: var(--cute);
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  white-space: normal;
}
.proximity .sample-hint { display: none !important; }
.proximity .prox-meta {
  margin-top: 3px;
  font-size: 10px;
  color: var(--sage-deep);
}
.proximity .prox-actions {
  display: flex;
  gap: 6px;
  margin-top: 8px;
}
.proximity .prox-actions .btn {
  flex: 1;
  margin: 0;
  padding: 6px 8px;
  font-size: 12px;
  border-radius: 999px;
  font-family: var(--cute);
}
.proximity .prox-actions .btn:disabled {
  opacity: 0.7;
  cursor: default;
}

/* ---------- Joystick ---------- */
.joystick {
  position: absolute;
  left: 16px;
  bottom: calc(var(--dock-h) + 20px + var(--safe-bottom));
  width: 96px;
  height: 96px;
  background: rgba(251, 247, 240, 0.28);
  border: 1px solid rgba(255, 255, 255, 0.45);
  border-radius: 50%;
  z-index: 18;
  display: none;
  touch-action: none;
}
.joystick .knob {
  width: 42px;
  height: 42px;
  background: rgba(232, 180, 160, 0.85);
  border-radius: 50%;
  position: absolute;
  left: 27px;
  top: 27px;
  box-shadow: 0 4px 10px rgba(70, 58, 42, 0.2);
  pointer-events: none;
}
@media (max-width: 768px) {
  .joystick { display: block; }
  .proximity {
    left: 124px;
    right: auto;
    width: min(280px, calc(100% - 140px));
    transform: translateY(8px);
    z-index: 20;
  }
  .proximity.visible {
    transform: translateY(0);
  }
}

/* ---------- Bottom dock ---------- */
.dock {
  position: absolute;
  left: 50%;
  bottom: calc(14px + var(--safe-bottom));
  transform: translateX(-50%);
  z-index: 20;
  display: flex;
  gap: 6px;
  background: rgba(251, 247, 240, 0.92);
  padding: 6px;
  border-radius: 24px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(16px);
  width: min(420px, calc(100% - 20px));
}
.dock button {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--ink);
  border-radius: 18px;
  padding: 8px 6px 7px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  font-size: 11px;
  line-height: 1.2;
}
.dock button svg {
  width: 22px;
  height: 22px;
  stroke: var(--sage-deep);
  fill: none;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.dock button.active {
  background: var(--terracotta);
  color: #fff;
}
.dock button.active svg { stroke: #fff; }

/* ---------- Overlays / panels ---------- */
.overlay {
  position: absolute;
  inset: 0;
  z-index: 40;
  display: none;
  align-items: flex-end;
  justify-content: center;
}
.overlay.open { display: flex; }
.overlay .backdrop {
  position: absolute;
  inset: 0;
  background: var(--fog);
  backdrop-filter: blur(6px);
}
.panel {
  position: relative;
  z-index: 1;
  background: var(--paper);
  width: min(520px, calc(100% - 20px));
  max-height: min(86dvh, calc(100% - 24px));
  overflow: auto;
  border-radius: var(--radius) var(--radius) 18px 18px;
  margin-bottom: calc(12px + var(--safe-bottom));
  padding: 20px 20px 24px;
  box-shadow: var(--shadow);
  animation: rise 0.28s ease;
}
@keyframes rise {
  from { transform: translateY(18px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}
.panel header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}
.panel h2 {
  font-family: var(--serif);
  font-weight: 600;
  font-size: 20px;
}
.panel .lede {
  margin-top: 4px;
  font-size: 13px;
  color: var(--muted);
  line-height: 1.55;
}
.icon-btn {
  border: none;
  background: #efe8dc;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  cursor: pointer;
  color: var(--ink);
  font-size: 18px;
  line-height: 1;
  flex-shrink: 0;
}

label {
  display: block;
  font-size: 13px;
  margin: 12px 0 5px;
  color: var(--sage-deep);
}
.field-row { display: flex; gap: 10px; }
.field-row > div { flex: 1; min-width: 0; }
input, textarea {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid #e6ddd0;
  border-radius: 12px;
  font-size: 15px;
  background: #fff;
  color: var(--ink);
}
input:focus, textarea:focus {
  outline: 2px solid rgba(232, 180, 160, 0.7);
  border-color: var(--terracotta);
}
textarea { min-height: 88px; resize: vertical; }
.hint {
  font-size: 12px;
  color: var(--muted);
  margin-top: 6px;
}

.btn {
  background: var(--terracotta);
  color: #fff;
  border: none;
  padding: 13px;
  border-radius: 999px;
  width: 100%;
  margin-top: 16px;
  cursor: pointer;
  font-size: 15px;
}
.btn:hover { background: var(--terracotta-deep); }
.btn:disabled { opacity: 0.65; cursor: wait; }
.btn.secondary {
  background: transparent;
  color: var(--muted);
  margin-top: 8px;
}
.btn.secondary:hover { background: rgba(0,0,0,0.04); color: var(--ink); }

.auth-error {
  color: #9a4f3c;
  font-size: 13px;
  min-height: 1.2em;
  margin-top: 8px;
}
.switch-auth {
  display: block;
  width: 100%;
  margin-top: 10px;
  border: none;
  background: none;
  color: var(--sage-deep);
  cursor: pointer;
  font-size: 13px;
  text-decoration: underline;
  text-underline-offset: 3px;
}

/* ---------- Memorial list ---------- */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 12px;
}
.empty {
  text-align: center;
  color: var(--muted);
  padding: 36px 12px;
  line-height: 1.7;
  font-size: 14px;
}
.mem-card {
  background: #fff;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 2px 10px rgba(70, 58, 42, 0.06);
  cursor: pointer;
  display: flex;
  flex-direction: column;
}
.mem-card .thumb {
  width: 100%;
  height: 120px;
  object-fit: cover;
  background: #efe8dc;
  display: block;
}
.mem-card .thumb.ph {
  background:
    linear-gradient(160deg, #efe8dc, #dce3d4);
}
.mem-card .card-info { padding: 10px 12px 12px; }
.mem-card strong { font-size: 14px; }
.mem-card .dates { font-size: 12px; color: var(--muted); margin: 2px 0 4px; }
.mem-card .note { font-size: 13px; line-height: 1.45; color: var(--ink); }
.mem-card .card-actions {
  display: flex;
  justify-content: flex-end;
  padding: 0 8px 10px;
}
.mem-card .card-actions button {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 8px;
}
.mem-card .card-actions button:hover { background: #f3ece3; color: #9a4f3c; }

/* ---------- Toast ---------- */
.toast {
  position: absolute;
  left: 50%;
  top: calc(18px + env(safe-area-inset-top, 0px));
  transform: translateX(-50%) translateY(-8px);
  z-index: 60;
  background: var(--ink);
  color: var(--cream);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
  max-width: min(420px, calc(100% - 24px));
  text-align: center;
  box-shadow: var(--shadow);
}
.toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

@media (min-width: 769px) {
  .overlay { align-items: center; }
  .panel {
    margin-bottom: 0;
    border-radius: var(--radius);
    max-height: min(80vh, 720px);
  }
}
