:root {
  --bg: #0a0a0f;
  --panel: #12121a;
  --panel-glass: rgba(14, 14, 22, 0.92);
  --border: #2a2a3a;
  --border-soft: rgba(42, 42, 58, 0.85);
  --text: #c8c8d8;
  --dim: #6a6a7a;
  --gold: #c9a227;
  --gold-dim: rgba(201, 162, 39, 0.18);
  --gold-glow: rgba(201, 162, 39, 0.35);
  --cyan: #6a8ac9;
  --cyan-dim: rgba(106, 138, 201, 0.16);
  --ok: #5a9a5a;
  --ok-glow: rgba(90, 154, 90, 0.35);
  --err: #c94a4a;
  --err-glow: rgba(201, 74, 74, 0.4);
  --shadow-deep: 0 12px 40px rgba(0, 0, 0, 0.55), 0 2px 8px rgba(0, 0, 0, 0.4);
  --radius: 6px;
  --radius-lg: 10px;
}

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

body {
  font-family: "IBM Plex Mono", monospace;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

kbd {
  display: inline-block;
  padding: 0.12rem 0.38rem;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 4px;
  font-size: 0.65rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  background: linear-gradient(180deg, #1a1a26 0%, var(--panel) 100%);
  color: var(--text);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.35);
  vertical-align: baseline;
}

/* ─── Join screen (premium mono) ─── */
.join-screen {
  max-width: 380px;
  margin: 8vh auto;
  text-align: center;
  padding: 2rem 1.75rem 1.75rem;
  background:
    linear-gradient(180deg, rgba(201, 162, 39, 0.06) 0%, transparent 42%),
    var(--panel-glass);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-deep), inset 0 1px 0 rgba(201, 162, 39, 0.08);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}

.join-screen h1 {
  color: var(--gold);
  margin-bottom: 0.35rem;
  font-size: 1.65rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  text-shadow: 0 0 28px rgba(201, 162, 39, 0.25);
}

.join-screen > p:first-of-type {
  color: var(--dim);
  margin-bottom: 1.35rem;
  font-size: 0.78rem;
  letter-spacing: 0.04em;
  line-height: 1.5;
}

.join-controls {
  font-size: 0.7rem !important;
  color: var(--dim) !important;
  margin: 0.85rem 0 0.35rem !important;
  line-height: 1.55;
  letter-spacing: 0.01em;
}

.join-controls kbd {
  margin: 0 0.08rem;
  color: var(--gold);
  border-color: rgba(201, 162, 39, 0.35);
  background: linear-gradient(180deg, rgba(201, 162, 39, 0.12) 0%, var(--panel) 100%);
}

.join-screen input {
  width: 100%;
  padding: 0.8rem 0.85rem;
  background: rgba(8, 8, 12, 0.85);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
  letter-spacing: 0.04em;
  margin-bottom: 0.75rem;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.join-screen input::placeholder {
  color: var(--dim);
  letter-spacing: 0.06em;
  opacity: 0.75;
}

.join-screen input:focus {
  outline: none;
  border-color: var(--gold);
  box-shadow: 0 0 0 3px var(--gold-dim), 0 0 20px rgba(201, 162, 39, 0.08);
}

.join-screen button {
  width: 100%;
  padding: 0.8rem;
  background: linear-gradient(180deg, #d4b03a 0%, var(--gold) 55%, #a8841c 100%);
  color: #0a0a0f;
  border: 1px solid rgba(255, 220, 120, 0.25);
  border-radius: var(--radius);
  font-family: inherit;
  font-weight: 600;
  font-size: 0.9rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  cursor: pointer;
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.35), 0 6px 18px rgba(201, 162, 39, 0.2);
  transition: filter 0.12s ease, transform 0.1s ease, box-shadow 0.12s ease;
}

.join-screen button:hover {
  filter: brightness(1.06);
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.35), 0 8px 22px rgba(201, 162, 39, 0.32);
}

.join-screen button:active {
  transform: translateY(1px);
  filter: brightness(0.96);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.3), 0 3px 10px rgba(201, 162, 39, 0.15);
}

.err {
  color: var(--err);
  font-size: 0.82rem;
  min-height: 1.25rem;
  margin-top: 0.55rem;
  letter-spacing: 0.02em;
}

.hint {
  font-size: 0.72rem;
  color: var(--dim);
  margin-top: 1.25rem;
  letter-spacing: 0.02em;
}

