:root {
  color-scheme: light dark;
  --bg: #f7f6f3;
  --surface: #ffffff;
  --text: #1a1a1a;
  --text-muted: #5c5c5c;
  --border: #e2e0db;
  --accent: #2c5f4a;
  --accent-hover: #234a3a;
  --accent-soft: rgba(44, 95, 74, 0.12);
  --danger: #b33a3a;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
  --radius: 12px;
  --header-h: 56px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121412;
    --surface: #1c1f1c;
    --text: #f0f0ec;
    --text-muted: #a8a8a0;
    --border: #2e322e;
    --accent: #5daa84;
    --accent-hover: #7bc49e;
    --accent-soft: rgba(93, 170, 132, 0.15);
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
  }
}

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

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

button,
input {
  font: inherit;
}

button {
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

.app {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.boot-status {
  padding: 48px 24px;
  text-align: center;
  color: var(--text-muted);
}

/* Header */

.header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 0;
  min-height: calc(var(--header-h) + var(--safe-top));
  padding: calc(8px + var(--safe-top)) calc(16px + var(--safe-right)) 8px calc(16px + var(--safe-left));
  background: var(--bg);
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}

.header__main {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
}

.header__title {
  flex: 1;
  margin: 0;
  font-size: 1.125rem;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.header__actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.header-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-height: 36px;
  padding: 0 10px;
  border-radius: 8px;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--accent);
  background: transparent;
  border: 1px solid var(--border);
}

.header-btn svg {
  width: 16px;
  height: 16px;
}

.header-btn:hover,
.header-btn:focus-visible {
  background: var(--accent-soft);
  outline: none;
}

.header-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.header__progress {
  width: 100%;
  height: 3px;
  margin-top: 8px;
  border-radius: 999px;
  background: var(--border);
  overflow: hidden;
}

.header__progress-bar {
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width 0.2s ease;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  border-radius: 10px;
  color: var(--text);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.icon-btn:hover,
.icon-btn:focus-visible {
  background: var(--accent-soft);
  outline: none;
}

.icon-btn svg {
  width: 22px;
  height: 22px;
  pointer-events: none;
}

/* Status badge */

.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-muted);
  background: var(--surface);
  border: 1px solid var(--border);
}

.status-badge--ready {
  color: var(--accent);
  border-color: color-mix(in srgb, var(--accent) 35%, var(--border));
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: currentColor;
}

/* Search */

.search-wrap {
  margin: 16px 0px;
  padding: 0 calc(16px + var(--safe-right)) 12px calc(16px + var(--safe-left));
}

.search {
  width: 100%;
  min-height: 44px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
}

.search:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* List */

.list-view {
  flex: 1;
  padding: 0 calc(16px + var(--safe-right)) calc(24px + var(--safe-bottom)) calc(16px + var(--safe-left));
}

.psalm-group {
  margin-bottom: 20px;
}

.psalm-group__title {
  margin: 0 0 8px;
  font-size: 0.8125rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

.psalm-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 8px;
}

.psalm-item {
  margin: 0;
}

.psalm-link {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 48px;
  padding: 12px 16px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  text-decoration: none;
  box-shadow: var(--shadow);
  transition: border-color 0.15s, transform 0.1s;
}

.psalm-link:hover,
.psalm-link:focus-visible {
  border-color: var(--accent);
  outline: none;
}

.psalm-link:active {
  transform: scale(0.99);
}

.psalm-link__label {
  font-size: 1rem;
  font-weight: 500;
}

.psalm-link__meta {
  font-size: 0.875rem;
  color: var(--text-muted);
}

.empty-state {
  padding: 48px 16px;
  text-align: center;
  color: var(--text-muted);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 16px;
  border-radius: 10px;
  font-size: 0.9375rem;
  font-weight: 500;
  background: var(--accent);
  color: #fff;
}

.btn svg {
  width: 18px;
  height: 18px;
}

.btn:hover,
.btn:focus-visible {
  background: var(--accent-hover);
  outline: none;
}

.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn--ghost {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--border);
}

.progress {
  margin-top: 10px;
  height: 6px;
  border-radius: 999px;
  background: var(--border);
  overflow: hidden;
}

.progress__bar {
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width 0.2s ease;
}

/* Sing view */

.sing-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
}

.sing-controls {
  flex-shrink: 0;
  position: relative;
  z-index: 2;
  border-top: 1px solid var(--border);
  background: var(--bg);
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(10px);
  padding: 8px calc(8px + var(--safe-right)) calc(8px + var(--safe-bottom)) calc(8px + var(--safe-left));
}

/* Offline save (header button uses .header-btn) */

.sing-controls__row {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 4px;
}

.sing-controls__back {
  justify-self: start;
}

.sing-controls__transport {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
}

.sing-controls__pdf {
  justify-self: end;
}

.sing-controls__parts {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  margin-top: 8px;
  padding: 0 4px;
}

.part-toggle {
  min-width: 36px;
  min-height: 32px;
  padding: 0 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  font-size: 0.8125rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  transition: background 0.15s, color 0.15s, border-color 0.15s, opacity 0.15s;
}

.part-toggle--active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.part-toggle:not(.part-toggle--active) {
  opacity: 0.55;
}

.part-toggle:hover,
.part-toggle:focus-visible {
  outline: none;
  border-color: var(--accent);
}

.part-toggle--active:hover,
.part-toggle--active:focus-visible {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.sing-controls__play {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  min-width: 44px;
  min-height: 44px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.sing-controls__play:hover,
.sing-controls__play:focus-visible {
  background: var(--accent-hover);
  outline: none;
}

.sing-controls__play svg {
  width: 20px;
  height: 20px;
  pointer-events: none;
}

.sing-controls__progress {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 6px;
  padding: 0 4px;
}

.sing-controls__time {
  flex-shrink: 0;
  font-size: 0.6875rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.sing-controls__track {
  flex: 1;
  min-width: 0;
  height: 3px;
  border-radius: 999px;
  background: var(--border);
  overflow: hidden;
}

.sing-controls__fill {
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width 0.1s linear;
}

#open-pdf {
  text-decoration: none;
  color: var(--text);
}

.pdf-stage {
  flex: 1 1 0;
  min-height: 0;
  position: relative;
  background: var(--surface);
  overflow: hidden;
}

.pdf-scroll {
  width: 100%;
  height: 100%;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.sheet-img {
  display: block;
  width: 100%;
  height: auto;
}

.sheet-img + .sheet-img {
  margin-top: 8px;
}

/* Desktop: keep sheet readable width; scrolling stays inside pdf-stage. */
@media (min-width: 768px) and (hover: hover) {
  .pdf-scroll {
    padding: 12px 16px;
  }

  .sheet-img {
    width: auto;
    max-width: min(100%, 520px);
    margin: 0 auto;
  }
}

.sheet-missing {
  padding: 24px 16px;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.875rem;
}

.sheet-missing code {
  font-size: 0.8125rem;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
