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

html, body {
  height: 100%;
  background: #0b1a2b;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  color: #eee;
  overflow: hidden;
}

#game-wrap {
  position: relative;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

canvas#game {
  display: block;
  max-width: 100%;
  max-height: calc(100% - 96px); /* room for the top and build bars */
  aspect-ratio: 1280 / 800;
  background: #1a4d80;
  cursor: crosshair;
  border: 2px solid #123;
}

/* ---------- Top bar ---------- */
#top-bar {
  width: 100%;
  max-width: 1280px;
  display: flex;
  gap: 22px;
  justify-content: center;
  padding: 5px 12px;
  font-size: 15px;
  font-weight: 600;
  background: #10233a;
  border-bottom: 2px solid #1d3a5c;
  user-select: none;
  white-space: nowrap;
}
#stat-day { color: #ffd76e; }
#stat-castle.danger { color: #ff6b6b; }
#stat-nation {
  color: #ffd76e;
  background: rgba(255, 215, 110, 0.12);
  border: 1px solid rgba(255, 215, 110, 0.5);
  border-radius: 5px;
  padding: 0 8px;
}

/* ---------- Build bar ---------- */
#build-bar {
  width: 100%;
  max-width: 1280px;
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 5px 10px;
  background: #10233a;
  border-top: 2px solid #1d3a5c;
}
#cat-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 3px;
}
.cat-btn {
  font-family: inherit;
  font-size: 11px;
  font-weight: 700;
  padding: 4px 10px;
  background: #14293f;
  color: #9fc0e0;
  border: 1px solid #24486e;
  border-radius: 5px;
  cursor: pointer;
  white-space: nowrap;
}
.cat-btn:hover { background: #1c3d63; }
.cat-btn.active { background: #2f5d94; color: #fff; border-color: #4a90d9; }
#build-items {
  display: flex;
  gap: 6px;
  align-items: center;
  min-height: 56px;
}
#build-items .build-btn { display: none; }
#build-items .build-btn.visible { display: flex; }
.build-btn {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0;
  padding: 3px 9px;
  background: #1c3d63;
  color: #eee;
  border: 1px solid #2f5d94;
  border-radius: 6px;
  cursor: pointer;
  font-family: inherit;
  white-space: nowrap;
}
.build-btn:hover { background: #275088; }
.build-btn.active { background: #3a6db3; outline: 2px solid #7db4f0; }
.build-btn.unaffordable { opacity: 0.5; }
.b-name { font-size: 13px; font-weight: 700; }
.b-cost { font-size: 11px; color: #ffd76e; }
.b-desc { font-size: 10px; color: #9fc0e0; }
#hint {
  margin-left: auto;
  font-size: 11px;
  color: #7d9ab8;
  user-select: none;
  white-space: normal;
  max-width: 220px;
}

/* ---------- Warning banner ---------- */
#warning-banner {
  position: absolute;
  top: 70px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(120, 20, 20, 0.92);
  border: 2px solid #ff5a5a;
  border-radius: 8px;
  padding: 10px 26px;
  text-align: center;
  pointer-events: none;
  animation: pulse 1s infinite;
  z-index: 10;
}
#warning-title { font-size: 22px; font-weight: 800; color: #ffdddd; }
#warning-detail { font-size: 15px; color: #ffc4c4; margin-top: 2px; }
#warning-countdown { font-size: 30px; font-weight: 800; color: #fff; margin-top: 2px; }
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 6px #ff5a5a; }
  50% { box-shadow: 0 0 22px #ff5a5a; }
}

/* ---------- Day banner ---------- */
#day-banner {
  position: absolute;
  top: 42%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 56px;
  font-weight: 900;
  color: #fff;
  text-shadow: 0 0 18px #000;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.4s;
  z-index: 9;
}
#day-banner.show { opacity: 1; }
#day-banner.milestone { font-size: 84px; color: #ffd76e; }

/* ---------- Toasts ---------- */
#toasts {
  position: absolute;
  right: 16px;
  top: 70px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  pointer-events: none;
  z-index: 11;
}
.toast {
  background: rgba(12, 30, 50, 0.92);
  border-left: 4px solid #4a90d9;
  padding: 7px 14px;
  border-radius: 4px;
  font-size: 14px;
  animation: toast-in 0.25s ease-out;
}
.toast.bad { border-left-color: #ff5a5a; color: #ffc4c4; }
.toast.good { border-left-color: #57d977; color: #c9f5d4; }
@keyframes toast-in {
  from { transform: translateX(30px); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* ---------- Upgrade panel ---------- */
#upgrade-panel {
  position: absolute;
  bottom: 64px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 14px;
  background: rgba(16, 35, 58, 0.95);
  border: 1px solid #2f5d94;
  border-radius: 8px;
  padding: 8px 16px;
  z-index: 8;
}
#up-title { font-size: 16px; font-weight: 700; }
#up-detail { font-size: 13px; color: #9fc0e0; }
#up-btn {
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  padding: 6px 14px;
  background: #2f7d4f;
  color: #fff;
  border: none;
  border-radius: 6px;
  cursor: pointer;
}
#up-btn:hover { background: #3a9c63; }
#up-btn:disabled { background: #3a4a5c; color: #8aa; cursor: default; }

/* ---------- Chat ---------- */
#chat-log {
  position: absolute;
  left: 14px;
  bottom: 108px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  max-width: 380px;
  pointer-events: none;
  z-index: 9;
}
.chat-msg {
  background: rgba(10, 25, 42, 0.85);
  border-radius: 4px;
  padding: 4px 10px;
  font-size: 13px;
  color: #d6e6f5;
  word-break: break-word;
}
.chat-msg b { color: #ffd76e; }
#chat-input {
  position: absolute;
  left: 14px;
  bottom: 78px;
  width: 320px;
  font-family: inherit;
  font-size: 14px;
  background: rgba(10, 25, 42, 0.95);
  color: #eee;
  border: 1px solid #2f5d94;
  border-radius: 6px;
  padding: 7px 10px;
  z-index: 13;
}

/* ---------- Raid dialog ---------- */
#raid-dialog {
  position: absolute;
  bottom: 70px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: rgba(16, 35, 58, 0.97);
  border: 1px solid #2f5d94;
  border-radius: 10px;
  padding: 14px 20px;
  z-index: 12;
}
#raid-title { font-size: 16px; font-weight: 700; }
.raid-row { display: flex; align-items: center; gap: 10px; }
.raid-row label { font-size: 14px; color: #9fc0e0; }
#raid-count {
  font-family: inherit;
  font-size: 18px;
  font-weight: 700;
  width: 70px;
  text-align: center;
  background: #0b1a2b;
  color: #ffd76e;
  border: 1px solid #2f5d94;
  border-radius: 6px;
  padding: 5px;
}
#raid-avail { font-size: 12px; color: #7d9ab8; }
#raid-error { font-size: 13px; color: #ff8080; min-height: 16px; }
#raid-launch, #raid-cancel {
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  padding: 7px 18px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
}
#raid-launch { background: #2f7d4f; color: #fff; flex: 1; }
#raid-launch:hover { background: #3a9c63; }
#raid-cancel { background: #3a4a5c; color: #cdd; }
#raid-cancel:hover { background: #4a5c70; }

