/* ── Reset / base ──────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  background: #f4f5f7;
  color: #1a1a2e;
}

a { color: inherit; text-decoration: none; }

/* ── App shell ─────────────────────────────────────────────────────────── */
#app {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ── Sidebar ───────────────────────────────────────────────────────────── */
#sidebar {
  width: 260px;
  min-width: 260px;
  background: #fff;
  color: #1a1a2e;
  display: flex;
  flex-direction: column;
  padding: 1.2rem 1rem;
  gap: 1rem;
  border-right: 1px solid #e2e4e9;
}

#sidebar-logo {
  padding: 0.1rem 0 0.3rem;
}
#decamerone-logo {
  width: 100%;
  max-width: 150px;
  height: auto;
  display: block;
}

#sidebar-nav {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.nav-link {
  display: block;
  padding: 0.55rem 0.8rem;
  border-radius: 6px;
  color: #555;
  font-size: 0.9rem;
  transition: background 0.15s;
}
.nav-link:hover, .nav-link.active {
  background: rgba(0,0,0,0.06);
  color: #1a1a2e;
}

#sidebar-user {
  font-size: 0.8rem;
  color: #888;
  border-top: 1px solid #e2e4e9;
  padding-top: 0.8rem;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}
.logout-link { color: #e63946; font-size: 0.78rem; }
.logout-link:hover { text-decoration: underline; }

/* ── Drag-to-resize handles ────────────────────────────────────────────── */
.resize-handle {
  width: 4px;
  flex-shrink: 0;
  background: transparent;
  cursor: col-resize;
  transition: background 0.15s;
}
.resize-handle:hover {
  background: rgba(0,0,0,0.1);
}

/* ── Main content ──────────────────────────────────────────────────────── */
#main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

/* ── Video panel ───────────────────────────────────────────────────────── */
#video-panel {
  width: 420px;
  min-width: 280px;
  background: #f4f5f7;
  display: flex;
  flex-direction: column;
  border-left: 1px solid #e2e4e9;
  flex-shrink: 0;
}
#video-panel[hidden] { display: none; }

#video-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid #e2e4e9;
  font-size: 0.78rem;
  color: #555;
  flex-shrink: 0;
  gap: 0.5rem;
}
#video-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#video-panel-close {
  background: none;
  border: none;
  color: #999;
  font-size: 1.15rem;
  cursor: pointer;
  padding: 0 0.2rem;
  line-height: 1;
  flex-shrink: 0;
}
#video-panel-close:hover { color: #1a1a2e; }

#rr-video-player {
  width: 100%;
  aspect-ratio: 16 / 9;
  background: transparent;
  display: block;
}

/* ── Timecode links in chat ────────────────────────────────────────────── */
.tc-link {
  display: inline;
  background: none;
  border: 1px solid #c0392b;
  color: #e74c3c;
  border-radius: 4px;
  padding: 0.05rem 0.3rem;
  font-size: 0.82em;
  cursor: pointer;
  font-family: monospace;
  margin: 0 0.1rem;
  line-height: 1.4;
}
.tc-link:hover {
  background: #e74c3c;
  color: #fff;
}

/* ── Chat wrap ─────────────────────────────────────────────────────────── */
#chat-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
}

#chat-history {
  flex: 1;
  overflow-y: auto;
  padding: 1.2rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

/* ── Chat messages ─────────────────────────────────────────────────────── */
.msg {
  max-width: 80%;
  padding: 0.75rem 1rem;
  border-radius: 12px;
  line-height: 1.55;
}

.user-msg {
  align-self: flex-end;
  background: #e63946;
  color: #fff;
}

