:root {
  --bg: #f5f2ec;
  --panel: #fffdf9;
  --panel-2: #f0eae1;
  --line: #e2d9cc;
  --text: #2f2a24;
  --text-dim: #857c70;
  --accent: #b9754a;
  --accent-dark: #9a5c36;
  --accent-soft: #f2e3d6;
  --danger: #b0483c;
  --bubble-me: #b9754a;
  --bubble-me-text: #fffaf5;
  --bubble-you: #fffdf9;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(60, 45, 30, 0.07), 0 6px 18px rgba(60, 45, 30, 0.05);
}

* {
  box-sizing: border-box;
}

/* hidden 屬性必須勝過 .modal / .toast 的 display 設定 */
[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", -apple-system,
    "Segoe UI", sans-serif;
  font-size: 15px;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
}

.app {
  display: grid;
  grid-template-columns: minmax(0, 1.35fr) minmax(360px, 1fr);
  gap: 18px;
  height: 100vh;
  height: 100dvh;
  padding: 18px;
}

/* ---------- 對話框 ---------- */
.chat-panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, #fffdf9, #fbf7f1);
}

.chat-title {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.avatar {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent-dark);
  font-weight: 600;
  font-size: 16px;
}

.chat-title-text {
  min-width: 0;
}

.chat-title h1 {
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: 0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-stage {
  margin: 0;
  font-size: 12px;
  color: var(--text-dim);
}

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 20px 18px 8px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scroll-behavior: smooth;
}

.empty-hint {
  margin: auto;
  color: var(--text-dim);
  font-size: 14px;
  text-align: center;
}

.msg-row {
  display: flex;
  gap: 8px;
  max-width: 100%;
}

.msg-row.me {
  justify-content: flex-end;
}

.bubble {
  position: relative;
  max-width: min(78%, 560px);
  padding: 9px 14px;
  border-radius: 16px;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 15px;
  line-height: 1.65;
  border: 1px solid transparent;
  animation: pop 0.16s ease-out;
}

@keyframes pop {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

.msg-row.you .bubble {
  background: var(--bubble-you);
  border-color: var(--line);
  border-bottom-left-radius: 5px;
  box-shadow: 0 1px 2px rgba(60, 45, 30, 0.04);
}

.msg-row.me .bubble {
  background: var(--bubble-me);
  color: var(--bubble-me-text);
  border-bottom-right-radius: 5px;
}

.bubble-time {
  display: block;
  margin-top: 3px;
  font-size: 11px;
  color: var(--text-dim);
  opacity: 0.75;
}

.msg-row.me .bubble-time {
  color: #f7e6d8;
  text-align: right;
}

.typing .dot {
  display: inline-block;
  width: 6px;
  height: 6px;
  margin-right: 3px;
  border-radius: 50%;
  background: var(--text-dim);
  animation: blink 1.2s infinite;
}

.typing .dot:nth-child(2) {
  animation-delay: 0.2s;
}

.typing .dot:nth-child(3) {
  animation-delay: 0.4s;
  margin-right: 0;
}

@keyframes blink {
  0%, 60%, 100% {
    opacity: 0.28;
  }
  30% {
    opacity: 1;
  }
}

.composer {
  border-top: 1px solid var(--line);
  padding: 12px 18px 16px;
  background: var(--panel);
}

.composer-error {
  min-height: 0;
  color: var(--text-dim);
  font-size: 12.5px;
  padding: 0 2px;
}

.composer-error:not(:empty) {
  padding-bottom: 6px;
}

.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 10px;
}

#input-text {
  flex: 1;
  min-height: 44px;
  max-height: 160px;
  resize: none;
  padding: 11px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fff;
  color: var(--text);
  font: inherit;
  line-height: 1.5;
  transition: border-color 0.15s, box-shadow 0.15s;
}

#input-text:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

/* ---------- 按鈕 ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--text);
  font: inherit;
  font-size: 14px;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s, transform 0.06s, opacity 0.15s;
}

.btn:hover:not(:disabled) {
  background: var(--panel-2);
  border-color: #d6caba;
}

.btn:active:not(:disabled) {
  transform: translateY(1px);
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fffaf5;
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-dark);
  border-color: var(--accent-dark);
}

.btn-danger {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}

.btn-danger:hover:not(:disabled) {
  background: #9a3b31;
  border-color: #9a3b31;
}

.btn-ghost {
  background: transparent;
  border-color: transparent;
  color: var(--text-dim);
}

.btn-ghost:hover:not(:disabled) {
  background: var(--panel-2);
  border-color: transparent;
  color: var(--text);
}

.btn-sm {
  padding: 6px 11px;
  font-size: 13px;
  border-radius: 8px;
}

.send-btn {
  min-height: 44px;
  padding: 0 22px;
}

/* ---------- 右側面板 ---------- */
.side-panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.tabs {
  display: flex;
  gap: 2px;
  padding: 8px 8px 0;
  border-bottom: 1px solid var(--line);
  background: #fbf7f1;
  overflow-x: auto;
}

.tab {
  flex: 1 0 auto;
  padding: 11px 12px 12px;
  border: none;
  border-bottom: 2px solid transparent;
  background: none;
  color: var(--text-dim);
  font: inherit;
  font-size: 14px;
  cursor: pointer;
  white-space: nowrap;
  transition: color 0.15s, border-color 0.15s;
}

