:root {
  color-scheme: dark;
  --bg: #101712;
  --panel: #17221a;
  --panel-2: #202b22;
  --line: #344334;
  --text: #edf4e7;
  --muted: #aab7a2;
  --accent: #f6d365;
  --danger: #f2777a;
  --good: #8bdca5;
  --tile: min(6.6vw, 28px);
}

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

html,
body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  overscroll-behavior: none;
  overflow: hidden;
}

button {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #223024;
  color: var(--text);
  min-height: 44px;
  font: inherit;
  cursor: pointer;
  touch-action: manipulation;
}

select {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #223024;
  color: var(--text);
  padding: 0 10px;
  font: inherit;
}

button:active {
  transform: translateY(1px);
}

.hidden {
  display: none !important;
}

.setup-screen {
  min-height: 100svh;
  display: grid;
  place-items: center;
  padding: max(10px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  overflow-y: auto;
}

.setup-panel {
  width: min(100%, 440px);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: 14px;
}

.setup-head {
  margin-bottom: 12px;
}

.setup-head p,
.setup-grid span,
.setup-summary p {
  color: var(--muted);
  font-size: 0.78rem;
}

.setup-grid {
  display: grid;
  gap: 9px;
}

.setup-grid label {
  display: grid;
  gap: 5px;
}

.setup-summary {
  margin: 12px 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-2);
  padding: 10px;
}

.setup-summary h2 {
  color: var(--text);
}

.setup-summary p {
  margin-top: 4px;
}

.setup-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.setup-actions button:first-child {
  color: var(--good);
}

.app {
  height: 100svh;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  gap: 8px;
  padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  overflow: hidden;
}

.game-shell,
.control-shell {
  min-width: 0;
}

.game-shell {
  min-height: 0;
  display: grid;
  grid-template-rows: auto auto auto minmax(0, 1fr) auto;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.top-copy {
  min-width: 0;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: clamp(0.92rem, 4.2vw, 1.05rem);
  line-height: 1.2;
}

h2 {
  font-size: 0.9rem;
  color: var(--muted);
}

#subtitle {
  color: var(--muted);
  font-size: clamp(0.64rem, 2.9vw, 0.76rem);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.meta-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 5px;
  margin-top: 2px;
  min-width: 0;
}

.icon-button {
  width: 40px;
  min-height: 40px;
  flex: 0 0 40px;
  color: var(--accent);
  font-weight: 800;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 7px;
}

.save-status {
  color: var(--good);
  font-size: 0.66rem;
  opacity: 0.84;
  white-space: nowrap;
}

.stats {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 6px;
  margin: 7px 0;
}

.stats div {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: 5px 4px;
  text-align: center;
}

.stats span {
  display: block;
  color: var(--muted);
  font-size: 0.66rem;
}

.stats strong {
  display: block;
  margin-top: 2px;
  font-size: clamp(0.62rem, 2.5vw, 0.82rem);
  overflow-wrap: anywhere;
}

.status-effects {
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: 4px;
  min-width: 0;
  overflow: hidden;
}

.status-chip {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: 2px 6px;
  color: var(--muted);
  font-size: 0.62rem;
  line-height: 1.25;
  white-space: nowrap;
}

.status-chip.good {
  border-color: rgba(139, 220, 165, 0.45);
  color: var(--good);
}

.status-chip.warn {
  border-color: rgba(246, 211, 101, 0.5);
  color: var(--accent);
}

.status-chip.danger {
  border-color: rgba(242, 119, 122, 0.62);
  color: var(--danger);
}

.status-chip.mode {
  border-color: rgba(139, 220, 240, 0.48);
  color: #8bdcf0;
}

.equipment-slots {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 5px;
  margin: 0 0 7px;
}

.equipment-slot {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: 4px 5px;
  color: var(--text);
  font-size: 0.62rem;
  line-height: 1.2;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.equipment-slot b {
  display: block;
  color: var(--muted);
  font-size: 0.56rem;
  font-weight: 700;
}

.stage-wrap {
  width: min(100%, max(210px, calc(100svh - 320px)));
  max-height: 100%;
  margin: 0 auto;
  aspect-ratio: 1;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #050706;
  overflow: hidden;
  box-shadow: 0 18px 44px rgba(0, 0, 0, 0.28);
}

#gameCanvas {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  touch-action: none;
}

