* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #16110c;
  font-family: Georgia, 'Palatino Linotype', 'Book Antiqua', serif;
  color: #e8d9b5;
  /* keeps a strip clear along the top so the back link never sits over the board */
  padding-top: 44px;
  /* a click on the board is a game move, not a text drag — without this the
     browser paints a blue selection over the labels as you play */
  -webkit-user-select: none;
  user-select: none;
}

/* Back to the landing page. Same corner and size as Gravity Golf's. */
.back {
  position: fixed;
  top: 16px;
  left: 16px;
  z-index: 20;
  font-size: 12px;
  font-weight: bold;
  line-height: 1;
  letter-spacing: 1px;
  padding: 7px 11px;
  border: 1px solid #1c1712;
  border-radius: 3px;
  background: #2a1f16;
  box-shadow: inset 0 0 0 1px #4a3524;
  color: #b89a6b;
  text-decoration: none;
}

.back:hover, .back:focus-visible {
  color: #d4af37;
  outline: 2px solid #d4af37;
  outline-offset: 1px;
}

#app {
  display: flex;
  gap: 16px;
  padding: 16px;
}

#game-area {
  position: relative;
}

#canvas {
  background: #3f6b2f;
  border: 6px solid #3a2a1a;
  border-radius: 2px;
  box-shadow: 0 0 0 2px #6b4a2e, inset 0 0 0 2px #1c1712;
  display: block;
  cursor: pointer;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

#overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  background: rgba(10, 6, 2, 0.75);
  border-radius: 2px;
}

#overlay.hidden {
  display: none;
}

#overlay-message {
  font-size: 32px;
  font-weight: bold;
  color: #d4af37;
  letter-spacing: 2px;
  text-shadow: 2px 2px 0 #1c1712;
}

#restart-btn, #start-wave-btn, .tower-btn {
  cursor: pointer;
  border: 2px solid #1c1712;
  border-radius: 3px;
  font-size: 14px;
  padding: 10px 14px;
  background: #7a4a25;
  color: #f4e4c1;
  font-family: inherit;
}

#restart-btn:hover, #start-wave-btn:hover:not(:disabled), .tower-btn:hover:not(:disabled) {
  filter: brightness(1.2);
}

/* 44px tap targets: Start Wave rendered 42px tall, the game-over Restart
   less. */
#restart-btn, #start-wave-btn { min-height: 44px; }

#start-wave-btn {
  width: 100%;
  margin: 10px 0 4px;
  font-size: 16px;
  padding: 10px;
  letter-spacing: 1px;
}

#start-wave-btn:disabled {
  background: #4a4038;
  color: #a89a86;
  cursor: default;
}

#sidebar {
  width: 240px;
  display: flex;
  flex-direction: column;
  background: #2a1f16;
  border: 3px solid #1c1712;
  border-radius: 2px;
  padding: 14px;
  box-shadow: inset 0 0 0 2px #4a3524;
}

#sidebar h1 {
  font-size: 19px;
  margin: 0 0 12px;
  color: #d4af37;
  letter-spacing: 1px;
  text-align: center;
}

#sidebar h2 {
  font-size: 14px;
  margin: 16px 0 8px;
  color: #b89a6b;
  letter-spacing: 1px;
  text-transform: uppercase;
  border-bottom: 1px solid #4a3524;
  padding-bottom: 4px;
}

.stat-row {
  display: flex;
  justify-content: space-between;
  padding: 4px 0;
  font-size: 15px;
}

.stat-label {
  color: #b89a6b;
}

.stat-value {
  font-weight: bold;
  color: #f4e4c1;
}

.stat-value.boss-wave {
  color: #ff4d4d;
  text-shadow: 0 0 6px rgba(255, 77, 77, 0.6);
}

#tower-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.tower-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  background: #3a2a1e;
  border: 2px solid #1c1712;
  text-align: left;
}

.tower-btn.selected {
  border-color: #d4af37;
  box-shadow: 0 0 0 1px #d4af37;
}

.tower-btn:disabled {
  opacity: 0.5;
  cursor: default;
}

.tower-swatch {
  width: 18px;
  height: 18px;
  border-radius: 3px;
  border: 1px solid #1c1712;
  flex-shrink: 0;
}

.tower-info {
  display: flex;
  flex-direction: column;
}

.tower-name {
  font-weight: bold;
  font-size: 13px;
}

.tower-cost {
  font-size: 12px;
  color: #d4af37;
}

#hint {
  margin-top: 16px;
  font-size: 12px;
  color: #a89a86;
  line-height: 1.4;
  font-style: italic;
}

#upgrade-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: #221912;
  border: 2px solid #1c1712;
  border-radius: 3px;
  padding: 10px;
  min-height: 20px;
}

.muted {
  margin: 0;
  font-size: 12px;
  color: #a89a86;
  font-style: italic;
}

.upgrade-tower-name {
  font-weight: bold;
  font-size: 13px;
  color: #f4e4c1;
}

.upgrade-cell {
  font-weight: normal;
  font-size: 11px;
  color: #a89a86;
}

