/* Pocket Pal — styles
   Layout notes:
   - .stage is a fixed-aspect, letterboxed box. Everything drawn on top of it is
     sized in container units (cqh/cqw) so the HUD keeps its proportions no matter
     how the stage letterboxes. Viewport units would measure the wrong box.
   - [hidden] is only a UA-stylesheet rule, so any class here that sets `display`
     would keep a hidden element painted. The !important rule below settles it. */

:root {
  color-scheme: light dark;

  --bg:        #efe4d2;
  --bg-2:      #e5d7c1;
  --surface:   #fffaf1;
  --surface-2: #f6ecdc;
  --line:      #d9c8ae;
  --line-soft: #e8dcc8;
  --ink:       #40342a;
  --ink-soft:  #7a6a58;
  --ink-faint: #a2917c;

  --accent:      #e2725b;
  --accent-ink:  #fff6f2;
  --good:        #6aa84f;
  --warn:        #e0a02c;
  --bad:         #d1495b;
  --cool:        #5b8fd6;

  --radius:   14px;
  --radius-s: 9px;
  --shadow:   0 1px 0 rgba(255,255,255,.7) inset, 0 2px 6px rgba(72,55,38,.13);
  --shadow-lg: 0 12px 34px rgba(50,36,22,.28);

  --font: ui-rounded, "SF Pro Rounded", "Segoe UI Rounded", "Nunito",
          system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:        #221d19;
    --bg-2:      #1a1613;
    --surface:   #2e2823;
    --surface-2: #38302a;
    --line:      #4a3f36;
    --line-soft: #3e352e;
    --ink:       #f0e6d8;
    --ink-soft:  #bdab97;
    --ink-faint: #8a7a68;

    --accent:      #f0866c;
    --accent-ink:  #2a1a14;
    --good:        #8bc46a;
    --warn:        #f0bd52;
    --bad:         #ef6b7d;
    --cool:        #7fb0ef;

    --shadow: 0 1px 0 rgba(255,255,255,.05) inset, 0 2px 6px rgba(0,0,0,.35);
    --shadow-lg: 0 12px 34px rgba(0,0,0,.5);
  }
}

[hidden] { display: none !important; }

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background:
    radial-gradient(1200px 700px at 50% -10%, var(--bg) 0%, var(--bg-2) 70%) fixed;
  color: var(--ink);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

:focus-visible {
  outline: 3px solid var(--cool);
  outline-offset: 2px;
  border-radius: 6px;
}

/* ---------------------------------------------------------------- app shell */

.app {
  height: 100%;
  max-width: 760px;
  margin: 0 auto;
  padding: max(8px, env(safe-area-inset-top)) 10px max(8px, env(safe-area-inset-bottom));
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto auto auto;
  gap: 8px;
}

/* ------------------------------------------------------------------- topbar */

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 4px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 800;
  letter-spacing: .2px;
  color: var(--ink-soft);
  flex: none;
}
.brand-mark { font-size: 18px; }
.brand-name { font-size: 14px; }

.ident {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1.15;
}
.ident-name {
  font-weight: 800;
  font-size: 17px;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.ident-meta {
  font-size: 11.5px;
  color: var(--ink-faint);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.topbar-tools { display: flex; gap: 6px; flex: none; }

.icon-btn {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  font-size: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow);
  transition: transform .1s ease, background .15s ease;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn:active { transform: translateY(1px); }

/* -------------------------------------------------------------------- stage */

.stage-wrap {
  min-height: 92px;
  display: grid;
  place-items: center;
}

.stage {
  position: relative;
  width: 100%;
  /* --ar is set from JS to the shape of the space available, clamped so the
     room never gets extreme. The row is `1fr` tall, so an unclamped box can
     overflow it: cqh here resolves against .stage-wrap (the stage is its own
     container, so these units are not self-referential) and letterboxes it. */
  --ar: 1.3333;
  aspect-ratio: var(--ar);
  max-width: min(100%, calc(100cqh * var(--ar)));
  max-height: 100%;
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--line);
  box-shadow: var(--shadow-lg);
  background: #14202c;
  container-type: size;
  touch-action: manipulation;
}

.stage-wrap { container-type: size; }

#scene {
  display: block;
  width: 100%;
  height: 100%;
  cursor: pointer;
}

/* HUD chips — sized against the stage, not the viewport */
.hud {
  position: absolute;
  inset: 0;
  padding: 2.4cqh;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1cqw;
  pointer-events: none;
}
.hud-row { display: flex; gap: 1cqw; flex-wrap: wrap; }
.hud-right { justify-content: flex-end; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: .35em;
  padding: .3em .6em;
  font-size: clamp(9px, 3.4cqh, 13px);
  font-weight: 700;
  white-space: nowrap;
  color: #4a3a2a;
  background: rgba(255,252,245,.82);
  border: 1px solid rgba(120,95,70,.18);
  border-radius: 999px;
  backdrop-filter: blur(3px);
}
.chip-alert {
  color: #fff;
  background: rgba(209,73,91,.92);
  border-color: rgba(255,255,255,.35);
}