.tab:hover {
  color: var(--text);
}

.tab.active {
  color: var(--accent-dark);
  border-bottom-color: var(--accent);
  font-weight: 600;
}

.tab-panels {
  flex: 1;
  min-height: 0;
  display: flex;
}

.tab-panel {
  display: none;
  flex: 1;
  min-height: 0;
}

.tab-panel.active {
  display: flex;
}

.panel-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 18px;
}

/* ---------- 表單 ---------- */
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 14px;
  border: none;
  padding: 0;
}

.field-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-dim);
  padding: 0;
}

.field-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0 14px;
}

input[type="text"],
input[type="url"],
textarea {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--text);
  font: inherit;
  font-size: 14.5px;
  line-height: 1.6;
  transition: border-color 0.15s, box-shadow 0.15s;
}

textarea {
  resize: vertical;
  min-height: 60px;
}

input:focus,
textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.radio-row {
  display: flex;
  gap: 18px;
  flex-wrap: wrap;
}

.radio {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 14px;
  cursor: pointer;
}

.radio input {
  accent-color: var(--accent);
  width: 16px;
  height: 16px;
  cursor: pointer;
}

.panel-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 6px;
}

.status {
  font-size: 13px;
  color: var(--text-dim);
}

.status.ok {
  color: #4a7a4a;
}

.status.err {
  color: var(--danger);
}

.divider {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 20px 0 14px;
  color: var(--text-dim);
  font-size: 12.5px;
}

.divider::before,
.divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--line);
}

.inline-row {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}

.inline-row input {
  flex: 1;
  min-width: 0;
}

.hint {
  margin: 0 0 14px;
  color: var(--text-dim);
  font-size: 12.5px;
  line-height: 1.6;
}

.list-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin: 20px 0 10px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}

.list-header h2 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-dim);
  letter-spacing: 0.02em;
}

.count {
  font-size: 12.5px;
  color: var(--text-dim);
}

/* ---------- 清單項目 ---------- */
.item-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 9px;
}

.item {
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: #fff;
}

.item.disabled {
  opacity: 0.55;
}

.item-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.item-title {
  font-weight: 600;
  font-size: 14.5px;
  word-break: break-word;
}

.item-meta {
  margin-top: 3px;
  font-size: 12px;
  color: var(--text-dim);
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.badge {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent-dark);
  font-size: 11.5px;
  font-weight: 600;
}

.badge.muted {
  background: var(--panel-2);
  color: var(--text-dim);
}

.badge.good {
  background: #e3efe0;
  color: #4a7a4a;
}

.item-body {
  margin-top: 8px;
  font-size: 13.5px;
  color: var(--text);
  word-break: break-word;
}

.item-actions {
  display: flex;
  gap: 6px;
  margin-top: 10px;
  flex-wrap: wrap;
}

.item.is-pending {
  border-color: #d9c7a8;
  background: #fffbf2;
}

.item.is-forgotten .item-title,
.item.is-forgotten .item-body {
  opacity: 0.65;
}

.memory-group {
  margin-bottom: 20px;
}

.memory-group h3 {
  margin: 0 0 9px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-dim);
  letter-spacing: 0.03em;
}

.empty {
  padding: 22px 14px;
  border: 1px dashed var(--line);
  border-radius: 11px;
  color: var(--text-dim);
  font-size: 13.5px;
  text-align: center;
}

/* ---------- toast / modal ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translateX(-50%);
  max-width: min(90vw, 480px);
  padding: 11px 18px;
  border-radius: 11px;
  background: #332d26;
  color: #fffaf5;
  font-size: 14px;
  box-shadow: 0 8px 28px rgba(40, 30, 20, 0.24);
  z-index: 60;
  animation: toast-in 0.2s ease-out;
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translate(-50%, 8px);
  }
  to {
    opacity: 1;
    transform: translate(-50%, 0);
  }
}

.modal {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(40, 32, 24, 0.4);
  z-index: 70;
}

.modal-box {
  width: min(420px, 100%);
  padding: 22px;
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: 0 18px 50px rgba(40, 30, 20, 0.28);
}

.modal-text {
  margin: 0 0 18px;
  font-size: 15px;
  line-height: 1.7;
  white-space: pre-wrap;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 9px;
}

/* ---------- 手機版 ---------- */
@media (max-width: 820px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1.15fr) minmax(0, 1fr);
    height: auto;
    min-height: 100dvh;
    padding: 10px;
    gap: 10px;
  }

  .chat-panel {
    min-height: 58vh;
  }

  .side-panel {
    min-height: 62vh;
  }

  .field-grid {
    grid-template-columns: 1fr;
  }

  .bubble {
    max-width: 86%;
  }
}

@media (max-width: 480px) {
  body {
    font-size: 14.5px;
  }

  .app {
    padding: 0;
    gap: 0;
  }

  .chat-panel,
  .side-panel {
    border-radius: 0;
    border-left: none;
    border-right: none;
  }

  .chat-header,
  .composer,
  .panel-scroll,
  .messages {
    padding-left: 13px;
    padding-right: 13px;
  }

  .send-btn {
    padding: 0 15px;
  }
}
