:root {
  --bg: #0d0d12;
  --panel: #16161d;
  --panel-2: #1f1f28;
  --border: #2a2a36;
  --text: #ececf3;
  --muted: #9494a8;
  --accent: #7c7cf5;
  --accent-ink: #ffffff;
  --accent-soft: #25254a;
  --danger: #f06a7e;
  --ok: #3ccf8e;
  --warn: #e7b04a;
  --radius: 14px;
  --shadow: 0 1px 0 rgba(255, 255, 255, .03) inset, 0 8px 24px rgba(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: 15px/1.55 system-ui, -apple-system, "Segoe UI", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
}
[hidden] { display: none !important; }
h1, h2 { margin: 0; }
h2 { font-size: 15px; font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--danger); }
.warn { color: var(--warn); font-size: 13px; margin: 8px 0 0; }

/* 按鈕 */
.btn {
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 9px 16px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  background: var(--panel-2);
  color: var(--text);
  transition: filter .15s, background .15s;
}
.btn:hover:not(:disabled) { filter: brightness(1.06); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.danger { background: var(--danger); color: #fff; }
.btn.ghost { background: transparent; border-color: var(--border); }
.btn.small { padding: 5px 10px; font-size: 13px; font-weight: 500; }
.btn.block { width: 100%; padding: 12px; font-size: 16px; }
.danger-text { color: var(--danger); }
.icon-btn {
  display: inline-grid; place-items: center;
  width: 36px; height: 36px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--panel);
  color: var(--muted); cursor: pointer; flex: none;
}
.icon-btn:hover { color: var(--text); }

/* 登入 */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 20px;
  padding: 40px 32px; width: min(380px, 100%); text-align: center; box-shadow: var(--shadow);
  display: grid; gap: 12px; justify-items: center;
}
.login-card h1 { font-size: 24px; }

/* 版面 */
.app { height: 100%; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--panel);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; }
.brand-mark {
  width: 30px; height: 30px; border-radius: 9px; background: var(--accent); color: #fff;
  display: grid; place-items: center; font-weight: 800;
}
.brand-mark.big { width: 56px; height: 56px; border-radius: 16px; font-size: 26px; }
.topbar-right { display: flex; align-items: center; gap: 10px; }

.layout { flex: 1; min-height: 0; display: grid; grid-template-columns: 330px 1fr; }
.dash { border-right: 1px solid var(--border); padding: 16px; overflow-y: auto; display: grid; gap: 16px; align-content: start; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }

