* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  max-width: 100%;
  height: 100%;
  overflow-x: hidden;
}

body {
  margin: 0;
  overflow: hidden;
  background: #efe7d5;
  color: #1f160d;
  font-family: Georgia, serif;
}

button {
  padding: 7px 11px;
  border: 1px solid #b9a77f;
  border-radius: 7px;
  background: #fffdf5;
  color: #1f160d;
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.08);
  cursor: pointer;
  font-family: Georgia, serif;
  font-size: 14px;
  white-space: nowrap;
}

button:hover {
  background: #f4ead4;
}

button:disabled {
  opacity: 0.48;
  cursor: default;
}

button:disabled:hover {
  background: #fffdf5;
}

button:focus-visible,
input:focus-visible,
select:focus-visible,
a:focus-visible,
[tabindex]:focus-visible {
  outline: 3px solid #b89b55;
  outline-offset: 2px;
}

input,
select {
  color: #1f160d;
  font-family: Georgia, serif;
  font-size: 15px;
}

.app {
  display: grid;
  grid-template-columns: clamp(280px, 22vw, 365px) minmax(0, 1fr);
  width: 100vw;
  max-width: 100vw;
  height: 100vh;
  min-width: 0;
  min-height: 0;
}

.sidebar {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  min-width: 0;
  overflow: hidden;
  border-right: 1px solid #cabd9f;
  background: #e7dcc7;
}

.filterbox {
  position: relative;
  z-index: 2;
  padding: 12px 12px 14px 16px;
  border-bottom: 1px solid #cabd9f;
  background: #e7dcc7;
  box-shadow: 0 2px 6px rgba(45, 58, 40, 0.08);
}

.filterbox h2 {
  margin: 0 0 10px;
  color: #2d3a28;
  font-size: 1.28rem;
}

.filter-label {
  display: block;
  margin: 9px 0 4px;
  color: #4c412e;
  font-size: 0.92rem;
  font-weight: bold;
}

.filterbox input,
.filterbox select {
  width: 100%;
  padding: 8px 9px;
  border: 1px solid #bbaa88;
  border-radius: 7px;
  background: #fffdf8;
}

.meta-search-row,
.sidebar-search-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 6px;
  align-items: center;
}

.meta-search-row button {
  padding: 7px 10px;
}

.search-nav-row {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-wrap: wrap;
  margin-top: 6px;
}

.search-nav-row button {
  padding: 6px 8px;
  font-size: 0.83rem;
}

.search-nav-row span {
  min-width: 44px;
  margin-left: auto;
  color: #5f543f;
  font-size: 0.88rem;
  text-align: right;
}

.metadata-nav-row {
  margin-bottom: 8px;
}

.date-range-filter {
  display: block;
  margin: 13px 0 11px;
  padding-top: 8px;
  border-top: 1px solid #cabd9f;
}

.date-filter-title {
  display: block;
  margin: 0 0 4px;
  color: #4c412e;
  font-size: 0.92rem;
  font-weight: bold;
}

.date-row {
  display: grid;
  grid-template-columns: 72px minmax(0, 1fr) minmax(0, 1.15fr) minmax(0, 1fr);
  gap: 8px;
  align-items: center;
  margin-top: 6px;
}

.date-range-label {
  align-self: center;
  color: #4c412e;
  font-size: 0.92rem;
  font-weight: bold;
  line-height: 1.15;
}

.date-range-filter select {
  width: 100%;
  min-width: 0;
  padding: 7px 6px;
  border: 1px solid #bbaa88;
  border-radius: 7px;
  background: #fffdf8;
}

#nameFilter,
#placeFilter {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  text-overflow: ellipsis;
}

.transcript-search-block {
  margin-top: 13px;
  padding-top: 10px;
  border-top: 1px solid #cabd9f;
}

.sidebar-search-row input {
  min-width: 0;
}

.search-help {
  margin: 4px 0 5px;
  color: #6b604b;
  font-size: 0.78rem;
  line-height: 1.25;
}

