:root {
  --bg: #0b0509;
  --panel: #150b12;
  --panel-2: #1e121b;
  --line: #2c1c28;
  --text: #f3e9ef;
  --muted: #a98fa0;
  --pink: #ff2da6;
  --pink-dim: #c71f80;
  --gold: #e7c873;
  --radius: 12px;
  --font: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}
button { font-family: inherit; cursor: pointer; }
textarea, input, select { font-family: inherit; }

/* ---- Login ---- */
.login-body {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh;
  background: radial-gradient(1200px 600px at 50% -10%, #22101c 0%, var(--bg) 60%);
}
.login-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 40px 36px;
  width: 320px;
  display: flex; flex-direction: column; gap: 14px;
  box-shadow: 0 20px 60px rgba(0,0,0,.5);
}
.login-logo {
  font-weight: 800; font-size: 26px; text-align: center;
  color: var(--pink);
  text-shadow: 0 0 24px rgba(255,45,166,.4);
}
.login-sub { text-align: center; color: var(--muted); margin: -6px 0 8px; font-size: 13px; }
.login-card input {
  background: var(--panel-2); border: 1px solid var(--line);
  color: var(--text); padding: 12px 14px; border-radius: 10px; outline: none;
}
.login-card input:focus { border-color: var(--pink); }
.login-card button {
  background: var(--pink); color: #fff; border: none;
  padding: 12px; border-radius: 10px; font-weight: 700;
}
.login-card button:hover { background: var(--pink-dim); }
.login-err { color: #ff7a9c; text-align: center; font-size: 13px; min-height: 16px; }

/* ---- Layout ---- */
#app { display: flex; height: 100vh; overflow: hidden; }

#sidebar {
  width: 260px; flex-shrink: 0;
  background: var(--panel); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; padding: 14px;
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 14px; }
.logo {
  font-weight: 800; color: var(--pink); font-size: 15px; letter-spacing: -.2px;
  text-shadow: 0 0 18px rgba(255,45,166,.35);
  flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.hidden { display: none !important; }

.view-tabs { display: flex; gap: 3px; margin-bottom: 10px; background: var(--panel-2); padding: 3px; border-radius: 10px; }
.view-tab {
  flex: 1; background: transparent; border: none; color: var(--muted);
  padding: 7px 3px; border-radius: 8px; font-size: 12px; font-weight: 600; white-space: nowrap;
}
.view-tab:hover { color: var(--text); }
.view-tab.active { background: var(--panel); color: var(--pink); }

#conv-list, #script-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; }

/* library list */
.script-item {
  padding: 9px 11px; border-radius: 9px; cursor: pointer;
  display: flex; flex-direction: column; gap: 5px; font-size: 13px;
  color: var(--muted); overflow: hidden;
}
.script-item:hover { background: var(--panel-2); color: var(--text); }
.script-item.active { background: var(--panel-2); color: var(--text); border: 1px solid var(--line); }
.item-top { display: flex; align-items: center; gap: 7px; min-width: 0; }
.script-item .title {
  flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  color: var(--text); font-weight: 500;
}
.item-star {
  background: none; border: none; color: var(--muted); font-size: 14px;
  line-height: 1; padding: 0; flex-shrink: 0; opacity: 0; transition: opacity .1s;
}
.script-item:hover .item-star { opacity: .7; }
.item-star.pinned { color: var(--gold); opacity: 1; }
.item-star:hover { color: var(--gold); opacity: 1; }
.badge {
  font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .4px;
  padding: 2px 6px; border-radius: 6px; flex-shrink: 0;
}
.badge.draft     { background: #2c2230; color: #c3a9bd; }
.badge.to_record { background: #3a2b12; color: var(--gold); }
.badge.recorded  { background: #123a24; color: #6fe0a0; }
.badge.posted    { background: #2a1236; color: #d08cff; }
.conv {
  padding: 9px 11px; border-radius: 9px; color: var(--muted);
  cursor: pointer; display: flex; align-items: center; gap: 8px;
  font-size: 13px; white-space: nowrap; overflow: hidden;
}
.conv:hover { background: var(--panel-2); color: var(--text); }
.conv.active { background: var(--panel-2); color: var(--text); border: 1px solid var(--line); }
.conv .title { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.conv .edit, .conv .del { opacity: 0; color: var(--muted); flex-shrink: 0; }
.conv .edit { font-size: 12px; }
.conv .del { font-size: 15px; }
.conv:hover .edit, .conv:hover .del { opacity: .7; }
.conv .edit:hover { color: var(--gold); opacity: 1; }
.conv .del:hover { color: #ff7a9c; opacity: 1; }
.conv-rename {
  flex: 1; min-width: 0; background: var(--bg); color: var(--text);
  border: 1px solid var(--pink); border-radius: 6px; padding: 3px 7px;
  font-size: 13px; outline: none;
}

.btn-primary { background: var(--pink); color: #fff; border: none; padding: 8px 14px; border-radius: 9px; font-weight: 700; white-space: nowrap; flex-shrink: 0; }
.btn-primary:hover { background: var(--pink-dim); }
.btn-primary:disabled { opacity: .5; cursor: default; }
.btn-ghost { background: transparent; color: var(--muted); border: 1px solid var(--line); padding: 8px; border-radius: 9px; margin-top: 10px; }
.btn-ghost:hover { color: var(--text); border-color: var(--pink-dim); }
.sidebar-link { display: block; text-align: center; text-decoration: none; color: var(--gold); }
.sidebar-link:hover { color: var(--gold); border-color: rgba(231,200,115,.5); }

/* ---- Main ---- */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#chat-view { flex: 1; display: flex; flex-direction: column; min-height: 0; }

#toolbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; padding: 12px 20px; border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.modes { display: flex; gap: 6px; flex-wrap: wrap; }
.mode-btn {
  background: var(--panel-2); color: var(--muted); border: 1px solid var(--line);
  padding: 6px 12px; border-radius: 20px; font-size: 12.5px;
}
.mode-btn:hover { color: var(--text); }
.mode-btn.active { background: rgba(255,45,166,.15); color: var(--pink); border-color: var(--pink-dim); }

.model-pick { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
#model-select, #persona-select {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--line);
  padding: 7px 10px; border-radius: 9px; max-width: 260px; outline: none;
}
#model-select:focus, #persona-select:focus { border-color: var(--pink); }
#persona-select { max-width: 200px; }
/* highlight when a persona is active */
#persona-select.active { border-color: var(--pink-dim); color: var(--pink); }
.allmodels { color: var(--muted); font-size: 12px; display: flex; align-items: center; gap: 5px; white-space: nowrap; }
.allmodels input { accent-color: var(--pink); }

/* ---- Messages ---- */
#messages { flex: 1; min-height: 0; overflow-y: auto; padding: 24px 0; }
.msg-row { display: flex; padding: 6px 24px; }
.msg-row.user { justify-content: flex-end; }
.msg {
  max-width: 760px; padding: 13px 16px; border-radius: 14px;
  line-height: 1.55; white-space: pre-wrap; word-wrap: break-word;
}
.msg-row.user .msg { background: rgba(255,45,166,.14); border: 1px solid var(--pink-dim); }
.msg-row.assistant .msg { background: var(--panel); border: 1px solid var(--line); max-width: 820px; }
.msg-row.assistant .msg { position: relative; }
.msg-actions {
  position: absolute; top: 8px; right: 8px; display: flex; gap: 6px;
  opacity: 0; transition: opacity .12s;
}
.msg-row.assistant .msg:hover .msg-actions { opacity: 1; }
.copy-btn {
  background: var(--panel-2); border: 1px solid var(--line); color: var(--muted);
  font-size: 11px; padding: 3px 8px; border-radius: 7px;
}
.copy-btn:hover { color: var(--pink); }
.cursor::after { content: "▋"; color: var(--pink); animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }

.empty { text-align: center; color: var(--muted); margin-top: 12vh; }
.empty h1 { color: var(--text); font-weight: 700; font-size: 26px; margin-bottom: 6px; }

/* ---- Composer ---- */
#composer {
  display: flex; gap: 10px; padding: 14px 20px 18px;
  border-top: 1px solid var(--line); align-items: flex-end;
}
#input {
  flex: 1; resize: none; max-height: 220px;
  background: var(--panel-2); color: var(--text); border: 1px solid var(--line);
  border-radius: 12px; padding: 12px 14px; outline: none; line-height: 1.5;
}
#input:focus { border-color: var(--pink); }
#send { align-self: stretch; padding: 0 22px; }

/* ---- Library editor ---- */
#library-view { flex: 1; display: flex; flex-direction: column; min-height: 0; }
#editor { flex: 1; display: flex; flex-direction: column; min-height: 0; }
#editor-head {
  display: flex; align-items: center; gap: 12px; padding: 14px 20px;
  border-bottom: 1px solid var(--line); flex-wrap: wrap;
}
#script-title {
  flex: 1; min-width: 180px; background: transparent; border: none;
  color: var(--text); font-size: 18px; font-weight: 700; outline: none; padding: 4px 0;
}
#script-title:focus { border-bottom: 1px solid var(--pink); }
#script-status {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--line);
  padding: 7px 10px; border-radius: 9px; outline: none; font-weight: 600;
}
#script-status:focus { border-color: var(--pink); }
#save-state { color: var(--muted); font-size: 12px; min-width: 54px; }
.editor-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.btn-ghost.danger:hover { color: #ff7a9c; border-color: #ff7a9c; }

.lib-btn { color: var(--gold) !important; }
.lib-btn:hover { color: var(--pink) !important; }

/* ---- Library filters (sidebar) ---- */
#lib-controls { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
#lib-search {
  background: var(--panel-2); border: 1px solid var(--line); color: var(--text);
  padding: 8px 11px; border-radius: 9px; outline: none; font-size: 13px;
}
#lib-search:focus { border-color: var(--pink); }
.chip-row { display: flex; flex-wrap: wrap; gap: 5px; }
#tag-filter-wrap { display: flex; flex-direction: column; gap: 6px; }
.filter-toggle {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--panel-2); border: 1px solid var(--line); color: var(--muted);
  padding: 7px 11px; border-radius: 9px; font-size: 12.5px; font-weight: 600;
  cursor: pointer; width: 100%;
}
.filter-toggle:hover { color: var(--text); }
.filter-toggle.open { color: var(--pink); border-color: var(--pink-dim); }
.filter-toggle .chevron { transition: transform .15s; font-size: 11px; }
.filter-toggle.open .chevron { transform: rotate(180deg); }
.chip {
  background: var(--panel-2); border: 1px solid var(--line); color: var(--muted);
  font-size: 11.5px; padding: 4px 9px; border-radius: 20px; cursor: pointer;
  white-space: nowrap;
}
.chip:hover { color: var(--text); }
.chip.active { background: rgba(255,45,166,.15); color: var(--pink); border-color: var(--pink-dim); }
.chip.tag::before { content: "#"; opacity: .5; margin-right: 1px; }
.clear-filters {
  align-self: flex-start; background: none; border: none; color: var(--gold);
  font-size: 11.5px; padding: 2px 0; text-decoration: underline; cursor: pointer;
}
.clear-filters:hover { color: var(--pink); }
.list-empty { color: var(--muted); font-size: 12.5px; padding: 10px 4px; text-align: center; }

/* tags on library list items */
.script-item .badge { flex-shrink: 0; }
.script-item .item-tags {
  display: flex; flex-wrap: wrap; gap: 5px; padding-left: 21px;
}
.mini-tag { font-size: 10px; color: var(--gold); opacity: .85; white-space: nowrap; }
.mini-more { font-size: 10px; color: var(--muted); font-weight: 600; white-space: nowrap; }

/* ---- Editor: segmented toggle, tags, preview, stats ---- */
.star-btn {
  background: none; border: none; color: var(--muted); font-size: 20px;
  line-height: 1; padding: 0 2px; flex-shrink: 0;
}
.star-btn:hover { color: var(--gold); }
.star-btn.pinned { color: var(--gold); }

.segmented { display: inline-flex; background: var(--panel-2); border: 1px solid var(--line); border-radius: 9px; overflow: hidden; }
.seg { background: transparent; border: none; color: var(--muted); padding: 6px 12px; font-size: 12.5px; font-weight: 600; }
.seg:hover { color: var(--text); }
.seg.active { background: var(--pink); color: #fff; }

#tags-row {
  display: flex; align-items: center; flex-wrap: wrap; gap: 6px;
  padding: 10px 20px; border-bottom: 1px solid var(--line);
}
#tag-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.tag-chip {
  display: inline-flex; align-items: center; gap: 5px; background: rgba(231,200,115,.12);
  border: 1px solid rgba(231,200,115,.3); color: var(--gold);
  font-size: 12px; padding: 3px 8px; border-radius: 16px;
}
.tag-chip .x { cursor: pointer; opacity: .7; font-weight: 700; }
.tag-chip .x:hover { opacity: 1; color: var(--pink); }
#tag-input {
  background: transparent; border: none; color: var(--text); outline: none;
  font-size: 13px; padding: 4px; min-width: 90px; flex: 1;
}

