:root {
  --bg: #f6f3ee;
  --panel: #fffaf2;
  --ink: #25211c;
  --muted: #746b5e;
  --line: #ded2bf;
  --accent: #7d2f24;
  --accent-dark: #5b2119;
  --blue: #243b62;
  --shadow: 0 16px 38px rgba(37, 33, 28, 0.12);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  line-height: 1.5;
}

a {
  color: inherit;
  text-decoration: none;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 60px;
  padding: 0 28px;
  border-bottom: 1px solid var(--line);
  background: rgba(246, 243, 238, 0.92);
  backdrop-filter: blur(12px);
}

.brand {
  font-weight: 800;
  color: var(--accent);
}

.topbar nav {
  display: flex;
  gap: 16px;
  color: var(--muted);
  font-size: 14px;
}

main {
  max-width: 1180px;
  margin: 0 auto;
  padding: 28px;
}

.login-shell {
  min-height: calc(100vh - 120px);
  display: grid;
  place-items: center;
}

.login-panel {
  width: min(420px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 28px;
}

.login-panel h1,
.page-head h1,
.chat-side h1 {
  margin: 0 0 14px;
  letter-spacing: 0;
}

label {
  display: grid;
  gap: 8px;
  margin: 14px 0;
  color: var(--muted);
  font-size: 14px;
}

input,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fffdf9;
  color: var(--ink);
  font: inherit;
  padding: 12px 14px;
}

textarea {
  resize: vertical;
  min-height: 72px;
}

button,
.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 14px;
  padding: 10px 14px;
  cursor: pointer;
}

button:hover,
.button:hover {
  background: var(--accent-dark);
}

.button.secondary {
  background: var(--blue);
}

.new-session-form {
  margin: 12px 0 0;
}

.new-session-form .button {
  width: 100%;
}

button:disabled,
.button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.button.danger {
  width: 100%;
  margin-top: 10px;
  background: #8b3a2f;
}

.error {
  color: var(--accent);
  font-weight: 700;
}

.page-head {
  margin-bottom: 24px;
}

.page-head p,
.muted {
  color: var(--muted);
}

.backlink {
  display: inline-block;
  margin-bottom: 12px;
  color: var(--accent);
  font-weight: 700;
}

.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.tags span {
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fffdf9;
  color: var(--muted);
  font-size: 13px;
  padding: 4px 10px;
}

.world-grid,
.character-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 18px;
}

.world-card,
.character-card,
.session-strip {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(37, 33, 28, 0.08);
}

.world-card {
  display: grid;
  gap: 10px;
  padding: 20px;
}

.world-card h2,
.character-card h2 {
  margin: 0 0 4px;
  font-size: 20px;
}

.character-card {
  overflow: hidden;
}

.character-card img {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
  object-position: center 28%;
  display: block;
}

.character-body {
  padding: 18px;
}

.role {
  color: var(--accent);
  font-weight: 700;
  margin: 0 0 8px;
}

.session-strip {
  margin-top: 24px;
  padding: 18px;
}

.session-strip a,
.session-list a {
  display: block;
  padding: 8px 0;
  color: var(--blue);
}

.chat-layout {
  max-width: none;
  display: grid;
  grid-template-columns: 310px minmax(0, 1fr);
  gap: 20px;
}

.chat-side {
  align-self: start;
  position: sticky;
  top: 82px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 18px;
}

.avatar-large {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  object-position: center 25%;
  border-radius: 8px;
  margin-bottom: 14px;
}

.session-list {
  max-height: 220px;
  overflow: auto;
  border-top: 1px solid var(--line);
}

.session-list a.active {
  color: var(--accent);
  font-weight: 800;
}

.session-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 30px;
  gap: 8px;
  align-items: center;
  border-bottom: 1px solid rgba(222, 210, 191, 0.65);
}

.session-row a {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.session-row.active a {
  color: var(--accent);
  font-weight: 800;
}

.session-row form {
  margin: 0;
}

.icon-danger {
  width: 28px;
  height: 28px;
  padding: 0;
  border-radius: 50%;
  background: transparent;
  color: #9a4034;
  font-size: 20px;
  line-height: 1;
}

.icon-danger:hover {
  background: #ecd8d3;
  color: #6d251d;
}

.chat-main {
  min-height: calc(100vh - 118px);
  display: grid;
  grid-template-rows: 1fr auto auto;
  gap: 12px;
}

.messages {
  background: #fffdf9;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 18px;
  overflow-y: auto;
  min-height: 520px;
  max-height: calc(100vh - 240px);
}

.empty-state {
  color: var(--muted);
  text-align: center;
  padding: 80px 0;
}

.message {
  display: flex;
  margin: 12px 0;
}

.message.user {
  justify-content: flex-end;
}

.bubble {
  max-width: min(760px, 88%);
  white-space: pre-wrap;
  background: #f0e7d9;
  border-radius: 8px;
  padding: 12px 14px;
}

.message.user .bubble {
  background: #dfe7ef;
}

.composer {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: end;
}

.composer button {
  height: 72px;
  min-width: 96px;
}

.matched {
  min-height: 24px;
  color: var(--muted);
  font-size: 13px;
}

@media (max-width: 820px) {
  main {
    padding: 18px;
  }

  .chat-layout {
    grid-template-columns: 1fr;
  }

  .chat-side {
    position: static;
  }

  .composer {
    grid-template-columns: 1fr;
  }

  .composer button {
    height: 46px;
  }
}
