:root,
html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #14110e;
  --bg-2: #1c1814;
  --panel: #221c16;
  --ink: #f2e8d8;
  --muted: #9a8c78;
  --line: #3a3026;
  --accent: #d9772e;
  --accent-2: #e8b86a;
  --ok: #6f9f6a;
  --err: #c45c4a;
  --user: #2a241c;
  --user-line: #4a3c2c;
  --bot: #1a2218;
  --bot-line: #2c3a2a;
  --err-bg: #2a1814;
  --err-ink: #f0c8c0;
  --wash-a: #3a2818;
  --wash-b: #1a2418;
  --send-ink: #1a1008;
  --grain: 0.07;
  --serif: "Instrument Serif", Georgia, serif;
  --sans: "IBM Plex Sans", sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
}

html[data-theme="light"] {
  color-scheme: light;
  --bg: #f3e9dc;
  --bg-2: #efe3d2;
  --panel: #faf4eb;
  --ink: #2c1c12;
  --muted: #8a6f56;
  --line: #dccbb4;
  --accent: #c45c18;
  --accent-2: #a34a12;
  --ok: #3d7a48;
  --err: #b44132;
  --user: #f0dfc8;
  --user-line: #e0c8a8;
  --bot: #e7eee3;
  --bot-line: #c9d6c2;
  --err-bg: #f8e4de;
  --err-ink: #8a2e22;
  --wash-a: #efd2ae;
  --wash-b: #d5e2c8;
  --send-ink: #1a1008;
  --grain: 0.1;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
}

body {
  background:
    radial-gradient(ellipse 80% 50% at 10% -10%, var(--wash-a) 0%, transparent 55%),
    radial-gradient(ellipse 60% 40% at 100% 100%, var(--wash-b) 0%, transparent 50%),
    var(--bg);
}

.grain {
  pointer-events: none;
  position: fixed;
  inset: 0;
  opacity: var(--grain);
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  z-index: 2;
}

.shell {
  display: grid;
  grid-template-columns: 260px 1fr;
  grid-template-rows: auto minmax(0, 1fr);
  grid-template-areas:
    "top top"
    "rail stage";
  height: 100dvh;
  max-width: 1200px;
  margin: 0 auto;
  padding: 1.25rem 1.5rem 1.25rem;
  gap: 1rem 1.25rem;
  position: relative;
  z-index: 1;
}

.top {
  grid-area: top;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid var(--line);
  animation: rise 0.6s ease both;
}

.top-tools {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  flex-shrink: 0;
}

.theme-btn {
  appearance: none;
  width: 2.15rem;
  height: 2.15rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  cursor: pointer;
  display: grid;
  place-items: center;
  padding: 0;
  transition: border-color 0.15s, color 0.15s;
}

.theme-btn:hover,
.theme-btn:focus-visible {
  border-color: var(--accent);
  color: var(--accent);
  outline: none;
}

.theme-btn svg { display: none; }
.theme-btn .icon-sun { display: block; }
html[data-theme="light"] .theme-btn .icon-sun { display: none; }
html[data-theme="light"] .theme-btn .icon-moon { display: block; }
.rail-toggle svg { display: block; }

.brand {
  display: flex;
  gap: 0.9rem;
  align-items: center;
}

.mark-wrap {
  width: 2.55rem;
  height: 2.55rem;
  border-radius: 50%;
  overflow: hidden;
  flex-shrink: 0;
  display: grid;
  place-items: center;
}

.mark {
  width: 168%;
  height: 168%;
  object-fit: cover;
  mix-blend-mode: screen;
}

html[data-theme="light"] .mark-wrap {
  background: #1c140e;
  box-shadow: 0 0 0 1px #c9b49a;
}

h1 {
  margin: 0;
  font-family: var(--serif);
  font-weight: 400;
  font-size: 1.85rem;
  letter-spacing: -0.02em;
  line-height: 1.1;
}

.sub {
  margin: 0.15rem 0 0;
  color: var(--muted);
  font-size: 0.78rem;
  font-family: var(--mono);
}

.status {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  font-family: var(--mono);
  font-size: 0.75rem;
  color: var(--muted);
  padding: 0.4rem 0.7rem;
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 999px;
}

.dot {
  width: 0.5rem;
  height: 0.5rem;
  border-radius: 50%;
  background: var(--muted);
}

