* {
    box-sizing: border-box;
}

body {
    margin: 0;
    min-height: 100vh;
    font-family: "Trebuchet MS", "Segoe UI", sans-serif;
    background: radial-gradient(circle at top left, #f5ead7 0, #f3f7ff 34%, #edf0f8 100%);
    color: #152033;
}

body:before {
    content: "";
    position: fixed;
    inset: -20%;
    background: linear-gradient(135deg, rgba(19, 37, 78, .08), rgba(255, 255, 255, 0));
    pointer-events: none;
    transform: rotate(-4deg);
}

.shell {
    position: relative;
    max-width: 920px;
    margin: 0 auto;
    padding: 32px max(20px, env(safe-area-inset-right)) calc(40px + env(safe-area-inset-bottom)) max(20px, env(safe-area-inset-left));
}

.topbar {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 18px;
}

.titleblock h2 {
    margin: 0;
    font-family: Georgia, "Times New Roman", serif;
    font-size: clamp(2rem, 4vw, 3.25rem);
    letter-spacing: -.04em;
}

.brand-mark {
    display: block;
    height: 58px;
    width: auto;
    object-fit: contain;
    border-radius: 8px;
    filter: drop-shadow(0 6px 14px rgba(21, 32, 51, .2));
}

.titleblock p {
    margin: 8px 0 0;
    color: #5b6475;
    max-width: 42rem;
    line-height: 1.5;
}

#profile {
    display: flex;
    align-items: center;
    gap: 10px;
    background: rgba(255, 255, 255, .72);
    backdrop-filter: blur(14px);
    border: 1px solid rgba(21, 32, 51, .1);
    border-radius: 999px;
    padding: 10px 14px;
    box-shadow: 0 12px 30px rgba(21, 32, 51, .08);
    flex-wrap: wrap;
}

#profile label {
    font-size: .78rem;
    font-weight: 700;
    letter-spacing: .08em;
    text-transform: uppercase;
    color: #6a7281;
}

#name {
    width: 170px;
    border: none;
    background: transparent;
    font: inherit;
    font-size: 16px;
    color: #152033;
    outline: none;
    min-height: 40px;
}

.panel {
    position: relative;
    background: rgba(255, 255, 255, .76);
    backdrop-filter: blur(16px);
    border: 1px solid rgba(21, 32, 51, .12);
    border-radius: 28px;
    box-shadow: 0 24px 70px rgba(21, 32, 51, .14);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    min-height: 480px;
    height: min(70dvh, 680px);
}

#chat {
    flex: 1;
    overflow: auto;
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    -webkit-overflow-scrolling: touch;
}

.message {
    max-width: 78%;
    padding: 14px 16px;
    border-radius: 18px;
    line-height: 1.45;
    box-shadow: 0 8px 20px rgba(21, 32, 51, .06);
    word-break: break-word;
}

.message.peer {
    align-self: flex-start;
    background: #ffffff;
    border: 1px solid rgba(21, 32, 51, .08);
    border-top-left-radius: 6px;
}

.message.self {
    align-self: flex-end;
    background: linear-gradient(135deg, #16324f, #345c87);
    color: #fff;
    border-top-right-radius: 6px;
}

.message-head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 6px;
    font-size: .8rem;
    font-weight: 700;
    letter-spacing: .02em;
}

.message-self .message-head {
    color: rgba(255, 255, 255, .82);
}

.message-peer .message-head {
    color: #667085;
}

.message-text {
    font-size: 1rem;
    white-space: pre-wrap;
}

.message-image {
    white-space: normal;
}

.message-image-preview {
    display: block;
    width: min(280px, 100%);
    max-height: 280px;
    object-fit: cover;
    border-radius: 12px;
}

.image-picker {
    margin: 10px 18px 0;
    border: 1px solid rgba(21, 32, 51, .12);
    background: rgba(255, 255, 255, .82);
    border-radius: 16px;
    padding: 10px;
}

.image-picker-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-bottom: 8px;
}

.picker-close {
    min-height: 34px;
    min-width: 34px;
    width: 34px;
    border-radius: 10px;
    font-size: .95rem;
    padding: 0;
}

