:root {
  color-scheme: light;
  --bg: #f7f8f6;
  --surface: #ffffff;
  --surface-muted: #eff3ef;
  --surface-strong: #e5ebe5;
  --ink: #18201c;
  --ink-muted: #687169;
  --border: #d8dfd8;
  --accent: #1769aa;
  --accent-strong: #0f5d66;
  --accent-soft: #d9edf4;
  --warm: #b6532a;
  --focus: #0b6bcb;
  --shadow: 0 20px 60px rgba(24, 32, 28, 0.09);
  --radius: 8px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #101412;
  --surface: #171d1a;
  --surface-muted: #202823;
  --surface-strong: #2c3630;
  --ink: #eff5ee;
  --ink-muted: #a7b1aa;
  --border: #334139;
  --accent: #75b9df;
  --accent-strong: #7ed0c4;
  --accent-soft: #173542;
  --warm: #f09a66;
  --focus: #92c5ff;
  --shadow: 0 20px 60px rgba(0, 0, 0, 0.28);
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  min-width: 320px;
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  letter-spacing: 0;
}

button,
input,
select {
  font: inherit;
}

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

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

button,
a,
input,
select {
  outline-offset: 3px;
}

button:focus-visible,
a:focus-visible,
input:focus-visible,
select:focus-visible {
  outline: 2px solid var(--focus);
}

.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;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 18px;
  min-height: 72px;
  padding: 12px clamp(16px, 3vw, 34px);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(18px);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
  color: var(--ink);
  text-decoration: none;
}

.brand-mark {
  display: grid;
  width: 42px;
  height: 42px;
  flex-shrink: 0;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--accent-strong);
  box-shadow: 0 8px 24px rgba(24, 32, 28, 0.06);
}

.brand-copy {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.brand-copy strong {
  font-size: 1rem;
  line-height: 1.1;
}

.brand-copy small,
.affiliation-note,
.panel-status,
.empty-note,
.toc-empty,
.article-meta,
.article-description {
  color: var(--ink-muted);
}

.affiliation-note {
  justify-self: center;
  max-width: 560px;
  margin: 0;
  font-size: 0.9rem;
  text-align: center;
}

.topbar-actions {
  display: inline-flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
}

.select-label select {
  width: 118px;
  min-height: 42px;
  padding: 0 34px 0 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
}

.icon-button,
.auth-button,
.tool-button,
.quick-links button,
.result-item,
.saved-item {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
}

.icon-button {
  display: inline-grid;
  width: 42px;
  height: 42px;
  place-items: center;
  padding: 0;
}

.icon-button.compact {
  width: 34px;
  height: 34px;
}

.icon-button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.auth-button:disabled {
  cursor: wait;
  opacity: 0.72;
}

.auth-button,
.tool-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 42px;
  padding: 0 14px;
  text-decoration: none;
}

.auth-button.signed-in {
  border-color: color-mix(in srgb, var(--accent-strong) 50%, var(--border));
  background: var(--accent-soft);
}

.google-mark {
  display: grid;
  width: 22px;
  height: 22px;
  place-items: center;
  border-radius: 999px;
  background: #ffffff;
  color: #1a73e8;
  font-size: 0.8rem;
  font-weight: 800;
}

.tool-button.active {
  border-color: color-mix(in srgb, var(--warm) 58%, var(--border));
  color: var(--warm);
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(280px, 360px) minmax(0, 880px) minmax(200px, 260px);
  align-items: start;
  gap: 24px;
  width: min(1540px, 100%);
  min-height: calc(100vh - 72px);
  margin: 0 auto;
  padding: 24px clamp(16px, 3vw, 34px) 16px;
}

.search-panel,
.toc-panel {
  position: sticky;
  top: 96px;
  display: grid;
  gap: 18px;
  max-height: calc(100vh - 118px);
  overflow: auto;
  scrollbar-width: thin;
}