.count {
  z-index: 1;
  margin: 0;
  padding: 6px 12px 8px 16px;
  border-top: 1px solid #cabd9f;
  border-bottom: 1px solid #cabd9f;
  background: #e7dcc7;
  color: #5f543f;
  font-size: 0.88rem;
}

.letter-list {
  z-index: 0;
  min-height: 0;
  overflow: auto;
  padding: 8px 12px 14px 16px;
  background: #e7dcc7;
}

.letter-item {
  margin: 0 0 9px;
  padding: 10px 12px;
  overflow: visible;
  border: 1px solid #d6c8a9;
  border-radius: 10px;
  background: #fbf8ef;
  cursor: pointer;
}

.letter-item:hover,
.letter-item.active {
  border-color: #b89b55;
  background: #fff4d6;
}

.letter-inv {
  display: inline-block;
  margin-right: 5px;
  padding: 1px 7px;
  border-radius: 12px;
  background: #235b42;
  color: #fff;
  font-size: 0.84rem;
  font-weight: bold;
}

.letter-date {
  display: inline;
  font-size: 1.03rem;
  font-weight: bold;
}

.letter-names {
  margin-top: 3px;
  overflow-wrap: anywhere;
  font-size: 0.98rem;
  line-height: 1.22;
}

.letter-id {
  margin-top: 5px;
  overflow-wrap: anywhere;
  color: #6b604b;
  font-size: 0.82rem;
  line-height: 1.25;
  white-space: normal;
}

.mainpane,
.content-grid,
.scanpane,
.transcript-pane,
.metadata-pane {
  min-width: 0;
}

.mainpane {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: #f7f1e5;
}

.masthead {
  padding: 10px 18px 12px;
  border-bottom: 3px solid #b89b55;
  background: #2d3a28;
  color: #f8f1df;
  text-align: center;
}

.masthead h1 {
  margin: 0 0 5px;
  font-size: 1.55rem;
  line-height: 1.2;
}

.masthead p {
  max-width: 1280px;
  margin: 0 auto;
  color: #eadfbd;
  font-size: 0.82rem;
  line-height: 1.38;
  text-align: left;
}

.top-tools {
  display: block;
  overflow: hidden;
  padding: 8px 14px 8px 18px;
  border-bottom: 1px solid #d4c5a6;
  background: #fbf6ea;
}

.letter-actions-top {
  display: flex;
  gap: 8px;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
}

.letterbar {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 54px;
  padding: 8px 18px 5px;
  border-bottom: 0;
  background: #f5eddb;
}

.letter-heading {
  min-width: 0;
  max-width: 1200px;
  text-align: center;
}

.letter-heading h2 {
  margin: 0;
  overflow: hidden;
  font-size: 1.23rem;
  line-height: 1.18;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.letter-heading p {
  margin: 4px 0 0;
  overflow: hidden;
  color: #5b4f39;
  font-size: 0.9rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.page-tabs {
  display: flex;
  gap: 8px;
  align-items: center;
  height: 44px;
  overflow-x: auto;
  padding: 2px 18px 6px;
  border-bottom: 1px solid #d4c5a6;
  background: #f5eddb;
}

.page-tab {
  padding: 6px 12px;
  border: 1px solid #cabd9f;
  border-radius: 7px;
  background: #fffdf5;
  cursor: pointer;
  font-size: 0.93rem;
  font-weight: bold;
}

.page-tab.active {
  border-color: #235b42;
  background: #235b42;
  color: #fff;
}

.content-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.45fr) minmax(220px, 0.48fr) minmax(220px, 0.42fr);
  flex: 1;
  width: 100%;
  max-width: 100%;
  min-height: 0;
  overflow: hidden;
  background: #d3c9b6;
}

.content-grid.has-splitter {
  grid-template-columns: minmax(320px, 1fr) 8px minmax(260px, var(--transcript-col-px, 340px)) minmax(210px, 0.42fr);
}

.content-grid.has-splitter .scanpane {
  grid-column: 1;
}