.status.ok .dot { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.status.err .dot { background: var(--err); }

.rail-toggle { display: none; }

.rail-backdrop {
  display: none;
}

.rail {
  grid-area: rail;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding: 1rem;
  min-height: 0;
  overflow-y: auto;
  background: color-mix(in srgb, var(--panel) 88%, transparent);
  border: 1px solid var(--line);
  border-radius: 0.75rem;
  animation: rise 0.7s 0.08s ease both;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  font-size: 0.75rem;
  color: var(--muted);
  font-family: var(--mono);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.field em {
  font-style: normal;
  color: var(--accent-2);
  float: right;
}

select, input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

select {
  background: var(--bg);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 0.4rem;
  padding: 0.55rem 0.6rem;
  font-family: var(--mono);
  font-size: 0.8rem;
  text-transform: none;
  letter-spacing: 0;
}

.rail-grow { flex: 1 1 auto; min-height: 0.5rem; }

.ghost {
  appearance: none;
  background: transparent;
  color: var(--muted);
  border: 1px dashed var(--line);
  border-radius: 0.45rem;
  padding: 0.65rem;
  font-family: var(--mono);
  font-size: 0.75rem;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}

.ghost:hover {
  color: var(--ink);
  border-color: var(--accent);
}

html[data-theme="light"] .ghost {
  border-color: #c4ae94;
  background: color-mix(in srgb, var(--bg) 70%, white);
}

.rail-foot {
  margin-top: 0.5rem;
  padding-top: 0.85rem;
  border-top: 1px solid var(--line);
}

.rail-foot code {
  display: block;
  font-family: var(--mono);
  font-size: 0.72rem;
  color: var(--accent-2);
  margin-bottom: 0.35rem;
}

.rail-foot p {
  margin: 0;
  font-size: 0.75rem;
  color: var(--muted);
  line-height: 1.4;
}

.stage {
  grid-area: stage;
  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100%;
  border: 1px solid var(--line);
  border-radius: 0.85rem;
  background: color-mix(in srgb, var(--bg-2) 92%, transparent);
  overflow: hidden;
  animation: rise 0.75s 0.12s ease both;
}

.thread {
  flex: 1;
  overflow-y: auto;
  padding: 1.4rem 1.35rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  scroll-behavior: smooth;
}

.empty {
  margin: auto;
  text-align: center;
  max-width: 22rem;
  color: var(--muted);
  animation: rise 0.8s ease both;
}

.empty h2 {
  font-family: var(--serif);
  font-weight: 400;
  font-size: 2rem;
  color: var(--ink);
  margin: 0 0 0.5rem;
  font-style: italic;
}

.empty p {
  margin: 0;
  font-size: 0.9rem;
  line-height: 1.5;
}

.bubble {
  max-width: min(42rem, 92%);
  padding: 0.85rem 1rem;
  border-radius: 0.65rem;
  line-height: 1.55;
  font-size: 0.95rem;
  word-break: break-word;
  animation: rise 0.35s ease both;
}

.bubble .body:not(.md) {
  white-space: pre-wrap;
}

.bubble-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.4rem;
}

.bubble-head .meta,
.bubble-head time {
  font-family: var(--mono);
  font-size: 0.68rem;
  color: var(--muted);
  letter-spacing: 0.04em;
}

.bubble-head .meta {
  text-transform: uppercase;
}

.bubble-head time {
  margin-left: auto;
  flex-shrink: 0;
  font-variant-numeric: tabular-nums;
}

.day-rule {
  align-self: stretch;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.68rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.day-rule::before,
.day-rule::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--line);
}

.bubble.user {
  align-self: flex-end;
  background: var(--user);
  border: 1px solid var(--user-line);
  border-bottom-right-radius: 0.2rem;
}

.bubble.assistant {
  align-self: flex-start;
  background: var(--bot);
  border: 1px solid var(--bot-line);
  border-bottom-left-radius: 0.2rem;
}

