:root {
  --bg: #f6ede0;
  --surface: #fffaf3;
  --surface-alt: #f0e2ce;
  --border: #e0cfb2;
  --text: #332a22;
  --text-muted: #7a6a58;
  --accent: #c1502e;
  --accent-hover: #a5411f;
  --amber: #d9a441;
  --brown: #8a6240;
  --olive: #6f7a45;
  --danger: #a5411f;
  --radius: 10px;
  --shadow: 0 2px 8px rgba(51, 42, 34, 0.08);
}

* {
  box-sizing: border-box;
}

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

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
}

#app {
  height: 100%;
}

button {
  font-family: inherit;
  cursor: pointer;
}

input,
textarea {
  font-family: inherit;
  font-size: 1rem;
}

a {
  color: var(--accent);
}

/* Login screen */

.login-screen {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 2rem;
  width: 100%;
  max-width: 360px;
  text-align: center;
}

.login-card h1 {
  margin: 0 0 0.25rem;
  color: var(--accent);
}

.tagline {
  color: var(--text-muted);
  margin-top: 0;
}

.login-card form {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  text-align: left;
  margin-top: 1.5rem;
}

.login-card label {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.login-card input {
  padding: 0.6rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
}

.login-card button[type='submit'] {
  margin-top: 0.5rem;
  background: var(--accent);
  color: var(--surface);
  border: none;
  padding: 0.65rem;
  border-radius: 6px;
  font-weight: 600;
}

.login-card button[type='submit']:hover {
  background: var(--accent-hover);
}

.form-error {
  color: var(--danger);
  font-size: 0.85rem;
  margin-bottom: 0;
}

/* App shell */

.app-shell {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.book-info h1 {
  margin: 0;
  font-size: 1.1rem;
}

.book-author {
  color: var(--text-muted);
  font-size: 0.85rem;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.avatar-button,
.user-info button {
  display: inline-flex;
  align-items: center;
  height: 2.25rem;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0 0.7rem;
  color: var(--text);
  line-height: 1;
}

.avatar-button:hover,
.user-info button:hover {
  background: var(--border);
}

.app-body {
  flex: 1;
  display: flex;
  min-height: 0;
}

/* Sidebar */

.topic-sidebar {
  width: 220px;
  flex-shrink: 0;
  background: var(--surface-alt);
  border-right: 1px solid var(--border);
  overflow-y: auto;
}

.topic-list {
  list-style: none;
  margin: 0;
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.topic-item {
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  border-radius: 6px;
  padding: 0.55rem 0.7rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  color: var(--text);
}

.topic-item:hover {
  background: var(--border);
}

.topic-item.active {
  background: var(--accent);
  color: var(--surface);
}

.topic-item.closed {
  color: var(--text-muted);
}

.topic-item.active.closed {
  color: var(--surface);
}

/* Message pane */

.message-pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.message-feed {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 1.5rem;
}

.empty-feed {
  color: var(--text-muted);
  text-align: center;
  margin-top: 2rem;
}

.message-thread {
  margin-bottom: 1rem;
}

.message {
  display: flex;
  gap: 0.75rem;
  padding: 0.5rem;
  border-radius: var(--radius);
}

.message:hover {
  background: var(--surface);
}

.message-avatar {
  font-size: 1.5rem;
  line-height: 1;
  width: 2rem;
  text-align: center;
  flex-shrink: 0;
}

.message-content {
  flex: 1;
  min-width: 0;
}

.message-meta {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
}

.message-author {
  font-weight: 600;
}

.message-time {
  color: var(--text-muted);
  font-size: 0.8rem;
}

.message-edited {
  color: var(--text-muted);
  font-size: 0.75rem;
  font-style: italic;
}

.message-body {
  margin: 0.2rem 0 0;
  word-wrap: break-word;
}

.message-body blockquote {
  margin: 0.4rem 0;
  padding-left: 0.75rem;
  border-left: 3px solid var(--amber);
  color: var(--text-muted);
}

.deleted-placeholder {
  color: var(--text-muted);
  font-style: italic;
}

.reply-button {
  background: none;
  border: none;
  color: var(--olive);
  font-size: 0.8rem;
  padding: 0;
  margin-top: 0.3rem;
}

.reply-button:hover {
  text-decoration: underline;
}

.replies {
  margin-left: 2.25rem;
  border-left: 2px solid var(--border);
  padding-left: 0.75rem;
  margin-top: 0.25rem;
}

/* Composer */

.composer {
  border-top: 1px solid var(--border);
  background: var(--surface);
  padding: 0.75rem 1.5rem 1rem;
}

.composer.closed-note {
  color: var(--text-muted);
  font-style: italic;
  text-align: center;
}

.composer textarea {
  width: 100%;
  min-height: 4.5rem;
  resize: vertical;
  padding: 0.6rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
}

.composer-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 0.4rem;
}

.markdown-hint {
  color: var(--text-muted);
  font-size: 0.75rem;
  font-family: 'SF Mono', Consolas, monospace;
}

.composer button[type='submit'] {
  background: var(--accent);
  color: var(--surface);
  border: none;
  border-radius: 6px;
  padding: 0.5rem 1.1rem;
  font-weight: 600;
}

.composer button[type='submit']:hover {
  background: var(--accent-hover);
}

.reply-banner {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.4rem 0.6rem;
  margin-bottom: 0.5rem;
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.reply-snippet {
  color: var(--text-muted);
  font-style: italic;
}

.cancel-reply {
  margin-left: auto;
  background: none;
  border: none;
  color: var(--danger);
  font-size: 1rem;
  line-height: 1;
}

/* Profile modal */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(51, 42, 34, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.modal {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.5rem;
  width: 100%;
  max-width: 380px;
  max-height: 100%;
  overflow-y: auto;
}

.modal h2 {
  margin-top: 0;
}

.modal form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.modal label[for] {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.modal input[type='text'],
.modal input:not([type]) {
  padding: 0.55rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  width: 100%;
}

.avatar-picker {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.6rem;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.4rem;
}

.avatar-picker legend {
  color: var(--text-muted);
  font-size: 0.85rem;
  padding: 0 0.3rem;
}

.avatar-option {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.4rem;
  cursor: pointer;
  font-size: 0.75rem;
  position: relative;
  color: var(--text-muted);
}

/* Follows the checked radio, so the highlight moves as soon as an avatar is clicked. */
.avatar-option:has(input:checked) {
  border-color: var(--accent);
  background: var(--surface-alt);
  color: var(--text);
}

.avatar-option input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.avatar-option:has(input:focus-visible) {
  outline: 2px solid var(--accent);
}

.avatar-glyph {
  font-size: 1.4rem;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.modal-actions button[type='submit'] {
  background: var(--accent);
  color: var(--surface);
  border: none;
  border-radius: 6px;
  padding: 0.5rem 1rem;
  font-weight: 600;
}

.modal-actions button[type='button'] {
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.5rem 1rem;
}

@media (max-width: 640px) {
  .app-body {
    flex-direction: column;
  }

  .topic-sidebar {
    width: 100%;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  .topic-list {
    flex-direction: row;
    overflow-x: auto;
  }

  .replies {
    margin-left: 1rem;
  }
}

/* Message edit/delete actions */

.message-actions {
  display: flex;
  gap: 0.75rem;
  margin-top: 0.3rem;
}

.edit-button,
.delete-button {
  background: none;
  border: none;
  font-size: 0.8rem;
  padding: 0;
}

.edit-button {
  color: var(--olive);
}

.delete-button {
  color: var(--danger);
}

.edit-button:hover,
.delete-button:hover {
  text-decoration: underline;
}

.edit-message-form textarea {
  width: 100%;
  min-height: 4rem;
  resize: vertical;
  margin-top: 0.3rem;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
}

.edit-message-form .composer-actions {
  justify-content: flex-end;
  gap: 0.5rem;
}

.edit-message-form button[type='submit'] {
  background: var(--accent);
  color: var(--surface);
  border: none;
  border-radius: 6px;
  padding: 0.4rem 0.9rem;
  font-weight: 600;
}

.edit-message-form button[type='button'] {
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.4rem 0.9rem;
}

/* Empty state (no active book) */

.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.25rem;
  color: var(--text-muted);
  padding: 2rem;
  text-align: center;
}

/* Admin panel */

.admin-panel {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1.75rem;
  max-width: 720px;
  margin: 0 auto;
  width: 100%;
}

.admin-section h2 {
  margin: 0 0 0.75rem;
  font-size: 1.1rem;
}

.admin-inline-form {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.admin-inline-form input,
.admin-inline-form select {
  padding: 0.45rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
}

.admin-inline-form input[name='title'],
.admin-inline-form input[name='displayName'] {
  flex: 1;
  min-width: 10rem;
}

.admin-inline-form button[type='submit'] {
  background: var(--accent);
  color: var(--surface);
  border: none;
  border-radius: 6px;
  padding: 0.45rem 1rem;
  font-weight: 600;
}

.admin-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.admin-list li {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.5rem 0.7rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.admin-list li.selected {
  border-color: var(--accent);
}

.admin-list button {
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.3rem 0.6rem;
  font-size: 0.85rem;
}

.admin-code-input {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.3rem 0.6rem;
  font-size: 0.85rem;
  width: 10rem;
}

.admin-list-select {
  flex: 1;
  text-align: left;
  border: none !important;
  background: none;
  padding: 0 !important;
  font-weight: 600;
}

.admin-item-name {
  flex: 1;
  font-weight: 600;
}

.admin-item-role {
  color: var(--text-muted);
  font-size: 0.85rem;
}

.current-badge {
  color: var(--olive);
  font-size: 0.8rem;
  font-weight: 600;
  margin-left: 0.4rem;
}

.one-time-code {
  background: var(--surface-alt);
  border: 1px solid var(--amber);
  border-radius: 8px;
  padding: 0.6rem 0.8rem;
  font-size: 0.9rem;
}

.one-time-code code {
  background: var(--surface);
  padding: 0.1rem 0.4rem;
  border-radius: 4px;
}

.one-time-code button {
  margin-left: 0.5rem;
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.2rem 0.5rem;
}
