/* Realm — dunkles, warmes Erzähl-Design */
:root {
  --bg: #14100e;
  --bg-raise: #1e1815;
  --bg-input: #2a2220;
  --border: #3a2f2a;
  --text: #e8ddd0;
  --text-dim: #a3937f;
  --accent: #d4a24e;
  --accent-dim: #8a6a33;
  --error: #e07a5f;
  --user-bubble: #2d2320;
  --radius: 12px;
  font-size: 16px;
}

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

body {
  background: var(--bg);
  color: var(--text);
  font-family: Georgia, 'Times New Roman', serif;
  min-height: 100vh;
}

h1, h2, h3 { font-weight: 600; letter-spacing: 0.02em; }
a { color: var(--accent); }
.hidden { display: none !important; }
.home-link,.mode-home { color: inherit; text-decoration: none; }
.mode-home { display: inline-flex; align-items: center; padding: .35rem .55rem; }
.billing-choice { border: 1px solid var(--border); border-radius: 10px; padding: 12px; margin: 14px 0; }
.billing-choice legend { font-weight: 700; padding: 0 6px; }
.billing-choice label { display: flex; gap: 10px; align-items: flex-start; cursor: pointer; padding: 9px; border-radius: 7px; }
.billing-choice label:has(input:checked) { background: rgba(190,150,70,.12); }
.billing-choice input { margin: 4px 0 0; width: auto; }
.billing-choice span { display: grid; gap: 2px; }
.billing-choice small { color: var(--text-dim); font-weight: 400; line-height: 1.35; }
.clear-key { display: flex; align-items: center; gap: 7px; color: var(--text-dim); font-size: .86rem; margin: -4px 0 10px; }
.clear-key input { width: auto; margin: 0; }
.key-guide { margin: 16px 0; border: 1px solid var(--border); border-radius: 9px; padding: 12px; }
.key-guide summary { cursor: pointer; font-weight: 700; }
.guide-tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-top: 14px; }
.guide-tabs section { background: rgba(0,0,0,.15); padding: 13px; border-radius: 8px; }
.guide-tabs h3 { margin: 0 0 10px; font-size: 1rem; }
.guide-tabs ol { padding-left: 20px; line-height: 1.45; }
.guide-visual { min-height: 78px; border: 1px solid #596b87; background: linear-gradient(140deg,#17213a,#24375c); border-radius: 8px; padding: 12px; display: flex; align-items: center; gap: 12px; color: #e9efff; }
.guide-visual.openai { border-color: #488273; background: linear-gradient(140deg,#102b27,#1c4b42); }
.guide-visual > span { display: grid; place-items: center; width: 36px; height: 36px; flex: 0 0 auto; border-radius: 50%; background: rgba(255,255,255,.16); font-weight: 700; }
.guide-visual div { display: grid; gap: 4px; }.guide-visual i { opacity: .75; font-size: .86rem; }

button {
  font: inherit;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--bg-raise);
  color: var(--text);
  border-radius: var(--radius);
  padding: 0.5rem 1rem;
}
button.primary {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: #fff;
}
button.primary:hover { background: var(--accent); color: #1a1208; }
button.ghost { background: transparent; border-color: transparent; font-size: 1.1rem; }
button.ghost:hover { background: var(--bg-input); }
button:disabled { opacity: 0.5; cursor: default; }

input, textarea, select {
  font: inherit;
  width: 100%;
  background: var(--bg-input);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.55rem 0.7rem;
}
input:focus, textarea:focus { outline: 1px solid var(--accent-dim); }
textarea { resize: vertical; }
label { display: block; margin-bottom: 0.8rem; color: var(--text-dim); font-size: 0.92rem; }
label input, label textarea, label select { margin-top: 0.3rem; }

.card {
  background: var(--bg-raise);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.4rem;
}
.row { display: flex; gap: 1rem; }
.row > label { flex: 1; }
.error { color: var(--error); }
.hint { color: var(--text-dim); font-size: 0.85rem; margin: 0.4rem 0; }

.notice {
  background: #33261455;
  border: 1px solid var(--accent-dim);
  border-radius: var(--radius);
  padding: 0.8rem 1rem;
  margin-bottom: 1.2rem;
}

/* Overlays (Login, Einstellungen) */
.overlay {
  position: fixed; inset: 0;
  background: #000a;
  display: flex; align-items: center; justify-content: center;
  z-index: 50;
  padding: 1rem;
}
.overlay .card { width: 100%; max-width: 430px; }
.overlay .card.settings-card { max-width: 820px; max-height: calc(100vh - 32px); overflow-y: auto; }
.overlay h1 { margin-bottom: 0.6rem; }
.overlay p { margin-bottom: 0.8rem; color: var(--text-dim); }
.overlay input { margin-bottom: 0.8rem; }
.settings-card details { margin: 0.6rem 0 1rem; }
.settings-card summary { color: var(--text-dim); cursor: pointer; margin-bottom: 0.6rem; }

/* Startseite */
.home-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 1.1rem 1.4rem;
  border-bottom: 1px solid var(--border);
}
.user-badge { color: var(--text-dim); font-size: 0.9rem; }
.home-main { max-width: 760px; margin: 0 auto; padding: 1.4rem; }
.adventure-list { display: flex; flex-direction: column; gap: 0.8rem; margin-bottom: 1.6rem; }
.adventure-item {
  display: flex; justify-content: space-between; align-items: center; gap: 1rem;
  background: var(--bg-raise);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.2rem;
  cursor: pointer;
}
.adventure-item:hover { border-color: var(--accent-dim); }
.adventure-item h3 { margin-bottom: 0.2rem; }
.adventure-item .meta { color: var(--text-dim); font-size: 0.85rem; }
.adventure-item .delete { flex-shrink: 0; }
.new-adventure h2 { margin-bottom: 1rem; }
.new-vitals-choice > label:not(.clear-key) { display: block; cursor: default; }
.new-vitals-choice select { margin-top: .35rem; }
.new-vitals-choice .hint { margin: 2px 9px 4px; }

/* Spielansicht */
#game-view { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }
.game-header {
  display: flex; align-items: center; gap: 0.6rem;
  padding: 0.6rem 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg-raise);
}
.game-header h1 { font-size: 1.05rem; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.header-right { display: flex; align-items: center; gap: 0.6rem; }
.cost-badge {
  color: var(--accent);
  font-size: 0.75rem;
  font-weight: 700;
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
  background: rgba(201,162,75,.15);
  border: 1px solid var(--border);
  padding: 0.32rem 0.75rem;
  border-radius: 3px;
  transition: background .2s;
}
.cost-badge:hover { background: rgba(201,162,75,.25); }

/* Werte stehen außerhalb der scrollenden Chat-Fläche und bleiben deshalb in
   jedem Zustand sichtbar. Die Balken selbst sind zugleich der Edit-Knopf. */
.vitals-bar {
  position: relative;
  display: grid;
  grid-template-columns: minmax(150px, 1fr) minmax(150px, 1fr) auto;
  align-items: center;
  gap: .75rem;
  padding: .55rem .9rem;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(90deg, rgba(123,45,45,.11), rgba(185,139,55,.08));
  flex-shrink: 0;
}
.vital-meter {
  display: grid;
  gap: .3rem;
  min-width: 0;
  padding: .25rem .4rem;
  border: 0;
  border-radius: 7px;
  background: transparent;
  text-align: left;
}
.vital-meter:hover { background: rgba(255,255,255,.035); }
.vital-heading { display: flex; justify-content: space-between; gap: .7rem; color: var(--text-dim); font-size: .76rem; }
.vital-heading strong { color: var(--text); font-variant-numeric: tabular-nums; }
.vital-track { display: block; height: 7px; overflow: hidden; border-radius: 999px; background: #0d0a09; border: 1px solid rgba(255,255,255,.05); }
.vital-fill { display: block; width: 100%; height: 100%; border-radius: inherit; transition: width .45s ease, background .25s ease; }
.vital-meter.health .vital-fill { background: linear-gradient(90deg,#8d3030,#d4675c); }
.vital-meter.health.critical .vital-fill { background: #e24848; box-shadow: 0 0 8px #d33a; }
.vital-meter.stamina .vital-fill { background: linear-gradient(90deg,#557b40,#a7b956); }
.vitals-edit { white-space: nowrap; font-size: .78rem !important; padding: .35rem .55rem; }
.vitals-notice {
  position: absolute;
  right: .8rem;
  bottom: -.2rem;
  transform: translateY(100%);
  z-index: 12;
  max-width: min(430px, calc(100vw - 1.6rem));
  padding: .45rem .7rem;
  border: 1px solid var(--accent-dim);
  border-radius: 7px;
  background: #2b211b;
  color: var(--text);
  font-size: .76rem;
  box-shadow: 0 7px 22px #0008;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
}
.vitals-notice.show { opacity: 1; }
.vitals-dialog-card h2 { margin-bottom: .65rem; }
.vitals-open-hint { padding: .7rem .8rem; border: 1px solid var(--accent-dim); border-radius: 8px; background: rgba(190,150,70,.08); }
.vitals-form { display: grid; grid-template-columns: 1fr 1fr; gap: .8rem; margin: 1rem 0 .4rem; }
.game-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 0.9rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg-raise);
  flex-shrink: 0;
}
.game-toolbar #focus-select { width: 11rem; }
.toolbar-cost{font-size:12px;color:var(--ink-dim);white-space:nowrap}
.game-toolbar label {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  margin: 0;
  font-size: 0.8rem;
  color: var(--text-dim);
  white-space: nowrap;
  min-width: 0;
}
.game-toolbar button.small { white-space: nowrap; height: 100%; }
.game-toolbar select {
  flex: 1;
  min-width: 0;
  width: 100%;
  max-width: 100%;
  margin: 0;
  border-radius: 999px;
  font-size: 0.8rem;
  padding: 0.25rem 0.6rem;
  text-overflow: ellipsis;
}
.game-toolbar .toolbar-vitals-toggle {
  justify-content: center;
  align-self: stretch;
  padding: .3rem .65rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--text);
  cursor: pointer;
}
.toolbar-vitals-toggle:has(input:checked) { border-color: var(--accent-dim); background: rgba(190,150,70,.1); }
.toolbar-vitals-toggle input { width: auto; margin: 0; accent-color: var(--accent); }
#difficulty-control.disabled { opacity: .45; }

.toolbar-more { position: relative; margin-left: auto; }
.toolbar-more-panel {
  position: absolute;
  top: calc(100% + 0.5rem);
  right: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  min-width: 16.5rem;
  padding: 0.9rem 1rem;
  background: var(--bg-raise);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.4);
}
.toolbar-more-panel.hidden { display: none; }
.toolbar-more-panel label {
  justify-content: space-between;
  gap: 0.75rem;
}
.toolbar-more-panel select { flex: 1; max-width: 10.5rem; padding: 0.35rem 0.7rem; }

.game-body { display: flex; flex: 1; min-height: 0; }
.chat-area { flex: 1; display: flex; flex-direction: column; min-width: 0; }

#messages { flex: 1; overflow-y: auto; padding: 1.2rem; scroll-behavior: smooth; }
.msg { max-width: 46rem; margin: 0 auto 1.1rem; line-height: 1.65; }
.msg p { margin-bottom: 0.7em; }
.msg.user {
  background: var(--user-bubble);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.7rem 1rem;
  color: #d9c8b0;
  font-style: italic;
}
.msg.model em { color: var(--accent); font-style: italic; }
.msg .model-tag { display: block; color: var(--text-dim); font-size: 0.72rem; margin-top: 0.4rem; font-family: ui-monospace, monospace; }
.msg.streaming::after { content: '▍'; color: var(--accent); animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: 0; } }
.msg.error-msg { color: var(--error); font-size: 0.9rem; }
.msg.proposal {
  border: 1px solid #b58a3a;
  border-radius: var(--radius);
  padding: 0.8rem 1rem;
  background: rgba(181, 138, 58, 0.08);
}
.msg.proposal ul { margin: 0.3rem 0 0.6rem 1.2rem; }
.msg.proposal button.small { margin-right: 0.4rem; }
.edit-hint { font-size: 0.75rem; color: var(--text-dim); margin: 0.3rem 0 0.5rem; }
.msg.user { position: relative; }
.msg-edit-btn {
  position: absolute;
  top: 0.15rem;
  right: 0.25rem;
  background: transparent;
  border: none;
  padding: 0.1rem 0.25rem;
  font-size: 0.8rem;
  opacity: 0.45;
}
.msg-edit-btn:hover { opacity: 1; background: transparent; }

.tts-btn {
  display: inline-block;
  margin-top: 0.4rem;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.1rem 0.5rem;
  font-size: 0.85rem;
  line-height: 1.4;
  opacity: 0.7;
  cursor: pointer;
}
.tts-btn:hover { opacity: 1; background: transparent; }
.tts-btn:disabled { opacity: 0.4; cursor: default; }
.msg.user textarea { margin: 0.2rem 0 0.4rem; }
.msg.user .edit-hint { font-size: 0.75rem; color: var(--text-dim); margin-bottom: 0.4rem; }
.msg.user button.small { margin-right: 0.4rem; }
.msg.scene img {
  display: block;
  max-width: 100%;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  cursor: zoom-in;
}

.composer {
  display: flex; gap: 0.5rem; align-items: flex-end;
  padding: 0.8rem 1rem;
  border-top: 1px solid var(--border);
  background: var(--bg-raise);
}
.composer textarea { flex: 1; max-height: 10rem; }
.composer .primary { height: 2.9rem; width: 3.2rem; font-size: 1.1rem; }
#mic-btn.recording { color: var(--error); border-color: var(--error); animation: mic-pulse 1.2s infinite; }
@keyframes mic-pulse { 50% { opacity: 0.5; } }

/* Seitenleiste */
.sidebar {
  width: 330px; flex-shrink: 0;
  border-left: 1px solid var(--border);
  background: var(--bg-raise);
  display: flex; flex-direction: column;
  min-height: 0;
}
.tabs { display: flex; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.tabs button { flex: 1; border: none; border-radius: 0; background: transparent; padding: 0.7rem 0.2rem; font-size: 0.88rem; color: var(--text-dim); }
.tabs button.active { color: var(--accent); border-bottom: 2px solid var(--accent); }
.tab-content { flex: 1; min-height: 0; overflow-y: auto; padding: 1rem; font-size: 0.9rem; line-height: 1.5; }
.npc-card { border-bottom: 1px solid var(--border); padding: 0.7rem 0; }
.npc-card h4 { color: var(--accent); }
.npc-card .rel { color: var(--text-dim); font-size: 0.82rem; }
.npc-card ul { margin: 0.4rem 0 0 1.1rem; color: var(--text-dim); font-size: 0.84rem; }
.world-row { padding: 0.45rem 0; border-bottom: 1px solid var(--border); }
.world-row b { color: var(--accent); }
.event-row { padding: 0.5rem 0; border-bottom: 1px solid var(--border); color: var(--text-dim); }
.event-row .src { font-size: 0.75rem; color: var(--accent-dim); }
#tab-story { white-space: pre-wrap; color: var(--text-dim); }
.spoiler-gate {
  border: 1px dashed var(--accent-dim);
  border-radius: 8px;
  padding: 0.9rem;
  text-align: center;
  margin-top: 0.5rem;
}
.spoiler-gate .spoiler-title { color: var(--accent); margin-bottom: 0.4rem; }
.spoiler-gate button { margin-top: 0.6rem; }

.map-box { overflow: hidden; border-radius: 8px; border: 1px solid var(--border); margin-bottom: 0.5rem; }
.map-box svg { width: 100%; height: auto; display: block; }
.zoomable { cursor: zoom-in; }
button.small { font-size: 0.82rem; padding: 0.35rem 0.7rem; }
.player-card { text-align: center; border-bottom: 2px solid var(--accent-dim); padding-bottom: 0.9rem; margin-bottom: 0.7rem; }
.player-card img { width: 100%; max-width: 210px; border-radius: 12px; border: 1px solid var(--border); margin-bottom: 0.5rem; }
.player-card h4 { color: var(--accent); font-size: 1.05rem; }
.player-card button { margin-top: 0.5rem; }
.inv-list { list-style: none; }
.inv-list li { padding: 0.45rem 0; border-bottom: 1px solid var(--border); }
.tabs button { font-size: 0.8rem; padding: 0.7rem 0.1rem; }

/* Lightbox: Porträt & Karte groß ansehen + herunterladen */
.lightbox {
  position: fixed; inset: 0;
  background: #000c;
  display: flex; align-items: center; justify-content: center;
  z-index: 60;
  padding: 1rem;
}
.lightbox-inner {
  display: flex; flex-direction: column; gap: 0.8rem;
  max-width: min(96vw, 1100px); max-height: 96dvh;
}
.lightbox-content { overflow: auto; border-radius: var(--radius); min-height: 0; }
.lightbox-content img { display: block; max-width: 100%; max-height: 80dvh; margin: 0 auto; border-radius: var(--radius); }
.lightbox-content .lightbox-map svg { display: block; width: min(92vw, 1050px); height: auto; }
.lightbox-actions { display: flex; gap: 0.6rem; justify-content: center; flex-wrap: wrap; }

/* Seitenleisten-Hintergrund & Schließen-Knopf (nur mobil sichtbar) */
.backdrop { position: fixed; inset: 0; background: #0009; z-index: 39; }
.sidebar-close { display: none; }

/* KI-Helfer-Chat */
.create-row { display: flex; gap: 0.8rem; flex-wrap: wrap; align-items: center; }
.assist-panel {
  position: fixed; right: 1rem; bottom: 1rem; z-index: 45;
  width: min(400px, calc(100vw - 2rem));
  height: min(560px, calc(100dvh - 2rem));
  display: flex; flex-direction: column;
  background: var(--bg-raise);
  border: 1px solid var(--accent-dim);
  border-radius: var(--radius);
  box-shadow: 0 10px 40px #000c;
}
.assist-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 0.6rem 0.9rem;
  border-bottom: 1px solid var(--border);
  color: var(--accent);
}
.assist-messages { flex: 1; overflow-y: auto; padding: 0.8rem; font-size: 0.92rem; line-height: 1.5; }
.assist-msg { margin-bottom: 0.8rem; }
.assist-msg p { margin-bottom: 0.5em; }
.assist-msg.user {
  background: var(--user-bubble);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.5rem 0.8rem;
  margin-left: 1.5rem;
}
.assist-msg.thinking { color: var(--text-dim); font-style: italic; }
.assist-proposal {
  margin-top: 0.5rem;
  border: 1px dashed var(--accent-dim);
  border-radius: 8px;
  padding: 0.6rem 0.8rem;
  font-size: 0.85rem;
}
.assist-proposal ul { margin: 0.4rem 0 0.6rem 1.1rem; color: var(--text-dim); }
.assist-composer {
  display: flex; gap: 0.5rem; align-items: flex-end;
  padding: 0.6rem;
  border-top: 1px solid var(--border);
}
.assist-composer textarea { flex: 1; max-height: 7rem; }

@media (max-width: 900px) {
  .sidebar {
    position: fixed; right: 0; top: 0; bottom: 0; z-index: 40;
    transform: translateX(100%);
    transition: transform 0.25s;
    box-shadow: -8px 0 30px #000a;
    width: min(88vw, 340px);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-close {
    display: block;
    position: absolute; top: 0.25rem; right: 0.4rem; z-index: 2;
    padding: 0.35rem 0.6rem;
  }
  .tabs { padding-right: 2.4rem; }
  .row { flex-direction: column; gap: 0; }
  .cost-badge { display: inline; font-size:0.7rem; }
}

/* Auf Desktop ist die Seitenleiste dauerhaft sichtbar; der mobile Toggle
   und dessen Abdunkelung dürfen dort keine Klicks blockieren. */
@media (min-width: 901px) {
  #sidebar-toggle { display: none; }
  .backdrop { display: none !important; }
}

/* Smartphone-Feinschliff */
@media (max-width: 700px) {
  .guide-tabs { grid-template-columns: 1fr; }
  .mode-home { display: none; }
  :root { font-size: 15px; }
  /* mind. 16px verhindert den Auto-Zoom von iOS beim Fokussieren */
  input, textarea, select { font-size: 16px; }
  .home-header { padding: 0.9rem 1rem; }
  .home-main { padding: 1rem 0.8rem; }
  .card { padding: 1.1rem 1rem; }
  .adventure-item { padding: 0.8rem 0.9rem; }
  .game-header { padding: 0.5rem 0.6rem; gap: 0.4rem; }
  .game-header h1 { font-size: .92rem; }
  .cost-badge { max-width: 42vw; overflow: hidden; text-overflow: ellipsis; padding: .3rem .5rem; }
  .vitals-bar { grid-template-columns: 1fr 1fr auto; gap: .35rem; padding: .42rem .55rem; }
  .vital-heading { font-size: .7rem; gap: .3rem; }
  .vital-meter { padding: .2rem; }
  .vitals-edit { padding: .3rem; }
  .vitals-edit span { display: none; }
  /* 2×2-Raster statt Flex-Umbruch: Fokus/Werte in Zeile 1, Szene/Mehr in
     Zeile 2 — kein "margin-left:auto" mehr, das beim Umbruch eine Lücke
     zwischen Szene-Button und Mehr-Button aufreißen konnte. */
  .game-toolbar { display: grid; grid-template-columns: 1fr 1fr; gap: 0.45rem; padding: 0.42rem 0.55rem; }
  .game-toolbar label { font-size: .72rem; min-width: 0; }
  .game-toolbar #focus-select { width: auto; }
  .game-toolbar select { padding: 0.25rem 0.4rem; font-size: 16px; }
  .game-toolbar #scene-btn { white-space: normal; line-height: 1.15; padding: .35rem .45rem; font-size: .72rem; }
  .toolbar-more-panel { min-width: min(88vw, 16.5rem); }
  .vitals-form { grid-template-columns: 1fr; gap: 0; }
  #messages { padding: 0.9rem 0.8rem; }
  .composer {
    padding: 0.5rem 0.6rem;
    padding-bottom: calc(0.5rem + env(safe-area-inset-bottom));
  }
  .composer .ghost { padding: 0.5rem 0.55rem; }
  .composer .primary { height: 2.7rem; width: 2.9rem; }
  .assist-panel { right: 0; bottom: 0; width: 100vw; height: min(85dvh, 560px); border-radius: var(--radius) var(--radius) 0 0; }
  .lightbox { padding: 0.5rem; }
  .lightbox-actions button { flex: 1; }
}
