/* ==========================================================================
   Home dashboard + Stats page
   Grimmory-parity surfaces built on the shared tokens. Everything here is
   scoped under `.home-*` / `.stats-*` so it never collides with the library
   grid. Chart colours are exposed as `--chart-1..6` and reused by both the
   CSS bar lists and the SVG donut.
   ========================================================================== */

:root {
  --chart-1: var(--color-primary-500);
  --chart-2: var(--color-sky-600);
  --chart-3: var(--color-green-500);
  --chart-4: var(--color-amber-400);
  --chart-5: var(--color-orange-900);
  --chart-6: var(--color-surface-400);
}

.dark {
  --chart-1: var(--color-primary-400);
  --chart-2: var(--color-sky-400);
  --chart-3: var(--color-green-500);
  --chart-4: var(--color-amber-400);
  --chart-5: var(--color-primary-700);
  --chart-6: var(--color-neutral-500);
}

/* ---- Shared section scaffolding ----------------------------------------- */

.home-section {
  margin-top: var(--space-6);
}

.home-section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin-bottom: var(--space-3);
}

.home-section-title {
  display: flex;
  align-items: baseline;
  gap: var(--space-2);
  font-size: 1.0625rem;
  font-weight: var(--weight-semibold);
  color: var(--color-text-strong);
  letter-spacing: -0.01em;
}

.home-section-sub {
  font-size: var(--app-text-xs);
  font-weight: var(--weight-regular);
  color: var(--color-text-muted);
}

/* ---- Header ------------------------------------------------------------- */

.home-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--space-4);
  flex-wrap: wrap;
  margin-bottom: var(--space-2);
}

.home-header-main {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.home-welcome {
  font-size: var(--app-text-sm);
  color: var(--color-text-secondary);
}

.home-header-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

/* ---- Horizontal scrollers ----------------------------------------------- */

.home-scroller {
  position: relative;
}

.home-scroller-wrap {
  position: relative;
}

.home-scroller-track {
  display: flex;
  gap: var(--space-3);
  overflow-x: auto;
  overflow-y: hidden;
  scroll-snap-type: x mandatory;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  padding-block: 4px;
  padding-inline: 2px;
  scrollbar-width: thin;
}

.home-scroller-track > * {
  flex: 0 0 var(--home-card-width, 148px);
  width: var(--home-card-width, 148px);
  scroll-snap-align: start;
}

/* Cards reuse `.book-card`; make anchors/buttons behave like the grid cards. */
.home-card {
  color: inherit;
  text-align: left;
}

.home-continue-card {
  position: relative;
}

/* Overlay label sits above the cover's progress bar. */
.home-continue-badge {
  position: absolute;
  left: 6px;
  right: 6px;
  bottom: 8px;
  z-index: 4;
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 3px 7px;
  border-radius: var(--radius-pill);
  background: rgb(0 0 0 / 62%);
  color: #fff;
  font-size: 10.5px;
  font-weight: 600;
  line-height: 1.2;
  pointer-events: none;
  backdrop-filter: blur(2px);
}

.home-continue-badge svg {
  width: 12px;
  height: 12px;
  flex-shrink: 0;
}

/* Desktop-only scroll arrows. Touch users swipe instead. */
.home-scroller-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  z-index: 6;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 1px solid var(--color-border);
  background: var(--color-card);
  color: var(--color-text-secondary);
  box-shadow: var(--shadow-pop);
  transition: background var(--duration-micro), color var(--duration-micro), opacity var(--duration-micro);
}

.home-scroller-btn:hover {
  background: var(--color-surface-hover);
  color: var(--color-text);
}

.home-scroller-btn--prev {
  left: -12px;
}

.home-scroller-btn--next {
  right: -12px;
}

.home-scroller-btn:disabled {
  opacity: 0;
  pointer-events: none;
}

@media (hover: none), (pointer: coarse) {
  .home-scroller-btn {
    display: none;
  }
}

.home-row-empty {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-4);
  border: 1px dashed var(--color-border);
  border-radius: var(--radius-md);
  color: var(--color-text-muted);
  font-size: var(--app-text-sm);
}

