:root {
  --primary-color: #1e56a0;
  --primary-hover: #163f77;
  --bg-color: #f4f6f9;
  --sidebar-width: 260px;
  --border-color: #d1d5db;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

body {
  display: flex;
  height: 100dvh;
  background-color: var(--bg-color);
  overflow: hidden;
}

/* 側邊欄 (Sidebar) */
.sidebar {
  width: var(--sidebar-width);
  background-color: #ffffff;
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  transition: transform 0.3s ease;
  z-index: 100;
}

.sidebar-header {
  padding: 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #f0f0f0;
  font-weight: bold;
  color: #333;
}

.btn-new-chat {
  background-color: var(--primary-color);
  color: white;
  border: none;
  border-radius: 6px;
  width: 32px;
  height: 32px;
  cursor: pointer;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.session-list {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
}

.session-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  margin-bottom: 6px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  color: #4b5563;
  border: 1px solid transparent;
}

.session-item:hover {
  background-color: #f3f4f6;
}

.session-item.active {
  background-color: #e0f2fe;
  border-color: #93c5fd;
  color: var(--primary-color);
  font-weight: 600;
}

.btn-delete-session {
  background: none;
  border: none;
  color: #9ca3af;
  cursor: pointer;
  font-size: 16px;
  padding: 2px 6px;
}

.btn-delete-session:hover {
  color: #ef4444;
}

.sidebar-footer {
  padding: 12px 15px;
  border-top: 1px solid var(--border-color);
  background-color: #ffffff;
  display: flex;
  gap: 8px;
}

.btn-nav {
  flex: 1;
  padding: 8px;
  background-color: #f3f4f6;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  color: #374151;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  font-size: 13px;
  text-decoration: none;
}

.btn-nav:hover {
  background-color: #e5e7eb;
}

/* 主區域 (Main Content) */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  position: relative;
  background-color: #ffffff;
  height: 100dvh;
}

