/* ---------------------------------------------------------------------
   CONTENT FACTORY — ChatGPT-style operator console.
   Clean monochrome chat UI: sidebar history + message feed + composer.
--------------------------------------------------------------------- */

:root {
  --bg-sidebar: #171717;
  --bg-main: #212121;
  --bg-elevated: #2f2f2f;
  --bg-elevated-2: #383838;
  --bg-hover: rgba(255, 255, 255, 0.06);
  --border: rgba(255, 255, 255, 0.1);
  --text: #ececec;
  --text-dim: #b4b4b4;
  --text-faint: #8e8e8e;
  --white: #ffffff;
  --danger: #f28b82;
  --danger-bg: rgba(242, 139, 130, 0.08);
  --danger-border: rgba(242, 139, 130, 0.3);
  --green: #3ecf6e;
  --red: #f2545b;
  --amber: #f0a93c;

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, "SF Mono", "JetBrains Mono", Consolas, monospace;

  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg-main);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

button, textarea, input { font-family: inherit; }

/* ---------- app shell ---------- */

.app {
  display: flex;
  height: 100dvh;
  overflow: hidden;
}
.app[hidden] { display: none; }

/* ---------- sidebar ---------- */

.sidebar {
  width: 268px;
  flex-shrink: 0;
  background: var(--bg-sidebar);
  display: flex;
  flex-direction: column;
  height: 100%;
}

.sidebar-top {
  padding: 12px;
}

.new-btn {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 13.5px;
  cursor: pointer;
  transition: background 0.12s ease;
}
.new-btn:hover { background: var(--bg-hover); }

.sidebar-label {
  font-size: 12px;
  color: var(--text-faint);
  padding: 10px 16px 4px;
}

.history {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.history-empty {
  color: var(--text-faint);
  font-size: 12.5px;
  padding: 8px 8px;
}

.history-item {
  display: flex;
  align-items: center;
  gap: 2px;
  width: 100%;
  border-radius: 8px;
  color: var(--text-dim);
  transition: background 0.12s ease, color 0.12s ease;
  position: relative;
}
.history-item:hover { background: var(--bg-hover); color: var(--text); }
.history-item.active { background: var(--bg-elevated); color: var(--text); }

.history-main {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  text-align: left;
  background: transparent;
  border: none;
  color: inherit;
  padding: 9px 4px 9px 10px;
  font-size: 13.5px;
  cursor: pointer;
}

.history-dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  flex-shrink: 0;
  background: var(--text-faint);
}
.history-item[data-state="running"] .history-dot { background: var(--amber); animation: pulse 1.1s ease-in-out infinite; }
.history-item[data-state="done"] .history-dot { background: var(--green); }
.history-item[data-state="failed"] .history-dot { background: var(--red); }

.history-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-rename-input {
  flex: 1;
  min-width: 0;
  background: var(--bg-main);
  border: 1px solid var(--white);
  border-radius: 5px;
  color: var(--text);
  font-size: 13.5px;
  font-family: inherit;
  padding: 3px 6px;
  outline: none;
}

.history-actions {
  display: none;
  align-items: center;
  gap: 2px;
  padding-right: 6px;
  flex-shrink: 0;
}
.history-item:hover .history-actions,
.history-item.active .history-actions { display: flex; }

.history-action {
  width: 24px; height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  border-radius: 6px;
  color: var(--text-faint);
  cursor: pointer;
}
.history-action:hover { background: var(--bg-elevated-2); color: var(--text); }
.history-action-danger:hover { color: var(--danger); }

.sidebar-scrim {
  display: none;
}

/* ---------- main column ---------- */

.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  height: 100%;
  position: relative;
}

.topbar {
  height: 52px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 14px;
}

.icon-btn {
  display: none;
  background: transparent;
  border: none;
  color: var(--text-dim);
  width: 34px; height: 34px;
  border-radius: 8px;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }

.topbar-title {
  display: flex;
  align-items: center;
  gap: 4px;
  flex: 1;
  min-width: 0;
}

.topbar-title-text {
  font-size: 14.5px;
  font-weight: 600;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
}

.topbar-title-input {
  flex: 1;
  min-width: 0;
  max-width: 360px;
  background: var(--bg-elevated);
  border: 1px solid var(--white);
  border-radius: 6px;
  color: var(--text);
  font-size: 14.5px;
  font-weight: 600;
  font-family: inherit;
  padding: 4px 8px;
  outline: none;
}

.topbar-edit-btn {
  display: flex;
  width: 28px;
  height: 28px;
  opacity: 0;
  transition: opacity 0.12s ease;
}
.topbar-title:hover .topbar-edit-btn,
.topbar-title:focus-within .topbar-edit-btn {
  opacity: 1;
}

/* ---------- feed ---------- */

.feed {
  flex: 1;
  min-width: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 12px 0 24px;
}

.empty-hero {
  height: 100%;
  min-height: 40vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 20px;
  gap: 10px;
}

.empty-hero[hidden] { display: none; }

.empty-hero h1 {
  font-size: 28px;
  font-weight: 600;
  margin: 0;
  color: var(--text);
}

