/* ── Reset & Base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg-primary: #08080d;
  --bg-card: #111118;
  --bg-surface: #16161f;
  --bg-input: #1c1c28;
  --border: #252535;
  --text-primary: #e8e8f0;
  --text-secondary: #8888a0;
  --text-muted: #555568;
  --accent: #7c5cfc;
  --accent-hover: #9b7fff;
  --accent-glow: rgba(124, 92, 252, 0.15);
  --danger: #e84057;
  --success: #38c97a;
  --radius: 12px;
  --radius-sm: 8px;
  --transition: 0.2s ease;
  --font: 'Inter', -apple-system, sans-serif;
}

html, body {
  font-family: var(--font);
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100dvh;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}

/* ── Pages ── */
.page { display: none; min-height: 100dvh; padding-bottom: 24px; }
.page.active { display: flex; flex-direction: column; }

/* ── Top Bar ── */
.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  position: sticky;
  top: 0;
  z-index: 100;
  background: rgba(8, 8, 13, 0.85);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border);
}

.logo {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.3px;
  background: linear-gradient(135deg, var(--accent), #c084fc);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.card-count {
  font-size: 12px;
  color: var(--text-muted);
  padding: 4px 10px;
  background: var(--bg-surface);
  border-radius: 20px;
}

.btn-back {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  padding: 6px 0;
  font-family: var(--font);
}

.top-actions { display: flex; gap: 8px; }

.btn-icon {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  width: 36px;
  height: 36px;
  font-size: 16px;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: background var(--transition);
}
.btn-icon:active { background: var(--bg-input); }
.btn-icon.btn-danger:active { background: rgba(232, 64, 87, 0.15); }

.page-title {
  font-size: 16px;
  font-weight: 600;
  flex: 1;
  text-align: center;
  margin-right: 40px;
}

/* ── Gallery Grid ── */
.card-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  padding: 12px;
  align-content: start;
  flex: 1 0 0;
  overflow-y: auto;
}

@media (min-width: 540px) {
  .card-grid { grid-template-columns: repeat(3, 1fr); }
}
@media (min-width: 800px) {
  .card-grid { grid-template-columns: repeat(4, 1fr); gap: 14px; padding: 16px; }
}

/* ── Card Item ── */
.card-item {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  transition: transform var(--transition), box-shadow var(--transition);
  position: relative;
}
.card-item:active {
  transform: scale(0.97);
}

.card-avatar {
  width: 100%;
  aspect-ratio: 3/4;
  object-fit: cover;
  display: block;
  background: var(--bg-surface);
}

.card-info {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 24px 10px 10px;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.85));
  pointer-events: none;
}

.card-name {
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: #fff;
  text-shadow: 0 1px 4px rgba(0,0,0,0.5);
}

.card-creator {
  font-size: 11px;
  color: rgba(255,255,255,0.65);
  margin-top: 2px;
  text-shadow: 0 1px 3px rgba(0,0,0,0.4);
}

/* ── Empty State ── */
.empty-state {
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  padding: 60px 24px;
  text-align: center;
}
.empty-state.show { display: flex; }

.empty-icon { font-size: 48px; margin-bottom: 16px; opacity: 0.5; }
.empty-state p { color: var(--text-secondary); font-size: 15px; }
.empty-sub { font-size: 13px !important; color: var(--text-muted) !important; margin-top: 6px; }

/* ── FAB ── */
.fab {
  position: fixed;
  bottom: 24px;
  right: 20px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  font-size: 28px;
  border: none;
  cursor: pointer;
  box-shadow: 0 4px 24px rgba(124, 92, 252, 0.35);
  display: grid;
  place-items: center;
  z-index: 200;
  transition: transform var(--transition), background var(--transition);
}
.fab:active { transform: scale(0.9); background: var(--accent-hover); }

/* ── Detail Page ── */
.detail-scroll {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.detail-hero {
  position: relative;
  width: 100%;
  max-height: 400px;
  overflow: hidden;
}

.detail-hero img {
  width: 100%;
  display: block;
  object-fit: cover;
}

.detail-hero-overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 40px 16px 16px;
  background: linear-gradient(transparent, var(--bg-primary));
}

.detail-name {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.3px;
}

.detail-creator {
  font-size: 13px;
  color: var(--text-secondary);
  margin-top: 4px;
}

.detail-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 12px 16px 4px;
}

