* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
#root { margin: 0; height: var(--vvh, 100%); }
body { background: var(--paper); color: var(--ink); font-family: var(--font); font-size: 16px; line-height: 1.6; }
button { font: inherit; color: inherit; cursor: pointer; }
code, pre { font-family: var(--mono); }

.app { display: flex; flex-direction: column; height: 100%; min-width: 0; max-width: 900px; margin: 0 auto; width: 100%; }

.header { display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--line); position: relative; }
.kicker { background: var(--sun); border: 2px solid var(--ink); box-shadow: 2px 2px 0 var(--ink); border-radius: 8px; padding: 0 8px; font-weight: 700; font-size: 14px; }
.kicker { flex: none; }
.header-title { font-weight: 700; font-size: 17px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 0 1 auto; }
.target-badge { flex: 0 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; line-height: 1.4; background: var(--panel); color: var(--ink); border: 1px solid var(--line); border-radius: 999px; padding: 2px 10px; }
button.target-badge:hover { background: rgba(23,32,51,.06); }
.target-badge.target-none { color: var(--muted); border-style: dashed; }
.handle { flex: none; font-size: 14px; color: var(--muted); white-space: nowrap; }
.logout-btn { flex: none; background: none; border: 1px solid var(--line); border-radius: 8px; padding: 2px 10px; font-size: 13px; white-space: nowrap; }
.logout-btn:hover { background: rgba(23,32,51,.06); }
.gear { flex: none; }
.spacer { flex: 1; }
.py-status { font-size: 12px; color: var(--muted, #7a7a7a); margin-right: 8px; }
.py-note { display: inline-flex; align-items: center; gap: 4px; }
.py-note-close { background: none; border: 0; padding: 0 4px; font-size: 14px; color: inherit; min-width: 24px; }
.gear { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; width: 40px; height: 40px; font-size: 20px; }
.url-pop { position: absolute; top: 100%; left: 16px; z-index: 5; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--soft); padding: 12px 14px; display: flex; align-items: center; gap: 12px; max-width: calc(100% - 32px); }
.url-pop .url { font-family: var(--mono); font-size: 13px; color: var(--muted); overflow-wrap: anywhere; }
.open-btn { background: var(--mint); border: 2px solid var(--ink); box-shadow: 2px 2px 0 var(--ink); border-radius: 8px; padding: 2px 12px; font-weight: 700; color: var(--ink); text-decoration: none; white-space: nowrap; }

.transcript { flex: 1; overflow-y: auto; padding: 16px; }

/* 整頁的提示畫面：登入、連不上、分頁衝突、對話滿了、載入中 */
.gate { min-height: 100%; display: grid; place-content: center; justify-items: center; gap: 16px; padding: 24px; text-align: center; }
.gate h1 { margin: 0; font-size: 24px; }
.gate .link-btn { background: var(--sun); border: 2px solid var(--ink); box-shadow: var(--hard); border-radius: 10px; padding: 8px 18px; font-weight: 700; color: var(--ink); text-decoration: none; }
.gate .muted { color: var(--muted); margin: 0; }
.gate .login-error { color: #b3261e; font-weight: 600; margin: 0; }

/* ---- 對話 ---- */
.transcript { display: flex; flex-direction: column; gap: 12px; }
.msg { max-width: 92%; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: #e4eefb; border: 1px solid #c9dbf2; border-radius: var(--radius); padding: 8px 14px; white-space: pre-wrap; }
.msg.assistant { align-self: flex-start; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 8px 14px; }
.msg-error { color: var(--rose); font-weight: 600; }
.msg-note { color: var(--muted); font-size: 14px; }
.md p { margin: 0 0 8px; white-space: pre-wrap; }
.md p:last-child { margin-bottom: 0; }
.md ul, .md ol { margin: 0 0 8px; padding-left: 22px; }
.md-h { font-weight: 700; }
.md code { background: rgba(23,32,51,.07); border-radius: 4px; padding: 0 4px; font-size: .92em; }
.code-block { position: relative; margin: 8px 0; }
.code-block pre { margin: 0; background: #172033; color: #f3efe4; border-radius: 10px; padding: 12px 14px; overflow-x: auto; font-size: 13px; line-height: 1.5; }
.code-block pre code { background: none; padding: 0; }
.copy-btn { position: absolute; top: 6px; right: 6px; background: var(--panel); border: 1px solid var(--line); border-radius: 6px; font-size: 12px; padding: 0 8px; }

.thinking { color: var(--muted); font-size: 14px; margin-bottom: 6px; }
.thinking summary { cursor: pointer; list-style: none; }
.thinking summary::before { content: '▸ '; }
.thinking[open] summary::before { content: '▾ '; }
.thinking-body { white-space: pre-wrap; border-left: 3px solid var(--line); padding-left: 10px; margin-top: 4px; }

.tool-card { align-self: stretch; border: 1px dashed var(--line); border-left-width: 3px; border-radius: 10px; background: var(--panel); font-size: 14px; }
.tool-card.ok { border-left-color: var(--mint); border-left-style: solid; }
.tool-card.error { border-left-color: var(--rose); border-left-style: solid; }
.tool-card.running { border-left-color: var(--sky); border-left-style: solid; animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .55; } }
.tool-head { display: flex; gap: 8px; align-items: baseline; width: 100%; background: none; border: 0; padding: 6px 10px; text-align: left; }
.tool-name { font-weight: 700; }
.tool-sum { color: var(--muted); font-family: var(--mono); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.tool-body { padding: 0 10px 8px; }
.tool-body pre { margin: 4px 0 0; padding: 8px; background: rgba(23,32,51,.05); border-radius: 8px; white-space: pre-wrap; overflow-wrap: anywhere; font-size: 12.5px; max-height: 320px; overflow: auto; }
.diff .add { background: rgba(102,184,156,.25); }
.diff .del { background: rgba(221,127,114,.25); }
.thumbs { padding: 0 10px 8px; }
.thumb { max-width: 160px; max-height: 160px; border: 1px solid var(--line); border-radius: 8px; display: block; margin-top: 6px; }
.deploy-link { display: block; padding: 0 10px 8px; color: var(--ink); font-weight: 600; }

/* ---- 輸入框 ---- */
.composer { display: flex; gap: 8px; align-items: flex-end; padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); background: var(--paper); }
.composer textarea { flex: 1; resize: none; font: inherit; border: 1px solid var(--line); border-radius: 12px; padding: 8px 12px; background: var(--panel); color: var(--ink); max-height: 200px; }
.send-btn { background: var(--sun); border: 2px solid var(--ink); box-shadow: 2px 2px 0 var(--ink); border-radius: 10px; padding: 6px 14px; font-weight: 700; white-space: nowrap; }
.send-btn.stop { background: var(--rose); }
.send-btn:disabled { opacity: .5; box-shadow: none; cursor: default; }

/* ---- 面板（最小版，Task 11 會重做版面） ---- */
/* 關閉時移出畫面但保留版面尺寸（不能 display:none，否則預覽頁沒有大小、截圖會壞） */
.panel-head { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--line); }
.tabs { display: flex; gap: 6px; flex: 1; }
.tab { background: none; border: 1px solid transparent; border-radius: 8px; padding: 2px 12px; font-weight: 700; }
.tab.active { background: var(--sun); border-color: var(--ink); }
.panel-close { flex: none; background: none; border: 0; font-size: 18px; width: 36px; height: 36px; } /* 面板頭多了額度那一行，關閉鍵不能被擠窄 */
.panel-body { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.preview-tab { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.preview-bar { padding: 8px 12px; display: flex; justify-content: flex-end; }
.preview-box { flex: 1; min-height: 0; }
.host-frame { width: 100%; height: 100%; border: 0; display: block; }

.s2t-mark { display: inline-block; margin-left: 6px; padding: 0 6px; border: 1px solid var(--line, #ccc); border-radius: 8px; font-size: 11px; line-height: 16px; color: var(--muted, #888); vertical-align: middle; white-space: nowrap; }
.msg.assistant > .s2t-mark { display: table; margin: 4px 0 0; }

/* ---- 對話分頁、提示列（Task 7） ---- */
.panel-body > .preview-tab[aria-hidden="true"] { position: absolute; left: -10000px; top: 0; width: 100%; height: 100%; visibility: hidden; }
.notice { margin: 0 16px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel); font-size: 14px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.notice.warn { border-color: var(--sun, #e8b400); }
.notice.block { border-color: var(--rose, #d33); }
.sessions-tab { flex: 1; min-height: 0; overflow: auto; padding: 8px 12px 16px; }
.storage-note { margin: 0 0 10px; font-size: 13px; color: var(--muted, #666); }
.sessions-bar { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.new-session { background: var(--sun); border: 1px solid var(--ink); border-radius: 10px; padding: 4px 14px; font-weight: 700; }
.new-session:disabled { opacity: .5; }
.session-count { margin-left: auto; font-size: 13px; color: var(--muted, #666); }
.session-error { color: var(--rose, #d33); font-size: 14px; margin: 4px 0 8px; }
.session-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.session-row { display: flex; align-items: center; gap: 4px; border: 1px solid var(--line); border-radius: 10px; padding: 4px 6px; background: var(--panel); }
.session-row.current { border-color: var(--ink); background: var(--sun); }
.session-main { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: flex-start; text-align: left; background: none; border: 0; padding: 2px 4px; font: inherit; }
.session-title { font-weight: 700; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.session-time { font-size: 12px; color: var(--muted, #666); }
.session-edit { flex: 1; min-width: 0; font: inherit; padding: 4px 8px; border: 1px solid var(--line); border-radius: 8px; }
.session-act { background: none; border: 1px solid transparent; border-radius: 8px; min-width: 32px; height: 32px; padding: 0 6px; }
.session-act.danger { border-color: var(--rose, #d33); color: var(--rose, #d33); font-weight: 700; }
.session-target { font-size: 12px; color: var(--ink); max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sessions-fieldset { border: 0; margin: 0; padding: 0; min-width: 0; }

/* ---- 塗鴉板（Task 11 會統一重做面板樣式） ---- */
.sketch-tab { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.sketch-tab[hidden] { display: none; }
.sketch-scroll { flex: 1; min-height: 0; overflow: auto; padding: 8px; background: var(--paper); }
.sketch-wrap { position: relative; width: 100%; aspect-ratio: 3 / 4; background: #fff; border: 1px solid var(--line); box-shadow: var(--soft); }
.sketch-canvas { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; cursor: crosshair; }
.sketch-wrap.tool-text .sketch-canvas { cursor: text; }
.sk-box { position: absolute; display: flex; flex-direction: column; pointer-events: none; border: 1px dashed transparent; }
.tool-text .sk-box { pointer-events: auto; border-color: var(--line); }
.sk-box.sel { border-color: var(--sky); }
.sk-handle { display: none; align-items: center; justify-content: space-between; height: 20px; background: var(--paper); font-size: 12px; cursor: move; touch-action: none; padding: 0 4px; }
.tool-text .sk-handle { display: flex; }
.sk-del { background: none; border: 0; font-size: 12px; cursor: pointer; }
.sk-text { flex: 1; min-height: 0; width: 100%; resize: none; border: 0; background: transparent; padding: 2px 6px; font-family: sans-serif; line-height: 1.3; overflow: hidden; outline: none; }
.sk-resize { display: none; position: absolute; right: 0; bottom: 0; width: 18px; height: 18px; cursor: nwse-resize; touch-action: none; background: linear-gradient(135deg, transparent 50%, var(--sky) 50%); }
.tool-text .sk-resize { display: block; }
.sketch-toolbar { display: flex; flex-wrap: wrap; gap: 8px 14px; padding: 8px 12px calc(8px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); background: var(--panel); }
.sk-group { display: flex; gap: 4px; align-items: center; }
.sk-btn { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 4px 10px; min-height: 36px; }
.sk-btn.on { background: var(--sun); border-color: var(--ink); }
.sk-btn.warn { background: var(--rose); border-color: var(--ink); }
.sk-btn:disabled, .sk-color:disabled { opacity: .5; }
.sk-color { width: 32px; height: 32px; border-radius: 50%; border: 2px solid var(--line); }
.sk-color.on { border-color: var(--ink); box-shadow: 2px 2px 0 var(--ink); }
.sketch-msg { margin: 0; padding: 6px 12px; font-size: 14px; color: var(--ink); background: var(--rose); }
.attach-btn { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 6px 10px; white-space: nowrap; }
.attach-btn.on { background: var(--sky); border-color: var(--ink); }
.attach-hint { font-size: 13px; color: var(--muted); align-self: center; }

/* ---- 上傳／下載（Task 10） ---- */
.deploy-bar { padding: 0 12px 8px; display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.deploy-btn.confirm { background: var(--sun); }
.deploy-btn:disabled { opacity: 0.6; cursor: progress; }
.deploy-result { flex-basis: 100%; margin: 0; font-size: 13px; overflow-wrap: anywhere; white-space: pre-wrap; }
.deploy-note { margin: 0; font-size: 13px; color: var(--muted); flex: 1 1 12em; }
.deploy-result.error { color: #b3261e; }

/* ---- 版面（Task 11）：手機＝底部抽屜；桌機（≥ 900px）＝右欄 ---- */
body { overflow: hidden; }
.panel { position: fixed; z-index: 10; background: var(--panel); border: 1px solid var(--line); box-shadow: var(--soft); display: flex; flex-direction: column; min-height: 0; }
/* 關閉時移出畫面但保留版面尺寸（不能 display:none，否則預覽頁沒有大小、Service Worker 也會停） */
.panel.closed { left: -10000px; right: auto; visibility: hidden; pointer-events: none; }
.panel.mode-drawer { left: 0; right: 0; bottom: 0; top: auto; width: 100%; height: 66dvh; border-radius: 16px 16px 0 0; border-bottom: 0; transition: height .18s ease; }
.panel.mode-drawer.full { height: 100dvh; border-radius: 0; }
.panel.mode-drawer.closed { left: -10000px; right: auto; width: 100vw; }
.panel-scrim { position: fixed; inset: 0; z-index: 9; background: rgba(23,32,51,.28); }
.panel-handle { display: flex; justify-content: center; padding: 8px 0 2px; touch-action: none; cursor: grab; }
.panel-handle span { width: 40px; height: 5px; border-radius: 3px; background: var(--line); }
.panel.mode-column { top: 0; bottom: 0; right: 0; width: min(46%, 640px); }
.panel-body { position: relative; }
.panel-head { flex: none; }
.tab { white-space: nowrap; color: var(--ink); text-decoration: none; display: inline-flex; align-items: center; background: none; border: 1px solid var(--line); border-radius: 8px; padding: 2px 12px; font-weight: 700; min-height: 32px; }
.tabs .tab { border-color: transparent; }
.tabs .tab.active, .preview-bar .tab.active { background: var(--sun); border-color: var(--ink); }
.preview-bar { flex: none; flex-wrap: wrap; gap: 6px; justify-content: flex-start; align-items: center; }
.preview-bar .seg { display: flex; gap: 4px; }
.icon-btn { min-width: 36px; justify-content: center; padding: 2px 8px; }
.reset-db.confirm { background: var(--rose); border-color: var(--ink); }
.preview-box { border-top: 1px solid var(--line); background: #eee; }
@media (min-width: 900px) {
  #root { display: flex; }
  #root > .app { flex: 1; max-width: none; margin: 0; }
  #root > .gate { flex: 1; }
  /* 桌機：面板不再浮在上面，而是排在對話右邊 */
  .panel.mode-column.open { position: static; flex: none; height: 100%; border-top: 0; border-bottom: 0; border-right: 0; box-shadow: none; }
}

/* ---- 檔案分頁 ---- */
.files-tab { flex: 1; min-height: 0; overflow: auto; padding: 8px 12px 16px; }
.files-tab.drag { outline: 2px dashed var(--sky); outline-offset: -6px; background: rgba(110,159,214,.08); }
.files-bar { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.files-usage { font-size: 13px; color: var(--muted); flex: 1; }
.files-msg { margin: 0 0 8px; padding: 6px 10px; border-radius: 8px; background: rgba(102,184,156,.2); font-size: 14px; overflow-wrap: anywhere; }
.files-msg.error { background: rgba(221,127,114,.25); }
.files-empty { color: var(--muted); font-size: 14px; }
.files-list { list-style: none; margin: 0; padding: 0; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.file-row { display: flex; align-items: center; gap: 8px; padding: 4px 8px; border-top: 1px solid var(--line); font-size: 14px; }
.file-row:first-child { border-top: 0; }
.file-row.dir { color: var(--muted); font-weight: 600; }
.file-name { flex: 1; min-width: 0; background: none; border: 0; padding: 4px 0; text-align: left; font-family: var(--mono); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-name:hover { text-decoration: underline; }
.file-size { color: var(--muted); font-size: 12px; white-space: nowrap; }

/* ---- 歡迎畫面、pyodide 進度 ---- */
.welcome { flex: 0 1 auto; min-height: 0; overflow-y: auto; padding: 20px 16px 4px; }
.welcome-title { margin: 0 0 4px; font-size: 22px; line-height: 1.3; overflow-wrap: anywhere; }
.welcome-sub { margin: 0 0 12px; color: var(--muted); font-size: 14px; }
.welcome-cards { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }
.welcome-card { text-align: left; background: var(--panel); border: 2px solid var(--ink); box-shadow: 2px 2px 0 var(--ink); border-radius: 12px; padding: 12px 14px; font-weight: 700; }
.welcome-card:hover { background: var(--sun); }
.welcome + .transcript { padding-top: 0; }
.py-load { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin: 0 16px 6px; font-size: 13px; color: var(--muted); }
.py-load.error { color: var(--rose); font-weight: 600; }
.py-bar { flex: none; width: 120px; height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; }
.py-bar span { display: block; width: 40%; height: 100%; background: var(--sky); border-radius: 3px; animation: slide 1.2s ease-in-out infinite; }
@keyframes slide { 0% { margin-left: -40%; } 100% { margin-left: 100%; } }

/* ---- 底欄（桌機）與額度 ---- */
.footer { flex: none; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 4px 16px; border-top: 1px solid var(--line); font-size: 12px; color: var(--muted); }
.footer-model { font-family: var(--mono); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.quota { font-size: 12px; color: var(--muted); white-space: nowrap; border-radius: 6px; }
.quota-reset { margin-left: 8px; }
.quota-warn { background: var(--sun); color: var(--ink); padding: 0 6px; font-weight: 600; }
.quota-empty { background: var(--rose); color: var(--ink); padding: 0 6px; font-weight: 700; }
.panel-head .quota { flex: 0 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; }

/* ---- 不橫向溢出 ---- */
.transcript { overflow-x: hidden; min-width: 0; }
.tool-card, .tool-body, .msg { min-width: 0; max-width: 100%; }
.tool-body pre, .code-block pre { max-width: 100%; }

/* 手機：輸入框獨佔一行，附件與送出鍵排在下面 */
@media (max-width: 599px) {
  .header { gap: 6px; padding: 8px 12px; }
  .handle { display: none; }
  .composer { flex-wrap: wrap; }
  .composer textarea { flex: 1 1 100%; }
  .composer .send-btn { margin-left: auto; }
}

/* ---- 手機觸控目標：至少 44x44 px（視覺大小不變的用 padding／min-size 撐開） ---- */
@media (max-width: 899px) {
  .panel-handle { min-height: 44px; width: 100%; align-items: center; padding: 0; }
  .tab, .sk-btn, .session-act { min-height: 44px; }
  .tab.icon-btn { min-width: 44px; }
  .panel-close { width: 44px; height: 44px; }
  .gear { width: 44px; height: 44px; }
  .logout-btn, button.target-badge { min-height: 44px; }
  .file-row { min-height: 44px; }
  .file-name { min-height: 44px; }
  /* 確定上傳、繼續、重試、改在這裡開、工具卡片標題、送出 */
  .open-btn, .tool-head, .send-btn { min-height: 44px; }
}

.notice.info { white-space: pre-line; }
.notice.error { border-color: var(--rose, #d33); }
.err-countdown { font-variant-numeric: tabular-nums; color: var(--muted, inherit); }
