* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: #f4f2ed;
  color: #1c1b18;
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
}

.app {
  display: grid;
  grid-template-columns: 17rem 1fr;
  height: 100dvh;
}

a {
  color: inherit;
}

/* ---- Sidebar: the sessions list ---- */

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 1.5rem 1rem;
  border-right: 1px solid #e1ddd3;
  background: #efece5;
  overflow-y: auto;
}

.sidebar-logo {
  display: block;
  width: 8rem;
  height: auto;
  margin-bottom: 0.9rem;
}

.sidebar header h1 {
  margin: 0;
  font-size: 1.05rem;
}

.sidebar header p {
  margin: 0.15rem 0 0;
  color: #6e6a61;
  font-size: 0.78rem;
}

.new-chat {
  padding: 0.55rem 0.9rem;
  border: 0;
  border-radius: 10px;
  background: #1c1b18;
  color: #f4f2ed;
  font: inherit;
  font-size: 0.875rem;
  cursor: pointer;
}

.sidebar nav {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.session {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.05rem;
  padding: 0.5rem 0.65rem;
  border: 1px solid transparent;
  border-radius: 10px;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.session:hover {
  background: #e7e3d9;
}

.session.active {
  background: #fff;
  border-color: #e1ddd3;
}

.session .title {
  max-width: 100%;
  overflow: hidden;
  font-size: 0.85rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.session .when,
.session .owner {
  color: #8a857a;
  font-size: 0.7rem;
}

.tech-tools {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.8rem;
}

.signout {
  margin: 0;
}

.sidenote {
  margin: 0;
  color: #8a857a;
  font-size: 0.72rem;
  line-height: 1.45;
}

/* ---- Main pane ---- */

main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}

/* Chat on the left, Outputs on the right, each scrolling on its own. */
.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(20rem, 28rem);
  height: 100%;
  min-height: 0;
}

.chat {
  display: flex;
  flex-direction: column;
  min-width: 0;
  padding: 0 1.25rem;
  overflow-y: auto;
}

.chat-header {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.9rem 0 0.6rem;
  border-bottom: 1px solid #e1ddd3;
  background: #f4f2ed;
}

.chat-header h2 {
  flex: 1;
  margin: 0;
  overflow: hidden;
  font-size: 0.95rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* The agent is working: in the chat header and on the sidebar entry. */
.status-pill {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.15rem 0.6rem;
  border-radius: 999px;
  background: #e6efe5;
  color: #2f6b3f;
  font-size: 0.75rem;
  font-weight: 600;
}

.status-pill::before,
.dot {
  content: "";
  display: inline-block;
  width: 0.5rem;
  height: 0.5rem;
  border-radius: 50%;
  background: #2f7d4f;
  animation: pulse 1.2s infinite;
}

.dot {
  margin-right: 0.4rem;
  vertical-align: 0.05rem;
}

/* What the conversation has cost, next to its title. */
.cost {
  position: relative;
  flex: none;
}

.cost-badge {
  padding: 0.15rem 0.6rem;
  border: 1px solid #d8d2c4;
  border-radius: 999px;
  background: #fff;
  color: #4f4b43;
  font-size: 0.75rem;
  font-variant-numeric: tabular-nums;
}

.cost-panel {
  position: absolute;
  top: calc(100% + 0.4rem);
  right: 0;
  z-index: 2;
  width: 19rem;
  padding: 0.75rem 0.9rem;
  border: 1px solid #d8d2c4;
  border-radius: 10px;
  background: #fff;
  box-shadow: 0 6px 20px rgb(0 0 0 / 0.08);
}

.cost-panel dl {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.25rem 1rem;
  margin: 0 0 0.6rem;
  font-size: 0.85rem;
}

.cost-panel dt:last-of-type,
.cost-panel dd:last-of-type {
  padding-top: 0.25rem;
  border-top: 1px solid #e1ddd3;
  font-weight: 600;
}

.cost-panel dd {
  margin: 0;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* Tech: the OpenRouter budget, and the warning when it runs low. */
.budget-line {
  margin: 0;
  color: #6e6a61;
  font-size: 0.75rem;
}

.budget-warning {
  padding: 0.6rem 0.75rem;
  border: 1px solid #e3b341;
  border-radius: 10px;
  background: #fdf3d8;
  color: #5c4100;
  font-size: 0.78rem;
  line-height: 1.45;
}

.admin .budget-warning,
.admin .budget-line {
  margin-bottom: 0.75rem;
}

.notice {
  margin: 0.75rem 0 0;
  padding: 0.5rem 0.75rem;
  border-radius: 10px;
  background: #ece6d6;
  font-size: 0.85rem;
}

.empty {
  margin: auto;
  padding: 0 1.25rem;
  max-width: 26rem;
  text-align: center;
}

.empty h2 {
  font-size: 1.15rem;
}

.empty p {
  color: #6e6a61;
  font-size: 0.9rem;
}

.empty button {
  margin-top: 0.5rem;
}

.transcript {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 1.5rem 0;
}

.hint,
.pulse,
.error {
  color: #6e6a61;
  font-size: 0.875rem;
}

.error {
  color: #a3341f;
}

.working {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.working .pulse {
  margin: 0;
}

.pulse::after {
  content: "...";
  animation: pulse 1.2s infinite;
}

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

/* The live feed: tool calls, model requests, thinking, retries. */
.activity {
  margin: 0;
  padding: 0.5rem 0.75rem;
  list-style: none;
  border-left: 2px solid #e1ddd3;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.75rem;
  color: #8a857a;
  overflow-wrap: anywhere;
}

.activity li {
  opacity: 0.55;
}

.activity li:last-child {
  opacity: 1;
  color: #4f4b43;
}

.activity li.tool_error,
.activity li.retry {
  color: #a3341f;
}

/* The kept per-turn tool-call trace, collapsed to one line in the transcript. */
.tool-trace summary {
  cursor: pointer;
  color: #6e6a61;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.75rem;
}

.tool-trace .activity {
  margin-top: 0.4rem;
}

/* Trace lines are a record, not a fading feed: uniform, not dimmed. */
.tool-trace .activity li,
.tool-trace .activity li:last-child {
  opacity: 1;
  color: #6e6a61;
}

.tool-trace .activity li.tool_error {
  color: #a3341f;
}

article {
  max-width: 85%;
  padding: 0.6rem 0.9rem;
  border-radius: 12px;
  overflow-wrap: anywhere;
}

article.user {
  align-self: flex-end;
  background: #1c1b18;
  color: #f4f2ed;
}

article.assistant {
  align-self: flex-start;
  background: #fff;
  border: 1px solid #e1ddd3;
}

article > :first-child {
  margin-top: 0;
}

article > :last-child {
  margin-bottom: 0;
}

article ul {
  padding-left: 1.2rem;
}

/* The "Preview ready" / "Video ready" card closing a turn that delivered files. */
.outputs-card {
  align-self: flex-start;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  min-width: 16rem;
  max-width: 85%;
  padding: 0.75rem 1rem;
  border: 1px solid #d8d2c4;
  border-left: 3px solid #1c1b18;
  border-radius: 12px;
  background: linear-gradient(135deg, #fffdf8, #f6f2e8);
}

.outputs-card.video {
  border-left-color: #2f7d4f;
}

.outputs-card.preview {
  border-left-color: #b7791f;
}

.card-title {
  margin: 0;
  font-size: 0.9rem;
  font-weight: 600;
}

.card-meta,
.size {
  margin: 0;
  color: #6e6a61;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.75rem;
}

.outputs-card ul,
.downloads {
  margin: 0.4rem 0 0;
  padding: 0;
  list-style: none;
  font-size: 0.8rem;
}

.outputs-card .link {
  align-self: flex-start;
  margin-top: 0.4rem;
}

.outputs-card li,
.downloads li {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  overflow-wrap: anywhere;
}

/* ---- Outputs panel ---- */

.outputs {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 1rem 1.1rem 2rem;
  border-left: 1px solid #e1ddd3;
  background: #faf8f3;
  overflow-y: auto;
}

.outputs h2 {
  margin: 0;
  font-size: 0.95rem;
}

.output-group {
  padding-top: 0.75rem;
  border-top: 1px solid #e1ddd3;
}

.group-body {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.output-group > summary {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.75rem;
  cursor: pointer;
  list-style: none;
}

.output-group > summary::-webkit-details-marker {
  display: none;
}

.output-group > summary::before {
  content: "▸";
  color: #8a857a;
  font-size: 0.75rem;
}

.output-group[open] > summary::before {
  content: "▾";
}

.output-group[open] > summary {
  margin-bottom: 0.25rem;
}

.group-title {
  flex: 1;
  font-size: 0.85rem;
  font-weight: 600;
}

.group-when {
  color: #8a857a;
  font-size: 0.75rem;
}

.output-group video,
.output-group .poster {
  width: 100%;
  border-radius: 8px;
  background: #000;
}

.frames {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.35rem;
}

.frames img {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 9;
  object-fit: cover;
  border-radius: 4px;
}

.md-file summary {
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 500;
}

.md-body {
  margin-top: 0.4rem;
  padding: 0.5rem 0.75rem;
  border: 1px solid #e1ddd3;
  border-radius: 8px;
  background: #fff;
  font-size: 0.82rem;
  overflow-wrap: anywhere;
}

/* ---- Admin ---- */

.admin {
  padding: 1.25rem;
  overflow: auto;
}

.admin table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
}

.admin th,
.admin td {
  padding: 0.4rem 0.5rem;
  border-bottom: 1px solid #e1ddd3;
  text-align: left;
}

.admin tbody tr {
  cursor: pointer;
}

.admin tbody tr:hover {
  background: #efece5;
}

/* ---- Composer ---- */

.composer {
  position: sticky;
  bottom: 0;
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  padding: 0.75rem 0 1rem;
  background: #f4f2ed;
}

.composer textarea {
  flex: 1;
  padding: 0.6rem 0.9rem;
  border: 1px solid #c9c4b8;
  border-radius: 10px;
  font: inherit;
  background: #fff;
  resize: none;
}

.composer .attach {
  padding: 0.6rem 0.75rem;
  background: #e7e3d9;
  color: #1c1b18;
}

button {
  padding: 0.6rem 1.1rem;
  border: 0;
  border-radius: 10px;
  background: #1c1b18;
  color: #f4f2ed;
  font: inherit;
  cursor: pointer;
}

button:disabled {
  opacity: 0.4;
  cursor: default;
}

button.link {
  padding: 0;
  background: none;
  color: #4f4b43;
  font-size: 0.8rem;
  text-decoration: underline;
}

button.danger {
  padding: 0.35rem 0.75rem;
  background: none;
  border: 1px solid #c9c4b8;
  color: #a3341f;
  font-size: 0.8rem;
}

/* ---- Sign-in screen (served by src/auth.ts) ---- */

.signin {
  max-width: 24rem;
  margin: 18vh auto 0;
  padding: 0 1.25rem;
  text-align: center;
}

.signin-logo {
  display: block;
  width: 15rem;
  max-width: 70%;
  height: auto;
  margin: 0 auto 1.75rem;
}

.signin .button {
  display: inline-block;
  margin: 0.5rem 0;
  padding: 0.6rem 1.1rem;
  border-radius: 10px;
  background: #1c1b18;
  color: #f4f2ed;
  text-decoration: none;
}

@media (max-width: 900px) {
  .app {
    grid-template-columns: 1fr;
    height: auto;
  }

  .workspace {
    grid-template-columns: 1fr;
  }
}
