:root {
  --bg: #f6f7fb;
  --card: #ffffff;
  --text: #1e2430;
  --muted: #7b8394;
  --line: #e7eaf0;
  --primary: #ff8a3d;
  --danger: #e5484d;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'PingFang SC', 'Microsoft YaHei', sans-serif;
  background: var(--bg);
  color: var(--text);
}

.app { min-height: 100vh; }
.topbar {
  height: 56px;
  background: #fff;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  position: sticky;
  top: 0;
}
.brand { font-weight: 700; }
.session { color: var(--muted); font-size: 13px; }

.main {
  max-width: 900px;
  margin: 0 auto;
  padding: 16px;
  display: grid;
  gap: 14px;
}
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px;
}
#chatCard {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
#chatCard.hidden {
  display: none;
}
.hidden { display: none; }

h2 {
  margin: 0 0 12px;
  font-size: 18px;
}

.grid {
  display: grid;
  gap: 10px;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
.config-grid {
  margin-bottom: 12px;
}
.config-panel.hidden {
  display: none;
}
.config-digest {
  border: 1px dashed #ccd3df;
  border-radius: 10px;
  background: #f9fbff;
  color: #576176;
  font-size: 12px;
  padding: 8px 10px;
  line-height: 1.45;
}

label {
  display: grid;
  gap: 6px;
}
.inline {
  display: flex;
  gap: 8px;
  align-items: center;
}
.inline > select {
  flex: 1;
}
label > span {
  color: var(--muted);
  font-size: 12px;
}
input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 10px;
  background: #fff;
}
input, select { height: 38px; }
textarea {
  min-height: 96px;
  padding: 10px;
  resize: vertical;
  font-family: inherit;
}

.row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
}
.between { justify-content: space-between; }
.end { justify-content: flex-end; }

.btn {
  height: 36px;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 8px;
  padding: 0 12px;
  cursor: pointer;
}
.btn.primary {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.chat-messages {
  min-height: 320px;
  max-height: 62vh;
  overflow: auto;
  background: #f8fafc;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px;
  display: grid;
  gap: 12px;
}
.empty-chat {
  color: var(--muted);
  text-align: center;
  padding: 28px 12px;
}
.msg {
  display: grid;
  gap: 4px;
}
.msg.user {
  justify-items: end;
}
.msg.assistant {
  justify-items: start;
}
.meta {
  font-size: 12px;
  color: var(--muted);
}
.bubble {
  max-width: min(90%, 680px);
  border-radius: 12px;
  padding: 10px 12px;
  white-space: pre-wrap;
  line-height: 1.45;
  border: 1px solid var(--line);
}
.msg.user .bubble {
  background: #ffeddc;
  border-color: #ffd3b1;
}
.msg.assistant .bubble {
  background: #fff;
}
.msg.assistant .bubble.loading {
  border-style: dashed;
}
.process-state {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid #d9dfeb;
  background: #f7f9fd;
  color: #4a556a;
  font-size: 11px;
}
.process-state .dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #99a3b7;
}
.process-state.running .dot {
  background: #ff8a3d;
  animation: pulse 1s infinite ease-in-out;
}
.process-state.failed .dot {
  background: #e5484d;
}
.process-details {
  margin-top: 8px;
  border-top: 1px solid #edf0f5;
  padding-top: 8px;
}
.process-details > summary {
  cursor: pointer;
  color: #5f6980;
  font-size: 12px;
  list-style: none;
}
.process-details > summary::-webkit-details-marker {
  display: none;
}
.process-log {
  margin-top: 8px;
  max-height: 160px;
  overflow: auto;
  background: #f2f5fb;
  border: 1px solid #e2e7f1;
  border-radius: 8px;
  padding: 8px;
  color: #364156;
  font-size: 12px;
  line-height: 1.45;
  white-space: pre-wrap;
}
.composer {
  margin-top: 4px;
  border-top: 1px solid #edf0f6;
  padding-top: 10px;
  background: #fff;
}
.composer textarea {
  min-height: 96px;
}
.status {
  font-size: 12px;
  color: var(--muted);
}
.status.ok { color: #19753a; }
.status.err { color: #b42328; }
.policy-hint {
  margin: -4px 0 10px;
  padding: 8px 10px;
  border: 1px solid #d7ead9;
  background: #f3fcf4;
  color: #1f6f3f;
  border-radius: 8px;
  font-size: 12px;
}

@keyframes pulse {
  0% { transform: scale(0.9); opacity: 0.7; }
  50% { transform: scale(1.15); opacity: 1; }
  100% { transform: scale(0.9); opacity: 0.7; }
}

@media (max-width: 768px) {
  .grid { grid-template-columns: 1fr; }
  .main {
    padding: 8px;
    min-height: calc(100vh - 56px);
  }
  #chatCard {
    min-height: calc(100vh - 72px);
    padding: 10px;
    border-radius: 10px;
  }
  h2 {
    margin-bottom: 8px;
  }
  .inline {
    flex-wrap: wrap;
  }
  .btn {
    height: 34px;
    padding: 0 10px;
  }
  .config-grid {
    margin-bottom: 8px;
    gap: 8px;
  }
  input, select {
    height: 36px;
  }
  .policy-hint {
    margin: 0;
    padding: 7px 8px;
  }
  .chat-messages {
    flex: 1;
    min-height: 0;
    max-height: none;
    height: auto;
    padding: 10px;
  }
  .bubble {
    max-width: 100%;
    padding: 9px 10px;
  }
  .process-log {
    max-height: 120px;
  }
  .composer {
    position: sticky;
    bottom: 0;
    margin-top: 2px;
    padding-top: 8px;
    background: linear-gradient(to bottom, rgba(255,255,255,0.92), #fff 24px);
    backdrop-filter: blur(3px);
  }
  .composer textarea {
    min-height: 82px;
  }
}
