:root {
  --bg: #faf8f4;
  --surface: #ffffff;
  --surface-2: #f2efe8;
  --text: #1f2a27;
  --muted: #6b7470;
  --border: #e4e0d8;
  --primary: #2f5d50;
  --primary-text: #ffffff;
  --accent: #b8692f;
  --danger: #b3261e;
  --warn-bg: #fff4dc;
  --warn-text: #6b4a00;
  --info-bg: #e8f1ee;
  --star: #e0a526;
  --star-off: #d9d4c9;
  --radius: 10px;
  --tabbar: 62px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141816;
    --surface: #1d2320;
    --surface-2: #252c29;
    --text: #e9ece9;
    --muted: #9aa39f;
    --border: #2f3834;
    --primary: #7fb8a4;
    --primary-text: #0f1f1a;
    --accent: #e09a62;
    --danger: #f2867d;
    --warn-bg: #3a2f14;
    --warn-text: #f3d48a;
    --info-bg: #1f2e29;
    --star-off: #3f4743;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

a { color: var(--primary); text-decoration: none; }
h1 { font-size: 1.45rem; line-height: 1.2; margin: 0 0 .75rem; }
h2 { font-size: 1.05rem; margin: 0; }
h3 { font-size: .95rem; margin: .75rem 0 .25rem; color: var(--muted); font-weight: 600; }
p { margin: .35rem 0; }

main {
  max-width: 760px;
  margin: 0 auto;
  padding: 12px 16px calc(var(--tabbar) + 24px + env(safe-area-inset-bottom));
}

/* ---------------------------------------------------------------- pasek górny */

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 1.1rem; color: var(--text); }
.back { font-size: 1.8rem; line-height: 1; padding: 0 .4rem; }

.sync {
  border-radius: 999px; padding: .2rem .7rem; font-size: .85rem; min-height: 32px;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--border);
}
.sync-ok { color: var(--primary); }
.sync-busy { animation: pulse 1s infinite alternate; }
.sync-warn { background: var(--warn-bg); color: var(--warn-text); }
.sync small { font-weight: 700; }
@keyframes pulse { to { opacity: .45; } }

.banner {
  background: var(--warn-bg); color: var(--warn-text);
  padding: .6rem 16px; text-align: center; font-size: .9rem;
}
.banner a { color: inherit; text-decoration: underline; }

/* ---------------------------------------------------------------- dolne menu */

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; justify-content: space-around;
  height: calc(var(--tabbar) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--border);
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-size: .72rem; color: var(--muted); gap: 2px;
}
.tabbar a.active { color: var(--primary); font-weight: 600; }
.tab-icon { font-size: 1.3rem; line-height: 1; }

/* ---------------------------------------------------------------- przyciski i formularze */

button, .button {
  font: inherit; font-size: .95rem;
  display: inline-flex; align-items: center; justify-content: center; gap: .35rem;
  min-height: 42px; padding: .45rem 1rem;
  border-radius: var(--radius); border: 1px solid var(--border);
  background: var(--surface); color: var(--text); cursor: pointer;
}
button:disabled { opacity: .55; cursor: default; }
.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); font-weight: 600; }
.danger { color: var(--danger); }
.big { width: 100%; min-height: 56px; font-size: 1.1rem; }
button.icon, .icon { min-height: 36px; padding: .2rem .55rem; border: none; background: none; color: var(--muted); }
button.link, .link {
  border: none; background: none; padding: .2rem 0; min-height: 0; color: var(--primary); font-size: .9rem;
}

input, select, textarea {
  font: inherit; width: 100%;
  padding: .6rem .7rem; min-height: 44px;
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text);
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus, button:focus-visible, .stars.editable:focus-visible {
  outline: 2px solid var(--primary); outline-offset: 1px;
}
input[type="checkbox"] { width: auto; min-height: 0; }

.field { display: block; margin: .65rem 0; flex: 1; }
.field-label { display: block; font-size: .82rem; color: var(--muted); margin-bottom: .2rem; }
.field-hint { display: block; font-size: .78rem; color: var(--muted); margin-top: .2rem; }
.field-row { display: flex; gap: .75rem; }
.field-row .field:has(.narrow) { flex: 0 0 6.5rem; }

.actions { display: flex; flex-wrap: wrap; gap: .5rem; margin: 1rem 0; }
.actions.sticky {
  position: sticky; bottom: calc(var(--tabbar) + env(safe-area-inset-bottom));
  background: var(--bg); padding: .6rem 0; margin-bottom: 0;
}

