/* prytan owner page.
 *
 * Layout: the page is exactly one screen tall (100dvh). The header takes what
 * it needs and <main> gets the rest; nothing on the Cards and Updates views
 * scrolls the page itself. Inside a card the answer buttons stay at the
 * bottom, the image gives up height first, and only the text scrolls. When the
 * screen is wider than tall (a phone on its side, a tablet, a desktop) the
 * image moves to the left and the text and buttons to the right. Sizes use
 * clamp() so they follow the screen between a small phone and a desktop.
 */
:root {
  --bg: #f6f5f1;
  --fg: #1d2433;
  --muted: #6b7280;
  --card: #ffffff;
  --line: #e2e0d8;
  --accent: #2f5d9e;
  --yes: #2e7d4f;
  --no: #b3402f;
  --warn: #9a6b00;
  color-scheme: light dark;

  --gap: clamp(8px, 1.6vmin, 16px);
  --pad: clamp(12px, 2.4vmin, 24px);
  --tap: clamp(40px, 6.5vmin, 52px); /* button height: smaller on short screens */
  --radius: clamp(10px, 1.6vmin, 16px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151c;
    --fg: #e8e6e1;
    --muted: #9aa0ab;
    --card: #1c2029;
    --line: #2c313c;
    --accent: #7aa7e8;
    --yes: #5cbf86;
    --no: #e07b6a;
    --warn: #e0b04a;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--fg);
  font: clamp(15px, 0.85rem + 0.35vw, 18px)/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

/* ---- Header: server and connection, tabs, refresh ---- */
header {
  flex: none;
  display: flex;
  align-items: center;
  gap: clamp(4px, 1vw, 12px);
  padding: 8px var(--pad);
  padding-top: max(8px, env(safe-area-inset-top));
  min-width: 0;
}
.server {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  min-height: 36px;
  padding: 4px 10px 4px 0;
  border: 0;
  background: transparent;
  color: var(--fg);
  font-weight: 700;
}
.server-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 22vw; }
.conn-text { font-weight: 600; font-size: 0.8em; color: var(--no); white-space: nowrap; }
.dot { flex: none; display: inline-block; width: 10px; height: 10px; border-radius: 50%; background: var(--line); }
.dot.on, .dot.ok { background: var(--yes); }
.dot.wait { background: var(--warn); }
.dot.bad { background: var(--no); }
nav#tabs { display: flex; gap: 2px; min-width: 0; overflow-x: auto; scrollbar-width: none; }
nav#tabs::-webkit-scrollbar { display: none; }
.tab {
  flex: none;
  min-height: 36px;
  padding: 4px clamp(8px, 1.6vw, 14px);
  border-radius: 999px;
  font-size: 0.9em;
  white-space: nowrap;
  background: transparent;
  border-color: transparent;
  color: var(--muted);
}
.tab.active { background: var(--card); border-color: var(--line); color: var(--fg); }
#count { flex: 1; }
#refresh { flex: none; min-height: 36px; padding: 4px 8px; }
/* On a narrow phone the server shows only its dot (tap it for the details). */
@media (max-width: 420px) {
  .server-name { display: none; }
  .conn-text { display: none; }
}

/* ---- Main area ---- */
main {
  flex: 1;
  min-height: 0;
  width: 100%;
  max-width: 1400px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 0 var(--pad) max(var(--pad), env(safe-area-inset-bottom));
}
main.top { justify-content: flex-start; overflow-y: auto; }
main.full { padding: 0; overflow: hidden; }

button, select, input, textarea {
  font: inherit;
  border-radius: calc(var(--radius) * 0.75);
  border: 1px solid var(--line);
  padding: 0.55em 0.8em;
  background: var(--bg);
  color: var(--fg);
}
button, select, input { min-height: var(--tap); }
button { cursor: pointer; font-weight: 600; }
button.yes { background: var(--yes); border-color: var(--yes); color: #fff; }
button.no { background: var(--no); border-color: var(--no); color: #fff; }
button.ghost { background: transparent; border-color: transparent; color: var(--accent); }
textarea { width: 100%; resize: vertical; }
.muted { color: var(--muted); text-align: center; }
.error { color: var(--no); text-align: center; flex: none; margin: 8px 0 0; }
.small { font-size: 0.85em; }
h1 { font-size: clamp(19px, 1rem + 1.1vw, 28px); line-height: 1.25; margin: 0; overflow-wrap: anywhere; }
h2 { margin: 0; font-size: clamp(17px, 0.9rem + 0.6vw, 22px); line-height: 1.3; }

#login { display: flex; flex-direction: column; gap: var(--gap); width: 100%; max-width: 480px; margin: 0 auto; }
#login label { color: var(--muted); }

/* ---- A card: head, image, text, actions ---- */
#card {
  flex: 0 1 auto;
  min-height: 0;
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  padding: var(--pad);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.card-head { flex: none; display: flex; flex-direction: column; gap: calc(var(--gap) * 0.75); }
.meta { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; font-size: 0.8em; color: var(--muted); }
.pill { background: var(--accent); color: var(--card); border-radius: 999px; padding: 1px 10px; font-weight: 600; }
#urgency { color: var(--no); font-weight: 700; }
.context { margin: 0; padding: 6px 10px; border-radius: 10px; background: var(--bg); color: var(--muted); font-size: 0.85em; overflow-wrap: anywhere; }
/* The image is the first thing to shrink when the screen is short. */
.shot {
  flex: 0 1 auto;
  min-height: 96px;
  width: 100%;
  object-fit: contain;
  border-radius: calc(var(--radius) * 0.6);
  background: var(--bg);
  cursor: zoom-in;
}
/* The text scrolls inside the card if it does not fit. */
.card-content { flex: 0 1 auto; min-height: 2.5em; overflow-y: auto; overscroll-behavior: contain; display: flex; flex-direction: column; gap: 6px; }
#body { white-space: pre-wrap; overflow-wrap: anywhere; }
#detail { color: var(--accent); }
#unblocks { margin: 0; color: var(--muted); }
.card-actions { flex: none; display: flex; flex-direction: column; gap: calc(var(--gap) * 0.75); }
#actions { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(120px, 40%), 1fr)); gap: calc(var(--gap) * 0.75); }
#free { display: flex; gap: 8px; }
#free input { flex: 1; min-width: 0; }
#explain-form { display: flex; flex-direction: column; gap: 8px; }
.pair { display: flex; justify-content: flex-end; flex-wrap: wrap; gap: 8px; }
.secondary { display: grid; grid-template-columns: 1fr auto auto; gap: 4px; }
.secondary button.ghost { padding-left: 10px; padding-right: 10px; }
.next { flex: none; color: var(--muted); font-size: 0.85em; margin: 8px 4px 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: center; }

/* The card's column is wider than tall: image left, everything else right.
   This asks the column, not the screen, because side panes take width. */
@container cardcol (min-aspect-ratio: 5/4) and (min-width: 640px) {
  #card.has-image {
    max-width: 1280px;
    height: 100%;
    display: grid;
    grid-template-columns: minmax(0, 3fr) minmax(300px, 2fr);
    grid-template-rows: auto minmax(0, 1fr) auto;
    grid-template-areas: "img head" "img content" "img actions";
    column-gap: var(--pad);
  }
  #card.has-image .card-head { grid-area: head; }
  #card.has-image .shot { grid-area: img; height: 100%; min-height: 0; }
  #card.has-image .card-content { grid-area: content; }
  #card.has-image .card-actions { grid-area: actions; }
}

/* ---- Updates: one post per screen ---- */
#updates-view { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.reel { flex: 1; min-height: 0; overflow-y: auto; scroll-snap-type: y mandatory; overscroll-behavior: contain; }
.slide {
  height: 100%;
  scroll-snap-align: start;
  scroll-snap-stop: always;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  padding: 0 var(--pad) var(--pad);
}
.slide-img {
  flex: 1 1 0;
  min-height: 0;
  width: 100%;
  object-fit: contain;
  border-radius: var(--radius);
  background: var(--card);
  border: 1px solid var(--line);
  cursor: zoom-in;
}
.caption { flex: none; max-height: 45%; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; }
.caption h2 { overflow-wrap: anywhere; }
.post-body {
  margin: 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  cursor: pointer;
}
.post-body.open { display: block; overflow: visible; }
.ask { display: flex; flex-wrap: wrap; gap: 8px; align-items: flex-end; }
.ask textarea { flex: 1 1 100%; }
.ask-btn { align-self: flex-start; padding-left: 0; }
.seen-mark { display: none; }
.seen .seen-mark { display: inline; }
.seen .ask-btn { display: none; }
.reel-end { justify-content: center; }
/* Wider than tall: the image beside its caption, using the whole width. */
@media (min-aspect-ratio: 5/4) and (min-width: 640px) {
  .slide { display: grid; grid-template-columns: minmax(0, 1fr) clamp(280px, 30vw, 440px); align-items: stretch; padding-top: 4px; }
  .slide-img { height: 100%; }
  .caption { max-height: 100%; justify-content: flex-end; }
  .reel-end { display: flex; }
}

/* ---- History: decided cards, as a project stream or as part_of stacks ---- */
#history-view, #history-detail { display: flex; flex-direction: column; gap: var(--gap); width: 100%; max-width: 720px; margin: 0 auto; padding-bottom: var(--pad); }
#history-view.two { max-width: 1200px; display: grid; grid-template-columns: minmax(260px, 360px) minmax(0, 1fr); align-items: start; column-gap: var(--pad); }
#history-view.two #history-head { grid-column: 1 / -1; display: flex; flex-direction: column; gap: var(--gap); }
#history-view.two #history-detail { grid-column: 2; grid-row: 2; max-width: none; position: sticky; top: 0; }
.modes { display: flex; gap: 6px; }
.hist-title { font-weight: 700; overflow-wrap: anywhere; }
.hist-card[data-depth="1"] { margin-left: 14px; }
.hist-card[data-depth="2"] { margin-left: 28px; }
.hist-card[data-depth="3"] { margin-left: 42px; }
.hist-open { color: var(--warn); }

/* ---- Agents ---- */
#agents-view, #agent-detail { display: flex; flex-direction: column; gap: var(--gap); width: 100%; max-width: 720px; margin: 0 auto; padding-bottom: var(--pad); }
/* Two columns: the list stays beside the open agent. */
#agents-view.two { max-width: 1200px; display: grid; grid-template-columns: minmax(260px, 360px) minmax(0, 1fr); align-items: start; column-gap: var(--pad); }
#agents-view.two #no-agents { grid-column: 1 / -1; }
#agents-view.two #agent-detail { grid-column: 2; grid-row: 1 / span 3; max-width: none; position: sticky; top: 0; }
.row.current { border-color: var(--accent); box-shadow: inset 3px 0 0 var(--accent); }
#agents-view .muted { text-align: left; }
.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.row { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; text-align: left; width: 100%; display: block; font-weight: 400; }
.row .name { font-weight: 700; }
.row .dot { margin-right: 8px; }
.row .sub { color: var(--muted); font-size: 0.85em; overflow-wrap: anywhere; }
.badge { float: right; background: var(--accent); color: var(--card); border-radius: 999px; padding: 0 8px; font-size: 0.8em; font-weight: 700; }
#agent-back { align-self: flex-start; padding-left: 0; }
#send { display: flex; flex-direction: column; gap: 10px; }
.msg { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 16px; }
.msg .kind { font-size: 0.8em; color: var(--muted); }
.msg .reply { margin-top: 6px; border-left: 3px solid var(--accent); padding-left: 10px; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg .text { white-space: pre-wrap; overflow-wrap: anywhere; }

/* ---- Server status sheet: from the bottom on a phone, centred on a big screen ---- */
.sheet { position: fixed; inset: 0; z-index: 9; display: flex; align-items: flex-end; justify-content: center; background: transparent; border: 0; padding: 0; margin: 0; width: 100%; height: 100%; max-width: none; max-height: none; color: var(--fg); }
.sheet::backdrop { background: rgb(0 0 0 / .35); }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; }
.sheet-body {
  width: 100%;
  max-width: 560px;
  max-height: 90dvh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  padding: var(--pad);
  padding-bottom: max(var(--pad), env(safe-area-inset-bottom));
  background: var(--card);
  border-radius: var(--radius) var(--radius) 0 0;
}
@media (min-width: 640px) and (min-height: 500px) {
  .sheet { align-items: center; }
  .sheet-body { border-radius: var(--radius); }
}
/* Short and wide (a phone on its side): a full-height panel on the right. */
@media (min-width: 640px) and (max-height: 499px) {
  .sheet { justify-content: flex-end; align-items: stretch; }
  .sheet-body { max-height: none; height: 100%; max-width: 420px; border-radius: var(--radius) 0 0 var(--radius); }
}
.sheet-head { display: flex; justify-content: space-between; align-items: center; }
.status-conn { margin: 0; display: flex; align-items: center; gap: 8px; font-weight: 600; }
.status-list { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; font-size: 0.9em; }
.status-list dt { color: var(--muted); }
.status-list dd { margin: 0; overflow-wrap: anywhere; }
.status-list .warn { color: var(--warn); }

/* ---- Undo bar and full-size image ---- */
.toast {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: max(16px, env(safe-area-inset-bottom));
  width: calc(100% - 2 * var(--pad));
  max-width: 568px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 4px 4px 16px;
  border-radius: var(--radius);
  background: var(--fg);
  color: var(--bg);
  box-shadow: 0 4px 16px rgb(0 0 0 / .2);
  z-index: 8;
}
.toast span { flex: 1; overflow-wrap: anywhere; }
.toast button.ghost { color: var(--bg); text-decoration: underline; }
.viewer { position: fixed; inset: 0; z-index: 10; overflow: auto; background: rgb(0 0 0 / .92); cursor: zoom-out; }
.viewer img { display: block; max-width: none; margin: auto; min-height: 100%; object-fit: contain; }

/* ---- How much to show ----
 *
 * <main> is a size container, so these rules follow the space the page
 * actually gets (a phone held sideways is short; a desktop window may be
 * narrow). Three steps:
 *   compact  - small or short: the card alone; who asked and when are left to
 *              the Context pane, context lines shrink to one line;
 *   regular  - the full card (the default rules above);
 *   wide     - room beside the card: a Context pane on the right (who asked,
 *              what it unblocks, the rest of the project); wider still, the
 *              Queue on the left.
 */
main { container: main / size; }
#cards-view { flex: 1; min-height: 0; display: flex; justify-content: center; gap: var(--pad); width: 100%; }
#cards-view[hidden] { display: none; }
/* The card starts at the top, level with the Queue and Context panes, so the
   eye does not have to find it in the middle of a tall screen. */
.card-column { flex: 1 1 auto; min-width: 0; min-height: 0; display: flex; flex-direction: column; justify-content: flex-start; padding-top: var(--pad); container: cardcol / size; }
.pane { display: none; }

@container main (max-width: 340px) or (max-height: 600px) {
  #agent, #age { display: none; }
  .context { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  #unblocks { font-size: 0.8em; }
}
@container main (max-height: 460px) {
  .next { display: none; }
}

@container main (min-width: 960px) and (min-height: 520px) {
  #context-pane { display: flex; }
  .card-column { max-width: 900px; }
}
@container main (min-width: 1240px) and (min-height: 520px) {
  #queue-pane { display: flex; }
  .next { display: none; } /* the Queue pane shows what comes next */
}
/* With a mouse and room to spare, answer buttons show their keyboard key. */
@media (pointer: fine) and (min-width: 960px) {
  #actions button[data-key]::after {
    content: attr(data-key);
    margin-left: 0.6em;
    padding: 0 0.4em;
    border: 1px solid currentColor;
    border-radius: 4px;
    font-size: 0.75em;
    font-weight: 400;
    opacity: 0.6;
  }
}
.pane {
  flex: 0 0 clamp(240px, 22vw, 320px);
  min-height: 0;
  flex-direction: column;
  gap: 8px;
  padding: var(--pad) 0;
  overflow-y: auto;
  overscroll-behavior: contain;
}
.pane-title { font-size: 0.8em; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.queue { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.qitem {
  width: 100%;
  min-height: 0;
  text-align: left;
  font-weight: 400;
  padding: 8px 10px;
  background: transparent;
  border-color: transparent;
}
.qitem:hover { background: var(--card); border-color: var(--line); }
.qitem.current { background: var(--card); border-color: var(--accent); box-shadow: inset 3px 0 0 var(--accent); }
.qmeta { display: flex; flex-wrap: wrap; gap: 2px 8px; font-size: 0.75em; color: var(--muted); }
.qkind { font-weight: 700; color: var(--accent); }
.qurg { font-weight: 700; color: var(--no); }
.qtitle { overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.ctx { display: flex; flex-direction: column; gap: 4px; padding-bottom: 10px; border-bottom: 1px solid var(--line); }
.ctx:last-child { border-bottom: 0; }
.ctx h3 { margin: 0; font-size: 0.8em; color: var(--muted); font-weight: 600; }
.ctx-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; font-size: 0.9em; overflow-wrap: anywhere; }
.ctx-agent { font-weight: 700; display: flex; align-items: center; gap: 8px; }
.ctx-link { align-self: flex-start; padding-left: 0; min-height: 0; }
.ctx-posts { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.ctx-post { display: flex; flex-direction: column; gap: 4px; padding: 0; min-height: 0; font-weight: 400; font-size: 0.75em; text-align: left; background: transparent; border: 0; color: var(--fg); }
.ctx-post img { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; border-radius: 8px; border: 1px solid var(--line); background: var(--card); }
.ctx-post span { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.muted-left { color: var(--muted); margin: 0; font-size: 0.9em; }

/* ---- Notifications, in the server status sheet ---- */
.notify { display: flex; flex-direction: column; gap: 8px; padding-top: var(--gap); border-top: 1px solid var(--line); }
.notify p { margin: 0; }

/* ---- Reading aloud ---- */
.meta .listen { min-height: 0; padding: 0 6px; font-size: 1em; margin-left: auto; }
.read-aloud { display: flex; align-items: center; gap: 8px; padding-top: var(--gap); border-top: 1px solid var(--line); }
.read-aloud input { min-height: 0; width: 1.1em; height: 1.1em; }
.sub.session { overflow-wrap: anywhere; }
#ad-sessions { display: flex; flex-direction: column; gap: 2px; color: var(--muted); }

/* ---- Phone: the card scrolls as a whole ----
 * Held to the screen height, a card on a phone gave the body whatever the
 * title and buttons left over: with large phone text that was a line or two,
 * and the answer row slid out of the card onto the "Next" line. On a narrow
 * column the card takes its natural height and the column scrolls instead,
 * like a feed. The speaker line stays: on a phone it is how the owner tells
 * which window is asking.
 */
@container main (max-width: 520px) {
  .card-column { justify-content: flex-start; overflow-y: auto; overscroll-behavior: contain; padding-bottom: var(--pad); }
  #card { flex: none; }
  .card-content { flex: none; overflow: visible; }
  .shot { max-height: 45vh; }
  #title { font-size: clamp(18px, 0.95rem + 1vw, 22px); }
  #actions { grid-template-columns: 1fr; }
  #actions button { min-height: var(--tap); padding: 0.45em 0.8em; }
  #agent { display: inline; }
  .next { white-space: normal; }
  .secondary { display: flex; flex-wrap: wrap; justify-content: flex-end; }
  .secondary select { flex: 1 1 100%; }
}
