* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: #000;
  color: #d8b94a;
  font-family: Georgia, "Times New Roman", serif;
  overflow: hidden;
  touch-action: manipulation;
}

#app {
  display: grid;
  grid-template-rows: auto 1fr auto;
  /* minmax(0, 1fr) verhindert, dass die Canvas-Eigenbreite die Spalte (und
     damit die Tastatur) breiter als den Viewport macht. */
  grid-template-columns: minmax(0, 1fr);
  width: 100%;
  height: 100%;
  overflow: hidden;
}

[hidden] { display: none !important; }

/* Anmeldung */
#auth { height: 100%; display: flex; align-items: center; justify-content: center; }
.auth-box {
  width: min(360px, 90vw);
  padding: 2rem;
  background: #1f1830;
  border: 1px solid #4a3c6e;
  border-radius: 8px;
}
.auth-box h1 {
  margin: 0; text-align: center; font-size: 2.2rem;
  letter-spacing: 0.1em; color: #d8b94a;
}
.subtitle { margin: 0.25rem 0 1.5rem; text-align: center; font-style: italic; color: #9d92b8; }
#auth-form { display: flex; flex-direction: column; gap: 0.4rem; }
#auth-form label { font-size: 0.9rem; color: #9d92b8; }
#auth-form input {
  padding: 0.5rem; margin-bottom: 0.6rem; background: #14101e;
  border: 1px solid #4a3c6e; border-radius: 4px; color: #e8e2d0; font-size: 1rem;
}
.buttons { display: flex; gap: 0.6rem; margin-top: 0.4rem; }
.buttons button {
  flex: 1; padding: 0.55rem 0.9rem; background: #d8b94a; border: none;
  border-radius: 4px; color: #14101e; font-weight: bold; cursor: pointer;
}
.buttons button.secondary, #topbar button {
  background: #4a3c6e; color: #e8e2d0;
}
.error { color: #e06c5a; font-size: 0.9rem; margin: 0.2rem 0; }

/* Kopfleiste */
#topbar {
  display: flex; align-items: center; gap: 0.6rem;
  padding: 0.4rem 0.8rem; background: #14101e; border-bottom: 1px solid #4a3c6e;
}
#topbar button { padding: 0.4rem 0.8rem; border: 1px solid #4a3c6e; border-radius: 4px; cursor: pointer; }
#save-btn { background: #d8b94a; color: #14101e; font-weight: bold; border-color: #d8b94a; }
#save-btn:disabled { opacity: 0.6; cursor: wait; }
#player-name { font-weight: bold; color: #d8b94a; }
#save-info { font-size: 0.8rem; color: #9d92b8; }
.spacer { flex: 1; }

/* Spielbildschirm */
#screen-wrap {
  position: relative;
  min-width: 0;  /* darf unter die Canvas-Eigenbreite schrumpfen */
  min-height: 0;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000;
}

#canvas {
  display: block;
  /* Canvas füllt den verfügbaren Bereich; object-fit skaliert das Spielbild
     unter Beibehaltung des Seitenverhältnisses (Vollbreite am Handy,
     Vollhöhe am Desktop). !important schlägt Emscriptens Inline-Styles. */
  width: 100% !important;
  height: 100% !important;
  min-width: 0;  /* erzwingt nicht die Eigenbreite als Mindestbreite */
  min-height: 0;
  object-fit: contain;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #000;
}

#loading {
  position: absolute;
  font-size: 1.2rem;
  letter-spacing: 0.05em;
}

/* Steuerung – nur auf Touch-Geräten anzeigen */
#controls {
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 0.3rem;
  padding: 0.4rem 0; /* kein horizontales Padding – Tastatur füllt volle Breite */
  background: #14101e;
  border-top: 1px solid #4a3c6e;
  width: 100%;
  min-width: 0;
  max-width: 100%;
  overflow: hidden;
}

@media (hover: none) and (pointer: coarse) {
  #controls {
    display: flex;
  }
}

/* QWERTZ-Tastatur */
#keyboard {
  width: 100%;
  max-width: 100%;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.kb-row {
  display: flex;
  justify-content: center;
  gap: 2px;
  width: 100%;
  min-width: 0;
}