.assistant-msg {
  align-self: flex-start;
  background: #fff;
  border: 1px solid #e2e4e9;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}

.msg-speaker {
  display: block;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.3rem;
  opacity: 0.75;
}

.msg-content {
  font-size: 0.92rem;
  white-space: pre-wrap;
  word-break: break-word;
}

/* ── Loading indicator (spinner + cycling text) ────────────────────────── */
.loading-indicator {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  color: #888;
  font-size: 0.88rem;
  font-style: italic;
  padding: 0.1rem 0;
}

.loading-spinner {
  width: 14px;
  height: 14px;
  border: 2px solid #e2e4e9;
  border-top-color: #e63946;
  border-radius: 50%;
  animation: rr-spin 0.9s linear infinite;
  flex-shrink: 0;
}

@keyframes rr-spin {
  to { transform: rotate(360deg); }
}

/* ── Mini spinner (inline, used in agent thoughts + idle toast) ────────── */
.mini-spinner {
  display: inline-block;
  width: 11px;
  height: 11px;
  border: 1.5px solid #dde;
  border-top-color: #888;
  border-radius: 50%;
  animation: rr-spin 1.2s linear infinite;
  margin-right: 0.4rem;
  vertical-align: middle;
  flex-shrink: 0;
}

/* ── Agent idle status (inline, same level as agent-thought) ───────────── */
.agent-idle-status {
  display: flex;
  align-items: baseline;
  gap: 0;
  font-size: 0.82rem;
  color: #888;
  font-style: italic;
  padding: 0.3rem 0 0.3rem 1rem;
}
.agent-idle-status[hidden] { display: none; }

/* ── Agent streaming elements ──────────────────────────────────────────── */
.agent-thought {
  font-size: 0.82rem;
  color: #888;
  font-style: italic;
  padding: 0.3rem 0;
  display: flex;
  align-items: baseline;
  gap: 0;
}

.tool-call {
  background: #f8f9fb;
  border: 1px solid #dde;
  border-radius: 8px;
  padding: 0.5rem 0.75rem;
  margin: 0.4rem 0;
  font-size: 0.82rem;
}

.tool-call summary {
  cursor: pointer;
  user-select: none;
  font-weight: 500;
  color: #444;
}

.tool-body { padding-top: 0.4rem; }

.tool-output {
  background: #f1f2f5;
  border-radius: 4px;
  padding: 0.4rem 0.6rem;
  font-size: 0.78rem;
  max-height: 200px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-word;
  color: #333;
}

.agent-answer {
  margin-top: 0.3rem;
}

.agent-retry {
  font-size: 0.82rem;
  color: #e6a800;
  padding: 0.25rem 0;
}

.agent-error {
  font-size: 0.82rem;
  color: #c0392b;
  background: #fdecea;
  border-radius: 4px;
  padding: 0.3rem 0.5rem;
}

.thinking {
  animation: blink 1s step-start infinite;
}
@keyframes blink { 50% { opacity: 0; } }

/* ── Chat input area ───────────────────────────────────────────────────── */
#chat-input-area {
  border-top: 1px solid #e2e4e9;
  background: #fff;
  padding: 0.85rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

#chat-form {
  display: flex;
  gap: 0.6rem;
}

#msg-input {
  flex: 1;
  padding: 0.6rem 0.9rem;
  border: 1.5px solid #d1d5db;
  border-radius: 8px;
  font-size: 0.92rem;
  outline: none;
  transition: border-color 0.15s;
}
#msg-input:focus { border-color: #e63946; }

#chat-form button {
  padding: 0.6rem 1.2rem;
  background: #e63946;
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}
#chat-form button:hover { background: #c0392b; }

#spinner {
  font-size: 0.82rem;
  color: #888;
  display: none;
}
.htmx-request #spinner { display: block; }