.search-box {
  display: grid;
  grid-template-columns: 22px minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  min-height: 54px;
  padding: 0 10px 0 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.search-box input {
  width: 100%;
  height: 52px;
  border: 0;
  background: transparent;
  color: var(--ink);
  font-size: 1rem;
}

.search-box input:focus {
  outline: 0;
}

.quick-links {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.quick-links button {
  min-height: 38px;
  padding: 0 10px;
  color: var(--accent-strong);
  font-size: 0.88rem;
}

.panel-status {
  min-height: 20px;
  margin: 0;
  font-size: 0.88rem;
}

.results-list,
.saved-list {
  display: grid;
  gap: 10px;
}

.result-item {
  display: grid;
  grid-template-columns: 58px minmax(0, 1fr);
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 76px;
  padding: 8px;
  text-align: left;
}

.result-item:hover,
.saved-item:hover,
.icon-button:hover,
.quick-links button:hover,
.tool-button:hover,
.auth-button:hover {
  border-color: color-mix(in srgb, var(--accent) 42%, var(--border));
  background: color-mix(in srgb, var(--surface) 78%, var(--accent-soft));
}

.result-media {
  display: grid;
  width: 58px;
  height: 58px;
  place-items: center;
  overflow: hidden;
  border-radius: var(--radius);
  background: var(--surface-muted);
}

.result-media img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.result-fallback {
  color: var(--accent-strong);
  font-weight: 800;
}

.result-copy {
  display: grid;
  gap: 4px;
  min-width: 0;
}

.result-copy strong,
.saved-item span {
  overflow: hidden;
  color: var(--ink);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.result-copy small {
  display: -webkit-box;
  overflow: hidden;
  color: var(--ink-muted);
  line-height: 1.35;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
}

.searchmatch {
  color: var(--warm);
  font-weight: 700;
}

.saved-panel {
  display: grid;
  gap: 12px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

.section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.section-heading h2 {
  margin: 0;
  font-size: 0.82rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.saved-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 44px;
  padding: 0 12px;
  text-align: left;
}

.saved-item small {
  color: var(--ink-muted);
  font-size: 0.72rem;
}

.empty-note,
.toc-empty {
  margin: 0;
  font-size: 0.9rem;
}

.reader {
  min-width: 0;
  padding-bottom: 48px;
}

.article-hero {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(200px, 300px);
  gap: 28px;
  align-items: start;
  padding: clamp(22px, 4vw, 48px);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.eyebrow {
  margin: 0 0 12px;
  color: var(--accent-strong);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.article-hero h1 {
  max-width: 13ch;
  margin: 0;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(2.35rem, 7vw, 5.4rem);
  font-weight: 700;
  line-height: 0.94;
  letter-spacing: 0;
}

.article-description {
  max-width: 64ch;
  margin: 18px 0 0;
  font-size: 1.05rem;
  line-height: 1.65;
}

.article-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 26px;
}

.article-media {
  display: grid;
  gap: 10px;
  margin: 0;
}

.article-media img {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
  border-radius: var(--radius);
  background: var(--surface-muted);
}

.article-media figcaption {
  display: -webkit-box;
  overflow: hidden;
  color: var(--ink-muted);
  font-size: 0.8rem;
  line-height: 1.35;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
}

.article-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 18px 0 0;
  font-size: 0.88rem;
}

.article-loading {
  display: grid;
  gap: 12px;
  margin-top: 28px;
}

.article-loading span {
  display: block;
  height: 18px;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--surface-strong), var(--surface-muted), var(--surface-strong));
  background-size: 220% 100%;
  animation: loading 1.15s linear infinite;
}

.article-loading span:nth-child(2) {
  width: 76%;
}

.article-loading span:nth-child(3) {
  width: 54%;
}

@keyframes loading {
  to {
    background-position: -220% 0;
  }
}

.article-content {
  max-width: 78ch;
  margin: 34px auto 0;
  color: var(--ink);
  font-family: Georgia, "Noto Serif", "Times New Roman", serif;
  font-size: 1.12rem;
  line-height: 1.78;
}

/* RTL language support (Arabic, Farsi, Hebrew, Urdu…) */
.article-content[dir="rtl"] {
  text-align: right;
}

.article-content[dir="rtl"] ul,
.article-content[dir="rtl"] ol {
  padding-right: 1.25em;
  padding-left: 0;
}

.article-content[dir="rtl"] blockquote {
  padding-right: 1em;
  padding-left: 0;
  border-right: 3px solid var(--warm);
  border-left: none;
}

/* Wiktionary cross-links — subtle italic accent */
.article-content a[data-wiki-site="wiktionary"] {
  color: var(--accent-strong);
  font-style: italic;
  text-decoration: underline;
  text-decoration-style: dotted;
  text-underline-offset: 3px;
}

.article-content a[data-wiki-site="wiktionary"]:hover {
  text-decoration-style: solid;
}

.article-content > *:first-child {
  margin-top: 0;
}

.article-content h2,
.article-content h3,
.article-content h4 {
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, sans-serif;
  letter-spacing: 0;
}

.article-content h2 {
  margin: 2.3em 0 0.65em;
  padding-top: 0.7em;
  border-top: 1px solid var(--border);
  font-size: 1.65rem;
  line-height: 1.2;
}

.article-content h3 {
  margin: 1.8em 0 0.55em;
  font-size: 1.25rem;
}

.article-content p {
  margin: 0 0 1.1em;
}

.article-content figure {
  margin: 1.6em 0;
}

.article-content img {
  max-width: 100%;
  height: auto;
  border-radius: var(--radius);
}

.article-content figcaption {
  color: var(--ink-muted);
  font-family: Inter, ui-sans-serif, system-ui, sans-serif;
  font-size: 0.85rem;
  line-height: 1.45;
}

.article-content blockquote {
  margin: 1.4em 0;
  padding: 0.2em 0 0.2em 1em;
  border-left: 3px solid var(--warm);
  color: var(--ink-muted);
}

.article-content ul,
.article-content ol {
  padding-left: 1.25em;
}

.article-content li + li {
  margin-top: 0.35em;
}

.article-content .table-wrap {
  width: 100%;
  overflow-x: auto;
  margin: 1.5em 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.article-content table {
  width: 100%;
  border-collapse: collapse;
  font-family: Inter, ui-sans-serif, system-ui, sans-serif;
  font-size: 0.92rem;
  line-height: 1.45;
}

.article-content th,
.article-content td {
  padding: 10px;
  border: 1px solid var(--border);
  vertical-align: top;
}

.article-content th {
  background: var(--surface-muted);
  text-align: left;
}

.toc-panel {
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

#tocNav {
  display: grid;
  gap: 4px;
}

#tocNav a {
  display: block;
  overflow: hidden;
  padding: 8px 10px;
  border-radius: var(--radius);
  color: var(--ink-muted);
  text-decoration: none;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#tocNav a:hover {
  background: var(--surface-muted);
  color: var(--ink);
}

#tocNav a.h3 {
  padding-left: 22px;
  font-size: 0.9rem;
}

.site-footer {
  display: grid;
  gap: 10px;
  width: min(1540px, 100%);
  margin: 0 auto;
  padding: 10px clamp(16px, 3vw, 34px) 28px;
  color: var(--ink-muted);
  font-size: 0.85rem;
}

.site-footer p {
  margin: 0;
}

.site-footer nav {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
}

.legal-page {
  width: min(880px, calc(100% - 32px));
  margin: 0 auto;
  padding: 42px 0 70px;
}

.legal-page h1 {
  margin: 0 0 16px;
  font-size: clamp(2rem, 6vw, 4rem);
  line-height: 1;
  letter-spacing: 0;
}

.legal-page h2 {
  margin: 2em 0 0.6em;
  font-size: 1.25rem;
}

.legal-page p,
.legal-page li {
  color: var(--ink-muted);
  line-height: 1.7;
}

.legal-page a {
  overflow-wrap: anywhere;
}

@media (max-width: 1180px) {
  .app-shell {
    grid-template-columns: minmax(250px, 340px) minmax(0, 1fr);
  }

  .toc-panel {
    display: none;
  }
}

@media (max-width: 820px) {
  .topbar {
    position: static;
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .affiliation-note {
    justify-self: start;
    text-align: left;
  }

  .topbar-actions {
    justify-content: start;
    width: 100%;
    flex-wrap: wrap;
  }

  .app-shell {
    grid-template-columns: 1fr;
    padding-top: 16px;
  }

  .search-panel,
  .toc-panel {
    position: static;
    max-height: none;
  }

  .article-hero {
    grid-template-columns: 1fr;
  }

  .article-hero h1 {
    max-width: 100%;
    font-size: clamp(2.25rem, 14vw, 4.4rem);
  }

  .article-content {
    font-size: 1.06rem;
    padding: 0 2px;
  }
}

@media (max-width: 520px) {
  .brand-copy strong {
    font-size: 0.95rem;
  }

  .select-label select {
    width: 100%;
  }

  .auth-button {
    flex: 1;
  }

  .quick-links {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .article-hero {
    padding: 20px;
  }

  .article-actions {
    display: grid;
    grid-template-columns: 1fr;
  }

  .tool-button {
    width: 100%;
  }
}
