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

:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --arena-w: 390px;
  --side-min: 200px;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: -apple-system, 'Segoe UI', system-ui, sans-serif;
  background: #0d1117;
  color: #e6edf3;
  min-height: 100vh;
  padding-bottom: var(--safe-bottom);
}

header {
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
  padding: calc(10px + var(--safe-top)) 12px 10px;
  border-bottom: 2px solid #f1c40f;
}

header h1 {
  font-family: Georgia, 'Times New Roman', serif;
  color: #f1c40f;
  font-size: 1.15rem;
}

header p {
  color: #8b949e;
  font-size: 0.75rem;
  margin-top: 2px;
}

/* Battle left, stats right — always side by side */
.layout {
  display: grid;
  grid-template-columns: minmax(150px, 42%) minmax(180px, 1fr);
  gap: 10px;
  padding: 8px;
  width: 100%;
  max-width: 100%;
  min-height: calc(100vh - 56px);
  align-items: start;
}

.game-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
}

.canvas-wrap {
  position: relative;
  width: 100%;
}

#game {
  width: 100%;
  height: auto;
  border: 3px solid #f1c40f;
  border-radius: 12px;
  display: block;
  background: #c9b896;
  touch-action: none;
}

#game-error {
  position: absolute;
  inset: 0;
  background: rgba(192, 57, 43, 0.92);
  color: #fff;
  padding: 12px;
  font-family: monospace;
  font-size: 0.8rem;
  overflow: auto;
  border-radius: 12px;
  white-space: pre-wrap;
}

.controls {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.controls button {
  flex: 1;
  min-width: 70px;
  background: #21262d;
  color: #e6edf3;
  border: 1px solid #30363d;
  padding: 8px 6px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.78rem;
  touch-action: manipulation;
}

.controls button:hover {
  background: #30363d;
  border-color: #f1c40f;
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
  max-height: calc(100vh - 64px);
  overflow: hidden;
}

.stats-panel,
#hud {
  background: #161b22;
  border: 1px solid #30363d;
  border-radius: 8px;
  padding: 10px;
  flex-shrink: 0;
}

.hud-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
  margin-bottom: 10px;
}

.hud-grid .label {
  display: block;
  font-size: 0.62rem;
  color: #8b949e;
  text-transform: uppercase;
}

.hud-grid .value {
  font-weight: 600;
  color: #f1c40f;
  font-size: 0.8rem;
}

.party-status {
  display: flex;
  flex-direction: column;
  gap: 5px;
  max-height: 200px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.party-card {
  background: #0d1117;
  border: 1px solid #30363d;
  border-radius: 6px;
  padding: 6px;
  font-size: 0.7rem;
}

.party-card.dead { opacity: 0.4; }

.party-card .hp-bar {
  height: 5px;
  background: #30363d;
  border-radius: 3px;
  margin: 3px 0;
  overflow: hidden;
}

.party-card .hp-bar div {
  height: 100%;
  background: #2ecc71;
  transition: width 0.2s;
}

.panel {
  background: #161b22;
  border: 1px solid #30363d;
  border-radius: 8px;
  padding: 10px;
  flex-shrink: 0;
}

.panel-log {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.panel h2 {
  font-size: 0.82rem;
  color: #f1c40f;
  margin-bottom: 6px;
  font-family: Georgia, serif;
}

.song-buff {
  background: #1a2332;
  border: 1px solid #f1c40f44;
  border-radius: 6px;
  padding: 6px;
  font-size: 0.68rem;
  margin-bottom: 6px;
  line-height: 1.4;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.62rem;
}

th, td {
  border: 1px solid #30363d;
  padding: 3px 4px;
  text-align: left;
}

th {
  background: #21262d;
  color: #8b949e;
}

tr.in-party {
  background: rgba(241, 196, 15, 0.08);
}

tr.in-party td:first-child {
  color: #f1c40f;
}

#class-table {
  max-height: 180px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

#combat-log {
  flex: 1 1 auto;
  min-height: 80px;
  max-height: none;
  overflow-y: auto;
  font-family: 'Consolas', 'Monaco', monospace;
  font-size: 0.66rem;
  line-height: 1.35;
  -webkit-overflow-scrolling: touch;
}

#combat-log div {
  padding: 2px 0;
  border-bottom: 1px solid #21262d;
}

#combat-log .t { color: #8b949e; }

