:root {
  --bg: #ffffff;
  --brand: #1a73e8;
  --brand-soft: #e8f1fd;
  --brand-softer: #f4f8fe;
  --text: #2b2f36;
  --muted: #6b7280;
  --line: #e6e8ec;
  --danger: #d93025;
  --radius: 20px;
  --shadow: 0 4px 24px rgba(26, 115, 232, 0.08), 0 1px 4px rgba(20, 30, 50, 0.06);
  --shadow-lg: 0 12px 40px rgba(20, 30, 50, 0.14);
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --side: 76px; /* 預留給左右下角固定按鈕 */
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; background: var(--bg); color: var(--text); font-family: var(--font); -webkit-font-smoothing: antialiased; }
body { min-height: 100dvh; font-size: 16px; line-height: 1.6; }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }

/* 頂部 */
.topbar { position: fixed; top: 0; left: 0; right: 0; height: 60px; display: flex; align-items: center; justify-content: space-between; padding: 0 16px; background: rgba(255,255,255,.86); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); z-index: 20; border-bottom: 1px solid transparent; transition: border-color .3s; }
body.chatting .topbar { border-bottom-color: var(--line); }
.brand-mini { display: flex; align-items: center; gap: 8px; font-weight: 700; letter-spacing: .08em; opacity: 0; transition: opacity .3s; }
body.chatting .brand-mini { opacity: 1; }
.brand-mini img { border-radius: 8px; }
.chat-title { position: absolute; left: 50%; transform: translateX(-50%); max-width: 46%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); font-size: 14px; }
.avatar { width: 36px; height: 36px; border-radius: 50%; border: 0; background: var(--brand); color: #fff; font-weight: 600; box-shadow: var(--shadow); transition: transform .15s; }
.avatar:hover { transform: scale(1.06); }

/* 歡迎畫面 */
.hero { position: fixed; left: 0; right: 0; bottom: calc(46% + 28px); display: flex; flex-direction: column; align-items: center; text-align: center; padding: 0 24px; transition: opacity .35s, transform .35s; z-index: 5; }
body.chatting .hero { opacity: 0; transform: translateY(-16px); pointer-events: none; }
.hero-logo { border-radius: 22px; }
.hero h1 { margin: 12px 0 0; font-size: clamp(40px, 7vw, 56px); letter-spacing: .14em; font-weight: 700; color: var(--brand); line-height: 1.1; }
.hero-welcome { margin: 10px 0 0; font-size: clamp(17px, 2.6vw, 21px); }
.hero-tag { margin: 4px 0 0; color: var(--muted); font-size: 14px; letter-spacing: .04em; }

/* 輸入框 */
.composer-wrap { position: fixed; left: var(--side); right: var(--side); bottom: max(24px, env(safe-area-inset-bottom)); display: flex; justify-content: center; z-index: 15; transition: bottom .45s cubic-bezier(.2,.8,.2,1), top .45s cubic-bezier(.2,.8,.2,1); pointer-events: none; }
body.home .composer-wrap { bottom: calc(46% - 56px); }
.composer { pointer-events: auto; width: 100%; max-width: 720px; display: flex; align-items: flex-end; gap: 8px; padding: 10px 10px 10px 20px; background: #fff; border: 1px solid var(--line); border-radius: 28px; box-shadow: var(--shadow); transition: box-shadow .2s, border-color .2s; }
.composer:focus-within { border-color: #b9d4f8; box-shadow: 0 6px 28px rgba(26,115,232,.16); }
.composer textarea { flex: 1; border: 0; outline: 0; resize: none; background: transparent; line-height: 1.5; padding: 8px 0; max-height: 200px; min-height: 28px; }
.send { width: 40px; height: 40px; flex: none; border-radius: 50%; border: 0; background: var(--brand); color: #fff; display: grid; place-items: center; transition: background .15s, transform .15s, opacity .15s; }
.send:hover { transform: scale(1.06); }
.send:disabled { opacity: .35; cursor: not-allowed; transform: none; }
.send.stop svg { display: none; }
.send.stop::after { content: ""; width: 12px; height: 12px; background: #fff; border-radius: 3px; }

/* 左右下角按鈕 */
.fab { position: fixed; bottom: max(24px, env(safe-area-inset-bottom)); width: 52px; height: 52px; border-radius: 50%; border: 1px solid var(--line); background: #fff; color: var(--brand); display: grid; place-items: center; box-shadow: var(--shadow); z-index: 16; transition: transform .15s, background .15s; }
.fab:hover { transform: translateY(-2px); background: var(--brand-softer); }
.fab-left { left: 16px; }
.fab-right { right: 16px; }

/* 訊息 */
.messages { max-width: 760px; margin: 0 auto; padding: 84px 20px 180px; display: none; }
body.chatting .messages { display: block; }
.msg { margin: 0 0 26px; animation: rise .3s ease both; }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble { max-width: 85%; background: var(--brand-soft); border-radius: 20px 20px 6px 20px; padding: 10px 16px; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.assistant .bubble { overflow-wrap: anywhere; }
.msg-actions { margin-top: 6px; display: flex; gap: 6px; }
.chip { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 2px 12px; font-size: 13px; color: var(--muted); transition: background .15s, color .15s; }
.chip:hover { background: var(--brand-softer); color: var(--brand); }
.msg.error .bubble { color: var(--danger); }
.typing { display: inline-flex; gap: 4px; padding: 6px 0; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--brand); opacity: .35; animation: blink 1.1s infinite; }
.typing i:nth-child(2) { animation-delay: .18s; } .typing i:nth-child(3) { animation-delay: .36s; }
@keyframes blink { 40% { opacity: 1; transform: translateY(-3px); } }

/* Markdown */
.bubble p { margin: 0 0 .8em; } .bubble p:last-child { margin-bottom: 0; }
.bubble h2, .bubble h3, .bubble h4, .bubble h5 { margin: 1.1em 0 .4em; line-height: 1.35; }
.bubble h2 { font-size: 1.35em; } .bubble h3 { font-size: 1.15em; } .bubble h4, .bubble h5 { font-size: 1em; }
.bubble ul, .bubble ol { margin: 0 0 .8em; padding-left: 1.4em; }
.bubble li { margin: .2em 0; }
.bubble blockquote { margin: 0 0 .8em; padding: 2px 14px; border-left: 3px solid var(--brand-soft); color: var(--muted); }
.bubble hr { border: 0; border-top: 1px solid var(--line); margin: 1.2em 0; }
.bubble a { color: var(--brand); }
.bubble code { font-family: var(--mono); font-size: .9em; background: #f1f3f6; padding: .1em .35em; border-radius: 6px; }
.code { margin: 0 0 .9em; border: 1px solid var(--line); border-radius: 14px; overflow: hidden; background: #fafbfc; }
.code-head { display: flex; justify-content: space-between; align-items: center; padding: 6px 12px; background: #f1f3f6; font-size: 12px; color: var(--muted); }
.code-head .copy-code { border: 0; background: transparent; color: var(--brand); font-size: 12px; padding: 2px 6px; border-radius: 6px; }
.code-head .copy-code:hover { background: var(--brand-soft); }
.code pre { margin: 0; padding: 12px 14px; overflow-x: auto; }
.code pre code { background: none; padding: 0; font-size: 13.5px; line-height: 1.55; }
.table-wrap { overflow-x: auto; margin: 0 0 .9em; }
.bubble table { border-collapse: collapse; font-size: .95em; }
.bubble th, .bubble td { border: 1px solid var(--line); padding: 6px 12px; text-align: left; }
.bubble th { background: var(--brand-softer); }

/* 浮層 */
.overlay { position: fixed; inset: 0; background: rgba(20, 30, 50, .28); backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px); display: flex; align-items: flex-start; justify-content: center; padding: 8vh 16px 16px; z-index: 40; animation: fade .18s ease; overflow-y: auto; }
.overlay.solid { background: #fff; align-items: center; padding-top: 16px; }
@keyframes fade { from { opacity: 0; } }
.sheet { width: 100%; max-width: 520px; background: #fff; border-radius: var(--radius); box-shadow: var(--shadow-lg); padding: 20px; animation: pop .22s cubic-bezier(.2,.8,.2,1); }
.overlay.solid .sheet { box-shadow: none; }
@keyframes pop { from { opacity: 0; transform: translateY(10px) scale(.98); } }
.sheet-head { display: flex; align-items: center; gap: 10px; }
.sheet-head h3 { margin: 0; flex: 1; font-size: 18px; }
.sheet-head input { flex: 1; }
.sheet-label { margin: 14px 4px 6px; font-size: 12px; color: var(--muted); letter-spacing: .04em; }
.sheet h4 { margin: 0 0 8px; }
.sheet hr { border: 0; border-top: 1px solid var(--line); margin: 18px 0; }

input[type=text], input[type=email], input[type=password], input[type=search] { width: 100%; padding: 11px 14px; border: 1px solid var(--line); border-radius: 12px; background: #fff; outline: 0; transition: border-color .15s, box-shadow .15s; }
input:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
input:disabled { background: #f6f7f9; color: var(--muted); }
label { display: block; margin: 0 0 12px; font-size: 14px; color: var(--muted); }
label input { margin-top: 4px; color: var(--text); }
.hint, .muted { color: var(--muted); font-size: 13px; margin: 6px 0; }
.error { color: var(--danger); font-size: 14px; margin: 6px 0; }
.status { font-size: 14px; margin: 6px 0; color: #188038; }
.status.bad { color: var(--danger); }

.primary { width: 100%; padding: 11px 16px; border: 0; border-radius: 12px; background: var(--brand); color: #fff; font-weight: 600; transition: filter .15s; }
.primary:hover { filter: brightness(1.06); }
.primary:disabled { opacity: .6; cursor: wait; }
.ghost { border: 0; background: transparent; color: var(--brand); padding: 8px 10px; border-radius: 10px; }
.ghost:hover { background: var(--brand-soft); }
.danger { width: 100%; padding: 10px 16px; border: 1px solid #f3c4c0; background: #fff; color: var(--danger); border-radius: 12px; }
.danger:hover { background: #fdf0ef; }
.solid-danger { width: auto; background: var(--danger); color: #fff; border-color: var(--danger); }
.solid-danger:hover { background: #c5271d; }

.auth { text-align: center; max-width: 400px; }
.auth h2 { margin: 8px 0 0; font-size: 34px; letter-spacing: .14em; color: var(--brand); }
.auth form { text-align: left; margin-top: 8px; }
.tabs { display: flex; gap: 4px; margin: 16px 0 12px; padding: 4px; background: var(--brand-softer); border-radius: 12px; }
.tabs button { flex: 1; border: 0; background: transparent; padding: 8px; border-radius: 9px; color: var(--muted); }
.tabs button[aria-selected=true] { background: #fff; color: var(--brand); font-weight: 600; box-shadow: 0 1px 4px rgba(20,30,50,.1); }

.results { list-style: none; margin: 0; padding: 0; max-height: 56vh; overflow-y: auto; }
.results li { display: flex; align-items: center; gap: 6px; border-radius: 12px; }
.results li:hover { background: var(--brand-softer); }
.results .open { flex: 1; min-width: 0; text-align: left; border: 0; background: transparent; padding: 10px 12px; }
.results .t { display: block; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.results .s { display: block; font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.results .del { border: 0; background: transparent; color: var(--muted); width: 34px; height: 34px; border-radius: 50%; flex: none; }
.results .del:hover { background: #fdecea; color: var(--danger); }
.results .empty { display: block; padding: 18px 12px; color: var(--muted); text-align: center; }
.confirm p { margin: 4px 0 16px; }
.row { display: flex; justify-content: flex-end; gap: 8px; }

.menu { position: fixed; top: 58px; right: 12px; min-width: 220px; background: #fff; border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow-lg); padding: 6px; z-index: 30; animation: pop .18s ease; }
.menu-user { display: flex; flex-direction: column; padding: 10px 12px; border-bottom: 1px solid var(--line); margin-bottom: 4px; }
.menu-user span { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; }
.menu button { display: block; width: 100%; text-align: left; border: 0; background: transparent; padding: 9px 12px; border-radius: 10px; }
.menu button:hover { background: var(--brand-softer); }

.banner { position: fixed; top: 68px; left: 50%; transform: translateX(-50%); max-width: min(560px, calc(100% - 32px)); background: #fff8e6; border: 1px solid #f6d78a; color: #7a5b00; border-radius: 12px; padding: 8px 14px; font-size: 14px; z-index: 18; box-shadow: var(--shadow); }
.toast { position: fixed; left: 50%; bottom: 96px; transform: translateX(-50%); background: #2b2f36; color: #fff; padding: 8px 16px; border-radius: 999px; font-size: 14px; z-index: 60; animation: fade .2s; }

@media (max-width: 640px) {
  :root { --side: 72px; }
  .chat-title { display: none; }
  .messages { padding: 76px 14px 170px; }
  .msg.user .bubble { max-width: 92%; }
  body.home .composer-wrap { bottom: calc(46% - 48px); }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }

/* Logo 為 JPEG（底色近白）：用 multiply 讓它與純白背景無縫融合 */
.hero-logo, .brand-mini img { mix-blend-mode: multiply; }

.primary.auto { width: auto; padding: 10px 22px; }
.results .ren { border: 0; background: transparent; color: var(--muted); width: 34px; height: 34px; border-radius: 50%; flex: none; }
.results .ren:hover { background: var(--brand-soft); color: var(--brand); }
