/* 桌伴本機助手 UI */
.tm-mate-fab {
  position: fixed;
  right: 16px;
  bottom: 88px;
  z-index: 15050;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(145deg, #0ea5e9, #0369a1);
  color: #fff;
  box-shadow: 0 8px 24px rgba(3, 105, 161, 0.35);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
/* 聚落：與工具遙控器垂直堆疊（變數由 colony-lib/styles.css 提供；優先掛入 FAB 欄） */
body[data-tm-assist="colony"] .tm-mate-fab {
  right: 20px;
  bottom: var(--colony-ai-fab-bottom, calc(24px + 55px + 14px + env(safe-area-inset-bottom, 0px)));
  z-index: 920;
}
/* 已入欄：取消 fixed，避免與 Tools／Share 同座標重疊 */
body[data-tm-assist="colony"] .colony-embed-mobile-fabs > .tm-mate-fab {
  position: relative !important;
  right: auto !important;
  bottom: auto !important;
  left: auto !important;
}
.tm-mate-fab:hover { transform: translateY(-2px) scale(1.04); }
.tm-mate-fab:focus-visible { outline: 2px solid #38bdf8; outline-offset: 3px; }
.tm-mate-fab .icon-svg,
.tm-mate-fab svg { width: 24px; height: 24px; stroke: currentColor; fill: none; stroke-width: 2; }

.tm-mate-panel {
  position: fixed;
  right: 16px;
  bottom: 152px;
  z-index: 15060;
  width: min(380px, calc(100vw - 24px));
  height: min(560px, calc(100vh - 180px));
  display: none;
  flex-direction: column;
  background: var(--card-bg, #fff);
  color: var(--text-main, #0f172a);
  border: 1px solid var(--border-soft, #e2e8f0);
  border-radius: 16px;
  box-shadow: 0 18px 50px rgba(15, 23, 42, 0.22);
  overflow: hidden;
}
body[data-tm-assist="colony"] .tm-mate-panel {
  bottom: calc(var(--colony-ai-fab-bottom, 93px) + var(--colony-ai-fab-size, 52px) + 14px);
}
.tm-mate-panel.is-open { display: flex; }

.tm-mate-panel__head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  background: linear-gradient(120deg, #0ea5e9, #0284c7);
  color: #fff;
}
.tm-mate-panel__head h2 {
  margin: 0;
  font-size: 15px;
  font-weight: 800;
  flex: 1;
  line-height: 1.3;
}
.tm-mate-panel__head .tm-mate-sub {
  display: block;
  font-size: 11px;
  font-weight: 500;
  opacity: 0.92;
}
.tm-mate-panel__head .tm-mate-llm-status {
  display: block;
  font-size: 10px;
  font-weight: 600;
  margin-top: 2px;
  opacity: 0.88;
}
.tm-mate-panel__head button {
  background: rgba(255,255,255,0.18);
  border: none;
  color: #fff;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.tm-mate-panel__head button svg { width: 16px; height: 16px; stroke: currentColor; fill: none; stroke-width: 2; }

.tm-mate-panel__chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border-soft, #e2e8f0);
  background: var(--bg-surface, #f8fafc);
}
.tm-mate-chip {
  border: 1px solid var(--border-soft, #cbd5e1);
  background: var(--card-bg, #fff);
  color: var(--color-brand, #0369a1);
  font-size: 12px;
  font-weight: 700;
  padding: 5px 10px;
  border-radius: 999px;
  cursor: pointer;
}
.tm-mate-chip:hover { background: var(--info-bg, #e0f2fe); }

.tm-mate-panel__log {
  flex: 1;
  overflow: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--bg-main, #f1f5f9);
}
.tm-mate-msg {
  max-width: 92%;
  padding: 10px 12px;
  border-radius: 12px;
  font-size: 13px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
}
.tm-mate-msg--bot {
  align-self: flex-start;
  background: var(--card-bg, #fff);
  border: 1px solid var(--border-soft, #e2e8f0);
  color: var(--text-main, #0f172a);
}
.tm-mate-msg--user {
  align-self: flex-end;
  background: #0ea5e9;
  color: #fff;
  border: none;
}
.tm-mate-msg a { color: var(--color-brand, #0369a1); font-weight: 700; }
.tm-mate-msg--user a { color: #e0f2fe; }

.tm-mate-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}
.tm-mate-action {
  display: inline-flex;
  align-items: center;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid var(--info-border, #7dd3fc);
  background: var(--info-bg, #e0f2fe);
  color: var(--info-text, #0369a1) !important;
  font-size: 12px;
  font-weight: 700;
  text-decoration: none !important;
}
.tm-mate-action:hover { filter: brightness(1.08); }

.tm-mate-followups {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px dashed var(--border-soft, #e2e8f0);
}
.tm-mate-follow {
  border: 1px solid var(--border-soft, #cbd5e1);
  background: var(--bg-surface, #f8fafc);
  color: var(--text-main, #334155);
  font-size: 12px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 999px;
  cursor: pointer;
}
.tm-mate-follow:hover { background: var(--info-bg, #e0f2fe); border-color: var(--info-border, #7dd3fc); color: var(--info-text, #0369a1); }

.tm-mate-msg--status {
  opacity: 0.92;
  font-size: 12px;
  color: var(--text-muted, #64748b);
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.tm-mate-dots {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #0ea5e9;
  display: inline-block;
  animation: tm-mate-pulse 1s ease-in-out infinite;
}
@keyframes tm-mate-pulse {
  0%, 100% { opacity: 0.35; transform: scale(0.85); }
  50% { opacity: 1; transform: scale(1.15); }
}

.tm-mate-upload {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  border: 1px solid var(--border-soft, #cbd5e1);
  background: var(--bg-surface, #f8fafc);
  color: var(--color-brand, #0369a1);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.tm-mate-upload:hover { background: var(--info-bg, #e0f2fe); }
.tm-mate-upload-preview {
  display: block;
  margin-top: 8px;
  max-width: 160px;
  max-height: 160px;
  border-radius: 8px;
  object-fit: cover;
  border: 1px solid rgba(255,255,255,0.35);
}

.tm-mate-compose-preview {
  margin-top: 10px;
  border: 1px solid var(--border-soft, #e2e8f0);
  border-radius: 12px;
  background: var(--bg-surface, #f8fafc);
  overflow: hidden;
}
.tm-mate-compose-preview__head {
  padding: 8px 10px;
  font-size: 12px;
  font-weight: 700;
  color: #0369a1;
  border-bottom: 1px solid var(--border-soft, #e2e8f0);
  background: #e0f2fe;
}
.tm-mate-compose-preview__grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0;
}
.tm-mate-compose-preview__col {
  padding: 8px 10px;
  min-width: 0;
  border-right: 1px solid var(--border-soft, #e2e8f0);
}
.tm-mate-compose-preview__col--ai {
  border-right: none;
  background: #fff;
}
.tm-mate-compose-preview__label {
  font-size: 11px;
  font-weight: 700;
  color: #64748b;
  margin-bottom: 6px;
}
.tm-mate-compose-preview__body {
  font-size: 12px;
  line-height: 1.55;
  color: #334155;
  max-height: 220px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
}
.tm-mate-compose-preview__body--html {
  white-space: normal;
}
.tm-mate-compose-preview__body--html a {
  color: #0369a1;
  font-weight: 700;
  word-break: break-all;
}
.tm-mate-compose-preview__body--html ul {
  margin: 0.4em 0;
  padding-left: 1.2em;
}
.tm-mate-cover-assist {
  margin-top: 10px;
  border: 1px solid var(--border-soft, #e2e8f0);
  border-radius: 12px;
  background: var(--bg-surface, #f8fafc);
  overflow: hidden;
}
.tm-mate-cover-assist__head {
  padding: 8px 10px;
  font-size: 12px;
  font-weight: 700;
  color: #9f1239;
  border-bottom: 1px solid var(--border-soft, #e2e8f0);
  background: #ffe4e6;
}
.tm-mate-cover-assist__grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0;
}
.tm-mate-cover-assist__col {
  padding: 8px 10px;
  min-width: 0;
  border-right: 1px solid var(--border-soft, #e2e8f0);
}
.tm-mate-cover-assist__col:last-child {
  border-right: none;
}
.tm-mate-cover-assist__label {
  font-size: 11px;
  font-weight: 700;
  color: #64748b;
  margin-bottom: 6px;
}
.tm-mate-cover-assist__img {
  display: block;
  width: 100%;
  max-height: 180px;
  object-fit: contain;
  border-radius: 8px;
  background: #fff;
  border: 1px solid #e2e8f0;
}
.tm-mate-darkroom-assist {
  margin-top: 10px;
  padding: 10px 12px;
  border-radius: 12px;
  background: linear-gradient(160deg, #15201a 0%, #1a1510 100%);
  border: 1px solid rgba(212, 163, 90, 0.4);
  color: #eef3ef;
}
.tm-mate-darkroom-assist__head {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: #d4a35a;
  margin-bottom: 8px;
}
.tm-mate-darkroom-assist__tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}
.tm-mate-darkroom-tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  padding: 3px 8px;
  border-radius: 999px;
  background: rgba(212, 163, 90, 0.14);
  border: 1px solid rgba(212, 163, 90, 0.35);
}
.tm-mate-darkroom-tag small {
  color: #d4a35a;
  font-size: 10px;
}
.tm-mate-darkroom-tag--free {
  background: rgba(61, 143, 110, 0.18);
  border-color: rgba(61, 143, 110, 0.4);
}
.tm-mate-darkroom-assist__prompt {
  font-size: 12px;
  line-height: 1.45;
  color: #c5d0c8;
  margin-bottom: 8px;
}
.tm-mate-darkroom-assist__img {
  display: block;
  width: 100%;
  max-width: 280px;
  border-radius: 10px;
  border: 1px solid rgba(238, 243, 239, 0.12);
  background: #0f1412;
}
.tm-mate-followups {
  flex-wrap: wrap;
}

@media (max-width: 640px) {
  .tm-mate-compose-preview__grid {
    grid-template-columns: 1fr;
  }
  .tm-mate-compose-preview__col {
    border-right: none;
    border-bottom: 1px solid var(--border-soft, #e2e8f0);
  }
  .tm-mate-compose-preview__col--ai {
    border-bottom: none;
  }
  .tm-mate-cover-assist__grid {
    grid-template-columns: 1fr;
  }
  .tm-mate-cover-assist__col {
    border-right: none;
    border-bottom: 1px solid var(--border-soft, #e2e8f0);
  }
  .tm-mate-cover-assist__col:last-child {
    border-bottom: none;
  }
}

.tm-mate-panel__foot {
  display: flex;
  gap: 8px;
  padding: 10px;
  border-top: 1px solid var(--border-soft, #e2e8f0);
  background: var(--card-bg, #fff);
  align-items: flex-end;
  position: relative;
}
.tm-mate-slash-wrap {
  flex: 1;
  min-width: 0;
  position: relative;
  display: flex;
}
.tm-mate-slash {
  position: absolute;
  left: 0;
  right: 0;
  bottom: calc(100% + 6px);
  max-height: 240px;
  overflow-y: auto;
  z-index: 40;
  background: var(--card-bg, #fff);
  border: 1px solid var(--border-soft, #cbd5e1);
  border-radius: 12px;
  box-shadow: 0 10px 28px rgba(15, 23, 42, 0.14);
  padding: 6px;
}
.tm-mate-slash__item {
  display: grid;
  grid-template-columns: minmax(0, 1.1fr) minmax(0, 1.4fr) auto;
  gap: 6px 10px;
  width: 100%;
  text-align: left;
  align-items: center;
  padding: 8px 10px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--text-main, #0f172a);
  cursor: pointer;
  font: inherit;
}
.tm-mate-slash__item:hover,
.tm-mate-slash__item.is-active {
  background: var(--bg-muted, #f1f5f9);
}
.tm-mate-slash__cmd {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  font-weight: 700;
  color: #0369a1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tm-mate-slash__label {
  font-size: 13px;
  font-weight: 600;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tm-mate-slash__tip {
  display: block;
  font-size: 11px;
  font-weight: 500;
  color: var(--text-muted, #64748b);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tm-mate-slash__group {
  font-size: 11px;
  color: var(--text-muted, #94a3b8);
  white-space: nowrap;
}
.tm-mate-slash__empty {
  padding: 10px 12px;
  font-size: 13px;
  color: var(--text-muted, #64748b);
}
@media (max-width: 480px) {
  .tm-mate-slash__item {
    grid-template-columns: 1fr auto;
    grid-template-rows: auto auto;
  }
  .tm-mate-slash__cmd {
    grid-column: 1 / 2;
  }
  .tm-mate-slash__group {
    grid-column: 2 / 3;
    grid-row: 1 / 2;
  }
  .tm-mate-slash__label {
    grid-column: 1 / -1;
  }
}
.tm-mate-panel__foot textarea {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  max-height: 120px;
  resize: vertical;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid var(--border-soft, #cbd5e1);
  font-size: 14px;
  font-family: inherit;
  line-height: 1.4;
  background: var(--bg-surface, #fff);
  color: var(--text-main, #0f172a);
}
.tm-mate-panel__foot input {
  flex: 1;
  min-width: 0;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border-soft, #cbd5e1);
  font-size: 14px;
  background: var(--bg-surface, #fff);
  color: var(--text-main, #0f172a);
}
.tm-mate-panel__foot button.tm-mate-send {
  padding: 0 14px;
  border: none;
  border-radius: 10px;
  background: #0284c7;
  color: #fff;
  font-weight: 800;
  cursor: pointer;
  white-space: nowrap;
}

/* 獨立頁 */
.tm-mate-page {
  min-height: 100vh;
  margin: 0;
  font-family: "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
  background:
    radial-gradient(900px 400px at 10% -10%, #bae6fd 0%, transparent 55%),
    radial-gradient(700px 360px at 100% 0%, #e0f2fe 0%, transparent 50%),
    #f8fafc;
  color: #0f172a;
}
.tm-mate-page__wrap {
  max-width: 720px;
  margin: 0 auto;
  padding: 24px 16px 48px;
}
.tm-mate-page__brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
}
.tm-mate-page__brand img { width: 40px; height: 40px; border-radius: 10px; }
.tm-mate-page__brand h1 { margin: 0; font-size: 22px; }
.tm-mate-page__brand p { margin: 2px 0 0; font-size: 13px; color: #64748b; }
.tm-mate-page .tm-mate-panel {
  position: relative;
  right: auto;
  bottom: auto;
  width: 100%;
  height: min(70vh, 640px);
  display: flex;
  box-shadow: 0 12px 40px rgba(15, 23, 42, 0.12);
}
.tm-mate-teach {
  margin-top: 18px;
  padding: 16px;
  border-radius: 14px;
  background: #fff;
  border: 1px solid #e2e8f0;
}
.tm-mate-teach h3 { margin: 0 0 8px; font-size: 15px; }
.tm-mate-teach textarea,
.tm-mate-teach input {
  width: 100%;
  box-sizing: border-box;
  margin: 6px 0;
  padding: 10px;
  border-radius: 8px;
  border: 1px solid #cbd5e1;
  font-size: 14px;
}
.tm-mate-teach textarea { min-height: 88px; resize: vertical; }
.tm-mate-teach .row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
.tm-mate-teach button {
  padding: 8px 14px;
  border-radius: 8px;
  border: none;
  background: #0284c7;
  color: #fff;
  font-weight: 700;
  cursor: pointer;
}
.tm-mate-teach button.secondary { background: #64748b; }

/* 頂欄／獨立網 AI 入口（與 FAB 同一套） */
.tm-mate-assist-btn,
.tm-mate-assist-btn--header {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-width: 35px;
  height: 35px;
  padding: 0 8px;
  border-radius: 999px;
  border: 1px solid var(--border-soft, #e2e8f0);
  background: var(--bg-surface, #fff);
  color: var(--text-main, #0369a1);
  cursor: pointer;
  box-sizing: border-box;
  flex-shrink: 0;
}
.tm-mate-assist-btn:hover,
.tm-mate-assist-btn--header:hover {
  border-color: #7dd3fc;
  background: #f0f9ff;
}
.tm-mate-assist-btn .icon-svg,
.tm-mate-assist-btn svg {
  width: 16px;
  height: 16px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
}
.tm-mate-assist-btn__label {
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.02em;
}
.tm-mate-assist-btn--standalone {
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: 10px;
  margin-left: 4px;
}
.tm-mate-ctx-banner {
  display: block;
  font-size: 10px;
  font-weight: 600;
  margin-top: 2px;
  opacity: 0.9;
}

body.dark-mode .tm-mate-assist-btn,
body.dark-mode .tm-mate-assist-btn--header,
html.dark .tm-mate-assist-btn,
html[data-tm-scheme="dark"] .tm-mate-assist-btn {
  background: rgba(15, 23, 42, 0.55);
  border-color: rgba(148, 163, 184, 0.35);
  color: #7dd3fc;
}

.colony-sticky-bar .tm-mate-assist-btn--standalone {
  margin-left: auto;
  margin-right: 8px;
  align-self: center;
}

@media (max-width: 640px) {
  .tm-mate-fab { bottom: 76px; right: 12px; }
  /* 聚落頁改由 colony FAB 欄 flex 堆疊；未入欄時仍用變數 */
  body[data-tm-assist="colony"] .tm-mate-fab {
    bottom: var(--colony-ai-fab-bottom, 76px);
    right: 16px;
  }
  body[data-tm-assist="colony"] .colony-embed-mobile-fabs > .tm-mate-fab {
    position: relative !important;
    right: auto !important;
    bottom: auto !important;
  }
  .tm-mate-panel {
    right: 8px;
    left: 8px;
    width: auto;
    bottom: 140px;
    height: min(62vh, 520px);
  }
  body[data-tm-assist="colony"] .tm-mate-panel {
    bottom: calc(var(--colony-ai-fab-bottom, 76px) + var(--colony-ai-fab-size, 52px) + 14px);
  }
  .tm-mate-assist-btn--header .tm-mate-assist-btn__label {
    display: none;
  }
}