.tag {
  font-size: 11px;
  padding: 4px 10px;
  border-radius: 20px;
  background: var(--accent-glow);
  color: var(--accent);
  border: 1px solid rgba(124, 92, 252, 0.2);
}

/* ── Content Section ── */
.section {
  padding: 16px;
  border-bottom: 1px solid var(--border);
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.section-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-muted);
}

.btn-translate {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-secondary);
  font-size: 12px;
  padding: 3px 8px;
  cursor: pointer;
  font-family: var(--font);
  transition: all var(--transition);
  display: flex;
  align-items: center;
  gap: 4px;
}
.btn-translate:hover { border-color: var(--accent); color: var(--accent); }
.btn-translate.loading { opacity: 0.5; pointer-events: none; }
.btn-translate.translated { background: var(--accent-glow); border-color: var(--accent); color: var(--accent); }

.section-body {
  font-size: 14px;
  line-height: 1.65;
  color: var(--text-secondary);
  white-space: pre-wrap;
  word-break: break-word;
}

/* ── Alternate Greetings ── */
.greeting-toggle {
  font-size: 13px;
  color: var(--accent);
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px 0;
  font-family: var(--font);
}

.greeting-item {
  background: var(--bg-surface);
  border-radius: var(--radius-sm);
  padding: 12px;
  margin-top: 8px;
  font-size: 13px;
  line-height: 1.6;
  color: var(--text-secondary);
  white-space: pre-wrap;
  border-left: 3px solid var(--accent);
}

/* ── Notes Button (on detail page) ── */
.btn-notes {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin: 16px;
  padding: 12px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  font-family: var(--font);
  transition: all var(--transition);
}
.btn-notes:hover { border-color: var(--accent); background: var(--accent-glow); }

.notes-badge {
  background: var(--accent);
  color: white;
  font-size: 11px;
  padding: 1px 7px;
  border-radius: 10px;
  font-weight: 600;
}

