:root {
  --primary: #6f52b5;
  --danger: #dc2626;
  --bg: #f5f7fa;
  --card-bg: #ffffff;
  --border: #e0e0e0;
  --text: #1f2937;
  --muted: #6b7280;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 8px;
  background: linear-gradient(110deg, #5b3f98, #8e6ac8);
  color: white;
  padding: 12px 14px;
  transition: background 0.35s ease;
}
.brand-mark { display:flex; align-items:center; gap:5px; font-weight:800; letter-spacing:.02em; }
.brand-icon { width:28px; height:28px; border-radius:7px; flex:none; }
.topbar h1 { font-size: 17px; margin: 0; flex: 1; }
.topbar-lang { width:auto; min-width:78px; padding:6px 8px !important; border:1px solid rgba(255,255,255,.45) !important; background:rgba(255,255,255,.15) !important; color:#fff; font-size:12px !important; }
.topnav { display:flex; gap:5px; overflow-x:auto; max-width:52%; scrollbar-width:none; }
.topnav::-webkit-scrollbar { display:none; }
.topnav button { flex:0 0 auto; border:1px solid rgba(255,255,255,.28); border-radius:6px; padding:6px 8px; background:rgba(255,255,255,.14); color:white; font-size:11px; font-weight:700; }

.icon-btn {
  background: transparent;
  border: none;
  color: white;
  font-size: 20px;
  padding: 4px 8px;
  cursor: pointer;
}

main { padding: 14px; max-width: 640px; margin: 0 auto; padding-bottom: 60px; }

.view { display: block; }
.view[hidden] { display: none; }

label { display: block; font-weight: 600; font-size: 13px; margin: 12px 0 4px; }

input[type="text"], input[type="password"], textarea, select {
  width: 100%;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 15px;
  background: white;
  font-family: inherit;
}
textarea { resize: vertical; }

.title-input { font-size: 18px; font-weight: 700; color: var(--primary); }

button { font-family: inherit; cursor: pointer; }

.primary, .secondary, .danger {
  border: none;
  border-radius: 8px;
  padding: 12px 16px;
  font-size: 14px;
  font-weight: 600;
  margin-top: 10px;
}
.primary { background: var(--primary); color: white; transition: background 0.35s ease; }
.secondary { background: #6b7280; color: white; }
.danger { background: var(--danger); color: white; }
.full { width: 100%; display: block; }
button:disabled { opacity: 0.5; }

.file-btn-label {
  display: block;
  text-align: center;
  background: #e5e7eb;
  color: var(--text);
  border-radius: 8px;
  padding: 10px 16px;
  font-size: 13px;
  font-weight: 600;
  margin-top: 8px;
  cursor: pointer;
}

.hint { color: var(--muted); font-size: 13px; }
.status { color: var(--muted); font-size: 13px; min-height: 18px; }
.status.error { color: var(--danger); }

.toolbar { display: flex; gap: 8px; margin-bottom: 12px; }
.toolbar #select-category { display:none; }
.toolbar select { flex: 1; }
.environment-strip { display:flex; gap:8px; overflow-x:auto; padding:2px 0 12px; scrollbar-width:thin; }
.environment-chip { flex:0 0 auto; min-width:130px; border:1px solid var(--border); border-top:4px solid var(--chip, #7c5ce0); border-radius:10px; background:#fff; color:var(--text); padding:8px 10px; text-align:left; cursor:pointer; }
.environment-chip.selected { box-shadow:0 0 0 2px #c7b7eb; }
.environment-chip strong,.environment-chip small { display:block; }
.environment-chip strong { font-size:12px; }
.environment-chip small { color:var(--muted); font-size:10px; margin-top:4px; white-space:nowrap; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px;
}

.story-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  cursor: pointer;
}
.story-card.has-env { border-top-width: 4px; }
.story-card .cover {
  position: relative;
  aspect-ratio: 1 / 1;
  background: #eef2f6;
  overflow: hidden;
}
.story-card .cover img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  z-index: 1;
}
.story-card .cover .placeholder {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 36px;
}
.story-card .info { padding: 8px 10px; }
.draft-badge { display: inline-block; padding: 2px 5px; margin-bottom: 3px; border: 1px solid #d99a28; border-radius: 4px; color: #9a6500; background: #fff5d6; font-size: 9px; font-weight: 800; letter-spacing: .04em; }
.story-card .info .t { font-weight: 700; font-size: 13px; }
.story-card .info .d { color: var(--muted); font-size: 11px; margin-top: 2px; }

.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
  margin-bottom: 12px;
}

.cover-wrap {
  aspect-ratio: 16 / 10;
  background: #eef2f6;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  margin-bottom: 10px;
}
.cover-wrap img { width: 100%; height: 100%; object-fit: cover; cursor: zoom-in; }
.cover-wrap .placeholder { font-size: 48px; }

.chapter-card { }
.chapter-card .chapter-img-wrap {
  aspect-ratio: 4 / 3;
  background: #eef2f6;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  margin: 8px 0;
}
.chapter-card .chapter-img-wrap img { width: 100%; height: 100%; object-fit: cover; cursor: zoom-in; }
.chapter-card .chapter-img-wrap .placeholder { font-size: 32px; }

.ref-preview { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
.ref-preview img { width: 56px; height: 56px; object-fit: cover; border-radius: 6px; border: 1px solid var(--border); }

.manage-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}
.manage-row:last-child { border-bottom: none; }
.manage-icon { width: 28px; height: 28px; object-fit: cover; border-radius: 6px; }
.manage-name { flex: 1; font-size: 14px; }
.manage-row .danger { margin: 0; padding: 6px 12px; font-size: 12px; }

.avatarset-card { }
.avatarset-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}
.avatarset-name { flex: 1; font-weight: 700; font-size: 15px; }
.avatarset-header .danger { margin: 0; padding: 6px 12px; font-size: 12px; }

.avatar-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
  gap: 10px;
  margin-bottom: 10px;
}
.avatar-item { text-align: center; position: relative; }
.avatar-item .avatar-img-wrap {
  position: relative;
  aspect-ratio: 1 / 1;
  background: #eef2f6;
  border-radius: 50%;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}
.avatar-item img { width: 100%; height: 100%; object-fit: cover; cursor: zoom-in; }
.avatar-item .placeholder { font-size: 22px; }
.avatar-item .avatar-item-name { display: block; font-size: 11px; margin-top: 4px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.avatar-item .btn-remove-avatar {
  position: absolute;
  top: -4px;
  right: -4px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--danger);
  color: white;
  border: none;
  font-size: 11px;
  line-height: 1;
  padding: 0;
  cursor: pointer;
}

.avatarset-add-row { display: flex; gap: 6px; align-items: center; }
.avatarset-add-row input { flex: 1; margin: 0; }
.avatarset-add-row button { margin: 0; padding: 10px 14px; font-size: 12px; white-space: nowrap; }

.environment-card .env-description { color: var(--muted); font-size: 13px; margin: 4px 0 10px; }

.character-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}
.character-row:last-child { border-bottom: none; }
.character-row .char-img-wrap {
  width: 40px;
  height: 40px;
  flex-shrink: 0;
  border-radius: 50%;
  background: #eef2f6;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}
.character-row .char-img-wrap img { width: 100%; height: 100%; object-fit: cover; cursor: zoom-in; }
.character-row .char-info { flex: 1; min-width: 0; }
.character-row .char-name { font-weight: 600; font-size: 13px; }
.character-row .char-personality { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.character-row .danger { margin: 0; padding: 6px 10px; font-size: 11px; }

.char-add-form { margin-top: 10px; }
.char-add-form input, .char-add-form textarea { margin-bottom: 6px; }

.topic-pool { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--border); }
.topic-list { margin: 8px 0; max-height: 220px; overflow-y: auto; }
.topic-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
}
.topic-row:last-child { border-bottom: none; }
.topic-row .topic-title { flex: 1; font-size: 13px; }
.topic-row .danger { margin: 0; padding: 4px 10px; font-size: 11px; }
.input-add-topics { margin-top: 6px; }

.artstyle-card .style-description, .writingstyle-card .style-description { color: var(--muted); font-size: 13px; margin: 4px 0 10px; }
.writingstyle-card .style-example {
  color: var(--muted);
  font-size: 12px;
  font-style: italic;
  background: #f8fafc;
  border-left: 3px solid var(--border);
  padding: 8px 10px;
  margin: 0;
  white-space: pre-wrap;
}

.style-image-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
  gap: 10px;
  margin-bottom: 10px;
}
.style-image-item { position: relative; }
.style-image-item .style-img-wrap {
  position: relative;
  aspect-ratio: 1 / 1;
  background: #eef2f6;
  border-radius: 8px;
  overflow: hidden;
}
.style-image-item img { width: 100%; height: 100%; object-fit: cover; cursor: zoom-in; }
.style-image-item .btn-remove-style-image {
  position: absolute;
  top: -4px;
  right: -4px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--danger);
  color: white;
  border: none;
  font-size: 11px;
  line-height: 1;
  padding: 0;
  cursor: pointer;
}

