/* Tarinatuokio – © 2026 tarinatuokio.fi. Kaikki oikeudet pidätetään. */

/* Fontit ladataan omalta palvelimelta (ei Google Fonts -yhteyttä).
   Lora ja Inter: SIL Open Font License 1.1, lisenssit kansiossa fonts/. */
@font-face {
  font-family: 'Lora';
  src: url('fonts/Lora-Variable.ttf') format('truetype');
  font-weight: 400 700;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: 'Inter';
  src: url('fonts/Inter-Regular.woff2') format('woff2');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: 'Inter';
  src: url('fonts/Inter-SemiBold.woff2') format('woff2');
  font-weight: 600;
  font-style: normal;
  font-display: swap;
}
:root {
  --bg: #16130f;
  --surface: #221d17;
  --surface-2: #2c251d;
  --text: #ece4d8;
  --muted: #a89a87;
  --accent: #d4a373;
  --accent-strong: #e9b884;
  --good: #8fbf7f;
  --bad: #d77a6a;
  --neutral: #c9b27c;
  --radius: 14px;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Inter', system-ui, sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

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

.site-header {
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--surface-2);
}

.logo {
  font-family: 'Lora', serif;
  font-weight: 600;
  font-size: 1.35rem;
  color: var(--accent-strong);
  text-decoration: none;
}

main {
  flex: 1;
  width: 100%;
  max-width: 680px;
  margin: 0 auto;
  padding: 1.5rem 1.25rem 3rem;
}

.site-footer {
  text-align: center;
  color: var(--muted);
  font-size: 0.8rem;
  padding: 1rem 1.25rem calc(1rem + env(safe-area-inset-bottom));
}
.site-footer p { margin: .25rem 0; }
.site-footer .copyright { font-size: .75rem; opacity: .8; }

.loading, .error { color: var(--muted); }
.error { color: var(--bad); }

/* --- Tarinalista --- */
main.wide { max-width: 1120px; }

.hero { margin: .5rem 0 1.5rem; }
.hero h1 {
  font-family: 'Lora', serif;
  font-size: clamp(1.6rem, 4vw, 2.4rem);
  margin: 0 0 .5rem;
  color: var(--accent-strong);
  line-height: 1.2;
}
.intro {
  color: var(--muted);
  margin: 0;
  line-height: 1.5;
  max-width: 60ch;
}

.filters {
  display: flex;
  flex-wrap: wrap;
  gap: .45rem;
  margin-bottom: 1.5rem;
}

.genre {
  --g: #b8a68f;
  display: inline-block;
  font: inherit;
  font-size: .75rem;
  font-weight: 600;
  letter-spacing: .02em;
  color: var(--g);
  background: color-mix(in srgb, var(--g) 14%, transparent);
  border: 1px solid color-mix(in srgb, var(--g) 35%, transparent);
  border-radius: 999px;
  padding: .2rem .65rem;
}
button.genre {
  cursor: pointer;
  font-size: .85rem;
  padding: .4rem .9rem;
  transition: background .15s, color .15s;
}
button.genre.all { --g: var(--text); }
button.genre:hover { background: color-mix(in srgb, var(--g) 25%, transparent); }
button.genre.active { background: var(--g); color: #1b150f; border-color: var(--g); }

/* --- Genre-pudotusvalikko (puhelin) --- */
.filter-wrap { margin-bottom: 1.5rem; }
.filter-wrap .filters { margin-bottom: 0; }
.dropdown { display: none; position: relative; }

.dot {
  width: .6rem; height: .6rem;
  border-radius: 50%;
  background: var(--g);
  flex: none;
}

.dd-toggle {
  width: 100%;
  display: flex;
  align-items: center;
  gap: .55rem;
  background: var(--surface);
  border: 1px solid var(--surface-2);
  border-radius: 12px;
  padding: .75rem 1rem;
  color: var(--text);
  font: inherit;
  font-size: .95rem;
  cursor: pointer;
  text-align: left;
}
.dropdown.open .dd-toggle { border-color: var(--accent); }
.dd-caption { color: var(--muted); font-size: .85rem; }
.dd-value { flex: 1; font-weight: 600; }
.chev { color: var(--muted); transition: transform .2s; }
.dropdown.open .chev { transform: rotate(180deg); }

.dd-panel {
  position: absolute;
  z-index: 10;
  top: calc(100% + .4rem);
  left: 0; right: 0;
  margin: 0;
  padding: .35rem;
  list-style: none;
  background: var(--surface);
  border: 1px solid var(--surface-2);
  border-radius: 12px;
  box-shadow: 0 12px 30px rgba(0,0,0,.45);
  max-height: 60vh;
  overflow-y: auto;
  animation: fadeIn .15s ease;
}
.dd-panel[hidden] { display: none; }

.dd-option {
  width: 100%;
  display: flex;
  align-items: center;
  gap: .65rem;
  background: none;
  border: 0;
  border-radius: 8px;
  padding: .7rem .75rem;
  color: var(--text);
  font: inherit;
  font-size: .95rem;
  cursor: pointer;
  text-align: left;
}
.dd-option:hover, .dd-option:focus-visible { background: var(--surface-2); }
.dd-option.selected { background: color-mix(in srgb, var(--g) 16%, transparent); }
.dd-option.selected .dd-label { color: var(--g); font-weight: 600; }
.dd-label { flex: 1; }
.dd-count {
  color: var(--muted);
  font-size: .8rem;
  background: var(--surface-2);
  border-radius: 999px;
  padding: .1rem .5rem;
}

@media (max-width: 700px) {
  .filter-wrap .filters { display: none; }
  .dropdown { display: block; }
}

.genres { display: flex; flex-wrap: wrap; gap: .35rem; margin-bottom: .6rem; }

.story-list {
  display: grid;
  gap: 1rem;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
}

.story-card {
  --g: var(--accent);
  display: flex;
  flex-direction: column;
  border-top: 3px solid var(--g) !important;
  background: var(--surface);
  border: 1px solid var(--surface-2);
  border-radius: var(--radius);
  padding: 1.1rem 1.2rem;
  text-decoration: none;
  color: inherit;
  transition: border-color .15s, transform .15s;
}
.story-card:hover, .story-card:focus-visible {
  border-color: var(--accent);
  transform: translateY(-2px);
}
.story-card h2 {
  font-family: 'Lora', serif;
  margin: 0 0 .35rem;
  font-size: 1.25rem;
}
.story-card p { margin: 0 0 .75rem; color: var(--muted); line-height: 1.45; flex: 1; }
.story-card:hover, .story-card:focus-visible { border-color: var(--g); }

.meta { display: flex; gap: .5rem; flex-wrap: wrap; font-size: .8rem; }
.badge {
  background: var(--surface-2);
  border-radius: 999px;
  padding: .2rem .65rem;
  color: var(--muted);
}
.badge.continue { color: var(--accent-strong); }

/* --- Tarinanäkymä --- */
.story-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .5rem;
  margin-bottom: 1.25rem;
}
.story-top h1 {
  font-family: 'Lora', serif;
  font-size: 1.1rem;
  margin: 0;
  color: var(--muted);
  font-weight: 400;
}
.tools { display: flex; gap: .4rem; flex: none; }

