:root {
  --bg: #f2f2f7;
  --surface: #ffffff;
  --ink: #000000;
  --muted: #8e8e93;
  --line: rgba(60, 60, 67, 0.12);
  --blue: #007aff;
  --bubble-me: #007aff;
  --bubble-them: #e9e9eb;
  --danger: #ff3b30;
  --nav-h: 52px;
  --composer-h: 56px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --app-h: 100dvh;
  --scroll-thumb: rgba(60, 60, 67, 0.28);
}

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

html {
  min-height: 100dvh;
}

html,
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

body {
  overflow: hidden;
}

#app {
  height: var(--app-h);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.hidden { display: none !important; }

button,
input,
textarea {
  font: inherit;
  touch-action: manipulation;
}

button {
  cursor: pointer;
}

button:active {
  transform: scale(0.97);
}

/* Login */
.gate {
  min-height: var(--app-h);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px 20px calc(24px + var(--safe-b));
  background: var(--bg);
}

.gate-mark {
  width: 88px;
  height: 88px;
  border-radius: 22px;
  overflow: hidden;
  margin-bottom: 20px;
  box-shadow: 0 8px 24px rgba(52, 199, 89, 0.28);
}

.gate-mark img {
  width: 100%;
  height: 100%;
  display: block;
}

.gate h1 {
  margin: 0 0 6px;
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.03em;
}

.gate-err {
  min-height: 1.2em;
  color: var(--danger);
  font-size: 15px;
  margin: 0 0 12px;
}

.pin-form {
  width: min(100%, 320px);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.pin-form > input {
  height: 48px;
  border: 0;
  border-radius: 12px;
  background: var(--surface);
  padding: 0 14px;
  font-size: 17px;
  box-shadow: 0 0 0 1px var(--line);
}

.pin-form > input:focus {
  outline: none;
  box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.35);
}

.pin-row {
  display: flex;
  gap: 10px;
  width: 100%;
}

.pin-row input {
  flex: 1;
  height: 48px;
  border: 0;
  border-radius: 12px;
  background: var(--surface);
  padding: 0 14px;
  font-size: 17px;
  box-shadow: 0 0 0 1px var(--line);
}

.pin-row input:focus {
  outline: none;
  box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.35);
}

.pin-row button {
  height: 48px;
  padding: 0 18px;
  border: 0;
  border-radius: 12px;
  background: var(--blue);
  color: #fff;
  font-weight: 600;
}

.busy {
  opacity: 0.55;
  pointer-events: none;
}

/* Nav */
.im-nav {
  flex: 0 0 auto;
  padding: 0 8px;
  padding-top: env(safe-area-inset-top, 0px);
  height: calc(var(--nav-h) + env(safe-area-inset-top, 0px));
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 0.5px solid var(--line);
  z-index: 4;
}

.im-nav-title {
  font-size: 17px;
  font-weight: 600;
  letter-spacing: -0.02em;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0;
  line-height: 1.15;
}

.im-nav-title small {
  font-size: 11px;
  font-weight: 400;
  color: var(--muted);
}

.im-nav-title .im-avatar {
  width: 28px;
  height: 28px;
  font-size: 12px;
  margin-bottom: 2px;
}

.im-icon {
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--blue);
  font-size: 22px;
  line-height: 1;
  display: grid;
  place-items: center;
  padding: 0;
}

.im-icon svg {
  width: 22px;
  height: 22px;
  display: block;
}

.im-icon.muted {
  color: var(--muted);
}

.im-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: linear-gradient(180deg, #5ac8fa, #007aff);
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 14px;
  font-weight: 700;
}

/* Lists */
.im-scroll {
  flex: 1;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--surface);
  scrollbar-width: thin;
  scrollbar-color: var(--scroll-thumb) transparent;
}

.im-scroll::-webkit-scrollbar,
.bubbles::-webkit-scrollbar {
  width: 8px;
}

.im-scroll::-webkit-scrollbar-thumb,
.bubbles::-webkit-scrollbar-thumb {
  background: var(--scroll-thumb);
  border-radius: 99px;
}

.search-wrap {
  padding: 8px 16px 4px;
  background: var(--bg);
}

.search-wrap input {
  width: 100%;
  height: 36px;
  border: 0;
  border-radius: 10px;
  background: #e5e5ea;
  padding: 0 12px;
  font-size: 16px;
}

.search-wrap input:focus {
  outline: none;
}

.thread-row {
  display: grid;
  grid-template-columns: 52px 1fr auto;
  gap: 10px;
  align-items: center;
  width: 100%;
  border: 0;
  background: var(--surface);
  text-align: left;
  padding: 10px 16px;
  border-bottom: 0.5px solid var(--line);
  min-height: 72px;
}

.thread-row:active {
  background: rgba(60, 60, 67, 0.08);
  transform: none;
}

