:root {
  /* Follows the system; the header toggle pins it via data-theme. */
  color-scheme: light dark;
  --fg: light-dark(#111, #ddd);
  --bg: light-dark(#fff, #111);
  --dim: #888;
  --line: light-dark(#ddd, #333);
  --red: light-dark(#d00000, #ff4d4d);
  --warm: light-dark(#e07000, #ff9a33);
  --off: light-dark(#bbb, #555);
  --hover: light-dark(#f3f3f3, #1c1c1c);
  --open: light-dark(#eee, #242424);
  --flash: light-dark(#e4e4e4, #333);
}
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] { color-scheme: dark; }

* { box-sizing: border-box; }
/* No double-tap zoom on mobile; pinch zoom still works. */
html { touch-action: manipulation; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 14px/1.5 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

main {
  max-width: 1120px;
  margin: 0 auto;
  padding: 24px 24px 64px;
}

header { display: flex; align-items: baseline; }
#clock { margin-left: auto; }
#theme { border: none; padding: 0 4px; margin-left: 8px; color: var(--dim); }
@media (hover: hover) { #theme:hover { background: none; color: var(--fg); } }
#langBox { position: relative; display: inline-block; margin-left: 12px; color: var(--dim); cursor: pointer; }
#lang { position: absolute; inset: 0; width: 100%; opacity: 0; cursor: pointer; font: inherit; }
@media (hover: hover) { #langBox:hover { color: var(--fg); } }
#langBox:focus-within { outline: 1px solid var(--dim); }
h1 { font-size: 14px; margin: 0; letter-spacing: 0.05em; }
h1 button { border: none; padding: 0; font-weight: inherit; letter-spacing: inherit; background: none; }
@media (hover: hover) { h1 button:hover:not(:disabled) { background: none; color: var(--fg); text-decoration: underline; } }

/* About: privacy policy and terms (the app stores need them). Padding is on the inner div so a
   click on the dialog element itself is always the backdrop. */
#about { border: 1px solid var(--fg); padding: 0; color: var(--fg); background: var(--bg); max-width: min(40ch, 90vw); }
#about > div { padding: 16px 20px; }
#about p { margin: 0 0 12px; }
#about a { display: inline-block; padding: 4px 0; }
#about::backdrop { background: rgb(0 0 0 / 0.4); }

/* First-launch intro in the Android app: the same box as About. */
#intro { border: 1px solid var(--fg); padding: 0; color: var(--fg); background: var(--bg); max-width: min(40ch, 90vw); }
#intro > div { padding: 16px 20px; }
#intro p { margin: 8px 0 16px; outline: none; }
#intro .nav { display: flex; justify-content: space-between; gap: 12px; }
#intro .nav button:first-child { border: none; padding-left: 0; color: var(--dim); }
#intro .nav button:last-child { margin-left: auto; }
#intro::backdrop { background: rgb(0 0 0 / 0.4); }

/* privacy.html, terms.html */
main.doc { max-width: 70ch; line-height: 1.5; }
.doc h1 { margin-bottom: 4px; }
.doc h2 { margin-top: 28px; }
h2 { font-size: 14px; font-weight: normal; margin: 0 0 8px; border-bottom: 1px solid var(--fg); }

.dim { color: var(--dim); }
.red { color: var(--red); }
.warm { color: var(--warm); }

button {
  font: inherit;
  color: var(--fg);
  background: var(--bg);
  border: 1px solid var(--fg);
  padding: 1px 10px;
  cursor: pointer;
}
/* Hover only where there is one, so a tapped button doesn't stay inverted on touch screens. */
@media (hover: hover) { button:hover:not(:disabled) { background: var(--fg); color: var(--bg); } }
button:disabled { color: var(--off); border-color: var(--off); cursor: default; }
/* Big decisions: inverted, so they stand out from the buy buttons. */
button.loud { background: var(--fg); color: var(--bg); font-weight: bold; }
@media (hover: hover) { button.loud:hover:not(:disabled) { background: var(--bg); color: var(--fg); } }

a { color: inherit; }

/* Touch: 44px tap targets in the mobile layout. */
@media (max-width: 820px) {
  button { min-height: 44px; }
  #theme, #reader .x, .toast .x { min-width: 44px; background: none; }
  /* The clock gets its own line, so the title and the buttons fit beside each other. */
  header { flex-wrap: wrap; }
  #clock { order: 1; flex-basis: 100%; margin: 0; }
  #controls { margin-left: auto; white-space: nowrap; }
  #theme { margin: 0 -14px 0 0; }
  #langBox { min-width: 44px; line-height: 44px; margin: -14px -8px -14px 0; text-align: center; }
  .row[data-act] { min-height: 44px; align-items: center; }
  a[data-act] { display: inline-block; padding: 12px 0; }
}

/* ---- ticker & status ---- */

#ticker { min-height: 4.5em; margin: 16px 0 8px; }
#ticker div:nth-child(2) { opacity: 0.45; }
#ticker div:nth-child(3) { opacity: 0.2; }
#ticker div:first-child { animation: in 0.6s ease-out; }
#ticker .when { display: inline-block; min-width: 17ch; color: var(--dim); }
@keyframes in { from { opacity: 0; } }

#status {
  padding: 4px 0 16px;
  border-bottom: 1px solid var(--fg);
  margin-bottom: 16px;
}
#status .big { font-size: 28px; font-weight: bold; margin-bottom: 10px; }
#status .stack { font-size: 16px; line-height: 1.7; }

/* A number that just changed flashes, so early clicks are visible. */
.num { display: inline-block; padding: 0 3px; margin: 0 -3px; }
.num.changed { animation: changed 0.7s ease-out; }
@keyframes changed { from { background: var(--flash); } }

/* ---- layout ---- */

#columns {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: 40px;
  align-items: start;
}
@media (max-width: 820px) {
  #columns { grid-template-columns: minmax(0, 1fr); }
}

/* Once "Better model" is up, Automation moves above Rules and Staff. From
   stage 5 it goes to the very top, so it's near the ticker. */
#right { display: flex; flex-direction: column; }
body.model #pUpgrades { order: -2; }
body.model #pAuto { order: -1; }
body.stage5 #pAuto { order: -3; }

/* Desktop: the inbox scrolls on its own, so the panels stay in reach. */
@media (min-width: 821px) {
  #inbox { max-height: 100vh; overflow-y: auto; }
}

#actions { margin-bottom: 12px; display: flex; gap: 8px; align-items: baseline; }
#rate { margin-left: 8px; }

/* ---- inbox ---- */

.row {
  display: grid;
  grid-template-columns: 20ch minmax(0, 1fr) 7ch;
  gap: 16px;
  padding: 2px 6px;
  cursor: pointer;
  white-space: nowrap;
}
.row > span { overflow: hidden; text-overflow: ellipsis; }
@media (hover: hover) { .row:hover { background: var(--hover); } }
.row.unread { font-weight: bold; }
.row.open { background: var(--open); }
.row .time { text-align: right; color: var(--dim); font-weight: normal; }
.row .snip { color: var(--dim); font-weight: normal; }
.row.warm, .row.warm .snip { color: var(--warm); }

#empty { padding: 24px 6px; }

.row.fresh { animation: fresh 1.6s ease-out; }
@keyframes fresh { from { background: var(--flash); } }

.delta { display: inline-block; margin-left: 6px; color: var(--dim); opacity: 0; }
.delta.pop { animation: pop 1.2s ease-out; }
@keyframes pop {
  from { opacity: 1; transform: translateY(0); }
  to { opacity: 0; transform: translateY(-8px); }
}

#reader {
  border: 1px solid var(--fg);
  padding: 12px 16px;
  margin-bottom: 16px;
}
#reader .meta { display: flex; justify-content: space-between; }
#reader .subj { font-weight: bold; margin: 4px 0 8px; }
#reader .body { margin-bottom: 12px; max-width: 60ch; }
#reader .body .clip { margin-top: 8px; }
#reader.warm .body, #reader.warm .from { color: var(--warm); }
#reader .x { border: none; padding: 0 4px; }
.nophoto {
  display: inline-flex; flex-direction: column; align-items: center; gap: 6px;
  border: 1px dashed var(--dim); color: var(--dim); font-size: 13px;
  padding: 14px 24px; margin: 0 0 12px;
}
.nophoto svg { fill: none; stroke: var(--off); stroke-width: 4; stroke-dasharray: 6 6; }
#thread .nophoto { margin: 8px 0 4px; }
#backdrop { display: none; }

/* On mobile the reader is a popup so opening mail doesn't push the inbox down. */
@media (max-width: 820px) {
  #reader {
    position: fixed;
    left: 12px;
    right: 12px;
    top: 50%;
    transform: translateY(-50%);
    max-height: 80vh;
    overflow-y: auto;
    margin: 0;
    background: var(--bg);
    box-shadow: 3px 3px 0 var(--fg);
    z-index: 7;
  }
  #reader .x { font-size: 20px; line-height: 1; }
  #reader .meta { align-items: center; }
  #reader:not([hidden]) + #backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.25);
    z-index: 6;
  }
  body:has(#reader:not([hidden])) { overflow: hidden; }
}

/* ---- panels ---- */

.panel { margin-bottom: 28px; }
.item { margin-bottom: 10px; }
.item .desc { color: var(--dim); font-size: 13px; }
.item.done { color: var(--dim); }
.item.done s { text-decoration-thickness: 1px; }
.stat { display: flex; justify-content: space-between; }
.foot { margin-top: 6px; }

/* ---- new mail notifications ---- */

#toasts {
  position: fixed;
  right: 16px;
  bottom: 16px;
  width: 320px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 5;
}
.toast {
  background: var(--bg);
  border: 1px solid var(--fg);
  padding: 8px 12px;
  cursor: pointer;
  white-space: nowrap;
  box-shadow: 2px 2px 0 var(--fg);
  animation: toastIn 0.25s ease-out;
  transition: opacity 0.5s;
}
.toast { position: relative; }
.toast > div { overflow: hidden; text-overflow: ellipsis; padding-right: 16px; }
.toast .x {
  position: absolute;
  top: 2px;
  right: 4px;
  border: none;
  padding: 0 4px;
  color: var(--dim);
}
@media (hover: hover) { .toast .x:hover { background: none; color: var(--fg); } }
@media (max-width: 820px) {
  .toast .x { top: 0; right: 0; }
  .toast > div { padding-right: 32px; }
}
.toast .from { font-weight: bold; }
.toast .snip { color: var(--dim); }
.toast.warm .from, .toast.warm .subj { color: var(--warm); }
.toast.gone { opacity: 0; }
@keyframes toastIn { from { transform: translateX(24px); opacity: 0; } }

.toast.pin { white-space: normal; animation: toastIn 0.25s ease-out, pinIn 1.2s ease-out; }
.toast.pin .snip { color: var(--fg); margin-top: 4px; }
.toast.news > div { white-space: normal; }
.toast.news .when { display: block; color: var(--dim); font-size: 12px; }
@keyframes pinIn { from { box-shadow: 4px 4px 0 var(--red); } }

/* ---- fake ending: the agents' version ---- */

#fake {
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 10;
  animation: in 2s ease-out;
}
#fake .center {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
}
#fake .zero { font-size: 18px; }
#fake > .center > * { animation: in 1.5s ease-out; }
#fake .credits { height: 60vh; width: min(560px, 90vw); overflow: hidden; }
#fake .roll { animation: roll 13s linear forwards; }
#fake .pair { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; line-height: 2.2; }
#fake .pair > :first-child { text-align: right; }
#fake .solo { text-align: center; line-height: 2.2; }
@keyframes roll { from { transform: translateY(60vh); } to { transform: translateY(-100%); } }
#fake .replies { width: min(680px, 90vw); }
#fake .replies .row { cursor: default; animation: fresh 1s ease-out; }

/* ---- ending ---- */

body.draining #right,
body.draining #actions,
body.draining #status .stack {
  opacity: 0;
  transition: opacity 3s;
  pointer-events: none;
}

/* Stage 5: the agents push you out, one piece at a time, before the fake ending.
   Each piece fades, then the inbox collapses so what's left moves up. */
body.pushed1 #left,
body.pushed2 #right,
body.pushed3 #status,
body.pushed4 #ticker,
body.pushed4 #clock {
  opacity: 0;
  transition: opacity 3s;
  pointer-events: none;
}
body.pushed1 #left {
  max-height: 0;
  overflow: hidden;
  transition: opacity 3s, max-height 0s 3s;
}
/* A second to read the last line, then nothing. */
body.pushed4 #ticker { transition: opacity 2s 1s; }

#ending {
  position: fixed;
  inset: 0;
  background: var(--bg);
  overflow-y: auto;
  animation: in 2s ease-out;
}
#ending .center {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 24px;
}
#ending .zero { font-size: 18px; }
#ending .one { animation: in 3s ease-out; text-align: center; }
#ending .one .row { width: min(680px, 90vw); grid-template-columns: 12ch minmax(0, 1fr) 12ch; margin-top: 8px; text-align: left; font-weight: bold; }