.empty-hero p {
  margin: 0;
  color: var(--text-faint);
  font-size: 14.5px;
  max-width: 420px;
}

.turn {
  max-width: 768px;
  width: 100%;
  min-width: 0;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  scroll-margin-top: 20px;
}

.turn + .turn { margin-top: 22px; }

.msg { display: flex; min-width: 0; }

.msg-user { justify-content: flex-end; }

.msg-user .bubble {
  background: var(--bg-elevated);
  color: var(--text);
  border-radius: 20px;
  padding: 10px 16px;
  max-width: 78%;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font-size: 15px;
}

.msg-assistant {
  gap: 12px;
  align-items: flex-start;
}

.avatar {
  width: 28px; height: 28px;
  border-radius: 8px;
  background: var(--white);
  color: #111;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  margin-top: 2px;
}

.msg-body {
  flex: 1;
  min-width: 0;
  padding-top: 3px;
}

.type-tag {
  font-size: 11px;
  letter-spacing: 0.06em;
  color: var(--text-faint);
  text-transform: uppercase;
  margin-bottom: 4px;
}

.msg-content {
  color: var(--text);
  font-size: 15px;
  line-height: 1.65;
  overflow-wrap: anywhere;
}

.msg-content p { margin: 0 0 10px; white-space: pre-wrap; }
.msg-content p:last-child { margin-bottom: 0; }

/* ---------- markdown (rendered LLM replies) ---------- */

.markdown > *:first-child { margin-top: 0; }
.markdown > *:last-child { margin-bottom: 0; }

.markdown h1, .markdown h2, .markdown h3,
.markdown h4, .markdown h5, .markdown h6 {
  color: var(--text);
  font-weight: 600;
  line-height: 1.35;
  margin: 20px 0 10px;
}
.markdown h1 { font-size: 22px; }
.markdown h2 { font-size: 19px; }
.markdown h3 { font-size: 16.5px; }
.markdown h4, .markdown h5, .markdown h6 { font-size: 15px; }

.markdown strong { font-weight: 700; color: var(--text); }
.markdown em { font-style: italic; }
.markdown del { color: var(--text-faint); }

.markdown a { color: var(--white); text-decoration: underline; text-decoration-color: var(--border); text-underline-offset: 2px; }
.markdown a:hover { text-decoration-color: var(--white); }

.markdown ul, .markdown ol {
  margin: 0 0 10px;
  padding-left: 1.4em;
}
.markdown li { margin: 3px 0; }
.markdown li::marker { color: var(--text-faint); }

.markdown blockquote {
  margin: 0 0 10px;
  padding: 2px 14px;
  border-left: 3px solid var(--border);
  color: var(--text-dim);
}

.markdown hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 16px 0;
}

.markdown code {
  font-family: var(--font-mono);
  font-size: 0.88em;
  background: var(--bg-elevated-2);
  border-radius: 4px;
  padding: 0.15em 0.4em;
}

.markdown pre {
  background: var(--bg-elevated-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  margin: 0 0 10px;
  overflow-x: auto;
}
.markdown pre code {
  background: none;
  padding: 0;
  font-size: 13px;
  line-height: 1.55;
  white-space: pre;
}

.md-table-wrap {
  margin: 0 0 10px;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 10px;
}
.markdown table {
  border-collapse: collapse;
  width: 100%;
  font-size: 13.5px;
}
.markdown th, .markdown td {
  padding: 8px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  white-space: normal;
}
.markdown th {
  color: var(--text);
  font-weight: 600;
  background: var(--bg-elevated);
}
.markdown tr:last-child td { border-bottom: none; }

.typing {
  display: inline-flex;
  gap: 4px;
  padding: 6px 0;
}
.typing span {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--text-faint);
  animation: bounce 1.2s infinite ease-in-out;
}
.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }

@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30% { transform: translateY(-4px); opacity: 1; }
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

.msg-error {
  background: var(--danger-bg);
  border: 1px solid var(--danger-border);
  color: var(--danger);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 13.5px;
  white-space: pre-wrap;
}

.msg-image img {
  max-width: 100%;
  max-height: 420px;
  border-radius: 12px;
  display: block;
  border: 1px solid var(--border);
}

.msg-video video {
  max-width: 100%;
  max-height: 420px;
  border-radius: 12px;
  display: block;
  background: #000;
  border: 1px solid var(--border);
}

.msg-video video:fullscreen {
  max-width: none;
  max-height: none;
}

.doc-card {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 16px;
  text-decoration: none;
  color: var(--text);
  transition: background 0.12s ease;
}
.doc-card:hover { background: var(--bg-elevated-2); }

.doc-icon {
  width: 34px; height: 34px;
  border-radius: 8px;
  background: rgba(255,255,255,0.08);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  font-size: 15px;
}

.doc-meta { display: flex; flex-direction: column; gap: 2px; }
.doc-name { font-size: 13.5px; font-weight: 500; }
.doc-sub { font-size: 12px; color: var(--text-faint); }

/* ---------- composer ---------- */

.composer-wrap {
  flex-shrink: 0;
  padding: 6px 20px 18px;
  background: linear-gradient(180deg, transparent, var(--bg-main) 40%);
}

