:root {
  --brand-bg: #2e2d2a;
  --brand-accent: #32d24d;
  --bg: var(--brand-bg);
  --surface: #3a3935;
  --surface-2: #45443f;
  --text: #f5f5f2;
  --muted: #b8b7b0;
  --accent: var(--brand-accent);
  --accent-dim: rgba(50, 210, 77, 0.14);
  --accent-border: rgba(50, 210, 77, 0.35);
  --inbound: #353430;
  --outbound: #2a3d2e;
  --border: rgba(255, 255, 255, 0.1);
  --danger: #ff6b6b;
  --header-h: 3.25rem;
  --composer-h: 4.5rem;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 640px;
  margin: 0 auto;
}

.header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-height: var(--header-h);
  padding: 0.5rem 0.75rem;
  padding-top: max(0.5rem, env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.header-title {
  flex: 1;
  min-width: 0;
  margin: 0;
  font-size: 1.1rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.thread-actions {
  display: flex;
  gap: 0.35rem;
  flex-shrink: 0;
}

.thread-actions[hidden] {
  display: none !important;
}

.action-btn {
  min-height: 36px;
  padding: 0.35rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
}

.action-btn:active {
  background: var(--inbound);
}

.action-btn--danger {
  border-color: rgba(255, 107, 107, 0.35);
  color: #ffb4b4;
}

.icon-btn {
  min-width: 44px;
  min-height: 44px;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: var(--text);
  font-size: 1.25rem;
  cursor: pointer;
}

.icon-btn:active {
  background: var(--surface-2);
}

.icon-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

#syncBtn {
  color: var(--accent);
}

.toast {
  position: fixed;
  top: calc(var(--header-h) + 0.5rem);
  left: 50%;
  transform: translateX(-50%);
  z-index: 20;
  padding: 0.6rem 1rem;
  border-radius: 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: 0.875rem;
  max-width: calc(100% - 2rem);
  text-align: center;
}

.toast--error {
  border-color: var(--danger);
  color: #ffb4b4;
}

.list-view {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.thread-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.thread-item {
  display: block;
  width: 100%;
  padding: 0.875rem 1rem;
  border: none;
  border-bottom: 1px solid var(--border);
  background: transparent;
  color: inherit;
  text-align: left;
  cursor: pointer;
  min-height: 72px;
}

.thread-item:active {
  background: var(--surface);
}

.thread-row {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  align-items: baseline;
}

.thread-from {
  font-weight: 600;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.thread-time {
  font-size: 0.75rem;
  color: var(--muted);
  flex-shrink: 0;
}

.thread-subject {
  margin: 0.2rem 0;
  font-size: 0.875rem;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.thread-snippet {
  margin: 0;
  font-size: 0.8125rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.thread-count {
  display: inline-block;
  margin-left: 0.35rem;
  padding: 0.1rem 0.4rem;
  border-radius: 999px;
  background: var(--accent-dim);
  color: var(--accent);
  font-size: 0.7rem;
  font-weight: 600;
}

.loading,
.empty {
  padding: 2rem 1rem;
  text-align: center;
  color: var(--muted);
}

.thread-view {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 0.75rem 1rem;
  padding-bottom: calc(var(--composer-h) + var(--safe-bottom) + 1rem);
}

.thread-view[hidden] {
  display: none !important;
}

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

.thread-meta h2 {
  margin: 0 0 0.25rem;
  font-size: 1rem;
  font-weight: 600;
}

.thread-meta p {
  margin: 0;
  font-size: 0.8125rem;
  color: var(--muted);
}

.message-list {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.message {
  max-width: 92%;
  padding: 0.75rem 0.875rem;
  border-radius: 12px;
  border: 1px solid var(--border);
}

.message--inbound {
  align-self: flex-start;
  background: var(--inbound);
}

.message--outbound {
  align-self: flex-end;
  background: var(--outbound);
  border-color: var(--accent-border);
}

.message--outbound .message-from {
  color: var(--accent);
}

.message-header {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.4rem;
  font-size: 0.75rem;
  color: var(--muted);
}

.message-from {
  font-weight: 600;
  color: var(--text);
}

.message-direction {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.message--outbound .message-direction {
  color: var(--accent);
}

.message-body {
  font-size: 0.9375rem;
  line-height: 1.45;
  word-break: break-word;
}

.message-body--text {
  white-space: pre-wrap;
}

.message-body img {
  max-width: 100%;
  height: auto;
}

.composer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  max-width: 640px;
  margin: 0 auto;
  padding: 0.5rem 0.75rem;
  padding-bottom: max(0.5rem, var(--safe-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
  z-index: 10;
}

.composer[hidden] {
  display: none !important;
}

.reply-form {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.reply-form-row {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
}

.from-field {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.from-field-label {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.reply-from {
  width: 100%;
  padding: 0.45rem 0.55rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 0.8125rem;
}

.thread-meta .thread-delivered {
  margin-top: 0.35rem;
  font-size: 0.8125rem;
  color: var(--muted);
}

.thread-meta .thread-delivered strong {
  color: var(--text);
  font-weight: 500;
}

.reply-input {
  flex: 1;
  min-height: 44px;
  max-height: 120px;
  padding: 0.625rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  resize: none;
  line-height: 1.4;
}

.reply-input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 0;
}

.send-btn {
  min-width: 44px;
  min-height: 44px;
  border: none;
  border-radius: 12px;
  background: var(--accent);
  color: var(--brand-bg);
  font-size: 1.1rem;
  font-weight: 700;
  cursor: pointer;
  flex-shrink: 0;
}

.send-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.compose-view {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 0.75rem 1rem;
  padding-bottom: max(1rem, var(--safe-bottom));
}

.compose-view[hidden] {
  display: none !important;
}

.compose-form {
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

.compose-field {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.compose-field--grow {
  flex: 1;
}

.compose-label {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.compose-input {
  width: 100%;
  padding: 0.55rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 0.9375rem;
}

.compose-input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 0;
}

.compose-body {
  min-height: 160px;
  resize: vertical;
  line-height: 1.45;
}

.compose-send-btn {
  min-height: 48px;
  margin-top: 0.25rem;
  border: none;
  border-radius: 12px;
  background: var(--accent);
  color: var(--brand-bg);
  font: inherit;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
}

.compose-send-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.thread-item--outbound .thread-from::before {
  content: "To: ";
  color: var(--muted);
  font-weight: 500;
}

@media (min-width: 641px) {
  .app {
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
  }
}
