/* ===== 色彩與字體（淺色 / 深色自動切換） ===== */
:root {
  --bg: #f5f5f7;
  --surface: #ffffff;
  --surface-2: #f0f0f3;
  --text: #1d1d1f;
  --muted: #6e6e73;
  --line: rgba(0, 0, 0, 0.08);
  --accent: #6b6ff0;          /* 取自 App 圖示的紫藍色 */
  --accent-soft: #eef0ff;
  --accent-text: #ffffff;
  --peach: #ffb38a;
  --danger: #d93a3a;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 8px 24px rgba(0, 0, 0, 0.06);
  --radius: 18px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang TC", "Noto Sans TC",
    "Thonburi", "Noto Sans Thai", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0b0f;
    --surface: #17171c;
    --surface-2: #202027;
    --text: #f5f5f7;
    --muted: #9a9aa3;
    --line: rgba(255, 255, 255, 0.09);
    --accent: #8b8ff7;
    --accent-soft: #23243d;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.55 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ===== 登入 ===== */
.login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
  background:
    radial-gradient(60% 50% at 20% 10%, rgba(139, 143, 247, 0.18), transparent 70%),
    radial-gradient(50% 40% at 85% 90%, rgba(255, 179, 138, 0.2), transparent 70%),
    var(--bg);
}
.login-card {
  width: min(360px, 100%);
  text-align: center;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 28px;
  padding: 40px 28px 28px;
  box-shadow: var(--shadow);
}
.login-icon { border-radius: 22px; box-shadow: 0 10px 30px rgba(107, 111, 240, 0.35); }
.login-card h1 { margin: 18px 0 2px; font-size: 26px; letter-spacing: -0.02em; }
.login-card p { margin: 0 0 24px; font-size: 14px; }
.field input {
  width: 100%;
  padding: 14px 16px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  outline: none;
  text-align: center;
  font-size: 17px;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.field input:focus { border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
.btn-primary {
  width: 100%;
  margin-top: 12px;
  padding: 13px 16px;
  border: 0;
  border-radius: 14px;
  background: var(--text);
  color: var(--bg);
  font-weight: 600;
  transition: transform 0.1s, opacity 0.15s;
}
.btn-primary:active { transform: scale(0.98); }
.btn-primary:disabled { opacity: 0.5; }
.error { color: var(--danger); min-height: 1.4em; margin: 12px 0 0 !important; font-size: 14px; }
.shake { animation: shake 0.35s; }
@keyframes shake { 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }

/* ===== 主畫面版面 ===== */
.app { height: 100dvh; display: flex; flex-direction: column; }
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--bg) 80%, transparent);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 5;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand img { border-radius: 9px; flex: none; }
.brand div { display: flex; flex-direction: column; line-height: 1.2; }
.brand strong { font-size: 16px; letter-spacing: -0.01em; }
.brand span { font-size: 12px; }
.top-actions { display: flex; gap: 4px; }
.btn-ghost {
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 14px;
  padding: 7px 10px;
  border-radius: 10px;
}
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }

/* ===== 對話區 ===== */
.chat {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px 12px;
  -webkit-overflow-scrolling: touch;
}
.chat > * { max-width: 720px; margin-left: auto; margin-right: auto; }
.msg { display: flex; gap: 10px; margin-bottom: 16px; animation: rise 0.25s ease-out; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.msg .avatar { width: 30px; height: 30px; border-radius: 8px; flex: none; margin-top: 2px; }
.bubble {
  position: relative;
  max-width: min(560px, 85%);
  padding: 11px 15px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--line);
  white-space: pre-wrap;
  word-break: break-word;
}
.msg.ai .bubble { border-top-left-radius: 6px; }
.msg.user { justify-content: flex-end; }
.msg.user .bubble {
  background: var(--accent);
  color: var(--accent-text);
  border-color: transparent;
  border-top-right-radius: 6px;
}
.msg.user .bubble:has(img) { padding: 5px; }
.msg.user .bubble:has(img) > div { padding: 4px 9px 5px; }
.msg.user .bubble img { display: block; max-width: 220px; max-height: 220px; border-radius: 12px; margin-bottom: 6px; }
.msg.user .bubble img:last-child { margin-bottom: 0; }
.tag {
  display: inline-block;
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  margin-bottom: 6px;
  font-weight: 500;
}
.confirm-actions { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.pill {
  border: 1px solid var(--line);
  background: var(--surface-2);
  padding: 7px 16px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 500;
}
.pill.primary { background: var(--text); color: var(--bg); border-color: transparent; }
.pill:disabled { opacity: 0.45; cursor: default; }

/* 翻譯結果卡片 */
.result { white-space: normal; padding: 14px 16px; min-width: min(320px, 85%); }
.result .label { font-size: 12px; color: var(--muted); margin: 0 0 2px; letter-spacing: 0.02em; }
.result .translation { font-size: 19px; line-height: 1.5; margin: 0 0 10px; white-space: pre-wrap; font-weight: 500; }
.result .roman { font-size: 14px; color: var(--accent); margin: 0 0 10px; white-space: pre-wrap; }
.result .original { font-size: 14px; color: var(--muted); margin: 0 0 10px; white-space: pre-wrap; }
.result .notes {
  font-size: 13px;
  background: var(--surface-2);
  border-radius: 12px;
  padding: 8px 10px;
  margin: 0 0 10px;
  white-space: pre-wrap;
}
.result-actions { display: flex; gap: 6px; border-top: 1px solid var(--line); padding-top: 10px; }
.mini {
  border: 0;
  background: var(--surface-2);
  font-size: 13px;
  padding: 5px 11px;
  border-radius: 999px;
  color: var(--text);
}
.mini.del { margin-left: auto; color: var(--muted); background: transparent; }
.mini.del:hover { color: var(--danger); }
.bubble .msg-del {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--muted);
  font-size: 13px;
  line-height: 1;
  opacity: 0;
  transition: opacity 0.15s;
}
.msg:hover .msg-del, .msg:focus-within .msg-del { opacity: 1; }
@media (hover: none) { .bubble .msg-del { opacity: 0.8; } }