.image-picker-status {
    font-size: .82rem;
    font-weight: 700;
    margin-bottom: 8px;
}

.image-picker-status.ok {
    color: #1c5b39;
}

.image-picker-status.warn {
    color: #7b4a08;
}

.image-picker-status.error {
    color: #7a1722;
}

.image-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    gap: 8px;
    max-height: 210px;
    overflow: auto;
}

.image-option {
    min-height: auto;
    width: 100%;
    padding: 6px;
    border-radius: 10px;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
}

.image-option img {
    width: 100%;
    height: 82px;
    object-fit: cover;
    border-radius: 8px;
    background: #d5dde9;
}

.image-option span {
    color: #fff;
    font-size: .72rem;
    line-height: 1.25;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
}

.message-time {
    font-size: .74rem;
    font-weight: 600;
    opacity: .9;
    white-space: nowrap;
}

.message-meta {
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

.message-state {
    font-size: .72rem;
    font-weight: 700;
    letter-spacing: .01em;
    opacity: .95;
}

.message-state.sending {
    color: #f9dda2;
}

.message-state.queued {
    color: #f8cf82;
}

.message-state.sent {
    color: #b7efca;
}

.message-state.failed {
    color: #ffb9c0;
}

.day-divider {
    display: flex;
    justify-content: center;
    margin: 2px 0 4px;
}

.day-chip {
    padding: 5px 10px;
    border-radius: 999px;
    font-size: .72rem;
    font-weight: 700;
    letter-spacing: .01em;
    background: rgba(21, 32, 51, .08);
    color: #4f5c73;
}

.status {
    margin: 0 18px;
    padding: 10px 12px;
    border-radius: 12px;
    font-size: .86rem;
    font-weight: 700;
    letter-spacing: .01em;
}

.status.ok {
    background: rgba(32, 125, 73, .1);
    color: #1c5b39;
}

.status.warn {
    background: rgba(158, 99, 30, .14);
    color: #7b4a08;
}

.status.error {
    background: rgba(161, 45, 55, .14);
    color: #7a1722;
}

.composer {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 18px 18px 20px;
    border-top: 1px solid rgba(21, 32, 51, .08);
    background: linear-gradient(180deg, rgba(255, 255, 255, .35), rgba(255, 255, 255, .75));
}

.composer-actions {
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

.icon-btn {
    min-height: 44px;
    min-width: 44px;
    width: 44px;
    border-radius: 12px;
    padding: 0;
    font-size: 1.1rem;
}

#msg {
    flex: 1;
    border: none;
    border-radius: 16px;
    padding: 15px 16px;
    font: inherit;
    font-size: 16px;
    background: #fff;
    box-shadow: inset 0 0 0 1px rgba(21, 32, 51, .12);
    outline: none;
    min-height: 48px;
}

#msg:focus,
#name:focus {
    box-shadow: inset 0 0 0 1px rgba(52, 92, 135, .55), 0 0 0 4px rgba(52, 92, 135, .12);
}

