:root {
  --primary-cyan: #00dddd;
  --primary-orange: #ff8000;
  --bg-color: #eeeeee;
  --text-color: #444444;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: var(--bg-color);
  font-family: Raleway, "Source Sans Pro", "Open Sans", -apple-system, sans-serif;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

/* Hauptlayout-Container für flexiblen Viewport */
.app-container {
  display: flex;
  flex-direction: column;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

/* Spielfeldbereich */
.game-area {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  min-width: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

#sc {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

/* Toolbar & Buttons */
.toolbar {
  position: absolute;
  top: 15px;
  left: 15px;
  z-index: 5;
  display: flex;
  gap: 10px;
}

.t-btn {
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid #cccccc;
  padding: 8px 14px;
  font-size: 14px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  color: #444444;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.06);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: all 0.15s ease;
}

.t-btn:hover {
  background: #ffffff;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.1);
}

.t-btn:active {
  transform: scale(0.97);
}

.t-btn.ai {
  border-color: var(--primary-orange);
  color: var(--primary-orange);
}

.t-btn.info {
  width: 34px;
  height: 34px;
  padding: 0;
  border-color: var(--primary-cyan);
  border-radius: 50%;
  color: #168d8d;
  font-family: Georgia, serif;
  font-size: 19px;
  line-height: 1;
}

.desktop-hint {
  display: inline;
}

/* Win Overlay */
#win {
  width: 100%;
  top: 35%;
  touch-action: none;
  text-align: center;
  z-index: 10;
  position: absolute;
  background-color: rgba(255, 255, 255, 0.95);
  color: #444444;
  padding: 24px 16px;
  font-size: 32px;
  font-weight: bold;
  display: none;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(4px);
  box-sizing: border-box;
}

#win a {
  color: #4444aa;
  font-size: 20px;
  text-decoration: none;
  display: inline-block;
  margin-top: 12px;
  padding: 8px 18px;
  border: 2px solid #4444aa;
  border-radius: 8px;
  font-weight: 600;
  transition: all 0.2s ease;
}

#win a:hover {
  background-color: #4444aa;
  color: #ffffff;
}

/* Spielanleitung */
#game-instructions {
  width: min(540px, calc(100vw - 32px));
  max-height: min(78dvh, 700px);
  margin: auto;
  padding: 0;
  overflow: hidden;
  border: 0;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.96);
  color: var(--text-color);
  box-shadow: 0 18px 48px rgba(31, 51, 51, 0.24);
}

#game-instructions[open] {
  display: flex;
  flex-direction: column;
}

#game-instructions::backdrop {
  background: rgba(222, 229, 229, 0.58);
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
}

.instructions-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 20px 20px 16px 24px;
  border-bottom: 1px solid #d9e5e5;
}

.instructions-header h3 {
  margin: 0;
  color: #286363;
  font-size: 21px;
  line-height: 1.25;
}

.instructions-close {
  flex: 0 0 34px;
  width: 34px;
  height: 34px;
  padding: 0;
  border: 1px solid #d0dcdc;
  border-radius: 50%;
  background: #ffffff;
  color: #536565;
  cursor: pointer;
  font-size: 25px;
  line-height: 1;
  transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease, transform 0.15s ease;
}

.instructions-close:hover {
  border-color: var(--primary-orange);
  background: #fff7ed;
  color: var(--primary-orange);
}

.instructions-close:active {
  transform: scale(0.94);
}

.instructions-close:focus-visible,
.t-btn.info:focus-visible {
  outline: 2px solid var(--primary-cyan);
  outline-offset: 2px;
}

.instructions-content {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 4px 24px 28px;
  font-size: 15px;
  line-height: 1.55;
}

.instructions-content section {
  padding: 18px 0;
  border-bottom: 1px solid #e5eded;
}

.instructions-content section:last-child {
  padding-bottom: 0;
  border-bottom: 0;
}

.instructions-content h4 {
  margin: 0 0 7px;
  color: #286363;
  font-size: 16px;
}

.instructions-content p {
  margin: 0;
}

.instructions-content p + p,
.instructions-content ul + p {
  margin-top: 11px;
}

.instructions-content ul {
  margin: 10px 0 0;
  padding-left: 21px;
}

.instructions-content li + li {
  margin-top: 8px;
}

/* --- Mobile D-Pad: Desktop Standard = Komplett ausgeblendet --- */
.dpad-container {
  display: none;
}

/* Gemeinsame D-Pad Button Stile */
.dpad-btn {
  background: #ffffff;
  border: none;
  border-radius: 10px;
  color: #444444;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  box-shadow: 0 3px 0 #b5b5b5, 0 4px 6px rgba(0, 0, 0, 0.08);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  outline: none;
  transition: transform 0.05s ease, box-shadow 0.05s ease, background-color 0.08s ease, color 0.08s ease;
}

