/* Pearl surfaces and scoped diary and chat treatments. */
:is(#pg-diary, #pg-chat, #convPanel) {
  --pearl-ink: #302e2a;
  --pearl-muted: #605d57;
  --pearl-paper: rgba(250, 248, 243, .91);
  --pearl-edge: rgba(82, 77, 68, .24);
  --pearl-shadow: rgba(24, 23, 21, .17);
  --pearl-display: Georgia, "Times New Roman", "Songti SC", serif;
  color: var(--pearl-ink);
}
:is(#pg-diary, #pg-chat) [hidden] { display: none !important; }
.pearl-surface {
  background: var(--pearl-paper);
  -webkit-backdrop-filter: var(--pearl-filter, blur(14px) saturate(.8));
  backdrop-filter: var(--pearl-filter, blur(14px) saturate(.8));
  border: 1px solid var(--pearl-edge);
  border-radius: 18px;
  box-shadow: inset 0 1px rgba(255, 255, 255, .76), 0 5px 16px var(--pearl-shadow);
}
.pearl-button {
  min-height: 36px;
  padding: 5px 10px;
  border: 1px solid var(--pearl-edge);
  border-radius: 10px;
  background: var(--pearl-button-fill, rgba(255, 255, 255, .48));
  color: var(--pearl-ink);
  font: inherit;
  text-decoration: none;
  cursor: pointer;
}
.pearl-button:disabled { opacity: .55; cursor: default; }
:is(#pg-diary, #pg-chat) :focus-visible { outline: 2px solid #69665f; outline-offset: 2px; }

#pg-diary {
  --pearl-ink: #34363a;
  --pearl-muted: #383b41;
  --pearl-diary-rgb: 244, 245, 247;
  --pearl-edge: rgba(250, 251, 253, .58);
  --pearl-shadow: rgba(24, 26, 30, .10);
  --pearl-button-fill: rgba(var(--pearl-diary-rgb), .35);
  --pearl-field-fill: rgba(var(--pearl-diary-rgb), .38);
  --pearl-field-filter: blur(6px) saturate(.85);
  --diary-copy: system-ui, "PingFang SC", sans-serif;
  --diary-rule: rgba(80, 84, 90, .20);
  font-family: var(--diary-copy);
}
#pg-diary :focus-visible { outline-color: var(--pearl-muted); }
#pg-diary .diary-scroll {
  padding: max(14px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
    max(90px, calc(78px + env(safe-area-inset-bottom))) max(16px, env(safe-area-inset-left));
}
.diary-masthead {
  position: relative;
  margin: 0 auto 12px;
  width: min(100%, 390px);
  padding: 3px 0 0;
  text-align: center;
  isolation: isolate;
}
.diary-masthead::before {
  content: "";
  position: absolute;
  z-index: -1;
  inset: 2% 0 0;
  background: radial-gradient(ellipse, rgba(244, 245, 247, .76) 12%, transparent 73%);
  pointer-events: none;
}
.diary-heading {
  font: italic 400 clamp(47px, 15vw, 67px)/.96 "Snell Roundhand", "Apple Chancery", "Z003", var(--pearl-display);
  letter-spacing: .02em;
  padding-inline-start: .08em;
  text-shadow: 0 1px 7px rgba(248, 249, 250, .72);
}
#pg-diary .diary-illustration {
  position: relative;
  display: block;
  width: min(77%, 254px);
  aspect-ratio: 300 / 160;
  margin: 0 auto;
}
#diaryImageOpen { padding: 0; border: 0; background: transparent; cursor: pointer; }
#pg-diary :is(.diary-image-default, .diary-image-frame) {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}
#pg-diary .diary-image-crop {
  position: absolute;
  left: 11%;
  top: 16.25%;
  width: 78%;
  height: 65%;
  overflow: hidden;
  clip-path: ellipse(50% 50%);
}
#pg-diary .diary-image-crop img { position: absolute; max-width: none; pointer-events: none; }
#pg-diary .diary-image-preview .diary-image-crop { touch-action: none; cursor: grab; }
#pg-diary .diary-image-preview .diary-image-crop:active { cursor: grabbing; }
#pg-diary .diary-image-dialog {
  --pearl-paper: rgba(var(--pearl-diary-rgb), .95);
  position: fixed;
  inset: 0;
  margin: auto;
  width: min(calc(100% - 32px), 360px);
  max-height: calc(100dvh - max(32px, env(safe-area-inset-top) + env(safe-area-inset-bottom)));
  overflow: auto;
  padding: 16px;
  color: var(--pearl-ink);
}
#pg-diary .diary-image-dialog::backdrop { background: rgba(27, 29, 33, .28); }
#diaryImageTitle { font-size: 16px; font-weight: 500; }
#pg-diary .diary-image-file, #pg-diary .diary-image-controls label {
  display: grid;
  margin-top: 7px;
  gap: 4px;
  font-size: 13px;
}
#pg-diary .diary-image-controls label { grid-template-columns: 3em minmax(0, 1fr); align-items: center; }
#diaryImageFile { min-width: 0; width: 100%; font: inherit; }
#diaryImageStatus:not(:empty) { margin-top: 8px; font-size: 12px; }
.diary-calendar, .diary-card, .diary-status {
  width: min(100%, 390px);
  margin-inline: auto;
}
.diary-calendar {
  --pearl-paper: rgba(var(--pearl-diary-rgb), .64);
  --pearl-filter: blur(12px) saturate(.82);
  padding: 10px 8px 8px;
}
.diary-week-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 5px 8px;
  padding: 0 4px 7px;
}
#diaryWeek {
  color: var(--pearl-muted);
  font: 400 13px/1.3 var(--pearl-display);
}
.diary-week-row {
  display: grid;
  grid-template-columns: 22px minmax(0, 1fr) 22px;
  align-items: end;
  gap: 1px;
}
.diary-week-arrow {
  border: 0;
  background: transparent;
  color: var(--pearl-muted);
  font: 400 24px/1 var(--pearl-display);
  min-height: 38px;
  cursor: pointer;
}
.diary-week-arrow:disabled { cursor: default; }
#pg-diary .cal-grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  row-gap: 2px;
  text-align: center;
}
#pg-diary .cal-day-name {
  color: var(--pearl-muted);
  font-size: 10px;
  line-height: 1.4;
}
#pg-diary .cal-day {
  display: grid;
  place-items: center;
  position: relative;
  isolation: isolate;
  min-width: 0;
  height: 38px;
  padding: 0 0 3px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: inherit;
  font: 400 16px/1 var(--diary-copy);
  font-variant-numeric: lining-nums tabular-nums;
  cursor: pointer;
}
#pg-diary .cal-day.selected::before {
  content: "";
  position: absolute;
  z-index: -1;
  inset: 3px 0 6px;
  width: min(100% - 2px, 29px);
  margin-inline: auto;
  border: 1px dotted #656970;
  border-radius: 50%;
  background: rgba(255, 255, 255, .36);
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, .56);
}
#pg-diary .cal-day.has-diary::after {
  content: "";
  position: absolute;
  bottom: 1px;
  inset-inline: 0;
  margin-inline: auto;
  width: 3px;
  height: 3px;
  border-radius: 50%;
  background: var(--pearl-muted);
  pointer-events: none;
}
.diary-jump {
  display: flex;
  align-items: center;
  gap: 5px;
  color: var(--pearl-muted);
  font-size: 10px;
}
.diary-jump input, #diaryGen :is(input, textarea) {
  min-width: 0;
  max-width: 100%;
  padding: 6px 8px;
  border: 1px solid var(--pearl-edge);
  border-radius: 8px;
  background: var(--pearl-field-fill);
  color: var(--pearl-ink);
  font: 400 16px/1.4 var(--diary-copy);
}
#diaryJump {
  padding: 2px 4px;
  font-size: 13px;
  font-variant-numeric: lining-nums tabular-nums;
}
.diary-status:empty { display: none; }
.diary-status {
  margin-block: 8px;
  padding: 4px 8px;
  border-radius: 8px;
  background: rgba(var(--pearl-diary-rgb), .82);
  color: var(--pearl-muted);
  font-size: 12px;
}
.diary-card {
  --pearl-paper: rgba(var(--pearl-diary-rgb), .68);
  --pearl-filter: blur(15px) saturate(.82);
  margin-top: 12px;
  padding: 15px 16px;
  overflow-wrap: anywhere;
}
.diary-entry-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--diary-rule);
}
#diaryDateLabel {
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: baseline;
  column-gap: 7px;
  font-family: var(--pearl-display);
}
#diaryDayNumber { font-size: 29px; line-height: 37px; font-variant-numeric: lining-nums; }
#diaryEntryMonth { font-size: 14px; }
#diaryWeekday {
  grid-column: 1 / -1;
  color: var(--pearl-muted);
  font-size: 10.5px;
  line-height: 18.2px;
}
#diaryRecordedAt { color: var(--pearl-muted); font-size: 11px; line-height: 1.45; }
.diary-flower {
  flex: none;
  width: 41px;
  height: 46px;
  color: #6c7076;
  stroke: currentColor;
  stroke-width: 1;
  stroke-linecap: round;
  stroke-linejoin: round;
}
#diaryTitle { margin-top: 10px; font: 400 20px/1.4 var(--pearl-display); }
#diaryExcerpt, #diaryView { margin-top: 5px; font-size: 14px; line-height: 1.75; }
#diaryExcerpt {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
  color: var(--pearl-muted);
}
#diaryView { white-space: pre-wrap; }
.diary-entry-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding-top: 8px;
  margin-top: 9px;
  border-top: 1px solid var(--diary-rule);
}
#diaryEntryTags { color: var(--pearl-muted); font-size: 12px; }
#readDiaryBtn {
  flex: none;
  margin-inline-start: auto;
  padding-inline: 12px;
  font: 400 15px/1.2 var(--pearl-display);
}
.diary-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 9px; font-size: 12px; }
#editDiaryBtn, #readDiaryBtn { --pearl-button-fill: rgba(var(--pearl-diary-rgb), .54); }
#pg-diary :is(.pearl-button, #diaryJump),
#diaryGen :is(input, textarea) {
  -webkit-backdrop-filter: var(--pearl-field-filter);
  backdrop-filter: var(--pearl-field-filter);
}
#diaryGen { margin-top: 12px; font-size: 13px; }
#diaryGen label { display: grid; gap: 4px; margin-bottom: 8px; }
#diaryGen textarea { width: 100%; resize: vertical; }

