/* =========================================================
   AudioEase — WebView app
   Simple audiobook player. Mobile-first, large touch targets.
   ========================================================= */
:root {
  --black: #1d1d1f;
  --gray-600: #6e6e73;
  --gray-400: #a1a1a6;
  --gray-200: #d2d2d7;
  --gray-100: #f5f5f7;
  --white: #fff;
  --amber: #f59e0b;
  --amber-dark: #b45309;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --tabbar-h: 66px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  font-family: var(--font); color: var(--black); background: var(--gray-100);
  font-size: 17px; -webkit-font-smoothing: antialiased; overflow: hidden;
}
button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }
img { display: block; max-width: 100%; }
code { background: rgba(0,0,0,.06); padding: 1px 6px; border-radius: 6px; font-size: .85em; }

#app {
  position: relative; width: 100%; max-width: 480px; height: 100%; margin: 0 auto;
  background: var(--white); overflow: hidden;
  box-shadow: 0 0 60px rgba(0,0,0,.08);
}

/* ---------- Views ---------- */
.view { position: absolute; inset: 0 0 var(--tabbar-h) 0; display: none; flex-direction: column; }
.view.is-active { display: flex; }
.view-body { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 16px 18px calc(24px + var(--safe-b)); }

/* ---------- App bar ---------- */
.appbar {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; justify-content: space-between;
  min-height: 56px; padding: 12px 18px; background: rgba(255,255,255,.85);
  backdrop-filter: saturate(180%) blur(16px); -webkit-backdrop-filter: saturate(180%) blur(16px);
  border-bottom: 1px solid rgba(0,0,0,.07);
}
.appbar-brand { display: flex; align-items: center; gap: 9px; font-weight: 700; font-size: 19px; }
.appbar-title { font-size: 22px; font-weight: 700; letter-spacing: -.01em; }

/* ---------- Home ---------- */
.greeting { font-size: 28px; font-weight: 700; letter-spacing: -.02em; margin-bottom: 18px; }
.section-label { font-size: 14px; font-weight: 600; color: var(--gray-600); margin: 18px 0 12px; }