#editor-body { flex: 1; display: flex; min-height: 0; }
#script-body {
  flex: 1; resize: none; background: var(--bg); color: var(--text);
  border: none; outline: none; padding: 22px 24px; line-height: 1.6;
  font-family: "SF Mono", ui-monospace, Menlo, Consolas, monospace; font-size: 14px;
}
#preview { flex: 1; overflow-y: auto; padding: 22px 28px; line-height: 1.65; }

#editor-foot { padding: 8px 20px; border-top: 1px solid var(--line); }
#script-stats { color: var(--muted); font-size: 12px; }

/* funscript panel */
#funscript-panel { border-top: 1px solid var(--line); padding: 12px 20px; background: var(--panel); }
.fs-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.fs-title { font-weight: 700; color: var(--gold); font-size: 13px; }
.fs-len { color: var(--muted); font-size: 12px; display: flex; align-items: center; gap: 6px; }
#fs-duration {
  width: 78px; background: var(--panel-2); color: var(--text); border: 1px solid var(--line);
  border-radius: 8px; padding: 6px 8px; outline: none; font-size: 13px; text-align: center;
}
#fs-duration:focus { border-color: var(--pink); }
#fs-state { color: var(--muted); font-size: 12px; }
.fs-close { background: none; border: none; color: var(--muted); font-size: 18px; cursor: pointer; margin-left: auto; }
.fs-close:hover { color: #ff7a9c; }
#fs-preview {
  width: 100%; height: 70px; margin-top: 12px; background: var(--bg);
  border: 1px solid var(--line); border-radius: 8px;
}
.fs-hint { color: var(--muted); font-size: 11.5px; margin-top: 8px; }

