:root {
  --brand: #4292a5;
  --brand-dark: #35798b;
  --brand-text: #285966;
  --brand-tint: #dceaed;
  --accent: #c8565c;
  --accent-dark: #b2454b;
  --base: #f2f2f2;
  --surface: #ffffff;
  --border: #dbe3e5;
  --text: #2b3a3f;
  --muted: #607177;
  --success: #2f7d5a;
  --pending: #a5b0b4;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  color: var(--text);
  background: var(--base);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic", sans-serif;
}

button,
input {
  font: inherit;
}

button:focus-visible,
input:focus-visible {
  outline: 3px solid rgba(66, 146, 165, 0.35);
  outline-offset: 2px;
}

#top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 1rem;
  color: #fff;
  background: var(--brand);
}

#top-bar h1 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: 0.04em;
}

main {
  display: flex;
  height: calc(100vh - 56px);
}

#chat-panel {
  flex: 0 0 48%;
  width: 48%;
  min-width: 320px;
  display: flex;
  flex-direction: column;
  padding: 0.75rem;
  border-right: 1px solid var(--border);
}

#messages {
  min-width: 0;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  overflow-y: auto;
  scroll-behavior: smooth;
}

.msg {
  max-width: 90%;
  padding: 0.55rem 0.8rem;
  border-radius: 0.75rem;
  line-height: 1.55;
}

.msg-user {
  align-self: flex-end;
  white-space: pre-wrap;
  background: var(--brand-tint);
  border-bottom-right-radius: 0.25rem;
}

.msg-assistant {
  align-self: flex-start;
  width: 100%;
  max-width: 100%;
  border: 1px solid var(--border);
  border-bottom-left-radius: 0.25rem;
  background: var(--surface);
  box-shadow: 0 1px 2px rgba(43, 58, 63, 0.06);
}

.msg-assistant p {
  margin: 0 0 0.55rem;
}

.msg-assistant p:last-child {
  margin-bottom: 0;
}

.msg-assistant ul {
  margin: 0.45rem 0;
  padding-left: 1.4rem;
}

.msg-assistant img {
  display: block;
  max-width: 100%;
  max-height: 330px;
  margin: 0.5rem 0;
  border-radius: 0.45rem;
  object-fit: contain;
}

.msg-assistant .katex-display {
  margin: 0.55rem 0;
  padding: 0.15rem 0;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: thin;
}

.msg-assistant .katex-display > .katex {
  font-size: 0.95em;
}

.progress-card {
  padding: 0.72rem 0.85rem;
}

.progress-title {
  margin: 0 0 0.45rem;
  color: var(--brand-text);
  font-size: 0.88rem;
  font-weight: 700;
}

.progress-list {
  display: grid;
  gap: 0.32rem;
  margin: 0;
  padding: 0;
  list-style: none;
}

.progress-step {
  display: grid;
  grid-template-columns: 1.1rem minmax(8rem, auto) 1fr;
  align-items: baseline;
  gap: 0.4rem;
  color: var(--pending);
  font-size: 0.82rem;
}

.progress-step::before {
  content: "○";
  font-weight: 700;
}

.progress-step.active {
  color: var(--brand-text);
}

.progress-step.active::before {
  content: "●";
  animation: pulse 0.8s ease-in-out infinite alternate;
}

.progress-step.done {
  color: var(--text);
}

.progress-step.done::before {
  content: "✓";
  color: var(--success);
}

.progress-label {
  font-weight: 700;
}

.progress-result {
  min-width: 0;
  color: var(--muted);
}

@keyframes pulse {
  from { opacity: 0.4; }
  to { opacity: 1; }
}

#quick-reply-slot {
  display: flex;
  flex-wrap: wrap;
  gap: 0.42rem;
  min-height: 2.25rem;
  margin: 0.55rem 0;
}

.preset-btn {
  padding: 0.45rem 0.82rem;
  border: 1px solid var(--brand);
  border-radius: 1rem;
  color: var(--brand-text);
  background: var(--surface);
  font-size: 0.84rem;
  cursor: pointer;
}

.preset-btn:hover:not(:disabled) {
  background: var(--brand-tint);
}

.preset-btn:disabled,
#send-button:disabled {
  cursor: wait;
  opacity: 0.55;
}

#chat-form {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

#message-input {
  min-width: 0;
  flex: 1;
  padding: 0.58rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  background: var(--surface);
}

#message-input:focus {
  border-color: var(--brand);
}

#send-button {
  padding: 0.58rem 1.08rem;
  border: 0;
  border-radius: 0.5rem;
  color: #fff;
  background: var(--accent);
  font-weight: 700;
  cursor: pointer;
}

#send-button:hover:not(:disabled) {
  background: var(--accent-dark);
}

#form-status {
  opacity: 0;
  color: var(--brand-text);
  font-size: 0.78rem;
}

#form-status.visible {
  opacity: 1;
}

#map-panel {
  position: relative;
  min-width: 0;
  flex: 1;
  background: #e4eaec;
}

#map {
  width: 100%;
  height: 100%;
  background:
    linear-gradient(rgba(255, 255, 255, 0.34) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.34) 1px, transparent 1px),
    #d8e1e4;
  background-size: 32px 32px;
}

#map-status {
  position: absolute;
  top: 0.75rem;
  left: 50%;
  z-index: 1000;
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100% - 2rem);
  padding: 0.45rem 0.7rem;
  border: 1px solid #d1a7a9;
  border-radius: 0.4rem;
  color: #7d3438;
  background: rgba(255, 246, 246, 0.94);
  font-size: 0.76rem;
}

#legend {
  position: absolute;
  right: 1rem;
  bottom: 1rem;
  z-index: 1000;
  max-width: 270px;
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 0.4rem;
  background: rgba(255, 255, 255, 0.94);
  font-size: 0.75rem;
  line-height: 1.6;
}

#legend strong {
  display: block;
}

.legend-swatch {
  font-weight: 900;
}

.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;
}

@media (max-width: 900px) {
  main {
    flex-direction: column;
  }

  #chat-panel {
    flex: 0 0 60%;
    width: 100%;
    min-width: 0;
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }

  #map-panel {
    min-height: 40%;
  }
}

@media (max-width: 560px) {
  #chat-panel {
    flex-basis: 64%;
    padding: 0.6rem;
  }

  #form-status {
    display: none;
  }

  #legend {
    right: 0.5rem;
    bottom: 0.5rem;
    max-width: calc(100% - 1rem);
  }
}

@media (prefers-reduced-motion: reduce) {
  #messages {
    scroll-behavior: auto;
  }

  .progress-step.active::before {
    animation: none;
  }
}
