:root {
  --bg-color: #f5f3ef;
  --surface-color: #ffffff;
  --surface-alt: #faf8f5;
  --text-color: #262320;
  --muted-color: #736c60;
  --border-color: #e8e2d8;
  --accent-color: #9a3324;
  --accent-color-hover: #7d281c;
  --accent-soft: rgba(154, 51, 36, 0.1);
  --gold-color: #a9791f;
  --link-color: var(--accent-color);
  --header-bg: #1c1917;
  --header-text: #f4efe4;
  --code-bg: #f1ece3;
  --shadow: 0 10px 30px rgba(40, 30, 15, 0.08);
  --shadow-hover: 0 16px 36px rgba(40, 30, 15, 0.14);
  --font-heading: "Cinzel", Georgia, "Times New Roman", serif;
  --font-body: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg-color: #131211;
    --surface-color: #1c1a17;
    --surface-alt: #211e1a;
    --text-color: #eae5db;
    --muted-color: #a49b8b;
    --border-color: #35312a;
    --accent-color: #e2795e;
    --accent-color-hover: #ec9179;
    --accent-soft: rgba(226, 121, 94, 0.14);
    --gold-color: #d9ab52;
    --header-bg: #0d0c0a;
    --header-text: #f4efe4;
    --code-bg: #221f1a;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
    --shadow-hover: 0 16px 36px rgba(0, 0, 0, 0.45);
  }
}

* {
  box-sizing: border-box;
}

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
}

body {
  margin: 0;
  font-family: var(--font-body);
  color: var(--text-color);
  background: var(--bg-color);
  line-height: 1.65;
  transition: background-color 0.2s ease, color 0.2s ease;
}

a {
  color: var(--link-color);
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}

/* Header */
.site-header {
  background: var(--header-bg);
  color: var(--header-text);
  position: sticky;
  top: 0;
  z-index: 20;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.2);
}
.site-header-inner {
  max-width: 900px;
  margin: 0 auto;
  padding: 12px 30px;
  display: flex;
  align-items: center;
  gap: 24px;
}
.site-title {
  display: flex;
  align-items: center;
  gap: 10px;
}
.site-title:hover {
  text-decoration: none;
  opacity: 0.85;
}
.site-logo {
  height: 42px;
  width: auto;
  border-radius: 4px;
}
.site-nav {
  display: flex;
  gap: 6px;
  margin-left: auto;
}
.site-nav a {
  color: #d8d2c4;
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 0.95rem;
  font-weight: 600;
  transition: background-color 0.15s ease, color 0.15s ease;
}
.site-nav a:hover {
  color: #ffffff;
  text-decoration: none;
  background: rgba(255, 255, 255, 0.08);
}
.site-nav a.active {
  color: #ffffff;
  background: var(--accent-color);
}
.nav-toggle {
  display: none;
  background: none;
  border: none;
  color: var(--header-text);
  font-size: 1.4rem;
  cursor: pointer;
  line-height: 1;
}

@media screen and (max-width: 640px) {
  .nav-toggle {
    display: block;
    margin-left: auto;
  }
  .site-nav {
    display: none;
    flex-direction: column;
    width: 100%;
    margin-left: 0;
    gap: 2px;
    padding-top: 8px;
    order: 1;
  }
  .site-nav.open {
    display: flex;
  }
  .site-nav a {
    padding: 10px 12px;
  }
  .site-header-inner {
    flex-wrap: wrap;
  }
}

/* Search */
.search-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(15, 13, 10, 0.55);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 12vh 20px 20px;
}
.search-overlay[hidden] {
  display: none;
}
.search-panel {
  width: 100%;
  max-width: 640px;
  background: var(--surface-color);
  border: 1px solid var(--border-color);
  border-radius: 14px;
  box-shadow: var(--shadow-hover);
  overflow: hidden;
}
.search-input-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border-color);
  color: var(--muted-color);
}
.search-input {
  flex: 1;
  border: none;
  outline: none;
  background: none;
  color: var(--text-color);
  font-size: 1.05rem;
  font-family: var(--font-body);
}
.search-close {
  border: 1px solid var(--border-color);
  background: var(--surface-alt);
  color: var(--muted-color);
  border-radius: 6px;
  padding: 3px 8px;
  font-size: 0.78rem;
  cursor: pointer;
}
.search-close:hover {
  color: var(--text-color);
}
.search-results {
  max-height: 60vh;
  overflow-y: auto;
}
.search-result {
  display: block;
  padding: 12px 18px;
  border-bottom: 1px solid var(--border-color);
}
.search-result:last-child {
  border-bottom: none;
}
.search-result:hover,
.search-result.is-active {
  background: var(--surface-alt);
  text-decoration: none;
}
.search-result-title {
  font-family: var(--font-heading);
  font-weight: 700;
  color: var(--text-color);
  font-size: 0.98rem;
}
.search-result-title .search-result-section {
  font-family: var(--font-body);
  font-weight: 600;
  color: var(--accent-color);
}
.search-result-title mark {
  background: var(--accent-soft);
  color: var(--accent-color);
  border-radius: 3px;
  padding: 0 2px;
}
.search-empty,
.search-hint {
  padding: 18px;
  color: var(--muted-color);
  font-size: 0.9rem;
  text-align: center;
}

