/* =============================================
   V9 TAVERN — Flat Design Theme
   ============================================= */

:root {
  --bg: #1a1a2e;
  --bg-sidebar: #16162a;
  --bg-input: #1e1e38;
  --bg-hover: #24244a;
  --bg-active: #2a2a55;
  --border: #2e2e52;
  --accent: #c9a84c;
  --accent-dim: #9a7e38;
  --danger: #d45;
  --text: #ddd;
  --text-2: #999;
  --text-3: #666;
  --font: 'Inter', sans-serif;
  --font-story: 'Noto Serif SC', Georgia, serif;
  --radius: 6px;
}

*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}

::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* ─── Layout ─── */
#app {
  display: flex;
  height: 100vh;
}

/* ─── Sidebar ─── */
#sidebar {
  width: 240px;
  flex-shrink: 0;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

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

.sidebar-header h1 {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 0.04em;
}

.accent { color: var(--accent); }

.sidebar-section {
  border-bottom: 1px solid var(--border);
  overflow-y: auto;
  flex-shrink: 0;
  max-height: 35vh;
}

.sidebar-section:last-child {
  flex: 1;
  max-height: none;
}

.section-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 14px;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-2);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  position: sticky;
  top: 0;
  background: var(--bg-sidebar);
  z-index: 1;
}

/* Sidebar Items */
.sb-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 14px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text);
  border-left: 2px solid transparent;
  transition: background 0.15s, border-color 0.15s;
}

.sb-item:hover { background: var(--bg-hover); }
.sb-item.active { background: var(--bg-active); border-left-color: var(--accent); }

.chat-del { opacity: 0; font-size: 14px; transition: opacity 0.15s; }
.sb-item:hover .chat-del { opacity: 0.6; }
.sb-item:hover .chat-del:hover { opacity: 1; color: var(--danger); }

.sb-item-label {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sb-item-sub {
  font-size: 10px;
  color: var(--text-3);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sb-item-icon {
  font-size: 16px;
  flex-shrink: 0;
  width: 22px;
  text-align: center;
}

.sb-item-detail {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
}

.sb-badge {
  font-size: 9px;
  padding: 0 5px;
  border-radius: 3px;
  color: var(--text-3);
  border: 1px solid var(--border);
  flex-shrink: 0;
}

.sb-badge.locked { color: var(--accent); border-color: var(--accent-dim); }

.pipe-key-badge {
  display: inline-block;
  font-size: 10px;
  font-family: monospace;
  font-weight: 700;
  color: var(--accent);
  background: rgba(201,168,76,0.1);
  padding: 0 5px;
  border-radius: 3px;
  margin-right: 4px;
  vertical-align: middle;
}

.form-hint {
  font-size: 11px;
  color: var(--text-3);
  margin-top: 4px;
  line-height: 1.4;
}

/* Avatar Upload */
.avatar-upload {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  border: 1px dashed var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  overflow: hidden;
  margin: 0 auto 10px;
  position: relative;
  transition: border-color 0.15s;
}

.avatar-upload:hover { border-color: var(--accent); }

.avatar-upload img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: none;
}

.avatar-upload img.has-src { display: block; }

.avatar-upload span {
  font-size: 10px;
  color: var(--text-3);
  text-align: center;
  padding: 4px;
  line-height: 1.3;
}

.avatar-upload img.has-src ~ span { display: none; }

/* Sidebar avatar */
.sb-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  flex-shrink: 0;
  object-fit: cover;
}

.sb-avatar-placeholder {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
}

/* Chat dialogue avatar */
.dialogue-row {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
  align-items: flex-start;
}

.dialogue-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  flex-shrink: 0;
  object-fit: cover;
  margin-top: 2px;
}

.dialogue-avatar-placeholder {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: 700;
  margin-top: 2px;
}

.dialogue-body { flex: 1; min-width: 0; }

/* ─── Chat Main ─── */
#chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  position: relative;
}

#chat-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 20px 24px;
  cursor: default;
}

#chat-content {
  max-width: 680px;
  margin: 0 auto;
}

/* ─── Chat Bottom ─── */
#chat-bottom {
  flex-shrink: 0;
  border-top: 1px solid var(--border);
  background: var(--bg-sidebar);
  padding: 12px 24px 16px;
}

#choices-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 10px;
  max-width: 680px;
  margin-left: auto;
  margin-right: auto;
}

#choices-bar:empty { display: none; margin-bottom: 0; }

.choice-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 6px 14px;
  border-radius: var(--radius);
  font: 13px var(--font);
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s, background 0.15s;
}

