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

:root {
  --primary: #885f9b;
  --primary-hover: #441145;
  --primary-disabled: #a88cb5;
  --primary-added: #c4b5cf;
  --primary-light: #f3eff6;
  --danger: #b45670;
  --danger-hover: #9a4460;
  --bg: #faf8f6;
  --bg-muted: #ece7f0;
  --bg-error: #fdf2f5;
  --card-bg: #ffffff;
  --text: #3d3543;
  --text-muted: #8a7f91;
  --border: #e8e2ec;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(80,50,90,0.08);
  --shadow-lg: 0 4px 12px rgba(80,50,90,0.1);
}

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100dvh;
  padding-bottom: 80px; /* space for sticky bar */
}

/* === Header === */
.header {
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-hover) 100%);
  padding: 0;
  text-align: center;
  position: relative;
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 28px 16px 12px;
}

.header-icon {
  flex-shrink: 0;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.15));
}

.header h1 {
  font-size: 1.5rem;
  font-weight: 800;
  color: #fff;
  letter-spacing: 0.02em;
  text-shadow: 0 1px 3px rgba(0,0,0,0.15);
}

.header p {
  font-size: 0.85rem;
  color: rgba(255,255,255,0.8);
  margin-top: 2px;
}

.header-wave {
  display: block;
  width: 100%;
  height: 24px;
  margin-top: -2px;
}

/* === Main layout === */
.main {
  max-width: 960px;
  margin: 0 auto;
  padding: 16px;
}

/* === Search section === */
.search-section {
  margin-bottom: 24px;
}

.search-bar {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.search-bar input {
  flex: 1;
  padding: 12px 16px;
  font-size: 1rem;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  outline: none;
  transition: border-color 0.2s;
}

.search-bar input:focus {
  border-color: var(--primary);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 12px 20px;
  font-size: 1rem;
  font-weight: 600;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
  white-space: nowrap;
  min-height: 48px;
  min-width: 48px;
}

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

.btn-primary {
  background: var(--primary);
  color: white;
}

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

.btn-primary:disabled {
  background: var(--primary-disabled);
  cursor: not-allowed;
}

.btn-scan {
  background: transparent;
  color: var(--primary);
  border: 2px solid var(--primary);
  font-size: 0.9rem;
  padding: 10px 16px;
}

.btn-scan:hover {
  background: var(--primary);
  color: white;
}

.btn-danger {
  background: var(--danger);
  color: white;
}

.btn-danger:hover {
  background: var(--danger-hover);
}

.btn-added,
.btn-added:hover,
.btn-added:active {
  background: var(--primary-added);
  color: white;
  cursor: default;
  pointer-events: none;
}

.btn-sm {
  padding: 8px 14px;
  font-size: 0.85rem;
  min-height: 36px;
}

.btn-icon {
  padding: 8px;
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1.2rem;
  min-height: 40px;
  min-width: 40px;
}

.btn-icon:hover {
  background: var(--primary-light);
}

/* === Inline scanner === */
.scanner-section {
  margin-bottom: 24px;
  border-radius: var(--radius);
  overflow: hidden;
  background: #000;
  box-shadow: var(--shadow-lg);
}

.scanner-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  background: rgba(0,0,0,0.85);
  color: white;
}

.scanner-status {
  font-size: 0.85rem;
  color: #d1d5db;
}

.scanner-camera {
  position: relative;
  width: 100%;
  max-height: 280px;
  overflow: hidden;
}

.scanner-camera video {
  width: 100%;
  height: auto;
  display: block;
}

/* === Search results === */
.section-title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.results-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  margin-bottom: 24px;
}

.book-card {
  display: flex;
  gap: 12px;
  padding: 12px;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.book-card img {
  width: 60px;
  height: auto;
  border-radius: 6px;
  object-fit: cover;
  flex-shrink: 0;
}

.book-card .no-cover {
  width: 60px;
  height: 80px;
  background: var(--bg-muted);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  color: var(--text-muted);
  text-align: center;
  flex-shrink: 0;
}

.book-info {
  flex: 1;
  min-width: 0;
}

.book-info .title {
  font-weight: 600;
  font-size: 0.95rem;
  line-height: 1.3;
}

.book-info .author {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.book-info .meta {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 4px;
}

.book-card .actions {
  display: flex;
  flex-direction: column;
  justify-content: center;
}

/* === Selected covers === */
.selected-section {
  margin-bottom: 24px;
}

.clear-all-btn {
  margin-left: auto;
}

.selected-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
  gap: 8px;
}

.selected-item {
  position: relative;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  background: var(--card-bg);
}

.selected-item img {
  width: 100%;
  height: auto;
  display: block;
}

.selected-item .remove-btn {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--danger-hover);
  color: white;
  border: none;
  cursor: pointer;
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.selected-item .remove-btn:hover {
  background: var(--danger);
}

.selected-item .item-title {
  padding: 4px 6px;
  font-size: 0.7rem;
  line-height: 1.2;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* === Sticky bottom bar === */
.bottom-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 12px 16px;
  background: var(--card-bg);
  border-top: 1px solid var(--border);
  box-shadow: 0 -2px 8px rgba(0,0,0,0.08);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  z-index: 100;
}

.bottom-bar .count {
  font-size: 0.9rem;
  color: var(--text-muted);
  white-space: nowrap;
}

.bottom-bar .btn {
  flex-shrink: 0;
}

/* === Loading / empty states === */
.loading {
  text-align: center;
  padding: 32px 16px;
  color: var(--text-muted);
}

.spinner {
  display: inline-block;
  width: 24px;
  height: 24px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-bottom: 8px;
}

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

.empty-state {
  text-align: center;
  padding: 32px 16px;
  color: var(--text-muted);
  font-size: 0.9rem;
}

.error-msg {
  background: var(--bg-error);
  color: var(--danger);
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 0.9rem;
  margin-bottom: 12px;
}

/* === Disclaimer === */
.disclaimer {
  text-align: center;
  padding: 16px 16px 80px;
  color: var(--text-muted);
  font-size: 0.75rem;
  line-height: 1.4;
}

/* === Toast notifications === */
.toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: white;
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 0.9rem;
  z-index: 200;
  opacity: 0;
  transition: opacity 0.3s;
  pointer-events: none;
}

.toast.show {
  opacity: 1;
}

/* === Desktop layout (768px+) === */
@media (min-width: 768px) {
  .main {
    padding: 24px;
  }

  .search-bar input {
    font-size: 1rem;
  }

  .btn-scan {
    width: auto;
  }

  .results-grid {
    grid-template-columns: 1fr 1fr;
  }

  .selected-grid {
    grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
  }

  .bottom-bar {
    max-width: 960px;
    left: 50%;
    transform: translateX(-50%);
    border-radius: var(--radius) var(--radius) 0 0;
  }
}