/* ---- Rendered markdown ---- */
.markdown h1, .markdown h2, .markdown h3 { color: var(--text); font-weight: 700; margin: 1.1em 0 .5em; line-height: 1.25; }
.markdown h1 { font-size: 1.55em; }
.markdown h2 { font-size: 1.3em; }
.markdown h3 { font-size: 1.1em; color: var(--gold); }
.markdown p { margin: .6em 0; }
.markdown ul, .markdown ol { margin: .5em 0; padding-left: 1.4em; }
.markdown li { margin: .25em 0; }
.markdown strong { color: #fff; font-weight: 700; }
.markdown em { color: var(--text); font-style: italic; }
.markdown code { background: var(--panel-2); padding: 1px 5px; border-radius: 5px; font-size: .9em; }
.markdown blockquote { border-left: 3px solid var(--pink-dim); margin: .6em 0; padding: 2px 0 2px 14px; color: var(--muted); }
.markdown hr { border: none; border-top: 1px solid var(--line); margin: 1.2em 0; }
.markdown a { color: var(--pink); }
/* GWA cue highlighting in preview */
.cue-tag { color: var(--pink); background: rgba(255,45,166,.1); border-radius: 4px; padding: 0 3px; font-weight: 600; }
.cue-perf { color: var(--gold); font-style: italic; }

/* ---- Sagas ---- */
#sagas-view { flex: 1; display: flex; flex-direction: column; min-height: 0; }
#saga-editor { flex: 1; display: flex; flex-direction: column; min-height: 0; }
#saga-editor-head {
  display: flex; align-items: center; gap: 12px; padding: 14px 20px;
  border-bottom: 1px solid var(--line); flex-wrap: wrap;
}
#saga-name {
  flex: 1; min-width: 180px; background: transparent; border: none;
  color: var(--text); font-size: 18px; font-weight: 700; outline: none; padding: 4px 0;
}
#saga-name:focus { border-bottom: 1px solid var(--pink); }
#saga-save-state { color: var(--muted); font-size: 12px; min-width: 54px; }

