:root {
  --ink: #1a1714;
  --paper: #f3eee4;
  --paper-2: #ebe4d6;
  --red: #a4172c;
  --red-deep: #7e1021;
  --line: #d9cfbc;
  --muted: #6e6456;
  --side-w: 280px;
  --src-w: 260px;
  --top-h: 64px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  height: 100dvh;
}

.ai-body {
  margin: 0;
  height: 100%;
  height: 100dvh;
  background:
    radial-gradient(1200px 500px at 10% -10%, rgba(164, 23, 44, 0.08), transparent 55%),
    linear-gradient(180deg, #f7f2e9 0%, var(--paper) 40%, #efe8db 100%);
  color: var(--ink);
  font-family: "Noto Sans HK", "PingFang HK", sans-serif;
  overflow: hidden;
}

.ai-topbar {
  height: var(--top-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1.25rem;
  border-bottom: 1px solid var(--line);
  background: rgba(243, 238, 228, 0.92);
  backdrop-filter: blur(8px);
}

.ai-brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.ai-brand-mark {
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 999px;
  background: var(--red);
  color: #fff;
  display: grid;
  place-items: center;
  font-family: "Noto Serif HK", serif;
  font-weight: 900;
  font-size: 1.05rem;
}

.ai-brand-name {
  margin: 0;
  font-family: "Noto Serif HK", serif;
  font-weight: 800;
  font-size: 1.15rem;
  letter-spacing: 0.02em;
  line-height: 1.1;
}

.ai-brand-sub {
  margin: 0.1rem 0 0;
  color: var(--muted);
  font-size: 0.75rem;
}

.ai-topbar-actions {
  display: flex;
  align-items: center;
  gap: 0.85rem;
}

.ai-pill {
  display: inline-flex;
  align-items: center;
  padding: 0.2rem 0.55rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  font-size: 0.72rem;
  color: var(--muted);
  background: #fff;
}

.ai-pill.is-ok {
  border-color: #b9d4c4;
  color: #1f6b4a;
  background: #eef7f1;
}

.ai-pill.is-bad {
  border-color: #e2b4b8;
  color: var(--red-deep);
  background: #f8ecee;
}

.ai-link {
  color: var(--ink);
  font-size: 0.85rem;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.ai-shell {
  height: calc(100dvh - var(--top-h));
  display: grid;
  grid-template-columns: var(--side-w) minmax(0, 1fr) var(--src-w);
  gap: 0;
  min-height: 0;
}

.ai-shell > * {
  min-height: 0;
}

.ai-side,
.ai-sources-pane {
  overflow: auto;
  padding: 1rem 0.9rem 1.25rem;
  border-right: 1px solid var(--line);
}

.ai-sources-pane {
  border-right: 0;
  border-left: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.28);
}

.ai-panel + .ai-panel {
  margin-top: 1.1rem;
}

.ai-panel-title {
  margin: 0 0 0.55rem;
  font-size: 0.72rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 700;
}

.ai-mode-list {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.ai-mode {
  text-align: left;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 10px;
  padding: 0.7rem 0.8rem;
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.ai-mode:hover {
  border-color: #c9b8a0;
}

.ai-mode.is-active {
  border-color: var(--red);
  background: #fff7f7;
  box-shadow: inset 3px 0 0 var(--red);
}

.ai-mode-kicker {
  display: block;
  font-size: 0.68rem;
  color: var(--muted);
  letter-spacing: 0.08em;
}

.ai-mode-name {
  display: block;
  margin-top: 0.1rem;
  font-family: "Noto Serif HK", serif;
  font-size: 1.05rem;
  font-weight: 700;
}

.ai-mode-desc {
  display: block;
  margin-top: 0.15rem;
  font-size: 0.75rem;
  color: var(--muted);
}

.ai-suggest-list {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.ai-suggest {
  border: 0;
  background: transparent;
  text-align: left;
  padding: 0.45rem 0.2rem;
  border-bottom: 1px dashed var(--line);
  color: var(--ink);
  font-size: 0.82rem;
  line-height: 1.45;
  cursor: pointer;
}

.ai-suggest:hover {
  color: var(--red);
}

.ai-path {
  margin: 0;
  padding-left: 1.1rem;
  color: var(--muted);
}

.ai-path li {
  margin: 0.35rem 0;
}

.ai-path button {
  border: 0;
  background: none;
  padding: 0;
  color: var(--ink);
  font-size: 0.82rem;
  text-align: left;
  cursor: pointer;
  line-height: 1.4;
}

.ai-path button:hover {
  color: var(--red);
}

.ai-chat {
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100%;
  overflow: hidden;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.55), rgba(255, 255, 255, 0.2));
}

.ai-chat-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.85rem 1.25rem 0.65rem;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}

.ai-chat-eyebrow {
  margin: 0;
  font-size: 0.75rem;
  color: var(--red);
  font-weight: 600;
  letter-spacing: 0.04em;
}

.ai-chat-title {
  margin: 0.15rem 0 0;
  font-family: "Noto Serif HK", serif;
  font-size: 1.3rem;
  font-weight: 800;
  line-height: 1.25;
}

.ai-clear.button {
  background: transparent;
  border-color: var(--line);
  color: var(--muted);
}

.ai-chat-body {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.ai-chat-log {
  flex: 1;
  overflow: auto;
  padding: 1rem 1.25rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  scroll-behavior: auto;
  overscroll-behavior: contain;
}

.ai-chat-log::after {
  content: "";
  display: block;
  height: 0.25rem;
  flex-shrink: 0;
}

.ai-jump-latest {
  position: absolute;
  left: 50%;
  bottom: 0.85rem;
  transform: translateX(-50%);
  z-index: 3;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.96);
  color: var(--ink);
  border-radius: 999px;
  padding: 0.4rem 0.9rem;
  font-size: 0.78rem;
  font-weight: 600;
  box-shadow: 0 6px 20px rgba(26, 23, 20, 0.1);
  cursor: pointer;
  transition: opacity 0.15s ease, transform 0.15s ease;
}

.ai-jump-latest:hover {
  border-color: var(--red);
  color: var(--red);
}

.ai-jump-latest[hidden] {
  display: none !important;
}

.ai-welcome {
  margin: auto 0;
  max-width: 34rem;
  padding: 1.5rem 0;
}

.ai-welcome-brand {
  margin: 0 0 0.4rem;
  font-family: "Noto Serif HK", serif;
  font-size: 1.6rem;
  font-weight: 900;
  color: var(--red);
}

.ai-welcome h2 {
  margin: 0 0 0.7rem;
  font-family: "Noto Serif HK", serif;
  font-size: 1.55rem;
  line-height: 1.3;
  font-weight: 800;
}

.ai-welcome p {
  margin: 0;
  color: var(--muted);
  line-height: 1.7;
  font-size: 0.95rem;
}

.ai-msg {
  max-width: min(680px, 94%);
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.ai-msg.is-user {
  align-self: flex-end;
}

.ai-msg.is-assistant {
  align-self: flex-start;
}

.ai-bubble {
  padding: 0.8rem 0.95rem;
  border-radius: 14px;
  line-height: 1.65;
  word-break: break-word;
  font-size: 0.94rem;
}

.ai-bubble-body {
  white-space: normal;
}

.ai-bubble-body p {
  margin: 0 0 0.65em;
}

.ai-bubble-body p:last-child {
  margin-bottom: 0;
}

.ai-bubble-body ul,
.ai-bubble-body ol {
  margin: 0.35em 0 0.65em;
  padding-left: 1.25rem;
}

.ai-bubble-body li {
  margin: 0.2em 0;
}

.ai-bubble-body strong {
  font-weight: 700;
}

.ai-msg.is-user .ai-bubble {
  background: var(--ink);
  color: #fff;
  border-bottom-right-radius: 4px;
  white-space: pre-wrap;
}

.ai-msg.is-assistant .ai-bubble {
  background: #fff;
  border: 1px solid var(--line);
  border-bottom-left-radius: 4px;
}

.ai-msg.is-assistant .ai-bubble.is-error {
  border-color: #e2b4b8;
  background: #fbf1f2;
  color: var(--red-deep);
  white-space: pre-wrap;
}

.ai-msg.is-assistant .ai-bubble.is-collapsed .ai-bubble-body {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 8;
  overflow: hidden;
  max-height: 13.2em;
  mask-image: linear-gradient(180deg, #000 62%, transparent 100%);
}

.ai-expand {
  margin-top: 0.55rem;
  border: 0;
  background: transparent;
  color: var(--red);
  font-size: 0.78rem;
  font-weight: 600;
  padding: 0;
  cursor: pointer;
}

.ai-expand:hover {
  text-decoration: underline;
  text-underline-offset: 2px;
}

.ai-msg-meta {
  font-size: 0.72rem;
  color: var(--muted);
  padding: 0 0.2rem;
}

.ai-msg.is-user .ai-msg-meta {
  text-align: right;
}

.ai-typing::after {
  content: "▍
  animation: blink 1s steps(1) infinite;
}

@keyframes blink {
  50% { opacity: 0; }
}

.ai-composer {
  border-top: 1px solid var(--line);
  padding: 0.75rem 1.25rem 0.9rem;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(10px);
  flex-shrink: 0;
  position: sticky;
  bottom: 0;
  z-index: 5;
}

.ai-composer .textarea {
  background: #fff;
  border-color: var(--line);
  box-shadow: none;
  resize: none;
  min-height: 3.6rem;
  max-height: 9rem;
  font-family: inherit;
}

.ai-composer .textarea:focus {
  border-color: var(--red);
  box-shadow: 0 0 0 0.125em rgba(164, 23, 44, 0.15);
}

.ai-composer-bar {
  margin-top: 0.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.ai-composer-hint {
  margin: 0;
  font-size: 0.75rem;
  color: var(--muted);
}

.ai-composer .button.is-danger {
  background: var(--red);
  border-color: var(--red);
}

.ai-composer .button.is-danger:hover {
  background: var(--red-deep);
  border-color: var(--red-deep);
}

.ai-composer .button.is-danger:disabled {
  opacity: 0.55;
}

.ai-sources-empty {
  margin: 0;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.55;
}

.ai-sources-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}

.ai-source-item {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.65rem 0.7rem;
  background: #fff;
}

.ai-source-item .score {
  font-size: 0.68rem;
  color: var(--red);
  font-weight: 700;
}

.ai-source-item .title {
  display: block;
  margin-top: 0.15rem;
  font-size: 0.84rem;
  font-weight: 600;
  line-height: 1.35;
}

.ai-source-thumbs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-top: 0.45rem;
}

.ai-source-thumb {
  display: block;
  width: 3.4rem;
  height: 3.4rem;
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid var(--line);
  background: #f5f0e6;
  padding: 0;
  cursor: zoom-in;
  transition: border-color 0.15s ease;
}

.ai-source-thumb:hover {
  border-color: var(--red);
}

.ai-source-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.ai-inline-fig {
  margin: 0.65rem 0 0.85rem;
  max-width: min(420px, 100%);
}

.ai-inline-fig a {
  display: block;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--line);
  background: #efe8db;
}

.ai-inline-fig .ai-img-preview-trigger {
  display: block;
  width: 100%;
  padding: 0;
  margin: 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  background: #efe8db;
  cursor: zoom-in;
  transition: border-color 0.15s ease;
}

.ai-inline-fig .ai-img-preview-trigger:hover {
  border-color: var(--red);
}

.ai-inline-fig img {
  display: block;
  width: 100%;
  max-height: 260px;
  object-fit: cover;
}

.ai-inline-fig figcaption {
  margin-top: 0.35rem;
  font-size: 0.72rem;
  color: var(--muted);
  line-height: 1.4;
}

.ai-inline-miss {
  display: inline-block;
  margin: 0.25rem 0;
  font-size: 0.78rem;
  color: var(--muted);
}

.ai-media-label {
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin-bottom: 0.45rem;
}

.ai-media-row {
  display: flex;
  gap: 0.55rem;
  overflow-x: auto;
  padding-bottom: 0.15rem;
  scrollbar-width: thin;
}

.ai-media-item {
  margin: 0;
  flex: 0 0 auto;
  width: 7.5rem;
}

.ai-media-item a,
.ai-media-item .ai-media-trigger {
  display: block;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--line);
  background: #efe8db;
  aspect-ratio: 4 / 3;
}

.ai-media-item .ai-media-trigger {
  width: 100%;
  padding: 0;
  margin: 0;
  cursor: zoom-in;
  transition: border-color 0.15s ease;
}

.ai-media-item .ai-media-trigger:hover {
  border-color: var(--red);
}

.ai-media-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.ai-media-item figcaption {
  margin-top: 0.3rem;
  font-size: 0.68rem;
  line-height: 1.35;
  color: var(--muted);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.ai-tips {
  margin: 0;
  padding-left: 1.1rem;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.65;
}

.ai-tips strong {
  color: var(--ink);
}

/* ── 统一圖片預覽彈層 ── */
.ai-img-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  background: rgba(26, 23, 20, 0.82);
  cursor: zoom-out;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.18s ease, visibility 0.18s ease;
}

.ai-img-overlay[hidden] {
  display: flex !important;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.ai-img-overlay.is-open {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

.ai-img-overlay-fig {
  margin: 0;
  max-width: min(90vw, 1000px);
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: default;
}

.ai-img-overlay-img {
  display: block;
  max-width: 90vw;
  max-height: 80vh;
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: 8px;
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.45);
}

.ai-img-overlay-cap {
  margin-top: 0.75rem;
  color: #f3eee4;
  font-size: 0.88rem;
  line-height: 1.5;
  text-align: center;
  max-width: 90vw;
  word-break: break-word;
}

.ai-img-overlay-close {
  position: absolute;
  top: 1rem;
  right: 1.25rem;
  width: 2.4rem;
  height: 2.4rem;
  border: 0;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  font-size: 1.6rem;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background 0.15s ease;
}

.ai-img-overlay-close:hover {
  background: rgba(255, 255, 255, 0.3);
}

@media (max-width: 1100px) {
  .ai-shell {
    grid-template-columns: 240px minmax(0, 1fr);
  }
  .ai-sources-pane {
    display: none;
  }
}

@media (max-width: 820px) {
  .ai-body {
    overflow: hidden;
    height: 100dvh;
  }
  .ai-shell {
    height: calc(100dvh - var(--top-h));
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr);
  }
  .ai-side {
    border-right: 0;
    border-bottom: 1px solid var(--line);
    max-height: 32vh;
    overflow: auto;
  }
  .ai-chat {
    min-height: 0;
    height: 100%;
    overflow: hidden;
  }
  .ai-chat-title {
    font-size: 1.15rem;
  }
  .ai-composer {
    padding-bottom: calc(0.75rem + env(safe-area-inset-bottom, 0px));
  }
}