.composer {
  max-width: 768px;
  margin: 0 auto;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 10px 10px 6px;
  box-shadow: 0 8px 30px rgba(0,0,0,0.25);
}

.type-chips {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  padding: 2px 6px 8px;
}

.chip {
  display: flex;
  align-items: center;
  gap: 6px;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 14px;
  padding: 5px 11px;
  font-size: 12.5px;
  cursor: pointer;
  transition: all 0.12s ease;
}
.chip:hover { background: var(--bg-hover); color: var(--text); }
.chip[aria-pressed="true"] {
  background: var(--white);
  color: #111;
  border-color: var(--white);
}

.chip-icon { font-size: 11px; opacity: 0.85; }

.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 4px 6px 4px 12px;
}

textarea#prompt {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  resize: none;
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  max-height: 200px;
  padding: 8px 0;
}
textarea#prompt::placeholder { color: var(--text-faint); }

.send-btn {
  width: 34px; height: 34px;
  border-radius: 50%;
  background: var(--white);
  color: #111;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  cursor: pointer;
  margin-bottom: 2px;
  transition: transform 0.1s ease, opacity 0.1s ease;
}
.send-btn:hover { transform: scale(1.05); }
.send-btn:disabled {
  background: var(--bg-elevated-2);
  color: var(--text-faint);
  cursor: not-allowed;
  transform: none;
}

.form-error {
  margin: 8px 10px 4px;
  color: var(--danger);
  font-size: 12.5px;
}

.composer-hint {
  text-align: center;
  color: var(--text-faint);
  font-size: 11.5px;
  margin: 10px 0 0;
}

/* ---------- responsive ---------- */

@media (max-width: 860px) {
  .sidebar {
    position: fixed;
    left: 0; top: 0; bottom: 0;
    z-index: 30;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: 20px 0 40px rgba(0,0,0,0.3);
  }
  .sidebar.open { transform: translateX(0); }

  .sidebar-scrim {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 20;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
  }
  .sidebar-scrim.visible { opacity: 1; pointer-events: auto; }

  .icon-btn { display: flex; }

  .turn { padding: 0 14px; }
  .composer-wrap { padding: 6px 12px 14px; }
}

/* ---------- sidebar user footer ---------- */

.sidebar-bottom {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 14px;
  border-top: 1px solid var(--border);
}

.sidebar-user-email {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 12.5px;
  color: var(--text-dim);
}

.sidebar-bottom .icon-btn { display: flex; }

/* ---------- auth screens ---------- */

.auth-screen {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background:
    radial-gradient(ellipse at top, rgba(245, 166, 35, 0.06), transparent 55%),
    var(--bg-main);
}
.auth-screen[hidden] { display: none; }

.auth-card {
  width: 100%;
  max-width: 360px;
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 32px 28px;
  box-shadow: 0 20px 50px -20px rgba(0,0,0,0.5);
}

.auth-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 17px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 24px;
}
.auth-brand svg { color: var(--amber); flex-shrink: 0; }

.auth-tabs {
  display: flex;
  background: var(--bg-elevated);
  border-radius: 10px;
  padding: 3px;
  margin-bottom: 20px;
}

.auth-tab {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 13.5px;
  font-family: inherit;
  padding: 8px;
  border-radius: 8px;
  cursor: pointer;
}
.auth-tab[aria-pressed="true"] {
  background: var(--white);
  color: #111;
  font-weight: 600;
}

.auth-field {
  display: block;
  margin-bottom: 14px;
}
.auth-field span {
  display: block;
  font-size: 12px;
  color: var(--text-faint);
  margin-bottom: 6px;
}
.auth-field input {
  width: 100%;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  padding: 10px 12px;
  outline: none;
  transition: border-color 0.12s ease;
}
.auth-field input:focus { border-color: var(--white); }

.auth-submit {
  width: 100%;
  background: var(--white);
  color: #111;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  font-family: inherit;
  padding: 11px;
  cursor: pointer;
  margin-top: 4px;
  transition: opacity 0.12s ease;
}
.auth-submit:hover { opacity: 0.9; }
.auth-submit:disabled { opacity: 0.5; cursor: progress; }

.auth-link {
  display: block;
  width: 100%;
  text-align: center;
  background: none;
  border: none;
  color: var(--text-faint);
  font-size: 12.5px;
  font-family: inherit;
  padding: 16px 0 0;
  cursor: pointer;
}
.auth-link:hover { color: var(--text); }

.auth-hint {
  color: var(--text-dim);
  font-size: 13px;
  line-height: 1.5;
  margin: 0 0 18px;
}

.auth-error {
  color: var(--danger);
  font-size: 12.5px;
  background: var(--danger-bg);
  border: 1px solid var(--danger-border);
  border-radius: 6px;
  padding: 8px 10px;
  margin: 0 0 14px;
}

.auth-success {
  color: var(--green);
  font-size: 12.5px;
  background: rgba(62, 207, 110, 0.08);
  border: 1px solid rgba(62, 207, 110, 0.3);
  border-radius: 6px;
  padding: 8px 10px;
  margin: 0 0 14px;
}