.hint a {
  color: var(--gold);
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: border-color 0.12s ease, color 0.12s ease;
}

.hint a:hover {
  border-bottom-color: rgba(201, 162, 39, 0.5);
  color: #e0bc4a;
}

/* Join-screen X link (code → :verify) */
.join-x-link {
  margin-top: 1rem;
  max-width: 22rem;
  width: 100%;
  text-align: left;
  font-size: 0.75rem;
  color: var(--dim);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.55rem 0.75rem;
  background: rgba(18, 18, 26, 0.6);
}

.join-x-link summary {
  cursor: pointer;
  color: var(--cyan);
  font-weight: 600;
  list-style: none;
}

.join-x-link summary::-webkit-details-marker { display: none; }

.join-x-hint {
  margin: 0.55rem 0 0.45rem;
  line-height: 1.45;
  color: var(--dim);
}

.join-x-hint code,
.x-link-hint code,
.x-link-result code {
  color: var(--gold);
  font-size: 0.9em;
}

.join-x-form {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.join-x-form input {
  width: 100%;
  padding: 0.45rem 0.55rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--panel);
  color: var(--text);
  font-family: inherit;
  font-size: 0.8rem;
}

.join-x-form button {
  padding: 0.45rem 0.65rem;
  border: 1px solid var(--cyan);
  border-radius: 4px;
  background: var(--cyan-dim);
  color: var(--cyan);
  font-family: inherit;
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
}

.join-x-form button:hover {
  filter: brightness(1.1);
}

.join-x-result {
  margin-top: 0.5rem;
  color: var(--text);
  line-height: 1.45;
  word-break: break-word;
}

.join-x-result.err {
  color: var(--err);
}

/* ─── Game shell ─── */
.game {
  display: flex;
  flex-direction: column;
  height: 100vh;
  max-width: 100%;
  padding: 0.5rem 0.75rem 0;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  font-size: 0.8rem;
  margin-bottom: 0.5rem;
  flex-shrink: 0;
}

.topbar-left { display: flex; align-items: center; gap: 0.5rem; }
.topbar a { color: var(--gold); text-decoration: none; }
#header-status {
  color: var(--dim);
  flex: 1;
  text-align: center;
  font-size: 0.72rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conn-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--dim);
  box-shadow: 0 0 0 2px rgba(106, 106, 122, 0.2);
}
.conn-dot.ok {
  background: var(--ok);
  box-shadow: 0 0 0 2px rgba(90, 154, 90, 0.25), 0 0 8px var(--ok-glow);
}
.conn-dot.bad {
  background: var(--err);
  box-shadow: 0 0 0 2px rgba(201, 74, 74, 0.25), 0 0 8px var(--err-glow);
}

.topbar-actions { display: flex; gap: 0.35rem; }
.pill {
  padding: 0.35rem 0.6rem;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  border-radius: 999px;
  font-family: inherit;
  font-size: 0.68rem;
  cursor: pointer;
  transition: border-color 0.12s ease, color 0.12s ease, background 0.12s ease;
}
.pill:hover, .pill.active {
  border-color: var(--gold);
  color: var(--gold);
  background: var(--gold-dim);
}

.play-area {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#canvas {
  display: block;
  /* Keep native pixel aspect — stretching width breaks movement feel. */
  width: auto;
  max-width: 100%;
  height: auto;
  max-height: min(74vh, 760px);
  margin: 0 auto;
  background: #05060a;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
  box-shadow: 0 0 0 1px rgba(201, 162, 39, 0.1), 0 8px 32px rgba(0, 0, 0, 0.45);
  cursor: crosshair;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
}
#canvas:focus {
  border-color: var(--gold);
  box-shadow: 0 0 0 1px rgba(201, 162, 39, 0.35), 0 0 24px rgba(201, 162, 39, 0.12);
}
html.playing,
body.playing {
  overflow: hidden;
  height: 100%;
  overscroll-behavior: none;
  touch-action: manipulation;
}
#canvas.shake {
  animation: canvasShake 0.28s ease-out;
}
@keyframes canvasShake {
  0%, 100% { transform: translate(0, 0); }
  20% { transform: translate(-3px, 1px); }
  40% { transform: translate(3px, -1px); }
  60% { transform: translate(-2px, 0); }
  80% { transform: translate(2px, 1px); }
}
.play-area.hurt-flash {
  box-shadow: inset 0 0 0 2px rgba(201, 74, 74, 0.55);
}
.play-area.heal-flash {
  box-shadow: inset 0 0 0 2px rgba(90, 154, 90, 0.5);
}