.message-log {
  min-height: 44px;
  max-height: 54px;
  margin-top: 7px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: 7px 8px;
  color: var(--accent);
  font-size: 0.8rem;
  line-height: 1.35;
  overflow: hidden;
}

.control-shell {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-areas:
    "actions"
    "spells";
  gap: 7px;
  align-items: end;
  min-height: 0;
}

.action-row {
  grid-area: actions;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 5px;
}

.spell-row {
  grid-area: spells;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 5px;
}

.spell-row button {
  min-width: 0;
  min-height: 38px;
  padding: 0 6px;
  color: #8bdcf0;
  font-size: 0.72rem;
}

.spell-row button:disabled {
  color: var(--muted);
  opacity: 0.56;
}

.action-row button {
  min-width: 0;
  padding: 0 6px;
  font-size: 0.75rem;
  min-height: 40px;
}

.inventory {
  min-height: 0;
  max-height: min(62svh, 460px);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: 6px 7px;
  overflow: hidden;
}

.message-stats {
  color: var(--good);
  font-size: 0.66rem;
  line-height: 1.1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.message-log p {
  display: grid;
  gap: 2px;
}

.message-log span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.inventory-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.inventory-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.mini-button {
  min-height: 28px;
  padding: 0 8px;
  color: var(--accent);
  font-size: 0.68rem;
}

.mini-button.active {
  border-color: var(--accent);
  background: #3a321d;
}

.action-row button.active,
#inventoryBtn.active {
  border-color: var(--accent);
  background: #3a321d;
  color: var(--accent);
}

.inventory-list {
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: 5px;
  margin-top: 5px;
  min-height: 0;
  max-height: none;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  -webkit-overflow-scrolling: touch;
}

.inventory-drawer {
  position: fixed;
  left: max(8px, env(safe-area-inset-left));
  right: max(8px, env(safe-area-inset-right));
  bottom: max(8px, env(safe-area-inset-bottom));
  z-index: 3;
}

.item-pill {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  max-width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-2);
  padding: 4px 8px;
  color: var(--text);
  font-size: 0.74rem;
  overflow-wrap: anywhere;
}

.item-pill.empty {
  color: var(--muted);
}

.item-menu {
  position: fixed;
  left: max(8px, env(safe-area-inset-left));
  right: max(8px, env(safe-area-inset-right));
  bottom: max(8px, env(safe-area-inset-bottom));
  z-index: 4;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #17221a;
  box-shadow: 0 18px 44px rgba(0, 0, 0, 0.38);
  padding: 10px;
}

.item-menu h2 {
  color: var(--text);
}

.item-menu p {
  margin-top: 4px;
  color: var(--muted);
  font-size: 0.74rem;
}

.item-actions {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 5px;
  margin-top: 9px;
}

.item-actions button {
  min-width: 0;
  min-height: 36px;
  padding: 0 4px;
  font-size: 0.68rem;
}