.thread-row .av {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: linear-gradient(180deg, #ff9f0a, #ff375f);
  color: #fff;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 16px;
}

.thread-row .meta {
  min-width: 0;
}

.thread-row .who {
  display: block;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: -0.02em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.thread-row .snip {
  display: block;
  font-size: 15px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.thread-row .when {
  font-size: 15px;
  color: var(--muted);
  align-self: start;
  padding-top: 2px;
}

.thread-row.unread .who,
.thread-row.unread .snip {
  color: var(--ink);
  font-weight: 600;
}

.empty {
  padding: 48px 24px;
  text-align: center;
  color: var(--muted);
  font-size: 16px;
}

/* Thread */
.thread-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--bg);
}

.bubbles {
  flex: 1;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px 8px 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  width: 100%;
  max-width: 720px;
  align-self: center;
  scrollbar-width: thin;
  scrollbar-color: var(--scroll-thumb) transparent;
}

.day-chip {
  align-self: center;
  font-size: 12px;
  color: var(--muted);
  margin: 10px 0 6px;
}

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

.bubble-row.me {
  justify-content: flex-end;
}

.bubble-row.them {
  justify-content: flex-start;
}

.bubble {
  width: fit-content;
  max-width: 78%;
  padding: 8px 12px;
  font-size: 17px;
  line-height: 1.28;
  letter-spacing: -0.01em;
  word-break: break-word;
  white-space: pre-wrap;
  user-select: text;
  -webkit-user-select: text;
}

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

.bubble-row.me .bubble {
  align-self: flex-end;
}

.bubble.them {
  background: var(--bubble-them);
  color: #000;
  border-radius: 18px 18px 18px 5px;
}

.bubble.me {
  background: var(--bubble-me);
  color: #fff;
  border-radius: 18px 18px 5px 18px;
}

.bubble-row.stack-prev.them .bubble {
  border-top-left-radius: 5px;
}

.bubble-row.stack-next.them .bubble {
  border-bottom-left-radius: 5px;
}

.bubble-row.stack-prev.me .bubble {
  border-top-right-radius: 5px;
}

.bubble-row.stack-next.me .bubble {
  border-bottom-right-radius: 5px;
}

.bubble .sub {
  display: block;
  font-size: 11px;
  opacity: 0.72;
  margin-bottom: 4px;
}

.composer {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 10px calc(8px + var(--safe-b));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(16px);
  border-top: 0.5px solid var(--line);
}

.composer .plus,
.composer .send {
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  display: grid;
  place-items: center;
  padding: 0;
  flex: none;
}

.composer .plus {
  background: #d1d1d6;
  color: #3a3a3c;
  font-size: 22px;
  line-height: 1;
}

.composer .send svg,
.composer .plus svg {
  width: 18px;
  height: 18px;
  display: block;
}

.composer textarea {
  flex: 1;
  min-height: 36px;
  max-height: 120px;
  resize: none;
  border: 0;
  border-radius: 18px;
  background: var(--surface);
  box-shadow: 0 0 0 0.5px var(--line);
  padding: 8px 14px;
  font-size: 17px;
  line-height: 1.25;
}

.composer textarea:focus {
  outline: none;
}

.composer .send {
  background: var(--blue);
  color: #fff;
}

.composer .send:disabled {
  background: #c7c7cc;
}

.sheet {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(0, 0, 0, 0.32);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  animation: sheet-fade 0.16s ease;
}

.sheet-card {
  width: 100%;
  max-width: 520px;
  background: #f2f2f7;
  border-radius: 14px 14px 0 0;
  padding: 12px 16px calc(16px + var(--safe-b));
  animation: sheet-up 0.2s ease;
}

.sheet-card::before {
  content: "";
  display: block;
  width: 36px;
  height: 5px;
  border-radius: 99px;
  background: rgba(60, 60, 67, 0.28);
  margin: 2px auto 10px;
}

@keyframes sheet-fade {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes sheet-up {
  from { transform: translateY(18%); }
  to { transform: translateY(0); }
}

.sheet-card h2 {
  margin: 4px 0 12px;
  font-size: 17px;
  text-align: center;
}

.sheet-card label {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin: 10px 0 4px;
}

.sheet-card input {
  width: 100%;
  height: 44px;
  border: 0;
  border-radius: 10px;
  padding: 0 12px;
  background: #fff;
}

.sheet-actions {
  display: flex;
  gap: 8px;
  margin-top: 16px;
}

.sheet-actions button {
  flex: 1;
  height: 44px;
  border: 0;
  border-radius: 12px;
  font-weight: 600;
}

.sheet-actions .cancel {
  background: #e5e5ea;
}

.sheet-actions .go {
  background: var(--blue);
  color: #fff;
}

.sheet-status {
  min-height: 1.2em;
  margin: 8px 0 0;
  font-size: 14px;
  color: var(--muted);
  text-align: center;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + var(--safe-b));
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.78);
  color: #fff;
  padding: 8px 14px;
  border-radius: 14px;
  font-size: 14px;
  z-index: 40;
}

@media (prefers-reduced-motion: reduce) {
  button:active,
  .sheet,
  .sheet-card {
    animation: none;
    transform: none;
  }
}