.log-damage { color: #e74c3c; }
.log-heal { color: #2ecc71; }
.log-buff, .log-special { color: #f1c40f; }
.log-effect { color: #9b59b6; }
.log-summon { color: #bdc3c7; }
.log-shield { color: #3498db; }
.log-death { color: #c0392b; font-weight: bold; }
.log-warn { color: #e67e22; }
.log-info { color: #8b949e; }

.setup-screen {
  background: #161b22;
  border: 2px solid #f1c40f;
  border-radius: 8px;
  padding: 12px;
}

.setup-screen h2 {
  color: #f1c40f;
  font-family: Georgia, serif;
  margin-bottom: 10px;
  font-size: 1rem;
}

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

.setup-section h3 {
  font-size: 0.82rem;
  color: #e6edf3;
  margin-bottom: 4px;
}

.setup-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.setup-header h3 span {
  color: #f1c40f;
  font-weight: normal;
  font-size: 0.78rem;
}

.setup-actions {
  display: flex;
  gap: 6px;
}

.hint {
  font-size: 0.7rem;
  color: #8b949e;
  margin-bottom: 6px;
}

.class-picker {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
}

.class-pick {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  background: #0d1117;
  border: 2px solid #30363d;
  border-radius: 8px;
  padding: 8px;
  cursor: pointer;
  color: #e6edf3;
  text-align: left;
  touch-action: manipulation;
}

.class-pick:hover:not(:disabled) {
  border-color: #8b949e;
}

.class-pick.selected {
  border-color: #f1c40f;
  background: rgba(241, 196, 15, 0.1);
}

.class-pick:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.class-pick .name {
  font-weight: 600;
  font-size: 0.78rem;
}

.class-pick .stats {
  font-size: 0.65rem;
  color: #8b949e;
}

.class-pick .pick-icon {
  font-size: 1.2rem;
  line-height: 1;
}

.party-roster {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin-top: 8px;
  min-height: 24px;
}

.party-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: #0d1117;
  border: 2px solid #30363d;
  border-radius: 14px;
  padding: 3px 5px 3px 8px;
  font-size: 0.7rem;
}

.party-chip button {
  background: transparent;
  border: none;
  color: #e74c3c;
  font-size: 1rem;
  cursor: pointer;
  line-height: 1;
  padding: 0 2px;
}

.scaling-inputs {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
  margin-bottom: 8px;
}

.scaling-inputs label {
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-size: 0.68rem;
  color: #8b949e;
}

.scaling-inputs input {
  background: #0d1117;
  border: 1px solid #30363d;
  border-radius: 4px;
  color: #f1c40f;
  padding: 8px;
  font-size: 1rem;
  width: 100%;
}

.scaling-preview {
  background: #0d1117;
  border: 1px solid #30363d;
  border-radius: 6px;
  padding: 8px;
  font-size: 0.68rem;
  line-height: 1.5;
  color: #8b949e;
}

.scaling-preview strong {
  color: #e6edf3;
}

.btn-start {
  width: 100%;
  background: #f1c40f;
  color: #1a1a2e;
  border: none;
  border-radius: 8px;
  padding: 14px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
}

.btn-start:hover:not(:disabled) {
  background: #f39c12;
}

.btn-start:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* Tavern popup overlay */
.upgrade-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 2000;
  align-items: flex-start;
  justify-content: center;
  padding: calc(12px + var(--safe-top)) 12px 12px;
  background: rgba(0, 0, 0, 0.82);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.upgrade-overlay.is-open {
  display: flex;
}

body.tavern-open {
  overflow: hidden;
}

.upgrade-modal {
  width: 100%;
  max-width: 420px;
  background: #161b22;
  border: 2px solid #2ecc71;
  border-radius: 12px;
  padding: 14px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.55);
}

.btn-continue-top {
  margin-bottom: 12px;
}

.upgrade-modal h2 {
  color: #2ecc71;
  font-family: Georgia, serif;
  margin-bottom: 6px;
  font-size: 1rem;
}

.upgrade-xp {
  color: #f1c40f;
  font-weight: 600;
  margin-bottom: 4px;
  font-size: 0.85rem;
}

.upgrade-roster {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 10px 0;
  max-height: 40vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.upgrade-card {
  background: #0d1117;
  border: 1px solid #30363d;
  border-radius: 8px;
  padding: 8px;
}

.upgrade-head {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-bottom: 6px;
}

.upgrade-head span {
  font-weight: 600;
  font-size: 0.85rem;
}

.upgrade-head small {
  color: #8b949e;
  font-size: 0.68rem;
}

.upgrade-btns {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}

.upgrade-btns button {
  background: #21262d;
  border: 1px solid #30363d;
  color: #e6edf3;
  border-radius: 4px;
  padding: 8px 6px;
  font-size: 0.7rem;
  cursor: pointer;
  touch-action: manipulation;
}

.upgrade-btns button:hover:not(:disabled) {
  border-color: #2ecc71;
}

.upgrade-btns button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.recruit-panel {
  border-top: 1px solid #30363d;
  padding-top: 10px;
  margin-top: 6px;
}

.recruit-panel h3 {
  color: #9b59b6;
  font-size: 0.85rem;
  margin-bottom: 4px;
}

.recruit-count {
  color: #8b949e;
  font-weight: normal;
  font-size: 0.75rem;
}

.recruit-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin-top: 6px;
}

.recruit-pick {
  background: #21262d;
  border: 1px solid #9b59b6;
  color: #e6edf3;
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 0.68rem;
  cursor: pointer;
  touch-action: manipulation;
}

.recruit-pick:hover:not(:disabled) {
  background: #2d1f3d;
  border-color: #c39bd3;
}

.recruit-pick:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.btn-small {
  background: #21262d;
  color: #e6edf3;
  border: 1px solid #30363d;
  border-radius: 4px;
  padding: 6px 10px;
  font-size: 0.72rem;
  cursor: pointer;
  touch-action: manipulation;
}

.btn-small:hover {
  border-color: #f1c40f;
}

.setup-hud-msg {
  color: #8b949e;
  font-size: 0.82rem;
  padding: 6px 0;
}

.kill-board {
  background: #0d1117;
  border: 1px solid #30363d;
  border-radius: 8px;
  padding: 8px 10px;
  margin: 8px 0;
  font-size: 0.72rem;
}

.kill-totals {
  display: flex;
  justify-content: space-between;
  margin-bottom: 6px;
  color: #8b949e;
}

.kill-totals strong {
  color: #f1c40f;
}

.kill-heroes {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.kill-row {
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

.kill-nums {
  color: #2ecc71;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.kill-legend {
  display: block;
  margin-top: 4px;
  color: #6e7681;
  font-size: 0.62rem;
}

/* Wider screens: more room for stats column */
@media (min-width: 700px) {
  .layout {
    grid-template-columns: minmax(280px, 390px) 1fr;
  }

  .hud-grid {
    grid-template-columns: repeat(3, 1fr);
  }

  .party-status {
    max-height: 280px;
  }

  #class-table {
    max-height: 240px;
  }
}