.chapter-layout {
  display: grid;
  grid-template-columns: minmax(0, 820px) 260px;
  grid-template-areas:
    "head aside"
    "lead aside"
    "outline aside"
    "article aside"
    "pager aside";
  column-gap: 32px;
  align-items: start;
  width: min(100%, 1112px);
  margin: 0 auto;
}

body[data-annotation-key="stm32-notes-annotations-v1"] .chapter-layout {
  --annotation-top: 100px;
}

body[data-annotation-key="stm32-recite-annotations-v1"] .chapter-layout {
  --annotation-top: 64px;
}

.chapter-layout .chapter-head { grid-area: head; }
.chapter-layout .chapter-lead { grid-area: lead; }
.chapter-layout .chapter-outline { grid-area: outline; }
.chapter-layout .notes-article,
.chapter-layout .recite-article { grid-area: article; }
.chapter-layout .chapter-pager { grid-area: pager; }

.annotation-panel {
  grid-area: aside;
  position: sticky;
  top: var(--annotation-top, 64px);
  align-self: start;
  max-height: calc(100vh - var(--annotation-top, 64px) - 22px);
  overflow-y: auto;
  padding: 5px 0 16px 18px;
  color: var(--ink-soft);
  border-left: 1px solid var(--line);
  scrollbar-width: thin;
  scrollbar-color: rgba(29, 32, 26, 0.2) transparent;
}

.annotation-details {
  display: block;
}

.annotation-details > summary {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  padding: 4px 0 10px;
  color: var(--ink-soft);
  cursor: pointer;
  list-style: none;
  font-family: var(--mono);
  font-size: 9.5px;
  letter-spacing: 0.04em;
}

.annotation-details > summary::-webkit-details-marker {
  display: none;
}

.annotation-details > summary::after {
  content: "+";
  color: var(--accent);
  font-family: var(--sans);
  font-size: 14px;
  font-weight: 300;
  line-height: 1;
}

.annotation-details[open] > summary::after {
  content: "−";
}

.annotation-title {
  min-width: 0;
}

.annotation-count {
  margin-left: auto;
  color: var(--ink-faint);
  font-size: 9.5px;
  letter-spacing: 0.02em;
}

.annotation-count b {
  color: var(--accent-deep);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.annotation-body {
  padding: 0 0 8px;
}

.annotation-hint {
  margin: 0 0 8px;
  color: var(--ink-faint);
  font-family: var(--mono);
  font-size: 9.5px;
  line-height: 1.6;
  letter-spacing: 0.02em;
}

.annotation-input {
  display: block;
  width: 100%;
  min-height: 96px;
  padding: 9px 10px;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.32);
  border: 1px solid var(--line);
  border-radius: 2px;
  font-family: var(--sans);
  font-size: 12px;
  line-height: 1.7;
  resize: vertical;
}

.annotation-input::placeholder {
  color: var(--ink-faint);
}

.annotation-input:focus {
  outline: none;
  border-color: rgba(198, 94, 56, 0.52);
  background: rgba(255, 255, 255, 0.55);
}

.annotation-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin: 8px 0 2px;
}

.annotation-kbd {
  color: var(--ink-faint);
  font-family: var(--mono);
  font-size: 8.5px;
  letter-spacing: 0.02em;
  white-space: nowrap;
}

.annotation-add {
  flex: 0 0 auto;
  padding: 5px 9px;
  color: var(--accent-deep);
  background: rgba(255, 255, 255, 0.3);
  border: 1px solid rgba(198, 94, 56, 0.35);
  border-radius: 2px;
  cursor: pointer;
  font-family: var(--mono);
  font-size: 9.5px;
  letter-spacing: 0.02em;
}

.annotation-add:hover {
  color: #fff;
  background: var(--accent-deep);
  border-color: var(--accent-deep);
}

.annotation-list {
  margin: 9px 0 0;
  padding: 0;
  list-style: none;
}

.annotation-item {
  padding: 9px 0;
  border-top: 1px solid var(--line);
}

.annotation-text {
  margin: 0 0 5px;
  color: var(--ink);
  font-size: 12px;
  line-height: 1.72;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.annotation-item-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  color: var(--ink-faint);
  font-family: var(--mono);
  font-size: 9px;
  letter-spacing: 0.02em;
}

.annotation-delete {
  padding: 0;
  color: var(--ink-faint);
  background: transparent;
  border: 0;
  border-bottom: 1px solid transparent;
  cursor: pointer;
  font-family: var(--mono);
  font-size: 9px;
}

.annotation-delete:hover {
  color: var(--accent-deep);
  border-bottom-color: rgba(198, 94, 56, 0.4);
}

.annotation-empty {
  margin: 10px 0 0;
  color: var(--ink-faint);
  font-family: var(--mono);
  font-size: 9.5px;
  line-height: 1.6;
}

@media (max-width: 1200px) {
  .chapter-layout {
    display: block;
    width: auto;
  }

  .annotation-panel {
    position: static;
    width: auto;
    max-width: 820px;
    max-height: none;
    overflow: visible;
    margin: 16px auto 0;
    padding: 4px 0 0;
    border-top: 1px solid var(--line);
    border-bottom: 1px solid var(--line);
    border-left: 0;
  }

  .annotation-details > summary {
    padding: 8px 0;
  }

  .annotation-body {
    padding-bottom: 12px;
  }
}

@media (max-width: 680px) {
  .annotation-panel {
    margin-top: 14px;
  }

  .annotation-input {
    min-height: 84px;
  }
}

@media print {
  .annotation-panel {
    display: none !important;
  }
}