/* speech bubble */
.speech {
  position: absolute;
  left: 50%;
  top: 11cqh;
  transform: translateX(-50%);
  max-width: 78cqw;
  padding: .45em .8em;
  font-size: clamp(10px, 3.8cqh, 15px);
  font-weight: 700;
  text-align: center;
  color: #40342a;
  background: rgba(255,253,248,.95);
  border: 1px solid rgba(120,95,70,.2);
  border-radius: 12px;
  box-shadow: 0 4px 14px rgba(40,25,10,.18);
  pointer-events: none;
  animation: pop .22s ease;
}
.speech::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -7px;
  width: 12px;
  height: 12px;
  transform: translateX(-50%) rotate(45deg);
  background: rgba(255,253,248,.95);
  border-right: 1px solid rgba(120,95,70,.2);
  border-bottom: 1px solid rgba(120,95,70,.2);
}

@keyframes pop {
  from { transform: translateX(-50%) scale(.85); opacity: 0; }
  to   { transform: translateX(-50%) scale(1);   opacity: 1; }
}

/* ------------------------------------------------------------------ overlay */

.stage-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 4cqh 4cqw;
  background: rgba(30,20,12,.55);
  backdrop-filter: blur(2px);
  z-index: 5;
}

.panel {
  width: min(100%, 460px);
  max-height: 100%;
  overflow: auto;
  padding: clamp(12px, 4cqh, 20px);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  animation: rise .2s ease;
}
@keyframes rise {
  from { transform: translateY(10px) scale(.97); opacity: 0; }
}

.panel-title {
  margin: 0 0 .5em;
  font-size: clamp(15px, 5.2cqh, 21px);
  letter-spacing: -.01em;
}
.panel-content {
  font-size: clamp(11px, 3.9cqh, 14.5px);
  color: var(--ink-soft);
}
.panel-content p { margin: 0 0 .7em; }
.panel-content strong { color: var(--ink); }
.panel-actions {
  margin-top: 14px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
}

.btn {
  padding: .55em 1em;
  font-size: clamp(11px, 3.9cqh, 14.5px);
  font-weight: 700;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--radius-s);
  box-shadow: var(--shadow);
  transition: transform .1s ease, filter .15s ease;
}
.btn:hover { filter: brightness(1.04); }
.btn:active { transform: translateY(1px); }
.btn-primary {
  background: var(--accent);
  border-color: transparent;
  color: var(--accent-ink);
}
.btn-ghost { background: transparent; box-shadow: none; }
.btn-danger { color: var(--bad); }

.field {
  width: 100%;
  padding: .6em .7em;
  margin: 4px 0 2px;
  font: inherit;
  font-size: clamp(12px, 4cqh, 15px);
  color: var(--ink);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--radius-s);
}

/* panel widgets */
.kv {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 3px 12px;
  margin: 0 0 .8em;
  font-size: clamp(11px, 3.7cqh, 14px);
}
.kv dt { color: var(--ink-faint); }
.kv dd { margin: 0; color: var(--ink); font-weight: 700; text-align: right; }

.trophy-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 6px;
}
.trophy {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 7px 9px;
  background: var(--surface-2);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius-s);
  opacity: .5;
}
.trophy.got { opacity: 1; border-color: var(--warn); }
.trophy-mark { font-size: 18px; flex: none; }
.trophy-text { display: flex; flex-direction: column; min-width: 0; }
.trophy-name { font-weight: 800; color: var(--ink); }
.trophy-desc { font-size: .86em; color: var(--ink-faint); }

.choice-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
  gap: 8px;
}
.choice {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 12px 8px;
  font-weight: 800;
  background: var(--surface-2);
  border: 2px solid var(--line);
  border-radius: var(--radius-s);
  transition: transform .12s ease, border-color .12s ease;
}
.choice:hover { transform: translateY(-2px); border-color: var(--accent); }
.choice-emoji { font-size: 30px; }

/* ------------------------------------------------------------------- stats  */