#pg-chat :is(.chat-hdr, .chat-composer),
#convPanel {
  background: transparent;
  -webkit-backdrop-filter: var(--chat-chrome-filter, blur(10px) saturate(.85));
  backdrop-filter: var(--chat-chrome-filter, blur(10px) saturate(.85));
}
@media (prefers-reduced-transparency: reduce) {
  .pearl-surface { background: #faf8f3; -webkit-backdrop-filter: none; backdrop-filter: none; }
  #pg-diary .pearl-surface { background: #f3f5f9; }
  #pg-chat .chat-hdr,
  #pg-chat .chat-composer,
  #convPanel {
    background: transparent;
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
  }
  #pg-diary { --pearl-field-filter: none; }
  #pg-diary .diary-scroll { background: #e8eaed; }
}

#pg-chat .chat-hdr {
  position: relative;
  z-index: 3;
  display: grid;
  grid-template-columns: minmax(68px, 1fr) minmax(0, 2fr) minmax(68px, 1fr);
  gap: 4px;
  border-radius: 0 0 16px 16px;
}
#pg-chat .chat-hdr > :nth-child(3) { flex: none; }
#pg-chat .chat-hdr #chatMenu {
  justify-self: start;
  border: 1px solid var(--pearl-edge);
  color: inherit;
}
#pg-chat .chat-hdr :is(#chatMenu, #chatSearchToggle, #chatMoreToggle) {
  background: transparent;
}
.chat-identity, .chat-header-actions { display: flex; align-items: center; gap: 4px; min-width: 0; }
#pg-chat .chat-identity { justify-content: center; min-height: 32px; text-align: center; }
.chat-header-actions { justify-content: flex-end; }
.chat-identity-text { min-width: 0; }
#pg-chat .chat-name {
  font: 400 1.1rem/1.18 var(--pearl-display);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#pg-chat .chat-identity :is(.chat-signature, .chat-st) { color: #333438; }
