* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  font-family: var(--font-family);
  background: var(--color-bg);
  color: var(--color-text);
  overscroll-behavior-y: contain;
}

/* Mouse/touch clicks shouldn't leave a focus ring — only real keyboard nav should. */
button:focus:not(:focus-visible),
a:focus:not(:focus-visible),
.card:focus:not(:focus-visible) {
  outline: none;
}

.feed {
  height: 100dvh;
  overflow-y: auto;
  scroll-snap-type: y mandatory;
  scrollbar-width: none;
}

.feed::-webkit-scrollbar {
  display: none;
}

.feed[hidden] {
  display: none;
}

#feed-sentinel {
  height: 1px;
  scroll-snap-align: none;
}

.card-slide {
  height: 100dvh;
  scroll-snap-align: start;
  scroll-snap-stop: always;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-lg);
  padding: var(--space-lg);
}

.card {
  width: min(100%, 420px);
  min-height: 45vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--space-xl);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  text-align: center;
  cursor: pointer;
  user-select: none;
  transition: transform 0.15s ease, border-color 0.2s ease;
}

.card:active {
  transform: scale(0.98);
}

.card-image {
  width: 100%;
  aspect-ratio: 4 / 3;
  margin-bottom: var(--space-md);
  border-radius: var(--radius-md);
  background-color: var(--color-border);
  background-size: cover;
  background-position: center;
}

.card-image[hidden] {
  display: none;
}

.card-text {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 600;
  overflow-wrap: anywhere;
}

.card-hint {
  margin: var(--space-sm) 0 0;
  font-size: 0.85rem;
  color: var(--color-text-secondary);
}

.card.is-flipped .card-hint {
  visibility: hidden;
}

.card-actions {
  display: flex;
  gap: var(--space-md);
  width: min(100%, 420px);
}

.btn {
  flex: 1;
  padding: var(--space-md);
  border: none;
  border-radius: var(--radius-md);
  font-family: inherit;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}

.btn:disabled {
  opacity: 0.5;
  cursor: default;
}

.btn-remembered {
  background: var(--color-accent);
  color: var(--color-accent-text);
}

.btn-forgot {
  background: var(--color-surface);
  color: var(--color-destructive);
  border: 1px solid var(--color-border);
}

.card-slide.is-remembered .card {
  border-color: var(--color-accent);
}

.card-slide.is-forgotten .card {
  border-color: var(--color-destructive);
}

.empty-state {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-md);
  padding: var(--space-lg);
  color: var(--color-text-secondary);
  text-align: center;
}

.empty-state[hidden] {
  display: none;
}

.empty-state .btn {
  flex: none;
  padding: var(--space-sm) var(--space-lg);
}

.empty-state-icon {
  font-size: 3rem;
  line-height: 1;
}

.feed-error {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-md);
  padding: var(--space-lg);
  color: var(--color-text-secondary);
  text-align: center;
}

.feed-error[hidden] {
  display: none;
}

.feed-error .btn {
  flex: none;
  padding: var(--space-sm) var(--space-lg);
}

/* Used instead of the full-screen layout above when cards are already
   loaded and only a background page fetch failed — the existing feed stays
   put and this becomes a small dismissible-by-retry banner on top of it. */
.feed-error.is-banner {
  position: fixed;
  top: var(--space-md);
  left: var(--space-md);
  right: var(--space-md);
  z-index: 2;
  height: auto;
  flex-direction: row;
  justify-content: space-between;
  padding: var(--space-sm) var(--space-md);
  background: var(--color-surface);
  border: 1px solid var(--color-destructive);
  border-radius: var(--radius-md);
  text-align: left;
}

.feed-error.is-banner .empty-state-icon {
  display: none;
}

.feed-error.is-banner .btn {
  padding: var(--space-xs) var(--space-md);
  font-size: 0.9rem;
}

.feed-loading {
  position: fixed;
  inset: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-bg);
  color: var(--color-text-secondary);
}

.feed-loading[hidden] {
  display: none;
}

.due-counter {
  position: fixed;
  top: var(--space-md);
  left: var(--space-md);
  z-index: 1;
  padding: var(--space-xs) var(--space-sm);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  color: var(--color-text-secondary);
  font-size: 0.85rem;
  font-weight: 600;
}