.stats {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
  padding: 9px 10px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.stat {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}
.stat-icon { font-size: 15px; flex: none; }
.stat-body { display: flex; flex-direction: column; gap: 3px; flex: 1 1 auto; min-width: 0; }
.stat-label {
  display: flex;
  justify-content: space-between;
  gap: 4px;
  font-size: 10px;
  font-weight: 700;
  color: var(--ink-faint);
  letter-spacing: .02em;
}
.stat-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stat-value { font-variant-numeric: tabular-nums; color: var(--ink-soft); }

.meter {
  display: block;
  height: 7px;
  background: var(--line-soft);
  border-radius: 999px;
  overflow: hidden;
}
.meter-fill {
  display: block;
  height: 100%;
  width: 100%;
  border-radius: 999px;
  background: var(--good);
  transition: width .35s ease, background-color .35s ease;
}
.stat[data-level="mid"]  .meter-fill { background: var(--warn); }
.stat[data-level="low"]  .meter-fill { background: var(--bad); }
.stat[data-level="low"]  .stat-value { color: var(--bad); }
.stat[data-level="low"]  { animation: nudge 1.6s ease-in-out infinite; }

@keyframes nudge {
  0%, 88%, 100% { transform: none; }
  92% { transform: translateX(-1.5px); }
  96% { transform: translateX(1.5px); }
}

/* ------------------------------------------------------------------ actions */

.actions {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 6px;
}

.act {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 9px 2px 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-s);
  box-shadow: var(--shadow);
  overflow: hidden;
  transition: transform .1s ease, background .15s ease, opacity .2s ease;
}
.act:hover:not(:disabled) { background: var(--surface-2); }
.act:active:not(:disabled) { transform: translateY(1px) scale(.98); }
.act:disabled { opacity: .38; cursor: not-allowed; }
.act-icon { font-size: 19px; line-height: 1; }
.act-text { font-size: 10.5px; font-weight: 800; letter-spacing: .01em; }
.act-key {
  position: absolute;
  top: 3px;
  right: 4px;
  font-size: 8.5px;
  font-weight: 700;
  color: var(--ink-faint);
}
.act[data-want="1"]::after {
  content: "";
  position: absolute;
  top: 5px;
  left: 5px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  animation: blip 1.2s ease-in-out infinite;
}
@keyframes blip { 50% { opacity: .25; } }

/* ------------------------------------------------------------------- logbar */

.logbar { padding: 0 4px 2px; min-height: 1.4em; }
.log {
  margin: 0;
  font-size: 12px;
  color: var(--ink-faint);
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ------------------------------------------------------------- small screens */

/* Five stats in one row stops fitting well below ~520px; let them wrap rather
   than truncating every label to three letters. */
@media (max-width: 520px) {
  .stats { grid-template-columns: repeat(auto-fit, minmax(104px, 1fr)); }
}

@media (max-width: 420px) {
  .act-text { font-size: 9.5px; }
  .act-key { display: none; }
  .stat-icon { font-size: 13px; }
  .brand-name { display: none; }
}

@media (max-height: 560px) {
  .stats { padding: 6px 8px; }
  .act { padding: 6px 2px 5px; }
  .logbar { display: none; }
}

/* Very short viewports: the fixed chrome would otherwise eat the whole column
   and leave the stage — the actual game — at zero height. Shrink the chrome and
   keep a floor under the stage. */
@media (max-height: 460px) {
  .app { gap: 5px; padding-top: 4px; padding-bottom: 4px; }
  .topbar { padding: 1px 2px; gap: 6px; }
  .icon-btn { width: 28px; height: 28px; font-size: 14px; }
  .ident-name { font-size: 14px; }
  .ident-meta { display: none; }
  .stats {
    grid-template-columns: repeat(5, 1fr);
    padding: 4px 6px;
    gap: 4px;
  }
  .stat-icon { display: none; }
  .stat-label { font-size: 9px; }
  .stat-name { display: none; }
  .stat-value { margin-left: auto; }
  .meter { height: 5px; }
  .act { padding: 4px 2px 3px; }
  .act-icon { font-size: 15px; }
  .act-text { font-size: 8.5px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .001ms !important; animation-iteration-count: 1 !important; }
  .meter-fill { transition: none; }
}

/* --------------------------------------------------------------- mini-game  */

.mini-line {
  margin: 0 0 8px;
  font-weight: 700;
  color: var(--ink);
}

.mini-controls {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 8px;
}
.mini-stop {
  padding: .5em .8em;
  font-size: 13px;
  border: 1px solid var(--line);
}
.mini-btn {
  padding: .8em .5em;
  font-size: clamp(12px, 4.2cqh, 16px);
}

/* The mini-game is played in the room, so its controls dock to the bottom of
   the stage instead of covering it. */
.stage-overlay[data-variant="bottom"] {
  inset: auto 0 0 0;
  padding: 0 3cqw 3cqh;
  align-items: end;
  background: linear-gradient(to top, rgba(24,16,10,.58), rgba(24,16,10,0));
  backdrop-filter: none;
}
.stage-overlay[data-variant="bottom"] .panel {
  width: min(100%, 420px);
  padding: clamp(9px, 2.6cqh, 14px);
}
/* The round line says everything the title would; the stage is worth more. */
.stage-overlay[data-variant="bottom"] .panel-title { display: none; }
.stage-overlay[data-variant="bottom"] .panel-actions:empty { display: none; }
.stage-overlay[data-variant="bottom"] .panel-actions {
  margin-top: 8px;
}

/* ----------------------------------------------------------------- pal-dex  */

.dex-wrap {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin: 0 0 12px;
}
.dex {
  padding: .25em .55em;
  font-size: clamp(9.5px, 3.2cqh, 12px);
  font-weight: 700;
  color: var(--ink-faint);
  background: var(--surface-2);
  border: 1px dashed var(--line);
  border-radius: 999px;
}
.dex.known {
  color: var(--ink);
  border-style: solid;
  border-color: var(--accent);
}

.muted { color: var(--ink-faint); font-size: .92em; }
