/* In Dialogue with Nature — editorial chat app */

:root {
  --paper: #f3ecdd;
  --paper-2: #eae0c9;
  --paper-3: #fffdf6;
  --line: #d8cbae;
  --ink: #241a10;
  --ink-soft: #5f4e39;
  --ox: #8c2b1a;
  --ox-deep: #641c0f;
  --brass: #8f6f1e;
  --ok: #6f9a55;
  --warn: #c98a2e;
  --session: #39766f;
  --session-deep: #285e58;
  --session-soft: #e2efe9;
  --session-line: #91b8ae;
  --serif: "Newsreader", "Iowan Old Style", Georgia, serif;
  --display: "Fraunces", Georgia, serif;
  --mono: "IBM Plex Mono", "Consolas", monospace;
  --topbar-h: 60px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
}

body {
  background:
    repeating-linear-gradient(0deg, transparent 0 31px, rgba(95, 78, 57, 0.045) 31px 32px),
    radial-gradient(120% 90% at 20% 0%, #f7f1e4 0%, var(--paper) 55%, #ecdfc6 100%);
  color: var(--ink);
  font-family: var(--serif);
  font-size: 16.5px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.grain {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 5;
  opacity: 0.22;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='140' height='140'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2'/%3E%3CfeColorMatrix values='0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.06 0'/%3E%3C/filter%3E%3Crect width='140' height='140' filter='url(%23n)'/%3E%3C/svg%3E");
}

.mono { font-family: var(--mono); }

code {
  font-family: var(--mono);
  font-size: 0.86em;
  background: var(--paper-2);
  border: 1px solid var(--line);
  padding: 0.1em 0.4em;
  border-radius: 4px;
}

/* ---------- app shell ---------- */

.app {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 780px;
  margin: 0 auto;
  background: color-mix(in srgb, var(--paper-3) 55%, transparent);
  border-left: 1px solid var(--line);
  border-right: 1px solid var(--line);
}

/* ---------- topbar ---------- */

.topbar {
  flex: 0 0 auto;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 14px;
  padding-top: env(safe-area-inset-top);
  background: var(--ink);
  color: var(--paper);
}

.brand { display: flex; align-items: center; gap: 11px; min-width: 0; }

.brand-mark {
  width: 34px;
  height: 34px;
  flex: 0 0 auto;
  border-radius: 9px;
  background: var(--ox);
  color: var(--paper-3);
  font-family: var(--display);
  font-weight: 800;
  font-size: 15px;
  display: grid;
  place-items: center;
  box-shadow: inset 0 0 0 1px rgba(243, 236, 221, 0.15);
}

.brand-text { min-width: 0; }

.brand-title {
  font-family: var(--display);
  font-weight: 600;
  font-size: 16px;
  line-height: 1.1;
  letter-spacing: 0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.brand-sub {
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 0.04em;
  color: #c9b998;
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 2px;
}

.status-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: #7a6b50;
  flex: 0 0 auto;
}
.status-dot.live {
  background: var(--ok);
  box-shadow: 0 0 8px 0 color-mix(in srgb, var(--ok) 75%, transparent);
  animation: pulse 2.6s infinite;
}
.status-dot.preview { background: var(--warn); }
.status-dot.session {
  background: var(--session);
  box-shadow: 0 0 8px 0 color-mix(in srgb, var(--session) 75%, transparent);
  animation: pulse 2.6s infinite;
}
@keyframes pulse { 50% { opacity: 0.55; } }

.topbar-actions { display: flex; align-items: center; gap: 4px; }

.timer {
  font-size: 13px;
  color: var(--warn);
  margin-right: 4px;
  min-width: 46px;
  text-align: right;
}

.end-session-btn {
  flex: 0 0 auto;
  font-family: var(--mono);
  font-size: 11.5px;
  font-weight: 600;
  letter-spacing: 0.03em;
  border: 1px solid color-mix(in srgb, var(--paper) 40%, transparent);
  background: var(--ox);
  color: var(--paper-3);
  border-radius: 999px;
  padding: 7px 12px;
  cursor: pointer;
  white-space: nowrap;
}
.end-session-btn:hover { background: var(--ox-deep); }
.end-session-btn[hidden] { display: none; }

.icon-btn {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border: none;
  background: transparent;
  color: var(--paper);
  border-radius: 9px;
  cursor: pointer;
  opacity: 0.82;
  transition: background 0.14s ease, opacity 0.14s ease;
}
.icon-btn:hover { background: rgba(243, 236, 221, 0.12); opacity: 1; }

/* ---------- chat ---------- */

.chat {
  flex: 1 1 auto;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 20px 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 11px;
  scroll-behavior: smooth;
}

.msg {
  max-width: 82%;
  padding: 11px 15px;
  border-radius: 15px;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  font-size: 16px;
  line-height: 1.5;
  animation: pop 0.22s ease;
}
.msg-rich {
  white-space: normal;
}
.msg-rich p { margin: 0 0 0.55em; }
.msg-rich p:last-child { margin-bottom: 0; }
.msg-rich .msg-h {
  font-family: var(--display);
  font-weight: 640;
  font-size: 1.05em;
  margin: 0.7em 0 0.35em;
}
.msg-rich ul, .msg-rich ol {
  margin: 0.2em 0 0.55em;
  padding-left: 1.25em;
}
.msg-rich li { margin: 0.2em 0; }
.msg-rich .msg-gap { height: 0.45em; }
.msg-rich strong { font-weight: 650; }
.cite-chip {
  display: inline-block;
  margin: 0.35em 0.25em 0 0;
  font-family: var(--mono);
  font-size: 11.5px;
  border: 1px solid currentColor;
  background: transparent;
  color: inherit;
  border-radius: 999px;
  padding: 4px 9px;
  cursor: pointer;
}
.cite-chip:hover { background: color-mix(in srgb, currentColor 12%, transparent); }
@keyframes pop { from { opacity: 0; transform: translateY(6px); } }

.msg-bot {
  align-self: flex-start;
  background: var(--paper-3);
  border: 1px solid var(--line);
  border-bottom-left-radius: 4px;
  box-shadow: 0 1px 0 rgba(36, 26, 16, 0.04);
}

.msg-user {
  align-self: flex-end;
  background: var(--ox);
  color: #f8f1e2;
  border-bottom-right-radius: 4px;
}

.msg-bot.msg-session {
  background: var(--session-soft);
  border-color: var(--session-line);
  border-left: 3px solid var(--session);
}

.msg-user.msg-session {
  background: var(--session-deep);
  color: #ffffff;
}

.msg-system {
  align-self: center;
  font-family: var(--mono);
  font-size: 11.5px;
  color: var(--ink-soft);
  background: var(--paper-2);
  border: 1px dashed var(--line);
  border-radius: 999px;
  padding: 5px 15px;
  max-width: 92%;
  text-align: center;
}

.msg-system.msg-session {
  color: var(--session-deep);
  background: var(--session-soft);
  border-color: var(--session-line);
}

.msg-divider {
  align-self: stretch;
  text-align: center;
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--ox);
  margin: 10px 0 3px;
  display: flex;
  align-items: center;
  gap: 12px;
}
.msg-divider::before, .msg-divider::after {
  content: "";
  flex: 1;
  border-top: 1px solid color-mix(in srgb, var(--ox) 38%, transparent);
}

.typing { display: inline-flex; gap: 5px; padding: 4px 2px; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--ink-soft); animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: 0.2s; }
.typing i:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 1; } }

