:root {
  --bg: #07060d;
  --panel: rgba(16, 12, 24, 0.86);
  --panel-solid: #15101f;
  --line: #3b2f4f;
  --line-hi: #7a6696;
  --text: #f4ecdb;
  --muted: #a99fbd;
  --accent: #ffb347;
  --accent-dk: #c46a12;
  --gold: #ffd25a;
  --hp: #d8323a;
  --drunk: #e8a630;
  --trip: #9d5cff;
  --ok: #5fe08a;
  --warn: #ff7070;
  --px: 'Pixelify Sans', 'Silkscreen', monospace;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  touch-action: none; overscroll-behavior: none; -webkit-text-size-adjust: 100%;
  background: var(--bg); color: var(--text);
  font-family: var(--px);
  -webkit-tap-highlight-color: transparent;
  user-select: none; -webkit-user-select: none;
}
button { font-family: var(--px); color: inherit; }
kbd {
  display: inline-block; min-width: 18px; padding: 0 4px;
  font: 600 11px var(--px); line-height: 16px; text-align: center;
  color: var(--text); background: #2a2238; border: 1px solid var(--line-hi);
  box-shadow: 0 2px 0 #000;
}
.screen, .modal-card, #chatLog { touch-action: pan-y; }
#game, #overlay { position: fixed; inset: 0; width: 100vw; height: 100vh; display: block; touch-action: none; }
#game { image-rendering: pixelated; image-rendering: crisp-edges; }
#overlay { pointer-events: none; }

/* Pixel-Rahmen */
.panel, .card, .modal-card, #chatInput, .field input, .slot, #emotes button, .toast {
  border: 2px solid var(--line);
  box-shadow: inset 0 0 0 2px rgba(0, 0, 0, 0.55), 0 3px 0 rgba(0, 0, 0, 0.55);
}
.panel { background: var(--panel); }
.muted { color: var(--muted); }
.link { background: none; border: 0; color: var(--muted); font-size: 14px; cursor: pointer; text-decoration: underline; text-underline-offset: 3px; }
.link:hover { color: var(--text); }

/* ---------- Titelbildschirm ---------- */
.screen {
  position: fixed; inset: 0; z-index: 10;
  display: grid; place-items: safe center; overflow-y: auto;
  padding: 16px;
}
#title {
  align-content: center; justify-items: center; gap: 6px; text-align: center;
  background: radial-gradient(ellipse at 50% 55%, rgba(255, 120, 40, 0.10), transparent 55%),
              linear-gradient(to bottom, rgba(5, 4, 12, 0.55), rgba(5, 4, 12, 0.25) 40%, rgba(5, 4, 12, 0.75));
  cursor: pointer;
}
.title-logo {
  width: min(640px, 94vw); image-rendering: pixelated;
  filter: drop-shadow(0 8px 0 rgba(0, 0, 0, 0.6));
  animation: bob 2.6s steps(12) infinite;
}
@keyframes bob { 0%, 100% { transform: translateY(0) rotate(-0.6deg); } 50% { transform: translateY(-8px) rotate(0.6deg); } }
.title-claim { margin: 4px 0 18px; font-size: clamp(16px, 2.6vw, 22px); color: var(--accent); text-shadow: 3px 3px 0 #000; letter-spacing: 0.04em; }
.press {
  font: 400 clamp(18px, 3vw, 26px) Silkscreen, monospace; color: #fff;
  background: none; border: 0; cursor: pointer; text-shadow: 3px 3px 0 #000;
  animation: blink 1.1s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0.15; } }