.env-style-row { display: flex; align-items: center; gap: 8px; margin: 10px 0; }
.env-style-row label { margin: 0; white-space: nowrap; }
.env-style-row select { margin: 0; flex: 1; }

.environment-card { border-left-width: 5px; }

input[type="color"] {
  width: 48px;
  height: 38px;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: white;
  cursor: pointer;
}

.env-color-row { display: flex; align-items: center; gap: 8px; margin: 10px 0; }
.env-color-row label { margin: 0; white-space: nowrap; }

.translation-badges { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.translation-badge {
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  border: 1px solid var(--border);
  background: white;
  cursor: pointer;
}
.translation-badge.current { background: var(--primary); color: white; border-color: var(--primary); }

.translation-add-row { display: flex; gap: 6px; align-items: center; }
.translation-add-row select { flex: 1; margin: 0; }
.translation-add-row button { margin: 0; padding: 10px 14px; font-size: 12px; white-space: nowrap; }

.agent-notes-card { background: #f8fafc; }
.agent-note-row { display: flex; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--border); font-size: 12px; }
.agent-note-row:last-child { border-bottom: none; }
.agent-note-row .agent-role { font-weight: 700; white-space: nowrap; color: var(--primary); }
.agent-note-row .agent-text { color: var(--muted); }

.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}
.lightbox[hidden] { display: none; }
.lightbox img { max-width: 94vw; max-height: 90vh; object-fit: contain; }
.lightbox-close { position: absolute; top: 10px; right: 10px; background: rgba(255,255,255,0.15); border-radius: 50%; }