.due-counter[hidden] {
  display: none;
}

.feed-manage-link {
  position: fixed;
  top: var(--space-md);
  right: var(--space-md);
  z-index: 1;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 50%;
  color: var(--color-text);
  text-decoration: none;
  font-size: 1.1rem;
}

/* ---- Manage view (decks/cards CRUD) ---- */

.manage-view {
  max-width: 480px;
  margin: 0 auto;
  padding: var(--space-md);
  padding-bottom: var(--space-xl);
}

.manage-view[hidden] {
  display: none;
}

.topbar {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  margin-bottom: var(--space-md);
}

.topbar h1 {
  flex: 1;
  margin: 0;
  font-size: 1.1rem;
  text-align: center;
}

.btn-back {
  flex: none;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  background: none;
  border: none;
  border-radius: var(--radius-md);
  color: var(--color-link);
  font-family: inherit;
  font-size: 1.6rem;
  line-height: 1;
  cursor: pointer;
}

.btn-link {
  background: none;
  border: none;
  padding: var(--space-xs);
  color: var(--color-link);
  font-family: inherit;
  font-size: 0.95rem;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}

.btn-link-accent {
  font-weight: 600;
}

.entity-form {
  animation: list-row-in 0.28s cubic-bezier(0.22, 1, 0.36, 1) both;
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  margin-bottom: var(--space-md);
  padding: var(--space-md);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
}

.entity-form[hidden] {
  display: none;
}

.entity-form label {
  display: flex;
  flex-direction: column;
  gap: var(--space-xs);
  font-size: 0.85rem;
  color: var(--color-text-secondary);
}

.entity-form input,
.entity-form textarea {
  font-family: inherit;
  font-size: 1rem;
  color: var(--color-text);
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-sm);
  resize: vertical;
}

.entity-form-actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-sm);
}

.entity-form-actions .btn {
  flex: none;
  padding: var(--space-sm) var(--space-md);
}

.btn-primary {
  background: var(--color-accent);
  color: var(--color-accent-text);
}

.btn-secondary {
  background: var(--color-bg);
  color: var(--color-text);
  border: 1px solid var(--color-border);
}

.list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}

.list-row {
  animation: list-row-in 0.28s cubic-bezier(0.22, 1, 0.36, 1) both;
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-sm);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
}

.list-row-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  min-width: 0;
  background: none;
  border: none;
  padding: 0;
  font-family: inherit;
  color: inherit;
  text-align: left;
  cursor: pointer;
}

.list-row-title {
  font-weight: 600;
  overflow-wrap: anywhere;
}

.list-row-sub {
  font-size: 0.85rem;
  color: var(--color-text-secondary);
  overflow-wrap: anywhere;
}

.list-row-count {
  align-self: flex-start;
  margin-top: 2px;
  padding: 1px 8px;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: 999px;
  color: var(--color-text-secondary);
  font-size: 0.75rem;
  font-weight: 600;
}

.list-row-actions {
  display: flex;
  gap: var(--space-xs);
  flex: none;
}

.btn-icon {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  cursor: pointer;
  font-size: 0.95rem;
}

.card-image-preview {
  width: 100%;
  max-height: 180px;
  object-fit: cover;
  border-radius: var(--radius-md);
}

.card-image-preview[hidden] {
  display: none;
}

.card-thumb {
  width: 44px;
  height: 44px;
  border-radius: var(--radius-md);
  object-fit: cover;
  flex: none;
}

.card-thumb[hidden] {
  display: none;
}

.image-search {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}

.image-search-results {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--space-xs);
}

.image-search-results[hidden] {
  display: none;
}

.image-search-result {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  border-radius: var(--radius-md);
  border: 2px solid transparent;
  cursor: pointer;
}

.image-search-result.is-selected {
  border-color: var(--color-accent);
}

.empty-hint {
  color: var(--color-text-secondary);
  text-align: center;
  padding: var(--space-lg) 0;
}

.empty-hint[hidden] {
  display: none;
}

@keyframes list-row-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  .list-row,
  .entity-form {
    animation: none;
  }
}
