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

:root {
  --color-bg: #FFF8F0;
  --color-surface: #FFFFFF;
  --color-primary: #F59E0B;
  --color-primary-hover: #D97706;
  --color-text: #4A3728;
  --color-text-muted: #8B7355;
  --color-player-bg: rgba(255, 248, 240, 0.97);
  --color-green: #10B981;
  --color-red: #EF4444;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.08);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.1);
  --radius: 16px;
  --radius-sm: 10px;
  --player-height: 80px;
}

html {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 16px;
  color: var(--color-text);
  background: var(--color-bg);
  -webkit-font-smoothing: antialiased;
}

body {
  min-height: 100dvh;
  padding-bottom: var(--player-height);
  background: linear-gradient(135deg, #FFF8F0 0%, #FEF3C7 25%, #FFF8F0 50%, #FDE68A 75%, #FFF8F0 100%);
  background-size: 400% 400%;
  animation: bgShift 20s ease-in-out infinite;
}

@keyframes bgShift {
  0%   { background-position: 0% 50%; }
  25%  { background-position: 100% 0%; }
  50%  { background-position: 100% 100%; }
  75%  { background-position: 0% 100%; }
  100% { background-position: 0% 50%; }
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0.75rem 0.75rem 0;
  position: relative;
  z-index: 1;
}

/* ── canvas layers ── */
.firefly-canvas {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
}

.viz-canvas {
  position: fixed;
  z-index: 50;
  pointer-events: none;
  border-radius: 50%;
}
.viz-canvas[hidden] {
  display: none;
}

/* ── header ── */
.header {
  text-align: center;
  padding: 1.5rem 1rem 1.25rem;
}

.header-title {
  font-size: 2.5rem;
  font-weight: 800;
  color: var(--color-primary);
  letter-spacing: -0.02em;
  line-height: 1.1;
}

.header-tagline {
  font-size: 1.05rem;
  color: var(--color-text-muted);
  margin-top: 0.35rem;
}

.install-btn {
  margin-top: 1rem;
  padding: 0.6rem 1.4rem;
  font-size: 0.95rem;
  font-weight: 600;
  color: #fff;
  background: var(--color-primary);
  border: none;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.2s;
}
.install-btn:hover { background: var(--color-primary-hover); }
.install-btn[hidden] { display: none; }

/* ── novel grid ── */
.novels-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 1rem;
  padding: 0.5rem 0 1.25rem;
}

.empty-state {
  text-align: center;
  color: var(--color-text-muted);
  font-size: 1.15rem;
  padding: 4rem 1rem;
}
.empty-state[hidden] { display: none; }

/* ── novel card ── */
.novel-card {
  background: var(--color-surface);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  cursor: pointer;
  transition: transform 0.4s cubic-bezier(0.16, 1, 0.3, 1), box-shadow 0.2s;
  -webkit-tap-highlight-color: transparent;
  position: relative;
  transform-style: preserve-3d;
}

.novel-card::after {
  content: '';
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  background: radial-gradient(circle at var(--glare-x, 50%) var(--glare-y, 50%), rgba(255,255,255,0.25) 0%, transparent 60%);
  opacity: 0;
  transition: opacity 0.2s;
  border-radius: var(--radius);
}
.novel-card.tilted::after {
  opacity: 1;
}

.novel-card:hover {
  transform: translateY(-3px);
  box-shadow: var(--shadow-md);
}
.novel-card.tilted:hover {
  /* tilt handles transform */
}
.novel-card:active {
  transform: translateY(-1px);
}

.novel-card-image-wrapper {
  position: relative;
  aspect-ratio: 1;
  background: #F3E8D8;
  overflow: hidden;
}

.novel-card-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.novel-card-gradient {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.novel-card-emoji {
  font-size: 68px;
  line-height: 1;
  filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.2));
  transition: transform 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
  user-select: none;
}

.novel-card:hover .novel-card-emoji {
  transform: scale(1.15);
}

.novel-card.playing .novel-card-emoji {
  animation: emojiPulse 1.5s ease-in-out infinite;
}

@keyframes emojiPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.08); }
}

.novel-card-play-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.18);
  opacity: 0;
  transition: opacity 0.2s;
}
.novel-card:hover .novel-card-play-overlay,
.novel-card.playing .novel-card-play-overlay {
  opacity: 1;
}
.novel-card.playing .novel-card-play-overlay {
  background: rgba(0, 0, 0, 0.35);
}

.novel-card-play-overlay svg {
  width: 48px;
  height: 48px;
  fill: #fff;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.3));
}

.overlay-icon-play[hidden],
.overlay-icon-pause[hidden] {
  display: none;
}

.novel-card-body {
  padding: 0.85rem 1rem 1rem;
}

.novel-card-title {
  font-size: 1.05rem;
  font-weight: 700;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.novel-card-author {
  font-size: 0.88rem;
  color: var(--color-text-muted);
  margin-top: 0.25rem;
}

.novel-card-desc {
  font-size: 0.82rem;
  color: var(--color-text-muted);
  margin-top: 0.4rem;
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.novel-card-read-btn {
  display: block;
  width: 100%;
  margin-top: 0.6rem;
  padding: 0.45rem 0;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--color-primary);
  background: transparent;
  border: 1.5px solid var(--color-primary);
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}
.novel-card-read-btn:hover {
  background: var(--color-primary);
  color: #fff;
}
.novel-card-read-btn:active {
  background: var(--color-primary-hover);
  color: #fff;
}

/* ── player ── */
.player {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--player-height);
  background: var(--color-player-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid rgba(0, 0, 0, 0.06);
  box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.06);
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0 1rem;
  padding-bottom: env(safe-area-inset-bottom, 0px);
  z-index: 100;
  transform: translateY(100%);
  transition: transform 0.3s ease;
}
.player.visible {
  transform: translateY(0);
}
.player[hidden] { display: none; }