/* ── Notes Page ── */
.notes-container {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.notes-editor {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.notes-editor textarea {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-family: var(--font);
  font-size: 14px;
  padding: 12px;
  resize: vertical;
  min-height: 80px;
  outline: none;
  transition: border-color var(--transition);
}
.notes-editor textarea:focus { border-color: var(--accent); }
.notes-editor textarea::placeholder { color: var(--text-muted); }

.btn-primary {
  background: var(--accent);
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  font-family: var(--font);
  transition: background var(--transition);
}
.btn-primary:active { background: var(--accent-hover); }

/* ── Note Item ── */
.note-item {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  position: relative;
}

.note-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.note-date {
  font-size: 11px;
  color: var(--text-muted);
}

.note-actions {
  display: flex;
  gap: 6px;
}

.note-actions button {
  background: none;
  border: none;
  font-size: 14px;
  cursor: pointer;
  padding: 2px 4px;
  opacity: 0.5;
  transition: opacity var(--transition);
}
.note-actions button:hover { opacity: 1; }

.note-body {
  font-size: 14px;
  line-height: 1.6;
  color: var(--text-secondary);
  white-space: pre-wrap;
  word-break: break-word;
}

.notes-empty {
  text-align: center;
  padding: 40px 16px;
  color: var(--text-muted);
  font-size: 14px;
}

/* ── Toast ── */
.toast {
  position: fixed;
  bottom: 90px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--bg-surface);
  color: var(--text-primary);
  padding: 10px 20px;
  border-radius: 24px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: all 0.3s ease;
  z-index: 999;
  border: 1px solid var(--border);
  white-space: nowrap;
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ── Lorebook ── */
.lorebook-entry {
  background: var(--bg-surface);
  border-radius: var(--radius-sm);
  padding: 12px;
  margin-top: 8px;
}

.lorebook-keys {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 6px;
}

.lorebook-key {
  font-size: 10px;
  padding: 2px 8px;
  border-radius: 4px;
  background: rgba(56, 201, 122, 0.12);
  color: var(--success);
  border: 1px solid rgba(56, 201, 122, 0.2);
}

.lorebook-content {
  font-size: 13px;
  line-height: 1.55;
  color: var(--text-secondary);
  white-space: pre-wrap;
}

/* ── Collapsible Accordion Groups ── */
.accordion-group {
  margin: 8px 0;
  border-bottom: 1px solid var(--border);
}

.accordion-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 14px 16px;
  background: none;
  border: none;
  cursor: pointer;
  font-family: var(--font);
  color: var(--text-primary);
  transition: background var(--transition);
}
.accordion-toggle:hover { background: rgba(124, 92, 252, 0.04); }
.accordion-toggle:active { background: rgba(124, 92, 252, 0.08); }

.accordion-toggle-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.accordion-icon {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  background: var(--accent-glow);
  display: grid;
  place-items: center;
  color: var(--accent);
  flex-shrink: 0;
}

.accordion-title {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.2px;
}

.accordion-badge {
  font-size: 11px;
  color: var(--text-muted);
  font-weight: 400;
  margin-left: 6px;
}

.accordion-chevron {
  transition: transform 0.25s ease;
  color: var(--text-muted);
  flex-shrink: 0;
}

.accordion-group.open .accordion-chevron {
  transform: rotate(180deg);
}

.accordion-body {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.35s ease;
}

.accordion-group.open .accordion-body {
  max-height: none;
}

.accordion-body-inner {
  padding: 0 0 8px;
}

/* Sections inside accordion lose their own border */
.accordion-body .section {
  border-bottom: none;
  padding-top: 8px;
  padding-bottom: 8px;
}

/* Extension items */
.ext-item {
  background: var(--bg-surface);
  border-radius: var(--radius-sm);
  padding: 12px;
  margin: 6px 16px;
}

.ext-item-header {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 6px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.ext-item-label {
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 2px;
}

.ext-item-value {
  font-size: 13px;
  line-height: 1.55;
  color: var(--text-secondary);
  white-space: pre-wrap;
  word-break: break-word;
}

.ext-type-tag {
  font-size: 10px;
  padding: 2px 7px;
  border-radius: 4px;
  background: rgba(124, 92, 252, 0.12);
  color: var(--accent);
  font-weight: 500;
}

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

/* ── Upload Loading ── */
.upload-loading {
  position: fixed;
  inset: 0;
  background: rgba(8, 8, 13, 0.8);
  display: grid;
  place-items: center;
  z-index: 500;
}

.upload-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ── SVG Inline Icons ── */
.icon-inline {
  display: inline-block;
  vertical-align: -2px;
  flex-shrink: 0;
}

.icon-spin {
  animation: spin 0.8s linear infinite;
}

.icon-logo {
  display: inline-block;
  vertical-align: -2px;
  margin-right: 4px;
  color: var(--accent);
}

/* Toast with SVG icon */
.toast svg.icon-inline {
  vertical-align: -3px;
  margin-right: 2px;
}

/* Button SVG icons inherit color */
.btn-icon svg,
.btn-back svg,
.btn-translate svg,
.btn-translate-note svg,
.btn-delete-note svg,
.fab svg {
  pointer-events: none;
}

/* ── Settings Sidebar ── */
.settings-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 400;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
}
.settings-overlay.open { opacity: 1; pointer-events: auto; }

.settings-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(340px, 85vw);
  background: var(--bg-card);
  border-left: 1px solid var(--border);
  z-index: 401;
  transform: translateX(100%);
  transition: transform 0.3s cubic-bezier(0.4,0,0.2,1);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}
.settings-panel.open { transform: translateX(0); }

.settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--bg-card);
  z-index: 1;
}

.settings-header h2 {
  font-size: 15px;
  font-weight: 600;
}

.settings-body {
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.settings-group label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.6px;
  margin-bottom: 6px;
}

.settings-group input[type="text"],
.settings-group textarea {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-family: var(--font);
  font-size: 13px;
  padding: 10px 12px;
  outline: none;
  transition: border-color var(--transition);
}
.settings-group input:focus,
.settings-group textarea:focus { border-color: var(--accent); }
.settings-group input::placeholder,
.settings-group textarea::placeholder { color: var(--text-muted); }

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

.settings-group textarea {
  resize: vertical;
  min-height: 80px;
}

.settings-divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 0;
}

/* ── Model Row with Fetch ── */
.model-row {
  display: flex;
  gap: 8px;
  align-items: stretch;
}
.model-row input { flex: 1; min-width: 0; }

.btn-fetch {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 0 12px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-family: var(--font);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: border-color var(--transition), color var(--transition);
}
.btn-fetch:hover { border-color: var(--accent); color: var(--text-primary); }
.btn-fetch:active { transform: scale(0.97); }
.btn-fetch.loading { pointer-events: none; opacity: 0.6; }
.btn-fetch svg { flex-shrink: 0; }