/* ---------- Lobby ---------- */
#lobby {
  position: absolute;
  inset: 0;
  background: rgba(5, 15, 28, 0.82);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 25;
}
#lobby-panel {
  background: #10233a;
  border: 2px solid #2f5d94;
  border-radius: 14px;
  padding: 30px 46px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: stretch;
  text-align: center;
  min-width: 320px;
}
#lobby-panel h1 { font-size: 34px; margin-bottom: 8px; }
.lobby-big {
  font-family: inherit;
  font-size: 18px;
  font-weight: 700;
  padding: 12px 20px;
  background: #1c3d63;
  color: #eee;
  border: 1px solid #2f5d94;
  border-radius: 8px;
  cursor: pointer;
}
.lobby-big:hover { background: #275088; }
.lobby-big:disabled { opacity: 0.5; cursor: default; }
#lobby-solo { background: #2f7d4f; border-color: #3a9c63; }
#lobby-solo:hover { background: #3a9c63; }
#lobby-start { background: #b3822a; border-color: #d9a33c; }
#lobby-start:hover { background: #d9a33c; }
.lobby-divider {
  font-size: 12px;
  color: #7d9ab8;
  margin-top: 6px;
  text-transform: uppercase;
  letter-spacing: 1px;
}
.lobby-row { display: flex; gap: 8px; }
#lobby-code {
  flex: 1;
  font-family: inherit;
  font-size: 20px;
  font-weight: 700;
  text-transform: uppercase;
  text-align: center;
  letter-spacing: 6px;
  background: #0b1a2b;
  color: #ffd76e;
  border: 1px solid #2f5d94;
  border-radius: 8px;
  padding: 8px;
  width: 120px;
}
#lobby-join {
  font-family: inherit;
  font-size: 16px;
  font-weight: 700;
  padding: 8px 22px;
  background: #1c3d63;
  color: #eee;
  border: 1px solid #2f5d94;
  border-radius: 8px;
  cursor: pointer;
}
#lobby-join:hover { background: #275088; }
#lobby-status { font-size: 14px; color: #9fc0e0; min-height: 20px; }