.content-grid.has-splitter .pane-splitter {
  grid-column: 2;
  grid-row: 1;
}

.content-grid.has-splitter .transcript-pane {
  grid-column: 3;
}

.content-grid.has-splitter .metadata-pane {
  grid-column: 4;
}

.scanpane {
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-right: 1px solid #c4b797;
}

.scanbar {
  display: flex;
  gap: 7px;
  align-items: center;
  height: 50px;
  overflow-x: auto;
  padding: 8px 14px;
  background: #d2c8b5;
}

.scanbar span {
  color: #4e4432;
  font-size: 0.9rem;
}

.scanbar-sep {
  display: inline-block;
  width: 1px;
  height: 26px;
  margin: 0 4px;
  background: #b9aa8d;
}

.image-wrap {
  position: relative;
  flex: 1;
  overflow: auto;
  padding: 18px;
  background: #d3c9b6;
  cursor: grab;
  text-align: center;
  user-select: none;
}

.image-wrap.dragging {
  cursor: grabbing;
}

.image-wrap img {
  position: relative;
  z-index: 1;
  display: block;
  max-width: none;
  margin: 0 auto;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.18), 0 4px 18px rgba(0, 0, 0, 0.25);
  user-select: none;
  -webkit-user-drag: none;
}

.line-overlay {
  position: absolute;
  z-index: 3;
  top: 0;
  left: 0;
  pointer-events: none;
}

.line-box {
  position: absolute;
  display: block;
  border: 2px solid transparent;
  background: transparent;
  pointer-events: auto;
}

.line-box.active,
.line-box:hover {
  border-color: rgba(45, 95, 210, 0.95);
  background: rgba(45, 95, 210, 0.16);
}

.transcript-pane {
  overflow: auto;
  padding: 0;
  border-right: 1px solid #d4c5a6;
  background: #fffdf8;
}

.transcript-head {
  position: sticky;
  z-index: 3;
  top: 0;
  margin-bottom: 7px;
  padding: 10px 12px 7px;
  border-bottom: 1px solid #eadfc8;
  background: #fffdf8;
  box-shadow: 0 1px 0 #eadfc8;
}

.transcript-pane h2,
.metadata h2 {
  margin: 0 0 4px;
  font-size: 1.05rem;
}

.transcript {
  padding: 0 12px 12px;
  font-size: 0.97rem;
  line-height: 1.4;
  user-select: text;
}

.transcript .empty {
  margin: 0;
  padding: 8px 0 12px;
}

.tline {
  margin-bottom: 1px;
  padding: 2px 4px;
  border-radius: 4px;
}

.tline:hover,
.tline.active-line {
  background: #efe4c8;
}

.tline.active-line {
  outline: 1px solid #d4bd82;
}

.tline.search-current {
  outline: 1px solid #c89f30;
  background: #ffe7a6;
}

.search-hit {
  padding: 0 1px;
  border-radius: 2px;
  background: #ffe18a;
}

.empty {
  color: #7c705a;
  font-style: italic;
}

.metadata-pane {
  overflow: auto;
  padding: 16px;
  background: #fbf6ea;
}

.metadata {
  overflow-wrap: anywhere;
  font-size: 0.92rem;
  line-height: 1.3;
}

.meta-row {
  margin: 0 0 10px;
  overflow-wrap: anywhere;
}

.meta-label {
  display: block;
  font-weight: bold;
}

.inventory-desc-meta {
  color: #5f543f;
  font-style: italic;
}