#pg-chat .chat-identity #chatAv { display: none; }
.chat-icon-button { display: grid; place-items: center; width: 32px; padding: 4px; }
.chat-icon-button svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.6; }

/* Reuse the default chat wallpaper behind the drawer when no custom image is set. */
body {
  --chat-default-wallpaper: radial-gradient(ellipse at 18% 12%, #faf8f2 0, transparent 60%),
                            linear-gradient(155deg, #e8e6df, #d8d7d2);
}
#pg-chat:not(.chat-custom-wallpaper),
#pg-chat:not(.chat-custom-wallpaper) ~ #pg-diary {
  background: var(--chat-default-wallpaper);
}
body:has(#convPanel.open):has(#pg-chat.active:not(.chat-custom-wallpaper)) #wp {
  background-image: var(--chat-default-wallpaper);
}
/* Keep chat controls and messages out of the clear drawer's backdrop. */
#convPanel.open ~ #pg-chat.active {
  clip-path: inset(0 0 0 max(0px, calc(var(--conv-panel-width) - max(0px, (100vw - 480px) / 2))));
}
#convPanel {
  --chat-chrome-filter: blur(10px);
  box-shadow: inset -1px 0 var(--pearl-edge), 0 5px 16px var(--pearl-shadow);
}
#convPanel .conv-item:is(.active, :active) {
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .55);
}
#convPanel .conv-new { color: var(--pearl-ink); border-color: var(--pearl-edge); }
#convPanel .conv-del { color: #60504c; }
#convPanel, #pg-chat .chat-identity,
#pg-chat .chat-hdr :is(#chatMenu, #chatMoreToggle),
#pg-chat #chatForm :is(#ti, #sendChat) {
  text-shadow: 0 0 2px rgba(255, 255, 255, .95), 0 0 4px rgba(255, 255, 255, .85);
}
#pg-chat :is(#chatSearchToggle, .chat-composer-tool) svg {
  filter: drop-shadow(0 0 1px rgba(255, 255, 255, .95));
}
#pg-chat .chat-msgs { background: rgba(249, 247, 242, .08); }
#pg-chat .chat-message .chat-bubble { color: var(--chat-pearl-ink); }
#pg-chat .chat-message[data-material="liquid"] .chat-bubble,
#pg-chat .chat-message[data-material="frost"] .chat-bubble {
  background-color: rgba(252, 251, 248, var(--chat-pearl-alpha));
}
#pg-chat .chat-message[data-material="soft"] .chat-bubble {
  background-color: rgba(252, 251, 248, var(--chat-pearl-alpha));
}
#pg-chat .chat-tail { background: linear-gradient(var(--chat-color), var(--chat-color)) rgba(252, 251, 248, var(--chat-pearl-alpha)); }
#pg-chat .chat-message-avatar {
  background: rgba(252, 251, 248, .85);
  border-color: var(--pearl-edge);
}
#pg-chat :is(.chat-empty, .chat-nickname) {
  width: fit-content;
  color: var(--pearl-ink);
  background: rgba(250, 248, 243, .87);
  border-radius: 7px;
  padding: 1px 4px;
}
#pg-chat .chat-time { color: var(--pearl-muted); }
#pg-chat .chat-empty { flex: 0 0 auto; margin: 15px auto; padding: 5px 9px; }
#pg-chat .chat-search-match .chat-bubble { outline: 2px solid #77746b; outline-offset: 2px; }

