  :root {
    --purple: #6247C4;
    --purple-dark: #4F39A0;
    --bg: #f2f0f7;
    --text-dark: #2b2b33;
    --text-light: #ffffff;
    --gray: #8a8a95;
  }
  * { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
  html, body {
    height: 100%;
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    -webkit-text-size-adjust: 100%;
    overscroll-behavior-y: none;
  }
  #app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    max-width: 480px;
    margin: 0 auto;
    background: #fff;
    box-shadow: 0 0 24px rgba(0,0,0,0.08);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
    overflow: hidden;
  }
  header {
    background: var(--purple);
    color: #fff;
    padding: 12px 16px;
    padding-top: calc(12px + env(safe-area-inset-top));
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    flex-shrink: 0;
  }
  header .pill-btn {
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    background: rgba(255,255,255,0.18);
    border: 1px solid rgba(255,255,255,0.35);
    color: #fff;
    padding: 7px 14px;
    border-radius: 999px;
  }
  header .pill-btn:active {
    background: rgba(255,255,255,0.3);
  }
  .messages-wrap {
    position: relative;
    flex: 1;
    min-height: 0;
    display: flex;
  }
  #messages {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 14px 12px;
    display: flex;
    flex-direction: column;
    gap: 4px;
  }
  .new-message-btn {
    position: absolute;
    left: 50%;
    bottom: 14px;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    gap: 6px;
    background: var(--purple);
    color: #fff;
    border: none;
    padding: 8px 16px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 4px 14px rgba(43,25,90,0.3);
    z-index: 5;
  }
  .new-message-btn.hidden { display: none; }
  .date-separator {
    text-align: center;
    margin: 12px 0 8px;
  }
  .date-separator span {
    background: #ececf2;
    color: var(--gray);
    font-size: 11px;
    padding: 3px 11px;
    border-radius: 12px;
  }
  .bubble-row {
    display: flex;
    margin: 2px 0;
  }
  .bubble-row.left { justify-content: flex-start; }
  .bubble-row.right { justify-content: flex-end; }
  .bubble {
    max-width: 78%;
    padding: 7px 11px;
    border-radius: 15px;
    font-size: 13.5px;
    line-height: 1.4;
    word-wrap: break-word;
    overflow-wrap: break-word;
    word-break: break-word;
    white-space: pre-wrap;
  }
  .bubble a {
    color: inherit;
    text-decoration: underline;
  }
  .bubble-row.left .bubble {
    background: var(--purple);
    color: var(--text-light);
    border-bottom-left-radius: 4px;
  }
  .bubble-row.right .bubble {
    background: #ffffff;
    color: var(--text-dark);
    border: 1px solid #e2e0ea;
    border-bottom-right-radius: 4px;
  }
  .meta {
    display: block;
    font-size: 9.5px;
    margin-top: 3px;
    opacity: 0.7;
  }
  .bubble-row.left .meta { color: #ece8ff; }
  .bubble-row.right .meta { color: var(--gray); }
  #empty-state {
    text-align: center;
    color: var(--gray);
    font-size: 12px;
    margin-top: 40px;
  }
  form#composer {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 8px 12px;
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
    border-top: 1px solid #ececf2;
    background: #fff;
    flex-shrink: 0;
  }
  #text-input {
    flex: 1;
    min-width: 0;
    border: 1px solid #ddd9ea;
    border-radius: 18px;
    padding: 9px 14px;
    font-size: 16px;
    font-family: inherit;
    line-height: 1.35;
    outline: none;
    min-height: 40px;
    max-height: 120px;
    resize: none;
    overflow-y: auto;
  }
  #text-input:focus {
    border-color: var(--purple);
  }
  #send-btn {
    background: var(--purple);
    color: #fff;
    border: none;
    border-radius: 18px;
    padding: 0 16px;
    font-size: 13.5px;
    font-weight: 600;
    cursor: pointer;
    min-height: 40px;
    flex-shrink: 0;
  }
  #send-btn:active { background: var(--purple-dark); }
  #send-btn:disabled { opacity: 0.5; cursor: default; }

  .modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(20,15,35,0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 50;
    padding: 20px;
    padding-top: calc(20px + env(safe-area-inset-top));
    padding-bottom: calc(20px + env(safe-area-inset-bottom));
  }
  .modal-overlay.hidden { display: none; }
  .modal-box {
    background: #fff;
    border-radius: 14px;
    padding: 18px;
    width: 100%;
    max-width: 300px;
    box-shadow: 0 12px 32px rgba(0,0,0,0.18);
  }
  .modal-box h2 {
    margin: 0 0 5px;
    font-size: 14px;
    color: var(--text-dark);
  }
  .modal-box p {
    margin: 0 0 12px;
    font-size: 12px;
    color: var(--gray);
  }
  .modal-box input {
    width: 100%;
    padding: 9px 12px;
    border: 1px solid #ddd9ea;
    border-radius: 8px;
    font-size: 16px;
    outline: none;
    margin-bottom: 10px;
    letter-spacing: 1px;
    min-height: 40px;
  }
  .modal-box input:focus { border-color: var(--purple); }
  .modal-actions {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
  }
  .modal-actions button {
    padding: 8px 14px;
    border-radius: 8px;
    border: none;
    font-size: 13px;
    cursor: pointer;
    min-height: 36px;
  }
  .btn-cancel {
    background: #ececf2;
    color: var(--text-dark);
  }
  .btn-confirm {
    background: var(--purple);
    color: #fff;
    font-weight: 600;
  }
  .btn-danger {
    background: #d63b3b;
    color: #fff;
    font-weight: 600;
  }
  .modal-error {
    color: #d63b3b;
    font-size: 11px;
    margin: -4px 0 10px;
    display: none;
  }

  .identity-options {
    display: flex;
    flex-direction: column;
    gap: 8px;
  }
  .identity-option {
    display: flex;
    align-items: center;
    gap: 9px;
    width: 100%;
    padding: 10px 12px;
    border: 1px solid #ddd9ea;
    border-radius: 10px;
    background: #fff;
    font-size: 13.5px;
    font-weight: 600;
    color: var(--text-dark);
    cursor: pointer;
    min-height: 40px;
    text-align: left;
  }
  .identity-option:active { background: #f6f4fb; }
  .identity-option.active {
    border-color: var(--purple);
    background: #f6f4fb;
  }
  .identity-dot {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    flex-shrink: 0;
    border: 1px solid #ddd9ea;
  }
  .identity-dot.purple { background: var(--purple); border-color: var(--purple); }
  .identity-dot.white { background: #ffffff; }
  .link-btn {
    display: block;
    margin: 14px auto 0;
    padding: 4px 8px;
    background: none;
    border: none;
    font-size: 11px;
    color: var(--gray);
    text-decoration: underline;
    cursor: pointer;
  }

  #app.locked { display: none; }
  .login-screen {
    position: fixed;
    inset: 0;
    background: var(--bg);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    z-index: 100;
  }
  .login-screen.hidden { display: none; }
  .login-screen .modal-box {
    max-width: 320px;
    padding: 22px 20px;
  }
  .login-screen .modal-box h2 { font-size: 16px; }
  .login-screen .modal-actions { justify-content: stretch; }
  .login-screen .modal-actions button { width: 100%; }
  .send-error {
    color: #d63b3b;
    font-size: 12px;
    padding: 6px 14px 0;
    background: #fff;
    flex-shrink: 0;
  }

  @media (max-width: 380px) {
    .bubble { max-width: 85%; }
  }