#thread {
  max-width: 62ch;
  margin: 0 auto;
  padding: 64px 24px 120px;
  animation: in 1.5s ease-out;
}
#thread .msg { margin: 28px 0; }
#thread .msg .body { color: var(--warm); margin-top: 4px; }
#thread .msg.unread .body { font-weight: bold; }
#thread .note { color: var(--dim); font-size: 13px; margin-top: 2px; }
#thread .note b { color: var(--fg); }
#thread textarea {
  width: 100%;
  min-height: 8em;
  font: inherit;
  padding: 8px;
  color: var(--fg);
  background: var(--bg);
  border: 1px solid var(--fg);
  resize: vertical;
  margin: 12px 0 8px;
}

/* The ending fades to white, or to near-black in dark mode. */
#white {
  position: fixed;
  inset: 0;
  background: var(--bg);
  opacity: 0;
  pointer-events: none;
  transition: opacity 3.5s ease-out;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 16px;
}
#white.on { opacity: 1; pointer-events: auto; }
#whiteText { color: light-dark(#bbb, #666); text-align: center; display: flex; flex-direction: column; align-items: center; gap: 24px; }
#whiteText .stack { display: grid; place-items: center; }
#whiteText .stack > * { grid-area: 1 / 1; }
#whiteText .fade { opacity: 0; visibility: hidden; transition: opacity 3s ease-out, visibility 3s; }
#whiteText .fade.on { opacity: 1; visibility: visible; }
#whiteText .memorial.fade { transition-duration: 4s; }
#whiteText .memorial { display: flex; flex-direction: column; align-items: center; gap: 8px; }
#whiteText .memorial img { width: min(420px, 80vw); }
#whiteText .memorial .name { color: var(--dim); }
