/*
 * PTSB Builder — mobile-first dark theme.
 *
 * Layout is a fixed viewport (100dvh) with a scrolling thread between a pinned
 * topbar and a pinned composer. dvh rather than vh because iOS Safari's URL bar
 * collapses and vh does not follow it, which would otherwise hide the composer.
 */

:root {
  --bg: #0b1020;
  --bg-2: #121a33;
  --bg-3: #18213f;
  --line: #243156;
  --fg: #e8ecf7;
  --fg-dim: #98a3c4;
  --accent: #4b8cff;
  --accent-2: #7bd0a5;
  --danger: #ff6b6b;
  --warn: #ffb454;
  --radius: 14px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--sans);
  font-size: 16px; /* < 16px makes iOS zoom on focus */
  line-height: 1.5;
  overscroll-behavior: none;
}

[hidden] {
  display: none !important;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
}

/* ── Brand ──────────────────────────────────────────────────────────────── */

.brand-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.brand-row--lg {
  margin-bottom: 18px;
}

/* No fixed height here. Under border-box a height plus padding-top leaves a
   content box shorter than the logos, which silently crops them. Let the row
   size to its tallest child and control spacing with padding alone. */
.brand-row--foot {
  gap: 10px;
  padding: 8px 0 2px;
  opacity: 0.45;
}

.brand-row--foot .brand-div {
  height: 12px;
}

.brand-row--lg .brand-div {
  height: 34px;
}

.brand-div {
  width: 1px;
  height: 18px;
  background: var(--line);
}

.logo {
  display: block;
  object-fit: contain;
}

.logo-ptsb {
  height: 24px;
}
/* The Ardowen asset is a vertical lockup (glyph over wordmark), so it needs
   roughly double the height of the others to read at the same optical weight.
   The footer uses ardowen-mark.png — the glyph alone — which does not. */
.logo-ardowen {
  height: 46px;
}
/* No `color` here — bt-logo.svg is an <img>, so it cannot inherit it. The mark's
   colour is set inside the file itself. */
.logo-bt {
  height: 22px;
}
.logo.sm {
  height: 18px;
}
.logo.xs {
  height: 16px;
}
.brand-row--foot .logo-bt {
  height: 14px;
}

/* ── Login ──────────────────────────────────────────────────────────────── */

.login {
  display: grid;
  place-items: center;
  height: 100vh;
  height: 100dvh;
  padding: 24px;
  background: radial-gradient(120% 90% at 50% 0%, #16224a 0%, var(--bg) 60%);
}

.login-card {
  width: min(380px, 100%);
  padding: 28px 24px calc(24px + var(--safe-b));
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 18px;
  text-align: center;
}

.login-card h1 {
  margin: 0;
  font-size: 22px;
  letter-spacing: -0.01em;
}

.login-card input {
  width: 100%;
  margin: 18px 0 12px;
  padding: 13px 14px;
  font: inherit;
  color: var(--fg);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 12px;
}

.login-card input:focus {
  outline: none;
  border-color: var(--accent);
}

.login-card button {
  width: 100%;
  padding: 13px;
  font-weight: 600;
  color: #06122b;
  background: var(--accent);
  border: 0;
  border-radius: 12px;
}

.login-card button:disabled {
  opacity: 0.55;
}

.muted {
  margin: 4px 0 0;
  color: var(--fg-dim);
  font-size: 14px;
}
.tiny {
  font-size: 12px;
  margin-top: 14px;
}
.error {
  margin: 12px 0 0;
  color: var(--danger);
  font-size: 14px;
}

/* ── App shell ──────────────────────────────────────────────────────────── */

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(var(--safe-t) + 8px) 10px 8px;
  background: rgba(11, 16, 32, 0.9);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(10px);
}

.topbar-title {
  display: flex;
  flex: 1;
  align-items: center;
  gap: 8px;
  min-width: 0;
  font-size: 15px;
  font-weight: 600;
}

#session-title {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  color: var(--fg-dim);
  background: transparent;
  border: 0;
  border-radius: 10px;
  flex: none;
}

.icon-btn:active {
  background: var(--bg-3);
}

.icon-btn[aria-pressed='true'] {
  color: var(--accent-2);
}

/* The speaker "waves" only make sense when read-aloud is on. */
#tts-btn[aria-pressed='false'] #tts-waves {
  opacity: 0.25;
}

.icon-btn.mic.listening {
  color: var(--danger);
  animation: pulse 1.2s ease-in-out infinite;
}

@keyframes pulse {
  50% {
    opacity: 0.4;
  }
}

/* ── Drawer ─────────────────────────────────────────────────────────────── */

.drawer-scrim {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(0, 0, 0, 0.55);
}

.drawer {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 21;
  display: flex;
  flex-direction: column;
  width: min(320px, 86vw);
  height: 100vh;
  height: 100dvh;
  padding: calc(var(--safe-t) + 14px) 14px calc(var(--safe-b) + 14px);
  background: var(--bg-2);
  border-right: 1px solid var(--line);
}

