/* ---------- Reset & tokens ---------- */
*,
*::before,
*::after {
  box-sizing: border-box;
}

:root {
  color-scheme: light;

  --ink: #211d17;
  --ink-soft: #55503f;
  --paper: #f4efe2;
  --paper-raised: #fbf8f0;
  --line: #ddd3bd;
  --accent: #b5482b;
  --accent-dark: #8f3620;
  --focus-ring: #1857a4;

  --font-serif: Iowan Old Style, Palatino Linotype, Palatino, Georgia, "Times New Roman", serif;
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;

  --radius: 10px;
}

@media (prefers-reduced-motion: no-preference) {
  :root {
    --duration-fast: 140ms;
    --duration-base: 220ms;
  }
}
@media (prefers-reduced-motion: reduce) {
  :root {
    --duration-fast: 0ms;
    --duration-base: 0ms;
  }
}

html {
  background: var(--paper);
}

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-sans);
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
}

img,
svg {
  max-width: 100%;
  display: block;
}

button {
  font: inherit;
  color: inherit;
}

a {
  color: var(--accent-dark);
}

/* ---------- Focus ---------- */
a:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
[tabindex]:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 3px;
  border-radius: 4px;
}

/* ---------- Skip link ---------- */
.skip-link {
  position: absolute;
  left: 1rem;
  top: -3rem;
  background: var(--ink);
  color: var(--paper);
  padding: 0.6rem 1rem;
  border-radius: 6px;
  z-index: 100;
  transition: top var(--duration-fast) ease-out;
  text-decoration: none;
}
.skip-link:focus {
  top: 1rem;
}

/* ---------- Header ---------- */
.site-header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
  padding: 1.1rem 1.5rem 1rem;
}

.masthead {
  max-width: 76rem;
  margin: 0 auto;
  padding-bottom: 0.9rem;
}

.wordmark {
  display: inline-flex;
  align-items: center;
  gap: 0.7rem;
}

.wordmark-mark {
  width: 40px;
  height: 40px;
  flex-shrink: 0;
}
.mark-bg {
  fill: var(--ink);
}
.mark-sun {
  fill: #f2c265;
}
.mark-bldg {
  fill: var(--paper);
}

.wordmark-text {
  display: flex;
  flex-direction: column;
  line-height: 1.1;
}
.wordmark-title {
  font-family: var(--font-serif);
  font-weight: 700;
  font-size: 1.6rem;
  letter-spacing: 0.01em;
  margin: 0;
}
.wordmark-tag {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--ink-soft);
  margin-top: 0.15rem;
}

.controls {
  max-width: 76rem;
  margin: 0 auto;
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  gap: 0.9rem;
}

.control-field {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}
.control-field label {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-soft);
}

.control-search {
  flex: 1 1 16rem;
  min-width: 0;
}

#search-input {
  width: 100%;
  padding: 0.55rem 0.8rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper-raised);
  color: var(--ink);
  font-size: 0.95rem;
}
#search-input::placeholder {
  color: #8b846e;
}

.control-select {
  flex: 0 1 12rem;
}

#category-filter {
  width: 100%;
  padding: 0.55rem 2rem 0.55rem 0.8rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background-color: var(--paper-raised);
  color: var(--ink);
  font-size: 0.95rem;
  appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'><path d='M5 8l5 5 5-5' fill='none' stroke='%23211d17' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/></svg>");
  background-repeat: no-repeat;
  background-position: right 0.6rem center;
  background-size: 1rem;
}

.saved-toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  border: 1px solid var(--line);
  background: var(--paper-raised);
  border-radius: 8px;
  padding: 0.55rem 0.9rem;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 600;
  touch-action: manipulation;
  transition: background-color var(--duration-fast) ease-out, border-color var(--duration-fast) ease-out, color var(--duration-fast) ease-out;
}
.saved-toggle .star-icon path {
  fill: none;
  stroke: var(--ink-soft);
  stroke-width: 1.4;
  transition: fill var(--duration-fast) ease-out, stroke var(--duration-fast) ease-out;
}
.saved-toggle:hover {
  border-color: var(--accent);
  color: var(--accent-dark);
}
.saved-toggle[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.saved-toggle[aria-pressed="true"] .star-icon path {
  fill: #fff;
  stroke: #fff;
}

/* ---------- Main / list ---------- */
main {
  max-width: 76rem;
  margin: 0 auto;
  padding: 1.4rem 1.5rem 3rem;
}

.result-summary {
  margin: 0 0 1rem;
  font-size: 0.85rem;
  color: var(--ink-soft);
  font-variant-numeric: tabular-nums;
}

.story-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 1.2rem;
}

.story-card {
  position: relative;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper-raised);
  overflow: hidden;
  scroll-margin-top: 8rem;
  transition: border-color var(--duration-fast) ease-out, box-shadow var(--duration-fast) ease-out;
}

.story-card:hover {
  border-color: var(--accent);
  box-shadow: 0 4px 14px rgba(33, 29, 23, 0.08);
}

.open-item-btn {
  text-align: left;
  background: none;
  border: none;
  padding: 0;
  margin: 0;
  cursor: pointer;
  touch-action: manipulation;
  font: inherit;
  color: inherit;
}

/* Stretches the title button's hit area to cover the whole card. */
.open-item-btn::after {
  content: "";
  position: absolute;
  inset: 0;
}

