:root {
  --bg: #0b1220;
  --panel: #111a2d;
  --panel-2: #0e1627;
  --border: #1e2a44;
  --text: #e2e8f0;
  --muted: #7c8ba8;
  --primary: #60a5fa;
  --primary-hover: #3b82f6;
  --danger: #ef4444;
  --success: #22c55e;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
  font-size: 14px;
}
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 12px; }
.error { color: var(--danger); font-size: 12px; margin: 6px 0 0; }

button {
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--primary);
  color: #fff;
  padding: 8px 14px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  transition: background 0.15s;
}
button:hover { background: var(--primary-hover); }
button:disabled { opacity: 0.5; cursor: not-allowed; }
.icon-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  width: 32px;
  height: 32px;
  padding: 0;
  font-size: 18px;
}
.icon-btn:hover { background: var(--panel-2); color: var(--text); }
.danger-btn { background: transparent; color: var(--danger); border: 1px solid var(--border); }
.danger-btn:hover { background: rgba(239, 68, 68, 0.1); }
.ghost-btn { background: transparent; color: var(--muted); border: 1px solid var(--border); }
.ghost-btn:hover { background: var(--panel-2); color: var(--text); }

input, textarea {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 13px;
  font-family: inherit;
  outline: none;
  width: 100%;
}
input:focus, textarea:focus { border-color: var(--primary); }

/* -------- Login -------- */
.login-view {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
}
.login-card {
  background: var(--panel);
  border: 1px solid var(--border);
  padding: 32px;
  border-radius: 10px;
  width: 320px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.login-card h1 {
  margin: 0;
  font-weight: 400;
  font-size: 22px;
  letter-spacing: 1px;
  text-align: center;
}
.login-card p { margin: 0; text-align: center; }
.login-card button { padding: 10px; font-size: 14px; }

/* -------- App layout -------- */
.app-view {
  display: grid;
  grid-template-columns: 260px 1fr 360px;
  grid-template-areas: "sidebar main chat";
  height: 100vh;
  overflow: hidden;
  transition: grid-template-columns 0.2s ease;
}
.sidebar { grid-area: sidebar; }
.main-area { grid-area: main; }
.chat-panel { grid-area: chat; }
.app-view.no-doc {
  grid-template-columns: 260px 1fr;
  grid-template-areas: "sidebar chat";
}
.app-view.no-doc .main-area { display: none; }

/* Sidebar */
.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}
.sidebar-header {
  padding: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--border);
}
.sidebar-header h2 { margin: 0; font-size: 14px; font-weight: 500; letter-spacing: 0.5px; }
.doc-list {
  flex: 1;
  list-style: none;
  padding: 8px;
  margin: 0;
  overflow-y: auto;
}
.doc-list li {
  padding: 8px 12px;
  border-radius: 6px;
  cursor: pointer;
  margin-bottom: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.doc-list li:hover { background: var(--panel-2); }
.doc-list li.active { background: var(--primary); color: #fff; }
.sidebar-footer {
  padding: 12px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
}

/* Main area */
.main-area {
  background: var(--bg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 32px;
}
.empty-state h2 { font-weight: 300; font-size: 20px; margin-bottom: 8px; }
.empty-state p { max-width: 420px; }

.editor-pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding: 20px 24px;
  gap: 12px;
}
.editor-header {
  display: flex;
  gap: 12px;
  align-items: center;
}
.title-input {
  flex: 1;
  font-size: 18px;
  font-weight: 500;
  background: transparent;
  border: none;
  padding: 6px 0;
  border-bottom: 1px solid transparent;
}
.title-input:focus { border-bottom-color: var(--border); }
.editor-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}
.editor-wrap {
  flex: 1;
  min-height: 300px;
  background: #fff;
  color: #000;
  border-radius: 6px;
  overflow: hidden;
}
.toastui-editor-defaultUI {
  border: none !important;
  border-radius: 6px !important;
}

.files-section {
  border-top: 1px solid var(--border);
  padding-top: 12px;
}
.files-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}
.files-header h3 { margin: 0; font-size: 13px; font-weight: 500; text-transform: uppercase; letter-spacing: 0.5px; color: var(--muted); }
.upload-btn {
  cursor: pointer;
  background: var(--primary);
  color: #fff;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
}
.upload-btn:hover { background: var(--primary-hover); }
.file-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  max-height: 140px;
  overflow-y: auto;
}
.file-list li {
  background: var(--panel-2);
  border: 1px solid var(--border);
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.file-list .file-name { max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--primary-hover); text-decoration: none; }
.file-list .file-name:hover { text-decoration: underline; }
.file-list .remove-btn { cursor: pointer; color: var(--muted); background: none; border: none; padding: 0; font-size: 16px; }
.file-list .remove-btn:hover { color: var(--danger); }

