:root {
  --hx-canvas: #e8e8e8;
  --hx-panel: rgba(255, 255, 255, 0.95);
  --hx-line: #ccc;
  --hx-btn: #1f6f6a;
  --hx-btn-hover: #185a56;
  --hx-btn-off: #b7c4c2;
  --hx-toast: rgba(255, 255, 255, 0.94);
}

html[data-theme="dark"] {
  --hx-canvas: #2a2a2a;
  --hx-panel: rgba(24, 39, 37, 0.96);
  --hx-line: rgba(232, 243, 241, 0.2);
  --hx-btn: #1f6f6a;
  --hx-btn-hover: #2a8a84;
  --hx-btn-off: #3a4b49;
  --hx-toast: rgba(24, 39, 37, 0.94);
}

@media (prefers-color-scheme: dark) {
  html:not([data-theme="light"]) {
    --hx-canvas: #2a2a2a;
    --hx-panel: rgba(24, 39, 37, 0.96);
    --hx-line: rgba(232, 243, 241, 0.2);
    --hx-btn-off: #3a4b49;
    --hx-toast: rgba(24, 39, 37, 0.94);
  }
}

.hx-page {
  padding-top: env(safe-area-inset-top);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

.hx-page h1.hx-title {
  flex: 1;
  min-width: 0;
  margin: 0;
  font-size: 1.15rem;
  line-height: 1.2;
  letter-spacing: 0;
  font-weight: 750;
}

.hx-page .top {
  gap: 0.6rem;
}

#game-container {
  position: relative;
  width: min(800px, calc(100% - 1rem));
  height: min(70dvh, 640px);
  min-height: 360px;
  margin: 0.75rem auto 0;
  background: var(--hx-canvas);
  border-radius: 12px;
  overflow: hidden;
  transition: background 0.15s ease;
}

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

#ui-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

#hud {
  position: absolute;
  top: calc(8px + env(safe-area-inset-top));
  left: calc(8px + env(safe-area-inset-left));
  right: calc(8px + env(safe-area-inset-right));
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
  gap: 8px;
  z-index: 5;
  pointer-events: none;
}

#scenario-title {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  background: var(--hx-panel);
  color: inherit;
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid var(--hx-line);
  font-weight: 650;
  pointer-events: auto;
}

#controls {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  gap: 8px;
  pointer-events: auto;
}

#game-container button {
  min-width: 44px;
  min-height: 44px;
  padding: 8px 12px;
  background: var(--hx-btn);
  color: #fff;
  border: none;
  border-radius: 8px;
  font: inherit;
  font-size: 14px;
  font-weight: 650;
  cursor: pointer;
  touch-action: manipulation;
  transition: background 0.15s ease;
}

#game-container button:hover:not(:disabled) {
  background: var(--hx-btn-hover);
}

#game-container button:disabled {
  background: var(--hx-btn-off);
  cursor: not-allowed;
  opacity: 0.7;
}

#unit-panel {
  position: absolute;
  left: calc(8px + env(safe-area-inset-left));
  right: calc(8px + env(safe-area-inset-right));
  bottom: calc(8px + env(safe-area-inset-bottom));
  z-index: 4;
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
  gap: 8px;
  background: var(--hx-panel);
  padding: 8px;
  border-radius: 8px;
  border: 1px solid var(--hx-line);
  pointer-events: auto;
}

#unit-panel[hidden] {
  display: none;
}

#unit-info {
  flex: 1 1 auto;
  min-width: 0;
  font-size: 13px;
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#unit-actions {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
}

#unit-actions::-webkit-scrollbar {
  display: none;
}

#message {
  position: absolute;
  left: 50%;
  bottom: calc(64px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--hx-toast);
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--hx-line);
  font-weight: 650;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s ease;
}

#message.show {
  opacity: 1;
}

@media (max-width: 720px) {
  .hx-page .brand {
    font-size: 0;
  }

  .hx-page .brand img {
    width: 36px;
    height: 36px;
  }

  #game-container {
    width: 100%;
    height: calc(100dvh - 4.6rem - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    min-height: 320px;
    margin: 0;
    border-radius: 0;
  }

  #scenario-title {
    font-size: 13px;
    padding: 0 10px;
    min-height: 44px;
    display: flex;
    align-items: center;
  }
}

@media (max-width: 640px) {
  #unit-panel {
    flex-direction: column;
    align-items: stretch;
  }

  #unit-info {
    flex: none;
    white-space: normal;
    overflow: visible;
    text-overflow: clip;
  }

  #unit-actions {
    flex-wrap: wrap;
    overflow: visible;
    width: 100%;
  }

  #unit-actions button {
    flex: 1 1 calc(33.333% - 6px);
    max-width: 100%;
    padding-left: 8px;
    padding-right: 8px;
    white-space: nowrap;
  }

  #unit-panel:not([hidden]) ~ #message {
    bottom: calc(168px + env(safe-area-inset-bottom));
  }
}