.typing span {
  display: inline-block; width: 7px; height: 7px; margin: 0 2px; border-radius: 50%;
  background: var(--muted); animation: blink 1.2s infinite;
}
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 1; } }

/* ===== 輸入區 ===== */
.composer {
  padding: 10px max(16px, env(safe-area-inset-right)) calc(12px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-top: 1px solid var(--line);
}
.composer-inner { max-width: 720px; margin: 0 auto; }
.attachment {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px;
  margin-bottom: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  width: fit-content;
  max-width: 100%;
}
.attachment img { width: 44px; height: 44px; object-fit: cover; border-radius: 9px; }
.attachment span { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 200px; }
.chip-x { border: 0; background: var(--surface-2); width: 26px; height: 26px; border-radius: 50%; color: var(--muted); }

.input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 6px 6px 6px 16px;
  box-shadow: var(--shadow);
  transition: border-color 0.15s, box-shadow 0.15s;
}
.input-row:focus-within { border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
.input-row.dragover { border-color: var(--accent); border-style: dashed; }
textarea {
  flex: 1;
  border: 0;
  outline: none;
  resize: none;
  background: transparent;
  padding: 8px 0;
  max-height: 180px;
  font-size: 16px; /* 16px 以上 iPhone 才不會自動放大畫面 */
}
.send {
  flex: none;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 0;
  background: var(--text);
  color: var(--bg);
  display: grid;
  place-items: center;
  transition: transform 0.1s, opacity 0.15s;
}
.send:active { transform: scale(0.92); }
.send:disabled { opacity: 0.3; }

/* ===== 語音輸入 ===== */
.composer-inner { position: relative; }
.mic {
  flex: none;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 0;
  background: var(--surface-2);
  color: var(--text);
  display: grid;
  place-items: center;
  transition: background 0.15s, color 0.15s, transform 0.1s;
}
.mic:active { transform: scale(0.92); }
.mic.listening {
  background: var(--danger);
  color: #fff;
  animation: pulse 1.4s ease-out infinite;
}
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(217, 58, 58, 0.45); }
  100% { box-shadow: 0 0 0 12px rgba(217, 58, 58, 0); }
}
.mic-menu {
  position: absolute;
  right: 0;
  bottom: calc(100% + 8px);
  z-index: 6;
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 200px;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
  animation: rise 0.18s ease-out;
}
.mic-menu p { margin: 0 4px 2px; font-size: 13px; }
.mic-menu button {
  border: 0;
  background: var(--surface-2);
  padding: 11px 14px;
  border-radius: 12px;
  text-align: left;
  font-size: 15px;
  font-weight: 500;
}
.mic-menu button:hover { background: var(--accent-soft); color: var(--accent); }

.actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 10px; }
.action {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: 16px;
  border: 1px solid var(--line);
  background: var(--surface);
  text-align: left;
  transition: transform 0.1s, border-color 0.15s, background 0.15s;
}
.action:hover { border-color: color-mix(in srgb, var(--accent) 40%, transparent); }
.action:active { transform: scale(0.97); }
.action:disabled { opacity: 0.5; }
.action img { width: 36px; height: 36px; flex: none; }
.action span { display: flex; flex-direction: column; line-height: 1.25; min-width: 0; }
.action b { font-size: 14px; font-weight: 600; white-space: nowrap; }
.action small { font-size: 12px; color: var(--muted); white-space: nowrap; }

/* 手機窄螢幕：按鈕改成圖示在上、文字在下 */
@media (max-width: 480px) {
  .action { flex-direction: column; text-align: center; gap: 4px; padding: 10px 4px 8px; }
  .action span { align-items: center; }
  .action img { width: 34px; height: 34px; }
  .bubble { max-width: 88%; }
}
