* {
    box-sizing: border-box;
}

body {
    margin: 0;
    min-height: 100vh;
    padding: 24px;
    display: grid;
    place-items: center;
    font-family: Arial, sans-serif;
    color: #202938;
    background: #f4f6f8;
}

main {
    width: 100%;
    max-width: 460px;
    padding: 32px;
    background: #fff;
    border: 1px solid #dde3ea;
    border-radius: 16px;
}

h1 {
    margin: 0 0 10px;
    font-size: 32px;
}

p {
    line-height: 1.5;
}

form {
    display: grid;
    gap: 16px;
    margin-top: 24px;
}

label {
    display: grid;
    gap: 7px;
    font-weight: 600;
}

input[type="email"],
input[type="password"],
input[type="text"] {
    width: 100%;
    min-height: 44px;
    padding: 10px 12px;
    border: 1px solid #bcc7d3;
    border-radius: 8px;
    font: inherit;
    color: inherit;
    background: #fff;
}

input:focus-visible,
button:focus-visible,
a:focus-visible {
    outline: 3px solid #182b49;
    outline-offset: 3px;
}

.consent {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    font-weight: 400;
}

.consent input {
    margin-top: 3px;
}

button,
.button {
    min-height: 44px;
    padding: 11px 18px;
    border: 0;
    border-radius: 8px;
    font: inherit;
    font-weight: 600;
    text-decoration: none;
    text-align: center;
    color: #fff;
    background: #2456a6;
    cursor: pointer;
}

button:disabled {
    opacity: .65;
    cursor: wait;
}

.secondary {
    color: #2456a6;
    background: transparent;
    border: 1px solid #2456a6;
}

.actions {
    display: grid;
    gap: 10px;
    margin-top: 24px;
}

.status {
    min-height: 1.5em;
    margin: 16px 0 0;
}

.error {
    color: #a12222;
}

.success {
    color: #176b39;
}

.muted {
    color: #667085;
}

.chat-placeholder {
    margin-top: 24px;
    padding: 20px;
    border: 1px dashed #bcc7d3;
    border-radius: 12px;
    background: #fafbfc;
}

/* Minimal Chat MVP */

.chat-page {
    width: min(900px, calc(100% - 32px));
    margin: 0 auto;
}

.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
}

.messages {
    min-height: 420px;
    max-height: 60vh;
    overflow-y: auto;
    margin: 24px 0 16px;
    padding: 20px;
    border: 1px solid #e5e7eb;
    border-radius: 16px;
    background: #ffffff;
}

.message {
    width: fit-content;
    max-width: 80%;
    margin: 10px 0;
    padding: 12px 16px;
    border-radius: 16px;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.message.user {
    margin-left: auto;
    background: #111827;
    color: #ffffff;
}

.message.assistant {
    margin-right: auto;
    background: #f3f4f6;
    color: #111827;
}

.chat-form {
    display: flex;
    align-items: flex-end;
    gap: 12px;
}

.chat-form textarea {
    flex: 1;
    resize: vertical;
    min-height: 52px;
    max-height: 180px;
}

/* Sarafan CAPTCHA */
.captcha-block {
  margin: 18px 0;
}

.captcha-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 8px 0 10px;
}

.captcha-image {
  display: block;
  max-width: calc(100% - 54px);
  height: 82px;
  border: 1px solid #d6dce5;
  border-radius: 10px;
  background: #f8fafc;
}

.captcha-refresh {
  width: 44px;
  min-width: 44px;
  height: 44px;
  padding: 0;
  font-size: 23px;
  line-height: 1;
  border-radius: 9px;
}

.captcha-input {
  text-transform: uppercase;
  letter-spacing: .12em;
}

/* ---------------------------------------------------------
   Sarafan action and legal links
--------------------------------------------------------- */

.actions {
  margin-top: 22px;
  text-align: center;
}

.actions a {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 42px;
  padding: 9px 16px;
  border: 1px solid #d8dee8;
  border-radius: 10px;
  background: #ffffff;
  color: #1d4ed8;
  font-weight: 600;
  text-decoration: none;
  transition:
    border-color .15s ease,
    background-color .15s ease,
    transform .15s ease;
}

.actions a:hover {
  border-color: #aebbd0;
  background: #f8fafc;
}

.actions a:active {
  transform: translateY(1px);
}

