:root {
  --bg: #0f1210;
  --bg-elevated: #1a1f1c;
  --bg-bubble-in: #2a332e;
  --bg-bubble-out: #1c4d3a;
  --border: #2d3530;
  --text: #e8ebe9;
  --text-muted: #8a9690;
  --accent: #3dd68c;
  --accent-dim: #2a8f5e;
  --danger: #f07178;
  --radius: 14px;
  --font-display: 'Syne', system-ui, sans-serif;
  --font-body: 'DM Sans', system-ui, sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  font-family: var(--font-body);
  font-size: 15px;
  background: var(--bg);
  color: var(--text);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.app {
  display: grid;
  grid-template-columns: minmax(240px, 320px) 1fr;
  grid-template-rows: minmax(0, 1fr);
  height: 100%;
  max-height: 100dvh;
  min-height: 0;
  overflow: hidden;
}

.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  border-right: 1px solid var(--border);
  background: linear-gradient(180deg, #121614 0%, var(--bg) 45%);
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1rem 0.75rem;
}

.logo {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1.35rem;
  letter-spacing: 0.06em;
  color: var(--accent);
}

.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-elevated);
  color: var(--text-muted);
  cursor: pointer;
}

.icon-btn:hover {
  color: var(--text);
  border-color: var(--accent-dim);
}

.new-thread {
  display: flex;
  gap: 0.5rem;
  padding: 0 1rem 1rem;
}

.new-thread input {
  flex: 1;
  min-width: 0;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-elevated);
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
}

.new-thread input:focus {
  outline: 2px solid var(--accent-dim);
  outline-offset: 1px;
}

.thread-list-head {
  margin: 0;
  padding: 0.25rem 1rem 0.35rem;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-muted);
}

.thread-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 0.5rem 1rem;
}

.thread-item {
  width: 100%;
  text-align: left;
  padding: 0.75rem 0.85rem;
  margin-bottom: 4px;
  border: none;
  border-radius: var(--radius);
  background: transparent;
  color: inherit;
  font-family: inherit;
  font-size: 0.95rem;
  cursor: pointer;
}

.thread-item:hover {
  background: var(--bg-elevated);
}

.thread-item.active {
  background: var(--bg-elevated);
  box-shadow: inset 3px 0 0 var(--accent);
}

.thread-item-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.35rem;
}

.thread-item .num {
  font-weight: 600;
  display: block;
  flex: 1;
  min-width: 0;
  text-align: left;
}

.thread-busy-dot {
  flex-shrink: 0;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 10px rgba(61, 214, 140, 0.45);
}

.thread-item .preview {
  font-size: 0.8rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.hint {
  font-size: 0.75rem;
  color: var(--text-muted);
  padding: 0 1rem 1rem;
  margin: 0;
  line-height: 1.4;
}

.hint.warn {
  color: var(--danger);
}

.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background: radial-gradient(ellipse 80% 50% at 50% -20%, #1a2e24 0%, transparent 55%), var(--bg);
}

.main-head {
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
  background: rgba(15, 18, 16, 0.85);
  backdrop-filter: blur(8px);
}

.peer-label {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.05rem;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.bubble-row {
  display: flex;
  max-width: 85%;
}

.bubble-row.out {
  align-self: flex-end;
  justify-content: flex-end;
}

.bubble-row.in {
  align-self: flex-start;
}

.bubble {
  padding: 0.65rem 0.9rem;
  border-radius: var(--radius);
  line-height: 1.45;
  word-break: break-word;
}

.bubble.in {
  background: var(--bg-bubble-in);
  border-bottom-left-radius: 4px;
}

.bubble.out {
  background: var(--bg-bubble-out);
  border-bottom-right-radius: 4px;
}

.bubble-meta {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 0.35rem;
}

.composer {
  display: flex;
  gap: 0.65rem;
  align-items: stretch;
  padding: 1rem 1.25rem 1.25rem;
  border-top: 1px solid var(--border);
  background: var(--bg-elevated);
}

.composer textarea {
  flex: 1;
  min-height: 44px;
  max-height: 120px;
  resize: none;
  padding: 0.65rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  font-size: 0.95rem;
  line-height: 1.4;
  align-self: stretch;
}

.composer #btn-send {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  align-self: stretch;
  min-height: 44px;
  padding: 0.65rem 1.25rem;
}

.composer textarea:focus {
  outline: 2px solid var(--accent-dim);
  outline-offset: 1px;
}

.composer textarea:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.primary {
  padding: 0.65rem 1.1rem;
  border: none;
  border-radius: 12px;
  background: linear-gradient(145deg, var(--accent) 0%, var(--accent-dim) 100%);
  color: #061208;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  white-space: nowrap;
}

.primary:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.primary.sm {
  padding: 0.55rem 0.75rem;
  font-size: 0.8rem;
}

.ghost {
  padding: 0.55rem 1rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: transparent;
  color: var(--text-muted);
  font-family: inherit;
  cursor: pointer;
}

.modal {
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 0;
  background: var(--bg-elevated);
  color: var(--text);
  max-width: min(400px, 92vw);
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.55);
}

.modal::backdrop {
  background: rgba(0, 0, 0, 0.65);
}

.modal-form {
  padding: 1.35rem 1.5rem;
}

.modal-form h2 {
  margin: 0 0 0.5rem;
  font-family: var(--font-display);
  font-size: 1.25rem;
}

.modal-note {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin: 0 0 1rem;
  line-height: 1.45;
}

.modal-form label {
  display: block;
  font-size: 0.85rem;
  margin-bottom: 0.35rem;
  color: var(--text-muted);
}

.modal-form input[type='text'] {
  width: 100%;
  padding: 0.65rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  margin-bottom: 1rem;
}

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

code {
  font-size: 0.85em;
  background: var(--bg);
  padding: 0.1em 0.35em;
  border-radius: 4px;
}

.empty-state {
  margin: auto;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.95rem;
  padding: 2rem;
}

@media (max-width: 640px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 38vh) minmax(0, 1fr);
  }

  .sidebar {
    border-right: none;
    border-bottom: 1px solid var(--border);
    max-height: 38vh;
    min-height: 0;
  }
}
