:root {
  color-scheme: light;
  --paper: #faf8f3;
  --ink: #39332d;
  --button: #494039;
  --muted: #706960;
  --line: #d5cec3;
  --wash: #eeeae2;
  font: 16px/1.55 -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", sans-serif;
  color: var(--ink);
  background: var(--paper);
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
body, h1, h2, h3, p { margin: 0; }
main { max-width: 600px; margin: auto; }
input, button, textarea { font: inherit; color: inherit; }
button {
  min-height: 44px;
  padding: 8px 12px;
  border: 0;
  border-radius: 0;
  background: transparent;
  cursor: pointer;
}
button:disabled { color: var(--muted); cursor: default; }
:where(button, input, textarea):focus-visible, .roles input:focus-visible + span {
  outline: 2px solid var(--button);
  outline-offset: 2px;
}
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* Shared account and conversation mode selectors */
fieldset { margin: 0 0 24px; padding: 0; border: 0; }
.roles { display: flex; }
.roles > * { flex: 1; }
.roles label { position: relative; text-align: center; }
.roles input { position: absolute; opacity: 0; }
.roles span, .roles button {
  display: block;
  padding: 12px;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  cursor: pointer;
}
.roles input:checked + span, .roles button[aria-pressed="true"] {
  box-shadow: inset 0 -2px var(--ink);
  color: var(--ink);
  font-weight: 650;
}

#gate {
  display: grid;
  align-content: center;
  min-height: 100svh;
  max-width: 360px;
  margin: auto;
  padding: 32px 24px;
}
input[type="password"] {
  width: 100%;
  padding: 12px 0;
  border: 0;
  border-bottom: 1px solid #cdc6bc;
  border-radius: 0;
  background: transparent;
}
.remember {
  display: flex;
  align-items: center;
  gap: 7px;
  min-height: 40px;
  margin: 8px 0 12px;
  font-size: 13px;
  color: var(--muted);
}
.remember input { margin: 0; accent-color: var(--button); }
.primary {
  width: 100%;
  min-height: 49px;
  border-radius: 6px;
  background: var(--button);
  color: white;
}
h1, h2 { font-size: 19px; font-weight: 650; letter-spacing: -.035em; }
#reset h2 { margin-bottom: 26px; }
#reset label { display: block; color: var(--muted); font-size: 14px; }
#reset input { margin-bottom: 22px; }

#room { display: flex; flex-direction: column; height: 100dvh; }
header, #compose, #letters { padding-inline: clamp(20px, 5vw, 30px); }
header, .dialoghead, .account, .conversation {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
header {
  padding-top: max(8px, env(safe-area-inset-top));
  padding-bottom: 8px;
  border-bottom: 1px solid var(--line);
}
.conversation { justify-content: flex-start; gap: 12px; }
#mode, #allowance { font-size: 13px; color: var(--muted); }
#settings, #close, #logout { padding-inline: 8px; margin-right: -8px; font-size: 14px; }
#stream { flex: 1; min-height: 0; overflow: auto; overscroll-behavior: contain; }
#letters {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-height: 100%;
  padding-block: 16px 24px;
}
.date, .system, .empty { text-align: center; color: var(--muted); font-size: 13px; }
.date { margin: 6px 0 2px; font-size: 12px; }
.empty { margin: auto; }
.msg { align-self: flex-start; max-width: 83%; min-width: 0; }
.msg.mine { align-self: flex-end; }
.bubble {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 9px 13px;
}
.mine .bubble { background: var(--wash); border-color: transparent; }
.meta { margin-top: 4px; color: var(--muted); font-size: 12px; }
.mine .meta { text-align: right; }
#compose { padding-block: 10px max(16px, env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
#allowance:not(:empty) { margin: 4px 0 8px; }
.writer { display: flex; gap: 10px; align-items: flex-end; }

.writer textarea {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  max-height: 110px;
  padding: 9px 0;
  resize: none;
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  background: transparent;
}
.writer button { border-radius: 6px; background: var(--button); color: white; font-size: 14px; }
.writer button:disabled { background: var(--wash); color: var(--muted); }