.continue-card {
  display: flex; align-items: center; gap: 14px; width: 100%; text-align: left;
  padding: 14px; border-radius: 18px;
  background: linear-gradient(135deg, rgba(245,158,11,.14), rgba(245,158,11,.05));
  border: 1px solid rgba(245,158,11,.28);
}
.continue-card:active { transform: scale(.99); }
.continue-cover { width: 54px; height: 72px; border-radius: 9px; overflow: hidden; flex-shrink: 0; background: linear-gradient(145deg,#1E3A8A,#0f2557); }
.continue-cover img { width: 100%; height: 100%; object-fit: cover; }
.continue-info { flex: 1; min-width: 0; }
.continue-info strong { display: block; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.continue-info > span { font-size: 13px; color: var(--gray-600); }
.continue-bar { display: block; height: 5px; background: rgba(0,0,0,.1); border-radius: 4px; margin-top: 8px; overflow: hidden; }
.continue-bar i { display: block; height: 100%; width: 0; background: var(--amber); }
.continue-play {
  width: 46px; height: 46px; border-radius: 50%; background: var(--amber); color: #fff;
  display: flex; align-items: center; justify-content: center; font-size: 17px; flex-shrink: 0;
  box-shadow: 0 6px 16px rgba(245,158,11,.4);
}

/* ---------- Book grid ---------- */
.book-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; }
.book {
  display: flex; flex-direction: column; gap: 7px; text-align: left; width: 100%; min-width: 0;
}
.book-title, .book-author { min-width: 0; overflow-wrap: break-word; }
.book:active { transform: scale(.97); }
.book-cover {
  position: relative; aspect-ratio: 2/3; border-radius: 10px; overflow: hidden;
  background: linear-gradient(145deg, var(--c1,#2563EB), var(--c2,#1E3A8A));
  box-shadow: 0 4px 14px rgba(0,0,0,.12); display: flex; align-items: flex-end;
}
.book-cover span { position: relative; z-index: 1; color: #fff; font-size: 12px; font-weight: 700; line-height: 1.15; padding: 8px; }
.book-cover img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; z-index: 2; }
.book-title { font-size: 13px; font-weight: 600; line-height: 1.2; }
.book-author { font-size: 12px; color: var(--gray-600); }

/* ---------- Search / chips ---------- */
.search {
  display: flex; align-items: center; gap: 10px; background: var(--gray-100);
  border-radius: 14px; padding: 13px 16px; margin-bottom: 14px; font-size: 16px; color: var(--gray-600);
}
.search input { flex: 1; border: none; background: none; font-size: 16px; color: var(--black); outline: none; }
.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 16px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex-shrink: 0; font-size: 14px; padding: 9px 16px; border-radius: 980px; background: var(--gray-100); color: var(--gray-600); font-weight: 600; }
.chip.is-active { background: var(--black); color: #fff; }

/* ---------- Library list ---------- */
.lib-list { display: flex; flex-direction: column; }
.lib-row { display: flex; align-items: center; gap: 8px; border-bottom: 1px solid rgba(0,0,0,.07); }
.lib-open { flex: 1; display: flex; align-items: center; gap: 14px; text-align: left; min-width: 0; padding: 12px 0; }
.lib-open:active { opacity: .6; }
.lib-cover { width: 48px; height: 64px; border-radius: 8px; overflow: hidden; flex-shrink: 0; background: linear-gradient(145deg,#2563EB,#1E3A8A); }
.lib-cover img { width: 100%; height: 100%; object-fit: cover; }
.lib-info { flex: 1; min-width: 0; }
.lib-info strong { display: block; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lib-info span { font-size: 13px; color: var(--gray-600); }
.lib-info .lib-mini-bar { display: block; height: 4px; background: rgba(0,0,0,.1); border-radius: 3px; margin-top: 6px; overflow: hidden; max-width: 180px; }
.lib-info .lib-mini-bar i { display: block; height: 100%; background: var(--amber); }
.lib-dl-btn {
  flex-shrink: 0; width: 46px; height: 46px; border-radius: 50%; font-size: 20px; font-weight: 700;
  color: var(--amber-dark); background: rgba(245,158,11,.14); display: flex; align-items: center; justify-content: center;
}
.lib-dl-btn:active { background: rgba(245,158,11,.28); }
.lib-dl-btn[disabled] { opacity: .7; font-size: 13px; }

/* Saved badge on covers */
.book-badge {
  position: absolute; z-index: 3; top: 6px; right: 6px; width: 22px; height: 22px; border-radius: 50%;
  background: var(--amber); color: #fff; font-size: 12px; font-weight: 700; display: flex; align-items: center; justify-content: center;
  box-shadow: 0 2px 6px rgba(0,0,0,.3);
}

/* Offline banner */
.offline-banner {
  position: absolute; top: 0; left: 0; right: 0; z-index: 70; text-align: center;
  background: #1d1d1f; color: #fff; font-size: 13px; font-weight: 600; padding: 8px 12px;
}
.offline-banner[hidden] { display: none; }

/* ---------- Empty state ---------- */
.empty-state { text-align: center; padding: 60px 24px; color: var(--gray-600); }
.empty-ico { font-size: 46px; margin-bottom: 14px; }
.empty-state h3 { font-size: 20px; color: var(--black); margin-bottom: 8px; }
.empty-state p { font-size: 15px; line-height: 1.5; }

/* ---------- Tab bar ---------- */
.tabbar {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 40; display: flex;
  height: calc(var(--tabbar-h) + var(--safe-b)); padding-bottom: var(--safe-b);
  background: rgba(255,255,255,.92); backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px);
  border-top: 1px solid rgba(0,0,0,.08);
}
.tab {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  font-size: 11px; font-weight: 600; color: var(--gray-400);
}
.tab span { font-size: 21px; line-height: 1; }
.tab.is-active { color: var(--amber-dark); }

/* ---------- Player overlay ---------- */
.player {
  position: absolute; inset: 0; z-index: 60; background: linear-gradient(180deg, #fff 0%, #fdf6ec 100%);
  display: flex; flex-direction: column; transform: translateY(100%); transition: transform .35s cubic-bezier(.22,.61,.36,1);
}
.player.is-open { transform: translateY(0); }
.player-bar { background: transparent; border-bottom: none; }
.player-bar-label { font-size: 15px; font-weight: 600; color: var(--gray-600); }
.icon-btn { width: 40px; height: 40px; border-radius: 50%; font-size: 26px; display: flex; align-items: center; justify-content: center; color: var(--gray-600); }
.icon-btn:active { background: rgba(0,0,0,.06); }

.player-body { flex: 1; display: flex; flex-direction: column; padding: 8px 28px calc(28px + var(--safe-b)); overflow-y: auto; }
.player-cover {
  width: 62%; max-width: 260px; aspect-ratio: 2/3; margin: 10px auto 24px; border-radius: 18px; overflow: hidden;
  background: linear-gradient(145deg,#1E3A8A,#0f2557); box-shadow: 0 24px 50px rgba(0,0,0,.28);
}
.player-cover img { width: 100%; height: 100%; object-fit: cover; }
.player-meta { text-align: center; margin-bottom: 22px; }
.player-meta h2 { font-size: 21px; font-weight: 700; letter-spacing: -.01em; line-height: 1.2; }
.player-meta p { font-size: 15px; color: var(--gray-600); margin-top: 4px; }

.player-progress { margin-bottom: 24px; }
#seek { -webkit-appearance: none; appearance: none; width: 100%; height: 6px; border-radius: 4px;
  background: rgba(0,0,0,.12); outline: none; }
#seek::-webkit-slider-thumb { -webkit-appearance: none; width: 20px; height: 20px; border-radius: 50%; background: var(--amber); box-shadow: 0 2px 8px rgba(245,158,11,.5); cursor: pointer; }
#seek::-moz-range-thumb { width: 20px; height: 20px; border: none; border-radius: 50%; background: var(--amber); cursor: pointer; }
.player-times { display: flex; justify-content: space-between; font-size: 13px; color: var(--gray-600); margin-top: 8px; }

.player-controls { display: flex; align-items: center; justify-content: center; gap: 28px; margin-bottom: 26px; }
.ctrl { position: relative; font-size: 26px; font-weight: 700; color: var(--black); display: flex; align-items: center; justify-content: center; }
.ctrl-num { font-size: 12px; font-weight: 700; }
.ctrl:active { opacity: .5; }
.ctrl-play {
  width: 80px; height: 80px; border-radius: 50%; background: var(--amber); color: #fff; font-size: 30px;
  box-shadow: 0 10px 26px rgba(245,158,11,.45);
}
.ctrl-play:active { transform: scale(.95); opacity: 1; }

.player-tools { display: flex; justify-content: space-around; margin-top: auto; padding-top: 16px; border-top: 1px solid rgba(0,0,0,.08); }
.tool { display: flex; flex-direction: column; align-items: center; gap: 3px; color: var(--gray-600); font-size: 12px; padding: 6px 14px; border-radius: 12px; }
.tool b { font-size: 16px; font-weight: 700; color: var(--black); }
.tool small { font-size: 11px; }
.tool:active { background: rgba(0,0,0,.05); }

/* ---------- Sheets ---------- */
.scrim { position: absolute; inset: 0; z-index: 80; background: rgba(0,0,0,.4); }
.sheet {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 90; max-height: 72%;
  background: #fff; border-radius: 22px 22px 0 0; padding: 8px 20px calc(24px + var(--safe-b));
  display: flex; flex-direction: column; box-shadow: 0 -10px 40px rgba(0,0,0,.2);
  animation: sheetUp .3s cubic-bezier(.22,.61,.36,1);
}
.sheet[hidden] { display: none; }
@keyframes sheetUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
.sheet::before { content: ""; width: 40px; height: 5px; border-radius: 4px; background: var(--gray-200); margin: 8px auto 12px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.sheet-head h3 { font-size: 20px; font-weight: 700; }
.sheet-body { overflow-y: auto; }
.sheet-opt { display: block; width: 100%; text-align: left; padding: 15px 14px; border-radius: 12px; font-size: 16px; color: var(--black); }
.sheet-opt:active, .sheet-opt.is-active { background: rgba(245,158,11,.16); color: var(--amber-dark); font-weight: 600; }
.sheet-note { font-size: 13px; color: var(--gray-400); margin-top: 12px; padding: 0 4px; }

/* Chapter rows */
.chapter { display: flex; justify-content: space-between; align-items: center; width: 100%; text-align: left; padding: 14px; border-radius: 12px; font-size: 15px; }
.chapter small { color: var(--gray-400); font-size: 13px; }
.chapter.is-playing { background: rgba(245,158,11,.16); color: var(--amber-dark); font-weight: 700; }
.chapter.is-done { color: var(--gray-400); }
.chapter:active { background: rgba(0,0,0,.05); }

/* ---------- Toast ---------- */
.toast {
  position: absolute; left: 50%; bottom: calc(var(--tabbar-h) + 20px); transform: translateX(-50%);
  z-index: 120; background: rgba(29,29,31,.95); color: #fff; padding: 12px 20px; border-radius: 12px;
  font-size: 14px; max-width: 84%; text-align: center; box-shadow: 0 8px 24px rgba(0,0,0,.3);
  animation: toastIn .2s ease;
}
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 8px); } to { opacity: 1; transform: translate(-50%, 0); } }

@media (prefers-reduced-motion: reduce) {
  .player, .sheet, .toast { animation: none; transition: none; }
}