/* ─── Context tip (stable height — no layout jump) ─── */
.context-tip {
  min-height: 1.35rem;
  height: 1.35rem;
  font-size: 0.68rem;
  color: var(--gold);
  opacity: 1;
  padding: 0.2rem 0.35rem;
  margin-top: 0.15rem;
  line-height: 1.15rem;
  letter-spacing: 0.02em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  background: linear-gradient(90deg, var(--gold-dim), transparent 70%);
  border-left: 2px solid rgba(201, 162, 39, 0.55);
  border-radius: 0 4px 4px 0;
  flex-shrink: 0;
  text-shadow: 0 0 12px rgba(201, 162, 39, 0.2);
}
/* Keep reserved slot even when empty — avoids jump when tip clears */
.context-tip:empty {
  display: block;
  opacity: 0;
  border-left-color: transparent;
  background: transparent;
  pointer-events: none;
}

/* ─── HUD bar ─── */
.hud-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.35rem 0.65rem;
  padding: 0.5rem 0.65rem;
  margin-top: 0.4rem;
  background:
    linear-gradient(180deg, rgba(201, 162, 39, 0.05) 0%, transparent 55%),
    var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.72rem;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.03), 0 4px 16px rgba(0, 0, 0, 0.25);
  flex-shrink: 0;
}

.hud-bar span {
  white-space: nowrap;
  color: var(--dim);
  letter-spacing: 0.02em;
}

/* Primary vitals: HP first — stronger hierarchy + bar fill feel */
.hud-bar > span:first-child {
  position: relative;
  color: var(--text);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  padding: 0.18rem 0.5rem 0.28rem;
  background: rgba(8, 8, 12, 0.65);
  border: 1px solid rgba(90, 154, 90, 0.35);
  border-radius: 4px;
  overflow: hidden;
  z-index: 0;
}

/* Simulated fill under HP text (decorative juice; low-HP uses warn) */
.hud-bar > span:first-child::before {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0;
  top: 0;
  width: 72%;
  background: linear-gradient(
    90deg,
    rgba(90, 154, 90, 0.45) 0%,
    rgba(90, 154, 90, 0.22) 70%,
    transparent 100%
  );
  border-radius: 3px 0 0 3px;
  z-index: -1;
  pointer-events: none;
}

.hud-bar > span:first-child::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 2px;
  background: linear-gradient(90deg, var(--ok) 0%, rgba(90, 154, 90, 0.35) 72%, transparent);
  pointer-events: none;
}

.hud-bar > span:nth-child(2),
.hud-bar > span:nth-child(3) {
  color: var(--cyan);
  font-weight: 500;
}

.hud-bar > span:nth-child(5) {
  color: var(--gold);
  font-weight: 500;
}

.hud-bar .warn {
  color: var(--err) !important;
  font-weight: 700;
  animation: warnPulse 1.1s ease-in-out infinite;
}

.hud-bar > span:first-child.warn {
  border-color: rgba(201, 74, 74, 0.55);
  background: rgba(201, 74, 74, 0.1);
  box-shadow: 0 0 12px rgba(201, 74, 74, 0.15);
}

.hud-bar > span:first-child.warn::before {
  width: 28%;
  background: linear-gradient(
    90deg,
    rgba(201, 74, 74, 0.55) 0%,
    rgba(201, 74, 74, 0.25) 80%,
    transparent 100%
  );
}

.hud-bar > span:first-child.warn::after {
  background: linear-gradient(90deg, var(--err) 0%, rgba(201, 74, 74, 0.4) 28%, transparent 40%);
  box-shadow: 0 0 6px var(--err-glow);
}

@keyframes warnPulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.78; }
}

.hud-bar .threat {
  color: #f07070;
  font-weight: 700;
  padding: 0.15rem 0.45rem;
  background: rgba(201, 74, 74, 0.12);
  border: 1px solid rgba(201, 74, 74, 0.45);
  border-radius: 4px;
  letter-spacing: 0.03em;
  box-shadow: 0 0 14px rgba(201, 74, 74, 0.18);
  animation: threatGlow 1.4s ease-in-out infinite;
}

@keyframes threatGlow {
  0%, 100% { box-shadow: 0 0 10px rgba(201, 74, 74, 0.15); }
  50% { box-shadow: 0 0 18px rgba(201, 74, 74, 0.35); }
}