dialog {
  width: min(360px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px 24px;
  background: var(--paper);
  color: var(--ink);
}
dialog::backdrop { background: rgb(57 51 45 / 26%); }
.dialoghead { margin-bottom: 20px; }
dialog h3 { font-size: 14px; font-weight: 600; }
dialog p { margin: 8px 0; font-size: 14px; color: var(--muted); overflow-wrap: anywhere; }
#notify { padding: 8px 0; font-size: 14px; text-decoration: underline; text-underline-offset: 4px; }
dialog hr { border: 0; border-top: 1px solid var(--line); margin: 20px 0 6px; }
.account { font-size: 14px; }
#notice {
  position: fixed;
  z-index: 3;
  bottom: max(18px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  width: max-content;
  max-width: 90vw;
  border-radius: 6px;
  background: var(--button);
  color: white;
  padding: 10px 16px;
  font-size: 14px;
}
dialog #notice { position: static; transform: none; width: auto; margin-top: 12px; }

:root {
  --regal-ink: #39293b;
  --regal-type: "Nanum Myeongjo", "AppleMyungjo", "Batang", "Noto Serif CJK KR", serif;
  --brass: #a88c60;
}
.royal-letter .bubble {
  --foil: #ddc29c;
  position: relative;
  background-color: var(--regal-ink);
  background-image: linear-gradient(115deg, #53384a, transparent 66%);
  color: #fff8f0;
  border: 1px solid #8c7066;
  border-radius: 8px 3px;
  padding: 12px 19px;
  font-family: var(--regal-type);
  font-size: 1.075rem;
  font-weight: 600;
  line-height: 1.65;
}
body[data-viewer="jae"] #partner {
  color: var(--regal-ink);
  font-family: var(--regal-type);
}
body[data-viewer="queen"] {
  --button: var(--regal-ink);
}
body[data-viewer="queen"] header {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: flex-start;
  padding-top: max(20px, env(safe-area-inset-top));
  padding-bottom: 14px;
  background: var(--regal-ink);
  color: #fbf6ed;
  border-bottom: 2px solid var(--brass);
}
#chamber {
  margin: 0 0 6px;
  font: 600 24px/1.4 var(--regal-type);
  letter-spacing: -.04em;
}
body[data-viewer="queen"] header .conversation { gap: 8px; }
body[data-viewer="queen"] #partner {
  color: #eee5d8;
  font-size: 14px;
  font-weight: 500;
}
body[data-viewer="queen"] #settings { color: #eee5d8; }
body[data-viewer="queen"] :is(#reset h2, .dialoghead h2) {
  color: var(--regal-ink);
  font: 600 23px/1.5 var(--regal-type);
}
body[data-viewer="queen"] :is(#reset h2, .dialoghead) {
  border-bottom: 1px solid var(--brass);
  padding-bottom: 12px;
}
body[data-viewer="queen"] .writer textarea { font-family: var(--regal-type); }
body[data-viewer="queen"] #send { white-space: nowrap; font-family: var(--regal-type); }
body[data-viewer="queen"] #room header button:focus-visible { outline-color: #eadabb; }

/* Fine foil tooling on opposite corners leaves the text area clear. */
.royal-letter .bubble::before,
.royal-letter .bubble::after {
  content: "";
  position: absolute;
  width: 28px;
  height: 13px;
  pointer-events: none;
  background:
    radial-gradient(ellipse 6px 5px at 0 0,
      transparent 76%, var(--foil) 84% 96%, transparent 100%) 0 0 / 7px 6px,
    linear-gradient(to right, var(--foil), transparent) 6px 0 / 22px 1px,
    linear-gradient(to bottom, var(--foil), transparent) 0 5px / 1px 8px;
  background-repeat: no-repeat;
}
.royal-letter .bubble::before { top: 4px; left: 4px; }
.royal-letter .bubble::after { right: 4px; bottom: 4px; transform: rotate(180deg); }
#modes {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  margin-top: 12px;
  padding: 3px;
  border: 1px solid #806d78;
  border-radius: 7px;
}
#modes button {
  border-radius: 4px;
  padding: 7px 5px;
  color: #f2e8de;
  font-size: 14px;
  word-break: keep-all;
}
#modes button[aria-pressed="true"] {
  background: #eee3d1;
  color: var(--regal-ink);
  font-weight: 650;
}

