/* ========================================
   Underlore — Complete Application Styles
   ======================================== */

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

html, body {
  height: 100%;
  overflow: hidden;
  font-family: var(--font-ui);
  background: var(--paper);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* --- CSS Variables --- */
:root {
  --paper: #FFFFFF;
  --ink: #1A1A1A;
  --ink-light: #555555;
  --ink-faint: #999999;
  --accent: #1A1A1A;
  --accent-warm: #555555;
  --paper-dark: #E5E5E5;
  --overlay-bg: rgba(255, 255, 255, 0.94);
  --font-ui: 'Inter', -apple-system, sans-serif;
  --font-story: 'JetBrains Mono', 'SF Mono', 'Consolas', monospace;
}

/* --- Map --- */
#map {
  width: 100%;
  height: 100vh;
  position: relative;
}

#map::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
  z-index: 1;
  opacity: 0.035;
  mix-blend-mode: multiply;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noise)'/%3E%3C/svg%3E");
  background-repeat: repeat;
  background-size: 256px 256px;
}

.maplibregl-canvas {
  outline: none;
}

/* --- Floating Controls --- */
.floating-controls {
  position: fixed;
  top: 16px;
  right: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 10;
}

.control-btn {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: 1px solid var(--paper-dark);
  background: var(--overlay-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--ink);
  transition: all 0.2s ease;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
}

.control-btn:hover {
  background: var(--paper);
  border-color: var(--ink-faint);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.control-btn:active {
  transform: scale(0.95);
}

.control-btn svg {
  width: 18px;
  height: 18px;
}

/* --- Collection Badge --- */
.collection-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  width: 18px;
  height: 18px;
  background: var(--ink);
  color: var(--paper);
  border-radius: 50%;
  font-size: 10px;
  font-weight: 600;
  display: none;
  align-items: center;
  justify-content: center;
  font-family: var(--font-ui);
}

.collection-badge.visible {
  display: flex;
}

.collection-btn {
  position: fixed;
  top: 16px;
  left: 16px;
  z-index: 10;
}

/* --- Locate Button --- */
.locate-btn {
  position: fixed;
  bottom: 32px;
  right: 16px;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: 1px solid var(--paper-dark);
  background: var(--overlay-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--ink);
  transition: all 0.2s ease;
  z-index: 10;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
}