.hud-bar .stairs-hint {
  color: var(--gold);
  font-weight: 600;
  padding: 0.15rem 0.5rem;
  background: linear-gradient(90deg, var(--gold-dim), rgba(201, 162, 39, 0.06));
  border: 1px solid rgba(201, 162, 39, 0.45);
  border-radius: 4px;
  letter-spacing: 0.03em;
  box-shadow: 0 0 12px rgba(201, 162, 39, 0.12);
  animation: stairsPulse 2s ease-in-out infinite;
}

@keyframes stairsPulse {
  0%, 100% { border-color: rgba(201, 162, 39, 0.4); }
  50% { border-color: rgba(201, 162, 39, 0.75); box-shadow: 0 0 16px rgba(201, 162, 39, 0.22); }
}

/* ─── Game log (stable height, high contrast) ─── */
.game-log {
  margin-top: 0.3rem;
  padding: 0.45rem 0.6rem;
  background: rgba(8, 8, 12, 0.92);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.7rem;
  line-height: 1.45;
  /* Fixed height band so row count changes don't shove layout */
  height: 5.6rem;
  min-height: 5.6rem;
  max-height: 5.6rem;
  overflow-y: auto;
  color: #9a9aac;
  flex-shrink: 0;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.02);
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.game-log .msg-danger {
  color: #e06060;
  font-weight: 600;
  text-shadow: 0 0 10px rgba(201, 74, 74, 0.2);
}
.game-log .msg-good {
  color: #6cb86c;
}
.game-log .msg-discover {
  color: #dbb84a;
  font-weight: 500;
}
.game-log .msg-miss {
  color: var(--dim);
  opacity: 0.9;
}
.game-log .msg-flash {
  animation: msgFlash 0.45s ease-out;
}
@keyframes msgFlash {
  from { background: rgba(201, 162, 39, 0.22); }
  to { background: transparent; }
}

/* ─── Inventory overlay panel ─── */
.inv-panel {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  width: min(300px, 92vw);
  max-height: 70%;
  overflow-y: auto;
  background:
    linear-gradient(165deg, rgba(201, 162, 39, 0.08) 0%, transparent 38%),
    linear-gradient(180deg, rgba(18, 18, 28, 0.97) 0%, rgba(10, 10, 16, 0.96) 100%);
  border: 1px solid rgba(201, 162, 39, 0.55);
  border-radius: var(--radius-lg);
  padding: 0.65rem 0.7rem 0.55rem;
  font-size: 0.72rem;
  z-index: 5;
  box-shadow:
    var(--shadow-deep),
    0 0 0 1px rgba(201, 162, 39, 0.08),
    0 0 32px rgba(201, 162, 39, 0.1),
    inset 0 1px 0 rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(14px) saturate(1.15);
  -webkit-backdrop-filter: blur(14px) saturate(1.15);
  scrollbar-width: thin;
  scrollbar-color: rgba(201, 162, 39, 0.35) transparent;
}

.inv-title {
  color: var(--gold);
  font-weight: 600;
  margin-bottom: 0.5rem;
  font-size: 0.68rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding-bottom: 0.4rem;
  border-bottom: 1px solid rgba(201, 162, 39, 0.2);
  text-shadow: 0 0 14px rgba(201, 162, 39, 0.2);
}

.inv-gear {
  color: var(--cyan);
  margin-bottom: 0.3rem;
  font-size: 0.68rem;
  padding: 0.2rem 0.35rem;
  background: var(--cyan-dim);
  border-radius: 3px;
  border-left: 2px solid rgba(106, 138, 201, 0.5);
}

.inv-row {
  display: flex;
  gap: 0.4rem;
  align-items: baseline;
  padding: 0.32rem 0.35rem;
  margin: 0 -0.15rem;
  border-bottom: 1px solid rgba(30, 30, 42, 0.9);
  border-radius: 4px;
  cursor: default;
  transition: background 0.1s ease, box-shadow 0.1s ease, border-color 0.1s ease;
}

.inv-row:last-of-type {
  border-bottom-color: transparent;
}

.inv-row:hover,
.inv-row:focus-within {
  background: linear-gradient(90deg, var(--gold-dim), rgba(201, 162, 39, 0.04));
  box-shadow: inset 0 0 0 1px rgba(201, 162, 39, 0.28);
  border-bottom-color: transparent;
}