#saga-body { flex: 1; display: flex; min-height: 0; }
#saga-parts-panel {
  width: 340px; flex-shrink: 0; border-right: 1px solid var(--line);
  display: flex; flex-direction: column; padding: 16px 18px; overflow-y: auto;
}
#saga-notes-panel { flex: 1; min-width: 0; display: flex; flex-direction: column; padding: 16px 18px; }
.saga-section-label {
  font-size: 11px; text-transform: uppercase; letter-spacing: .5px;
  color: var(--muted); font-weight: 700; margin-bottom: 10px;
}
.saga-section-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.saga-section-head .saga-section-label { margin-bottom: 0; }
#saga-recap { font-size: 12px; }
#saga-recap:not(:disabled):hover { color: var(--gold); border-color: rgba(231,200,115,.4); }
.saga-notes-actions { display: flex; align-items: center; gap: 8px; }
#saga-notes-body { flex: 1; display: flex; min-height: 0; }
#saga-notes {
  flex: 1; resize: none; background: var(--bg); color: var(--text);
  border: 1px solid var(--line); border-radius: 10px; outline: none;
  padding: 14px 16px; line-height: 1.6; font-size: 14px;
}
#saga-notes:focus { border-color: var(--pink-dim); }
#saga-notes-preview {
  flex: 1; overflow-y: auto; border: 1px solid var(--line); border-radius: 10px;
  padding: 14px 18px; line-height: 1.65;
}

