/* ============================================================================
   Vellum — viewer shell styles (trusted shell only; NOT iframe content).
   Warm "vellum/paper" palette borrowed from the product proposal: off-white
   paper, ink text, a restrained terracotta accent, serif display headings.
   ========================================================================== */

/* The `hidden` attribute must beat author `display` rules. Without this,
   elements toggled via element.hidden (composer, sign-in CTA, toast) that also
   carry a `display: flex/block` rule never actually hide. */
[hidden] {
  display: none !important;
}

:root {
  --ink: #1a1714;
  --ink-soft: #4a423b;
  --muted: #8b8178;
  --paper: #f6f1e7;
  --card: #fffdf8;
  --card-2: #fbf6ec;
  --line: #e3dccd;
  --line-soft: #efe8d9;
  --accent: #9a4d2e;
  --accent-soft: #c97a4e;
  --accent-wash: #f7ede2;
  --good: #3f6f4f;
  --good-wash: #e9f1ea;
  --danger: #8a3324;

  --shadow-sm: 0 1px 0 rgba(0, 0, 0, 0.03), 0 6px 16px -12px rgba(60, 40, 20, 0.4);
  --shadow: 0 1px 0 rgba(0, 0, 0, 0.03), 0 12px 30px -18px rgba(60, 40, 20, 0.35);

  --mono: "SF Mono", ui-monospace, "JetBrains Mono", Menlo, monospace;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, system-ui, sans-serif;

  --bar-h: 60px;
  --sidebar-w: 380px;
  --radius: 14px;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background:
    radial-gradient(1200px 500px at 80% -10%, #fbf6ec, transparent),
    var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ----------------------------------------------------------------- App bar */

.app-bar {
  height: var(--bar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 0 20px;
  background: linear-gradient(180deg, #fffdf8, #fbf6ec);
  border-bottom: 1px solid var(--line);
  box-shadow: var(--shadow-sm);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.mark {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 9px;
  background: var(--accent);
  color: #fff;
  font-family: var(--serif);
  font-size: 19px;
  font-weight: 700;
  line-height: 1;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.25), var(--shadow-sm);
  flex: none;
}
.mark.big { width: 56px; height: 56px; font-size: 32px; border-radius: 14px; }

.brand-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
  line-height: 1.15;
}
.brand-name {
  font-family: var(--serif);
  font-size: 18px;
  letter-spacing: -0.01em;
  color: var(--ink);
}
.brand-page {
  font: 600 11px/1.3 var(--mono);
  letter-spacing: 0.04em;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 38vw;
}

.bar-tools {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

/* Version picker */
.version-picker {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 5px 6px 5px 14px;
  box-shadow: var(--shadow-sm);
}
.vp-label {
  font: 600 10px/1 var(--mono);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}
.version-picker select {
  appearance: none;
  -webkit-appearance: none;
  border: 0;
  background:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='7' viewBox='0 0 10 7'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%239a4d2e' stroke-width='1.5' fill='none' stroke-linecap='round'/%3E%3C/svg%3E")
      no-repeat right 12px center;
  background-color: var(--card-2);
  border-radius: 999px;
  padding: 7px 30px 7px 12px;
  font: 600 13px/1 var(--sans);
  color: var(--ink);
  cursor: pointer;
  max-width: 240px;
}
.version-picker select:focus-visible {
  outline: 2px solid var(--accent-soft);
  outline-offset: 1px;
}

.who-wrap { display: inline-flex; align-items: center; gap: 10px; }
.who {
  font: 600 12px/1 var(--mono);
  color: var(--ink-soft);
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Buttons */
.primary-btn,
.ghost-btn,
.link-btn {
  font-family: var(--sans);
  cursor: pointer;
  border-radius: 9px;
  transition: background-color 0.15s ease, border-color 0.15s ease,
    transform 0.05s ease;
}
.primary-btn {
  background: var(--accent);
  color: #fff;
  border: 1px solid var(--accent);
  padding: 9px 16px;
  font-size: 13px;
  font-weight: 650;
  box-shadow: var(--shadow-sm);
}
.primary-btn:hover { background: #8a4226; }
.primary-btn:active { transform: translateY(1px); }
.primary-btn:disabled { opacity: 0.55; cursor: default; }

.ghost-btn {
  background: var(--card);
  color: var(--ink-soft);
  border: 1px solid var(--line);
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
}
.ghost-btn:hover { background: var(--card-2); border-color: var(--accent-soft); }

.link-btn {
  background: none;
  border: 0;
  color: var(--accent);
  padding: 2px 4px;
  font-size: 12px;
  font-weight: 600;
}
.link-btn:hover { text-decoration: underline; }

/* ----------------------------------------------------------- Stale banner */
.stale-banner {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  padding: 9px 20px;
  font-size: 13px;
  color: var(--ink-soft);
  background: linear-gradient(180deg, #fff8ec, #fffdf8);
  border-bottom: 1px solid var(--line);
}
.stale-banner b { color: var(--accent); }

/* ---------------------------------------------------------------- Stage */
.stage {
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--sidebar-w);
  height: calc(100vh - var(--bar-h));
  height: calc(100dvh - var(--bar-h));
}
body:has(.stale-banner) .stage {
  height: auto;
  min-height: calc(100vh - var(--bar-h));
}

.canvas {
  padding: 22px;
  min-width: 0;
  min-height: 0;
}
.agent-frame {
  width: 100%;
  height: 100%;
  min-height: 420px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  box-shadow: var(--shadow);
  display: block;
}

/* ---------------------------------------------------------------- Sidebar */
.sidebar {
  border-left: 1px solid var(--line);
  background: linear-gradient(180deg, #fffdf8, #faf4e9);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.sidebar-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  padding: 20px 20px 0;
}
.sidebar-head h2 {
  font-family: var(--serif);
  font-size: 24px;
  letter-spacing: -0.01em;
  margin: 0;
}
.version-pill {
  font: 600 11px/1 var(--mono);
  letter-spacing: 0.04em;
  color: var(--accent);
  background: var(--accent-wash);
  border: 1px solid #ecd9c8;
  padding: 5px 9px;
  border-radius: 999px;
  flex: none;
}
.sidebar-sub {
  margin: 6px 20px 0;
  font-size: 12.5px;
  color: var(--muted);
}

.thread-list {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 16px 20px 8px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
}

.loading,
.empty-state {
  color: var(--muted);
  font-size: 13px;
  text-align: center;
  padding: 28px 12px;
}
.empty-state .emoji { font-size: 22px; display: block; margin-bottom: 6px; }

/* Comment thread cards */
.thread {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow-sm);
  padding: 14px 14px 12px;
}
.thread.resolved { opacity: 0.72; background: var(--card-2); }

.comment-head {
  display: flex;
  align-items: center;
  gap: 9px;
  margin-bottom: 7px;
}
.avatar {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  flex: none;
  font: 700 11px/1 var(--sans);
  color: #fff;
  background: var(--accent-soft);
  text-transform: uppercase;
}
.comment-meta { min-width: 0; line-height: 1.25; }
.comment-author {
  font-size: 13px;
  font-weight: 650;
  color: var(--ink);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.comment-time { font: 500 11px/1 var(--mono); color: var(--muted); }
.resolved-tag {
  margin-left: auto;
  font: 600 9px/1 var(--mono);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--good);
  background: var(--good-wash);
  border: 1px solid #d4e3d6;
  padding: 4px 7px;
  border-radius: 999px;
  flex: none;
}

.comment-body {
  font-size: 14px;
  color: var(--ink-soft);
  white-space: pre-wrap;
  word-break: break-word;
  margin: 0;
}

.comment-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 9px;
}

/* Replies */
.replies {
  margin-top: 12px;
  padding-left: 12px;
  border-left: 2px solid var(--line-soft);
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.reply .avatar { width: 22px; height: 22px; font-size: 10px; }
.reply .comment-author { font-size: 12.5px; }

/* Inline reply composer */
.reply-form {
  margin-top: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.reply-form textarea,
.composer textarea {
  width: 100%;
  resize: vertical;
  font-family: var(--sans);
  font-size: 13.5px;
  line-height: 1.45;
  color: var(--ink);
  background: var(--card-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 11px;
}
.reply-form textarea:focus,
.composer textarea:focus {
  outline: none;
  border-color: var(--accent-soft);
  background: #fffaf0;
}
.reply-form-actions { display: flex; gap: 8px; }

/* Composer at sidebar bottom */
.composer {
  border-top: 1px solid var(--line);
  background: var(--card);
  padding: 14px 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  flex: none;
}
.composer textarea { rows: 3; min-height: 64px; }
.composer-actions { display: flex; justify-content: flex-end; gap: 8px; }

/* ------------------------------------------------------------------ Markup */

/* Pending-selection chip above the composer (the passage being marked up). */
.anchor-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 9px;
  background: var(--accent-wash);
  border: 1px solid #ecd9c7;
  border-radius: 9px;
  font-size: 12.5px;
}
.anchor-chip-label {
  font: 600 9px/1 var(--mono);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--accent);
  flex: none;
}
.anchor-chip-quote {
  min-width: 0;
  flex: 1;
  color: var(--ink-soft);
  font-style: italic;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.anchor-chip-clear {
  flex: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 17px;
  line-height: 1;
  cursor: pointer;
  padding: 0 2px;
}
.anchor-chip-clear:hover { color: var(--accent); }

/* Quoted passage shown on an anchored comment; clicks scroll to the highlight. */
.anchor-quote {
  display: block;
  width: 100%;
  text-align: left;
  margin: 0 0 8px;
  padding: 6px 9px;
  background: var(--accent-wash);
  border: 1px solid #ecd9c7;
  border-left: 3px solid var(--accent-soft);
  border-radius: 7px;
  color: var(--ink-soft);
  font: italic 12.5px/1.4 var(--serif);
  cursor: pointer;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.anchor-quote:hover { background: #f4e4d4; text-decoration: none; }

/* Brief flash when a thread is focused from a highlight click in the doc. */
.thread.flash { animation: thread-flash 1.2s ease; }
@keyframes thread-flash {
  0% { box-shadow: 0 0 0 2px var(--accent-soft); }
  100% { box-shadow: var(--shadow-sm); }
}

.signin-cta {
  border-top: 1px solid var(--line);
  background: var(--card);
  padding: 16px 20px 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: flex-start;
  flex: none;
}
.signin-copy { margin: 0; font-size: 13px; color: var(--ink-soft); }

/* Inline sign-in panel (email prompt) injected by app.js */
.signin-panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
}
.signin-panel input[type="email"] {
  width: 100%;
  font-family: var(--sans);
  font-size: 13.5px;
  color: var(--ink);
  background: var(--card-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 11px;
}
.signin-panel input[type="email"]:focus {
  outline: none;
  border-color: var(--accent-soft);
  background: #fffaf0;
}
.signin-panel .row { display: flex; gap: 8px; }
.signin-note {
  font-size: 12.5px;
  color: var(--ink-soft);
  background: var(--accent-wash);
  border: 1px solid #ecd9c8;
  border-radius: 10px;
  padding: 10px 12px;
  line-height: 1.45;
}
.signin-note .dev-link {
  display: block;
  margin-top: 6px;
  font: 500 11.5px/1.4 var(--mono);
  word-break: break-all;
}

/* ---------------------------------------------------------------- Toast */
.toast {
  position: fixed;
  bottom: 22px;
  left: 50%;
  transform: translateX(-50%) translateY(8px);
  background: var(--ink);
  color: #fdf8ee;
  font-size: 13px;
  font-weight: 550;
  padding: 11px 18px;
  border-radius: 999px;
  box-shadow: 0 14px 34px -14px rgba(0, 0, 0, 0.5);
  z-index: 60;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  max-width: 86vw;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { background: var(--danger); }

/* ---------------------------------------------------------------- 404 */
.error-page {
  display: grid;
  place-items: center;
  min-height: 100vh;
  padding: 24px;
}
.error-card {
  text-align: center;
  max-width: 440px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 44px 36px;
  box-shadow: var(--shadow);
}
.error-card .mark { margin: 0 auto 18px; }
.error-card h1 {
  font-family: var(--serif);
  font-size: 40px;
  letter-spacing: -0.02em;
  margin: 0 0 10px;
}
.error-card p { color: var(--ink-soft); font-size: 15px; margin: 0; }
.error-card code {
  font: 500 13px/1 var(--mono);
  background: var(--card-2);
  border: 1px solid var(--line);
  padding: 2px 6px;
  border-radius: 6px;
}

/* ---------------------------------------------------------------- Scrollbar */
.thread-list::-webkit-scrollbar { width: 10px; }
.thread-list::-webkit-scrollbar-thumb {
  background: #e0d7c5;
  border: 3px solid transparent;
  background-clip: padding-box;
  border-radius: 999px;
}

/* ============================================================ Responsive */
@media (max-width: 900px) {
  :root { --sidebar-w: 340px; }
}

@media (max-width: 760px) {
  /* Sidebar collapses BELOW the iframe on narrow screens. */
  .stage {
    grid-template-columns: 1fr;
    height: auto;
    min-height: 0;
  }
  .canvas { padding: 14px; }
  .agent-frame { height: 64vh; min-height: 360px; }
  .sidebar {
    border-left: 0;
    border-top: 1px solid var(--line);
    max-height: none;
  }
  .thread-list { overflow: visible; }
  .brand-page { max-width: 30vw; }
  .bar-tools { gap: 10px; }
  .who { display: none; }
}

@media (max-width: 460px) {
  .app-bar { flex-wrap: wrap; height: auto; padding: 10px 14px; gap: 10px; }
  .stage { height: auto; }
  .version-picker select { max-width: 150px; }
}

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

/* ============================================================ Gallery
   Signed-in home: "Your artifacts". Visual preview tiles by default with a
   grid/list toggle. Thumbnails are sandboxed iframes pointed at /raw, zoomed
   out via a transform so a full artifact reads at tile scale. */

.gallery-page { min-height: 100dvh; }

/* Inline logout form inside the app bar shouldn't add layout. */
.app-bar form { margin: 0; display: inline-flex; }

/* Grid / list toggle */
.view-toggle {
  display: inline-flex;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px;
  box-shadow: var(--shadow-sm);
}
.vt-btn {
  display: grid;
  place-items: center;
  width: 32px;
  height: 28px;
  border: 0;
  border-radius: 999px;
  background: none;
  color: var(--muted);
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease;
}
.vt-btn svg { fill: currentColor; }
.vt-btn:hover { color: var(--ink-soft); }
.vt-btn[aria-pressed="true"] {
  background: var(--accent-wash);
  color: var(--accent);
}

.gallery-main {
  max-width: 1180px;
  margin: 0 auto;
  padding: 28px 22px 56px;
}

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 20px;
}

/* Tile */
.tile {
  --z: 0.3; /* thumbnail zoom-out: artifact viewport ≈ tile-width / z */
  display: flex;
  flex-direction: column;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
  color: var(--ink);
  text-decoration: none;
  transition: box-shadow 0.15s ease, transform 0.08s ease, border-color 0.15s ease;
}
.tile:hover {
  text-decoration: none;
  border-color: var(--accent-soft);
  box-shadow: var(--shadow);
  transform: translateY(-2px);
}
.tile:focus-visible {
  outline: 2px solid var(--accent-soft);
  outline-offset: 2px;
}

.tile-preview {
  position: relative;
  display: block;
  aspect-ratio: 4 / 3;
  overflow: hidden;
  background: #fff;
  border-bottom: 1px solid var(--line);
}
.tile-frame {
  position: absolute;
  top: 0;
  left: 0;
  width: calc(100% / var(--z));
  height: calc(100% / var(--z));
  transform: scale(var(--z));
  transform-origin: top left;
  border: 0;
  background: #fff;
  pointer-events: none; /* clicks fall through to the tile anchor */
}
.tile-empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font: 600 12px/1 var(--mono);
  letter-spacing: 0.04em;
  color: var(--muted);
  background: var(--card-2);
}
/* Subtle top sheen so busy artifacts don't fight the card chrome. */
.tile-scrim {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: linear-gradient(180deg, rgba(255, 253, 248, 0.12), transparent 22%);
}

.tile-body {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 12px 14px 13px;
  min-width: 0;
}
.tile-title {
  font-family: var(--serif);
  font-size: 16px;
  letter-spacing: -0.01em;
  color: var(--ink);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tile-meta {
  font: 600 11px/1.3 var(--mono);
  letter-spacing: 0.02em;
  color: var(--accent);
}
.tile-time { font: 500 11px/1.3 var(--mono); color: var(--muted); }

/* ---- List layout (body.view-list) ---- */
body.view-list .gallery { grid-template-columns: 1fr; gap: 10px; }
body.view-list .tile {
  flex-direction: row;
  align-items: stretch;
}
body.view-list .tile:hover { transform: none; }
body.view-list .tile-preview {
  flex: none;
  width: 132px;
  aspect-ratio: auto;
  border-bottom: 0;
  border-right: 1px solid var(--line);
}
body.view-list .tile-body {
  flex: 1 1 auto;
  flex-direction: row;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
}
body.view-list .tile-title { flex: 1 1 auto; font-size: 15px; }
body.view-list .tile-meta,
body.view-list .tile-time { flex: none; }
body.view-list .tile-time {
  width: 84px;
  text-align: right;
}

/* ---- Empty state ---- */
.gallery-empty {
  text-align: center;
  max-width: 440px;
  margin: 8vh auto 0;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 44px 36px;
  box-shadow: var(--shadow);
}
.gallery-empty .mark { margin: 0 auto 18px; }
.gallery-empty h2 {
  font-family: var(--serif);
  font-size: 28px;
  letter-spacing: -0.01em;
  margin: 0 0 8px;
}
.gallery-empty p { color: var(--ink-soft); font-size: 14.5px; margin: 0 0 18px; }
.gallery-cmd {
  margin: 0;
  text-align: left;
  background: var(--card-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px;
  overflow-x: auto;
}
.gallery-cmd code {
  font: 500 12.5px/1.5 var(--mono);
  color: var(--ink-soft);
  white-space: pre;
}

@media (max-width: 520px) {
  .gallery { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 14px; }
  .gallery-main { padding: 18px 14px 40px; }
  body.view-list .tile-preview { width: 92px; }
  body.view-list .tile-body { flex-wrap: wrap; gap: 4px 12px; }
  body.view-list .tile-time { width: auto; text-align: left; }
}