@media (min-width: 760px) {
  body {
    overflow: auto;
  }

  .app {
    min-height: 100svh;
    grid-template-columns: minmax(430px, 680px) minmax(250px, 330px);
    grid-template-rows: 1fr;
    justify-content: center;
    align-items: center;
    gap: 18px;
    padding: 24px;
  }

  .stage-wrap {
    width: min(100%, 620px);
  }

  .control-shell {
    grid-template-columns: 1fr;
    grid-template-areas:
      "actions"
      "spells";
    align-content: center;
  }

  .action-row {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .spell-row {
    grid-template-columns: 1fr;
  }

}

@media (max-width: 759px) {
  .app {
    gap: 5px;
    padding-top: max(5px, env(safe-area-inset-top));
    padding-bottom: max(5px, env(safe-area-inset-bottom));
  }

  .game-shell {
    grid-template-rows: auto auto auto minmax(0, 1fr) auto;
  }

  .topbar {
    gap: 6px;
  }

  h1 {
    font-size: 0.82rem;
    line-height: 1.05;
  }

  #subtitle {
    font-size: 0.58rem;
  }

  .save-status {
    font-size: 0.58rem;
  }

  .icon-button {
    width: 34px;
    min-height: 34px;
    flex-basis: 34px;
  }

  button {
    min-height: 36px;
  }

  .stats {
    gap: 4px;
    margin: 4px 0;
  }

  .stats div {
    padding: 3px 3px;
  }

  .stats span {
    font-size: 0.56rem;
  }

  .stats strong {
    margin-top: 0;
    font-size: 0.68rem;
  }

  .status-effects {
    gap: 3px;
    max-height: 20px;
  }

  .status-chip {
    padding: 1px 5px;
    font-size: 0.56rem;
  }

  .equipment-slots {
    gap: 3px;
    margin-bottom: 4px;
  }

  .equipment-slot {
    padding: 2px 3px;
    font-size: 0.54rem;
  }

  .equipment-slot b {
    font-size: 0.5rem;
  }

  .stage-wrap {
    width: min(100%, max(230px, calc(100svh - 250px)));
  }

  .message-log {
    min-height: 34px;
    max-height: 38px;
    margin-top: 4px;
    padding: 4px 6px;
    font-size: 0.68rem;
    line-height: 1.25;
  }

  .message-stats {
    font-size: 0.58rem;
  }

  .control-shell {
    gap: 4px;
  }

  .action-row,
  .spell-row {
    gap: 4px;
  }

  .action-row button {
    min-height: 34px;
    padding: 0 4px;
    font-size: 0.68rem;
  }

  .spell-row button {
    min-height: 32px;
    padding: 0 4px;
    font-size: 0.66rem;
  }

  .mini-button {
    min-height: 24px;
    padding: 0 6px;
    font-size: 0.62rem;
  }

  .item-pill {
    min-height: 24px;
    padding: 3px 6px;
    font-size: 0.68rem;
  }

  .item-actions button {
    min-height: 32px;
    font-size: 0.64rem;
  }

  .inventory {
    min-height: min(300px, 48svh);
    max-height: min(70svh, 520px);
    padding: 8px;
  }

  .inventory-head {
    align-items: flex-start;
    min-height: 28px;
  }

  .inventory-actions {
    margin-left: auto;
    justify-content: flex-end;
  }

  .inventory-list {
    gap: 4px;
    margin-top: 3px;
    padding-bottom: max(4px, env(safe-area-inset-bottom));
  }
}

@media (max-width: 370px) {
  .app {
    gap: 8px;
    padding-left: 6px;
    padding-right: 6px;
  }

  .stats {
    gap: 5px;
  }

  .stats div {
    padding-left: 3px;
    padding-right: 3px;
  }

  .status-chip {
    padding-left: 4px;
    padding-right: 4px;
    font-size: 0.58rem;
  }

  .equipment-slots {
    gap: 4px;
  }

  .equipment-slot {
    padding-left: 3px;
    padding-right: 3px;
    font-size: 0.56rem;
  }

  .equipment-slot b {
    font-size: 0.52rem;
  }

  .action-row button {
    min-height: 32px;
    font-size: 0.64rem;
    padding: 0 3px;
  }

  .item-actions {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .spell-row button {
    min-height: 30px;
    font-size: 0.62rem;
    padding: 0 3px;
  }
}

@media (max-height: 700px) and (max-width: 759px) {
  .app {
    gap: 6px;
  }

  .topbar {
    gap: 8px;
  }

  .stats {
    margin: 5px 0;
  }

  .stats div {
    padding-top: 4px;
    padding-bottom: 4px;
  }

  .stage-wrap {
    width: min(100%, max(220px, calc(100svh - 226px)));
  }

  .equipment-slots {
    margin-bottom: 5px;
  }

  .status-chip {
    padding-top: 2px;
    padding-bottom: 2px;
    font-size: 0.58rem;
  }

  .message-log {
    min-height: 34px;
    max-height: 40px;
    font-size: 0.64rem;
    padding-top: 3px;
    padding-bottom: 3px;
  }

  .inventory {
    min-height: min(260px, 46svh);
    max-height: min(70svh, 460px);
  }

  h2 {
    display: none;
  }

  .inventory-list {
    margin-top: 0;
  }
}