.upgrade-stats-line {
  font-size: 11px;
  color: #b89a6b;
  padding-bottom: 4px;
  border-bottom: 1px solid #4a3524;
}

.upgrade-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.upgrade-row-top {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
}

.upgrade-dots {
  color: #d4af37;
  letter-spacing: 2px;
}

.upgrade-btn {
  cursor: pointer;
  border: 1px solid #1c1712;
  border-radius: 3px;
  font-size: 11px;
  padding: 6px 8px;
  background: #4a3524;
  color: #f4e4c1;
  font-family: inherit;
}

.upgrade-btn:hover:not(:disabled) {
  filter: brightness(1.25);
}

.upgrade-btn:disabled {
  opacity: 0.45;
  cursor: default;
}

/* Phones and tablets. A tap is already a click, so only the layout changes:
   the board scales to fit, and double-tap zoom is turned off so quick taps
   on the board and buttons are never swallowed. */
body { touch-action: manipulation; }
#canvas { max-width: 100%; height: auto; }

/* Narrower than the side-by-side layout: board on top, sidebar below. */
@media (max-width: 1060px) {
  body { align-items: flex-start; }
  #app { flex-direction: column; width: 100%; max-width: 762px; padding: 8px; gap: 10px; }
  #canvas { width: 100%; }
  #sidebar { width: 100%; }
  #tower-list { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* A phone on its side: board as tall as the screen, sidebar beside it and
   scrolling on its own, back link moved to the sidebar's top corner. */
@media (orientation: landscape) and (max-height: 560px) {
  body { padding: 4px; }
  .back { top: 8px; left: auto; right: 8px; padding: 6px 9px; }
  #app { flex-direction: row; max-width: none; padding: 0; gap: 6px; }
  #game-area { flex: none; }
  #canvas { width: auto; height: calc(100vh - 8px); }
  #sidebar { width: auto; flex: 1; min-width: 0; max-height: calc(100vh - 8px); overflow-y: auto; padding: 38px 10px 10px; }
  /* The title stays, small, on one line under the back link. */
  #sidebar h1 { flex: none; margin: 0 0 4px; font-size: 14px; letter-spacing: .5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  #hint { display: none; }
  #sidebar h2 { margin: 10px 0 6px; }
  .stat-row { padding: 2px 0; font-size: 14px; }
  #start-wave-btn { margin: 6px 0 2px; padding: 8px; font-size: 15px; }
  .tower-btn { padding: 6px 8px; }
  #overlay-message { font-size: 24px; }
}

/* A nudge, not a wall: on a touch screen held upright the game still plays,
   but the board is small, so a banner above it suggests turning sideways. */
#landscape-tip { display: none; }

@media (pointer: coarse) and (orientation: portrait) {
  #landscape-tip {
    display: block;
    margin: 0;
    padding: 8px 12px;
    text-align: center;
    font-size: 14px;
    font-weight: bold;
    letter-spacing: 0.5px;
    color: #1c1712;
    background: #d4af37;
    border: 2px solid #1c1712;
    border-radius: 3px;
    box-shadow: inset 0 0 0 1px #f0d77a;
  }
}

/* Testing tools: a parchment-on-wood sheet pinned to the right. */
#admin-btn {
  width: 100%;
  margin: 4px 0;
  padding: 6px;
  cursor: pointer;
  font-family: inherit;
  font-size: 13px;
  border: 2px solid #1c1712;
  border-radius: 3px;
  background: #3a2a1a;
  color: #d4af37;
}
#admin-btn[hidden] { display: none; }

#admin {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 50;
  width: min(300px, 70vw);
  overflow-y: auto;
  padding: 8px 10px 16px;
  background: #1c1712f2;
  border-left: 2px solid #4a3524;
  font-size: 12px;
  -webkit-user-select: text;
  user-select: text;
}
#admin[hidden] { display: none; }
#admin header { display: flex; justify-content: space-between; align-items: center; color: #d4af37; letter-spacing: 2px; text-transform: uppercase; }
#admin header span { display: flex; gap: 4px; letter-spacing: 0; text-transform: none; }
#admin section { margin-top: 10px; }
#admin h3 { margin: 0 0 4px; font-size: 11px; letter-spacing: 1.5px; text-transform: uppercase; color: #b89a6b; }
#admin .btns { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 4px; }
#admin button, #admin input {
  font: inherit;
  font-size: 12px;
  padding: 4px 7px;
  border: 1px solid #4a3524;
  border-radius: 3px;
  background: #2a1f16;
  color: #e8d9b5;
}
#admin button { cursor: pointer; }
#admin button:hover { border-color: #d4af37; }
#admin button.on { border-color: #d4af37; background: #4a3a14; color: #ffd23f; }
#admin input { width: 64px; }
#admin pre { margin: 0; font-family: ui-monospace, Consolas, monospace; font-size: 10.5px; line-height: 1.45; white-space: pre-wrap; word-break: break-word; color: #b89a6b; }

/* With a mouse there is usually room: the game moves over rather than under. */
@media (pointer: fine) {
  body:has(#admin:not([hidden])) { padding-right: 300px; }
}