.locate-btn:hover {
  background: var(--paper);
  border-color: var(--ink-faint);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.locate-btn:active {
  transform: scale(0.95);
}

.locate-btn i {
  font-size: 16px;
}

.locate-btn.locating i {
  animation: spin 0.8s linear infinite;
}

/* --- Scope / Discover This Area Button --- */
.scope-btn {
  position: fixed;
  bottom: 32px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 18px;
  border-radius: 24px;
  border: 1px solid var(--paper-dark);
  background: var(--overlay-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  font-family: var(--font-ui);
  font-size: 13px;
  font-weight: 500;
  color: var(--ink);
  cursor: pointer;
  transition: all 0.3s ease;
  z-index: 10;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  white-space: nowrap;
}

.scope-btn--hidden {
  opacity: 0;
  pointer-events: none;
  transform: translateX(-50%) translateY(10px);
}

.scope-btn:hover {
  background: var(--paper);
  border-color: var(--ink-faint);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
}

.scope-btn:active {
  transform: translateX(-50%) scale(0.97);
}

.scope-btn svg {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
}

/* --- Search (inline expand) --- */
.search-container {
  display: flex;
  align-items: center;
  position: relative;
}

.search-container .search-btn {
  z-index: 1;
  transition: all 0.35s cubic-bezier(0.4, 0, 0.2, 1);
}

.search-container.open .search-btn {
  border-color: transparent;
  background: transparent;
  backdrop-filter: none;
  -webkit-backdrop-filter: none;
  box-shadow: none;
}

.search-bar {
  width: 42px;
  height: 42px;
  overflow: hidden;
  transition: all 0.35s cubic-bezier(0.4, 0, 0.2, 1);
  display: flex;
  align-items: center;
  background: var(--overlay-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--paper-dark);
  border-radius: 21px;
  margin-left: -42px;
  padding-left: 42px;
  opacity: 0;
  pointer-events: none;
}

.search-bar.open {
  width: 280px;
  opacity: 1;
  pointer-events: auto;
}

.search-bar input {
  width: 100%;
  height: 100%;
  border: none;
  background: none;
  font-family: var(--font-ui);
  font-size: 14px;
  color: var(--ink);
  padding: 0 16px 0 8px;
  outline: none;
}

.search-bar input::placeholder {
  color: var(--ink-faint);
}

/* --- Search Results Dropdown --- */
.search-results {
  position: absolute;
  top: 48px;
  left: 0;
  right: 0;
  background: var(--overlay-bg);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid var(--paper-dark);
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  z-index: 10;
}

.search-result-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  width: 100%;
  padding: 12px 16px;
  border: none;
  border-bottom: 1px solid var(--paper-dark);
  background: none;
  cursor: pointer;
  text-align: left;
  font-family: var(--font-ui);
  transition: background 0.15s;
}

.search-result-item:last-child {
  border-bottom: none;
}

.search-result-item:hover {
  background: rgba(0, 0, 0, 0.03);
}

.search-result-item:active {
  background: rgba(0, 0, 0, 0.06);
}

.search-result-name {
  font-size: 14px;
  font-weight: 500;
  color: var(--ink);
}

.search-result-region {
  font-size: 12px;
  color: var(--ink-faint);
}

/* --- Shared Overlay Styles --- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  flex-direction: column;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

.overlay.open {
  opacity: 1;
  pointer-events: auto;
}

.overlay-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(44, 40, 37, 0.3);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.overlay-content {
  position: relative;
  margin: auto;
  max-width: 480px;
  width: calc(100% - 32px);
  max-height: 75vh;
  background: var(--overlay-bg);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-radius: 2px;
  border: 1px solid var(--paper-dark);
  overflow-y: auto;
  transform: scale(0.97) translateY(8px);
  transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.25s ease;
}

.overlay.open .overlay-content {
  transform: scale(1) translateY(0);
}

.collection-overlay .overlay-content {
  max-height: 80vh;
}

.overlay-close {
  position: sticky;
  top: 0;
  display: flex;
  justify-content: flex-end;
  padding: 16px 16px 0;
  z-index: 1;
}

.overlay-close button {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: rgba(0, 0, 0, 0.05);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--ink-faint);
  transition: background 0.2s;
}

.overlay-close button svg {
  width: 16px;
  height: 16px;
  stroke: var(--ink-light);
  stroke-width: 2;
  fill: none;
}

.overlay-close button:hover {
  background: rgba(0, 0, 0, 0.1);
}

.overlay-handle {
  width: 36px;
  height: 4px;
  background: var(--paper-dark);
  border-radius: 2px;
  margin: 10px auto 0;
}

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

/* --- Story Overlay --- */
.story-content {
  padding: 4px 28px 36px;
}

.story-overlay .story-category {
  font-family: var(--font-story);
  font-size: 11px;
  color: var(--ink-faint);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 6px;
}

.story-overlay .story-title,
.story-overlay .story-place-name {
  font-family: var(--font-ui);
  font-size: 22px;
  font-weight: 600;
  color: var(--ink);
  margin-bottom: 4px;
}

.story-overlay .story-coords {
  font-family: var(--font-story);
  font-size: 11px;
  color: var(--ink-faint);
  margin-bottom: 16px;
}

.story-overlay .story-body {
  font-family: var(--font-story);
  font-size: 13px;
  line-height: 1.75;
  color: var(--ink-light);
}

.story-overlay .story-body p {
  margin-bottom: 14px;
}

.story-overlay .story-body p:last-child {
  margin-bottom: 0;
}

.story-overlay .story-meta {
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--paper-dark);
  font-family: var(--font-ui);
  font-size: 11px;
  color: var(--ink-faint);
  display: flex;
  align-items: center;
  gap: 8px;
}

.story-overlay .collect-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 14px;
  margin-top: 20px;
  border-radius: 12px;
  border: 1.5px solid var(--ink);
  background: var(--paper);
  font-family: var(--font-ui);
  font-size: 14px;
  font-weight: 500;
  color: var(--ink);
  cursor: pointer;
  transition: all 0.2s;
}

