:root {
  color-scheme: dark;
  --paper: #070a0b;
  --paper-deep: #101616;
  --ink: #bdc9b7;
  --muted: #748177;
  --line: #3c4942;
  --panel: rgba(9, 13, 14, 0.97);
  --purple: #82927b;
  --purple-dark: #a5b59c;
  --focus: #c7a66b;
  font-family: "Courier New", Courier, monospace;
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
  margin: 0;
  min-width: 280px;
  background: #050708;
  color: var(--ink);
}

button { font: inherit; }

button:focus-visible,
canvas:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 3px;
}

.app-shell {
  width: min(1120px, 100%);
  min-height: 100svh;
  margin: 0 auto;
  padding: max(18px, env(safe-area-inset-top)) clamp(14px, 3vw, 36px) max(24px, env(safe-area-inset-bottom));
  display: grid;
  grid-template-rows: auto minmax(310px, 1fr) auto;
  gap: 18px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
}

.title-lockup { display: flex; align-items: center; gap: 12px; }

.title-pixels {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  background: var(--purple);
  color: #080b0b;
  letter-spacing: -2px;
  font-size: 14px;
  box-shadow: 4px 4px 0 #29332e;
}

h1, h2, p { margin: 0; }

h1 {
  font: 700 clamp(1.1rem, 2.2vw, 1.45rem)/1.1 "Courier New", monospace;
  letter-spacing: -0.04em;
  text-shadow: 0 0 10px rgba(173, 199, 169, .16);
}

.title-lockup p {
  margin-top: 4px;
  color: var(--muted);
  font-size: .72rem;
}

.controls { display: flex; align-items: center; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }

.button {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 0;
  padding: 9px 13px;
  background: #0c1112;
  color: var(--ink);
  font-size: .78rem;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 2px 2px 0 #020303;
  touch-action: manipulation;
}

.button:hover:not(:disabled) { background: #18201e; border-color: #64766b; transform: translateY(-1px); }
.button:active:not(:disabled) { transform: translate(1px, 1px); box-shadow: none; }
.button:disabled { opacity: .42; cursor: not-allowed; }
.button-primary { background: var(--purple); border-color: var(--purple); color: #070a0b; }
.button-primary:hover:not(:disabled) { background: var(--purple-dark); color: #070a0b; }
.icon-button { display: inline-flex; align-items: center; gap: 7px; }
.icon-button[aria-pressed="true"] .sound-mark { text-decoration: line-through; opacity: .45; }
#feedButton[aria-pressed="true"] { color: #d2b777; border-color: #9a8050; background: #1b170e; }
#feedButton:disabled { opacity: .72; }
#eggButton[aria-pressed="true"] { color: #d8d2ad; border-color: #9b9575; background: #1a1912; }

.resident-count {
  min-width: 48px;
  text-align: center;
  color: var(--muted);
  font-size: .72rem;
}

.farm {
  position: relative;
  min-height: 310px;
  overflow: hidden;
  border: 1px solid #26302c;
  background: #070a0b;
  box-shadow: inset 0 0 38px rgba(104, 135, 112, .035);
}

.farm::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: repeating-linear-gradient(
    to bottom,
    transparent 0,
    transparent 3px,
    rgba(153, 181, 151, .025) 3px,
    rgba(153, 181, 151, .025) 4px
  );
}

#farmCanvas {
  display: block;
  width: 100%;
  height: 100%;
  min-height: 310px;
  cursor: crosshair;
  image-rendering: pixelated;
  touch-action: manipulation;
}

#farmCanvas.is-feeding { cursor: crosshair; }

.canvas-hint {
  position: absolute;
  right: 12px;
  bottom: 10px;
  color: rgba(139, 158, 141, .48);
  font-size: .64rem;
  pointer-events: none;
}

.empty-message {
  position: absolute;
  inset: 50% auto auto 50%;
  width: min(310px, 78%);
  transform: translate(-50%, -50%);
  color: var(--muted);
  text-align: center;
  font-size: .78rem;
  line-height: 1.6;
  pointer-events: none;
}

.selected-panel,
.records-panel,
.info-panel {
  border: 1px solid var(--line);
  background: var(--panel);
  box-shadow: 5px 5px 0 #020303;
}

.selected-panel {
  position: fixed;
  z-index: 20;
  left: 50%;
  bottom: max(18px, env(safe-area-inset-bottom));
  width: min(780px, calc(100vw - 28px));
  transform: translateX(-50%);
  min-height: 100px;
  padding: 12px 50px 12px 12px;
  display: grid;
  grid-template-columns: 104px minmax(110px, 1fr) auto auto;
  align-items: center;
  gap: 18px;
  border-color: #65766c;
  box-shadow:
    0 0 0 1px rgba(174, 199, 171, .08),
    0 16px 54px rgba(0, 0, 0, .72);
  animation: panel-in 120ms steps(3, end);
}

.selected-panel[hidden], .records-panel[hidden], .info-panel[hidden] { display: none; }

#portraitCanvas {
  width: 104px;
  height: 72px;
  background: #0b1010;
  image-rendering: pixelated;
}