.inv-row:hover kbd,
.inv-row:focus-within kbd {
  color: #0a0a0f;
  background: linear-gradient(180deg, #e0bc4a 0%, var(--gold) 100%);
  border-color: rgba(255, 220, 120, 0.4);
  box-shadow: 0 0 10px rgba(201, 162, 39, 0.35);
}

.inv-row kbd {
  min-width: 1.25rem;
  text-align: center;
  color: var(--gold);
  border-color: rgba(201, 162, 39, 0.35);
  background: linear-gradient(180deg, rgba(201, 162, 39, 0.14) 0%, var(--panel) 100%);
  font-size: 0.62rem;
  flex-shrink: 0;
}

.inv-char {
  color: var(--text);
  width: 1rem;
  font-weight: 600;
  flex-shrink: 0;
}

.inv-empty {
  color: var(--dim);
  padding: 0.5rem 0.25rem;
  font-style: italic;
  font-size: 0.7rem;
}

/* ─── Chat dock ─── */
.chat-dock {
  flex-shrink: 0;
  margin-top: 0.4rem;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px 8px 0 0;
  transition: max-height 0.2s ease;
  max-height: 220px;
  display: flex;
  flex-direction: column;
}

.chat-dock.collapsed {
  max-height: 36px;
  overflow: hidden;
}

.chat-dock.collapsed .chat-body { display: none; }

.chat-toggle {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  padding: 0.5rem 0.75rem;
  background: transparent;
  border: none;
  color: var(--text);
  font-family: inherit;
  font-size: 0.75rem;
  cursor: pointer;
  text-align: left;
}

.badge {
  background: var(--gold);
  color: #0a0a0f;
  font-size: 0.6rem;
  padding: 0.1rem 0.4rem;
  border-radius: 999px;
  font-weight: 600;
}

.chat-body {
  display: flex;
  flex-direction: column;
  padding: 0 0.5rem 0.5rem;
  min-height: 0;
  flex: 1;
}

.feed {
  flex: 1;
  overflow-y: auto;
  font-size: 0.72rem;
  line-height: 1.45;
  max-height: 120px;
  min-height: 60px;
  margin-bottom: 0.4rem;
  padding-right: 0.25rem;
}

.feed .line { margin-bottom: 0.3rem; word-break: break-word; }
.feed .line .who { color: var(--cyan); font-weight: 600; }
.feed .line.dm .who { color: var(--gold); }
.feed .line.sys { color: var(--dim); font-style: italic; }
.feed .line.me { color: var(--gold); }

.chat-form, .inline-form, .stack-form {
  display: flex;
  gap: 0.35rem;
}

.stack-form { flex-direction: column; }

.chat-form input, .inline-form input, .stack-form input {
  flex: 1;
  padding: 0.5rem 0.6rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  font-family: inherit;
  font-size: 0.75rem;
}

.chat-form button, .inline-form button, .stack-form button {
  padding: 0.5rem 0.75rem;
  background: var(--gold);
  color: #0a0a0f;
  border: none;
  border-radius: 4px;
  font-family: inherit;
  font-size: 0.72rem;
  cursor: pointer;
  font-weight: 600;
  white-space: nowrap;
}

/* ─── Social drawer ─── */
.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 40;
}

.social-drawer {
  position: fixed;
  top: 0;
  right: 0;
  width: min(320px, 92vw);
  height: 100%;
  background: var(--panel);
  border-left: 1px solid var(--border);
  z-index: 50;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.2s ease;
}

.social-drawer.open {
  transform: translateX(0);
}

.drawer-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
}

.drawer-head button {
  background: transparent;
  border: none;
  color: var(--dim);
  font-size: 1.4rem;
  cursor: pointer;
  line-height: 1;
}

.social-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
}

.social-tabs .tab {
  flex: 1;
  padding: 0.6rem 0.25rem;
  background: transparent;
  border: none;
  color: var(--dim);
  font-family: inherit;
  font-size: 0.72rem;
  cursor: pointer;
}

.social-tabs .tab.active {
  color: var(--gold);
  border-bottom: 2px solid var(--gold);
}

.tab-pane {
  display: none;
  flex: 1;
  flex-direction: column;
  padding: 0.75rem;
  overflow: hidden;
  min-height: 0;
}

.tab-pane.active { display: flex; }

.x-link-status {
  font-size: 0.78rem;
  color: var(--text);
  margin-bottom: 0.45rem;
  line-height: 1.4;
}

.x-link-status.linked {
  color: var(--ok);
}