.pill {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 13px; font-weight: 600; padding: 3px 10px; border-radius: 999px; background: var(--panel-2);
}
.pill i { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.pill.ready i { background: var(--ok); }
.pill.busy i { background: var(--warn); animation: pulse 1.2s infinite; }
.pill.error i { background: var(--danger); }
@keyframes pulse { 50% { opacity: .3; } }

.field-row { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.field-row label { color: var(--muted); font-size: 13px; }
.field-row select { flex: 1; }
select, input, textarea {
  font: inherit; color: var(--text); background: var(--panel);
  border: 1px solid var(--border); border-radius: 8px; padding: 6px 8px;
}
select:focus, input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; margin: 14px 0 0; }
.stats div { min-width: 0; }
.stats dt { font-size: 12px; color: var(--muted); }
.stats dd { margin: 0; font-weight: 600; font-variant-numeric: tabular-nums; }
.status-msg { margin: 12px 0 0; font-size: 13px; color: var(--muted); min-height: 1.2em; }
.log { margin-top: 8px; font-size: 12px; color: var(--muted); }
.log summary { cursor: pointer; }
.log ol { list-style: none; padding: 0; margin: 6px 0 0; max-height: 160px; overflow-y: auto; font-family: ui-monospace, Consolas, monospace; }

.balance { display: flex; align-items: baseline; gap: 8px; }
.balance span { font-size: 34px; font-weight: 700; font-variant-numeric: tabular-nums; }
.balance small { color: var(--muted); }
#btn-stop-all { margin-top: 14px; }
.drive-link { color: var(--accent); font-weight: 600; text-decoration: none; word-break: break-all; }
.drive-link:hover { text-decoration: underline; }
.drive-note { margin: 8px 0 0; }

/* 對話 */
.chat { display: flex; flex-direction: column; min-height: 0; }
.messages { flex: 1; overflow-y: auto; padding: 20px 16px; display: flex; flex-direction: column; gap: 14px; }
.empty { margin: auto; text-align: center; max-width: 420px; }
.empty h2 { font-size: 20px; margin-bottom: 8px; }

.msg { max-width: min(720px, 100%); display: grid; gap: 8px; }
.msg.user { align-self: flex-end; justify-items: end; }
.msg.bot { align-self: flex-start; }
.bubble { padding: 10px 14px; border-radius: 14px; background: var(--panel); border: 1px solid var(--border); white-space: pre-wrap; word-break: break-word; }
.msg.user .bubble { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { font-size: 12px; color: var(--muted); background: var(--panel-2); border-radius: 999px; padding: 2px 8px; }
.thumbs { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.thumbs img { width: 72px; height: 72px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); }
.result { border-radius: 14px; overflow: hidden; border: 1px solid var(--border); background: var(--panel-2); }
.result img { display: block; max-width: 100%; max-height: 70vh; cursor: zoom-in; }
.actions { display: flex; flex-wrap: wrap; gap: 6px; }
.pending { display: flex; align-items: center; gap: 10px; }
.spinner { width: 18px; height: 18px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.bubble.err { border-color: var(--danger); color: var(--danger); }

.composer { border-top: 1px solid var(--border); background: var(--panel); padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); display: grid; gap: 8px; }
.attachments { display: flex; gap: 8px; flex-wrap: wrap; }
.att { position: relative; }
.att img { width: 64px; height: 64px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); display: block; }
.att button {
  position: absolute; top: -6px; right: -6px; width: 22px; height: 22px; border-radius: 50%;
  border: none; background: var(--text); color: var(--bg); cursor: pointer; font-size: 13px; line-height: 1;
}
.input-row { display: flex; gap: 8px; align-items: flex-end; }
.input-row textarea { flex: 1; resize: none; max-height: 200px; padding: 8px 10px; min-width: 0; }
.send { flex: none; }
.options, .advanced { display: flex; flex-wrap: wrap; gap: 10px 14px; align-items: center; font-size: 13px; color: var(--muted); }
.options label, .advanced label { display: inline-flex; align-items: center; gap: 6px; }
.options input[type=number] { width: 84px; }
#seed { width: 120px; }
.advanced input[type=text] { width: min(360px, 60vw); }
.hint { margin: 0; font-size: 12px; color: var(--muted); min-height: 1em; }
.composer.drag { outline: 2px dashed var(--accent); outline-offset: -6px; }

.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.85); display: grid; place-items: center; z-index: 20; cursor: zoom-out; padding: 16px; }
.lightbox img { max-width: 100%; max-height: 100%; }
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 30;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 14px; max-width: calc(100% - 32px);
}

/* 頂部迷你狀態（只在手機顯示） */
.mini-status {
  display: none; align-items: center; gap: 8px;
  border: 1px solid var(--border); background: var(--panel-2); color: var(--text);
  border-radius: 999px; padding: 4px 10px; font: inherit; font-size: 13px; cursor: pointer;
}
.mini-status i { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.mini-status.ready i { background: var(--ok); }
.mini-status.busy i { background: var(--warn); animation: pulse 1.2s infinite; }
.mini-status.error i { background: var(--danger); }
.mini-status b { font-weight: 600; font-variant-numeric: tabular-nums; }

/* 手機：整頁自然捲動，輸入框固定在底部 */
@media (max-width: 860px) {
  .app { height: auto; min-height: 100%; display: block; }
  .topbar { position: sticky; top: 0; z-index: 10; padding: 8px 12px; }
  .brand span { display: none; }
  .mini-status { display: inline-flex; }
  #me { display: none; }
  .layout { display: block; }
  .dash { border-right: none; border-bottom: 1px solid var(--border); padding: 12px; gap: 12px; overflow: visible; }
  .card { padding: 14px; }
  .card-head { margin-bottom: 10px; }
  .balance span { font-size: 28px; }
  .stats { margin-top: 12px; }
  .chat { display: block; }
  .messages { overflow: visible; padding: 16px 12px; min-height: 40vh; }
  .msg { max-width: 100%; }
  .result img { max-height: none; width: 100%; height: auto; }
  .composer { position: sticky; bottom: 0; z-index: 5; padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); box-shadow: 0 -8px 24px rgba(0, 0, 0, .4); }
  .options { gap: 8px 12px; }
  .options input[type=number] { width: 64px; }
  #seed { width: 96px; }
  .input-row textarea { font-size: 16px; } /* iOS 字級小於 16px 會自動放大畫面 */
  .input-row .attach, .send { height: 42px; }
  .options .extra { display: none; }
  .composer.adv .options .extra { display: inline-flex; }
  .hint:empty { display: none; }
}
