:root {
  --bg: #0e0e0e;
  --surface: #1a1a1a;
  --surface2: #242424;
  --border: #2e2e2e;
  --accent: #c8941a;
  --accent-dim: #8a6210;
  --text: #f0f0f0;
  --text-muted: #888;
  --text-dim: #555;
  --player-h: 80px;
  --nav-h: 56px;
  --radius: 12px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body { height: 100%; overflow: hidden; background: var(--bg); color: var(--text); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }

#app { display: flex; flex-direction: column; height: 100vh; height: -webkit-fill-available; }

/* NAV BAR */
#nav-bar {
  display: flex; align-items: center; gap: 8px;
  padding: calc(var(--safe-top) + 10px) 12px 10px;
  background: rgba(14,14,14,0.95);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  min-height: var(--nav-h);
  z-index: 100; flex-shrink: 0;
}
#nav-title-wrap { flex: 1; min-width: 0; }
#nav-title { font-size: 17px; font-weight: 700; display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#nav-subtitle { font-size: 12px; color: var(--text-muted); display: block; }
.nav-btn {
  width: 36px; height: 36px; border: none; background: var(--surface2); border-radius: 50%;
  color: var(--text); cursor: pointer; display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.nav-btn svg { width: 18px; height: 18px; }
.nav-btn:active { opacity: 0.7; }
.download-btn { background: var(--accent); color: #000; }
.download-btn.offline { background: #2a5c2a; color: #6dbd6d; }

/* SCREENS */
.screen {
  display: none; flex: 1; overflow-y: auto; overflow-x: hidden;
  padding-bottom: calc(var(--player-h) + 8px + var(--safe-bottom));
  -webkit-overflow-scrolling: touch;
}
.screen.active { display: block; }

/* LIBRARY GRID */
#library-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 12px; padding: 16px;
}
.magazine-card {
  background: var(--surface); border-radius: var(--radius);
  overflow: hidden; cursor: pointer; position: relative;
  transition: transform 0.15s;
}
.magazine-card:active { transform: scale(0.96); }
.magazine-card-cover {
  width: 100%; aspect-ratio: 3/4; object-fit: cover;
  background: var(--surface2); display: block;
}
.magazine-card-cover.loading { filter: brightness(0.3); }
.magazine-card-info { padding: 8px 10px 10px; }
.magazine-card-title { font-size: 13px; font-weight: 700; line-height: 1.3; }
.magazine-card-date { font-size: 11px; color: var(--text-muted); margin-top: 2px; }
.magazine-card-count { font-size: 11px; color: var(--text-dim); margin-top: 4px; }
.offline-badge {
  position: absolute; top: 8px; right: 8px;
  background: rgba(0,0,0,0.7); border-radius: 50%;
  width: 26px; height: 26px; display: flex; align-items: center; justify-content: center;
}
.offline-badge svg { width: 14px; height: 14px; color: #6dbd6d; }

/* ISSUE SCREEN */
#issue-hero {
  display: flex; gap: 16px; padding: 16px;
  border-bottom: 1px solid var(--border);
}
#issue-cover {
  width: 120px; height: 160px; object-fit: cover;
  border-radius: 8px; flex-shrink: 0; background: var(--surface2);
}
#issue-meta { display: flex; flex-direction: column; justify-content: center; gap: 6px; }
#issue-title-large { font-size: 18px; font-weight: 800; line-height: 1.2; }
#issue-date-large { font-size: 14px; color: var(--accent); font-weight: 600; }
#issue-stats { font-size: 12px; color: var(--text-muted); }

/* ARTICLE LIST */
.article-row {
  display: flex; gap: 12px; padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer; align-items: center;
  transition: background 0.1s;
}
.article-row:active { background: var(--surface); }
.article-row.playing { background: rgba(200,148,26,0.08); }
.article-row.playing .article-row-title { color: var(--accent); }
.article-thumb {
  width: 72px; height: 96px; object-fit: cover;
  border-radius: 6px; flex-shrink: 0; background: var(--surface2);
}
.article-row-meta { flex: 1; min-width: 0; }
.article-row-num { font-size: 11px; color: var(--text-dim); margin-bottom: 4px; }
.article-row-title { font-size: 14px; font-weight: 700; line-height: 1.3; }
.article-row-author { font-size: 12px; color: var(--text-muted); margin-top: 3px; }
.article-row-duration { font-size: 11px; color: var(--text-dim); margin-top: 4px; }
.article-row-playing-icon { flex-shrink: 0; }
.article-row-playing-icon svg { width: 20px; height: 20px; color: var(--accent); }

/* ARTICLE PLAYER SCREEN */
#article-page-image-wrap {
  padding: 16px 16px 8px;
  display: flex; justify-content: center;
}
#article-page-image {
  max-width: 100%; max-height: 50vh; object-fit: contain;
  border-radius: 8px; box-shadow: 0 4px 20px rgba(0,0,0,0.5);
  background: var(--surface2);
}
#article-info { padding: 12px 16px; }
#article-title-display { font-size: 18px; font-weight: 800; line-height: 1.3; }
#article-author-display { font-size: 14px; color: var(--text-muted); margin-top: 6px; }