.x-link-hint {
  font-size: 0.7rem;
  color: var(--dim);
  line-height: 1.5;
  margin-bottom: 0.65rem;
}

.x-link-result {
  margin-top: 0.65rem;
  font-size: 0.75rem;
  line-height: 1.5;
  color: var(--text);
  padding: 0.55rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: rgba(8, 8, 12, 0.5);
}

.x-link-result .verify-cmd {
  display: block;
  margin-top: 0.35rem;
  color: var(--gold);
  font-weight: 600;
  user-select: all;
}

.friend-list {
  list-style: none;
  font-size: 0.75rem;
  overflow-y: auto;
  flex: 1;
  margin-top: 0.5rem;
}

.friend-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.45rem 0;
  border-bottom: 1px solid var(--border);
}

.friend-list button {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--cyan);
  border-radius: 4px;
  font-size: 0.65rem;
  padding: 0.2rem 0.5rem;
  cursor: pointer;
  font-family: inherit;
}

.sublist {
  font-size: 0.72rem;
  color: var(--dim);
  margin-top: 0.35rem;
}

.sublist button {
  margin-left: 0.35rem;
  background: var(--gold);
  color: #0a0a0f;
  border: none;
  border-radius: 3px;
  font-size: 0.65rem;
  padding: 0.15rem 0.4rem;
  cursor: pointer;
  font-family: inherit;
}

/* ─── Controls ─── */
.controls-bar {
  flex-shrink: 0;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.45rem 0;
  font-size: 0.65rem;
  color: var(--dim);
}

.keys { display: flex; flex-wrap: wrap; gap: 0.35rem 0.75rem; }

.touch-controls {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}

html.playing,
body.playing {
  overscroll-behavior: none;
  touch-action: manipulation;
}