.story-overlay .collect-btn:hover {
  background: var(--ink);
  color: var(--paper);
}

.story-overlay .collect-btn.collected {
  background: var(--ink);
  color: var(--paper);
  border-color: var(--ink);
  cursor: default;
}

/* --- Collection / Lorebook Overlay --- */
.collection-header {
  padding: 4px 28px 20px;
  border-bottom: 1px solid var(--paper-dark);
}

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

.collection-header h2 {
  font-family: var(--font-ui);
  font-size: 18px;
  font-weight: 600;
  color: var(--ink);
}

.collection-header span {
  font-family: var(--font-story);
  font-size: 12px;
  color: var(--ink-faint);
}

.collection-overlay .collection-stats {
  display: flex;
  gap: 24px;
  margin-bottom: 20px;
}

.collection-overlay .stat {
  display: flex;
  flex-direction: column;
}

.collection-overlay .stat-number {
  font-family: var(--font-story);
  font-size: 28px;
  font-weight: 500;
  color: var(--ink);
  line-height: 1;
}

.collection-overlay .stat-label {
  font-family: var(--font-ui);
  font-size: 11px;
  color: var(--ink-faint);
  margin-top: 4px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.collection-overlay .collection-list {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.collection-overlay .collection-item {
  display: flex;
  align-items: flex-start;
  gap: 14px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--paper-dark);
  cursor: pointer;
  transition: background 0.15s;
}

.collection-overlay .collection-item:hover {
  background: rgba(0, 0, 0, 0.02);
}

.collection-overlay .collection-item:last-child {
  border-bottom: none;
}

.collection-overlay .collection-item-number {
  font-family: var(--font-story);
  font-size: 12px;
  color: var(--ink-faint);
  width: 28px;
  text-align: right;
  flex-shrink: 0;
}

.collection-overlay .collection-item-info {
  flex: 1;
  min-width: 0;
}

.collection-overlay .collection-item-name {
  font-family: var(--font-ui);
  font-size: 15px;
  font-weight: 500;
  color: var(--ink);
}

.collection-overlay .collection-item-date {
  font-family: var(--font-story);
  font-size: 11px;
  color: var(--ink-faint);
  margin-top: 2px;
}

.collection-overlay .collection-item-num {
  font-family: var(--font-story);
  font-size: 12px;
  color: var(--ink-faint);
  width: 28px;
  text-align: right;
  flex-shrink: 0;
}

.collection-overlay .collection-item-body {
  flex: 1;
  min-width: 0;
}

.collection-overlay .collection-item-preview {
  font-family: var(--font-story);
  font-size: 11px;
  color: var(--ink-faint);
  margin-top: 2px;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.collection-overlay .collection-item-map {
  width: 48px;
  height: 48px;
  border-radius: 8px;
  overflow: hidden;
  flex-shrink: 0;
}

.collection-overlay .collection-item-map img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.collection-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.collection-list .empty-state {
  text-align: center;
  padding: 40px 20px;
  font-family: var(--font-ui);
  font-size: 14px;
  color: var(--ink-faint);
}

.collection-overlay .collection-empty {
  text-align: center;
  padding: 40px 20px;
}

.collection-overlay .collection-empty-icon {
  font-size: 32px;
  margin-bottom: 12px;
  opacity: 0.3;
}

.collection-overlay .collection-empty-text {
  font-family: var(--font-ui);
  font-size: 14px;
  color: var(--ink-faint);
  line-height: 1.5;
}

/* --- Loading Spinner --- */
.loading-spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid var(--paper-dark);
  border-top-color: var(--ink);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

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

/* --- Utility --- */
.hidden {
  display: none !important;
}

.fade-in {
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* --- Auth Pages --- */
.auth-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  min-height: 100dvh;
  padding: 20px;
  overflow: auto;
}

.auth-card {
  width: 100%;
  max-width: 380px;
  text-align: center;
}

.auth-title {
  font-family: var(--font-ui);
  font-size: 20px;
  font-weight: 600;
  color: var(--ink);
  margin-bottom: 6px;
}

.auth-subtitle {
  font-family: var(--font-ui);
  font-size: 14px;
  color: var(--ink-faint);
  margin-bottom: 28px;
}

.auth-alert {
  font-family: var(--font-ui);
  font-size: 13px;
  color: #c53030;
  background: #fff5f5;
  border: 1px solid #fed7d7;
  border-radius: 8px;
  padding: 10px 14px;
  margin-bottom: 20px;
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.auth-code-input,
.auth-email-input {
  display: flex;
  justify-content: center;
}

.auth-code-field {
  font-family: var(--font-story);
  font-size: 28px;
  font-weight: 500;
  letter-spacing: 8px;
  text-align: center;
  width: 100%;
  max-width: 260px;
  padding: 14px 16px;
  border: 1px solid var(--paper-dark);
  border-radius: 10px;
  background: var(--paper);
  color: var(--ink);
  outline: none;
  transition: border-color 0.2s;
  text-transform: uppercase;
}

.auth-code-field:focus {
  border-color: var(--ink);
}

.auth-code-field::placeholder {
  color: var(--paper-dark);
  letter-spacing: 12px;
}

.auth-email-field {
  font-family: var(--font-ui);
  font-size: 15px;
  width: 100%;
  padding: 12px 16px;
  border: 1px solid var(--paper-dark);
  border-radius: 10px;
  background: var(--paper);
  color: var(--ink);
  outline: none;
  transition: border-color 0.2s;
}

.auth-email-field:focus {
  border-color: var(--ink);
}

.auth-email-field::placeholder {
  color: var(--ink-faint);
}

.auth-submit {
  font-family: var(--font-ui);
  font-size: 14px;
  font-weight: 500;
  padding: 12px 24px;
  border: 1.5px solid var(--ink);
  border-radius: 10px;
  background: var(--ink);
  color: var(--paper);
  cursor: pointer;
  transition: all 0.2s;
}

.auth-submit:hover {
  background: var(--paper);
  color: var(--ink);
}

.auth-submit:active {
  transform: scale(0.98);
}

.auth-hint {
  font-family: var(--font-story);
  font-size: 12px;
  color: var(--ink-faint);
  margin-top: 20px;
}

/* --- Lorebook Auth Section --- */
.lorebook-auth {
  padding: 20px 28px;
  border-top: 1px solid var(--paper-dark);
}

.lorebook-auth-signed-in {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.lorebook-auth-email {
  font-family: var(--font-story);
  font-size: 12px;
  color: var(--ink-faint);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.lorebook-auth-signout {
  font-family: var(--font-ui);
  font-size: 12px;
  color: var(--ink-faint);
  text-decoration: none;
  flex-shrink: 0;
  transition: color 0.2s;
}

.lorebook-auth-signout:hover {
  color: var(--ink);
}

.lorebook-auth-label {
  font-family: var(--font-ui);
  font-size: 13px;
  color: var(--ink-faint);
  margin-bottom: 12px;
}

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

.lorebook-auth-email-input {
  flex: 1;
  min-width: 0;
  font-family: var(--font-ui);
  font-size: 13px;
  padding: 8px 12px;
  border: 1px solid var(--paper-dark);
  border-radius: 8px;
  background: var(--paper);
  color: var(--ink);
  outline: none;
  transition: border-color 0.2s;
}

.lorebook-auth-email-input:focus {
  border-color: var(--ink);
}

.lorebook-auth-email-input::placeholder {
  color: var(--ink-faint);
}

.lorebook-auth-submit {
  font-family: var(--font-ui);
  font-size: 13px;
  font-weight: 500;
  padding: 8px 14px;
  border: 1px solid var(--ink);
  border-radius: 8px;
  background: var(--ink);
  color: var(--paper);
  cursor: pointer;
  flex-shrink: 0;
  transition: all 0.2s;
}

.lorebook-auth-submit:hover {
  background: var(--paper);
  color: var(--ink);
}

/* --- Scope Button Loading State --- */
.scope-btn--loading {
  pointer-events: none;
  opacity: 0.85;
}

.scope-btn-spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid rgba(0,0,0,0.15);
  border-top-color: var(--ink);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  vertical-align: middle;
}

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

/* --- Responsive --- */
@media (min-width: 600px) {
  .search-bar.open {
    width: 320px;
  }
}