.metadata a {
  color: #14573d;
  font-weight: bold;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.metadata-note {
  margin: 2px 0 12px;
  padding: 7px 8px;
  border: 1px solid #d6c8a9;
  border-radius: 8px;
  background: #f5eddb;
  color: #5f543f;
  font-size: 0.82rem;
  line-height: 1.35;
}

.emlo-link-row {
  margin-top: 14px;
  padding-top: 10px;
  border-top: 1px solid #d6c8a9;
}

.emlo-link-row a {
  display: inline-block;
  padding: 7px 10px;
  border: 1px solid #b9a77f;
  border-radius: 7px;
  background: #fffdf5;
  text-decoration: none;
}

.emlo-link-row a:hover {
  background: #f4ead4;
  text-decoration: underline;
}

.emlo-link-row .emlo-id-link {
  display: inline-flex;
  gap: 7px;
  align-items: center;
  width: max-content;
  max-width: 100%;
}

.emlo-link-row .emlo-open {
  display: inline-block;
  padding: 3px 7px;
  border: 1px solid #b9a77f;
  border-radius: 7px;
  background: #fffdf5;
  font-size: 0.84rem;
  font-weight: normal;
  text-decoration: none;
}

.emlo-link-row .emlo-id-link:hover .emlo-open {
  background: #f4ead4;
  text-decoration: underline;
}

.actions {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  flex-wrap: wrap;
  margin: 14px 0 10px;
}

.pane-splitter {
  z-index: 6;
  min-width: 8px;
  border-right: 1px solid #b7a98c;
  border-left: 1px solid #b7a98c;
  background: #c7baa0;
  cursor: col-resize;
}

.pane-splitter:hover,
body.resizing-transcript .pane-splitter {
  background: #9f8f6f;
}

body.resizing-transcript {
  cursor: col-resize;
  user-select: none;
}

@media (max-width: 1450px) {
  .app {
    grid-template-columns: clamp(260px, 24vw, 335px) minmax(0, 1fr);
  }

  .content-grid,
  .content-grid.has-splitter {
    grid-template-columns: minmax(320px, 1fr) 8px minmax(280px, var(--transcript-col-px, 380px));
    grid-template-rows: minmax(0, 1fr) minmax(150px, 0.55fr);
  }

  .content-grid.has-splitter .scanpane {
    grid-column: 1;
    grid-row: 1 / span 2;
  }

  .content-grid.has-splitter .pane-splitter {
    grid-column: 2;
    grid-row: 1 / span 2;
  }

  .content-grid.has-splitter .transcript-pane {
    grid-column: 3;
    grid-row: 1;
    border-right: 0;
    border-bottom: 1px solid #d4c5a6;
  }

  .content-grid.has-splitter .metadata-pane {
    display: block;
    grid-column: 3;
    grid-row: 2;
    padding: 12px;
  }

  .metadata {
    font-size: 0.88rem;
  }

  .letter-heading h2,
  .letter-heading p {
    white-space: normal;
  }
}

@media (max-width: 1300px) {
  .date-row {
    grid-template-columns: 68px minmax(0, 1fr) minmax(0, 1fr) minmax(0, 1fr);
    gap: 6px;
  }
}

@media (max-width: 950px) {
  body {
    overflow: auto;
  }

  .app {
    display: block;
    width: 100%;
    max-width: 100%;
    height: auto;
    min-height: 100vh;
  }

  .sidebar {
    height: auto;
    max-height: none;
    border-right: 0;
    border-bottom: 1px solid #cabd9f;
  }

  .mainpane {
    height: auto;
    min-height: 100vh;
  }

  .letter-actions-top {
    justify-content: flex-start;
    margin-bottom: 8px;
  }

  .search-nav-row span {
    margin-left: 0;
    text-align: left;
  }

  .content-grid,
  .content-grid.has-splitter {
    display: block;
  }

  .pane-splitter {
    display: none;
  }

  .scanpane {
    height: 70vh;
    border-right: 0;
    border-bottom: 1px solid #c4b797;
  }

  .transcript-pane {
    height: 35vh;
    border-right: 0;
    border-bottom: 1px solid #d4c5a6;
  }

  .metadata-pane {
    display: block;
    height: auto;
    max-height: 40vh;
    border-top: 1px solid #d4c5a6;
  }
}

@media (max-width: 900px) {
  .sidebar {
    height: 55vh;
  }

  .filterbox {
    max-height: 42vh;
    overflow: auto;
  }
}