.consent a {
  color: #1d4ed8;
  font-weight: 500;
  text-decoration: none;
  border-bottom: 1px solid rgba(29, 78, 216, .28);
}

.consent a:hover {
  border-bottom-color: currentColor;
}

main > h1 {
  margin-bottom: 6px;
}

/* ---------------------------------------------------------
   Registration success
--------------------------------------------------------- */

.registration-success {
  margin-top: 18px;
  padding: 28px 22px;
  border: 1px solid #d8e2ef;
  border-radius: 14px;
  background: #ffffff;
  text-align: center;
}

.registration-success[hidden] {
  display: none;
}

.registration-success-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 54px;
  height: 54px;
  margin: 0 auto 16px;
  border-radius: 50%;
  background: #ecfdf3;
  color: #15803d;
  font-size: 30px;
  font-weight: 700;
}

.registration-success h2 {
  margin: 0 0 14px;
  font-size: 24px;
}

.registration-success p {
  margin: 10px 0;
  line-height: 1.55;
}

.registration-success strong {
  overflow-wrap: anywhere;
}

.registration-success-actions {
  display: grid;
  gap: 10px;
  margin-top: 24px;
}

.primary-link-button,
.secondary-link-button {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 46px;
  padding: 10px 18px;
  border-radius: 10px;
  font-weight: 600;
  text-decoration: none;
}

.primary-link-button {
  border: 1px solid #2f63b7;
  background: #2f63b7;
  color: #ffffff;
}

.primary-link-button:hover {
  background: #2858a6;
}

.secondary-link-button {
  border: 1px solid #d5dce7;
  background: #ffffff;
  color: #1d4ed8;
}

.secondary-link-button:hover {
  background: #f8fafc;
}

/* Registration state switching */
#registration-stage[hidden],
#registration-success[hidden] {
  display: none !important;
}


/* Sarafan optional response speech */
.speech-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  cursor: pointer;
  user-select: none;
}

.speech-toggle input {
  width: 18px;
  height: 18px;
  margin: 0;
  cursor: pointer;
}

/* Conversation navigation */

/* =========================================================
   Sarafan chat shell — desktop + mobile drawer
   ========================================================= */

.chat-layout {
    width: min(1240px, calc(100% - 32px));
    margin: 0 auto;
    display: grid;
    grid-template-columns: 260px minmax(0, 1fr);
    gap: 24px;
    align-items: start;
}

.chat-layout .chat-page {
    width: 100%;
    min-width: 0;
    margin: 0;
}

.conversation-sidebar {
    position: sticky;
    top: 16px;
    align-self: start;
    max-height: calc(100vh - 32px);
    padding: 16px;
    overflow: hidden;
    border: 1px solid #e5e7eb;
    border-radius: 16px;
    background: #ffffff;
}

.conversation-sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 14px;
}

.conversation-sidebar-header strong {
    font-size: 18px;
}

.conversation-close,
.conversation-menu {
    display: none;
}

.conversation-new {
    width: 100%;
    margin: 0 0 12px;
}

.conversation-list {
    display: flex;
    flex-direction: column;
    gap: 6px;
    max-height: calc(100vh - 150px);
    overflow-y: auto;
    overscroll-behavior: contain;
}

.conversation-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 38px;
    align-items: center;
    border-radius: 10px;
}

.conversation-row.active {
    background: #f3f4f6;
}

.conversation-open,
.conversation-delete {
    border: 0;
    background: transparent;
    color: #111827;
}