.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line);
}

.session-list {
  flex: 1;
  margin: 8px -6px;
  padding: 0 6px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.session-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 10px 10px;
  margin-bottom: 4px;
  text-align: left;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 10px;
}

.session-item.active {
  background: var(--bg-3);
  border-color: var(--line);
}

.session-item .si-main {
  flex: 1;
  min-width: 0;
}

.session-item .si-title {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  font-size: 14px;
}

.session-item .si-date {
  color: var(--fg-dim);
  font-size: 12px;
}

.si-del {
  flex: none;
  padding: 4px 8px;
  color: var(--fg-dim);
  background: transparent;
  border: 0;
  font-size: 16px;
}

.drawer-foot {
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.drawer-foot .meta {
  margin-bottom: 10px;
  color: var(--fg-dim);
  font-size: 12px;
  font-family: var(--mono);
  line-height: 1.6;
}

.ghost-btn {
  padding: 8px 12px;
  color: var(--fg);
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 14px;
}

.ghost-btn.danger {
  width: 100%;
  color: var(--danger);
}

/* ── Thread ─────────────────────────────────────────────────────────────── */

.thread {
  flex: 1;
  padding: 14px 12px 4px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.msg {
  max-width: 44rem;
  margin: 0 auto 14px;
}

/* A long press must reach our own menu, so the iOS callout and the text
   selection it starts are suppressed on bubbles — but only on bubbles. Code
   blocks keep native selection, which is more useful there than a Copy button,
   and the long-press handler skips them for the same reason. */
.msg .bubble {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

.msg .bubble pre,
.msg .bubble pre * {
  -webkit-touch-callout: default;
  -webkit-user-select: text;
  user-select: text;
}

/* Images sent with a message. */
.msg-images {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}

.msg-images img {
  max-width: 180px;
  max-height: 180px;
  border-radius: 10px;
  border: 1px solid rgba(0, 0, 0, 0.25);
}

.msg-imgnote {
  margin-bottom: 6px;
  font-size: 12px;
  opacity: 0.7;
}

.msg.user .bubble {
  margin-left: auto;
  max-width: 88%;
  padding: 10px 14px;
  background: var(--accent);
  color: #06122b;
  border-radius: 16px 16px 4px 16px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.msg.assistant .bubble {
  padding: 2px 2px;
  overflow-wrap: anywhere;
}

.msg.system .bubble {
  padding: 8px 12px;
  color: var(--fg-dim);
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 13px;
}

.msg.system.error .bubble {
  color: var(--danger);
  border-color: rgba(255, 107, 107, 0.4);
}

/* Minimal markdown rendering — enough for the answers this agent gives. */
.bubble p {
  margin: 0 0 10px;
}
.bubble p:last-child {
  margin-bottom: 0;
}
.bubble ul,
.bubble ol {
  margin: 0 0 10px;
  padding-left: 22px;
}
.bubble li {
  margin-bottom: 4px;
}
.bubble h1,
.bubble h2,
.bubble h3 {
  margin: 16px 0 8px;
  font-size: 16px;
}
.bubble code {
  padding: 1px 5px;
  background: var(--bg-3);
  border-radius: 5px;
  font-family: var(--mono);
  font-size: 0.88em;
}
.bubble pre {
  margin: 0 0 10px;
  padding: 12px;
  overflow-x: auto;
  background: #060a16;
  border: 1px solid var(--line);
  border-radius: 10px;
}
.bubble pre code {
  padding: 0;
  background: none;
  font-size: 13px;
  line-height: 1.45;
}
.bubble a {
  color: var(--accent);
}
.bubble blockquote {
  margin: 0 0 10px;
  padding-left: 12px;
  border-left: 3px solid var(--line);
  color: var(--fg-dim);
}

/* The blinking caret while tokens are still arriving. */
.streaming::after {
  content: '▍';
  color: var(--accent);
  animation: blink 1s steps(2, start) infinite;
}

@keyframes blink {
  50% {
    opacity: 0;
  }
}

/* ── Tool + thinking blocks ─────────────────────────────────────────────── */

.tool,
.think {
  max-width: 44rem;
  margin: 0 auto 8px;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
}

.tool > summary,
.think > summary {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 12px;
  color: var(--fg-dim);
  font-size: 13px;
  cursor: pointer;
  list-style: none;
}

.tool > summary::-webkit-details-marker,
.think > summary::-webkit-details-marker {
  display: none;
}

.tool > summary::before,
.think > summary::before {
  content: '▸';
  font-size: 11px;
}

.tool[open] > summary::before,
.think[open] > summary::before {
  content: '▾';
}

.tool .t-name {
  color: var(--accent-2);
  font-family: var(--mono);
  font-weight: 600;
}

.tool.err .t-name {
  color: var(--danger);
}

.tool .t-arg {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-family: var(--mono);
  font-size: 12px;
  opacity: 0.75;
}

.tool .t-spin {
  flex: none;
  animation: blink 1s steps(2, start) infinite;
}

.tool pre,
.think .t-body {
  margin: 0;
  padding: 10px 12px;
  max-height: 45vh;
  overflow: auto;
  background: #060a16;
  border-top: 1px solid var(--line);
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.45;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.think .t-body {
  font-family: var(--sans);
  color: var(--fg-dim);
  font-style: italic;
}

/* ── Composer ───────────────────────────────────────────────────────────── */

.composer-wrap {
  padding: 6px 10px calc(var(--safe-b) + 8px);
  background: rgba(11, 16, 32, 0.92);
  border-top: 1px solid var(--line);
  backdrop-filter: blur(10px);
}

.chips {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
  padding-bottom: 2px;
  overflow-x: auto;
  scrollbar-width: none;
}

.chips::-webkit-scrollbar {
  display: none;
}

.chip {
  flex: none;
  padding: 6px 12px;
  color: var(--fg-dim);
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 999px;
  font-size: 13px;
  white-space: nowrap;
}

/* Staged attachments, above the composer so they are clearly part of the next
   message rather than of the conversation so far. */
.attachments {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
  overflow-x: auto;
  scrollbar-width: none;
}

.attachments::-webkit-scrollbar {
  display: none;
}

.att {
  position: relative;
  flex: none;
}

.att img {
  width: 64px;
  height: 64px;
  object-fit: cover;
  border-radius: 10px;
  border: 1px solid var(--line);
}

.att-x {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 22px;
  height: 22px;
  padding: 0;
  line-height: 1;
  color: var(--fg);
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-radius: 50%;
  font-size: 15px;
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 4px;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 20px;
}

.composer textarea {
  flex: 1;
  max-height: 40vh;
  padding: 9px 4px;
  color: var(--fg);
  background: transparent;
  border: 0;
  resize: none;
  font: inherit;
  line-height: 1.4;
}

.composer textarea:focus {
  outline: none;
}

.send-btn {
  display: grid;
  place-items: center;
  flex: none;
  width: 40px;
  height: 40px;
  color: #06122b;
  background: var(--accent);
  border: 0;
  border-radius: 50%;
}

.send-btn:disabled {
  opacity: 0.35;
}

.send-btn.stop {
  background: var(--danger);
  color: #2b0606;
}

/* Two icon buttons now share the composer with the text; keep them compact so a
   narrow phone still has a usable typing area. */
.composer .icon-btn {
  width: 38px;
  height: 38px;
  flex: none;
}

/* The speaker pulses while it is actually reading, so "is it broken or is it
   just quiet?" is answerable at a glance. */
.icon-btn.speaking {
  color: var(--accent);
  animation: pulse 1.4s ease-in-out infinite;
}

@keyframes pulse {
  50% {
    opacity: 0.45;
  }
}

/* ── Long-press message menu ────────────────────────────────────────────── */

#msg-menu-scrim {
  position: fixed;
  inset: 0;
  z-index: 40;
}

.msg-menu {
  position: fixed;
  z-index: 41;
  display: flex;
  flex-direction: column;
  min-width: 168px;
  padding: 6px;
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.5);
}

.msg-menu button {
  padding: 12px 14px;
  color: var(--fg);
  background: none;
  border: 0;
  border-radius: 8px;
  text-align: left;
  font: inherit;
  font-size: 15px;
}

.msg-menu button:active {
  background: var(--bg-2);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--safe-b) + 84px);
  z-index: 50;
  transform: translateX(-50%);
  padding: 8px 16px;
  color: var(--fg);
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-radius: 999px;
  font-size: 13px;
  pointer-events: none;
}

/* ── Desktop ────────────────────────────────────────────────────────────── */

@media (min-width: 900px) {
  .app {
    flex-direction: row;
  }

  /* The drawer stops being an overlay and becomes a permanent sidebar. */
  .drawer {
    position: static;
    display: flex !important;
    width: 300px;
    flex: none;
  }

  .drawer-scrim {
    display: none !important;
  }

  #menu-btn {
    display: none;
  }

  .app > .topbar,
  .app > .thread,
  .app > .composer-wrap {
    /* Stack the rest to the right of the sidebar. */
    grid-column: 2;
  }

  .app {
    display: grid;
    grid-template-columns: 300px 1fr;
    grid-template-rows: auto 1fr auto;
  }

  .app > .drawer {
    grid-row: 1 / 4;
    grid-column: 1;
    height: 100vh;
  height: 100dvh;
  }

  .thread {
    padding: 20px 24px 8px;
  }

  .composer-wrap {
    padding: 8px 24px 16px;
  }
}

/* Install affordance on the login card. Hidden unless the browser offers a real
   install (beforeinstallprompt) — on iOS the adjacent hint text is shown instead,
   since Add to Home Screen cannot be triggered programmatically. */
.install-btn {
  width: 100%;
  margin-top: 10px;
  padding: 12px;
  font-weight: 600;
  color: var(--fg);
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-radius: 12px;
}
