/* Open House — cozy building-arrangement puzzle.
   Portrait-first, phone 360px and up. No external fonts or assets.
   Compact metrics: a 5-floor level with 6 clues fits an 800px viewport;
   all tap targets are >= 48px (Android guideline). */

:root {
  --paper: #f6efe4;
  --paper-deep: #efe3d0;
  --ink: #3d3229;
  --ink-soft: #7a6a58;
  --terracotta: #c96f4a;
  --terracotta-deep: #a95536;
  --sage: #7d9471;
  --sage-deep: #5f7a55;
  --wood: #b98d63;
  --wood-deep: #9a7049;
  --cream: #fff8ec;
  --wall: #ead9bd;
  --win-warm: #ffd98a;
  --good: #4c8a4c;
  --bad: #b4523f;
  --shadow: 0 2px 10px rgba(61, 50, 41, .14);
  --radius: 14px;
  --bw: min(260px, 70vw);   /* building width */
  --tap: 48px;              /* minimum tap target */
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; padding: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: "Avenir Next", "Segoe UI", system-ui, -apple-system, sans-serif;
  min-height: 100dvh;
}

#app { max-width: 430px; margin: 0 auto; padding: 0 12px calc(12px + env(safe-area-inset-bottom)); }

.screen { display: flex; flex-direction: column; gap: 8px; padding-top: 8px; }
.hidden { display: none !important; }

