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

/* `hidden` must win over the display rules below. */
[hidden] {
  display: none !important;
}

:root {
  --bg: #f4f5f2;
  --panel: #ffffff;
  --ink: #171a14;
  --muted: #6b7166;
  --line: #e1e4dc;
  --accent: #2f6b1e;
  --accent-ink: #ffffff;
  --in: #ffffff;
  --out: #dff3d0;
  --danger: #b3261e;
  --notice: #fff6d6;
  font-family: system-ui, -apple-system, "Segoe UI", Helvetica, Arial, sans-serif;
  color: var(--ink);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11140f;
    --panel: #1a1e17;
    --ink: #ecefe7;
    --muted: #9aa193;
    --line: #2c3227;
    --accent: #9ad26f;
    --accent-ink: #11140f;
    --in: #23281f;
    --out: #2c4a1f;
    --danger: #ff8a80;
    --notice: #3a3416;
  }
}

html,
body {
  height: 100%;
  margin: 0;
  background: var(--bg);
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
  border: 0;
  background: none;
  border-radius: 8px;
  padding: 8px 10px;
  min-height: 40px;
}

button:disabled {
  opacity: 0.45;
  cursor: default;
}

.ghost:hover:not(:disabled),
.icon:hover:not(:disabled) {
  background: color-mix(in srgb, var(--ink) 8%, transparent);
}

.primary-button,
.icon.primary {
  background: var(--accent);
  color: var(--accent-ink);
}

.muted {
  color: var(--muted);
}
.small {
  font-size: 0.82rem;
}
.spacer {
  flex: 1;
}
.error {
  color: var(--danger);
  margin: 0;
  font-size: 0.88rem;
}
.error:empty {
  display: none;
}

/* --- Sign-in --- */
.login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 16px;
}
.login-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 32px;
  width: min(380px, 100%);
  display: grid;
  gap: 12px;
  justify-items: center;
  text-align: center;
}
.login-card h1 {
  margin: 0;
  font-size: 1.4rem;
}
.login-card p {
  margin: 0;
}

/* --- Layout --- */
.app {
  height: 100dvh;
  display: grid;
  grid-template-columns: minmax(280px, 360px) 1fr;
}
.sidebar,
.thread {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--line);
}
.bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  min-height: 56px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
.empty {
  margin: auto;
  padding: 24px;
  color: var(--muted);
  text-align: center;
}

/* --- Conversation list --- */
.contacts {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
}
.contact {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 2px 8px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
}
.contact:hover {
  background: color-mix(in srgb, var(--ink) 5%, transparent);
}
.contact.active {
  background: color-mix(in srgb, var(--accent) 16%, transparent);
}
.contact .name {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.contact .preview {
  color: var(--muted);
  font-size: 0.88rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.contact .time {
  color: var(--muted);
  font-size: 0.76rem;
  white-space: nowrap;
}
.badge {
  justify-self: end;
  background: var(--accent);
  color: var(--accent-ink);
  border-radius: 999px;
  font-size: 0.74rem;
  font-weight: 700;
  min-width: 20px;
  padding: 2px 6px;
  text-align: center;
}

/* --- Thread --- */
.thread-body {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}
.thread-body[hidden],
.empty[hidden] {
  display: none;
}
.who {
  display: grid;
  min-width: 0;
}
.back {
  display: none;
  font-size: 1.2rem;
}
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.day {
  align-self: center;
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--muted);
  border-radius: 999px;
  font-size: 0.74rem;
  padding: 2px 10px;
  margin: 8px 0;
}
.bubble {
  position: relative;
  max-width: min(520px, 82%);
  padding: 7px 10px 5px;
  border-radius: 12px;
  background: var(--in);
  border: 1px solid var(--line);
  align-self: flex-start;
  overflow-wrap: anywhere;
}
.bubble.out {
  align-self: flex-end;
  background: var(--out);
}
.bubble.has-reaction {
  margin-bottom: 14px;
}
.bubble .text {
  white-space: pre-wrap;
  margin: 0;
}
.bubble a {
  color: inherit;
}
.bubble img,
.bubble video {
  display: block;
  max-width: 100%;
  max-height: 340px;
  border-radius: 8px;
  margin-bottom: 4px;
}
.bubble img.sticker {
  max-height: 140px;
}
.bubble audio {
  display: block;
  width: 260px;
  max-width: 100%;
}
.file {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  text-decoration: none;
  margin-bottom: 4px;
}
.kind {
  color: var(--muted);
  font-style: italic;
  font-size: 0.86rem;
}
.meta {
  display: flex;
  justify-content: flex-end;
  gap: 6px;
  color: var(--muted);
  font-size: 0.7rem;
  margin-top: 2px;
}
.meta .read {
  color: #1a8cd8;
}
.failure {
  color: var(--danger);
  font-size: 0.8rem;
  margin-top: 2px;
}
.reaction {
  position: absolute;
  bottom: -14px;
  left: 8px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0 5px;
  font-size: 0.86rem;
}

/* --- Composer --- */
.notice {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  padding: 8px 12px;
  background: var(--notice);
  font-size: 0.88rem;
}
.notice[hidden],
.attachment[hidden],
.composer[hidden] {
  display: none;
}
.notice button {
  background: var(--accent);
  color: var(--accent-ink);
}
.send-error {
  padding: 6px 12px;
}
.attachment {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 12px;
  font-size: 0.88rem;
  border-top: 1px solid var(--line);
  background: var(--panel);
}
.composer {
  display: flex;
  align-items: flex-end;
  gap: 4px;
  padding: 8px;
  padding-bottom: calc(8px + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--line);
  background: var(--panel);
}
.composer textarea {
  flex: 1;
  min-width: 0;
  resize: none;
  max-height: 140px;
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 9px 14px;
  background: var(--bg);
  /* 16px keeps iOS Safari from zooming in on focus. */
  font-size: 16px;
  line-height: 1.35;
}
.icon {
  width: 44px;
  height: 44px;
  padding: 0;
  display: grid;
  place-items: center;
  border-radius: 50%;
  flex: none;
}
.recording {
  align-items: center;
  gap: 10px;
}
.dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--danger);
  animation: pulse 1s infinite;
}
@keyframes pulse {
  50% {
    opacity: 0.25;
  }
}
@media (prefers-reduced-motion: reduce) {
  .dot {
    animation: none;
  }
}

/* --- Dialogs --- */
dialog {
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
  color: var(--ink);
  width: min(460px, calc(100vw - 32px));
  padding: 20px;
}
dialog::backdrop {
  background: rgb(0 0 0 / 0.45);
}
dialog form {
  display: grid;
  gap: 12px;
}
dialog h2 {
  margin: 0;
  font-size: 1.15rem;
}
dialog p {
  margin: 0;
}
dialog label {
  display: grid;
  gap: 4px;
  font-size: 0.88rem;
}
dialog input,
dialog select {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 10px;
  background: var(--bg);
  font-size: 16px;
}
dialog pre {
  margin: 0;
  white-space: pre-wrap;
  font: inherit;
  font-size: 0.9rem;
  background: var(--bg);
  border-radius: 8px;
  padding: 10px;
  max-height: 180px;
  overflow: auto;
}
#template-variables {
  display: grid;
  gap: 8px;
}
.actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

/* --- Phone: one pane at a time --- */
@media (max-width: 768px) {
  .app {
    grid-template-columns: 1fr;
  }
  .thread {
    display: none;
  }
  .app.thread-open .sidebar {
    display: none;
  }
  .app.thread-open .thread {
    display: flex;
  }
  .back {
    display: block;
  }
}
