:root {
  --bg: #f5f5f7;
  --panel: #ffffff;
  --text: #1d1d1f;
  --muted: #6e6e73;
  --line: #d2d2d7;
  --blue: #0071e3;
  --blue-hover: #0077ed;
  --user: #e8f1fc;
  --assistant: #ffffff;
  --danger: #b42318;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --radius: 16px;
  --shadow: 0 8px 28px rgba(0, 0, 0, 0.06);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  height: var(--app-h, 100dvh);
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
}

body {
  overflow: hidden;
}

.app {
  display: flex;
  height: 100%;
}

.shade {
  display: none;
}

.sidebar {
  width: 272px;
  flex-shrink: 0;
  background: #fbfbfd;
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  padding: 20px 16px;
}

.sidebar__brand {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 16px;
}

.btn-close {
  display: none;
}

.mark {
  font-weight: 700;
  letter-spacing: 0.04em;
}

.brand-name { color: var(--muted); font-size: 14px; }

.btn-new, #send, .chip, .btn-menu, .btn-close, .history button.item {
  border: 0;
  cursor: pointer;
  font: inherit;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.btn-new {
  background: var(--blue);
  color: #fff;
  border-radius: 980px;
  padding: 10px 14px;
  font-weight: 600;
}

.btn-new:hover { background: var(--blue-hover); }

