:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --surface2: #f0f2f6;
  --border: #dde2eb;
  --text: #1a1f2e;
  --muted: #5c6678;
  --accent: #3b6fd9;
  --accent2: #2d57b5;
  --user: #e8f0ff;
  --bot: #ffffff;
  --danger: #d63b3b;
  --ok: #16a34a;
  --radius: 12px;
  --font: "SF Pro Text", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --shadow: 0 1px 3px rgba(26, 31, 46, 0.06);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  height: 100%;
  overflow: hidden;
}

html {
  height: 100%;
}

.hidden { display: none !important; }

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(244, 246, 250, 0.88);
  backdrop-filter: blur(4px);
  display: grid;
  place-items: center;
  z-index: 100;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem;
  width: min(420px, 92vw);
  box-shadow: 0 8px 32px rgba(26, 31, 46, 0.1);
}

.card h1 { margin: 0 0 0.5rem; font-size: 1.5rem; }
.card p { color: var(--muted); margin: 0 0 1rem; }

.auth-step.hidden { display: none; }

.totp-hint {
  font-size: 0.95rem;
}

.topbar .secondary {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
  padding: 0.45rem 0.75rem;
  font-size: 0.85rem;
}

.topbar .secondary:hover {
  background: var(--surface2);
  color: var(--text);
}

.card input {
  width: 100%;
  padding: 0.75rem 1rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  margin-bottom: 0.75rem;
}

.card input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(59, 111, 217, 0.15);
}

.card button, .composer button, .kb-search button, .upload-btn {
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 8px;
  padding: 0.65rem 1rem;
  cursor: pointer;
  font-weight: 600;
}

.card button:hover, .composer button:hover, .kb-search button:hover, .upload-btn:hover {
  background: var(--accent2);
}

.card button.secondary {
  background: var(--surface);
  color: var(--muted);
  border: 1px solid var(--border);
  margin-top: 0.5rem;
  width: 100%;
}

.card button.secondary:hover {
  color: var(--text);
  background: var(--surface2);
}

.error { color: var(--danger); font-size: 0.9rem; }
.hint { color: var(--muted); font-size: 0.85rem; margin: 0 0 0.75rem; }

.app {
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.topbar {
  flex-shrink: 0;
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.85rem 1.25rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.brand { font-weight: 700; font-size: 1.1rem; color: var(--text); }
.brand span { color: var(--accent); }

.controls { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }

.toggle {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  color: var(--muted);
  font-size: 0.9rem;
}

#model-select {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.45rem 0.6rem;
  min-width: 180px;
}

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 320px;
  min-height: 0;
  overflow: hidden;
}

.chat-panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

.msg {
  max-width: 92%;
  padding: 0.85rem 1rem;
  border-radius: var(--radius);
  line-height: 1.5;
  word-break: break-word;
  box-shadow: var(--shadow);
}

.msg.user {
  align-self: flex-end;
  background: var(--user);
  border: 1px solid #c5d8f5;
}

.msg.bot {
  align-self: flex-start;
  background: var(--bot);
  border: 1px solid var(--border);
}