.chat-search {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 4px;
  padding: 6px;
  margin: 4px 10px;
  z-index: 2;
}
.chat-search input {
  min-width: 0;
  width: 100%;
  border: 1px solid var(--pearl-edge);
  padding: 6px;
  border-radius: 8px;
  background: rgba(255, 255, 255, .56);
  color: inherit;
  font: 16px system-ui, sans-serif;
}
#chatSearchCount { grid-column: 1 / -1; color: var(--pearl-muted); font-size: 11px; }
.chat-popover { position: absolute; padding: 8px; z-index: 4; }
#chatMorePanel { inset: calc(100% + 4px) 8px auto auto; display: grid; gap: 5px; max-width: calc(100% - 16px); }
.chat-model-label { font-size: 11px; color: var(--pearl-muted); overflow-wrap: anywhere; }
.chat-text-file { margin-top: 3px; padding: 4px 7px; border: 1px solid var(--pearl-edge); border-radius: 8px; white-space: normal; }
.chat-file-name { cursor: pointer; overflow-wrap: anywhere; }
.chat-file-content { white-space: pre-wrap; overflow-wrap: anywhere; margin-top: 4px; }

#pg-chat #chatForm { position: relative; }
#pg-chat .chat-composer {
  position: relative;
  top: 7px;
  --chat-chrome-filter: blur(12px) saturate(.85);
  color: #212328;
  box-shadow: inset 0 1px rgba(255, 255, 255, .76), inset 0 0 0 1px var(--pearl-edge), 0 4px 13px var(--pearl-shadow);
}
.chat-keyboard #pg-chat .chat-composer { top: 5px; }
#pg-chat #chatForm .chat-composer-tool { width: 27px; }
#pg-chat #chatForm #ti { padding-left: 5px; padding-right: 5px; font-size: 16px; }
#pg-chat #chatForm #ti::placeholder {
  color: var(--pearl-muted);
  font-size: 14px;
  line-height: 1.6;
  opacity: 1;
  text-shadow: none;
}
#pg-chat #chatForm #sendChat { background: transparent; border-color: var(--pearl-edge); }
.chat-emoji-panel {
  display: flex;
  gap: 3px;
  bottom: calc(100% + 2px);
  left: max(14px, env(safe-area-inset-left));
}
#pg-chat .chat-emoji-panel button { width: 32px; height: 32px; border-color: var(--pearl-edge); }
@media (max-width: 350px) {
  #pg-chat .chat-hdr { padding-left: 8px; padding-right: 8px; }
  #pg-chat #chatForm .chat-composer { gap: 2px; }
  #pg-chat #chatForm .chat-composer-tool { width: 25px; }
}
@media (max-width: 350px) {
  body:has(:is(#pg-chat, #pg-diary).active) .nav .nav-btn { padding-inline: 12px; }
}