.cmd {
  flex: 1 1 0;
  height: 2.8rem;
  font-size: 0.68rem;
  line-height: 1.1;
  background: #1f1830;
  color: #e8e2d0;
  border: 1px solid #4a3c6e;
  border-radius: 5px;
  padding: 2px 1px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  min-width: 0;
  overflow: hidden;  /* lange Beschriftungen sprengen die Zelle nicht */
  touch-action: manipulation;
}
.cmd .k { font-weight: bold; color: #d8b94a; font-size: 0.88rem; line-height: 1.2; }
.cmd .s {
  font-size: 0.58rem; color: #9d92b8; line-height: 1;
  max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.cmd:active { background: #d8b94a; color: #14101e; }
.cmd:active .k { color: #14101e; }
.cmd:active .s { color: #14101e; }

/* breitere Sondertasten in der unteren Reihe */
.cmd.wide { flex: 2 1 0; }
.cmd.xwide { flex: 3 1 0; }

/* Steuerkreuz */
#dpad {
  display: grid;
  grid-template-columns: repeat(3, 3rem);
  grid-template-rows: repeat(3, 3rem);
  gap: 4px;
  margin-top: 0.2rem;
}
#dpad .dir[data-key="up"]    { grid-area: 1 / 2; }
#dpad .dir[data-key="left"]  { grid-area: 2 / 1; }
#dpad .dir[data-key="enter"] { grid-area: 2 / 2; }
#dpad .dir[data-key="right"] { grid-area: 2 / 3; }
#dpad .dir[data-key="down"]  { grid-area: 3 / 2; }

.dir {
  font-size: 1.2rem;
  background: #2a2140;
  color: #d8b94a;
  border: 1px solid #4a3c6e;
  border-radius: 6px;
  touch-action: manipulation;
}
.dir:active { background: #d8b94a; color: #14101e; }

/* Im Querformat Tasten etwas schmaler */
@media (max-height: 500px) {
  .cmd { height: 2.2rem; }
  #dpad { grid-template-columns: repeat(3, 2.6rem); grid-template-rows: repeat(3, 2.6rem); }
}

/* Touch-Hochformat: Spielfeld volle Breite, Tastatur füllt Rest */
@media (hover: none) and (pointer: coarse) and (orientation: portrait) {
  #app {
    grid-template-rows: auto auto 1fr;
  }
  #screen-wrap {
    height: auto;
  }
  #canvas {
    width: 100% !important;
    height: auto !important;
    object-fit: unset;
  }
  #controls {
    flex: 1 1 0;
    min-height: 0;
  }
  #keyboard {
    flex: 1 1 0;
    min-height: 0;
    justify-content: space-between;
  }
  .kb-row {
    flex: 1 1 0;
  }
  .cmd {
    height: auto;
    min-height: 0;
  }
  #dpad {
    grid-template-columns: repeat(3, 2.4rem);
    grid-template-rows: repeat(3, 2.4rem);
    margin-top: 0.1rem;
  }
}

/* Touch-Querformat: Spielfeld links, Steuerung rechts */
@media (hover: none) and (pointer: coarse) and (orientation: landscape) {
  #app {
    grid-template-rows: auto 1fr;
    grid-template-columns: 1fr auto;
  }
  #topbar {
    grid-column: 1 / -1;
  }
  #screen-wrap {
    grid-row: 2;
    grid-column: 1;
  }
  #controls {
    grid-row: 2;
    grid-column: 2;
    flex-direction: column;
    border-top: none;
    border-left: 1px solid #4a3c6e;
    width: auto;
    min-width: 0;
    padding: 0.3rem;
    gap: 0.2rem;
  }
  #canvas {
    width: auto !important;
    height: 100% !important;
  }
  #keyboard {
    flex: 1 1 0;
    min-height: 0;
    justify-content: space-between;
  }
  .kb-row {
    flex: 1 1 0;
    gap: 1px;
  }
  .cmd {
    height: auto;
    min-height: 0;
    font-size: 0.6rem;
  }
  .cmd .k {
    font-size: 0.75rem;
  }
  .cmd .s {
    font-size: 0.5rem;
  }
  #dpad {
    grid-template-columns: repeat(3, 2.2rem);
    grid-template-rows: repeat(3, 2.2rem);
    margin-top: 0.1rem;
  }
}