.history {
  flex: 1;
  overflow: auto;
  margin: 16px 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.history a, .history button.item {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: 0;
  color: var(--text);
  font: inherit;
  padding: 8px 10px;
  border-radius: 10px;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history button.item:hover, .history button.item.active {
  background: #e8f1fc;
}

.sidebar__foot {
  font-size: 12px;
  color: var(--muted);
  line-height: 1.5;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.sidebar__foot a { color: var(--blue); text-decoration: none; }
.sidebar__foot a:hover { text-decoration: underline; }

.brand-home {
  font-weight: 700;
  letter-spacing: 0.04em;
  text-decoration: none;
}

.brand-home:hover { text-decoration: none; }

.brand-home__cas { color: #003366; }
.brand-home__stk { color: #000; }

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.top {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 24px 8px;
}

.top__title {
  flex: 1;
  min-width: 0;
}

.top h1 {
  font-size: 17px;
  font-weight: 600;
  margin: 0;
}

.site-nav {
  display: flex;
  gap: 12px;
  margin-top: 4px;
  font-size: 12px;
}

.site-nav a {
  color: var(--blue);
  text-decoration: none;
}

.site-nav a:hover { text-decoration: underline; }
.site-nav .brand-home:hover { text-decoration: none; }

.btn-menu {
  display: none;
  border: 0;
  background: none;
  font-size: 20px;
  cursor: pointer;
}

.reach {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
}

.reach b {
  font-weight: 600;
  color: var(--text);
}

.build {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
}

.legal .build {
  display: inline;
  margin-left: 8px;
}

.status {
  font-size: 12px;
  color: var(--muted);
}

.status.ok { color: #248a3d; }
.status.bad { color: var(--danger); }

.thread {
  flex: 1;
  overflow: auto;
  padding: 8px 24px 24px;
}

.welcome {
  max-width: 720px;
  margin: 8vh auto 0;
  text-align: center;
}

.welcome h2 {
  font-size: 40px;
  letter-spacing: -0.03em;
  margin: 0 0 8px;
}

.welcome p {
  color: var(--muted);
  margin: 0 0 28px;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
}

.chip {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 980px;
  padding: 10px 14px;
  box-shadow: var(--shadow);
}

.chip:hover { border-color: var(--blue); color: var(--blue); }

.msg {
  max-width: 760px;
  margin: 0 auto 18px;
  display: flex;
  gap: 12px;
}

.msg__who {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 12px;
  font-weight: 700;
  background: #eee;
}

.msg.user .msg__who { background: #d6e6fb; color: #004a99; }
.msg.assistant .msg__who { background: #1d1d1f; color: #fff; }

.msg__body {
  flex: 1;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px;
  line-height: 1.6;
  box-shadow: var(--shadow);
}

.msg.user .msg__body { background: var(--user); }

.msg__body p { margin: 0 0 10px; }
.msg__body p:last-child { margin-bottom: 0; }
.msg__body pre {
  background: #111;
  color: #f5f5f7;
  padding: 12px;
  border-radius: 10px;
  overflow: auto;
  font-family: var(--mono);
  font-size: 13px;
}
.msg__body code {
  font-family: var(--mono);
  font-size: 0.92em;
  background: #f0f0f2;
  padding: 1px 5px;
  border-radius: 5px;
}
.msg__body pre code { background: none; padding: 0; color: inherit; }

.sources {
  margin-top: 10px;
  font-size: 12px;
  color: var(--muted);
}

.sources span,
.sources a {
  display: inline-block;
  margin: 4px 6px 0 0;
  padding: 2px 8px;
  border-radius: 980px;
  background: #f0f0f2;
  color: var(--blue);
  text-decoration: none;
}
.sources a:hover { text-decoration: underline; }

.composer {
  padding: 0 24px 20px;
}

.hint, .legal {
  max-width: 760px;
  margin: 0 auto 8px;
  color: var(--muted);
  font-size: 13px;
}

.legal { font-size: 11px; text-align: center; margin-top: 8px; }

.box {
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  gap: 8px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 8px 8px 8px 16px;
  box-shadow: var(--shadow);
}

textarea {
  flex: 1;
  border: 0;
  resize: none;
  font: inherit;
  outline: none;
  max-height: 160px;
  background: transparent;
}

#send {
  background: var(--blue);
  color: #fff;
  border-radius: 980px;
  padding: 8px 16px;
  font-weight: 600;
}

#send:disabled { opacity: 0.45; cursor: default; }

.err { color: var(--danger); }

@media (max-width: 800px) {
  html, body {
    height: var(--app-h, 100dvh);
  }

  .shade {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 3;
    background: rgba(0, 0, 0, 0.36);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
  }
  body.sidebar-open .shade {
    opacity: 1;
    pointer-events: auto;
  }

  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(86vw, 320px);
    z-index: 4;
    padding-top: max(16px, env(safe-area-inset-top));
    padding-bottom: max(16px, env(safe-area-inset-bottom));
    box-shadow: 8px 0 28px rgba(0, 0, 0, 0.12);
    transform: translateX(-105%);
    transition: transform 0.2s ease;
  }
  .sidebar.open { transform: none; }

  .sidebar__brand {
    align-items: center;
  }
  .btn-close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-left: auto;
    min-height: 44px;
    padding: 0 12px;
    border: 0;
    border-radius: 980px;
    background: #eee;
    color: var(--text);
    font: inherit;
    font-size: 14px;
    cursor: pointer;
  }

  .btn-new {
    min-height: 44px;
  }
  .history button.item {
    min-height: 44px;
    padding: 12px 10px;
  }

  .btn-menu {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    min-height: 44px;
    margin-left: -6px;
    font-size: 22px;
  }

  .top {
    align-items: center;
    gap: 8px;
    padding: max(8px, env(safe-area-inset-top)) 12px 8px;
  }
  .top h1 { font-size: 16px; }
  .site-nav,
  .reach,
  .top .build,
  .hint { display: none; }

  .status { font-size: 12px; flex-shrink: 0; }

  .welcome {
    margin: 4vh auto 0;
    padding: 0 4px;
  }
  .welcome h2 { font-size: 22px; }
  .welcome p { margin-bottom: 18px; font-size: 14px; }
  .chips {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }
  .chip {
    min-height: 44px;
    font-size: 15px;
    box-shadow: none;
  }

  .thread {
    padding: 4px 12px 12px;
    -webkit-overflow-scrolling: touch;
  }
  .msg { gap: 8px; margin-bottom: 12px; }
  .msg__who { width: 28px; height: 28px; font-size: 11px; }
  .msg__body { padding: 12px; font-size: 16px; }
  .msg__body pre { font-size: 13px; -webkit-overflow-scrolling: touch; }

  .composer {
    padding: 0 12px max(10px, env(safe-area-inset-bottom));
  }
  .legal {
    margin-top: 6px;
    line-height: 1.45;
  }
  .legal .build { display: block; margin: 4px 0 0; }

  .box {
    align-items: flex-end;
    padding: 8px 8px 8px 12px;
  }
  textarea {
    font-size: 16px;
    line-height: 1.4;
    min-height: 24px;
    padding: 8px 0;
  }
  #send {
    min-width: 64px;
    min-height: 44px;
    padding: 0 14px;
  }
}