/* Sub navigation (character sections) */
.subnav {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 28px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border-color);
}
.subnav-links {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  flex: 1;
}
.subnav a {
  padding: 7px 16px;
  border-radius: 999px;
  color: var(--muted-color);
  font-weight: 600;
  font-size: 0.92rem;
  transition: background-color 0.15s ease, color 0.15s ease;
}
.subnav a:hover {
  color: var(--text-color);
  text-decoration: none;
  background: var(--surface-alt);
}
.subnav .search-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  background: none;
  border: 1px solid var(--border-color);
  color: var(--muted-color);
  padding: 8px;
  border-radius: 999px;
  cursor: pointer;
  line-height: 1;
  transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}
.subnav .search-toggle:hover {
  color: var(--accent-color);
  border-color: var(--accent-color);
  background: var(--accent-soft);
}
.subnav a.active {
  color: #ffffff;
  background: var(--accent-color);
}

@media screen and (max-width: 640px) {
  .subnav {
    gap: 8px;
    margin-bottom: 20px;
    padding-bottom: 12px;
  }
  .subnav-links {
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }
  .subnav-links::-webkit-scrollbar {
    display: none;
  }
  /* Dissolvenza sul bordo destro: segnala che ci sono altri tab da scorrere.
     Aggiunta/rimossa via JS (is-scrollable) a inizio e a fine scroll. */
  .subnav-links.is-scrollable {
    -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 28px), transparent);
    mask-image: linear-gradient(to right, #000 calc(100% - 28px), transparent);
  }
  .subnav a {
    flex-shrink: 0;
    white-space: nowrap;
  }
}

/* Main content surface */
#write {
  max-width: 860px;
  margin: 28px auto 60px;
  padding: 36px 40px 48px;
  background: var(--surface-color);
  border: 1px solid var(--border-color);
  border-radius: 16px;
  box-shadow: var(--shadow);
}

@media screen and (max-width: 640px) {
  #write {
    margin: 16px auto 32px;
    padding: 24px 20px 32px;
    border-radius: 12px;
  }
}

.character-content {
  padding-top: 4px;
}

.section-banner {
  display: block;
  width: 100%;
  aspect-ratio: 3 / 1;
  object-fit: cover;
  border-radius: 12px;
  border: 1px solid var(--border-color);
  box-shadow: var(--shadow);
  margin-bottom: 1.75rem;
}

img {
  max-width: 100%;
  height: auto;
}

.character-header {
  display: flex;
  align-items: center;
  gap: 28px;
  margin: 0 0 1.75rem;
  padding-bottom: 1.5rem;
  border-bottom: 1px solid var(--border-color);
}
.character-portrait {
  width: 220px;
  height: 220px;
  object-fit: cover;
  border-radius: 14px;
  border: 1px solid var(--border-color);
  box-shadow: var(--shadow);
  flex-shrink: 0;
}
.character-header h1 {
  margin: 0;
  border-bottom: none;
  padding-bottom: 0;
}

@media screen and (max-width: 560px) {
  .character-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 18px;
  }
  .character-portrait {
    width: 160px;
    height: 160px;
    margin: 0 auto;
  }
  .character-header {
    align-items: center;
    text-align: center;
  }
}