.icon-btn {
  white-space: nowrap;
  background: var(--surface);
  border: 1px solid var(--surface-2);
  color: var(--text);
  border-radius: 10px;
  padding: .45rem .7rem;
  font: inherit;
  font-size: .85rem;
  cursor: pointer;
}
.icon-btn:disabled { opacity: .35; cursor: default; }
.icon-btn:not(:disabled):hover { border-color: var(--accent); }

.passage {
  font-family: 'Lora', serif;
  font-size: var(--fs, 1.15rem);
  line-height: 1.7;
}

/* --- Lukutila: kaikki yhdellä ruudulla ilman sivun vieritystä --- */
body.reading {
  /* --app-h asetetaan JavaScriptillä selaimen todellisen näkyvän alueen mukaan
     (esim. Braven ja Chromen alapalkit huomioiden). */
  height: 100vh;
  height: 100dvh;
  height: var(--app-h, 100dvh);
  min-height: 0;
  /* Ei lukita: jos jokin silti peittää napit, sivua voi vierittää. */
  overflow-y: auto;
}
body.reading .site-header { flex: none; }
body.reading .site-footer { display: none; }
body.reading main {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  /* puhelimella valinnat reilusti irti ruudun alareunasta, ettei selaimen palkki peitä niitä */
  padding: .9rem 1.1rem calc(2.6rem + env(safe-area-inset-bottom));
}
body.reading .story-top { margin-bottom: .9rem; flex: none; }
body.reading .passage {
  flex: 1;
  min-height: 0;
  overflow: hidden;
}
body.reading .passage.scrollable {
  overflow-y: auto;
  -webkit-mask-image: linear-gradient(to bottom, #000 80%, transparent);
          mask-image: linear-gradient(to bottom, #000 80%, transparent);
}
body.reading .passage.scrollable.at-end {
  -webkit-mask-image: none;
          mask-image: none;
}
/* Vieritysmerkki tekstilaatikon alareunassa */
.more-hint {
  flex: none;
  align-self: center;
  margin-top: .35rem;
  display: flex;
  align-items: center;
  gap: .35rem;
  background: var(--surface-2);
  color: var(--accent-strong);
  border: 1px solid color-mix(in srgb, var(--accent) 40%, transparent);
  border-radius: 999px;
  padding: .25rem .8rem;
  font: inherit;
  font-size: .8rem;
  font-weight: 600;
  cursor: pointer;
}
.more-hint[hidden] { display: none; }
.more-hint span { display: inline-block; animation: bob 1.2s ease-in-out infinite; }
@keyframes bob { 50% { transform: translateY(3px); } }
.dock { flex: none; margin-top: 1rem; }
body.reading .choices.dock { margin-top: 1rem; }

/* Loppulaatikko tulee heti tekstin alle (myös puhelimella) */
body.reading.ended .passage { flex: 0 1 auto; }
body.reading .ending.dock { margin-top: 1.6rem; }

/* Puhelimella isommat, helpommin painettavat valintanapit */
@media (max-width: 700px) {
  body.reading .choices { gap: .7rem; }
  body.reading .choice {
    min-height: 3.6rem;
    padding: 1rem 1.15rem;
    font-size: 1.08rem;
    line-height: 1.35;
  }
}

/* Isolla näytöllä myös valinnat tulevat tekstin alle, ei ruudun alareunaan */
@media (min-width: 701px) {
  body.reading main { padding-bottom: 1.5rem; }
  body.reading .passage { flex: 0 1 auto; }
  body.reading .choices.dock { margin-top: 1.75rem; }
}
.passage p { margin: 0 0 1.1rem; }

.choices {
  display: grid;
  gap: .65rem;
  margin-top: 1.75rem;
}
.choice {
  text-align: left;
  background: var(--surface);
  border: 1px solid var(--surface-2);
  color: var(--text);
  border-radius: var(--radius);
  padding: .9rem 1.1rem;
  font: inherit;
  font-size: 1rem;
  line-height: 1.4;
  cursor: pointer;
  transition: border-color .15s, background .15s;
}
.choice::before { content: '› '; color: var(--accent); font-weight: 600; }
.choice:hover, .choice:focus-visible {
  border-color: var(--accent);
  background: var(--surface-2);
}

.ending {
  margin-top: 2rem;
  padding: 1.4rem;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--surface-2);
  text-align: center;
}
.ending .label {
  text-transform: uppercase;
  letter-spacing: .12em;
  font-size: .75rem;
  margin-bottom: .3rem;
}
.ending h2 { font-family: 'Lora', serif; margin: 0 0 .5rem; }
.ending .found { color: var(--muted); font-size: .9rem; margin-bottom: 1.1rem; }
.ending.good  { border-color: var(--good); }    .ending.good .label  { color: var(--good); }
.ending.bad   { border-color: var(--bad); }     .ending.bad .label   { color: var(--bad); }
.ending.neutral { border-color: var(--neutral); } .ending.neutral .label { color: var(--neutral); }

.ending-actions { display: flex; gap: .6rem; justify-content: center; flex-wrap: wrap; }
.btn {
  background: var(--accent);
  color: #1b150f;
  border: 0;
  border-radius: 10px;
  padding: .65rem 1.1rem;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
}
.btn.secondary {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--surface-2);
}