.choice-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: rgba(201,168,76,0.06);
}

.choice-btn:active { transform: scale(0.97); }

#input-bar {
  display: flex;
  gap: 8px;
  max-width: 680px;
  margin: 0 auto;
}

#player-input {
  flex: 1;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 9px 12px;
  color: var(--text);
  font: 13px var(--font);
  outline: none;
  transition: border-color 0.15s;
}

#player-input:focus { border-color: var(--accent); }
#player-input::placeholder { color: var(--text-3); }

#btn-send {
  width: 40px;
  background: var(--accent);
  color: #000;
  border: none;
  border-radius: var(--radius);
  font-size: 15px;
  cursor: pointer;
  transition: background 0.15s;
  flex-shrink: 0;
}

#btn-send:hover { background: var(--accent-dim); }
#btn-send:disabled { opacity: 0.3; cursor: not-allowed; }

/* ─── Narrative Elements ─── */
.turn-divider {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 20px 0 12px;
  font-size: 10px;
  color: var(--text-3);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.turn-divider::before,
.turn-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

.narration {
  font: 15px/1.9 var(--font-story);
  color: #ccc;
  margin-bottom: 10px;
  white-space: pre-line;
}

.dialogue {
  padding-left: 10px;
  border-left: 2px solid var(--accent);
  flex: 1;
  min-width: 0;
}

.dialogue[data-ci="1"] { border-left-color: #5ba8d0; }
.dialogue[data-ci="2"] { border-left-color: #d06878; }
.dialogue[data-ci="3"] { border-left-color: #60c888; }

.dialogue-name {
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
}

.dialogue-name[data-ci="1"] { color: #5ba8d0; }
.dialogue-name[data-ci="2"] { color: #d06878; }
.dialogue-name[data-ci="3"] { color: #60c888; }

.dialogue-emotion {
  font-size: 10px;
  color: var(--text-3);
  font-style: italic;
  margin-left: 4px;
  font-weight: 400;
}

.dialogue-text {
  font: 14px/1.7 var(--font-story);
  color: var(--text);
}

.dialogue-text::before { content: '「'; color: var(--text-3); }
.dialogue-text::after { content: '」'; color: var(--text-3); }

.player-action {
  font-size: 13px;
  color: var(--accent);
  padding: 6px 10px;
  background: rgba(201,168,76,0.06);
  border-radius: var(--radius);
  border-left: 2px solid var(--accent-dim);
  margin-bottom: 10px;
}

.player-action::before { content: '▸ '; }

/* Player dialogue row (WeChat style, right-aligned) */
.player-row {
  justify-content: flex-end;
}

.player-dialogue {
  border-left: none !important;
  border-right: 2px solid #5a8fd0;
  padding-left: 0;
  padding-right: 10px;
  text-align: right;
  flex: none;
  max-width: 75%;
}

.player-name {
  color: #5a8fd0 !important;
}

.player-dialogue .dialogue-text::before { content: none; }
.player-dialogue .dialogue-text::after { content: none; }

.typewriter-cursor {
  display: inline-block;
  width: 2px; height: 1em;
  background: var(--accent);
  margin-left: 1px;
  animation: blink 0.7s infinite;
  vertical-align: text-bottom;
}

/* ─── Loading ─── */
#loading {
  position: absolute;
  bottom: 100px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 20;
}

.dots {
  display: flex;
  gap: 5px;
  padding: 8px 16px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 16px;
}

.dots i {
  width: 7px; height: 7px;
  background: var(--accent);
  border-radius: 50%;
  animation: pulse 1s ease infinite;
  font-style: normal;
}

.dots i:nth-child(2) { animation-delay: 0.15s; }
.dots i:nth-child(3) { animation-delay: 0.3s; }

/* ─── Overlay & Modal ─── */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 90;
}

.hidden { display: none !important; }

.modal {
  position: fixed;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: 440px;
  max-width: 92vw;
  max-height: 85vh;
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  z-index: 100;
  display: flex;
  flex-direction: column;
  animation: fadeUp 0.15s ease;
}

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

.modal-header h3 { font-size: 14px; font-weight: 600; }

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

.modal-footer {
  display: flex;
  justify-content: space-between;
  padding: 10px 16px;
  border-top: 1px solid var(--border);
}

/* ─── Form Elements ─── */
.form-section {
  margin-bottom: 16px;
}

.form-section h4 {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-2);
  margin-bottom: 8px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

label {
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-size: 11px;
  color: var(--text-2);
  margin-bottom: 8px;
}

input[type="text"],
input[type="password"],
input[type="number"],
textarea {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 7px 10px;
  color: var(--text);
  font: 13px var(--font);
  outline: none;
  transition: border-color 0.15s;
  width: 100%;
}

input:focus, textarea:focus { border-color: var(--accent); }

textarea {
  resize: vertical;
  min-height: 60px;
  font-family: var(--font);
}

input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

.form-row {
  display: flex;
  gap: 8px;
}

.form-row .half { flex: 1; }

.form-row-between {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
  margin-bottom: 8px;
}

/* Toggle */
.toggle {
  position: relative;
  display: inline-block;
  width: 36px; height: 20px;
  flex-shrink: 0;
  margin: 0;
}

.toggle input { display: none; }

.toggle-track {
  position: absolute;
  inset: 0;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 20px;
  cursor: pointer;
  transition: 0.2s;
}

.toggle-track::before {
  content: '';
  position: absolute;
  width: 14px; height: 14px;
  left: 2px; top: 2px;
  background: var(--text-3);
  border-radius: 50%;
  transition: 0.2s;
}

.toggle input:checked + .toggle-track {
  background: var(--accent);
  border-color: var(--accent);
}

.toggle input:checked + .toggle-track::before {
  transform: translateX(16px);
  background: #fff;
}

/* Buttons */
.btn-icon {
  background: none;
  border: none;
  color: var(--text-2);
  font-size: 16px;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 4px;
  transition: color 0.15s;
}

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

.btn-text {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 13px;
  cursor: pointer;
  padding: 0 4px;
}

.btn-text:hover { color: var(--accent-dim); }

.btn-primary {
  background: var(--accent);
  color: #000;
  border: none;
  padding: 6px 18px;
  border-radius: 4px;
  font: 600 13px var(--font);
  cursor: pointer;
}

.btn-primary:hover { background: var(--accent-dim); }

.btn-danger {
  background: none;
  color: var(--danger);
  border: 1px solid var(--danger);
  padding: 5px 14px;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
}

.btn-danger:hover { background: rgba(221,68,85,0.1); }

/* Char tag row in editor */
.tag-row {
  display: flex;
  gap: 6px;
  margin-bottom: 6px;
  align-items: center;
}

.tag-row input {
  margin-bottom: 0;
}

.tag-row input:first-child { width: 80px; flex-shrink: 0; }
.tag-row input:nth-child(2) { flex: 1; }

.tag-row .btn-icon { font-size: 14px; }

/* ─── Toast ─── */
.toast {
  position: fixed;
  top: 16px; left: 50%;
  transform: translateX(-50%);
  padding: 8px 20px;
  border-radius: var(--radius);
  font-size: 13px;
  z-index: 200;
  animation: fadeUp 0.2s ease;
  color: #fff;
}

.toast.ok { background: #2a7c4f; }
.toast.err { background: var(--danger); }

/* ─── Animations ─── */
@keyframes fadeUp {
  from { opacity: 0; transform: translate(-50%, -46%); }
  to { opacity: 1; transform: translate(-50%, -50%); }
}

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

@keyframes pulse {
  0%, 100% { opacity: 0.25; transform: scale(0.8); }
  50% { opacity: 1; transform: scale(1.15); }
}

/* ─── Delete-from button ─── */
.btn-del-from {
  display: none;
  background: none;
  border: none;
  color: var(--danger);
  font-size: 11px;
  cursor: pointer;
  padding: 2px 5px;
  border-radius: 3px;
  opacity: 0.5;
  align-self: center;
  flex-shrink: 0;
  transition: opacity 0.15s;
}

.player-row:hover .btn-del-from { display: block; }
.btn-del-from:hover { opacity: 1; background: rgba(221,68,85,0.1); }

/* ─── Export/Import buttons ─── */
.header-backup-btns {
  display: flex;
  gap: 2px;
  align-items: center;
}

.header-backup-btns .btn-icon {
  font-size: 14px;
  padding: 2px 4px;
  opacity: 0.7;
}

.header-backup-btns .btn-icon:hover { opacity: 1; }

.section-title-actions {
  display: flex;
  align-items: center;
  gap: 2px;
}

/* ─── Mobile ─── */
@media (max-width: 640px) {
  #sidebar { width: 56px; }
  .sb-item-detail, .section-title span, .sb-badge { display: none; }
  .sb-item { justify-content: center; padding: 10px 0; }
  .sb-item-icon { width: auto; }
  #chat-scroll { padding: 16px; }
  #chat-bottom { padding: 10px 16px 14px; }
}