.dpad-btn svg {
  pointer-events: none;
}

.dpad-btn:active, .dpad-btn.active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 #b5b5b5, 0 2px 3px rgba(0, 0, 0, 0.1);
  background-color: var(--primary-cyan);
  color: #ffffff;
}

.dpad-up    { grid-column: 2; grid-row: 1; border-radius: 12px 12px 5px 5px; }
.dpad-left  { grid-column: 1; grid-row: 2; border-radius: 12px 5px 5px 12px; }

/* D-Pad Zentrum: Oranger KI Button */
.dpad-center {
  grid-column: 2;
  grid-row: 2;
  background: #ffffff;
  border-radius: 6px;
  color: var(--primary-orange);
  font-weight: 800;
  font-size: 15px;
  letter-spacing: 0.5px;
}

.dpad-center:active, .dpad-center.active {
  background-color: var(--primary-orange);
  color: #ffffff;
}

.dpad-right { grid-column: 3; grid-row: 2; border-radius: 5px 12px 12px 5px; }
.dpad-down  { grid-column: 2; grid-row: 3; border-radius: 5px 5px 12px 12px; }

/* Kompakte Toolbar auf kleineren Bildschirmen */
@media (max-width: 768px), (max-height: 550px) {
  .toolbar {
    top: 10px;
    left: 10px;
    gap: 6px;
  }
  .t-btn {
    padding: 6px 11px;
    font-size: 12px;
    border-radius: 6px;
  }
  .t-btn.info {
    width: 30px;
    height: 30px;
    padding: 0;
    border-radius: 50%;
    font-size: 17px;
  }
  .desktop-hint {
    display: none; /* Blendet "(Space)" auf Mobilgeräten aus */
  }
  /* KI-Zug-Button in der mobilen Ansicht ausblenden */
  .t-btn.ai {
    display: none;
  }
  #game-instructions {
    width: 100%;
    max-height: min(82dvh, 640px);
    margin: 0;
    position: fixed;
    inset: auto 0 0;
    border-radius: 8px 8px 0 0;
  }
  .instructions-header {
    padding: 17px 16px 14px 20px;
  }
  .instructions-header h3 {
    font-size: 19px;
  }
  .instructions-content {
    padding: 2px 20px max(22px, env(safe-area-inset-bottom, 22px));
    font-size: 14px;
  }
  .instructions-content section {
    padding: 15px 0;
  }
}

/* ==========================================================================
   1. MOBILE PORTRAIT (Vertikal)
   ========================================================================== */
@media (max-width: 768px) and (orientation: portrait), (pointer: coarse) and (orientation: portrait) {
  .app-container {
    flex-direction: column;
  }

  .game-area {
    flex: 1 1 auto;
    width: 100%;
    height: auto;
  }

  .dpad-container {
    display: flex;
    justify-content: center;
    align-items: center;
    flex: 0 0 auto;
    height: 22dvh;
    min-height: 160px;
    max-height: 200px;
    width: 100%;
    background: linear-gradient(180deg, #eaeaea 0%, #dbdbdb 100%);
    box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.08);
    padding: 6px 12px;
    padding-bottom: max(6px, env(safe-area-inset-bottom, 6px));
    box-sizing: border-box;
    z-index: 20;
  }

  .dpad-grid {
    display: grid;
    grid-template-columns: repeat(3, 48px);
    grid-template-rows: repeat(3, 48px);
    gap: 6px;
    background: #cecece;
    padding: 6px;
    border-radius: 20px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
  }
}

/* ==========================================================================
   2. MOBILE LANDSCAPE (Horizontal / Querformat)
   ========================================================================== */
@media (pointer: coarse) and (orientation: landscape), (max-height: 550px) and (orientation: landscape) {
  .app-container {
    flex-direction: row; /* Nebeneinander statt übereinander */
  }

  .game-area {
    flex: 1 1 auto;
    height: 100dvh;
    width: auto;
  }

  .dpad-container {
    display: flex;
    justify-content: center;
    align-items: center;
    flex: 0 0 170px;
    width: 170px;
    height: 100dvh;
    background: linear-gradient(90deg, #eaeaea 0%, #dbdbdb 100%);
    box-shadow: inset 2px 0 6px rgba(0, 0, 0, 0.08);
    padding: 10px;
    padding-right: max(10px, env(safe-area-inset-right, 10px));
    box-sizing: border-box;
    z-index: 20;
  }

  .dpad-grid {
    display: grid;
    grid-template-columns: repeat(3, 44px);
    grid-template-rows: repeat(3, 44px);
    gap: 5px;
    background: #cecece;
    padding: 6px;
    border-radius: 18px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
  }
}