/* first-message intro flourish */
.intro-drop::first-letter {
  font-family: var(--display);
  font-size: 2.6em;
  line-height: 0.8;
  float: left;
  padding: 0.05em 0.1em 0 0;
  color: var(--ox);
  font-weight: 640;
}

/* ---------- chips ---------- */

.chips {
  flex: 0 0 auto;
  display: flex;
  gap: 8px;
  padding: 8px 14px 4px;
  overflow-x: auto;
  scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }

.chip {
  flex: 0 0 auto;
  font-family: var(--mono);
  font-size: 12px;
  border: 1px solid var(--ink);
  background: var(--paper-3);
  color: var(--ink);
  border-radius: 999px;
  padding: 7px 13px;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.12s ease, color 0.12s ease;
}
.chip:hover { background: var(--ink); color: var(--paper); }
.chip.chip-end {
  background: var(--ox);
  color: var(--paper-3);
  border-color: var(--ox);
}
.chip.chip-end:hover { background: var(--ox-deep); border-color: var(--ox-deep); }

.session-end-hint {
  flex: 0 0 auto;
  margin: 0;
  padding: 2px 16px 8px;
  font-size: 13.5px;
  color: var(--ox-deep);
}
.session-end-hint[hidden] { display: none; }

/* ---------- export bar ---------- */

.export-bar {
  align-self: center;
  max-width: 96%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 9px;
  padding: 13px 17px;
  margin: 3px 0;
  background: var(--paper-3);
  border: 1px solid color-mix(in srgb, var(--ox) 45%, var(--line));
  border-radius: 14px;
  box-shadow: 0 2px 0 color-mix(in srgb, var(--ox) 16%, transparent);
}
.export-cap {
  font-family: var(--mono);
  font-size: 11.5px;
  color: var(--ink-soft);
  text-align: center;
}
.export-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}
.export-btn {
  font-family: var(--mono);
  font-size: 12.5px;
  font-weight: 600;
  letter-spacing: 0.04em;
  border: 1px solid var(--ox);
  background: var(--ox);
  color: var(--paper-3);
  border-radius: 999px;
  padding: 8px 16px;
  cursor: pointer;
  transition: background 0.12s ease, transform 0.08s ease;
}
.export-btn:hover { background: var(--ox-deep); }
.export-btn:active { transform: translateY(1px); }
.export-btn:disabled { opacity: 0.55; cursor: default; }

