*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f9f8f6; --bg-alpha: rgba(249, 248, 246, 0.85);
  --surface: #ffffff; --surface2: #f1efe8;
  --border: rgba(0,0,0,0.12); --border2: rgba(0,0,0,0.22);
  --text: #1a1a18; --text2: #5f5e5a; --text3: #888780;
  --radius-md: 8px; --radius-lg: 12px;
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --font-mono: "SF Mono", "Cascadia Code", "Fira Code", monospace;
  --user-bg: #E6F1FB; --user-text: #042C53; --user-border: #B5D4F4; --user-label: #185FA5;
  --asst-bg: #EEEDFE; --asst-text: #26215C; --asst-border: #CECBF6; --asst-label: #3C3489;
  --sys-bg: #F1EFE8; --sys-text: #5f5e5a; --sys-border: rgba(0,0,0,0.15);
  --tool-bg: #E1F5EE; --tool-text: #04342C; --tool-border: #9FE1CB; --tool-label: #0F6E56;
  --danger-bg: #FCEBEB; --danger-text: #A32D2D;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1a18; --bg-alpha: rgba(26, 26, 24, 0.85);
    --surface: #242422; --surface2: #2c2c2a;
    --border: rgba(255,255,255,0.1); --border2: rgba(255,255,255,0.2);
    --text: #f0ede8; --text2: #b4b2a9; --text3: #888780;
    --user-bg: #0C447C; --user-text: #E6F1FB; --user-border: #185FA5; --user-label: #85B7EB;
    --asst-bg: #26215C; --asst-text: #EEEDFE; --asst-border: #534AB7; --asst-label: #AFA9EC;
    --sys-bg: #2c2c2a; --sys-text: #888780; --sys-border: rgba(255,255,255,0.1);
    --tool-bg: #04342C; --tool-text: #E1F5EE; --tool-border: #1D9E75; --tool-label: #5DCAA5;
    --danger-bg: #3d1a1a; --danger-text: #f09595;
  }
}

html, body { 
  background: var(--bg); color: var(--text); 
  font-family: var(--font-sans); font-size: 15px; 
  line-height: 1.6; min-height: 100vh; scroll-behavior: smooth;
}

/* Sticky Header */
header { 
  position: sticky; top: 0; z-index: 100;
  display: flex; align-items: center; justify-content: space-between;
  padding: 1rem 1.5rem; background: var(--bg-alpha);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.header-titles { display: flex; align-items: baseline; gap: 12px; }
header h1 { font-size: 18px; font-weight: 500; }
header span { font-size: 12px; color: var(--text3); font-family: var(--font-mono); }
#nav-controls { display: flex; align-items: center; gap: 1rem; }
#pagination { display: flex; align-items: center; gap: 8px; font-size: 13px; font-weight: 500; }

.page { max-width: 800px; margin: 0 auto; padding: 2rem 1.25rem 4rem; }

/* Input Elements */
#drop-zone {
  border: 1.5px dashed var(--border2); border-radius: var(--radius-lg);
  padding: 2rem 1.5rem; text-align: center; cursor: pointer;
  background: var(--surface2); transition: all 0.15s ease; user-select: none;
}
#drop-zone:hover, #drop-zone.over { background: var(--surface); border-color: var(--user-label); }
#drop-zone svg { display: block; margin: 0 auto 10px; opacity: 0.4; }
#drop-zone p { font-size: 13px; color: var(--text2); }
#drop-zone p b { font-weight: 500; color: var(--text); }

textarea {
  width: 100%; min-height: 180px; margin-top: 1rem;
  font-family: var(--font-mono); font-size: 12px; line-height: 1.6;
  padding: 10px 12px; border-radius: var(--radius-md);
  border: 0.5px solid var(--border2); background: var(--surface); color: var(--text);
  resize: vertical; outline: none; transition: border-color 0.15s;
}
textarea:focus { border-color: var(--user-label); }

/* Buttons */
.actions { display: flex; align-items: center; gap: 10px; margin-top: 10px; flex-wrap: wrap; }

button.primary {
  padding: 8px 20px; font-size: 13px; font-weight: 500; font-family: var(--font-sans);
  background: var(--surface); color: var(--text); border: 0.5px solid var(--border2);
  border-radius: var(--radius-md); cursor: pointer; transition: background 0.12s;
}
button.primary:hover { background: var(--surface2); }
button.primary:active { transform: scale(0.98); }

button.ghost { font-size: 13px; font-family: var(--font-sans); background: none; border: none; color: var(--text3); cursor: pointer; text-decoration: underline; padding: 0; }
button.ghost:hover { color: var(--text); }

button.icon-btn {
  background: var(--surface); border: 0.5px solid var(--border); color: var(--text);
  border-radius: var(--radius-md); cursor: pointer; padding: 4px 10px;
}
button.icon-btn:hover { background: var(--surface2); }

.hint { font-size: 12px; color: var(--text3); margin-left: auto; }
#error-box { display: none; margin-top: 10px; padding: 10px 14px; background: var(--danger-bg); color: var(--danger-text); border-radius: var(--radius-md); font-size: 13px; }

/* Conversation & Animations */
@keyframes popIn {
  0% { opacity: 0; transform: translateY(10px); }
  100% { opacity: 1; transform: translateY(0); }
}