#saga-parts { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
.saga-part {
  display: flex; align-items: center; gap: 6px; background: var(--panel-2);
  border: 1px solid var(--line); border-radius: 9px; padding: 8px 10px; font-size: 13px;
}
.saga-part .num { color: var(--gold); font-weight: 700; flex-shrink: 0; }
.saga-part .p-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; color: var(--text); }
.saga-part .p-title:hover { color: var(--pink); }
.saga-part .p-status { font-size: 9.5px; text-transform: uppercase; color: var(--muted); flex-shrink: 0; }
.saga-part .p-btn {
  background: none; border: none; color: var(--muted); cursor: pointer;
  font-size: 13px; padding: 0 2px; flex-shrink: 0;
}
.saga-part .p-btn:hover { color: var(--pink); }
.saga-part .p-btn:disabled { opacity: .25; cursor: default; }
.saga-part .p-btn.rm:hover { color: #ff7a9c; }
.saga-parts-empty { color: var(--muted); font-size: 12.5px; padding: 4px 0 10px; }
#saga-add-row { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
#saga-add-select {
  flex: 1; min-width: 120px; background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; outline: none; font-size: 12.5px;
}
#saga-add-select:focus { border-color: var(--pink); }

/* saga picker in the library editor */
#script-saga {
  background: var(--panel-2); color: var(--muted); border: 1px solid var(--line);
  border-radius: 8px; padding: 5px 8px; outline: none; font-size: 12px; max-width: 180px;
}
#script-saga:focus { border-color: var(--pink); }
#script-saga.active { color: var(--gold); border-color: rgba(231,200,115,.4); }

/* ---- Personas ---- */
#personas-view { flex: 1; display: flex; flex-direction: column; min-height: 0; }
#persona-editor { flex: 1; display: flex; flex-direction: column; min-height: 0; }
#persona-editor-head {
  display: flex; align-items: center; gap: 12px; padding: 14px 20px;
  border-bottom: 1px solid var(--line); flex-wrap: wrap;
}
#persona-name {
  flex: 1; min-width: 180px; background: transparent; border: none;
  color: var(--text); font-size: 18px; font-weight: 700; outline: none; padding: 4px 0;
}
#persona-name:focus { border-bottom: 1px solid var(--pink); }
#persona-save-state { color: var(--muted); font-size: 12px; min-width: 54px; }
#persona-details-body { flex: 1; display: flex; min-height: 0; }
#persona-details {
  flex: 1; resize: none; background: var(--bg); color: var(--text);
  border: none; outline: none; padding: 20px 24px; line-height: 1.6; font-size: 14px;
}
#persona-details::placeholder { color: var(--muted); opacity: .7; }
#persona-details-preview { flex: 1; overflow-y: auto; padding: 20px 24px; line-height: 1.65; }
#persona-foot { padding: 8px 20px; border-top: 1px solid var(--line); }
.persona-hint { color: var(--muted); font-size: 12px; }

/* scrollbars */
::-webkit-scrollbar { width: 10px; }
::-webkit-scrollbar-thumb { background: var(--line); border-radius: 6px; }
::-webkit-scrollbar-thumb:hover { background: var(--panel-2); }

@media (max-width: 720px) {
  #sidebar { width: 200px; }
  .msg { max-width: 100%; }
}