/* --- Kirjoituskone-efekti --- */
.passage.typing { cursor: pointer; }
.passage p.cursor::after {
  content: '';
  display: inline-block;
  width: 2px;
  height: 1.05em;
  margin-left: 2px;
  vertical-align: -0.15em;
  background: var(--accent);
  animation: blink .9s steps(1) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

.waiting { visibility: hidden; }
.reveal { animation: fadeIn .4s ease both; }

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}

/* --- Matalat näytöt: tiiviimmät napit, jotta tekstille jää tilaa --- */
@media (max-height: 640px) {
  body.reading main { padding-top: .6rem; padding-bottom: calc(1rem + env(safe-area-inset-bottom)); }
  body.reading .site-header { padding: .6rem 1.1rem; }
  body.reading .logo { font-size: 1.15rem; }
  body.reading .story-top { margin-bottom: .5rem; }
  body.reading .story-top h1 { font-size: .95rem; }
  body.reading .icon-btn { padding: .3rem .55rem; font-size: .8rem; }
  body.reading .choices { gap: .4rem; }
  body.reading .choice { padding: .55rem .85rem; font-size: .9rem; line-height: 1.3; border-radius: 10px; }
  body.reading .dock { margin-top: .6rem; }
  body.reading .ending { padding: .9rem; }
}
@media (max-height: 520px) {
  body.reading .story-top h1 { display: none; }
  body.reading .story-top { justify-content: flex-end; }
  body.reading .site-header { padding: .45rem 1rem; }
  body.reading .choice { padding: .45rem .75rem; font-size: .85rem; }
  body.reading .choice::before { content: '›'; margin-right: .3rem; }
}

/* --- Turvamarginaalit: lovi, pyöristetyt kulmat ja selaimen palkit --- */
.site-header {
  padding-top: max(1rem, env(safe-area-inset-top));
  padding-left: max(1.25rem, env(safe-area-inset-left));
  padding-right: max(1.25rem, env(safe-area-inset-right));
}
main {
  padding-left: max(1.1rem, env(safe-area-inset-left));
  padding-right: max(1.1rem, env(safe-area-inset-right));
}