/* ── Login page ────────────────────────────────────────────────────────── */
.login-wrap {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #f4f5f7;
  padding: 2rem 1rem;
}
.login-box {
  background: #fff;
  padding: 2.5rem 2rem;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.08);
  width: 340px;
}
.login-box h1 {
  font-size: 1.5rem;
  color: #1a1a2e;
  margin-bottom: 0.25rem;
}
.login-box .subtitle {
  color: #888;
  font-size: 0.85rem;
  margin-bottom: 1.5rem;
}
.login-box label {
  display: block;
  font-size: 0.82rem;
  font-weight: 600;
  color: #444;
  margin-bottom: 0.3rem;
}
.login-box input {
  width: 100%;
  padding: 0.6rem 0.8rem;
  border: 1.5px solid #d1d5db;
  border-radius: 7px;
  font-size: 0.92rem;
  margin-bottom: 1rem;
  outline: none;
  transition: border-color 0.15s;
}
.login-box input:focus { border-color: #e63946; }
.login-box button {
  width: 100%;
  padding: 0.7rem;
  background: #e63946;
  color: #fff;
  border: none;
  border-radius: 7px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
}
.login-box button:hover { background: #c0392b; }
.login-error {
  color: #c0392b;
  font-size: 0.85rem;
  margin-bottom: 0.75rem;
}
.login-legal {
  margin-top: 1.5rem;
  max-width: 340px;
  font-size: 0.72rem;
  color: #999;
  line-height: 1.55;
  text-align: center;
}

/* ── dataframe tables ──────────────────────────────────────────────────── */
.df-wrap {
  overflow-x: auto;
  margin: 0.4rem 0;
}
table {
  border-collapse: collapse;
  font-size: 0.78rem;
  width: 100%;
}
th, td {
  border: 1px solid #e2e4e9;
  padding: 0.3rem 0.5rem;
  text-align: left;
  vertical-align: top;
}
th { background: #f0f2f5; font-weight: 600; }
tr:nth-child(even) { background: #f9fafb; }

/* ── Screenplay formatting — matches original rr_dialoge_to_html ──────── */
/*
   Original app uses pt-based margins in a 625px container:
     role:   margin-left 144pt (~192px), right 100px, uppercase bold
     dialog: margin-left 80pt  (~107px), right 200px
     action: margin-left 15pt  (~20px),  right 70px
     parent: margin-left 110pt (~147px), right 200px  (parenthetical)
*/
.screenplay-block,
.source-zitat {
  background: #F6F2E9;
  border-radius: 4px;
  padding: 10px;
  margin: 0.5rem 0;
  width: 625px;
  max-width: 100%;
  overflow-x: auto;
  font-family: 'Courier New', Courier, monospace;
  font-size: 14px;
  line-height: 1.5;
  box-sizing: border-box;
}

.sp-role {
  margin-left: 144pt;
  margin-right: 100px;
  margin-top: 12px;
  margin-bottom: 0;
  font-weight: bold;
  text-transform: uppercase;
  font-size: 14px;
  font-family: 'Courier New', Courier, monospace;
  line-height: 1.5;
  color: #1a1a1a;
}

.sp-dialog {
  margin-left: 80pt;
  margin-right: 200px;
  margin-top: 0;
  margin-bottom: 1em;
  font-size: 14px;
  font-family: 'Courier New', Courier, monospace;
  line-height: 1.5;
  color: #1a1a1a;
}

.sp-action {
  margin-left: 15pt;
  margin-right: 70px;
  margin-top: 0;
  margin-bottom: 0;
  padding-top: 0.9em;
  font-size: 14px;
  font-family: 'Courier New', Courier, monospace;
  line-height: 1.5;
  color: #1a1a1a;
}

.sp-parent {
  margin-left: 110pt;
  margin-right: 200px;
  margin-top: 0;
  margin-bottom: 0;
  font-size: 14px;
  font-family: 'Courier New', Courier, monospace;
  line-height: 1.5;
  color: #1a1a1a;
}

.sp-sternchen {
  margin-left: 110pt;
  margin-right: 200px;
  margin-top: 0;
  margin-bottom: 0;
  font-size: 14px;
  font-family: 'Courier New', Courier, monospace;
  line-height: 1.5;
  color: #1a1a1a;
}

.sp-field {
  margin-left: 110pt;
  margin-right: 200px;
  margin-top: 0;
  margin-bottom: 0;
  font-size: 12px;
  font-family: 'Courier New', Courier, monospace;
  line-height: 1.5;
  color: #555;
}

.sp-plain {
  margin-left: 15pt;
  margin-top: 0;
  margin-bottom: 0;
  font-family: 'Courier New', Courier, monospace;
  font-size: 14px;
  line-height: 1.5;
  color: #1a1a1a;
}

/* ── Sources panel ─────────────────────────────────────────────────────── */

/* Sources rendered directly beneath the response — no outer wrapper */
.sources-list {
  margin-top: 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  font-size: 0.83rem;
}

/* Each source item is a <details> — collapsed shows header+cast only */
.source-item {
  border: 1px solid #eaecf0;
  border-radius: 6px;
  overflow: hidden;
}

/* Hide the browser's default disclosure triangle; no custom indicator.
   Target the <summary> element type directly — class-only selectors are
   insufficient in some browsers for suppressing the details marker. */
details.source-item > summary {
  display: block;          /* overrides list-item, removes the marker */
  list-style: none;
  cursor: pointer;
  user-select: none;
}
details.source-item > summary::-webkit-details-marker { display: none; }
details.source-item > summary::marker                 { display: none; }

.source-header {
  background: #f0f2f5;
  padding: 0.3rem 0.6rem;
  font-weight: 600;
  font-size: 0.78rem;
  color: #333;
  display: flex;
  align-items: center;
  gap: 0.35rem;
}

.source-loc {
  font-weight: 400;
  color: #777;
}

.source-cast {
  padding: 0.2rem 0.6rem;
  font-size: 0.75rem;
  color: #555;
  background: #f7f8fa;
  border-top: 1px solid #eaecf0;
}

.source-zitat {
  padding: 0;
}

.source-footer {
  padding: 0.25rem 0.6rem;
  font-size: 0.7rem;
  color: #999;
  border-top: 1px solid #eaecf0;
  background: #fafafa;
}

.source-raw {
  padding: 0.4rem 0.6rem;
  font-size: 0.78rem;
  color: #555;
  white-space: pre-wrap;
}

/* Yellow highlight for cited screenplay lines */
.sp-highlight {
  background: #fef08a;
  border-radius: 2px;
}

/* ── Conversation panel ────────────────────────────────────────────────── */
#conv-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  overflow: hidden;
  min-height: 0;
}

.conv-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.75rem;
  color: #999;
  padding: 0 0.2rem;
  flex-shrink: 0;
}

.conv-panel-header button {
  background: none;
  border: 1px solid #d1d5db;
  color: #555;
  border-radius: 4px;
  padding: 0.15rem 0.5rem;
  font-size: 0.72rem;
  cursor: pointer;
  transition: background 0.15s;
}
.conv-panel-header button:hover {
  background: rgba(0,0,0,0.06);
}

#conv-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  min-height: 0;
}

.conv-item {
  position: relative;
  display: flex;
  align-items: center;
  gap: 0.2rem;
  padding: 0.45rem 0.6rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.82rem;
  color: #444;
  transition: background 0.12s;
  flex-shrink: 0;
}
.conv-item:hover { background: rgba(0,0,0,0.05); }
.conv-item.active { background: rgba(0,0,0,0.09); color: #1a1a2e; font-weight: 500; }

.conv-item-body {
  flex: 1;
  min-width: 0;
}

.conv-name {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  outline: none;
}
.conv-name[contenteditable="true"] {
  white-space: normal;
  overflow: visible;
  background: rgba(0,0,0,0.05);
  border-radius: 3px;
  padding: 0 3px;
  color: #1a1a2e;
  cursor: text;
}

.conv-meta {
  display: block;
  font-size: 0.68rem;
  color: #666;
  margin-top: 0.1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── Three-dots menu button ────────────────────────────────────────────── */
.conv-dots {
  flex-shrink: 0;
  display: none;
  background: none;
  border: none;
  color: #888;
  cursor: pointer;
  padding: 0.1rem 0.25rem;
  font-size: 1rem;
  line-height: 1;
  border-radius: 4px;
  letter-spacing: 0.05em;
}
.conv-item:hover .conv-dots,
.conv-item.menu-open .conv-dots {
  display: block;
}
.conv-dots:hover { color: #1a1a2e; background: rgba(0,0,0,0.08); }

/* ── Dropdown menu ─────────────────────────────────────────────────────── */
.conv-menu {
  position: absolute;
  right: 0.3rem;
  top: calc(100% - 2px);
  background: #fff;
  border: 1px solid #e2e4e9;
  border-radius: 7px;
  overflow: hidden;
  z-index: 200;
  min-width: 140px;
  box-shadow: 0 6px 18px rgba(0,0,0,0.12);
}
.conv-menu button {
  display: block;
  width: 100%;
  padding: 0.5rem 0.85rem;
  background: none;
  border: none;
  color: #333;
  text-align: left;
  font-size: 0.82rem;
  cursor: pointer;
}
.conv-menu button:hover { background: rgba(0,0,0,0.05); color: #1a1a2e; }
.conv-menu-delete:hover { color: #e63946 !important; }

/* ── Delete confirmation overlay ───────────────────────────────────────── */
.conv-confirm-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}
.conv-confirm-box {
  background: #fff;
  border-radius: 12px;
  padding: 1.6rem 1.5rem 1.3rem;
  max-width: 320px;
  width: 90%;
  box-shadow: 0 10px 40px rgba(0,0,0,0.22);
  text-align: center;
  color: #1a1a2e;
}
.conv-confirm-box p {
  font-size: 0.9rem;
  line-height: 1.55;
  margin-bottom: 1.2rem;
}
.conv-confirm-btns {
  display: flex;
  gap: 0.7rem;
  justify-content: center;
}
.conv-btn-cancel,
.conv-btn-delete {
  padding: 0.38rem 1.2rem;
  border-radius: 999px;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  border: none;
}
.conv-btn-cancel { background: #f0f2f5; color: #555; }
.conv-btn-cancel:hover { background: #e2e4e9; }
.conv-btn-delete { background: #e63946; color: #fff; }
.conv-btn-delete:hover { background: #c0392b; }

/* ── Streaming tokens ──────────────────────────────────────────────────── */
.agent-token {
  display: inline;
}