.conversation-open {
    min-width: 0;
    padding: 11px 10px;
    text-align: left;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.conversation-delete {
    padding: 8px;
    font-size: 20px;
    line-height: 1;
}

.conversation-open:hover,
.conversation-delete:hover {
    background: #e5e7eb;
}

.chat-header-main {
    display: flex;
    align-items: center;
    min-width: 0;
    gap: 12px;
}

.chat-header-main h1 {
    margin-bottom: 4px;
}

.conversation-overlay {
    display: none;
}

@media (max-width: 1000px) and (min-width: 761px) {
    .chat-layout {
        grid-template-columns: 220px minmax(0, 1fr);
        gap: 16px;
    }

    .conversation-sidebar {
        padding: 12px;
    }
}

@media (max-width: 760px) {
    .chat-layout {
        display: block;
        width: min(100% - 24px, 900px);
    }

    .chat-page {
        width: 100%;
    }

    .chat-header {
        gap: 10px;
    }

    .chat-header-main {
        flex: 1;
        overflow: hidden;
    }

    .chat-header-main > div {
        min-width: 0;
    }

    .chat-header-main h1 {
        font-size: 24px;
    }

    .chat-header-main .muted {
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
    }

    .conversation-menu {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        flex: 0 0 44px;
        width: 44px;
        height: 44px;
        padding: 0;
        font-size: 23px;
        line-height: 1;
    }

    .conversation-sidebar {
        position: fixed;
        z-index: 1001;
        top: 0;
        left: 0;
        width: min(86vw, 330px);
        height: 100dvh;
        max-height: none;
        padding: 18px 14px;
        border: 0;
        border-right: 1px solid #e5e7eb;
        border-radius: 0;
        overflow: hidden;
        transform: translateX(-105%);
        transition: transform 180ms ease;
    }

    .conversation-sidebar.open {
        transform: translateX(0);
    }

    .conversation-close {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 40px;
        height: 40px;
        padding: 0;
        border: 0;
        background: transparent;
        color: #111827;
        font-size: 28px;
        line-height: 1;
    }

    .conversation-list {
        max-height: calc(100dvh - 135px);
    }

    .conversation-overlay {
        position: fixed;
        z-index: 1000;
        inset: 0;
        display: block;
        width: 100%;
        height: 100%;
        padding: 0;
        border: 0;
        border-radius: 0;
        background: rgba(17, 24, 39, .42);
        opacity: 0;
        visibility: hidden;
        pointer-events: none;
        transition:
            opacity 180ms ease,
            visibility 180ms ease;
    }

    .conversation-overlay.open {
        opacity: 1;
        visibility: visible;
        pointer-events: auto;
    }

    body.conversation-drawer-open {
        overflow: hidden;
    }

    .messages {
        min-height: 55dvh;
        max-height: 62dvh;
        margin-top: 16px;
        padding: 12px;
    }

    .message {
        max-width: 90%;
    }

    .chat-form {
        gap: 8px;
    }

    .chat-form textarea {
        min-width: 0;
    }
}

@media (max-width: 480px) {
    .chat-layout {
        width: calc(100% - 16px);
    }

    .chat-header {
        padding-top: 8px;
    }

    .chat-header #logout {
        padding-left: 10px;
        padding-right: 10px;
    }

    .messages {
        min-height: 52dvh;
        padding: 10px;
        border-radius: 12px;
    }

    .message {
        max-width: 94%;
        padding: 10px 12px;
    }

    .chat-form {
        display: grid;
        grid-template-columns:
            minmax(0, 1fr) auto auto;
    }

    .chat-form textarea {
        resize: none;
    }
}

/* Desktop chat width correction */

@media (min-width: 761px) {
    body:has(.chat-layout) .container {
        width: min(1400px, calc(100% - 40px));
        max-width: none;
    }

    .chat-layout {
        width: 100%;
        max-width: none;
        grid-template-columns: 260px minmax(500px, 1fr);
        gap: 24px;
    }

    .chat-layout .chat-page {
        width: 100%;
        max-width: none;
    }

    .messages {
        width: 100%;
        box-sizing: border-box;
    }

    .message {
        max-width: min(80%, 760px);
    }
}

/* Chat composer readability */

.chat-form textarea {
    font-size: 16px;
    line-height: 1.5;
    padding: 12px 14px;
}

/* Chat composer typography */

.chat-form textarea {
    font-size: 18px;
    line-height: 1.5;
    font-family: inherit;
    font-weight: 400;
}

@media (max-width: 760px) {
    .chat-form textarea {
        font-size: 16px;
    }
}


/* Continue truncated assistant response */

.continue-response-button {
    display: block;
    width: fit-content;
    margin: -2px 0 14px 0;
    padding: 8px 14px;
    border: 1px solid #d1d5db;
    border-radius: 10px;
    background: #ffffff;
    color: #111827;
    font: inherit;
    font-weight: 600;
    cursor: pointer;
}

.continue-response-button:hover {
    background: #f3f4f6;
}

.continue-response-button:disabled {
    opacity: 0.55;
    cursor: default;
}


/* Assistant message actions */

.message-actions {
    display: flex;
    align-items: center;
    gap: 4px;
    width: fit-content;
    margin: -5px 0 10px 6px;
}

