/* History page: a diary of what you said to each other, grouped by day. The
   list is the index; a conversation reads as chat bubbles. Read-only: no
   composer here. Loaded after app.css and overrides its older history rules. */

.history-page .hp-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; margin: 6px 0 12px; }
.history-page .hp-head .eyebrow { margin: 0 0 2px; }
.history-page .hp-head h2 { margin: 0; font-size: 28px; overflow-wrap: anywhere; }
.hp-refresh { flex: none; display: inline-flex; align-items: center; gap: 6px; min-height: 38px; padding: 0 14px; border: 1px solid var(--line); border-radius: 999px; color: #56524d; background: rgba(255, 255, 255, .7); font-size: 13px; }
.hp-refresh span { display: inline-block; font-family: system-ui, sans-serif; font-size: 16px; line-height: 1; }
.hp-refresh:disabled span { animation: hp-spin .7s linear infinite; }
@keyframes hp-spin { to { transform: rotate(360deg); } }

.hp-roles { display: flex; gap: 8px; margin: 0 -16px 14px; padding: 2px 16px; overflow-x: auto; scrollbar-width: none; }
.hp-roles::-webkit-scrollbar { display: none; }
.hp-role { flex: none; display: inline-flex; align-items: center; gap: 8px; min-height: 40px; max-width: 70vw; padding: 0 16px; border: 1px solid var(--line); border-radius: 999px; color: #56524d; background: rgba(255, 255, 255, .66); font-size: 14px; transition: background .2s, color .2s; }
.hp-role-avatar { flex: none; width: 26px; height: 26px; margin-left: -10px; border-radius: 50%; object-fit: cover; object-position: 50% 25%; }
.hp-role[aria-selected="true"] { border-color: transparent; color: #fff; background: var(--accent); }
.hp-role:focus-visible, .hp-item:focus-visible, .hp-back:focus-visible, .hp-refresh:focus-visible, .hp-more:focus-visible, .hp-delete:focus-visible, .hp-danger-link:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Session list */
.hp-list-col { display: grid; align-content: start; gap: 4px; }
.hp-list { display: grid; gap: 6px; }
.hp-day { margin: 12px 4px 2px; color: var(--muted); font-size: 12px; letter-spacing: .06em; }
.hp-day:first-child { margin-top: 0; }
.hp-item { position: relative; display: grid; grid-template-columns: 42px minmax(0, 1fr); align-items: center; gap: 12px; min-height: 66px; padding: 10px 14px 10px 10px; border: 1px solid transparent; border-radius: 18px; color: var(--ink); background: rgba(255, 253, 249, .92); box-shadow: 0 12px 30px -26px rgba(80, 45, 40, .5); text-align: left; transition: border-color .2s, background .2s; }
.hp-item[aria-current="true"] { border-color: rgba(141, 38, 55, .3); background: rgba(141, 38, 55, .05); }
.hp-item[aria-current="true"]::before { content: ""; position: absolute; left: -1px; top: 16px; bottom: 16px; width: 3px; border-radius: 3px; background: var(--accent); }
.hp-avatar { width: 42px; height: 42px; border-radius: 14px; object-fit: cover; object-position: 50% 25%; background: #e9dcd6; }
.hp-avatar.lg { width: 44px; height: 44px; }
.hp-avatar.sm { flex: none; width: 30px; height: 30px; border-radius: 10px; }
.hp-avatar-initial { display: grid; place-items: center; color: var(--accent); background: var(--accent-soft); font-family: "Songti SC", "STSong", serif; font-size: 16px; }
.hp-item-text strong { display: block; overflow: hidden; font-family: "Songti SC", "STSong", serif; font-size: 16px; font-weight: 500; text-overflow: ellipsis; white-space: nowrap; }
.hp-item-text small { color: var(--muted); font-size: 12px; }
.hp-empty { margin: 10px 4px; color: var(--muted); font-size: 14px; line-height: 1.7; }
.hp-more { justify-self: center; min-height: 42px; margin-top: 8px; padding: 0 22px; border: 1px solid var(--line); border-radius: 999px; color: #56524d; background: rgba(255, 255, 255, .7); font-size: 14px; }
.hp-count { margin: 4px 0 0; color: var(--muted); font-size: 12px; text-align: center; }
.hp-count:empty { display: none; }
.hp-policy { margin-top: 14px; padding: 12px 14px; border-radius: 16px; background: rgba(245, 241, 235, .85); font-size: 13px; }
.hp-policy summary { color: var(--ink); cursor: pointer; }
.hp-policy p { margin: 8px 0 0; color: var(--muted); line-height: 1.7; }
.hp-manage { display: flex; flex-wrap: wrap; justify-content: center; gap: 4px 18px; margin: 10px 0 4px; }
.hp-danger-link { min-height: 40px; padding: 0 4px; border: 0; border-radius: 8px; color: #9a4a55; background: none; font-size: 13px; text-decoration: underline; text-decoration-color: rgba(154, 74, 85, .35); text-underline-offset: 4px; }

/* Conversation */
.hp-detail-empty { display: grid; justify-items: center; align-content: center; gap: 6px; min-height: 320px; padding: 40px 20px; color: var(--muted); text-align: center; }
.hp-detail-empty span { display: grid; place-items: center; width: 56px; height: 56px; border-radius: 18px; color: var(--accent); background: var(--accent-soft); font-family: "Songti SC", "STSong", serif; font-size: 24px; }
.hp-detail-empty h3 { margin: 6px 0 0; color: var(--ink); font-family: "Songti SC", "STSong", serif; font-weight: 500; }
.hp-detail-empty p { margin: 0; font-size: 13px; }
.hp-conv-head { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; grid-template-areas: "av title del"; align-items: center; gap: 12px; padding: 0 0 14px; border-bottom: 1px solid var(--line); }
.hp-back { display: none; grid-area: back; }
.hp-conv-avatar { grid-area: av; display: block; }
.hp-conv-title { grid-area: title; min-width: 0; }
.hp-conv-title h3 { margin: 0; overflow: hidden; font-family: "Songti SC", "STSong", serif; font-size: 20px; font-weight: 500; text-overflow: ellipsis; white-space: nowrap; }
.hp-conv-title p { margin: 2px 0 0; color: var(--muted); font-size: 12px; }
.hp-delete { grid-area: del; min-height: 36px; padding: 0 14px; border: 1px solid rgba(154, 74, 85, .3); border-radius: 999px; color: #9a4a55; background: rgba(255, 255, 255, .7); font-size: 13px; white-space: nowrap; }
.hp-messages { display: grid; align-content: start; gap: 4px; padding: 16px 0 8px; }
.hp-sep { justify-self: center; margin: 0 0 10px; padding: 3px 12px; border-radius: 999px; color: var(--muted); background: rgba(245, 241, 235, .95); font-size: 11px; }
.hp-msg { display: flex; align-items: flex-end; gap: 8px; max-width: 86%; margin-top: 8px; }
.hp-msg.cont { margin-top: 2px; }
.hp-msg.from-user { justify-self: end; flex-direction: row-reverse; }
.hp-msg.from-role.cont .hp-avatar { visibility: hidden; }
.hp-bubble { min-width: 0; padding: 10px 14px 8px; border-radius: 18px 18px 18px 6px; background: rgba(255, 253, 249, .97); box-shadow: 0 8px 20px -18px rgba(80, 45, 40, .5); }
.hp-msg.from-user .hp-bubble { border-radius: 18px 18px 6px 18px; color: #fff; background: var(--accent); }
.hp-bubble p { margin: 0; font-size: 15px; line-height: 1.6; white-space: pre-wrap; overflow-wrap: anywhere; }
.hp-bubble small { display: block; margin-top: 3px; color: var(--muted); font-size: 11px; }
.hp-msg.from-user .hp-bubble small { color: rgba(255, 255, 255, .72); text-align: right; }

/* Phone: the list is the page; opening a conversation replaces it. */
@media (max-width: 760px) {
  #historyPanel.history-page.active { display: block; max-height: none; overflow: visible; }
  .history-page .hp-detail { display: none; }
  .history-page.mobile-history-detail .hp-head,
  .history-page.mobile-history-detail .hp-roles,
  .history-page.mobile-history-detail .hp-list-col { display: none; }
  .history-page.mobile-history-detail .hp-detail { display: block; }
  .history-page.mobile-history-detail .hp-detail-empty { display: none; }
  .hp-conv-head { position: sticky; z-index: 3; top: 0; grid-template-areas: "back back del" "av title title"; row-gap: 8px; margin: 0 -16px; padding: 8px 16px 12px; background: var(--bg); box-shadow: 0 10px 18px -16px rgba(80, 45, 40, .45); }
  .hp-back { display: inline-flex; align-items: center; justify-self: start; min-height: 36px; margin-left: -6px; padding: 0 6px; border: 0; border-radius: 10px; color: var(--muted); background: none; font-size: 14px; }
  .hp-back[hidden] { display: none; }
  .hp-delete { justify-self: end; min-height: 34px; }
  /* The bottom nav is fixed. #appView already pads 112px below the page;
     add the safe-area inset so the last message always scrolls clear. */
  .hp-messages { padding-bottom: calc(16px + env(safe-area-inset-bottom)); }
}

/* Desktop: main's fixed workspace (no page scroll), list and conversation
   side by side, each scrolling on its own. */
@media (min-width: 761px) {
  #historyPanel.history-page.active { display: flex !important; flex-direction: column; min-height: 0; overflow: hidden !important; }
  .history-page .hp-head, .history-page .hp-roles { flex: none; }
  .history-page .hp-roles { margin-inline: 0; padding-inline: 0; }
  .hp-layout { flex: 1 1 auto; min-height: 0; display: grid; grid-template-columns: 340px minmax(0, 1fr); gap: 22px; overflow: hidden; }
  .hp-list-col { min-height: 0; padding: 2px 6px 16px 2px; overflow-y: auto; overscroll-behavior: contain; }
  .hp-detail { min-height: 0; padding: 20px 24px 0; overflow-y: auto; overscroll-behavior: contain; border-radius: 26px; background: rgba(255, 253, 249, .74); box-shadow: 0 18px 46px -32px rgba(80, 45, 40, .45); }
  .hp-conv-head { position: sticky; z-index: 2; top: -20px; margin: -20px -24px 0; padding: 20px 24px 14px; border-radius: 26px 26px 0 0; background: #fcf9f5; }
  .hp-messages { padding-bottom: 24px; }
  .hp-msg { max-width: 72%; }
}

@media (prefers-reduced-motion: reduce) {
  .hp-refresh:disabled span { animation: none; }
}
