:root {
  --bg: #0a0e1a;
  --panel: #10182b;
  --panel-2: #0d1424;
  --ink: #e6edf7;
  --muted: #7b8bb0;
  --accent: #38e8b0;
  --accent-2: #ffb454;
  --user: #2f6df6;
  --bot: #1b2540;
  --border: #23304f;
  --font-mono: "IBM Plex Mono", ui-monospace, monospace;
  --font-disp: "Bricolage Grotesque", sans-serif;
}

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

body {
  font-family: var(--font-mono);
  color: var(--ink);
  min-height: 100vh;
  background:
    radial-gradient(circle at 15% 10%, rgba(56,232,176,0.10), transparent 40%),
    radial-gradient(circle at 85% 90%, rgba(255,180,84,0.08), transparent 45%),
    var(--bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 18px 14px;
  background-attachment: fixed;
}

.app-shell {
  width: 100%;
  max-width: 580px;
  display: flex;
  flex-direction: column;
  align-items: center;
  animation: rise 0.6s cubic-bezier(0.2, 0.8, 0.2, 1);
}
@keyframes rise { from { opacity: 0; transform: translateY(18px); } to { opacity: 1; transform: none; } }

.chat-card {
  width: 100%;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 18px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  height: min(74vh, 680px);
  box-shadow: 0 24px 60px rgba(0,0,0,0.55);
}

/* header */
.chat-header {
  background: linear-gradient(135deg, #0f1830, #131f3d);
  border-bottom: 1px solid var(--border);
  padding: 14px 16px 10px;
}
.header-top { display: flex; align-items: center; gap: 12px; }
.logo-mark {
  font-size: 26px;
  filter: drop-shadow(0 0 8px rgba(56,232,176,0.6));
  animation: swing 3.5s ease-in-out infinite;
}
@keyframes swing { 0%,100% { transform: rotate(-8deg); } 50% { transform: rotate(8deg); } }
.header-titles h1 {
  font-family: var(--font-disp);
  font-weight: 800;
  font-size: 20px;
  letter-spacing: 1.5px;
  color: var(--ink);
}
.header-titles p {
  font-size: 10.5px;
  color: var(--accent);
  letter-spacing: 0.3px;
  margin-top: 2px;
}

.stats-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.chip {
  font-size: 11px;
  color: var(--muted);
  background: var(--panel-2);
  border: 1px solid var(--border);
  padding: 4px 9px;
  border-radius: 20px;
  display: flex; align-items: center; gap: 5px;
}
.chip b { color: var(--accent-2); font-weight: 700; }
.temp-chip { flex: 1; min-width: 150px; }
.temp-chip input[type=range] {
  flex: 1;
  accent-color: var(--accent);
  height: 3px;
}

/* engine mode toggle */
.mode-row {
  display: flex;
  align-items: center;
  gap: 9px;
  margin-top: 10px;
}
.mode-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--muted);
}
.mode-toggle {
  display: flex;
  flex: 1;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 3px;
  gap: 3px;
}
.mode-btn {
  flex: 1;
  font-family: var(--font-mono);
  font-size: 11px;
  padding: 6px 8px;
  border: none;
  border-radius: 16px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}
.mode-btn:hover { color: var(--ink); }
.mode-btn.active {
  background: linear-gradient(135deg, var(--accent), #24c795);
  color: #06251b;
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(56,232,176,0.25);
}

/* messages */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--panel-2);
}
.chat-messages::-webkit-scrollbar { width: 8px; }
.chat-messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 8px; }

.msg-row { display: flex; }
.msg-row.right { justify-content: flex-end; }
.msg-row.left { justify-content: flex-start; }

.bubble {
  max-width: 84%;
  padding: 9px 13px;
  border-radius: 16px;
  line-height: 1.45;
  font-size: 13.5px;
  animation: pop 0.25s ease;
  word-wrap: break-word;
}
@keyframes pop { from { opacity: 0; transform: translateY(6px) scale(0.98); } to { opacity: 1; transform: none; } }

.bubble.user {
  background: linear-gradient(135deg, var(--user), #1a52d4);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.bubble.bot {
  background: var(--bot);
  border: 1px solid var(--border);
  color: var(--ink);
  border-bottom-left-radius: 4px;
}

.gen-label {
  font-size: 9.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-bottom: 5px;
  opacity: 0.85;
}
.label-matched { color: var(--accent); }
.label-matched-fallback { color: var(--accent-2); }
.label-tf-idf---4-gram { color: var(--accent-2); }
.label-simhash---4-gram { color: var(--accent-2); }
.label-improvised { color: var(--accent-2); }
.label-no-match { color: var(--accent-2); }
.label-system { color: var(--muted); }

.msg-text { white-space: pre-wrap; }

/* typing */
.bubble.typing { display: flex; gap: 5px; padding: 12px 15px; align-items: center; }
.dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--muted);
  animation: bounce 1.2s infinite ease-in-out;
}
.dot:nth-child(2) { animation-delay: 0.18s; }
.dot:nth-child(3) { animation-delay: 0.36s; }
@keyframes bounce { 0%,60%,100% { transform: translateY(0); opacity: 0.4; } 30% { transform: translateY(-5px); opacity: 1; } }