/* Keep the neutral reply field compact. The queen writes on a separate sheet. */
#recipient { display: none; }
#compose.locked .writer { display: none; }
.writer { flex-wrap: wrap; }
.writer #attach {
  flex: none;
  min-width: 44px;
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line);
}
.writer #selected {
  order: -1;
  width: 100%;
  max-height: 92px;
  overflow-y: auto;
  margin: 0;
  padding: 0;
  list-style: none;
}
#selected li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font: 13px/1.4 -apple-system, sans-serif;
}
#selected li span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#selected li button { color: var(--muted); background: transparent; flex: none; }
.attachment { margin-top: 6px; white-space: normal; }
.attachment:first-child { margin-top: 0; }
.attachment img, .attachment video {
  display: block;
  width: 100%;
  max-height: 240px;
  object-fit: contain;
  border-radius: 4px;
}
.attachment:has(img, video) { width: min(220px, 45vw); }
.attachment video { background: #221e22; min-width: min(180px, 46vw); }
.file-link {
  display: inline-block;
  max-width: 100%;
  color: inherit;
  font: 14px/1.5 -apple-system, sans-serif;
  overflow-wrap: anywhere;
  text-underline-offset: 3px;
}
body[data-viewer="queen"] #compose {
  padding-block: 11px max(12px, env(safe-area-inset-bottom));
  border-top: none;
}
body[data-viewer="queen"] .writer {
  position: relative;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0 12px;
  padding: 12px 15px 8px;
  border: 1px solid #b69c78;
  border-radius: 3px 10px 3px 10px;
  box-shadow: inset 0 0 0 3px #fbf6ec, inset 0 0 0 4px #dbccb6;
  background: linear-gradient(115deg,#fffaf0,#f4ead9);
}
body[data-viewer="queen"] #recipient {
  display: block;
  grid-column: 1 / -1;
  color: #797673;
  font: 400 12px/1.4 -apple-system, BlinkMacSystemFont, sans-serif;
}
body[data-viewer="queen"] #draft {
  grid-column: 1 / -1;
  width: 100%;
  padding: 7px 0;
  margin: 2px 0 4px;
  background: transparent;
  border-color: #d7c6aa;
  font: 17px/1.55 var(--regal-type);
}
body[data-viewer="queen"] .writer:focus-within { border-color: #977752; }
body[data-viewer="queen"] #draft:focus-visible {
  outline: none;
  border-bottom: 2px solid #73546a;
}
body[data-viewer="queen"] #selected { grid-column: 1 / -1; order: 0; }
body[data-viewer="queen"] #attach {
  justify-self: start;
  border: 0;
  padding-inline: 2px;
  color: #685649;
}
body[data-viewer="queen"] #send {
  border: 1px solid #8f755a;
  border-radius: 3px 7px;
  padding-inline: 19px;
  background: var(--regal-ink);
  color: #fff7e9;
}
body[data-viewer="queen"] #send:disabled { background: #e5daca; color: #736558; border-color: #c9b89f; }
@media (max-height: 520px) {
  body[data-viewer="queen"] #compose { padding-block: 4px; }
  body[data-viewer="queen"] .writer { padding-block: 5px; }
  body[data-viewer="queen"] #recipient { display: none; }
  .writer #selected { max-height: 46px; }
}

.letter-foot { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.mine .letter-foot { justify-content: flex-end; }
.reaction-chip { font-size: 14px; line-height: 1.25; }
button.reaction-chip {
  padding: 3px 5px;
  min-height: 28px;
  font-size: 12px;
  color: var(--muted);
}
.reaction-chip.has-emoji { font-size: 16px; }
#room.replies-closed button.reaction-chip:not(.has-emoji) { visibility: hidden; }
#emoji-options { display: grid; grid-template-columns: repeat(3,1fr); gap: 5px; }
#emoji-options button { font-size: 24px; border-radius: 6px; }
#emoji-options button[aria-pressed="true"] { background: var(--wash); outline: 1px solid var(--line); }
#remove-reaction { margin-top: 10px; font-size: 14px; }
dialog #notice { text-align: center; }