/* ---------- buttons (all >= 48px tall) ---------- */
.btn {
  font: inherit; font-weight: 600;
  border: none; border-radius: 999px;
  min-height: var(--tap);
  padding: 10px 18px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  transition: transform .08s ease, box-shadow .12s ease, background .12s ease;
  touch-action: manipulation;
}
.btn:active { transform: scale(.96); }
.btn.primary { background: var(--terracotta); color: #fff; box-shadow: var(--shadow); }
.btn.primary:hover { background: var(--terracotta-deep); }
.btn.ghost { background: transparent; color: var(--ink-soft); border: 1.5px solid #d8c8ae; }
.btn.ghost:hover { color: var(--ink); border-color: var(--ink-soft); }
.btn.big { font-size: 1.15rem; padding: 14px 34px; }
.btn.icon { min-width: var(--tap); padding: 0 10px; }
.btn:disabled { opacity: .4; cursor: default; }
.btn.pulse { animation: pulse 1.6s ease infinite; }
@keyframes pulse { 0%,100% { box-shadow: 0 0 0 0 rgba(201,111,74,.45); } 50% { box-shadow: 0 0 0 10px rgba(201,111,74,0); } }

/* ---------- home ---------- */
#screen-home { align-items: center; text-align: center; padding-top: 9dvh; }
.home-art .mini-building { width: 120px; margin: 0 auto 6px; }
.mb-roof {
  width: 0; height: 0; margin: 0 auto;
  border-left: 60px solid transparent; border-right: 60px solid transparent;
  border-bottom: 34px solid var(--terracotta);
}
.mb-floor {
  background: var(--wall); border: 2px solid var(--wood);
  border-top: none; height: 36px; margin: 0 10px;
  display: flex; align-items: center; justify-content: center; font-size: 20px;
}
.mb-floor.lit { background: var(--win-warm); }
#screen-home h1 { font-size: 2.2rem; margin: 4px 0 2px; letter-spacing: .5px; }
.tagline { color: var(--ink-soft); margin: 0 0 18px; line-height: 1.45; }
#screen-home .btn { min-width: 220px; margin-bottom: 10px; }
.footnote { color: var(--ink-soft); font-size: .78rem; margin-top: 14px; }

/* ---------- bars ---------- */
.bar { display: flex; align-items: center; gap: 8px; min-height: var(--tap); }
.bar h2 { font-size: 1rem; margin: 0; flex: 1; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar .back { flex: 0 0 auto; }
.bar-spacer { width: 48px; }

/* ---------- level grid ---------- */
.level-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.level-card {
  background: var(--cream); border: 1.5px solid #e3d3b8; border-radius: var(--radius);
  min-height: 84px; padding: 10px 6px 8px; text-align: center; cursor: pointer;
  transition: transform .08s ease, border-color .12s ease;
  touch-action: manipulation;
  font: inherit;
}
.level-card:hover { border-color: var(--terracotta); }
.level-card:active { transform: scale(.96); }
.level-card .num { font-size: 1.2rem; font-weight: 700; }
.level-card .nm { font-size: .7rem; color: var(--ink-soft); margin-top: 3px; line-height: 1.25; }
.level-card .done { font-size: .95rem; margin-top: 4px; visibility: hidden; }
.level-card.completed .done { visibility: visible; }

/* ---------- game ---------- */
.story { margin: 0; color: var(--ink-soft); font-style: italic; text-align: center; line-height: 1.35; font-size: .8rem; }
.lesson {
  margin: 0; background: var(--cream); border: 1.5px dashed var(--wood);
  border-radius: 10px; padding: 6px 10px; font-size: .78rem; line-height: 1.4;
  color: var(--ink); text-align: center;
}

.stage { display: flex; justify-content: center; }
.building { width: var(--bw); user-select: none; }
.roof {
  height: 0; margin: 0 -6px;
  border-left: calc(var(--bw) / 2 + 6px) solid transparent;
  border-right: calc(var(--bw) / 2 + 6px) solid transparent;
  border-bottom: 22px solid var(--terracotta);
}
.floors { border: 3px solid var(--wood-deep); border-radius: 0 0 10px 10px; background: var(--wall); overflow: hidden; }
.floor {
  position: relative;
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; padding: 0 10px;
  height: 50px;
  border-bottom: 2px solid var(--wood);
  cursor: pointer;
  transition: background .15s ease;
  touch-action: manipulation;
}
.floor:last-child { border-bottom: none; }
.floor .window {
  width: 40%; height: 62%;
  background: #f4e8cf; border: 2.5px solid var(--wood); border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  font-size: clamp(20px, 6.5vw, 26px);
  transition: background .25s ease, box-shadow .25s ease;
  position: relative;
}
.floor .resident-name {
  position: absolute; bottom: -2px; left: 50%; transform: translateX(-50%);
  font-size: .55rem; color: var(--ink-soft); background: rgba(255,248,236,.85);
  padding: 0 5px; border-radius: 6px; white-space: nowrap;
}
.floor.occupied .window { background: var(--win-warm); box-shadow: inset 0 0 10px rgba(201,111,74,.25); }
.floor.empty .window::after { content: "·"; color: var(--wood); font-size: 18px; font-weight: 700; opacity: .5; }
.floor .fnum {
  font-size: .6rem; color: var(--ink-soft); background: rgba(255,248,236,.7);
  border-radius: 6px; padding: 2px 6px; font-weight: 600;
}
.floor:hover { background: #f0e0c4; }
.floor.drop-target { background: #f7e7c8; box-shadow: inset 0 0 0 3px var(--terracotta); }
.building.shake { animation: shake .4s ease; }
@keyframes shake { 0%,100% { transform: translateX(0); } 25% { transform: translateX(-7px); } 75% { transform: translateX(7px); } }
.building.celebrate .floor.occupied .window {
  background: var(--win-warm); box-shadow: 0 0 16px 3px rgba(255,205,110,.75);
}

/* ---------- tray ---------- */
#tray-wrap { min-height: 74px; }
.tray-label { margin: 0 0 5px; font-size: .74rem; color: var(--ink-soft); text-align: center; }
.tray { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; min-height: 56px; }
.resident-chip {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  width: 60px; min-height: 56px; background: var(--cream);
  border: 2px solid #e3d3b8; border-radius: 12px; cursor: pointer;
  transition: transform .08s ease, border-color .12s ease, box-shadow .12s ease;
  touch-action: manipulation;
  font: inherit; padding: 2px 0;
}
.resident-chip .emoji { font-size: 24px; line-height: 1.1; }
.resident-chip .nm { font-size: .58rem; color: var(--ink-soft); }
.resident-chip:hover { border-color: var(--wood); }
.resident-chip:active { transform: scale(.94); }
.resident-chip.selected {
  border-color: var(--terracotta); box-shadow: 0 0 0 3px rgba(201,111,74,.25);
  transform: translateY(-3px);
}
.tray.empty .tray-hint { display: block; }
.tray-hint { display: none; color: var(--ink-soft); font-size: .8rem; align-self: center; }

/* ---------- clues (fit the screen; overflow collapses behind a toggle) ---------- */
.clues-label { margin: 0 0 5px; font-size: .74rem; color: var(--ink-soft); }
.clues {
  list-style: none; margin: 0; padding: 0 2px 0 0;
  display: flex; flex-direction: column; gap: 5px;
  /* No inner scroll: game.js hides overflow rows and offers "▾ N more clues",
     so touch users never fight a scrollable box inside a scrolling page. */
}
.clues-more {
  font: inherit; font-size: .74rem; font-weight: 600; color: var(--ink-soft);
  background: var(--paper-deep); border: 1.5px dashed #d8c8ae; border-radius: 8px;
  width: 100%; min-height: 44px; cursor: pointer; margin-top: 2px;
  touch-action: manipulation;
}
.clues-more:hover { color: var(--ink); border-color: var(--wood); }
.clue {
  display: flex; align-items: center; gap: 7px; flex: 0 0 auto;
  background: var(--cream); border: 1.5px solid #e3d3b8; border-radius: 10px;
  padding: 6px 9px; font-size: .8rem; line-height: 1.3;
  transition: border-color .2s ease, background .2s ease;
}
.clue .glyph { font-size: .95rem; flex: 0 0 auto; width: 1.4em; text-align: center; }
.clue .status { margin-left: auto; font-weight: 700; flex: 0 0 auto; }
.clue.ok { border-color: var(--sage); background: #f0f5ec; }
.clue.ok .status { color: var(--good); }
.clue.broken { border-color: #e0b8ac; background: #f9ece7; }
.clue.broken .status { color: var(--bad); }

/* ---------- controls / message ---------- */
.controls { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.controls .btn { padding: 10px 16px; }
.controls.center { justify-content: center; }
.message { min-height: 1.2em; text-align: center; font-size: .8rem; color: var(--ink-soft); margin: 0; }
.message.bad { color: var(--bad); }
.message.good { color: var(--good); font-weight: 600; }

/* ---------- overlays ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(61, 50, 41, .45);
  display: flex; align-items: center; justify-content: center; padding: 20px;
  z-index: 40; animation: fade .25s ease;
}
@keyframes fade { from { opacity: 0; } }
.overlay .card {
  background: var(--paper); border-radius: 18px; padding: 22px 18px;
  text-align: center; max-width: 320px; width: 100%; box-shadow: 0 12px 40px rgba(0,0,0,.25);
}
.overlay .win-art { font-size: 3rem; margin-bottom: 4px; }
.overlay h2 { margin: 0 0 6px; font-size: 1.25rem; }
#win-sub, #confirm-sub { color: var(--ink-soft); margin: 0 0 14px; line-height: 1.4; font-size: .9rem; }

/* reduced motion */
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}

/* Short phones (budget Android ~640px and below): tighten the fixed costs.
   The JS clue fitter guarantees the page fits even at 320×568. */
@media (max-height: 720px) {
  .screen { gap: 6px; }
  .roof { border-bottom-width: 18px; }
  .floor { height: 44px; }
  .floor .window { font-size: clamp(18px, 6vw, 24px); }
  .floor .resident-name { font-size: .5rem; }
  #tray-wrap { min-height: 64px; }
  .tray { min-height: 50px; gap: 6px; }
  .resident-chip { width: 56px; min-height: 50px; }
  .resident-chip .emoji { font-size: 22px; }
  .clue { padding: 5px 8px; font-size: .76rem; }
  .story { font-size: .75rem; }
  .lesson { font-size: .72rem; padding: 4px 8px; }
  .controls .btn { padding: 8px 14px; }
  .message { font-size: .74rem; }
}

/* Very narrow phones: keep the level title on one line */
@media (max-width: 340px) {
  .bar h2 { font-size: .85rem; }
}

/* Very short phones (~568px): single-line tray, tighter everything.
   The clue fitter may collapse to 1 visible clue + expander here. */
@media (max-height: 600px) {
  #app { padding-left: 8px; padding-right: 8px; }
  .screen { gap: 4px; }
  .story { font-size: .72rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .lesson { font-size: .7rem; padding: 3px 6px; }
  .roof { border-bottom-width: 14px; }
  .floor { height: 40px; }
  .floor .window { font-size: 18px; border-width: 2px; }
  .floor .fnum { font-size: .55rem; padding: 1px 4px; }
  #tray-wrap { min-height: 58px; }
  .tray-label { font-size: .68rem; margin-bottom: 3px; }
  .tray { gap: 4px; min-height: 48px; }
  .resident-chip { width: 52px; min-height: 48px; }
  .resident-chip .emoji { font-size: 20px; }
  .clues-label { font-size: .68rem; margin-bottom: 3px; }
  .clue { padding: 4px 7px; font-size: .72rem; gap: 5px; }
  .controls .btn { padding: 6px 12px; }
  .message { font-size: .7rem; }
}

/* Portal ad break in progress: freeze input until the ad closes */
body.ad-playing #app { pointer-events: none; }