#conversation { flex-direction: column; gap: 4px; }
.msg-group { 
  display: flex; flex-direction: column; position: relative;
  animation: popIn 0.3s cubic-bezier(0.2, 0.8, 0.2, 1) forwards; opacity: 0; 
}
.msg-group:nth-child(1) { animation-delay: 0.00s; }
.msg-group:nth-child(2) { animation-delay: 0.05s; }
.msg-group:nth-child(3) { animation-delay: 0.10s; }
.msg-group:nth-child(4) { animation-delay: 0.15s; }
.msg-group:nth-child(n+5) { animation-delay: 0.20s; }

.role-label { font-size: 10px; font-weight: 500; letter-spacing: 0.06em; text-transform: uppercase; margin-bottom: 4px; padding: 0 2px; }

.bubble { 
  position: relative; padding: 10px 14px; border-radius: 14px; 
  font-size: 14px; line-height: 1.7; max-width: 88%; 
  word-break: break-word; border: 0.5px solid transparent; 
}

/* Copy Button */
.copy-btn {
  position: absolute; top: 8px; right: 8px; background: rgba(0,0,0,0.1); 
  border: none; color: inherit; font-size: 10px; padding: 4px 8px; 
  border-radius: 4px; cursor: pointer; opacity: 0; transition: opacity 0.2s;
}
.bubble:hover .copy-btn { opacity: 1; }
.copy-btn:hover { background: rgba(0,0,0,0.2); }
@media (prefers-color-scheme: dark) { .copy-btn { background: rgba(255,255,255,0.1); } .copy-btn:hover { background: rgba(255,255,255,0.2); } }

/* Collapsible Content */
.content-wrapper { white-space: pre-wrap; }
.clamped { max-height: 200px; overflow: hidden; position: relative; }
.clamped::after {
  content: ""; position: absolute; bottom: 0; left: 0; right: 0; height: 60px;
  background: linear-gradient(to bottom, transparent, inherit);
}
.msg-user .clamped::after { background: linear-gradient(to bottom, transparent, var(--user-bg)); }
.msg-assistant .clamped::after { background: linear-gradient(to bottom, transparent, var(--asst-bg)); }
.msg-tool .clamped::after, .msg-function .clamped::after { background: linear-gradient(to bottom, transparent, var(--tool-bg)); }

.expand-btn {
  background: none; border: none; color: inherit; text-decoration: underline;
  cursor: pointer; font-size: 12px; font-weight: bold; padding: 4px 0; margin-top: 4px; opacity: 0.8;
}
.expand-btn:hover { opacity: 1; }

.spacer { height: 10px; }

/* Message Roles */
.msg-system { padding: 10px 14px; border-radius: var(--radius-md); margin: 6px 0 10px; border: 0.5px dashed var(--sys-border); background: var(--sys-bg); }
.msg-system .role-label { color: var(--text3); }
.msg-system .bubble { font-size: 13px; color: var(--sys-text); font-style: italic; background: none; border: none; padding: 0; max-width: 100%; }

.msg-user { align-items: flex-end; }
.msg-user .role-label { color: var(--user-label); }
.msg-user .bubble { background: var(--user-bg); color: var(--user-text); border-color: var(--user-border); border-radius: 14px 14px 4px 14px; }

.msg-assistant { align-items: flex-start; }
.msg-assistant .role-label { color: var(--asst-label); }
.msg-assistant .bubble { background: var(--asst-bg); color: var(--asst-text); border-color: var(--asst-border); border-radius: 14px 14px 14px 4px; }

.msg-tool, .msg-function { align-items: flex-start; }
.msg-tool .role-label, .msg-function .role-label { color: var(--tool-label); }
.msg-tool .bubble, .msg-function .bubble { background: var(--tool-bg); color: var(--tool-text); border-color: var(--tool-border); font-family: var(--font-mono); font-size: 12px; border-radius: 10px; }

.msg-other { align-items: flex-start; }
.msg-other .role-label { color: var(--text3); }
.msg-other .bubble { background: var(--surface2); color: var(--text); border-color: var(--border); border-radius: 10px; }

/* Markdown Elements */
pre {
  background: rgba(0,0,0,0.06); padding: 10px; border-radius: 6px; 
  margin: 8px 0; overflow-x: auto; font-family: var(--font-mono); font-size: 12px;
}
@media (prefers-color-scheme: dark) { pre { background: rgba(0,0,0,0.3); } }
code { font-family: var(--font-mono); font-size: 12.5px; background: rgba(0,0,0,0.06); padding: 2px 4px; border-radius: 4px; }
@media (prefers-color-scheme: dark) { code { background: rgba(0,0,0,0.3); } }
pre code { background: transparent; padding: 0; font-size: 12px; }

/* Stats Bar & Floating Controls */
#stats-bar { align-items: center; gap: 16px; margin-top: 24px; padding-top: 16px; border-top: 0.5px solid var(--border); font-size: 12px; color: var(--text3); flex-wrap: wrap; }
#stats-bar b { color: var(--text2); font-weight: 500; }

#scroll-controls { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 50; opacity: 0; pointer-events: none; transition: opacity 0.2s; }
#scroll-controls.visible { opacity: 1; pointer-events: auto; }
.fab { background: var(--surface); color: var(--text); border: 1px solid var(--border2); border-radius: 50%; width: 44px; height: 44px; font-size: 18px; cursor: pointer; box-shadow: 0 4px 12px rgba(0,0,0,0.1); transition: all 0.2s; }
.fab:hover { background: var(--surface2); transform: translateY(-2px); box-shadow: 0 6px 16px rgba(0,0,0,0.15); }
.fab:active { transform: translateY(0); }