.chips { display: flex; gap: .4rem; overflow-x: auto; padding: .5rem 0; scrollbar-width: none; }
.chip {
  flex: none; min-height: 34px; padding: .2rem .8rem; border-radius: 999px; font-size: .88rem;
  background: var(--surface);
}
.chip.active { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.quick { display: flex; gap: .4rem; margin-top: .4rem; }

.segmented { display: flex; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.segmented button {
  flex: 1; border: none; border-radius: 0; min-height: 44px; padding: .3rem .2rem; font-size: .85rem;
  background: var(--surface);
}
.segmented button + button { border-left: 1px solid var(--border); }
.segmented button.active { background: var(--primary); color: var(--primary-text); font-weight: 600; }

/* ---------------------------------------------------------------- komunikaty */

.loading, .empty { color: var(--muted); text-align: center; padding: 2rem 1rem; }
.muted { color: var(--muted); }
.small { font-size: .8rem; }
.error { color: var(--danger); }
.warning, .info { border-radius: var(--radius); padding: .7rem .9rem; margin: .5rem 0 1rem; font-size: .92rem; }
.warning { background: var(--warn-bg); color: var(--warn-text); }
.warning a { color: inherit; text-decoration: underline; }
.info { background: var(--info-bg); }

.toasts {
  position: fixed; left: 16px; right: 16px; z-index: 20;
  bottom: calc(var(--tabbar) + 12px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; align-items: center; gap: .4rem; pointer-events: none;
}
.toast {
  max-width: 520px; padding: .6rem 1rem; border-radius: var(--radius);
  background: var(--text); color: var(--bg); font-size: .92rem; box-shadow: 0 4px 16px #0003;
}
.toast-error { background: var(--danger); color: #fff; }

.login { max-width: 420px; margin: 20vh auto 0; padding: 0 16px; text-align: center; }
.login .button { margin-top: 1.5rem; }

/* ---------------------------------------------------------------- półka */

.toolbar { position: sticky; top: calc(53px + env(safe-area-inset-top)); z-index: 5; background: var(--bg); padding-top: .25rem; }
.search { min-height: 46px; }
.toolbar-row { display: flex; gap: .4rem; padding-bottom: .4rem; }
.toolbar-row select { flex: 1; min-width: 0; font-size: .85rem; padding: .4rem .5rem; min-height: 38px; }
.active-filter { font-size: .9rem; padding: .3rem 0 .5rem; display: flex; justify-content: space-between; gap: 1rem; }
.count { color: var(--muted); font-size: .82rem; margin: .2rem 0 .4rem; }

.list { display: flex; flex-direction: column; }
.book-row {
  display: flex; gap: .8rem; align-items: flex-start; padding: .55rem 0;
  border-bottom: 1px solid var(--border); color: var(--text);
}
.book-row-main { min-width: 0; flex: 1; }
.book-title { font-weight: 600; line-height: 1.25; }
.book-authors { color: var(--muted); font-size: .9rem; }
.book-meta { display: flex; flex-wrap: wrap; align-items: center; gap: .35rem .5rem; margin-top: .25rem; font-size: .8rem; }
.series { color: var(--muted); }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 14px 10px; }
.grid-item { color: var(--text); font-size: .82rem; min-width: 0; }
.grid-title { font-weight: 600; margin-top: .3rem; line-height: 1.2; overflow-wrap: anywhere; }
.grid-authors { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.badge {
  display: inline-block; padding: .05rem .5rem; border-radius: 999px; font-size: .75rem;
  background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.badge-reading { background: #dcecf8; color: #1d4f7a; }
.badge-read { background: #e0efe4; color: #2b6a3a; }
.badge-abandoned { background: #eee; color: #777; }
.badge-loan, .badge-borrowed { background: #fbe7d7; color: #8a4513; }
@media (prefers-color-scheme: dark) {
  .badge-reading { background: #1c3446; color: #9cc8ec; }
  .badge-read { background: #1d3524; color: #9fd6ab; }
  .badge-abandoned { background: #2a2a2a; color: #999; }
  .badge-loan, .badge-borrowed { background: #3d2717; color: #f0b88a; }
}

/* ---------------------------------------------------------------- okładki */

.cover { display: block; object-fit: cover; border-radius: 4px; background: var(--surface-2); flex: none; }
.cover-sm { width: 48px; height: 72px; }
.cover-md { width: 100%; aspect-ratio: 2 / 3; height: auto; }
.cover-lg { width: 132px; height: 198px; box-shadow: 0 4px 14px #0002; }
.cover-blank {
  display: flex; align-items: center; justify-content: center; text-align: center;
  color: #fff; padding: 4px; overflow: hidden; font-weight: 600;
}
.cover-blank span { font-size: .55rem; line-height: 1.15; overflow: hidden; }
.cover-md.cover-blank span, .cover-lg.cover-blank span { font-size: .85rem; }

.cover-editor { position: relative; cursor: pointer; flex: none; }
.cover-change {
  position: absolute; left: 6px; bottom: 6px; padding: .1rem .45rem; border-radius: 6px;
  background: #000a; color: #fff; font-size: .75rem;
}

.cover-pick { display: flex; gap: .5rem; overflow-x: auto; padding: .25rem 0 .5rem; }
.cover-pick button { padding: 3px; min-height: 0; flex: none; }
.cover-pick img { width: 72px; height: 108px; object-fit: cover; display: block; border-radius: 4px; }
.cover-pick button.active { border: 2px solid var(--primary); }
.cover-pick .none { width: 80px; height: 116px; font-size: .78rem; color: var(--muted); }

/* ---------------------------------------------------------------- książka */

.book-hero { display: flex; gap: 1rem; align-items: flex-start; margin-bottom: .5rem; }
.book-head h1 { margin-bottom: .25rem; font-size: 1.3rem; }
.subtitle { color: var(--muted); }
.authors { font-weight: 500; }

.section { margin: 1.25rem 0; padding-top: .9rem; border-top: 1px solid var(--border); }
.section-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: .6rem; }

.details { display: grid; grid-template-columns: max-content 1fr; gap: .25rem 1rem; margin: 0 0 .75rem; font-size: .92rem; }
.details dt { color: var(--muted); }
.details dd { margin: 0; overflow-wrap: anywhere; }

.description p { margin: 0 0 .6rem; }
.description.clamped p:not(:first-child) { display: none; }
.description.clamped p:first-child { display: -webkit-box; -webkit-line-clamp: 6; -webkit-box-orient: vertical; overflow: hidden; }

.sessions { margin-top: .5rem; }
.session-row { display: flex; align-items: center; gap: .4rem; margin: .3rem 0; }
.session-row input { min-height: 38px; padding: .3rem .4rem; font-size: .9rem; }

.loan-current { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .75rem; }
.loan-current input { margin: .5rem 0; }
.loan-current.overdue, .loan-row.overdue .due { border-color: var(--accent); }
.overdue .due { color: var(--accent); font-weight: 600; }
.loan-form { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .25rem .75rem; }
.loan-history { margin-top: .75rem; font-size: .9rem; }
.loan-history p { display: flex; justify-content: space-between; align-items: center; }

.loan-row { display: flex; gap: .75rem; align-items: center; padding: .55rem 0; border-bottom: 1px solid var(--border); }
.loan-main { flex: 1; min-width: 0; font-size: .9rem; }
.loan-main .book-title { display: block; color: var(--text); }
.history-row { font-size: .9rem; }

.other { font-size: .92rem; display: flex; align-items: center; gap: .3rem; flex-wrap: wrap; }

/* ---------------------------------------------------------------- gwiazdki */

.stars { --star-size: 22px; position: relative; display: inline-block; line-height: 0; flex: none; touch-action: manipulation; }
.stars-sm { --star-size: 14px; }
.stars-lg { --star-size: 38px; }
.stars-base, .stars-on { display: flex; width: calc(var(--star-size) * 5); }
.stars svg { width: var(--star-size); height: var(--star-size); padding: calc(var(--star-size) * .04); }
.stars-base svg { fill: var(--star-off); }
.stars-on svg { fill: var(--star); }
.stars-fill { position: absolute; inset: 0 auto 0 0; overflow: hidden; }
.stars.editable { cursor: pointer; border-radius: 6px; }
.rating-input { display: flex; align-items: center; gap: .75rem; margin: .9rem 0 .4rem; }
.rating-value { font-size: 1.15rem; font-weight: 600; min-width: 2.5rem; }

/* ---------------------------------------------------------------- dodawanie / skaner */

.add .big { margin: .5rem 0; }
.isbn-form { display: flex; gap: .5rem; align-items: flex-end; margin: 1rem 0; }
.isbn-form button { margin-bottom: .65rem; }

.scanner-view { position: relative; border-radius: var(--radius); overflow: hidden; background: #000; aspect-ratio: 4 / 3; }
.scanner-view video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scanner-frame {
  position: absolute; left: 12%; right: 12%; top: 32%; bottom: 32%;
  border: 3px solid #fffc; border-radius: 8px; box-shadow: 0 0 0 999px #0006;
}
.scanner-actions { display: flex; flex-wrap: wrap; gap: .5rem; margin-top: .75rem; }

/* ---------------------------------------------------------------- statystyki */

.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: .6rem; margin: 1.25rem 0; }
.stats div { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .7rem .8rem; }
.stats b { display: block; font-size: 1.4rem; }
.stats span { color: var(--muted); font-size: .82rem; }

@media (min-width: 640px) {
  .stats { grid-template-columns: repeat(4, 1fr); }
  .cover-lg { width: 168px; height: 252px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); }
}