.header {
  height: 56px;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.hamburger {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #4b5563;
}

.app-title {
  font-size: 18px;
  font-weight: bold;
  color: var(--primary-color);
  display: flex;
  align-items: center;
  gap: 8px;
}

/* 狀態指示燈 */
.status-badge {
  padding: 4px 10px;
  border-radius: 12px;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 500;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.status-idle { background-color: #e6f4ea; color: #137333; }
.status-idle .status-dot { background-color: #10b981; }

.status-processing { background-color: #fef3c7; color: #b45309; }
.status-processing .status-dot { background-color: #f59e0b; }

.status-offline { background-color: #fde8e8; color: #c81e1e; }
.status-offline .status-dot { background-color: #ef4444; }

/* 對話視窗 */
.chat-container {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.message {
  max-width: 85%;
  padding: 12px 16px;
  border-radius: 16px;
  font-size: 15px;
  line-height: 1.6;
  word-break: break-word;
}

.message.assistant {
  align-self: flex-start;
  background-color: #f3f4f6;
  color: #1f2937;
  border-bottom-left-radius: 4px;
}

.message.user {
  align-self: flex-end;
  background-color: var(--primary-color);
  color: #ffffff;
  border-bottom-right-radius: 4px;
  white-space: pre-wrap;
}

.typing-cursor::after {
  content: '▌';
  display: inline-block;
  margin-left: 2px;
  animation: blink 0.8s infinite;
  color: var(--primary-color);
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

/* Markdown 樣式 */
.message.assistant p { margin-bottom: 0.5em; }
.message.assistant p:last-child { margin-bottom: 0; }
.message.assistant ul, .message.assistant ol { margin-left: 20px; margin-bottom: 0.5em; }
.message.assistant code {
  background-color: #e5e7eb;
  padding: 2px 4px;
  border-radius: 4px;
  font-family: monospace;
}
.message.assistant pre {
  background-color: #1f2937;
  color: #f9fafb;
  padding: 10px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 8px 0;
}

/* 輸入區塊 (含 Safe Area 防護) */
.input-wrapper {
  padding: 12px 20px 16px 20px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  background-color: #ffffff;
  border-top: 1px solid #f0f0f0;
  flex-shrink: 0;
}

.tools-bar {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  margin-bottom: 8px;
}

.toggle-container {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: #6b7280;
}

.switch {
  position: relative;
  display: inline-block;
  width: 36px;
  height: 20px;
}

.switch input { opacity: 0; width: 0; height: 0; }

.slider {
  position: absolute;
  cursor: pointer;
  top: 0; left: 0; right: 0; bottom: 0;
  background-color: #ccc;
  transition: .3s;
  border-radius: 20px;
}

.slider:before {
  position: absolute;
  content: "";
  height: 14px; width: 14px;
  left: 3px; bottom: 3px;
  background-color: white;
  transition: .3s;
  border-radius: 50%;
}

input:checked + .slider { background-color: var(--primary-color); }
input:checked + .slider:before { transform: translateX(16px); }

.input-box {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  border: 1px solid var(--border-color);
  border-radius: 20px;
  padding: 8px 12px;
}

.icon-btn {
  background: none;
  border: none;
  color: #9ca3af;
  cursor: not-allowed;
  padding: 6px;
}

.chat-textarea {
  flex: 1;
  border: none;
  outline: none;
  font-size: 15px;
  padding: 4px 0;
  resize: none;
  max-height: 120px;
  min-height: 24px;
  font-family: inherit;
}

.btn-send {
  background-color: var(--primary-color);
  color: white;
  border: none;
  border-radius: 16px;
  padding: 8px 18px;
  font-weight: 600;
  cursor: pointer;
}

.btn-send:disabled {
  background-color: #9ca3af;
  cursor: not-allowed;
}

.send-hint {
  font-size: 11px;
  color: #9ca3af;
  text-align: right;
  margin-top: 4px;
  margin-right: 8px;
}

/* 表單與 Modal 通用組件 (適用於 config.html) */
.card {
  background: #ffffff;
  border-radius: 12px;
  width: 100%;
  max-width: 600px;
  padding: 24px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.05);
  height: fit-content;
  margin: 20px auto;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--border-color);
  padding-bottom: 12px;
  margin-bottom: 20px;
}

.card-header h2 { font-size: 20px; color: #1f2937; }

.form-group { margin-bottom: 18px; }
.form-group label { display: block; font-size: 14px; font-weight: 600; color: #374151; margin-bottom: 6px; }
.form-control { width: 100%; padding: 10px 12px; border: 1px solid var(--border-color); border-radius: 8px; font-size: 14px; }
textarea.form-control { resize: vertical; min-height: 100px; }

.card-footer { display: flex; justify-content: space-between; margin-top: 24px; }
.btn { padding: 10px 20px; border-radius: 8px; font-weight: 600; cursor: pointer; text-decoration: none; border: none; font-size: 14px; }
.btn-secondary { background: #e5e7eb; color: #374151; }
.btn-primary { background: var(--primary-color); color: white; }

.toast {
  display: none;
  margin-top: 15px;
  padding: 10px;
  background: #def7ec;
  color: #03543f;
  border-radius: 6px;
  text-align: center;
  font-size: 14px;
}

/* 除錯日誌樣式 (適用於 log.html) */
.dark-theme {
  background-color: #0f172a;
  color: #f8fafc;
  min-height: 100dvh;
  padding: 20px;
}

.log-list { display: flex; flex-direction: column; gap: 12px; }
.log-item { background: #1e293b; border: 1px solid #334155; border-radius: 8px; padding: 12px; font-size: 12px; font-family: monospace; }
.log-item.REQUEST { border-left: 4px solid #38bdf8; }
.log-item.RESPONSE_SUCCESS { border-left: 4px solid #4ade80; }
.log-item.RESPONSE_ERROR { border-left: 4px solid #f87171; }

.log-header { display: flex; justify-content: space-between; margin-bottom: 6px; color: #94a3b8; }
.log-type.REQUEST { color: #38bdf8; font-weight: bold; }
.log-type.RESPONSE_SUCCESS { color: #4ade80; font-weight: bold; }
.log-type.RESPONSE_ERROR { color: #f87171; font-weight: bold; }

.log-item pre { background: #090d16; padding: 8px; border-radius: 4px; overflow-x: auto; color: #e2e8f0; margin-top: 6px; }

/* 響應式適應 (RWD) */
@media (max-width: 768px) {
  .sidebar {
    position: absolute;
    top: 0; bottom: 0; left: 0;
    transform: translateX(-100%);
  }
  .sidebar.open { transform: translateX(0); }
  .overlay {
    display: none;
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.3);
    z-index: 90;
  }
  .overlay.active { display: block; }
}