/* PLAYER BAR */
#player-bar {
  position: fixed; bottom: 0; left: 0; right: 0;
  background: rgba(26,26,26,0.97);
  backdrop-filter: blur(16px);
  border-top: 1px solid var(--border);
  padding: 8px 12px calc(8px + var(--safe-bottom));
  z-index: 200; display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
}
#player-cover-wrap { flex-shrink: 0; }
#player-cover {
  width: 44px; height: 58px; object-fit: cover;
  border-radius: 4px; background: var(--surface2);
}
#player-info { flex: 1; min-width: 0; }
#player-article-title { font-size: 13px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#player-magazine-title { font-size: 11px; color: var(--text-muted); margin-top: 2px; }
#player-controls { display: flex; gap: 4px; align-items: center; }
.ctrl-btn {
  background: none; border: none; color: var(--text); cursor: pointer;
  width: 38px; height: 38px; display: flex; align-items: center; justify-content: center;
  border-radius: 50%;
}
.ctrl-btn svg { width: 22px; height: 22px; }
.ctrl-btn:active { background: var(--surface2); }
.play-btn { background: var(--accent); color: #000; width: 44px; height: 44px; }
.play-btn svg { width: 24px; height: 24px; }
.play-btn:active { background: var(--accent-dim); }
#player-progress-wrap { width: 100%; display: flex; flex-direction: column; gap: 2px; }
#player-scrubber {
  width: 100%; height: 3px; -webkit-appearance: none; appearance: none;
  background: var(--border); border-radius: 2px; outline: none; cursor: pointer;
}
#player-scrubber::-webkit-slider-thumb {
  -webkit-appearance: none; width: 14px; height: 14px;
  background: var(--accent); border-radius: 50%;
}
#player-time { display: flex; justify-content: space-between; font-size: 10px; color: var(--text-dim); }

/* DOWNLOAD OVERLAY */
#dl-overlay {
  display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.8);
  align-items: center; justify-content: center; z-index: 300;
}
#dl-overlay:not([hidden]) {
  display: flex;
}
#dl-progress-wrap { background: var(--surface); border-radius: var(--radius); padding: 24px; width: 280px; }
#dl-progress-label { font-size: 15px; font-weight: 700; margin-bottom: 16px; text-align: center; }
#dl-progress-bar-track { background: var(--border); border-radius: 4px; height: 6px; margin-bottom: 8px; }
#dl-progress-bar { background: var(--accent); height: 6px; border-radius: 4px; width: 0%; transition: width 0.2s; }
#dl-progress-bar.dl-active { background: linear-gradient(90deg, var(--accent) 60%, #fff4 100%); background-size: 200% 100%; animation: dl-pulse 1.2s linear infinite; }
@keyframes dl-pulse { 0%{background-position:100% 0} 100%{background-position:-100% 0} }
#dl-progress-count { font-size: 12px; color: var(--text-muted); text-align: center; }

/* LOADING / EMPTY */
.empty-msg { text-align: center; color: var(--text-muted); padding: 60px 20px; font-size: 15px; }
.loading-spinner {
  width: 32px; height: 32px; border: 3px solid var(--border);
  border-top-color: var(--accent); border-radius: 50%;
  animation: spin 0.8s linear infinite; margin: 60px auto;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* DOWNLOAD ALL BUTTON */
#library-actions { padding: 12px 16px 4px; }
.dl-all-btn {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; padding: 12px 16px;
  background: var(--accent); color: #000;
  border: none; border-radius: var(--radius);
  font-size: 15px; font-weight: 700; cursor: pointer;
}
.dl-all-btn svg { width: 18px; height: 18px; }
.dl-all-btn:active { background: var(--accent-dim); }
.dl-all-btn:disabled { background: var(--surface2); color: var(--text-muted); cursor: default; }

/* iPad / larger screens */
@media (min-width: 768px) {
  #library-grid { grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 16px; padding: 20px; }
  #issue-cover { width: 150px; height: 200px; }
  #issue-title-large { font-size: 22px; }
  #player-bar { flex-wrap: nowrap; }
  #player-progress-wrap { width: auto; flex: 1; }
}