.bubble.assistant .meta {
  display: inline;
  font-family: var(--mono);
  font-size: 0.68rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.bubble.error {
  align-self: stretch;
  background: var(--err-bg);
  border-color: var(--err);
  color: var(--err-ink);
}

.composer {
  display: flex;
  gap: 0.65rem;
  align-items: flex-end;
  flex-shrink: 0;
  position: sticky;
  bottom: 0;
  z-index: 4;
  padding: 0.9rem 1rem 1rem;
  border-top: 1px solid var(--line);
  background: var(--panel);
}

textarea {
  flex: 1;
  resize: none;
  min-height: 2.8rem;
  max-height: 9rem;
  padding: 0.75rem 0.9rem;
  border-radius: 0.55rem;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 0.95rem;
  line-height: 1.4;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(217, 119, 46, 0.18);
}

#send {
  appearance: none;
  flex: 0 0 auto;
  width: 2.8rem;
  height: 2.8rem;
  border: none;
  border-radius: 0.55rem;
  background: linear-gradient(160deg, var(--accent-2), var(--accent));
  color: var(--send-ink);
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: transform 0.12s, filter 0.12s;
}

#send:hover { transform: translateY(-1px); filter: brightness(1.05); }
#send:disabled { opacity: 0.45; cursor: wait; transform: none; }

.cursor {
  display: inline-block;
  width: 0.45rem;
  height: 1em;
  margin-left: 2px;
  background: var(--accent-2);
  vertical-align: text-bottom;
  animation: blink 1s steps(1) infinite;
}

.bubble .md {
  white-space: normal;
}

.bubble .md > *:first-child { margin-top: 0; }
.bubble .md > *:last-child { margin-bottom: 0; }

.bubble .md p {
  margin: 0 0 0.7em;
}

.bubble .md h1,
.bubble .md h2,
.bubble .md h3,
.bubble .md h4 {
  font-family: var(--serif);
  font-weight: 400;
  letter-spacing: -0.02em;
  line-height: 1.25;
  margin: 0.95em 0 0.4em;
  color: var(--ink);
}

.bubble .md h1 { font-size: 1.45rem; }
.bubble .md h2 { font-size: 1.22rem; }
.bubble .md h3 { font-size: 1.05rem; font-style: italic; }

.bubble .md ul,
.bubble .md ol {
  margin: 0 0 0.75em;
  padding-left: 1.2em;
}

.bubble .md li { margin: 0.2em 0; }

.bubble .md a {
  color: var(--accent);
  text-underline-offset: 0.15em;
}

.bubble .md strong { color: var(--ink); }

.bubble .md blockquote {
  margin: 0.6em 0;
  padding: 0.15em 0 0.15em 0.85em;
  border-left: 2px solid var(--accent);
  color: var(--muted);
}

.bubble .md code {
  font-family: var(--mono);
  font-size: 0.84em;
  background: color-mix(in srgb, var(--bg) 70%, transparent);
  border: 1px solid var(--line);
  border-radius: 0.25rem;
  padding: 0.08em 0.32em;
}

.bubble .md pre {
  margin: 0.7em 0;
  padding: 0.75rem 0.85rem;
  overflow-x: auto;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 0.45rem;
}

.bubble .md pre code {
  background: none;
  border: 0;
  padding: 0;
  font-size: 0.8rem;
}

.bubble .md hr {
  border: 0;
  border-top: 1px solid var(--line);
  margin: 1em 0;
}

.bubble .md table {
  border-collapse: collapse;
  width: 100%;
  margin: 0.7em 0;
  font-size: 0.88rem;
}

.bubble .md th,
.bubble .md td {
  border: 1px solid var(--line);
  padding: 0.35em 0.55em;
  text-align: left;
}

@keyframes blink {
  50% { opacity: 0; }
}

@keyframes rise {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}

@media (max-width: 820px) {
  .shell {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr);
    grid-template-areas:
      "top"
      "stage";
    padding: 0.75rem;
    height: 100dvh;
  }
  h1 { font-size: 1.5rem; }
  .sub { display: none; }
  .rail-toggle { display: grid; }

  .rail-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 60;
    background: color-mix(in srgb, var(--bg) 55%, transparent);
    backdrop-filter: blur(6px);
  }

  .rail-backdrop[hidden] { display: none; }

  .rail {
    position: fixed;
    z-index: 70;
    top: 0.75rem;
    left: 0.75rem;
    bottom: 0.75rem;
    width: min(19rem, calc(100vw - 1.5rem));
    border-radius: 0.85rem;
    animation: none;
    transform: translateX(calc(-100% - 1.5rem));
    transition: transform 0.28s ease;
    box-shadow: 0 18px 50px rgba(20, 12, 6, 0.28);
    pointer-events: none;
  }

  .rail.open {
    transform: translateX(0);
    pointer-events: auto;
  }

  body.rail-open { overflow: hidden; }
}