.player-cover {
  width: 48px;
  height: 48px;
  border-radius: var(--radius-sm);
  object-fit: cover;
  flex-shrink: 0;
  background: #F3E8D8;
}

.player-cover-emoji {
  width: 48px;
  height: 48px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 26px;
  line-height: 1;
  flex-shrink: 0;
}
.player-cover-emoji[hidden] {
  display: none;
}

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

.player-title {
  font-size: 0.9rem;
  font-weight: 700;
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.player-btn {
  background: none;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  color: var(--color-text);
  transition: background 0.15s;
  flex-shrink: 0;
}
.player-btn:hover { background: rgba(0, 0, 0, 0.06); }
.player-btn:active { background: rgba(0, 0, 0, 0.1); }

.player-btn-play {
  width: 44px;
  height: 44px;
  background: var(--color-primary);
  color: #fff;
}
.player-btn-play:hover {
  background: var(--color-primary-hover);
}
.player-btn-play:active {
  background: #B45309;
}

.player-icon-pause { display: none; }
.player-btn-play.playing .player-icon-play { display: none; }
.player-btn-play.playing .player-icon-pause { display: block; }

.player-progress-container {
  flex: 1;
  min-width: 60px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.player-progress {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 6px;
  background: #E5D5C0;
  border-radius: 3px;
  outline: none;
  cursor: pointer;
}
.player-progress::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--color-primary);
  border: 3px solid #fff;
  box-shadow: var(--shadow-sm);
  cursor: pointer;
  transition: transform 0.1s;
}
.player-progress::-webkit-slider-thumb:active {
  transform: scale(1.2);
}
.player-progress::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--color-primary);
  border: 3px solid #fff;
  box-shadow: var(--shadow-sm);
  cursor: pointer;
}

.player-time {
  font-size: 0.72rem;
  color: var(--color-text-muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.player-btn-close {
  color: var(--color-text-muted);
}

/* ── story modal ── */
.story-modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  opacity: 0;
  transition: opacity 0.3s;
}
.story-modal-overlay.visible {
  opacity: 1;
}
.story-modal-overlay[hidden] {
  display: none;
}

.story-modal {
  position: relative;
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
  max-width: 600px;
  width: 100%;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.story-modal-close {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  z-index: 10;
  background: var(--color-bg);
  border: none;
  border-radius: 50%;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--color-text-muted);
  transition: background 0.15s, color 0.15s;
}
.story-modal-close:hover {
  background: #e5d5c0;
  color: var(--color-text);
}

.story-modal-header {
  padding: 1.5rem 1.5rem 0.5rem;
  text-align: center;
}

.story-modal-title {
  font-size: 1.4rem;
  font-weight: 800;
  color: var(--color-primary);
}

.story-modal-author {
  font-size: 0.9rem;
  color: var(--color-text-muted);
  margin-top: 0.25rem;
}

.story-modal-text {
  padding: 0.5rem 1.5rem 1.5rem;
  font-size: 1rem;
  line-height: 1.8;
  color: var(--color-text);
  overflow-y: auto;
}
.story-modal-text p {
  margin-bottom: 1rem;
}
.story-modal-text p:last-child {
  margin-bottom: 0;
}

/* ── responsive ── */
@media (max-width: 480px) {
  .container { padding: 0.5rem 0.5rem 0; }
  .header { padding: 1rem 0.75rem 0.75rem; }
  .header-title { font-size: 1.75rem; }
  .novels-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 0.7rem;
    padding: 0.25rem 0 1rem;
  }
  .novel-card-body { padding: 0.7rem 0.8rem 0.8rem; }
  .novel-card-title { font-size: 0.92rem; }
  .novel-card-author { font-size: 0.78rem; }
  .novel-card-desc { font-size: 0.72rem; -webkit-line-clamp: 2; }
  .novel-card-emoji { font-size: 48px; }
  .player {
    padding: 0 0.5rem;
    gap: 0.35rem;
  }
  .player-cover { width: 40px; height: 40px; }
  .player-cover-emoji { width: 40px; height: 40px; font-size: 22px; }
  .player-title { font-size: 0.8rem; }
  .player-btn { width: 34px; height: 34px; }
  .player-btn-play { width: 38px; height: 38px; }
  .player-time { font-size: 0.65rem; }
}

@media (max-width: 380px) {
  .novels-grid {
    grid-template-columns: 1fr;
  }
  .novel-card-emoji { font-size: 56px; }
  .novel-card-body { padding: 0.8rem 1rem 0.9rem; }
  .novel-card-title { font-size: 1rem; }
  .novel-card-desc { -webkit-line-clamp: 3; }
  .story-modal { max-height: 90vh; }
  .story-modal-header { padding: 1.25rem 1.25rem 0.5rem; }
  .story-modal-title { font-size: 1.2rem; }
  .story-modal-text { padding: 0.5rem 1.25rem 1.25rem; font-size: 0.92rem; }
}

@media (min-width: 768px) {
  .container { padding: 1.5rem 2rem 0; }
  .player { justify-content: center; }
}