.msg.bot.weather-card {
  background: linear-gradient(180deg, #f8fbff 0%, #ffffff 100%);
  border-color: #c9daf7;
  line-height: 1.5;
  font-size: 0.95rem;
  padding: 1rem 1.1rem;
}

.weather-report {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.weather-title {
  font-size: 0.82rem;
  color: var(--muted);
  font-weight: 600;
  letter-spacing: 0.01em;
}

.weather-emoji {
  font-size: 2.6rem;
  line-height: 1;
  margin: 0.15rem 0 0.25rem;
}

.weather-condition {
  font-size: 1.05rem;
  font-weight: 700;
  margin-bottom: 0.15rem;
}

.weather-row {
  font-size: 0.94rem;
  color: var(--text);
}

.weather-periods {
  margin-top: 0.7rem;
  padding-top: 0.7rem;
  border-top: 1px solid #dbe4f0;
  display: grid;
  gap: 0.4rem;
}

.weather-period {
  display: grid;
  grid-template-columns: 4rem 1fr auto;
  gap: 0.65rem;
  align-items: baseline;
  font-size: 0.88rem;
}

.weather-period span {
  color: var(--muted);
}

.weather-period strong {
  font-weight: 600;
}

.weather-period em {
  font-style: normal;
  color: var(--accent2);
  font-weight: 600;
}

.weather-source {
  margin-top: 0.55rem;
  font-size: 0.74rem;
  color: var(--muted);
}

.msg.status {
  align-self: center;
  background: var(--surface2);
  color: var(--muted);
  font-size: 0.85rem;
  border: 1px dashed var(--border);
  box-shadow: none;
}

.msg :is(p, ul, ol) { margin: 0.4rem 0; }
.msg pre {
  background: var(--surface2);
  border: 1px solid var(--border);
  padding: 0.75rem;
  border-radius: 8px;
  overflow-x: auto;
}

.msg code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.9em;
  background: var(--surface2);
  padding: 0.1em 0.35em;
  border-radius: 4px;
}

.status {
  padding: 0 1.25rem 0.5rem;
  color: var(--muted);
  font-size: 0.85rem;
}

.composer {
  flex-shrink: 0;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.75rem;
  padding: 1rem 1.25rem 1.25rem;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.composer textarea {
  resize: vertical;
  min-height: 48px;
  max-height: 180px;
  padding: 0.75rem 1rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
}

.composer textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(59, 111, 217, 0.12);
}

.composer button:disabled { opacity: 0.5; cursor: not-allowed; }

.kb-panel,
.side-panel {
  min-height: 0;
  border-left: 1px solid var(--border);
  background: var(--surface);
  padding: 0.75rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.panel-section-title {
  margin: 0.65rem 0 0.15rem;
  padding: 0.35rem 0.15rem 0;
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}

.panel-status {
  font-size: 0.8rem;
  padding: 0.45rem 0.6rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface2);
}

.panel-status.loading { color: var(--muted); }
.panel-status.ok { color: #2d8a4e; border-color: #b8e0c8; background: #f0faf3; }
.panel-status.warn { color: #9a6b00; border-color: #f0d9a0; background: #fffaf0; }
.panel-status.error { color: #c0392b; border-color: #f0c0bc; background: #fff5f4; }

.acc {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  overflow: hidden;
}

.acc-nested {
  border: none;
  border-radius: 0;
  background: transparent;
}

.acc-nested .acc-toggle {
  font-size: 0.86rem;
  padding: 0.55rem 0.75rem 0.55rem 1.1rem;
}

.acc-toggle {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.65rem 0.8rem;
  border: none;
  background: var(--surface2);
  color: var(--text);
  font: inherit;
  font-size: 0.92rem;
  font-weight: 600;
  text-align: left;
  cursor: pointer;
}

.acc-toggle::after {
  content: "▸";
  color: var(--muted);
  transition: transform 0.15s ease;
}

.acc-toggle.open::after {
  transform: rotate(90deg);
}

.acc-body {
  display: none;
  padding: 0.55rem 0.65rem 0.65rem;
}

.acc-body.open {
  display: block;
}

#acc-tasks.acc-body.open {
  padding-top: 0.25rem;
  padding-bottom: 0.35rem;
}

.kb-scope-panel.acc-body.open {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}

.panel-upload {
  margin-top: auto;
  padding-top: 0.5rem;
  text-align: center;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.upload-hint {
  font-size: 0.72rem;
  color: var(--muted);
  font-weight: 400;
}

.kb-panel h2 {
  margin: 0 0 0.75rem;
  font-size: 1rem;
  color: var(--text);
}

.tasks-panel {
  margin-bottom: 1rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border);
}

.kb-section h2 {
  margin: 0 0 0.75rem;
}

.tasks-subsection + .tasks-subsection {
  margin-top: 0.85rem;
}

.tasks-subsection h3 {
  margin: 0 0 0.45rem;
  font-size: 0.86rem;
  color: var(--muted);
  font-weight: 600;
}

.tasks-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.tasks-item {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.5rem 0.65rem;
  font-size: 0.84rem;
  line-height: 1.35;
}

.tasks-item-title {
  font-weight: 600;
  color: var(--text);
}

.tasks-item-meta {
  margin-top: 0.15rem;
  font-size: 0.78rem;
  color: var(--accent2);
}

.tasks-empty {
  font-size: 0.82rem;
  color: var(--muted);
  padding: 0.35rem 0.1rem;
}

.kb-status {
  margin-bottom: 0.75rem;
  padding: 0.55rem 0.7rem;
  border-radius: 8px;
  font-size: 0.84rem;
  line-height: 1.35;
}

.kb-status.loading {
  background: #eef4ff;
  border: 1px solid #c9daf7;
  color: var(--accent2);
}

.kb-status.ok {
  background: #ecfdf3;
  border: 1px solid #b7ebc6;
  color: var(--ok);
}

.kb-status.warn {
  background: #fff8e8;
  border: 1px solid #f2df9a;
  color: #9a6700;
}

.kb-status.error {
  background: #fff1f1;
  border: 1px solid #f3b4b4;
  color: var(--danger);
}

.kb-scope-tabs {
  display: flex;
  gap: 0.35rem;
  margin-bottom: 0.75rem;
}

.kb-scope {
  flex: 1;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--muted);
  border-radius: 8px;
  padding: 0.45rem 0.5rem;
  font-size: 0.82rem;
  cursor: pointer;
}

.kb-scope.active {
  background: var(--user);
  border-color: #c5d8f5;
  color: var(--accent2);
  font-weight: 600;
}

.kb-field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-bottom: 0.75rem;
  font-size: 0.82rem;
  color: var(--muted);
}

.kb-field select {
  padding: 0.55rem 0.7rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}

.kb-blocks {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.45rem;
  margin-bottom: 0.75rem;
}

.kb-manage {
  margin-bottom: 0.75rem;
  padding: 0.7rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface2);
}

