:root {
  --background: #f4eee1;
  --foreground: #2a2520;
  --card: #fffdf8;
  --primary: #9c4221;
  --primary-foreground: #fffaf2;
  --border: #e3d9c4;
  --muted: #efe7d6;
  --muted-foreground: #7a6e58;
  --faint-foreground: #a8977a;
  --app-surface: #fbf7ee;
  --app-rail: #efe7d6;
  --app-paper: #fdfaf2;
  --app-divider: #ece1cb;
  --app-hover: #f1e8d6;
  --app-shadow: rgba(80, 55, 20, 0.09);
  --reader-font-size: 18px;
  --reader-measure: 40rem;
  color-scheme: light;
  font-family: Inter, system-ui, sans-serif;
}

:root[data-reader-theme="sepia"] {
  --background: #efe3cb;
  --foreground: #43382a;
  --card: #fbf3df;
  --border: #d8c8a4;
  --muted: #e9ddc0;
  --muted-foreground: #7d6c50;
  --faint-foreground: #a0906e;
  --app-surface: #f3e9d2;
  --app-rail: #e9ddc0;
  --app-paper: #f7eed7;
  --app-divider: #e0d2b2;
  --app-hover: #e7d9ba;
  --app-shadow: rgba(90, 60, 20, 0.12);
}

:root[data-reader-theme="night"] {
  --background: #211e19;
  --foreground: #ece3d2;
  --card: #322d25;
  --primary: #e0996f;
  --primary-foreground: #211e19;
  --border: #3a352c;
  --muted: #332f27;
  --muted-foreground: #a89a82;
  --faint-foreground: #7e7561;
  --app-surface: #2a2620;
  --app-rail: #1b1813;
  --app-paper: #2e2a22;
  --app-divider: #332f27;
  --app-hover: #37322a;
  --app-shadow: rgba(0, 0, 0, 0.42);
}

body {
  background: var(--background);
  color: var(--foreground);
  margin: 0;
}

h1,
h2,
h3 {
  color: #3a2a1c;
  font-family: Newsreader, Georgia, serif;
  letter-spacing: -0.01em;
}

