:root {
  --bg: #0f1115;
  --panel: #171a21;
  --border: #262b36;
  --text: #e6e8ec;
  --muted: #9aa3b2;
  --user: #2563eb;
  --assistant: #1f2530;
  --accent: #ec4899;
  --panel-w: 340px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", "PingFang TC", "Microsoft JhengHei", sans-serif;
}

body { height: 100vh; overflow: hidden; }

.layout {
  display: flex;
  height: 100vh;
}

/* ===== 故事設定面板 ===== */
.panel {
  width: var(--panel-w);
  flex: 0 0 var(--panel-w);
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: margin-left 0.2s ease;
}
.panel.collapsed { margin-left: calc(-1 * var(--panel-w)); }

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.panel-head h2 { font-size: 15px; margin: 0; font-weight: 600; }

.panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 14px 16px 24px;
}

.field { margin-bottom: 18px; }
.field > label,
.group-title {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin: 10px 0 5px;
  font-weight: 500;
}
.group-title {
  font-size: 13px;
  color: var(--text);
  font-weight: 600;
  border-bottom: 1px solid var(--border);
  padding-bottom: 6px;
  margin-top: 4px;
}
.field > label:first-child { margin-top: 0; }

.panel textarea,
.panel input[type="text"],
.panel select {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
  font-family: inherit;
  line-height: 1.45;
  resize: vertical;
}
.panel textarea:focus,
.panel input:focus,
.panel select:focus { outline: none; border-color: var(--accent); }

/* 角色卡片 */
.char-card {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  margin-bottom: 10px;
  background: rgba(255, 255, 255, 0.02);
  display: flex;
  flex-direction: column;
  gap: 7px;
}
.char-card-head { display: flex; gap: 8px; align-items: center; }
.char-card-head .char-name { flex: 1; font-weight: 600; }

.add-btn {
  width: 100%;
  background: transparent;
  border: 1px dashed var(--border);
  color: var(--muted);
  padding: 9px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
}
.add-btn:hover { color: var(--accent); border-color: var(--accent); }

.del-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  width: 28px;
  height: 28px;
  border-radius: 7px;
  cursor: pointer;
  flex: 0 0 auto;
}
.del-btn:hover { color: #fca5a5; border-color: #7f1d2e; }

/* ===== 右側主區 ===== */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.topbar h1 { font-size: 16px; margin: 0; font-weight: 600; flex: 1; }

.icon-btn,
.topbar button {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 6px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
}
.icon-btn:hover,
.topbar button:hover { color: var(--text); border-color: var(--accent); }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: 100%;
  max-width: 860px;
  margin: 0 auto;
}

.row { display: flex; }
.row.user { justify-content: flex-end; }
.row.assistant { justify-content: flex-start; }

.bubble {
  max-width: 88%;
  padding: 11px 14px;
  border-radius: 16px;
  line-height: 1.65;
  white-space: pre-wrap;
  word-wrap: break-word;
  font-size: 15px;
}
.row.user .bubble {
  background: var(--user);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.row.assistant .bubble {
  background: var(--assistant);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.bubble.error {
  background: #3b1d22;
  border-color: #7f1d2e;
  color: #fca5a5;
}

.bubble .cursor {
  display: inline-block;
  width: 7px;
  height: 1em;
  background: var(--accent);
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: blink 1s steps(2, start) infinite;
}
@keyframes blink { to { visibility: hidden; } }

.composer {
  display: flex;
  gap: 10px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  background: var(--panel);
  width: 100%;
  max-width: 860px;
  margin: 0 auto;
}

#input {
  flex: 1;
  resize: none;
  max-height: 180px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 15px;
  font-family: inherit;
  line-height: 1.4;
}
#input:focus { outline: none; border-color: var(--accent); }

#send {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 0 20px;
  font-size: 15px;
  cursor: pointer;
  font-weight: 600;
}
#send:disabled { opacity: 0.5; cursor: not-allowed; }

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

/* 手機版：面板變抽屜，預設收合 */
@media (max-width: 720px) {
  .panel {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 30;
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.5);
  }
  .messages, .composer { max-width: 100%; }
}