/* suggestions */
.suggestions {
  display: flex;
  gap: 7px;
  padding: 10px 12px 4px;
  overflow-x: auto;
  background: var(--panel);
  border-top: 1px solid var(--border);
}
.suggestions::-webkit-scrollbar { display: none; }
.sugg {
  flex: 0 0 auto;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--accent);
  background: transparent;
  border: 1px solid var(--border);
  padding: 5px 11px;
  border-radius: 20px;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s;
}
.sugg:hover { background: var(--accent); color: #06251b; border-color: var(--accent); }

/* input */
.chat-input-area {
  display: flex;
  gap: 8px;
  padding: 12px;
  background: var(--panel);
  border-top: 1px solid var(--border);
}
.text-in {
  flex: 1;
  font-family: var(--font-mono);
  font-size: 13.5px;
  padding: 11px 15px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--ink);
  border-radius: 22px;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}
.text-in:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(56,232,176,0.12); }
.text-in::placeholder { color: var(--muted); }

.send-btn {
  font-family: var(--font-disp);
  font-weight: 700;
  font-size: 13px;
  letter-spacing: 0.5px;
  padding: 10px 18px;
  border: none;
  border-radius: 22px;
  background: linear-gradient(135deg, var(--accent), #24c795);
  color: #06251b;
  cursor: pointer;
  transition: transform 0.12s, opacity 0.2s;
}
.send-btn:hover:not(:disabled) { transform: translateY(-1px); }
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.clear-btn {
  font-size: 16px;
  width: 42px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--muted);
  border-radius: 22px;
  cursor: pointer;
  transition: color 0.2s, border-color 0.2s;
}
.clear-btn:hover { color: var(--accent-2); border-color: var(--accent-2); }

/* set-rules chip / button */
.rules-chip {
  cursor: pointer;
  color: var(--accent);
  border-color: var(--accent);
  background: rgba(56,232,176,0.08);
  font-family: var(--font-mono);
  font-weight: 600;
  transition: all 0.15s;
}
.rules-chip:hover { background: var(--accent); color: #06251b; }

/* rules editor modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(4,7,15,0.78);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
  z-index: 50;
  animation: fade 0.2s ease;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.rules-modal {
  width: 100%;
  max-width: 620px;
  max-height: 88vh;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 24px 70px rgba(0,0,0,0.6);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: rise 0.3s cubic-bezier(0.2,0.8,0.2,1);
}
.rules-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(135deg, #0f1830, #131f3d);
}
.rules-head h2 {
  font-family: var(--font-disp);
  font-weight: 800;
  font-size: 17px;
  letter-spacing: 0.5px;
  color: var(--ink);
}
.x-btn {
  font-size: 24px;
  line-height: 1;
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  border-radius: 8px;
  transition: color 0.15s, background 0.15s;
}
.x-btn:hover { color: var(--accent-2); background: var(--panel-2); }

.rules-hint {
  font-size: 11.5px;
  line-height: 1.5;
  color: var(--muted);
  padding: 12px 18px 6px;
}
.rules-hint code {
  color: var(--accent);
  font-size: 11px;
  background: var(--panel-2);
  padding: 1px 5px;
  border-radius: 4px;
}

.rules-textarea {
  flex: 1;
  min-height: 240px;
  margin: 6px 18px;
  padding: 12px;
  font-family: var(--font-mono);
  font-size: 12.5px;
  line-height: 1.5;
  color: var(--ink);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  resize: vertical;
  outline: none;
  tab-size: 2;
}
.rules-textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(56,232,176,0.12); }

.rules-error {
  margin: 4px 18px;
  padding: 9px 12px;
  font-size: 11.5px;
  line-height: 1.45;
  color: #ffd0d0;
  background: rgba(220,60,60,0.12);
  border: 1px solid rgba(220,60,60,0.4);
  border-radius: 8px;
}

.rules-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 18px 16px;
}
.rules-spacer { flex: 1; }
.reset-rules-btn, .cancel-btn, .apply-btn {
  font-family: var(--font-mono);
  font-size: 12px;
  padding: 9px 15px;
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.15s;
}
.reset-rules-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
}
.reset-rules-btn:hover { color: var(--accent-2); border-color: var(--accent-2); }
.cancel-btn {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--ink);
}
.cancel-btn:hover { border-color: var(--muted); }
.apply-btn {
  font-family: var(--font-disp);
  font-weight: 700;
  letter-spacing: 0.4px;
  background: linear-gradient(135deg, var(--accent), #24c795);
  color: #06251b;
  border: none;
}
.apply-btn:hover { transform: translateY(-1px); }

.foot {
  margin-top: 14px;
  font-size: 11px;
  color: var(--muted);
  text-align: center;
}
.foot a { color: var(--accent); text-decoration: none; border-bottom: 1px dotted var(--accent); }
.foot a:hover { color: var(--accent-2); border-color: var(--accent-2); }

@media (max-width: 480px) {
  .chat-card { height: 82vh; }
  .header-titles h1 { font-size: 17px; }
}