/* ---------- disclaimer ---------- */

.disclaimer {
  align-self: stretch;
  margin: 2px 0 9px;
  padding: 10px 14px;
  font-family: var(--mono);
  font-size: 11px;
  line-height: 1.55;
  color: var(--ink-soft);
  background: color-mix(in srgb, var(--ox) 7%, var(--paper-3));
  border: 1px solid color-mix(in srgb, var(--ox) 26%, transparent);
  border-left: 3px solid var(--ox);
  border-radius: 8px;
}

/* ---------- composer ---------- */

.composer {
  flex: 0 0 auto;
  display: flex;
  gap: 9px;
  padding: 10px 14px calc(14px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--paper-3) 70%, transparent);
  border-top: 1px solid var(--line);
}

.composer textarea,
.composer input {
  flex: 1;
  font-family: var(--serif);
  font-size: 16px;
  padding: 12px 15px;
  border: 1.5px solid var(--ink);
  border-radius: 12px;
  background: var(--paper-3);
  color: var(--ink);
  resize: none;
  min-height: 46px;
  max-height: 196px;
  line-height: 1.45;
  overflow-y: auto;
}
.composer textarea:focus,
.composer input:focus { outline: 2px solid var(--ox); outline-offset: 1px; }
.composer textarea:disabled,
.composer input:disabled { opacity: 0.55; }

.composer button {
  width: 48px;
  flex: 0 0 auto;
  border: 1.5px solid var(--ink);
  border-radius: 12px;
  background: var(--ox);
  color: var(--paper-3);
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background 0.12s ease, transform 0.1s ease;
}
.composer button:hover:not(:disabled) { background: var(--ox-deep); }
.composer button:active:not(:disabled) { transform: scale(0.94); }
.composer button:disabled { opacity: 0.45; cursor: not-allowed; }

/* ---------- sheets (modals) ---------- */

.sheet { position: fixed; inset: 0; z-index: 20; display: flex; align-items: flex-end; justify-content: center; }
.sheet[hidden] { display: none; }

.sheet-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(36, 26, 16, 0.5);
  backdrop-filter: blur(2px);
  animation: fade 0.2s ease;
}
@keyframes fade { from { opacity: 0; } }

.sheet-card {
  position: relative;
  width: 100%;
  max-width: 620px;
  max-height: 88dvh;
  overflow-y: auto;
  background: var(--paper);
  border: 1.5px solid var(--ink);
  border-bottom: none;
  border-radius: 20px 20px 0 0;
  box-shadow: 0 -12px 40px rgba(36, 26, 16, 0.28);
  animation: rise 0.26s cubic-bezier(0.2, 0.7, 0.2, 1);
}
@keyframes rise { from { transform: translateY(24px); opacity: 0; } }

.sheet-head {
  position: sticky;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px 12px;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
}
.sheet-head h2 {
  font-family: var(--display);
  font-weight: 600;
  font-size: 21px;
  margin: 0;
}
.sheet-head .icon-btn { color: var(--ink); }
.sheet-head .icon-btn:hover { background: var(--paper-2); }

.sheet-body { padding: 16px 20px 26px; }
.sheet-body p { margin: 0 0 12px; color: var(--ink-soft); }
.sheet-body .lede { font-size: 17px; color: var(--ink); }
.sheet-body .lede em { font-style: italic; color: var(--ox); }
.sheet-body h3 {
  font-family: var(--display);
  font-weight: 640;
  font-size: 16px;
  margin: 20px 0 8px;
  color: var(--ink);
}
.sheet-body strong { color: var(--ink); }

.cmd-list { display: flex; flex-direction: column; gap: 6px; margin: 10px 0 4px; }
.cmd-list code { font-size: 13px; padding: 8px 12px; border-radius: 8px; }

.rounds { list-style: none; margin: 6px 0; padding: 0; }
.rounds li { display: flex; gap: 12px; margin-bottom: 12px; align-items: flex-start; }
.rounds .r-no {
  flex: 0 0 auto;
  width: 26px; height: 26px;
  border-radius: 50%;
  background: var(--ink);
  color: var(--paper);
  font-family: var(--display);
  font-weight: 700;
  font-size: 14px;
  display: grid;
  place-items: center;
}
.rounds li:last-child .r-no { background: var(--ox); }
.rounds div { font-size: 15px; color: var(--ink-soft); }
.rounds b { color: var(--ink); }