.topbar {
  align-items: center;
  background: var(--app-surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  gap: 1.25rem;
  height: 3.75rem;
  padding: 0 1.25rem;
}

.brand,
.profile {
  align-items: center;
  display: flex;
  gap: 0.7rem;
}

.brand {
  flex: 0 0 13rem;
}

.brand > span:last-child {
  display: grid;
}

.brand small {
  color: var(--faint-foreground);
  font-size: 0.65rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.brand-mark {
  align-items: center;
  background: var(--primary);
  border-radius: 8px;
  color: var(--primary-foreground);
  display: inline-flex;
  font-family: Newsreader, Georgia, serif;
  font-weight: 800;
  height: 1.9rem;
  justify-content: center;
  width: 1.9rem;
}

.topbar form {
  align-items: center;
  display: flex;
  flex: 1;
  gap: 0.5rem;
  margin: 0;
  max-width: 35rem;
}

.topbar form button {
  display: none;
}

.topbar input[type="search"] {
  background: var(--card);
  border-color: var(--border);
  border-radius: 9px;
  font-size: 0.86rem;
  min-height: 2.5rem;
}

.theme-dots {
  align-items: center;
  display: flex;
  gap: 0.45rem;
  margin-left: auto;
}

.theme-dot {
  border: 1px solid var(--border);
  border-radius: 999px;
  box-shadow: 0 0 0 2px var(--app-surface) inset;
  height: 1.05rem;
  min-height: 0;
  padding: 0;
  width: 1.05rem;
}

.theme-dot.rust { background: #9c4221; }
.theme-dot.paper { background: #efe3cb; }
.theme-dot.night { background: #211e19; }

.theme-dot:hover,
.theme-dot[aria-pressed="true"] {
  outline: 2px solid color-mix(in srgb, var(--primary) 45%, transparent);
  outline-offset: 2px;
}

.profile {
  border-left: 1px solid var(--border);
  color: var(--muted-foreground);
  font-size: 0.86rem;
  margin-left: 0.6rem;
  padding-left: 1.1rem;
}

.mailbox-shell {
  display: grid;
  grid-template-columns: 14.25rem minmax(21rem, 22rem) minmax(32rem, 1fr) minmax(18rem, 19rem);
  height: calc(100vh - 3.75rem);
  min-height: 34rem;
}

.rail-pane,
.pane {
  min-width: 0;
  overflow: auto;
}

.rail-pane {
  background: var(--app-rail);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 1rem 0.75rem 1.25rem;
}

.rail-pane h2 {
  color: var(--faint-foreground);
  font-family: Inter, system-ui, sans-serif;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  margin: 0.65rem 0 0.5rem;
  padding: 0 0.55rem;
  text-transform: uppercase;
}

.rail-nav,
.label-stack {
  display: grid;
  gap: 0.12rem;
}

.rail-item {
  align-items: center;
  background: transparent;
  border: 0;
  border-radius: 8px;
  color: #5a4a36;
  display: flex;
  font-size: 0.84rem;
  justify-content: space-between;
  padding: 0.48rem 0.6rem;
  text-align: left;
  width: 100%;
}

.rail-item:hover,
.rail-item[aria-current="true"] {
  background: var(--card);
  color: var(--primary);
}

.rail-item small,
.label-stack small {
  color: var(--faint-foreground);
}

.label-stack {
  color: #5a4a36;
  font-size: 0.84rem;
  padding: 0 0.6rem;
}

.label-stack span {
  align-items: center;
  display: flex;
  gap: 0.55rem;
  min-height: 1.9rem;
}

.dot {
  border-radius: 999px;
  display: inline-block;
  height: 0.55rem;
  width: 0.55rem;
}

.dot.personal { background: #a0566e; }
.dot.family { background: #5f7048; }
.dot.congress { background: #9c4221; }
.dot.source { background: #3f6470; }

.progress-card {
  background: #e7dcc6;
  border-radius: 9px;
  color: #5a4a36;
  display: grid;
  gap: 0.35rem;
  margin-top: auto;
  padding: 0.75rem;
}

.progress-card strong {
  color: var(--primary);
  font-size: 0.78rem;
}

.progress-card small {
  color: var(--muted-foreground);
}

.correspondence-pane {
  background: var(--app-surface);
  border-right: 1px solid var(--border);
  padding: 0;
}

.pane-header {
  border-bottom: 1px solid var(--app-divider);
  display: grid;
  gap: 0.7rem;
  padding: 0.9rem 1rem 0.8rem;
}

.list-title {
  align-items: baseline;
  display: flex;
  gap: 0.75rem;
  justify-content: space-between;
}

.list-title h2 {
  font-size: 1.35rem;
  font-weight: 500;
  margin: 0;
}

.list-title small {
  color: var(--faint-foreground);
}

.view-toggle {
  align-items: center;
  background: var(--muted);
  border-radius: 9px;
  display: flex;
  gap: 0.15rem;
  margin-left: auto;
  padding: 0.18rem;
}

.view-toggle button {
  border: 0;
  border-radius: 7px;
  font-size: 0.72rem;
  min-height: auto;
  padding: 0.24rem 0.65rem;
}

.view-toggle button[aria-pressed="true"] {
  background: var(--card);
  color: #3a2a1c;
}

.box-tabs {
  display: none;
  margin: 0;
}

.filter-row {
  display: grid;
  gap: 0.45rem;
  grid-template-columns: minmax(0, 1fr) 5.8rem 6rem;
}

.filter-row label {
  color: var(--faint-foreground);
  font-size: 0.7rem;
}

.filter-row select {
  background: var(--card);
  border-color: var(--border);
  font-size: 0.78rem;
  min-height: 1.9rem;
  padding-block: 0.25rem;
}

.quality-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.38rem;
}

.quality-row button {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 999px;
  color: #5a4a36;
  font-size: 0.72rem;
  min-height: auto;
  padding: 0.18rem 0.65rem;
}

.quality-row button[aria-pressed="true"] {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--primary-foreground);
}

.letter-list {
  display: grid;
  gap: 0.12rem;
  padding: 0.45rem 0.5rem 1rem;
}

.letter-list .mail-row {
  align-items: start;
  background: transparent;
  border: 0;
  border-left: 3px solid transparent;
  border-radius: 0;
  color: inherit;
  display: grid;
  gap: 0.7rem;
  grid-template-columns: 2.25rem minmax(0, 1fr);
  padding: 0.68rem 0.75rem;
  text-align: left;
  width: 100%;
}

.letter-list .mail-row:hover {
  background: var(--app-hover);
}

.letter-list .mail-row.is-selected {
  background: var(--card);
  border-left-color: var(--primary);
}

.avatar {
  align-items: center;
  aspect-ratio: 1;
  background: #f6e0d6;
  border-radius: 999px;
  color: var(--primary);
  display: inline-flex;
  flex: 0 0 auto;
  font-size: 0.8rem;
  font-weight: 700;
  justify-content: center;
  line-height: 1;
  margin-top: 0.1rem;
}

.avatar.small {
  height: 1.9rem;
  margin: 0;
  width: 1.9rem;
}

.mail-main {
  display: grid;
  gap: 0.16rem;
  min-width: 0;
}

.mail-topline {
  align-items: baseline;
  display: flex;
  gap: 0.5rem;
  justify-content: space-between;
  min-width: 0;
}

.mail-topline strong,
.mail-subject,
.mail-place {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mail-topline time {
  color: var(--faint-foreground);
  flex: 0 0 auto;
  font-size: 0.72rem;
}

.mail-topline strong {
  font-size: 0.86rem;
}

.mail-subject {
  color: #5a4a36;
  display: -webkit-box;
  font-family: Newsreader, Georgia, serif;
  font-size: 0.92rem;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-height: 1.35;
  overflow: hidden;
  white-space: normal;
}

.mail-place {
  color: var(--primary);
  font-size: 0.76rem;
}

.mail-labels {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  margin-top: 0.25rem;
}

.mail-label {
  background: #f3ecd0;
  border-radius: 5px;
  color: #8a6a15;
  font-size: 0.65rem;
  line-height: 1.3;
  padding: 0.1rem 0.4rem;
}

.reader-pane {
  background: var(--background);
  display: flex;
  flex-direction: column;
  padding: 0;
}

.reader-toolbar {
  align-items: center;
  background: var(--app-surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  gap: 1rem;
  height: 3.1rem;
  justify-content: flex-end;
  padding: 0 1.5rem;
}

.reader-toolbar span:first-child {
  background: var(--card);
  border-radius: 7px;
  box-shadow: 0 1px 4px var(--app-shadow);
  font-size: 0.78rem;
  font-weight: 700;
  padding: 0.3rem 0.8rem;
}

.reader-control {
  background: transparent;
  border: 0;
  border-radius: 7px;
  color: var(--muted-foreground);
  font-size: 0.82rem;
  font-weight: 700;
  min-height: 1.8rem;
  padding: 0.25rem 0.5rem;
  width: auto;
}

.reader-control:hover,
.reader-control[aria-pressed="true"] {
  background: var(--muted);
  color: var(--primary);
}

.letter-reader {
  background: var(--app-paper);
  border: 1px solid var(--app-divider);
  border-radius: 4px;
  box-shadow: 0 2px 18px var(--app-shadow);
  margin: 2.4rem auto 4rem;
  max-width: var(--reader-measure);
  padding: 3rem 3.5rem 3.5rem;
  transition: max-width 160ms ease, background-color 160ms ease, color 160ms ease;
  width: min(calc(100% - 5rem), var(--reader-measure));
}

.letter-reader h1 {
  font-size: clamp(1.8rem, 2.5vw, 2.4rem);
  line-height: 1.12;
  margin: 0 0 0.6rem;
}

.letter-kicker {
  color: var(--faint-foreground);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  margin-bottom: 0.9rem;
  text-transform: uppercase;
}

.letter-body {
  color: var(--foreground);
  font-family: Newsreader, Georgia, serif;
  font-size: var(--reader-font-size);
  line-height: 1.78;
  white-space: pre-wrap;
}

.context-pane {
  background: var(--app-rail);
  border-left: 1px solid var(--border);
  padding: 1.35rem 1.1rem 1.75rem;
}

.context-pane h2 {
  color: var(--faint-foreground);
  font-family: Inter, system-ui, sans-serif;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  margin: 0 0 0.75rem;
  text-transform: uppercase;
}

.meta-list {
  display: grid;
  gap: 0.85rem;
}

.context-person {
  align-items: center;
  display: flex;
  gap: 0.75rem;
}

.context-person strong {
  color: #3a2a1c;
  display: block;
  font-family: Newsreader, Georgia, serif;
  font-size: 1.1rem;
}

.section-rule {
  border-top: 1px solid var(--app-divider);
  padding-top: 0.9rem;
}

.section-rule h3 {
  color: var(--faint-foreground);
  font-family: Inter, system-ui, sans-serif;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  margin: 0 0 0.65rem;
  text-transform: uppercase;
}

.place-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 0.75rem;
}

.place-chips button {
  background: #f6e3d4;
  border: 1px solid #ecd0bb;
  border-radius: 999px;
  color: var(--primary);
  font-size: 0.74rem;
  min-height: auto;
  padding: 0.18rem 0.65rem;
}

.mini-map {
  background:
    linear-gradient(rgba(120, 95, 55, 0.12) 1px, transparent 1px),
    linear-gradient(90deg, rgba(120, 95, 55, 0.12) 1px, transparent 1px),
    linear-gradient(135deg, #e8dcc4, #ddcdad);
  background-size: 100% 50%, 50% 100%, auto;
  border: 1px solid var(--app-divider);
  border-radius: 8px;
  height: 7.2rem;
  overflow: hidden;
  position: relative;
}

.mini-map span {
  background: var(--primary);
  border-radius: 999px;
  box-shadow: 0 0 0 5px rgba(156, 66, 33, 0.16);
  height: 0.7rem;
  left: 62%;
  position: absolute;
  top: 62%;
  transform: translate(-50%, -50%);
  width: 0.7rem;
}

.mini-map strong {
  color: #3a2a1c;
  font-size: 0.72rem;
  left: calc(62% + 0.55rem);
  position: absolute;
  top: calc(62% - 1rem);
}

.metadata-list {
  display: grid;
  gap: 0.5rem;
  margin: 0;
}

.metadata-list > div {
  border-bottom: 1px solid var(--app-divider);
  display: grid;
  gap: 0.18rem;
  padding-bottom: 0.45rem;
}

.metadata-list dt {
  color: var(--faint-foreground);
  font-size: 0.72rem;
}

.metadata-list dd {
  color: #5a4a36;
  margin: 0;
}

.badge.warning {
  background: #fef3c7;
  color: #9a5b13;
}

.source-card {
  border-top: 1px solid var(--app-divider);
  padding-top: 0.75rem;
}

.source-card a {
  color: var(--primary);
  font-size: 0.78rem;
}

.review-shell {
  display: grid;
  gap: 1rem;
  grid-template-columns: minmax(18rem, 28rem) minmax(28rem, 1fr);
  padding: 1rem;
}

.review-shell > section {
  min-width: 0;
}

@media (max-width: 1120px) {
  .mailbox-shell {
    grid-template-columns: 12rem minmax(20rem, 23rem) minmax(28rem, 1fr);
  }

  .context-pane {
    display: none;
  }
}

@media (max-width: 840px) {
  .topbar {
    align-items: stretch;
    display: grid;
    height: auto;
    padding: 0.8rem;
  }

  .brand,
  .profile {
    flex: none;
  }

  .mailbox-shell {
    grid-template-columns: 1fr;
    height: auto;
  }

  .rail-pane {
    display: none;
  }

  .correspondence-pane,
  .reader-pane,
  .context-pane {
    border: 0;
    display: block;
    min-height: auto;
  }

  .letter-reader {
    margin: 1rem;
    padding: 1.5rem;
    width: auto;
  }
}