.kb-manage-title {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 0.55rem;
}

.kb-tree {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  margin-bottom: 0.65rem;
  max-height: 220px;
  overflow-y: auto;
}

.kb-tree-empty {
  font-size: 0.82rem;
  color: var(--muted);
}

.kb-tree-section {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.kb-tree-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 0.35rem;
  align-items: center;
}

.kb-tree-row.child {
  padding-left: 0.85rem;
}

.kb-tree-select {
  text-align: left;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 8px;
  padding: 0.4rem 0.55rem;
  font-size: 0.82rem;
  cursor: pointer;
}

.kb-tree-select:hover {
  border-color: #c5d8f5;
  background: #f7faff;
}

.kb-tree-meta {
  font-size: 0.72rem;
  color: var(--muted);
  white-space: nowrap;
}

.kb-tree-delete {
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  padding: 0.1rem 0.25rem;
}

.kb-tree-delete:hover {
  color: var(--danger);
}

.kb-tree-children {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.kb-create-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.4rem;
  margin-top: 0.45rem;
}

.kb-create-row:last-child {
  grid-template-columns: 1fr 1fr auto;
}

.kb-create-row input,
.kb-create-row select {
  padding: 0.5rem 0.65rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.82rem;
}

.kb-create-row button {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 0.5rem 0.7rem;
  font-size: 0.8rem;
  cursor: pointer;
  white-space: nowrap;
}

.kb-block {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.55rem 0.65rem;
  font-size: 0.8rem;
}

.kb-block .name {
  font-weight: 600;
  margin-bottom: 0.15rem;
}

.kb-block .meta {
  color: var(--muted);
  font-size: 0.74rem;
}

.upload-btn {
  display: inline-block;
  margin-bottom: 0.75rem;
  font-size: 0.9rem;
}

.kb-search {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.kb-search input {
  padding: 0.55rem 0.7rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}

.kb-list, .kb-results { display: flex; flex-direction: column; gap: 0.5rem; }

.kb-item, .kb-hit {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.65rem 0.75rem;
  font-size: 0.85rem;
}

.kb-item .name { font-weight: 600; margin-bottom: 0.2rem; }
.kb-item .meta, .kb-hit .source { color: var(--muted); font-size: 0.78rem; }
.kb-hit .content { margin-top: 0.35rem; line-height: 1.4; color: var(--text); }

@media (max-width: 900px) {
  .layout {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1fr) minmax(0, 38vh);
    overflow: hidden;
  }
  .kb-panel,
  .side-panel { border-left: none; border-top: 1px solid var(--border); }
}