h1, h2, h3, h4, h5, h6 {
  position: relative;
  margin-top: 1.6rem;
  margin-bottom: 0.9rem;
  font-weight: 700;
  line-height: 1.3;
  font-family: var(--font-heading);
  letter-spacing: 0.01em;
  scroll-margin-top: 86px;
}
h1 {
  font-size: 2.1em;
  line-height: 1.2;
}
h2 {
  font-size: 1.5em;
  padding-bottom: 0.4em;
  border-bottom: 1px solid var(--border-color);
}
h3 {
  font-size: 1.2em;
  color: var(--accent-color);
}
h4 {
  font-size: 1.05em;
  font-family: var(--font-body);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted-color);
}
h5 {
  font-size: 1em;
  font-family: var(--font-body);
}
h6 {
  font-size: 1em;
  font-family: var(--font-body);
  color: var(--muted-color);
}

p, blockquote, ul, ol, dl, table {
  margin: 0.8em 0;
}
li > ol, li > ul {
  margin: 0 0;
}
ul, ol {
  padding-left: 28px;
}
li {
  margin: 0.3em 0;
}

hr {
  height: 1px;
  padding: 0;
  margin: 2rem 0;
  background-color: var(--border-color);
  border: 0;
}

blockquote {
  border-left: 3px solid var(--accent-color);
  padding: 0.4em 1em;
  margin: 1em 0;
  color: var(--muted-color);
  background: var(--surface-alt);
  border-radius: 0 8px 8px 0;
}

/* Tables */
table {
  border-collapse: collapse;
  width: 100%;
  overflow: auto;
  border: 1px solid var(--border-color);
  border-radius: 10px;
}
table thead:has(th:empty) {
  display: none;
}
table tr {
  border-bottom: 1px solid var(--border-color);
  background-color: var(--surface-color);
}
table tr:last-child {
  border-bottom: none;
}
table tr:nth-child(2n) {
  background-color: var(--surface-alt);
}
table th,
table td {
  border-right: 1px solid var(--border-color);
  padding: 8px 14px;
}
table th:last-child,
table td:last-child {
  border-right: none;
}
table th {
  font-weight: 700;
  background-color: var(--surface-alt);
  color: var(--accent-color);
}

@media screen and (max-width: 480px) {
  table {
    display: block;
    overflow-x: auto;
    white-space: nowrap;
  }
}

code, tt {
  background-color: var(--code-bg);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  padding: 2px 5px;
  font-size: 0.9em;
  font-family: "Lucida Console", Consolas, "Courier", monospace;
}

pre {
  background-color: var(--code-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 12px 16px;
  overflow-x: auto;
}
pre code {
  border: none;
  background: none;
  padding: 0;
}

sub, sup {
  font-size: 0.8em;
  color: var(--muted-color);
}

/* Footer */
.site-footer {
  max-width: 860px;
  margin: 0 auto;
  padding: 8px 20px 40px;
  color: var(--muted-color);
  font-size: 0.88rem;
  text-align: center;
}

/* Back to top */
.back-to-top {
  position: fixed;
  right: 24px;
  bottom: 24px;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--border-color);
  background: var(--surface-color);
  color: var(--accent-color);
  box-shadow: var(--shadow-hover);
  cursor: pointer;
  opacity: 0;
  transform: translateY(8px);
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease, background-color 0.15s ease, color 0.15s ease;
}
.back-to-top.is-visible {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}
.back-to-top:hover {
  color: #ffffff;
  background: var(--accent-color);
}

@media screen and (max-width: 640px) {
  .back-to-top {
    right: 16px;
    bottom: 16px;
  }
}

/* Homepage character list */
.character-list {
  list-style: none;
  padding: 0;
  margin: 2rem 0 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 20px;
}
.character-list li {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 6px;
  padding: 24px 16px;
  background: var(--surface-alt);
  border: 1px solid var(--border-color);
  border-radius: 14px;
  transition: transform 0.15s ease, box-shadow 0.15s ease, border-color 0.15s ease;
}
.character-list li:hover {
  transform: translateY(-4px);
  box-shadow: var(--shadow-hover);
  border-color: var(--accent-color);
}
.character-thumb {
  width: 108px;
  height: 108px;
  object-fit: cover;
  border-radius: 12px;
  border: 1px solid var(--border-color);
  margin-bottom: 8px;
}
.character-list a {
  font-family: var(--font-heading);
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--text-color);
}
.character-list a:hover {
  color: var(--accent-color);
  text-decoration: none;
}
.character-summary {
  color: var(--muted-color);
  font-size: 0.88rem;
}