/* ---- Your Libraries ----------------------------------------------------- */

.home-libraries-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: var(--space-3);
}

.home-library-card {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  background: var(--color-card);
  color: inherit;
  transition: border-color var(--duration-micro), background var(--duration-micro),
    box-shadow var(--duration-micro);
}

.home-library-card:hover {
  border-color: color-mix(in srgb, var(--color-primary) 40%, var(--color-border));
  box-shadow: var(--shadow-control);
}

.home-library-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 42px;
  height: 42px;
  flex-shrink: 0;
  border-radius: var(--radius-md);
  background: var(--color-active-surface);
  color: var(--color-primary-text);
}

.home-library-body {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
  flex: 1;
}

.home-library-name {
  font-size: var(--app-text-sm);
  font-weight: var(--weight-semibold);
  color: var(--color-text-strong);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.home-library-count {
  font-size: var(--app-text-xs);
  color: var(--color-text-muted);
}

.home-library-mosaic {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(2, 1fr);
  gap: 2px;
  width: 52px;
  height: 52px;
  flex-shrink: 0;
  border-radius: var(--radius-sm);
  overflow: hidden;
}

.home-library-tile {
  background-size: cover;
  background-position: center;
  background-color: color-mix(in srgb, var(--color-text) 8%, var(--color-card));
  display: flex;
  align-items: center;
  justify-content: center;
  color: color-mix(in srgb, var(--color-text) 35%, transparent);
}

.home-library-tile svg {
  width: 12px;
  height: 12px;
}

.home-library-tile img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* ---- Reading snapshot --------------------------------------------------- */

.home-snapshot {
  display: flex;
  flex-wrap: wrap;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  background: var(--color-card);
  overflow: hidden;
}

.home-snapshot-item {
  flex: 1 1 150px;
  min-width: 130px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: var(--space-3) var(--space-4);
  border-right: 1px solid var(--color-border);
  color: inherit;
  transition: background var(--duration-micro);
}

.home-snapshot-item:last-child {
  border-right: none;
}

.home-snapshot-item:hover {
  background: var(--color-surface-hover);
}

.home-snapshot-value {
  font-size: 1.5rem;
  font-weight: var(--weight-bold);
  line-height: 1.1;
  color: var(--color-text-strong);
  font-variant-numeric: tabular-nums;
}

.home-snapshot-label {
  font-size: var(--app-text-xs);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: var(--weight-medium);
  color: var(--color-text-muted);
}

.home-snapshot-sub {
  font-size: var(--app-text-xs);
  color: var(--color-text-secondary);
}

/* ---- Customize dialog --------------------------------------------------- */

.home-customize-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.home-customize-row {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-card);
}

.home-customize-row.is-hidden {
  opacity: 0.62;
}

.home-customize-title {
  flex: 1;
  min-width: 0;
  font-size: var(--app-text-sm);
  font-weight: var(--weight-medium);
  color: var(--color-text);
}

.home-customize-order {
  display: inline-flex;
  gap: 2px;
}

.home-customize-order .icon-button {
  width: 28px;
  height: 28px;
}

.home-customize-order .icon-button:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

/* ==========================================================================
   Stats page
   ========================================================================== */

.stats-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--space-3);
  flex-wrap: wrap;
  margin-bottom: var(--space-4);
}

.stats-kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: var(--space-3);
}

.stats-kpi {
  display: flex;
  gap: var(--space-3);
  align-items: flex-start;
}

.stats-kpi-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  flex-shrink: 0;
  border-radius: var(--radius-md);
  background: var(--color-active-surface);
  color: var(--color-primary-text);
}

.stats-kpi-body {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}

.stats-panels {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: var(--space-4);
  margin-top: var(--space-4);
}

.stats-panel {
  padding: var(--space-4);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  background: var(--color-card);
  min-width: 0;
}

.stats-panel--wide {
  grid-column: 1 / -1;
}

.stats-panel-title {
  font-size: var(--app-text-sm);
  font-weight: var(--weight-semibold);
  color: var(--color-text-strong);
  margin-bottom: var(--space-3);
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-2);
}