.touch-actions {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.touch-action,
.dpad-descend {
  background: linear-gradient(180deg, #1c1c28 0%, var(--panel) 100%);
  border: 1px solid rgba(201, 162, 39, 0.55);
  color: #e0bc4a;
  border-radius: 6px;
  font-size: 0.72rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  padding: 0.45rem 0.6rem;
  min-height: 2.25rem;
  min-width: 2.75rem;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  letter-spacing: 0.04em;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.5);
  box-shadow:
    0 2px 0 rgba(0, 0, 0, 0.4),
    inset 0 1px 0 rgba(255, 255, 255, 0.05);
  transition: background 0.08s ease, border-color 0.08s ease, transform 0.08s ease, box-shadow 0.08s ease;
}

.touch-action:hover,
.dpad-descend:hover {
  background: linear-gradient(180deg, #242434 0%, #1a1a24 100%);
  border-color: var(--gold);
  color: #f0d060;
}

.touch-action:active,
.dpad-btn:active,
.dpad-descend:active {
  background: linear-gradient(180deg, var(--gold-dim) 0%, rgba(201, 162, 39, 0.22) 100%);
  border-color: var(--gold);
  color: #fff0b0;
  transform: translateY(1px) scale(0.97);
  box-shadow:
    0 0 0 rgba(0, 0, 0, 0.4),
    inset 0 2px 6px rgba(0, 0, 0, 0.35),
    0 0 14px rgba(201, 162, 39, 0.25);
}

.dpad {
  display: grid;
  grid-template-columns: repeat(3, 2.4rem);
  grid-template-rows: repeat(3, 2.4rem);
  gap: 3px;
}

.dpad-btn {
  background: linear-gradient(180deg, #1a1a26 0%, var(--panel) 100%);
  border: 1px solid #3a3a4e;
  color: #d0d0e0;
  border-radius: 6px;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.55);
  box-shadow:
    0 2px 0 rgba(0, 0, 0, 0.4),
    inset 0 1px 0 rgba(255, 255, 255, 0.04);
  transition: border-color 0.08s ease, background 0.08s ease, transform 0.08s ease, color 0.08s ease;
}

.dpad-btn:hover {
  border-color: var(--gold);
  color: var(--gold);
}

.dpad-btn.mid {
  color: var(--dim);
  font-size: 0.95rem;
}

.dpad-btn.up { grid-column: 2; grid-row: 1; }
.dpad-btn.left { grid-column: 1; grid-row: 2; }
.dpad-btn.mid { grid-column: 2; grid-row: 2; }
.dpad-btn.right { grid-column: 3; grid-row: 2; }
.dpad-btn.down { grid-column: 2; grid-row: 3; }

@media (max-width: 720px), (pointer: coarse) {
  .keys { display: none; }
  .controls-bar {
    justify-content: center;
    position: sticky;
    bottom: 0;
    background: linear-gradient(transparent, var(--bg) 28%);
    padding-top: 0.75rem;
    padding-bottom: max(0.55rem, env(safe-area-inset-bottom));
    z-index: 20;
  }
  .touch-controls {
    margin: 0 auto;
    gap: 0.85rem;
  }
  .dpad {
    grid-template-columns: repeat(3, 3.1rem);
    grid-template-rows: repeat(3, 3.1rem);
    gap: 5px;
  }
  .dpad-btn {
    font-size: 1rem;
    /* High-contrast on mobile: never wash out against dungeon bg */
    background: linear-gradient(180deg, #222232 0%, #14141e 100%);
    border-color: #4a4a62;
    color: #ececf6;
  }
  .dpad-btn:active {
    background: linear-gradient(180deg, rgba(201, 162, 39, 0.35) 0%, rgba(201, 162, 39, 0.18) 100%);
    color: #fff8d0;
  }
  .touch-action,
  .dpad-descend {
    min-height: 2.85rem;
    min-width: 3.4rem;
    font-size: 0.85rem;
    background: linear-gradient(180deg, #242434 0%, #161620 100%);
    border-color: rgba(201, 162, 39, 0.7);
    color: #f0d060;
  }
  #canvas { max-height: min(55vh, 520px); }
}

@media (prefers-reduced-motion: reduce) {
  #canvas.shake,
  .game-log .msg-flash,
  .play-area.hurt-flash,
  .play-area.heal-flash,
  .hud-bar .warn,
  .hud-bar .threat,
  .hud-bar .stairs-hint {
    animation: none !important;
  }
  #canvas.shake { transform: none !important; }
  .play-area.hurt-flash,
  .play-area.heal-flash {
    box-shadow: none;
  }
  .touch-action:active,
  .dpad-btn:active,
  .dpad-descend:active {
    transform: none;
  }
}

/* ─── Toast ─── */
.toast {
  position: fixed;
  bottom: 4rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--panel);
  border: 1px solid var(--gold);
  color: var(--text);
  padding: 0.6rem 1rem;
  border-radius: 6px;
  font-size: 0.75rem;
  z-index: 60;
  max-width: 90vw;
  box-shadow: var(--shadow-deep);
}
.toast.err { border-color: var(--err); }

/* ─── Death / victory overlay ─── */
.overlay {
  position: fixed;
  inset: 0;
  background:
    radial-gradient(ellipse at 50% 30%, rgba(40, 20, 20, 0.35) 0%, transparent 55%),
    rgba(0, 0, 0, 0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 70;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.overlay-box {
  background:
    linear-gradient(180deg, rgba(201, 162, 39, 0.06) 0%, transparent 40%),
    var(--panel);
  border: 2px solid var(--gold);
  border-radius: var(--radius-lg);
  padding: 1.75rem 1.65rem 1.5rem;
  text-align: center;
  max-width: 440px;
  width: min(440px, 92vw);
  box-shadow: var(--shadow-deep), 0 0 40px rgba(201, 162, 39, 0.08);
}

.overlay-box.dead {
  border-color: #e05050;
  background:
    linear-gradient(180deg, rgba(201, 74, 74, 0.14) 0%, transparent 42%),
    linear-gradient(180deg, #161018 0%, var(--panel) 100%);
  box-shadow:
    var(--shadow-deep),
    0 0 48px rgba(201, 74, 74, 0.22),
    inset 0 1px 0 rgba(255, 100, 100, 0.08);
}

.overlay-box.won {
  border-color: #6cbc6c;
  background:
    linear-gradient(180deg, rgba(90, 154, 90, 0.16) 0%, transparent 42%),
    linear-gradient(180deg, #0e1610 0%, var(--panel) 100%);
  box-shadow:
    var(--shadow-deep),
    0 0 48px rgba(90, 154, 90, 0.22),
    inset 0 1px 0 rgba(140, 220, 140, 0.08);
}

.overlay-box h2 {
  color: var(--gold);
  margin-bottom: 0.75rem;
  font-size: 1.35rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.overlay-box.dead h2 {
  color: #f07070;
  text-shadow: 0 0 24px rgba(201, 74, 74, 0.45);
}

.overlay-box.won h2 {
  color: #7ed07e;
  text-shadow: 0 0 24px rgba(90, 154, 90, 0.45);
}

.end-detail {
  color: var(--text);
  font-size: 0.9rem;
  margin-bottom: 0.5rem;
  line-height: 1.45;
}

.end-x-handle {
  color: #1d9bf0;
  font-size: 0.85rem;
  font-weight: 600;
  margin: 0.15rem 0 0.35rem;
  letter-spacing: 0.02em;
}

.end-stats {
  color: #8a8a9c;
  font-size: 0.75rem;
  margin-bottom: 0.5rem;
  letter-spacing: 0.03em;
  font-variant-numeric: tabular-nums;
}

.end-gear {
  color: var(--cyan);
  font-size: 0.72rem;
  margin-bottom: 0.65rem;
}

.end-log {
  text-align: left;
  background: rgba(4, 4, 8, 0.92);
  border: 1px solid #2e2e40;
  border-radius: 6px;
  padding: 0.55rem 0.7rem;
  margin: 0 auto 1rem;
  max-width: 100%;
  font-size: 0.7rem;
  color: #a8a8bc;
  line-height: 1.5;
  max-height: 7.5rem;
  overflow-y: auto;
  box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.35);
  scrollbar-width: thin;
  scrollbar-color: #3a3a4e transparent;
}

.overlay-box.dead .end-log {
  border-color: rgba(201, 74, 74, 0.28);
  box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.4), 0 0 0 1px rgba(201, 74, 74, 0.06);
}

.overlay-box.won .end-log {
  border-color: rgba(90, 154, 90, 0.28);
  box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.4), 0 0 0 1px rgba(90, 154, 90, 0.06);
}

.end-log-line {
  margin-bottom: 0.28rem;
  color: #9a9aac;
}

.end-log-line:last-child {
  margin-bottom: 0;
  color: #e0e0ec;
  font-weight: 600;
  padding-top: 0.2rem;
  border-top: 1px solid rgba(42, 42, 58, 0.8);
}

.overlay-box.dead .end-log-line:last-child {
  color: #f0a0a0;
  border-top-color: rgba(201, 74, 74, 0.25);
}

.overlay-box.won .end-log-line:last-child {
  color: #b0e0b0;
  border-top-color: rgba(90, 154, 90, 0.25);
}

.end-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: center;
  margin-bottom: 0.75rem;
}

.end-actions button,
.end-actions a {
  padding: 0.65rem 1rem;
  border: none;
  border-radius: 4px;
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: filter 0.12s ease, transform 0.08s ease;
}

.end-actions button:hover,
.end-actions a:hover {
  filter: brightness(1.08);
}

.end-actions button:active,
.end-actions a:active {
  transform: translateY(1px);
}

.end-actions button { background: var(--gold); color: #0a0a0f; }
.end-actions .btn-secondary {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}
.end-actions .btn-secondary:hover {
  border-color: var(--gold);
  color: var(--gold);
}
.end-actions .btn-share-x {
  background: #1d9bf0;
  color: #fff;
}
.end-actions .btn-share-x:hover { filter: brightness(1.08); }

.end-card-wrap {
  margin: 0.5rem auto 0.85rem;
  max-width: 300px;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
  background: #08080c;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
}

.end-card-preview {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 1200 / 630;
  object-fit: cover;
}

.end-feedback {
  margin: 0.65rem 0 0.25rem;
  font-size: 0.85rem;
  color: var(--dim, #6a6a7a);
  text-align: center;
}
.end-feedback a {
  color: var(--gold, #c9a227);
  text-decoration: underline;
  text-underline-offset: 2px;
}
.end-feedback a:hover { filter: brightness(1.1); }
.end-hint {
  color: var(--dim);
  font-size: 0.68rem;
  margin-top: 0.25rem;
  letter-spacing: 0.02em;
}

.overlay-box button {
  margin-top: 1rem;
  padding: 0.6rem 1.2rem;
  background: var(--gold);
  border: none;
  border-radius: 4px;
  font-family: inherit;
  cursor: pointer;
}

@media (max-width: 700px) {
  #header-status { display: none; }
  .keys { font-size: 0.6rem; }
  .join-screen {
    margin: 4vh 0.75rem;
    padding: 1.5rem 1.25rem 1.35rem;
  }
  .hud-bar {
    font-size: 0.68rem;
    gap: 0.3rem 0.5rem;
    padding: 0.4rem 0.5rem;
  }
  .end-log {
    font-size: 0.68rem;
    max-height: 6.5rem;
  }
}