.message-action {
    appearance: none;
    border: 0;
    border-radius: 6px;
    background: transparent;
    padding: 4px 7px;
    color: #6b7280;
    font: inherit;
    font-size: 13px;
    line-height: 1.2;
    cursor: pointer;
}

.message-action:hover {
    background: #f3f4f6;
    color: #111827;
}

.message-action:focus-visible {
    outline: 2px solid #9ca3af;
    outline-offset: 1px;
}

/* =========================================================
   Sarafan mobile chat — full-width optimized layout
   ========================================================= */

@media (max-width: 760px) {

    body:has(.chat-layout) {
        margin: 0;
        padding: 0;
        overflow-x: hidden;
    }

    body:has(.chat-layout) .container {
        width: 100%;
        max-width: none;
        margin: 0;
        padding: 0;
    }

    .chat-layout {
        display: block;
        width: 100%;
        max-width: none;
        margin: 0;
        padding: 0;
    }

    .chat-layout .chat-page {
        width: 100%;
        max-width: none;
        margin: 0;
        padding: 0;
    }

    /* Compact mobile header */

    .chat-header {
        width: 100%;
        min-height: 58px;
        box-sizing: border-box;
        margin: 0;
        padding: 8px 12px;
        gap: 8px;
        border-bottom: 1px solid #e5e7eb;
        background: #ffffff;
    }

    .chat-header-main {
        min-width: 0;
        flex: 1;
        gap: 10px;
    }

    .chat-header-main h1 {
        margin: 0;
        font-size: 22px;
        line-height: 1.15;
    }

    .chat-header-main .muted {
        margin: 2px 0 0;
        max-width: 42vw;
        font-size: 13px;
        line-height: 1.2;
    }

    .conversation-menu {
        flex: 0 0 42px;
        width: 42px;
        height: 42px;
    }

    .chat-header #logout {
        flex: 0 0 auto;
        padding: 9px 11px;
        font-size: 14px;
    }

    /* Messages use the available screen */

    .messages {
        width: 100%;
        box-sizing: border-box;
        min-height: 0;
        height: calc(100dvh - 142px);
        max-height: none;
        margin: 0;
        padding: 12px 10px 18px;
        border: 0;
        border-radius: 0;
        overflow-y: auto;
        background: #ffffff;
        overscroll-behavior: contain;
    }

    .message {
        max-width: 92%;
        box-sizing: border-box;
        margin-top: 7px;
        margin-bottom: 7px;
        padding: 10px 12px;
    }

    .message-actions {
        margin-bottom: 8px;
    }

    /* Composer always remains one usable row */

    .chat-form {
        display: grid;
        grid-template-columns:
            minmax(0, 1fr) 46px auto;
        align-items: end;
        width: 100%;
        box-sizing: border-box;
        gap: 7px;
        margin: 0;
        padding: 8px 10px;
        border-top: 1px solid #e5e7eb;
        background: #ffffff;
    }

    .chat-form textarea {
        width: 100%;
        min-width: 0;
        min-height: 46px;
        height: 46px;
        max-height: 120px;
        box-sizing: border-box;
        margin: 0;
        padding: 10px 12px;
        resize: none;
        font-size: 16px;
        line-height: 24px;
    }

    .chat-form #microphone {
        width: 46px;
        min-width: 46px;
        height: 46px;
        padding: 0;
    }

    .chat-form #send {
        width: auto;
        min-width: 0;
        height: 46px;
        padding: 0 14px;
        white-space: nowrap;
    }

    .chat-page > .status {
        box-sizing: border-box;
        margin: 0;
        padding: 0 10px;
    }
}

@media (max-width: 400px) {

    .messages {
        padding-left: 8px;
        padding-right: 8px;
    }

    .message {
        max-width: 95%;
    }

    .chat-form {
        grid-template-columns:
            minmax(0, 1fr) 44px 48px;
        gap: 6px;
        padding-left: 8px;
        padding-right: 8px;
    }

    .chat-form #microphone {
        width: 44px;
        min-width: 44px;
        height: 46px;
    }

    .chat-form #send {
        width: 48px;
        padding: 0;
        overflow: hidden;
        font-size: 0;
    }

    .chat-form #send::after {
        content: "➜";
        font-size: 22px;
        line-height: 1;
    }

    .chat-header #logout {
        padding-left: 8px;
        padding-right: 8px;
    }
}