.story-skyline {
  width: 100%;
  height: auto;
  aspect-ratio: 5 / 3;
}
.story-skyline .sky-bg {
  fill: var(--sky, #e4efe9);
}
.story-skyline .bldg {
  fill: var(--tint, #2f6f5e);
}
.story-skyline .sun {
  fill: var(--paper-raised);
  opacity: 0.85;
}

.story-card-body {
  padding: 1rem 1.1rem 1.2rem;
}

.category-tag {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.09em;
  padding: 0.2rem 0.6rem 0.2rem 0.5rem;
  border-radius: 999px;
  background: var(--tag-soft, #eee);
  color: var(--ink);
}
.category-tag::before {
  content: "";
  width: 6px;
  height: 6px;
  flex-shrink: 0;
  border-radius: 50%;
  background: var(--tag-tint, var(--accent));
}

.story-title {
  font-family: var(--font-serif);
  font-size: 1.2rem;
  line-height: 1.3;
  margin: 0.55rem 0 0.4rem;
  text-wrap: balance;
}

.story-title .open-item-btn {
  display: inline;
  color: var(--ink);
}

.story-card:hover .story-title .open-item-btn {
  color: var(--accent-dark);
  text-decoration: underline;
}

.story-summary {
  margin: 0 0 0.7rem;
  color: var(--ink-soft);
  font-size: 0.92rem;
  line-height: 1.45;
}

.story-meta {
  margin: 0;
  font-size: 0.78rem;
  color: var(--ink-soft);
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
}

/* ---------- Empty state ---------- */
.empty-state {
  text-align: center;
  padding: 3.5rem 1.5rem;
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  background: var(--paper-raised);
}
.empty-title {
  font-family: var(--font-serif);
  font-size: 1.3rem;
  margin: 0 0 0.5rem;
}
.empty-body {
  margin: 0 0 1.1rem;
  color: var(--ink-soft);
}
.empty-reset-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 0.6rem 1.2rem;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  transition: background-color var(--duration-fast) ease-out;
}
.empty-reset-btn:hover {
  background: var(--accent-dark);
}

/* ---------- Footer ---------- */
.site-footer {
  max-width: 76rem;
  margin: 0 auto;
  padding: 0 1.5rem 2.5rem;
  color: var(--ink-soft);
  font-size: 0.78rem;
}

/* ---------- Detail overlay ---------- */
.detail-overlay {
  position: fixed;
  inset: 0;
  background: rgba(33, 29, 23, 0.5);
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 3vh 1rem;
  overflow-y: auto;
  overscroll-behavior: contain;
  z-index: 50;
}
.detail-overlay[hidden] {
  display: none;
}

.detail-panel {
  background: var(--paper-raised);
  border-radius: 14px;
  max-width: 42rem;
  width: 100%;
  padding: 1.3rem 1.5rem 2.5rem;
  box-shadow: 0 20px 60px rgba(33, 29, 23, 0.35);
  animation: detail-in var(--duration-base) ease-out;
}

@keyframes detail-in {
  from {
    opacity: 0;
    transform: translateY(12px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
@media (prefers-reduced-motion: reduce) {
  .detail-panel {
    animation: none;
  }
}

.detail-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.6rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

.close-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  background: none;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.45rem 0.8rem;
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--ink-soft);
  touch-action: manipulation;
}
.close-btn:hover {
  border-color: var(--accent);
  color: var(--accent-dark);
}
.close-btn svg {
  width: 14px;
  height: 14px;
  color: currentColor;
}

.save-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  border: 1px solid var(--accent);
  background: transparent;
  color: var(--accent-dark);
  border-radius: 8px;
  padding: 0.45rem 0.9rem;
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 700;
  touch-action: manipulation;
  transition: background-color var(--duration-fast) ease-out, color var(--duration-fast) ease-out;
}
.save-btn .star-icon path {
  fill: none;
  stroke: var(--accent-dark);
  stroke-width: 1.4;
  transition: fill var(--duration-fast) ease-out, stroke var(--duration-fast) ease-out;
}
.save-btn:hover {
  background: var(--accent);
  color: #fff;
}
.save-btn:hover .star-icon path {
  stroke: #fff;
}
.save-btn[aria-pressed="true"] {
  background: var(--accent);
  color: #fff;
}
.save-btn[aria-pressed="true"] .star-icon path {
  fill: #fff;
  stroke: #fff;
}

.detail-category {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  margin: 0 0 0.5rem;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--ink-soft);
}
.detail-category::before {
  content: "";
  width: 8px;
  height: 8px;
  flex-shrink: 0;
  border-radius: 50%;
  background: var(--tag-tint, var(--accent));
}

.detail-title {
  font-family: var(--font-serif);
  font-size: clamp(1.5rem, 1.1rem + 1.4vw, 2.1rem);
  line-height: 1.2;
  margin: 0 0 0.6rem;
  text-wrap: balance;
}

.detail-meta {
  margin: 0 0 1.4rem;
  font-size: 0.85rem;
  color: var(--ink-soft);
  padding-bottom: 1.2rem;
  border-bottom: 1px solid var(--line);
}

.detail-body {
  font-family: var(--font-serif);
  font-size: 1.08rem;
  line-height: 1.7;
  color: var(--ink);
  white-space: pre-wrap;
  word-wrap: break-word;
}

/* ---------- Responsive ---------- */
@media (max-width: 640px) {
  .site-header {
    padding: 1rem 1rem 0.9rem;
  }
  main {
    padding: 1.1rem 1rem 2.5rem;
  }
  .controls {
    flex-direction: column;
    align-items: stretch;
  }
  .control-select {
    flex: 1 1 auto;
  }
  .saved-toggle {
    justify-content: center;
  }
  .story-list {
    grid-template-columns: 1fr;
  }
  .detail-overlay {
    padding: 0;
  }
  .detail-panel {
    max-width: none;
    min-height: 100%;
    border-radius: 0;
    padding: 1rem 1.1rem calc(2.5rem + env(safe-area-inset-bottom));
    padding-top: calc(1rem + env(safe-area-inset-top));
  }
}