/* ---------- Tutorial ---------- */
#tutorial {
  position: fixed;
  inset: 0;
  pointer-events: none; /* the game stays playable underneath */
  z-index: 30;
}
#tut-arrow {
  position: fixed;
  font-size: 58px;
  color: #ffd76e;
  text-shadow: 0 0 10px rgba(0,0,0,0.9), 0 0 22px rgba(255,215,110,0.6);
  pointer-events: none;
  user-select: none;
}
#tut-arrow.down { transform: rotate(90deg); animation: tut-bounce-down 0.8s infinite; }
#tut-arrow.up { transform: rotate(-90deg); animation: tut-bounce-up 0.8s infinite; }
@keyframes tut-bounce-down {
  0%, 100% { transform: rotate(90deg) translateX(0); }
  50% { transform: rotate(90deg) translateX(12px); }
}
@keyframes tut-bounce-up {
  0%, 100% { transform: rotate(-90deg) translateX(0); }
  50% { transform: rotate(-90deg) translateX(12px); }
}
#tut-card {
  position: fixed;
  top: 58px;
  left: 50%;
  transform: translateX(-50%);
  width: 480px;
  max-width: 92vw;
  background: rgba(13, 30, 50, 0.97);
  border: 2px solid #ffd76e;
  border-radius: 12px;
  padding: 14px 18px;
  pointer-events: auto;
  box-shadow: 0 6px 30px rgba(0,0,0,0.6);
}
#tut-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}
#tut-title { font-size: 17px; font-weight: 800; color: #ffd76e; }
#tut-close {
  font-family: inherit;
  background: none;
  border: none;
  color: #9fc0e0;
  font-size: 16px;
  cursor: pointer;
}
#tut-close:hover { color: #fff; }
#tut-text { font-size: 14px; line-height: 1.5; color: #d6e6f5; margin: 8px 0 10px; }
#tut-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
#tut-progress { font-size: 12px; color: #7d9ab8; }
#tut-back, #tut-next {
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  padding: 6px 16px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
}
#tut-back { background: #3a4a5c; color: #cdd; }
#tut-back:hover { background: #4a5c70; }
#tut-next { background: #2f7d4f; color: #fff; }
#tut-next:hover { background: #3a9c63; }

/* ---------- End overlay ---------- */
#end-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
}
#end-panel {
  background: #10233a;
  border: 2px solid #2f5d94;
  border-radius: 12px;
  padding: 30px 50px;
  text-align: center;
  max-height: 90%;
  overflow-y: auto;
}
#end-title { font-size: 40px; margin-bottom: 18px; }
#end-title.win { color: #ffd76e; }
#end-title.lose { color: #ff6b6b; }
#end-stats {
  text-align: left;
  font-size: 16px;
  line-height: 1.7;
  margin-bottom: 22px;
}
#end-stats b { color: #ffd76e; float: right; margin-left: 40px; }
#restart-btn {
  font-size: 20px;
  font-weight: 700;
  padding: 10px 36px;
  background: #2f7d4f;
  color: #fff;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-family: inherit;
}
#restart-btn:hover { background: #3a9c63; }

.hidden { display: none !important; }
