:root {
  --bg: #120a1e;
  --panel: #1d122b;
  --panel2: #281a3a;
  --text: #fff7fb;
  --muted: #cdbfd8;
  --accent: #e9b4ff;
  --accent2: #ffd38a;
  --danger: #ff7b91;
  --line: rgba(255,255,255,.14);
  --shadow: 0 18px 44px rgba(0,0,0,.35);
  --soft: rgba(255,255,255,.055);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }
body {
  margin: 0;
  font: 15px/1.45 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}

button, input, textarea {
  font: inherit;
}

button, .import-btn {
  border: 1px solid var(--line);
  background: #342248;
  color: var(--text);
  border-radius: 14px;
  padding: 10px 13px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 42px;
  text-decoration: none;
}
button:hover, .import-btn:hover { border-color: rgba(255,255,255,.32); }
button.danger { background: rgba(255,123,145,.12); border-color: rgba(255,123,145,.45); color: #ffd6df; }
button.ghost { background: transparent; }
button.small { min-height: 34px; padding: 6px 10px; border-radius: 12px; font-size: 13px; }
button:disabled { opacity: .55; cursor: not-allowed; }

.app-shell {
  width: min(1400px, 100%);
  margin: 0 auto;
  padding: 18px;
}

.top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 24px;
  background: linear-gradient(135deg, rgba(233,180,255,.08), rgba(255,211,138,.04)), var(--panel);
  box-shadow: var(--shadow);
}
.eyebrow {
  margin: 0 0 4px;
  color: var(--accent2);
  text-transform: uppercase;
  letter-spacing: .12em;
  font-weight: 700;
  font-size: 12px;
}
h1 { margin: 0; font-size: clamp(28px, 5vw, 44px); }
h2 { margin: 0 0 8px; }
.sub { margin: 6px 0 0; color: var(--muted); }
.top-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.notice {
  margin: 14px 0;
  padding: 12px 14px;
  border: 1px solid rgba(255,211,138,.25);
  border-radius: 18px;
  background: rgba(255,211,138,.08);
  color: #fff2d6;
}
.notice a { color: var(--accent2); font-weight: 700; }

.layout {
  display: grid;
  grid-template-columns: minmax(260px, 360px) minmax(0, 1fr);
  gap: 14px;
  min-height: calc(100vh - 170px);
}
.library, .detail {
  border: 1px solid var(--line);
  background: rgba(29,18,43,.96);
  border-radius: 24px;
  box-shadow: var(--shadow);
  min-width: 0;
}
.library {
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: calc(100vh - 175px);
  overflow: hidden;
}
.detail {
  padding: 16px;
  overflow: auto;
  max-height: calc(100vh - 175px);
}

.label {
  display: block;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}
input, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px;
  background: #120a1e;
  color: var(--text);
  resize: vertical;
}
input:focus, textarea:focus {
  outline: 2px solid rgba(233,180,255,.35);
  border-color: rgba(233,180,255,.7);
}
.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.chip {
  min-height: 34px;
  border-radius: 999px;
  padding: 7px 11px;
  background: #241633;
}
.chip.active {
  background: rgba(233,180,255,.2);
  border-color: rgba(233,180,255,.55);
}
.count {
  color: var(--muted);
  font-size: 13px;
}
.entry-list {
  overflow: auto;
  display: grid;
  gap: 8px;
  padding-right: 4px;
}
.entry {
  width: 100%;
  display: block;
  text-align: left;
  border-radius: 16px;
  background: #211530;
  padding: 12px;
}
.entry[aria-selected="true"] {
  border-color: var(--accent);
  background: rgba(233,180,255,.12);
}
.entry-title { font-weight: 750; margin-bottom: 3px; }
.entry-meta { font-size: 12px; color: var(--muted); }

.empty {
  min-height: 220px;
  display: grid;
  place-content: center;
  text-align: center;
  color: var(--muted);
}
.detail-card {
  max-width: 900px;
  margin: 0 auto;
}
.card-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}
.card-title { margin: 0; font-size: clamp(24px, 4vw, 40px); }
.card-meta { color: var(--muted); }
.section {
  margin: 14px 0;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: rgba(255,255,255,.035);
}
.section h3 { margin: 0 0 8px; color: var(--accent2); }
.pre {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.tag-row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.tag {
  font-size: 12px;
  color: #eddcff;
  background: rgba(233,180,255,.12);
  border: 1px solid rgba(233,180,255,.18);
  border-radius: 999px;
  padding: 4px 8px;
}

.entry-form {
  max-width: 880px;
  margin: 0 auto;
  display: grid;
  gap: 12px;
}
.entry-form label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-weight: 700;
}
.entry-form label input,
.entry-form label textarea {
  color: var(--text);
  font-weight: 400;
}
.form-head, .form-row, .form-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}
.check {
  display: inline-flex !important;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 8px !important;
}
.check input { width: auto; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
  border: 0;
}