.eyebrow {
  display: block;
  margin-bottom: 5px;
  color: var(--muted);
  font-size: .62rem;
  letter-spacing: .14em;
}

.identity h2, .panel-heading h2 { font-size: 1.18rem; letter-spacing: .08em; }

.timers {
  display: flex;
  gap: clamp(18px, 4vw, 44px);
  margin: 0;
}

.timers div { min-width: 128px; }
.timers dt { color: var(--muted); font-size: .66rem; }
.timers dd { margin: 5px 0 0; font-size: .83rem; font-weight: 700; white-space: nowrap; }
.resident-actions { display: grid; grid-template-columns: 1fr auto; gap: 8px; }
.extend-button { min-height: 48px; padding-inline: 18px; background: var(--ink); color: #070a0b; border-color: var(--ink); }
.kill-button { min-height: 48px; color: #c58f87; border-color: #6d4744; background: #160e0f; }
.kill-button:hover:not(:disabled) { color: #efb0a7; border-color: #9b625c; background: #241315; }

.close-button {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 34px;
  height: 34px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 1.35rem;
  line-height: 1;
  cursor: pointer;
}
.close-button:hover { color: var(--ink); background: #17201d; }
.close-button.static { position: static; flex: none; }

.records-panel,
.info-panel {
  position: fixed;
  z-index: 30;
  top: 50%;
  left: 50%;
  width: min(520px, calc(100vw - 28px));
  max-height: calc(100svh - 40px);
  padding: 20px;
  overflow-y: auto;
  transform: translate(-50%, -50%);
  box-shadow:
    0 0 0 100vmax rgba(0, 0, 0, .48),
    0 16px 54px rgba(0, 0, 0, .82);
  animation: panel-in-centered 120ms steps(3, end);
}
.info-panel { width: min(440px, calc(100vw - 28px)); }
.info-panel p { margin-top: 18px; color: var(--ink); font-size: .82rem; line-height: 1.7; }
.panel-heading { display: flex; align-items: start; justify-content: space-between; gap: 16px; }
.oldest-alive { margin: 18px 0; padding: 11px 12px; background: #101716; font-size: .75rem; }
.oldest-alive strong { color: var(--purple-dark); }
.record-list { margin: 0; padding: 0; list-style: none; counter-reset: lives; }
.record-list li {
  counter-increment: lives;
  display: grid;
  grid-template-columns: 2rem 1fr auto;
  gap: 8px;
  padding: 9px 4px;
  border-top: 1px solid rgba(112,109,120,.2);
  font-size: .76rem;
}
.record-list li::before { content: counter(lives, decimal-leading-zero); color: var(--muted); }
.record-list time { font-weight: 700; }
.no-records { margin-top: 12px; color: var(--muted); font-size: .74rem; }

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0);
  white-space: nowrap; border: 0;
}

@keyframes panel-in {
  from { opacity: 0; transform: translate(-50%, 8px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

@keyframes panel-in-centered {
  from { opacity: 0; transform: translate(-50%, calc(-50% + 8px)); }
  to { opacity: 1; transform: translate(-50%, -50%); }
}

@media (max-width: 760px) {
  .app-shell { grid-template-rows: auto minmax(330px, 1fr) auto; gap: 12px; }
  .topbar { align-items: flex-start; flex-direction: column; }
  .controls {
    width: 100%;
    display: grid;
    grid-template-columns: 1fr auto 1fr;
  }
  .controls .button { min-width: 0; width: 100%; padding-inline: 8px; }
  .resident-count { min-width: 42px; margin: 0; padding: 10px 2px; }
  .farm, #farmCanvas { min-height: 330px; }
  .selected-panel {
    bottom: max(10px, env(safe-area-inset-bottom));
    padding: 10px 44px 12px 10px;
    grid-template-columns: 78px 1fr;
    gap: 10px 14px;
  }
  #portraitCanvas { width: 78px; height: 64px; grid-row: 1 / span 2; }
  .timers { gap: 12px; }
  .timers div { min-width: 0; }
  .timers dd { font-size: .7rem; }
  .resident-actions { grid-column: 1 / -1; width: 100%; }
}

@media (max-width: 380px) {
  .title-lockup p { display: none; }
  .button { padding-inline: 9px; font-size: .71rem; }
  .sound-mark { display: none; }
  .canvas-hint { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .button { transition: none; }
  .selected-panel, .records-panel, .info-panel { animation: none; }
}