/* Chat panel */
.chat-panel {
  background: var(--panel);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.chat-header {
  padding: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--border);
}
.chat-header h2 { margin: 0; font-size: 14px; font-weight: 500; letter-spacing: 0.5px; }
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.chat-msg {
  padding: 10px 14px;
  border-radius: 10px;
  max-width: 90%;
  line-height: 1.5;
  font-size: 13px;
  word-wrap: break-word;
}
.chat-msg.user .chat-msg-body { white-space: pre-wrap; }

/* Reset — tight spacing, GitHub-style */
.chat-msg-body { line-height: 1.45; }
.chat-msg-body > *:first-child { margin-top: 0 !important; }
.chat-msg-body > *:last-child  { margin-bottom: 0 !important; }

/* Headings */
.chat-msg-body h1,
.chat-msg-body h2,
.chat-msg-body h3,
.chat-msg-body h4,
.chat-msg-body h5,
.chat-msg-body h6 {
  margin: 0.8em 0 0.25em;
  font-weight: 600;
  line-height: 1.25;
}
.chat-msg-body h1 { font-size: 15px; }
.chat-msg-body h2 { font-size: 14px; }
.chat-msg-body h3,
.chat-msg-body h4,
.chat-msg-body h5,
.chat-msg-body h6 { font-size: 13px; }

/* Paragraphs */
.chat-msg-body p { margin: 0.35em 0; }

/* Lists — tight, no orphan <p> margins */
.chat-msg-body ul,
.chat-msg-body ol { margin: 0.35em 0; padding-left: 1.4em; }
.chat-msg-body ul ul,
.chat-msg-body ol ol,
.chat-msg-body ul ol,
.chat-msg-body ol ul { margin: 0.15em 0; }
.chat-msg-body li { margin: 0.1em 0; }
.chat-msg-body li > p { margin: 0.1em 0; }
.chat-msg-body li > p:only-child { margin: 0; }

/* Inline */
.chat-msg-body strong { font-weight: 600; }
.chat-msg-body em     { font-style: italic; }
.chat-msg-body a      { color: var(--primary); text-decoration: underline; }

/* Code */
.chat-msg-body code {
  background: rgba(255, 255, 255, 0.08);
  padding: 1px 5px;
  border-radius: 4px;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 12px;
}
.chat-msg.user .chat-msg-body code { background: rgba(0, 0, 0, 0.2); }
.chat-msg-body pre {
  background: rgba(0, 0, 0, 0.35);
  padding: 10px 12px;
  border-radius: 6px;
  overflow-x: auto;
  margin: 0.5em 0;
}
.chat-msg-body pre code {
  background: none;
  padding: 0;
  font-size: 12px;
  line-height: 1.45;
}

/* Blocks */
.chat-msg-body blockquote {
  border-left: 3px solid var(--border);
  padding-left: 10px;
  margin: 0.4em 0;
  color: var(--muted);
}
.chat-msg-body hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 0.6em 0;
}

/* Tables */
.chat-msg-body table {
  border-collapse: collapse;
  margin: 0.4em 0;
  font-size: 12px;
}
.chat-msg-body th,
.chat-msg-body td {
  border: 1px solid var(--border);
  padding: 4px 8px;
  text-align: left;
}

/* Math */
.chat-msg-body .katex { font-size: 1em; }
.chat-msg-body .katex-display {
  margin: 0.4em 0;
  overflow-x: auto;
  overflow-y: hidden;
}
.chat-msg.user {
  background: var(--primary);
  color: #fff;
  align-self: flex-end;
  border-bottom-right-radius: 2px;
}
.chat-msg.assistant {
  background: var(--panel-2);
  border: 1px solid var(--border);
  align-self: flex-start;
  border-bottom-left-radius: 2px;
}
.chat-msg .sources {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px dashed var(--border);
  font-size: 11px;
  color: var(--muted);
}
.chat-input {
  padding: 12px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
}
.chat-input textarea {
  flex: 1;
  resize: none;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}
.modal-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 24px;
  width: 480px;
  max-width: 90vw;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.modal-card h2 { margin: 0 0 4px; font-size: 16px; font-weight: 500; }
.modal-card label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
}
.modal-card label span { color: var(--text); font-size: 13px; }
.modal-card em { color: var(--primary); font-style: normal; font-weight: 600; }
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

/* Scrollbar */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--muted); }
