/* ---------- Theme tokens ---------- */
:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --text: #1d2330;
  --muted: #667085;
  --border: #d9dee7;
  --accent: #2f6fed;
  --accent-text: #ffffff;
  --own-bg: #2f6fed;
  --own-text: #ffffff;
  --ok: #2e7d32;
  --warn: #b26a00;
  --danger: #c62828;
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1420;
    --surface: #171e2e;
    --surface-2: #1e2739;
    --text: #e6eaf2;
    --muted: #8b95a7;
    --border: #2a3448;
    --accent: #4c8dff;
    --accent-text: #0b1020;
    --own-bg: #3b6fd8;
    --own-text: #ffffff;
    --ok: #66bb6a;
    --warn: #ffb74d;
    --danger: #ef7070;
  }
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }

[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.4;
}

.visually-hidden {
  position: absolute !important;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  margin: -1px;
  padding: 0;
  border: 0;
}

input:focus-visible,
button:focus-visible,
summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- App shell ---------- */
.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

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

.app-header h1 {
  margin: 0;
  font-size: 1.15rem;
}

.app-footer {
  background: var(--surface);
  border-top: 1px solid var(--border);
}

/* ---------- Connection status ---------- */
.status {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
  color: var(--muted);
}

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--muted);
}

.status[data-state="disconnected"] .status-dot { background: var(--danger); }
.status[data-state="connecting"] .status-dot { background: var(--warn); animation: pulse 1s infinite alternate; }
.status[data-state="connected"] .status-dot { background: var(--ok); }

@keyframes pulse {
  from { opacity: 0.3; }
  to { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .status-dot { animation: none !important; }
}

/* ---------- Settings bar ---------- */
.settings {
  display: grid;
  grid-template-columns: 2fr 1fr 1fr auto;
  gap: 12px;
  align-items: end;
  padding: 12px 16px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.field label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
}

.settings .error {
  grid-column: 1 / -1;
}

input[type="text"] {
  width: 100%;
  min-width: 0;
  padding: 8px 10px;
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
}

input[type="text"]:disabled {
  opacity: 0.6;
}

button {
  font: inherit;
  font-weight: 600;
  padding: 8px 16px;
  color: var(--accent-text);
  background: var(--accent);
  border: 1px solid transparent;
  border-radius: 8px;
  cursor: pointer;
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.settings button {
  height: 36px;
}

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

/* ---------- Main layout: chat + sidebar ---------- */
.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 220px;
}

.chat {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.messages {
  flex: 1;
  min-height: 0;
  margin: 0;
  padding: 16px;
  list-style: none;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.message {
  align-self: flex-start;
  max-width: 75%;
  padding: 8px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px 12px 12px 4px;
}

/* Your own messages: right-aligned and filled with the accent colour */
.message.own {
  align-self: flex-end;
  color: var(--own-text);
  background: var(--own-bg);
  border-color: transparent;
  border-radius: 12px 12px 4px 12px;
}

.meta {
  display: flex;
  align-items: baseline;
  gap: 8px;
  font-size: 0.78rem;
  color: var(--muted);
}

.message.own .meta {
  color: inherit;
  opacity: 0.85;
}

.author {
  font-weight: 600;
}

.text {
  margin: 2px 0 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.chat-error {
  margin: 0;
  padding: 8px 16px;
  font-size: 0.9rem;
  color: var(--danger);
  background: color-mix(in srgb, var(--danger) 10%, transparent);
  border-top: 1px solid var(--border);
}

/* ---------- Sidebar: online users ---------- */
.sidebar {
  min-height: 0;
  padding: 16px;
  overflow-y: auto;
  background: var(--surface);
  border-left: 1px solid var(--border);
}

.sidebar h2 {
  margin: 0 0 8px;
  font-size: 0.95rem;
}

#online-list {
  margin: 0;
  padding: 0;
  list-style: none;
}

#online-list li {
  padding: 6px 0;
  border-bottom: 1px dashed var(--border);
  overflow-wrap: anywhere;
}

#online-list li.me {
  font-weight: 600;
}

/* ---------- Footer: composer + event log ---------- */
.composer {
  display: flex;
  gap: 8px;
  padding: 12px 16px 8px;
}

.composer input {
  flex: 1;
}

.composer button {
  height: 40px;
}

.event-log-panel {
  padding: 0 16px 12px;
}

.event-log-panel summary {
  padding: 6px 0;
  font-size: 0.85rem;
  color: var(--muted);
  cursor: pointer;
}

.event-log {
  max-height: 160px;
  margin: 0;
  padding: 8px 8px 8px 24px;
  overflow-y: auto;
  font-family: var(--mono);
  font-size: 0.78rem;
  background: var(--surface-2);
  border-radius: 8px;
}

.event-log li {
  padding: 1px 0;
  overflow-wrap: anywhere;
}

.log-time {
  margin-right: 6px;
  color: var(--muted);
}

/* ---------- Narrow screens: sidebar stacks below the chat ---------- */
@media (max-width: 720px) {
  .app {
    height: auto;
    min-height: 100vh;
    min-height: 100dvh;
  }

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

  .settings .field-url,
  .settings button {
    grid-column: 1 / -1;
  }

  .layout {
    grid-template-columns: minmax(0, 1fr);
  }

  .messages {
    flex: none;
    height: 60vh;
  }

  .message {
    max-width: 90%;
  }

  .sidebar {
    border-left: 0;
    border-top: 1px solid var(--border);
  }
}

@media (max-width: 480px) {
  .settings {
    grid-template-columns: 1fr;
  }
}