.title-foot { position: fixed; bottom: calc(14px + var(--safe-b)); left: 0; right: 0; margin: 0; font-size: 14px; color: var(--muted); text-shadow: 2px 2px 0 #000; }

/* ---------- Charakter-Editor ---------- */
#join { background: rgba(5, 4, 12, 0.6); }
.card {
  width: min(420px, 100%);
  padding: 0 22px 18px;
  background: var(--panel);
  text-align: center;
}
.card-head { margin: -8px -10px 0; }
.card-logo { width: 100%; max-width: 330px; image-rendering: pixelated; filter: drop-shadow(0 5px 0 rgba(0, 0, 0, 0.55)); margin-top: -48px; }
.creator { display: flex; align-items: center; justify-content: center; gap: 14px; margin: 4px 0 12px; }
.stage {
  width: 150px; padding: 8px 0 6px;
  background: radial-gradient(ellipse at 50% 85%, rgba(255, 150, 60, 0.25), transparent 65%), #120e1b;
  border: 2px solid #000; box-shadow: inset 0 0 0 2px var(--line);
}
#preview { width: 105px; height: 126px; image-rendering: pixelated; display: block; margin: 0 auto; }
#skinName { font-size: 16px; color: var(--accent); margin-top: 4px; }
.arrow {
  width: 40px; height: 40px; font-size: 16px; cursor: pointer;
  background: #2a2238; border: 2px solid #000; box-shadow: inset 0 -3px 0 #120e1b, 0 3px 0 #000;
}
.arrow:active { transform: translateY(2px); box-shadow: inset 0 -1px 0 #120e1b, 0 1px 0 #000; }
.field { display: grid; gap: 6px; margin-bottom: 12px; font-size: 14px; color: var(--muted); text-align: left; }
.name-row { display: flex; gap: 6px; }
.field input {
  flex: 1; min-width: 0; font: inherit; font-size: 17px; color: var(--text);
  padding: 9px 12px; background: rgba(0, 0, 0, 0.4); outline: none;
}
.field input:focus { border-color: var(--accent); }
.icon-btn { width: 44px; font-size: 20px; cursor: pointer; background: #2a2238; border: 2px solid #000; box-shadow: inset 0 -3px 0 #120e1b; }
.icon-btn:active { transform: rotate(-20deg); }
.swatches { display: flex; gap: 8px; flex-wrap: wrap; }
.swatch { width: 30px; height: 30px; padding: 0; cursor: pointer; border: 2px solid #000; box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.15); }
.swatch[aria-pressed="true"] { outline: 2px solid var(--accent); outline-offset: 2px; }
button.primary {
  width: 100%; margin-top: 4px; padding: 12px;
  font: 700 19px var(--px); color: #2a1404; letter-spacing: 0.02em;
  background: var(--accent); border: 2px solid #000;
  box-shadow: inset 0 -4px 0 var(--accent-dk), 0 3px 0 #000; cursor: pointer;
}
button.primary:hover { filter: brightness(1.08); }
button.primary:active { transform: translateY(2px); box-shadow: inset 0 -2px 0 var(--accent-dk), 0 1px 0 #000; }
button.primary:disabled { opacity: 0.6; cursor: wait; }
#backBtn { margin-top: 10px; }
.error { color: var(--warn); font-size: 14px; margin: 10px 0 0; }

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; z-index: 5; }
#hud > * { pointer-events: auto; }

#status { position: absolute; top: 12px; left: 12px; padding: 8px 10px; width: 236px; }
.who { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; font-size: 15px; }
#meName { color: #ffd27a; font-weight: 600; max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#coins { color: var(--gold); font-size: 15px; }
#coins.bump { animation: bump 0.4s steps(4); }
@keyframes bump { 50% { transform: scale(1.35); } }
.bar { display: flex; align-items: center; gap: 6px; margin-top: 4px; font-size: 13px; }
.bar .lbl { width: 18px; text-align: center; }
.bar .val { width: 36px; text-align: right; font-size: 12px; color: var(--muted); }
.track { flex: 1; height: 10px; background: #1b1626; border: 2px solid #000; }
.fill { height: 100%; width: 100%; transition: width 0.2s steps(5); box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.3); }
.fill.hp { background: var(--hp); }
.fill.hp.low { animation: pulse 0.6s steps(2) infinite; }
@keyframes pulse { 50% { background: #ff7a7a; } }
.fill.drunk { background: var(--drunk); width: 0; }
.fill.trip { width: 0; background: linear-gradient(90deg, #ff4fd8, #9d5cff, #4fd2ff, #6cff8a, #ffe14f); background-size: 200% 100%; animation: rainbow 2s linear infinite; }
@keyframes rainbow { to { background-position: 200% 0; } }

#mapbox { position: absolute; top: 12px; right: 12px; padding: 4px; }
#minimap { display: block; width: 132px; height: 132px; image-rendering: pixelated; }
#online { font-size: 12px; text-align: center; color: var(--ok); padding-top: 3px; }
#feed { position: absolute; top: 172px; right: 12px; display: flex; flex-direction: column; gap: 4px; align-items: flex-end; pointer-events: none; }
.feed-item { padding: 3px 8px; background: rgba(16, 12, 24, 0.8); border: 2px solid #000; font-size: 13px; animation: slidein 0.2s steps(3); }
.feed-item b { color: #ffd27a; font-weight: 600; }
.feed-item i { color: var(--warn); font-style: normal; }
.feed-item img { height: 14px; image-rendering: pixelated; vertical-align: -2px; margin: 0 4px; }
.feed-item.fade { opacity: 0; transition: opacity 0.6s; }
@keyframes slidein { from { transform: translateX(20px); opacity: 0; } }

#hotbar {
  position: absolute; left: 50%; bottom: calc(12px + var(--safe-b)); transform: translateX(-50%);
  display: flex; gap: 6px;
}
.slot {
  position: relative; width: 64px; height: 64px; padding: 0; cursor: pointer;
  background: var(--panel-solid); display: grid; place-items: center;
}
.slot img { width: 34px; height: 34px; object-fit: contain; image-rendering: pixelated; margin-top: -8px; }
.slot #slotFist { font-size: 26px; margin-top: -8px; }
.slot .key { position: absolute; top: 2px; left: 3px; font-size: 11px; color: var(--muted); }
.slot .count { position: absolute; top: 1px; right: 4px; font-size: 14px; font-weight: 700; color: #fff; text-shadow: 2px 2px 0 #000; }
.slot .name { position: absolute; bottom: 2px; left: 0; right: 0; font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; }
.slot.empty img { opacity: 0.3; filter: grayscale(1); }
.slot.empty .count { color: var(--warn); }
.slot.active { border-color: var(--accent); background: #2a1c12; }
.slot:hover { border-color: var(--line-hi); }
.slot:active { transform: translateY(2px); }
.slot.flash { animation: flash 0.3s steps(3); }
@keyframes flash { 50% { background: #5a2020; border-color: var(--warn); } }

#emotes { position: absolute; right: 12px; bottom: calc(12px + var(--safe-b)); display: flex; flex-direction: column; gap: 5px; }
#emotes button {
  display: flex; align-items: center; gap: 7px; padding: 6px 9px; min-width: 118px;
  font-size: 14px; background: var(--panel); cursor: pointer;
}
#emotes button em { font-style: normal; flex: 1; text-align: left; }
#emotes button:hover { border-color: var(--line-hi); }
#emotes button.on { background: rgba(255, 160, 60, 0.28); border-color: var(--accent); }
#emotes #micBtn.on { background: rgba(95, 224, 138, 0.22); border-color: var(--ok); }
#emotes #micBtn.talking span { animation: blink 0.4s steps(2) infinite; }

#chat { position: absolute; left: 12px; bottom: calc(12px + var(--safe-b)); width: min(360px, calc(50vw - 170px)); }
#chatLog {
  max-height: 168px; overflow-y: auto; display: flex; flex-direction: column; gap: 3px;
  margin-bottom: 6px; font-size: 15px; line-height: 1.3;
  mask-image: linear-gradient(to bottom, transparent, #000 26px);
  -webkit-mask-image: linear-gradient(to bottom, transparent, #000 26px);
  scrollbar-width: none;
}
#chatLog .msg { text-shadow: 2px 2px 0 #000; word-wrap: break-word; }
#chatLog .msg b { color: var(--accent); font-weight: 600; }
#chatLog .sys { color: var(--muted); }
#chatInput { width: 100%; padding: 8px 10px; font: 15px var(--px); color: var(--text); background: var(--panel); outline: none; }
#chatInput:focus { border-color: var(--accent); }

#touch { display: none; }

#tips {
  position: absolute; left: 50%; top: 14px; transform: translateX(-50%);
  width: min(420px, calc(100vw - 32px)); padding: 10px 12px; font-size: 15px; line-height: 1.45;
  border-color: var(--accent);
}
#tips b { color: var(--accent); }
.tip-actions { display: flex; justify-content: space-between; align-items: center; margin-top: 6px; }
#tipNext { background: #2a2238; border: 2px solid #000; padding: 3px 10px; cursor: pointer; font-size: 14px; }

#deathScreen {
  position: absolute; inset: 0; display: grid; place-content: center; text-align: center; gap: 6px;
  background: radial-gradient(ellipse at center, rgba(120, 0, 0, 0.12), rgba(70, 0, 0, 0.6));
  pointer-events: none;
}
.dead-title { font-family: Silkscreen, monospace; font-size: clamp(30px, 6vw, 56px); color: #ff5454; text-shadow: 4px 4px 0 #000; }
#deadBy { font-size: 18px; text-shadow: 2px 2px 0 #000; }
#deadCount { font-family: Silkscreen, monospace; font-size: 40px; color: #fff; text-shadow: 3px 3px 0 #000; }

/* ---------- Modals ---------- */
.modal { position: absolute; inset: 0; display: grid; place-items: center; padding: 16px; background: rgba(0, 0, 0, 0.45); z-index: 8; }
.modal-card { width: min(440px, 100%); max-height: calc(100vh - 32px); overflow-y: auto; padding: 16px 18px; background: var(--panel-solid); }
.modal-card h2 { margin: 0 0 4px; font-size: 22px; color: var(--accent); }
.modal-card p { margin: 0 0 12px; }

.shop-head { display: flex; gap: 12px; align-items: center; margin-bottom: 10px; }
.shop-head img { width: 76px; image-rendering: pixelated; }
#shopGreet { color: var(--muted); font-style: italic; margin: 0; }
.offer {
  display: grid; grid-template-columns: 40px 1fr auto; gap: 10px; align-items: center;
  padding: 8px; margin-bottom: 6px; background: #1d1629; border: 2px solid #000;
}
.offer img { width: 32px; height: 32px; object-fit: contain; image-rendering: pixelated; justify-self: center; }
.offer .t { font-size: 16px; }
.offer .d { font-size: 12px; color: var(--muted); }
.offer button {
  min-width: 82px; padding: 8px 10px; font: 700 15px var(--px); color: #2a1404; cursor: pointer;
  background: var(--gold); border: 2px solid #000; box-shadow: inset 0 -3px 0 #b8861b;
}
.offer button:disabled { background: #4a4152; color: #8c8299; box-shadow: none; cursor: not-allowed; }
.offer button:not(:disabled):active { transform: translateY(2px); }
.offer kbd { margin-right: 6px; }
.shop-foot { display: flex; justify-content: space-between; align-items: center; margin-top: 8px; font-size: 15px; }
.shop-foot b { color: var(--gold); }

.slider, .check { display: flex; align-items: center; gap: 10px; margin: 10px 0; font-size: 15px; }
.slider input { flex: 1; accent-color: var(--accent); }
.check input { accent-color: var(--accent); width: 18px; height: 18px; }
.controls { margin: 12px 0; padding: 10px; background: #1d1629; border: 2px solid #000; font-size: 14px; line-height: 2; color: var(--muted); }
.menu-actions { display: grid; gap: 10px; justify-items: center; }

#scoreboard table { width: 100%; border-collapse: collapse; margin: 8px 0 12px; font-size: 15px; }
#scoreboard th { text-align: left; color: var(--muted); font-weight: 400; padding: 4px; border-bottom: 2px solid var(--line); }
#scoreboard td { padding: 5px 4px; border-bottom: 1px solid #2a2238; }
#scoreboard th:not(:first-child), #scoreboard td:not(:first-child) { text-align: right; width: 44px; }
#scoreboard tr.me td { color: #ffd27a; }

/* ---------- Toasts ---------- */
#toasts { position: fixed; top: 70px; left: 50%; transform: translateX(-50%); z-index: 20; display: flex; flex-direction: column; gap: 6px; align-items: center; pointer-events: none; }
.toast { padding: 6px 12px; background: var(--panel-solid); font-size: 15px; animation: drop 0.2s steps(3); max-width: calc(100vw - 32px); text-align: center; }
.toast.ok { border-color: var(--ok); }
.toast.warn { border-color: var(--warn); }
.toast.coin { border-color: var(--gold); color: var(--gold); }
.toast.trip { border-color: var(--trip); background: linear-gradient(90deg, #3a1550, #15304f); }
.toast.out { opacity: 0; transition: opacity 0.4s; }
@keyframes drop { from { transform: translateY(-10px); opacity: 0; } }

/* ---------- Kleinere Bildschirme ---------- */
@media (max-width: 900px) {
  #emotes button { min-width: 0; }
  #emotes button em, #emotes kbd { display: none; }
  #chat { width: min(300px, calc(100vw - 420px)); }
}

/* ---------- Touch ---------- */
@media (pointer: coarse) {
  kbd, .slot .key { display: none; }
  #status { width: 176px; padding: 6px 8px; }
  #meName { max-width: 96px; font-size: 13px; }
  #minimap { width: 92px; height: 92px; }
  #feed { top: 128px; }
  .feed-item { font-size: 11px; }

  #touch { display: block; }
  #joystick {
    position: absolute; left: 18px; bottom: calc(92px + var(--safe-b));
    width: 120px; height: 120px; border-radius: 50%;
    background: rgba(255, 255, 255, 0.07); border: 2px solid rgba(255, 255, 255, 0.2); touch-action: none;
  }
  #stick {
    position: absolute; left: 50%; top: 50%; width: 50px; height: 50px; margin: -25px 0 0 -25px; border-radius: 50%;
    background: rgba(255, 255, 255, 0.22); border: 2px solid rgba(255, 255, 255, 0.4);
  }
  #attackBtn, #useBtn {
    position: absolute; border-radius: 50%; border: 3px solid #000; touch-action: manipulation;
    box-shadow: inset 0 -5px 0 rgba(0, 0, 0, 0.35), 0 3px 0 #000;
  }
  #attackBtn { right: 20px; bottom: calc(96px + var(--safe-b)); width: 84px; height: 84px; font-size: 34px; background: #a3232b; }
  #useBtn { right: 112px; bottom: calc(110px + var(--safe-b)); width: 56px; height: 56px; font-size: 24px; background: #2a2238; }
  #attackBtn:active, #useBtn:active { transform: translateY(3px); }

  #hotbar { gap: 4px; }
  .slot { width: 54px; height: 54px; }
  .slot img { width: 28px; height: 28px; }

  #emotes { bottom: auto; top: 156px; right: 12px; }
  #emotes button { padding: 6px; font-size: 16px; }
  #emotes #micBtn.on em { display: inline !important; font-size: 13px; }

  #chat { left: 12px; top: 100px; bottom: auto; width: min(240px, calc(100vw - 150px)); }
  #chatLog { max-height: 64px; font-size: 12px; }
  /* iOS zoomt bei Eingabefeldern unter 16px automatisch hinein */
  #chatInput { padding: 5px 8px; font-size: 16px; }
  .field input { font-size: 16px; }
  #tips { top: auto; bottom: calc(220px + var(--safe-b)); }
}
