:root {
  --brand: #b3281c;
  --brand-hover: #8e1e14;
  --brand-muted: rgba(179, 40, 28, 0.8);
  --ink: #222;
  --paper: #fdfbf2;
  --grass: #7aa84a;
  --shadow: 0 2px 6px rgba(0,0,0,0.2);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: var(--paper);
  overflow: hidden;
}

/* --- screen visibility --- */
.screen { display: none; }
body[data-screen="intro"]         .screen[data-name="intro"],
body[data-screen="instructions1"] .screen[data-name="instructions1"],
body[data-screen="instructions2"] .screen[data-name="instructions2"],
body[data-screen="players"]       .screen[data-name="players"],
body[data-screen="game"]          .screen[data-name="game"] {
  display: flex;
}

.screen {
  position: fixed;
  inset: 0;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: clamp(1rem, 4vw, 3rem);
  gap: 1.25rem;
  background: var(--paper);
}

/* --- intro / instruction text --- */
h1 {
  font-size: clamp(2rem, 6vw, 3.5rem);
  margin: 0;
  color: var(--brand);
  letter-spacing: -0.01em;
}
h2 { font-size: clamp(1.5rem, 4vw, 2.25rem); margin: 0; color: var(--brand); }
h3 { margin: 0.8em 0 0.2em; color: var(--brand); font-size: 1.1rem; }
.subtitle { margin: 0; font-size: 1.1rem; color: #555; }
.instructions {
  max-width: 42rem;
  line-height: 1.5;
  font-size: clamp(0.95rem, 2vw, 1.05rem);
  padding: 1rem 1.25rem;
  background: #fff;
  border: 1px solid #e6e1d3;
  border-radius: 8px;
  box-shadow: var(--shadow);
  overflow-y: auto;
  max-height: 60vh;
}
.instructions p { margin: 0.4em 0; }

.menu {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
  justify-content: center;
}

.credit {
  position: absolute;
  bottom: 1rem;
  font-size: 0.85rem;
  color: #666;
}
.credit a { color: var(--brand); text-decoration: none; }
.credit a:hover { text-decoration: underline; }

/* --- buttons --- */
button {
  appearance: none;
  border: none;
  border-radius: 6px;
  padding: 0.7em 1.2em;
  font-size: 1rem;
  font-weight: 600;
  min-height: 44px;
  background: var(--brand);
  color: #fff;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: background 0.1s, transform 0.05s;
}
button:hover  { background: var(--brand-hover); }
button:active { transform: translateY(1px); }
button:disabled { background: #ccc; cursor: not-allowed; }
button.ghost {
  background: transparent;
  color: var(--brand);
  box-shadow: none;
  border: 1px solid var(--brand);
}

/* --- game screen --- */
.screen[data-name="game"] { padding: 0; }
#game-root {
  width: 100%;
  height: 100%;
  display: grid;
  gap: 2px;
  background: #000;
}
#game-root[data-players="1"] { grid-template-columns: 1fr; }
#game-root[data-players="2"] { grid-template-columns: 1fr 1fr; }
@media (max-width: 720px) and (orientation: portrait) {
  #game-root[data-players="2"] {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr 1fr;
  }
}

.player {
  position: relative;
  display: grid;
  grid-template-rows: auto 1fr auto;
  overflow: hidden;
  background: var(--grass);
}

.hud {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 1.25rem;
  padding: 0.5rem 1rem;
  background: rgba(255,255,255,0.9);
  border-bottom: 1px solid #d7d2c0;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--ink);
}
.hud > span::before {
  content: attr(data-label);
  color: #666;
  font-weight: 500;
  margin-right: 0.3em;
}

.field {
  position: relative;
  overflow: hidden;
  background-image: url("img/background.webp");
  background-size: cover;
  background-position: center;
}
.field canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

/* --- bills --- */
.bills {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: calc(100% - 1rem);
  pointer-events: none;
}
.bill {
  width: clamp(60px, 12vw, 100px);
  height: auto;
  image-rendering: auto;
  pointer-events: auto;
  cursor: grab;
  user-select: none;
  -webkit-user-drag: none;
  filter: drop-shadow(0 1px 3px rgba(0,0,0,0.3));
}
.bill.dragging { cursor: grabbing; z-index: 1000; }

/* --- history sidebar --- */
.history {
  position: absolute;
  top: 0.5rem;
  left: 0.5rem;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.3rem;
  max-height: calc(100% - 1rem);
  font-size: 0.85rem;
}
.history-toggle {
  font-size: 0.8rem;
  padding: 0.35em 0.75em;
  min-height: 32px;
  background: rgba(255,255,255,0.9);
  color: var(--brand);
  box-shadow: var(--shadow);
}
.history-list {
  display: none;
  margin: 0;
  padding: 0.5rem 0.75rem 0.5rem 1.75rem;
  list-style: decimal;
  background: rgba(255,255,255,0.92);
  border-radius: 6px;
  box-shadow: var(--shadow);
  overflow-y: auto;
  max-height: calc(100% - 3rem);
  color: var(--ink);
}
.history[aria-expanded="true"] .history-list { display: block; }
.history-list li { margin: 0.2em 0; white-space: nowrap; }
.history-list li.good { color: #1e7a34; }
.history-list li.bad  { color: #8e1e14; }

/* --- school years icons --- */
.school-icons {
  position: absolute;
  bottom: 0.5rem;
  left: 0.5rem;
  display: flex;
  gap: 2px;
  pointer-events: none;
}
.school-icons img { width: 28px; height: auto; filter: drop-shadow(0 1px 2px rgba(0,0,0,0.4)); }

/* --- controls --- */
.controls {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 4px;
  padding: 4px;
  background: #222;
}
.controls .btn-done { grid-column: 1 / -1; }
.controls button {
  font-size: 0.95rem;
  padding: 0.75em 0.5em;
  border-radius: 4px;
  box-shadow: none;
}

/* --- popups --- */
#popup-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 2000;
}
.popup-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.4);
  pointer-events: auto;
  animation: fade 0.15s ease-out;
}
.popup {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: var(--paper);
  border-radius: 10px;
  padding: 1.25rem 1.5rem 1rem;
  box-shadow: 0 10px 30px rgba(0,0,0,0.3);
  min-width: min(320px, 90vw);
  max-width: min(500px, 90vw);
  max-height: 90vh;
  overflow-y: auto;
  pointer-events: auto;
  animation: pop 0.18s ease-out;
}
.popup.anchor-left  { left: 25%; }
.popup.anchor-right { left: 75%; }

.popup h3 {
  margin: 0 0 0.5rem;
  color: var(--brand);
  font-size: 1.2rem;
}
.popup-body {
  line-height: 1.5;
  margin-bottom: 1rem;
  white-space: pre-wrap;
}
.popup-buttons {
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
  flex-wrap: wrap;
}
.popup-buttons button { flex: 1 1 auto; min-width: 100px; }

@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes pop {
  from { opacity: 0; transform: translate(-50%, -45%) scale(0.96); }
  to   { opacity: 1; transform: translate(-50%, -50%) scale(1); }
}

/* --- game over summary --- */
.summary {
  display: grid;
  grid-template-columns: auto auto;
  gap: 0.3rem 1rem;
  margin: 0.75rem 0;
  font-size: 1rem;
}
.summary dt { color: #666; font-weight: 500; }
.summary dd { margin: 0; font-weight: 700; }
