/* Cozy Town — hand-drawn paper UI */
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }
body {
  font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
  background: #f7efdd;
  color: #4a3f30;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
#app { position: fixed; inset: 0; overflow: hidden; }
#game { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; cursor: grab; }
#game:active { cursor: grabbing; }

.hidden { display: none !important; }

/* ---------- Top bar ---------- */
#topbar {
  position: absolute; top: 0; left: 0; right: 0;
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px;
  padding: 8px 10px;
  background: rgba(247, 239, 221, 0.94);
  border-bottom: 2px solid #4a3f30;
  z-index: 10;
  font-size: 15px;
}
.stat {
  display: flex; align-items: center; gap: 5px;
  background: #fffdf6; border: 2px solid #4a3f30; border-radius: 999px;
  padding: 4px 10px; font-weight: 700; white-space: nowrap;
  box-shadow: 2px 2px 0 rgba(74, 63, 48, 0.25);
}
.stat .ico { font-size: 15px; }
.stat .net { font-size: 11px; font-weight: 600; color: #6b7a4f; }
.stat .net.neg { color: #b0523c; }
.speed { display: flex; gap: 4px; margin-left: auto; }
.speed-btn, .top-btn {
  min-width: 40px; min-height: 36px;
  border: 2px solid #4a3f30; border-radius: 10px;
  background: #fffdf6; color: #4a3f30; font-weight: 800; font-size: 15px;
  cursor: pointer; box-shadow: 2px 2px 0 rgba(74, 63, 48, 0.25);
}
.speed-btn.active { background: #4a3f30; color: #f7efdd; }
.top-btn { padding: 0 10px; font-size: 13px; }

/* ---------- RCI demand bars ---------- */
#rci {
  position: absolute; top: 56px; left: 10px; z-index: 9;
  background: rgba(247, 239, 221, 0.92);
  border: 2px solid #4a3f30; border-radius: 12px;
  padding: 6px 8px; display: flex; flex-direction: column; gap: 4px;
  box-shadow: 2px 2px 0 rgba(74, 63, 48, 0.25);
}
.rci-row { display: flex; align-items: center; gap: 5px; }
.rci-label {
  width: 16px; height: 16px; border-radius: 50%;
  color: #fffdf6; font-size: 10px; font-weight: 800;
  display: flex; align-items: center; justify-content: center;
}
.rci-label.r { background: #6b9e4f; }
.rci-label.c { background: #4f7fa8; }
.rci-label.i { background: #d98a3d; }
.rci-bar { width: 90px; height: 9px; background: #e5dcc4; border: 1.5px solid #4a3f30; border-radius: 999px; overflow: hidden; }
.rci-fill { height: 100%; width: 30%; transition: width 0.6s ease; }
.rci-fill.r { background: #6b9e4f; }
.rci-fill.c { background: #4f7fa8; }
.rci-fill.i { background: #d98a3d; }
.saved-row { margin-top: 2px; border-top: 1px dashed #d9c9a3; padding-top: 4px; }
.saved-row span { font-size: 11px; font-weight: 700; color: #6b7a4f; white-space: nowrap; }

/* ---------- Hint bar (tool info / placement reason) ---------- */
#hint {
  position: absolute; left: 50%; transform: translateX(-50%); bottom: 96px; z-index: 9;
  background: rgba(74, 63, 48, 0.92); color: #f7efdd;
  border-radius: 999px; padding: 7px 14px; font-size: 13px; font-weight: 600;
  max-width: calc(100vw - 20px); white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  pointer-events: none;
}
#hint.bad { background: rgba(176, 82, 60, 0.95); }
#hint.good { background: rgba(61, 107, 36, 0.95); }

/* ---------- Toast ---------- */
#toast {
  position: absolute; top: 64px; left: 50%; transform: translateX(-50%);
  z-index: 20; background: #4a3f30; color: #f7efdd;
  border-radius: 999px; padding: 9px 18px; font-weight: 700; font-size: 15px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.3); white-space: nowrap;
  max-width: 92vw; overflow: hidden; text-overflow: ellipsis;
}

/* ---------- Info panel ---------- */
#panel {
  position: absolute; z-index: 15;
  right: 10px; top: 110px; width: 280px; max-width: calc(100vw - 20px);
  max-height: calc(100vh - 230px); overflow-y: auto;
  background: #fffdf6; border: 2px solid #4a3f30; border-radius: 16px;
  padding: 14px 16px 16px; box-shadow: 3px 3px 0 rgba(74, 63, 48, 0.3);
}
#panel-close {
  position: absolute; top: 8px; right: 8px;
  width: 30px; height: 30px; border-radius: 50%;
  border: 2px solid #4a3f30; background: #f7efdd; font-size: 13px; cursor: pointer;
}
#panel h2 { font-size: 19px; margin-bottom: 2px; padding-right: 28px; }
#panel .sub { font-size: 13px; color: #8a7a5a; margin-bottom: 10px; }
#panel .row { display: flex; justify-content: space-between; gap: 8px; font-size: 14px; padding: 4px 0; border-bottom: 1px dashed #d9c9a3; }
#panel .row:last-child { border-bottom: none; }
#panel .row b { text-align: right; }
#panel .need { margin-top: 10px; background: #f7efdd; border: 2px solid #d9c9a3; border-radius: 10px; padding: 8px 10px; font-size: 14px; }
#panel .bar { height: 9px; background: #e5dcc4; border: 1.5px solid #4a3f30; border-radius: 999px; overflow: hidden; margin-top: 3px; }
#panel .bar > div { height: 100%; background: #6b9e4f; }
#panel .bar.low > div { background: #b0523c; }
#panel .bar.mid > div { background: #d98a3d; }

/* ---------- Toolbar ---------- */
#toolbar {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; gap: 8px; padding: 10px;
  overflow-x: auto; overflow-y: hidden;
  background: rgba(247, 239, 221, 0.94);
  border-top: 2px solid #4a3f30;
  scrollbar-width: thin;
  -webkit-overflow-scrolling: touch;
}
.tool {
  flex: 0 0 auto;
  min-width: 74px; min-height: 62px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
  background: #fffdf6; border: 2px solid #4a3f30; border-radius: 14px;
  color: #4a3f30; cursor: pointer; padding: 6px 8px;
  box-shadow: 2px 2px 0 rgba(74, 63, 48, 0.25);
  font-family: inherit;
}
.tool:active { transform: translate(1px, 1px); box-shadow: 1px 1px 0 rgba(74, 63, 48, 0.25); }
.tool.active { background: #4a3f30; color: #f7efdd; }
.tool.disabled { opacity: 0.45; }
.tool .t-ico { font-size: 22px; line-height: 1.1; }
.tool .t-name { font-size: 12px; font-weight: 800; }
.tool .t-cost { font-size: 10px; color: #8a7a5a; }
.tool.active .t-cost { color: #d9c9a3; }

/* ---------- Start overlay ---------- */
.overlay {
  position: absolute; inset: 0; z-index: 30;
  background: rgba(74, 63, 48, 0.55);
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.card {
  background: #fffdf6; border: 3px solid #4a3f30; border-radius: 22px;
  max-width: 520px; width: 100%; max-height: 92vh; overflow-y: auto;
  padding: 26px 24px; text-align: center;
  box-shadow: 6px 6px 0 rgba(74, 63, 48, 0.35);
}
.card h1 {
  font-size: 44px; letter-spacing: 1px; color: #4a3f30;
  text-shadow: 2px 2px 0 #f2d492;
}
.card .tagline { font-size: 17px; color: #8a7a5a; margin: 4px 0 16px; font-style: italic; }
.howto { text-align: left; font-size: 15px; line-height: 1.45; }
.howto p { margin-bottom: 10px; }
.big-btn {
  display: block; width: 100%; margin-top: 14px;
  padding: 14px; font-size: 19px; font-weight: 800; font-family: inherit;
  background: #6b9e4f; color: #fffdf6; border: 2px solid #4a3f30; border-radius: 14px;
  cursor: pointer; box-shadow: 3px 3px 0 rgba(74, 63, 48, 0.35);
}
.big-btn:active { transform: translate(2px, 2px); box-shadow: 1px 1px 0 rgba(74, 63, 48, 0.35); }
.big-btn.secondary { background: #fffdf6; color: #4a3f30; }
.fine { margin-top: 12px; font-size: 12px; color: #8a7a5a; }

/* ---------- Test output (hidden in normal play) ---------- */
#test-output {
  position: absolute; left: 0; bottom: 0; z-index: 40;
  background: #fff; color: #000; font-size: 11px;
  max-width: 100vw; max-height: 40vh; overflow: auto; white-space: pre-wrap;
}

/* ---------- Narrow phone ---------- */
@media (max-width: 560px) {
  #topbar { font-size: 13px; padding: 6px 8px; gap: 4px; }
  .stat { padding: 3px 8px; }
  .stat .net { display: none; }
  .speed-btn, .top-btn { min-width: 36px; min-height: 32px; font-size: 13px; }
  #rci { top: 140px; } /* top bar wraps to 3 rows on a narrow phone */
  .rci-bar { width: 64px; }
  #hint { bottom: 90px; font-size: 12px; }
  #panel { top: auto; bottom: 96px; right: 10px; left: 10px; width: auto; max-height: 42vh; }
  #toast { top: 140px; font-size: 13px; }
  .tool { min-width: 66px; min-height: 58px; }
  .card h1 { font-size: 34px; }
  .card { padding: 20px 16px; }
  .howto { font-size: 14px; }
}

/* ---------- Wide desktop ---------- */
@media (min-width: 900px) {
  #toolbar { justify-content: center; }
  #panel { top: 120px; }
}