button {
    border: none;
    border-radius: 16px;
    padding: 0 22px;
    min-height: 48px;
    font: 700 1rem/1 "Trebuchet MS", "Segoe UI", sans-serif;
    color: #fff;
    background: linear-gradient(135deg, #2f5e8c, #16324f);
    box-shadow: 0 12px 24px rgba(22, 50, 79, .24);
    cursor: pointer;
    transition: transform .15s ease, box-shadow .15s ease;
    touch-action: manipulation;
}

button:hover {
    transform: translateY(-1px);
    box-shadow: 0 16px 28px rgba(22, 50, 79, .28);
}

button:active {
    transform: translateY(0);
}

.logout {
    padding: 10px 14px;
    border-radius: 999px;
    font-size: .9rem;
    background: linear-gradient(135deg, #8d4250, #5e2130);
    box-shadow: 0 10px 20px rgba(94, 33, 48, .18);
}

.json {
    padding: 10px 14px;
    border-radius: 999px;
    font-size: .9rem;
    background: linear-gradient(135deg, #3b6ca3, #1f4166);
    box-shadow: 0 10px 20px rgba(31, 65, 102, .2);
}

.gate {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: max(24px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(24px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
    background: radial-gradient(circle at top, #f9f2e5 0, #e8eef8 48%, #dbe2ef 100%);
    z-index: 10;
}

.gate-card {
    width: min(420px, 100%);
    padding: 28px;
    background: rgba(255, 255, 255, .72);
    backdrop-filter: blur(16px);
    border: 1px solid rgba(21, 32, 51, .12);
    border-radius: 30px;
    box-shadow: 0 24px 70px rgba(21, 32, 51, .16);
    max-height: 100%;
    overflow: auto;
}

.gate-card h3 {
    margin: 0;
    font-family: Georgia, "Times New Roman", serif;
    font-size: 2rem;
}

.gate-brand {
    display: block;
    height: 56px;
    width: auto;
    max-width: 100%;
    margin: 0 auto;
    object-fit: contain;
    border-radius: 8px;
    filter: drop-shadow(0 6px 14px rgba(21, 32, 51, .2));
}

.gate-card p {
    margin: 10px 0 20px;
    color: #5b6475;
    line-height: 1.5;
}

.pin-display {
    display: flex;
    gap: 10px;
    justify-content: center;
    margin: 0 0 20px;
}

.pin-dot {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    border: 2px solid rgba(21, 32, 51, .22);
    background: transparent;
}

.pin-dot.filled {
    background: #16324f;
    border-color: #16324f;
}

.keypad {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 12px;
}

.key {
    aspect-ratio: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
    border-radius: 22px;
    background: linear-gradient(180deg, #ffffff, #edf2fb);
    color: #152033;
    box-shadow: 0 8px 18px rgba(21, 32, 51, .10), inset 0 0 0 1px rgba(21, 32, 51, .06);
    min-height: 56px;
}

.key.empty {
    background: transparent;
    box-shadow: none;
    pointer-events: none;
}

.key.primary {
    background: linear-gradient(135deg, #2f5e8c, #16324f);
    color: #fff;
}

.error {
    min-height: 1.2em;
    margin: 14px 0 0;
    text-align: center;
    font-size: .95rem;
    color: #9b2c2c;
}

.hidden {
    display: none !important;
}

@media (hover: none) {
    button:hover {
        transform: none;
        box-shadow: 0 12px 24px rgba(22, 50, 79, .24);
    }
}

@media (max-width: 640px) {
    .shell {
        padding: max(16px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) calc(20px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
    }

    .topbar {
        flex-direction: column;
    }

    #profile {
        width: 100%;
        justify-content: space-between;
        border-radius: 20px;
        padding: 10px 12px;
    }

    #name {
        width: 100%;
        min-width: 0;
    }

    .panel {
        border-radius: 20px;
        min-height: 0;
        height: calc(100dvh - 230px);
    }

    #chat {
        padding: 14px;
        gap: 10px;
    }

    .message {
        max-width: 92%;
        padding: 12px 14px;
    }

    .status {
        margin: 0 12px;
    }

    .image-picker {
        margin: 10px 12px 0;
    }

    .composer {
        position: sticky;
        bottom: 0;
        flex-direction: column;
        gap: 10px;
        padding: 12px;
        padding-bottom: calc(12px + env(safe-area-inset-bottom));
    }

    button {
        width: 100%;
        padding: 15px 18px;
    }

    .composer .composer-actions {
        width: 100%;
    }

    .composer .icon-btn {
        width: 44px;
        min-width: 44px;
        padding: 0;
    }

    .logout {
        width: auto;
        min-height: 42px;
        padding: 10px 14px;
    }

    .json {
        width: auto;
        min-height: 42px;
        padding: 10px 14px;
    }

    .key {
        min-height: 64px;
        font-size: 1.35rem;
    }
}

@media (max-width: 420px) {
    .titleblock h2 {
        font-size: 2.1rem;
    }

    .brand-mark {
        height: 46px;
    }

    .titleblock p {
        font-size: .92rem;
    }

    .message-head {
        font-size: .74rem;
    }

    .message-time {
        font-size: .7rem;
    }

    .gate-card {
        padding: 20px;
        border-radius: 24px;
    }

    .gate-brand {
        height: 44px;
    }
}