.fine { font-size: 13.5px; color: var(--ink-soft); }
.same-system {
  margin-top: 18px;
  padding: 12px 14px;
  background: var(--paper-3);
  border: 1px solid var(--line);
  border-left: 3px solid var(--brass);
  border-radius: 8px;
  font-size: 14px;
}

.sheet-card-small { max-width: 460px; }
.menu-item {
  display: block;
  width: 100%;
  text-align: left;
  font-family: var(--serif);
  font-size: 16px;
  padding: 13px 15px;
  margin-bottom: 8px;
  background: var(--paper-3);
  border: 1.5px solid var(--ink);
  border-radius: 11px;
  color: var(--ink);
  cursor: pointer;
  transition: background 0.12s ease, color 0.12s ease;
}
.menu-item:hover { background: var(--ink); color: var(--paper); }

/* ---------- in-page reader ---------- */

.reader-panel {
  position: fixed;
  z-index: 16;
  background: var(--paper);
  border-left: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  box-shadow: -12px 0 32px rgba(36, 26, 16, 0.12);
}
.reader-panel[hidden] { display: none; }
.reader-toolbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  background: var(--ink);
  color: var(--paper);
  font-family: var(--mono);
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.reader-toolbar-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}
.reader-toolbar .icon-btn { color: var(--paper); }
.reader-toolbar .text-btn {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.04em;
  text-transform: none;
  border: 1px solid rgba(243, 236, 221, 0.35);
  background: transparent;
  color: var(--paper);
  border-radius: 999px;
  padding: 5px 10px;
  cursor: pointer;
}
.reader-toolbar .text-btn:hover {
  background: rgba(243, 236, 221, 0.12);
}
.split-handle {
  display: none;
}
.reader-scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
  padding: 10px 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.reader-docs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.reader-docs button {
  font-family: var(--mono);
  font-size: 11.5px;
  border: 1px solid var(--ink);
  background: var(--paper-3);
  color: var(--ink);
  border-radius: 999px;
  padding: 5px 10px;
  cursor: pointer;
}
.reader-docs button.is-on {
  background: var(--ink);
  color: var(--paper);
}
.reader-status {
  margin: 0;
  font-size: 12.5px;
  line-height: 1.4;
  color: var(--ink-soft);
}
.reader-open-tab {
  font-family: var(--mono);
  font-size: 11.5px;
  color: var(--ox);
  text-decoration: none;
}
.reader-open-tab:hover { text-decoration: underline; }
.reader-frame {
  flex: 1 1 auto;
  width: 100%;
  min-height: 240px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}
.reader-empty {
  color: var(--ink-soft);
  font-size: 14.5px;
  margin: 8px 0 0;
}

@media (max-width: 1099px) {
  .reader-panel {
    inset: auto 0 0 0;
    height: min(68dvh, 640px);
    border-left: none;
    border-top: 1px solid var(--line);
    border-radius: 18px 18px 0 0;
  }
}
@media (min-width: 1100px) {
  body.reader-open {
    display: flex;
    width: 100%;
    max-width: none;
    justify-content: flex-start;
    align-items: stretch;
    flex-direction: row;
  }
  body.reader-open > .grain {
    position: fixed;
    flex: 0 0 0;
    width: 0;
    height: 0;
    overflow: visible;
  }
  body.reader-open.chat-right {
    flex-direction: row-reverse;
  }
  body.reader-open .split-handle {
    display: block;
    flex: 0 0 8px;
    width: 8px;
    align-self: stretch;
    cursor: col-resize;
    background: var(--line);
    z-index: 18;
    position: relative;
    touch-action: none;
  }
  body.reader-open .split-handle::after {
    content: "";
    position: absolute;
    inset: 0 -5px;
  }
  body.reader-open .split-handle:hover,
  body.is-resizing .split-handle {
    background: var(--ox);
  }
  body.reader-open .app {
    margin: 0;
    flex: 0 0 var(--chat-width, 720px);
    width: var(--chat-width, 720px);
    max-width: none;
    min-width: 420px;
    border-right: none;
  }
  body.reader-open .reader-panel {
    position: relative;
    inset: auto;
    flex: 1 1 0;
    width: auto;
    min-width: 380px;
    max-width: none;
    height: 100dvh;
    box-shadow: none;
  }
  body.reader-open.chat-right .reader-panel {
    border-left: none;
    border-right: 1px solid var(--line);
  }
  body.is-resizing {
    cursor: col-resize;
    user-select: none;
  }
  body.is-resizing .reader-frame {
    pointer-events: none;
  }
}

/* ---------- responsive ---------- */

@media (min-width: 781px) {
  .app { box-shadow: 0 0 60px rgba(36, 26, 16, 0.1); }
}
@media (prefers-reduced-motion: reduce) {
  .msg, .sheet-card, .sheet-backdrop { animation: none; }
  .status-dot.live, .status-dot.session { animation: none; }
  .chat { scroll-behavior: auto; }
}