.stats-panel-title span {
  font-weight: var(--weight-regular);
  color: var(--color-text-muted);
  font-size: var(--app-text-xs);
}

/* ---- Horizontal bar lists ----------------------------------------------- */

.stats-bar-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.stats-bar-row {
  display: grid;
  grid-template-columns: minmax(58px, 84px) 1fr auto;
  align-items: center;
  gap: var(--space-2);
}

.stats-bar-label {
  font-size: var(--app-text-xs);
  font-weight: var(--weight-medium);
  color: var(--color-text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.stats-bar-track {
  height: 8px;
  border-radius: var(--radius-pill);
  background: color-mix(in srgb, var(--color-text) 8%, transparent);
  overflow: hidden;
}

.stats-bar-fill {
  height: 100%;
  min-width: 3px;
  border-radius: var(--radius-pill);
  background: var(--bar-color, var(--chart-1));
  transition: width var(--duration-layout) var(--ease-glide);
}

.stats-bar-value {
  font-size: var(--app-text-xs);
  color: var(--color-text-muted);
  text-align: right;
  font-variant-numeric: tabular-nums;
  min-width: 3ch;
}

/* ---- Donut (By Status) -------------------------------------------------- */

.stats-donut-wrap {
  display: flex;
  align-items: center;
  gap: var(--space-4);
  flex-wrap: wrap;
}

.stats-donut {
  width: 132px;
  height: 132px;
  flex-shrink: 0;
}

.stats-donut circle {
  fill: none;
  stroke-width: 16;
}

.stats-donut-center {
  font-size: 20px;
  font-weight: var(--weight-bold);
  fill: var(--color-text-strong);
}

.stats-donut-center-sub {
  font-size: 9px;
  font-weight: var(--weight-medium);
  fill: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.stats-legend {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

.stats-legend-item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--app-text-xs);
  color: var(--color-text-secondary);
}

.stats-legend-swatch {
  width: 10px;
  height: 10px;
  border-radius: 3px;
  flex-shrink: 0;
}

.stats-legend-label {
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.stats-legend-value {
  font-variant-numeric: tabular-nums;
  color: var(--color-text-strong);
  font-weight: var(--weight-medium);
}

/* ---- Daily reading activity --------------------------------------------- */

.stats-activity {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.stats-activity-bars {
  display: flex;
  align-items: flex-end;
  gap: 2px;
  height: 170px;
  padding-top: var(--space-2);
  border-bottom: 1px solid var(--color-border);
}

.stats-activity-col {
  flex: 1 1 0;
  min-width: 2px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  height: 100%;
}

.stats-activity-bar {
  width: 100%;
  border-radius: 2px 2px 0 0;
  background: linear-gradient(180deg, var(--chart-1), color-mix(in srgb, var(--chart-1) 70%, transparent));
  transition: height var(--duration-layout) var(--ease-glide);
}

.stats-activity-bar.is-empty {
  background: color-mix(in srgb, var(--color-text) 10%, transparent);
  border-radius: 1px;
}

.stats-activity-col:hover .stats-activity-bar {
  filter: brightness(1.12);
}

.stats-activity-caption {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  flex-wrap: wrap;
  font-size: var(--app-text-xs);
  color: var(--color-text-muted);
}

/* ---- Empty / loading ---------------------------------------------------- */

.stats-empty {
  padding: var(--space-5) 0;
  text-align: center;
  color: var(--color-text-muted);
  font-size: var(--app-text-sm);
}

/* ---- Responsive --------------------------------------------------------- */

@media (max-width: 599px) {
  .home-scroller-track {
    --home-card-width: 132px;
    gap: var(--space-2);
  }

  .home-libraries-grid {
    grid-template-columns: 1fr;
  }

  .home-snapshot-item {
    flex-basis: 50%;
    border-right: none;
    border-bottom: 1px solid var(--color-border);
  }

  .home-snapshot-item:nth-child(odd) {
    border-right: 1px solid var(--color-border);
  }

  .home-snapshot-item:nth-last-child(-n + 2) {
    border-bottom: none;
  }

  .stats-activity-bars {
    height: 130px;
  }
}