.chat-backdrop {
  position: fixed;
  inset: 0;
  z-index: 18;
  background: rgba(8,4,13,.42);
}
body.chat-open { overflow: hidden; }

.chat-fab {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 10;
  border-radius: 999px;
  background: linear-gradient(135deg, rgba(233,180,255,.9), rgba(255,211,138,.88));
  color: #16091f;
  font-weight: 850;
  box-shadow: var(--shadow);
  padding-inline: 16px;
}
.chat {
  position: fixed;
  right: 18px;
  bottom: 76px;
  z-index: 20;
  width: min(460px, calc(100vw - 36px));
  height: min(700px, calc(100vh - 104px));
  display: grid;
  grid-template-rows: auto auto minmax(120px, 1fr) auto auto;
  border: 1px solid rgba(233,180,255,.18);
  border-radius: 26px;
  overflow: hidden;
  background: #160d23;
  box-shadow: 0 26px 70px rgba(0,0,0,.48);
}
.chat-head {
  padding: 13px 13px 11px;
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  gap: 10px;
  background: linear-gradient(135deg, rgba(233,180,255,.09), rgba(255,211,138,.04));
}
.chat-title-block { min-width: 0; }
.chat-kicker {
  margin: 0 0 2px;
  color: var(--accent2);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: .1em;
  text-transform: uppercase;
}
.chat-head strong { display: block; font-size: 18px; }
.chat-head span {
  display: block;
  color: var(--muted);
  font-size: 12px;
}
.chat-head-actions {
  display: flex;
  align-items: flex-start;
  gap: 6px;
}
.chat-head-actions button:last-child {
  min-height: 34px;
  width: 36px;
  padding: 0;
  border-radius: 12px;
  font-size: 22px;
  line-height: 1;
}
.chat-context {
  padding: 9px 13px;
  border-bottom: 1px solid rgba(255,255,255,.08);
  background: rgba(255,255,255,.035);
  color: var(--muted);
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chat-messages {
  overflow: auto;
  padding: 13px;
  display: grid;
  align-content: start;
  gap: 10px;
  overscroll-behavior: contain;
}
.msg {
  border-radius: 17px;
  padding: 10px 12px;
  background: var(--soft);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  max-width: 92%;
}
.msg.user {
  justify-self: end;
  background: rgba(233,180,255,.16);
  border: 1px solid rgba(233,180,255,.12);
}
.msg.ai {
  justify-self: start;
}
.msg.loading::after {
  content: "";
  display: inline-block;
  width: .55em;
  height: .55em;
  margin-left: .35em;
  border-radius: 50%;
  background: currentColor;
  opacity: .55;
  vertical-align: baseline;
}
.msg.error {
  border: 1px solid rgba(255,123,145,.55);
  background: rgba(255,123,145,.08);
}
.chat-prompts {
  display: flex;
  gap: 7px;
  overflow-x: auto;
  padding: 10px 12px 0;
  border-top: 1px solid rgba(255,255,255,.06);
}
.chat-prompts button {
  min-height: 32px;
  white-space: nowrap;
  border-radius: 999px;
  padding: 7px 10px;
  background: rgba(255,255,255,.045);
  font-size: 13px;
}
.chat-form {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  padding: 12px;
}
.chat-form textarea {
  min-height: 48px;
  max-height: 150px;
  resize: none;
}
.chat-form button { align-self: end; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  background: #fff7fb;
  color: #16091f;
  padding: 10px 14px;
  border-radius: 999px;
  box-shadow: var(--shadow);
  font-weight: 700;
  z-index: 50;
}

@media (max-width: 850px) {
  .app-shell { padding: 10px; }
  .top { display: grid; border-radius: 18px; }
  .top-actions { justify-content: start; }
  .layout { grid-template-columns: 1fr; min-height: auto; }
  .library, .detail { max-height: none; border-radius: 18px; }
  .entry-list { max-height: 38vh; }
  .chat {
    inset: auto 8px 8px 8px;
    width: auto;
    height: min(82vh, 720px);
    max-height: none;
    border-radius: 22px;
  }
  .chat-fab { right: 12px; bottom: 12px; }
  .chat-prompts { padding-bottom: 2px; }
  .msg { max-width: 96%; }
}

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; transition: none !important; animation: none !important; }
}
