/* ==================== iOS white minimal theme ==================== */
:root {
    --primary-color: #007AFF;
    --accent-color: #34C759;
    --danger-color: #FF3B30;
    --warning-color: #FF9500;
    --bg-primary: #F5F5F7;
    --bg-secondary: #FFFFFF;
    --bg-tertiary: #F2F2F7;
    --bg-hover: #F7F8FA;
    --bg-selected: #EAF2FF;
    --text-primary: #1D1D1F;
    --text-secondary: #515154;
    --text-tertiary: #8E8E93;
    --border-color: #E5E5EA;
    --bubble-incoming: #F2F2F7;
    --bubble-outgoing: #007AFF;
    --bubble-outgoing-text: #FFFFFF;
    --shadow-soft: 0 10px 30px rgba(0, 0, 0, 0.08);
    --shadow-subtle: 0 1px 2px rgba(0, 0, 0, 0.05);
}

/* ==================== Base ==================== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html,
body {
    height: 100%;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display", "Helvetica Neue", Arial, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    overflow: hidden;
    -webkit-font-smoothing: antialiased;
}

.hidden {
    display: none !important;
}

.page {
    display: none;
    height: 100vh;
    height: 100dvh;
}

.page.active {
    display: flex;
}

/* ==================== Login ==================== */
#login-page {
    align-items: center;
    justify-content: center;
    padding: max(24px, env(safe-area-inset-top)) 20px max(24px, env(safe-area-inset-bottom));
    background: var(--bg-primary);
}

.login-container {
    width: min(360px, 100%);
    padding: 34px 28px 28px;
    text-align: center;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    box-shadow: var(--shadow-soft);
}

.login-icon img {
    width: 72px;
    height: 72px;
    margin-bottom: 22px;
    border-radius: 8px;
    box-shadow: var(--shadow-subtle);
}

.login-container h1 {
    margin-bottom: 8px;
    font-size: 26px;
    font-weight: 700;
    letter-spacing: 0;
}

.subtitle {
    margin-bottom: 28px;
    color: var(--text-tertiary);
    font-size: 14px;
}

#login-form {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

#login-form input,
.modal input,
.modal textarea {
    width: 100%;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    font: inherit;
    font-size: 16px;
    outline: none;
    transition: border-color 0.18s ease, background 0.18s ease, box-shadow 0.18s ease;
}

#login-form input {
    height: 48px;
    padding: 0 14px;
}

.modal input,
.modal textarea {
    padding: 13px 14px;
    resize: none;
}

#login-form input:focus,
.modal input:focus,
.modal textarea:focus {
    background: var(--bg-secondary);
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.12);
}

#login-form input::placeholder,
.modal input::placeholder,
.modal textarea::placeholder,
#message-input::placeholder {
    color: var(--text-tertiary);
}

.btn-primary,
.btn-secondary,
.btn-danger {
    min-height: 46px;
    border: none;
    border-radius: 8px;
    font: inherit;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.16s ease, opacity 0.16s ease, background 0.16s ease;
}

.btn-primary {
    background: var(--primary-color);
    color: #FFFFFF;
}

.btn-secondary {
    width: 100%;
    background: var(--bg-tertiary);
    color: var(--text-primary);
}

.btn-danger {
    background: var(--danger-color);
    color: #FFFFFF;
}

.btn-primary:active,
.btn-secondary:active,
.btn-danger:active,
.icon-btn:active,
.text-btn:active,
.send-btn:active {
    transform: scale(0.97);
}

button:disabled {
    cursor: default;
    opacity: 0.42;
}

.error-text {
    min-height: 20px;
    margin-top: 10px;
    color: var(--danger-color);
    font-size: 14px;
}

/* ==================== App layout ==================== */
#main-page {
    height: 100vh;
    height: 100dvh;
    padding-top: env(safe-area-inset-top);
    background: var(--bg-primary);
}

.panel {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-width: 0;
}

#conversations-panel {
    width: 390px;
    min-width: 320px;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
}

#messages-panel {
    flex: 1;
    background: var(--bg-secondary);
}

.panel-header {
    position: sticky;
    top: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-height: 62px;
    padding: 10px 16px;
    background: rgba(255, 255, 255, 0.9);
    border-bottom: 1px solid var(--border-color);
    backdrop-filter: blur(18px);
    -webkit-backdrop-filter: blur(18px);
}

.panel-header h1 {
    font-size: 28px;
    font-weight: 700;
    letter-spacing: 0;
}

.panel-header h2 {
    overflow: hidden;
    color: var(--text-primary);
    font-size: 17px;
    font-weight: 650;
    letter-spacing: 0;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.header-actions,
.message-header-left,
.message-header-actions {
    display: flex;
    align-items: center;
    gap: 6px;
}

.message-header-left,
.message-header-actions {
    min-width: 76px;
}

.message-header-actions {
    justify-content: flex-end;
}

.contact-info {
    flex: 1;
    min-width: 0;
    padding: 0 8px;
    text-align: center;
}

.selection-count {
    margin-top: 2px;
    color: var(--text-tertiary);
    font-size: 12px;
}

.icon-btn,
.text-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: none;
    background: transparent;
    color: var(--primary-color);
    cursor: pointer;
    font: inherit;
}

.icon-btn {
    width: 38px;
    height: 38px;
    border-radius: 50%;
}

.icon-btn:hover {
    background: var(--bg-tertiary);
}

.icon-btn.danger {
    color: var(--danger-color);
}

.text-btn {
    min-width: 48px;
    min-height: 36px;
    padding: 0 8px;
    border-radius: 8px;
    font-size: 16px;
    font-weight: 600;
}

.text-btn:hover {
    background: var(--bg-tertiary);
}

/* ==================== Connection status ==================== */
.connection-status {
    display: flex;
    align-items: center;
    gap: 7px;
    padding: 8px 16px;
    color: var(--text-tertiary);
    font-size: 12px;
    border-bottom: 1px solid var(--border-color);
}

.status-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--text-tertiary);
}

.status-dot.connected {
    background: var(--accent-color);
}

.status-dot.connecting {
    background: var(--warning-color);
    animation: pulse 1.4s ease-in-out infinite;
}

@keyframes pulse {
    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0.45;
    }
}

/* ==================== Conversations ==================== */
.list {
    flex: 1;
    overflow-y: auto;
}

.conversation-item {
    display: flex;
    align-items: center;
    min-height: 74px;
    padding: 11px 16px;
    cursor: pointer;
    border-bottom: 1px solid var(--border-color);
    transition: background 0.16s ease;
}

.conversation-item:hover {
    background: var(--bg-hover);
}

.conversation-item.active {
    background: var(--bg-selected);
}

.avatar {
    display: flex;
    flex: 0 0 auto;
    align-items: center;
    justify-content: center;
    width: 48px;
    height: 48px;
    margin-right: 13px;
    border: 1px solid rgba(0, 0, 0, 0.04);
    border-radius: 50%;
    color: var(--text-primary);
    font-size: 17px;
    font-weight: 700;
}

.conversation-info {
    min-width: 0;
    flex: 1;
}

.conversation-header {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 5px;
}

.conversation-name {
    overflow: hidden;
    color: var(--text-primary);
    font-size: 16px;
    font-weight: 650;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.conversation-time {
    flex: 0 0 auto;
    color: var(--text-tertiary);
    font-size: 13px;
}

.conversation-preview {
    overflow: hidden;
    color: var(--text-secondary);
    font-size: 14px;
    line-height: 1.35;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* ==================== Messages ==================== */
.messages {
    display: flex;
    flex: 1;
    flex-direction: column;
    gap: 8px;
    overflow-y: auto;
    padding: 18px 18px 20px;
    background: var(--bg-secondary);
}

.empty-state {
    display: flex;
    flex: 1;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 14px;
    padding: 28px;
    color: var(--text-tertiary);
    text-align: center;
}

.empty-state img {
    width: 64px;
    height: 64px;
    border-radius: 8px;
    opacity: 0.45;
}

.empty-state p {
    font-size: 16px;
}

.message-row {
    display: flex;
    align-items: flex-end;
    width: 100%;
    gap: 10px;
}

.message-row.incoming {
    justify-content: flex-start;
}

.message-row.outgoing {
    justify-content: flex-end;
}

.message-row.selecting {
    cursor: pointer;
}

.message-row.selected .message-bubble {
    box-shadow: 0 0 0 2px rgba(0, 122, 255, 0.16);
}

.message-content {
    display: flex;
    flex-direction: column;
    max-width: 75%;
}

.message-row.outgoing .message-content {
    align-items: flex-end;
}

.message-row.incoming .message-content {
    align-items: flex-start;
}

.message-select-control {
    display: flex;
    flex: 0 0 auto;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    margin-bottom: 18px;
    border: none;
    border-radius: 50%;
    background: transparent;
    cursor: pointer;
}

.message-select-control span {
    position: relative;
    width: 20px;
    height: 20px;
    border: 2px solid #C7C7CC;
    border-radius: 50%;
    background: var(--bg-secondary);
    transition: background 0.16s ease, border-color 0.16s ease;
}

.message-row.selected .message-select-control span {
    border-color: var(--primary-color);
    background: var(--primary-color);
}

.message-row.selected .message-select-control span::after {
    position: absolute;
    top: 2px;
    left: 5px;
    width: 5px;
    height: 9px;
    border: solid #FFFFFF;
    border-width: 0 2px 2px 0;
    content: "";
    transform: rotate(45deg);
}

.message-bubble {
    max-width: 100%;
    padding: 10px 14px;
    border-radius: 18px;
    font-size: 16px;
    line-height: 1.38;
    white-space: pre-wrap;
    word-break: break-word;
    box-shadow: var(--shadow-subtle);
}

.message-bubble.incoming {
    color: var(--text-primary);
    background: var(--bubble-incoming);
    border-bottom-left-radius: 6px;
}

.message-bubble.outgoing {
    color: var(--bubble-outgoing-text);
    background: var(--bubble-outgoing);
    border-bottom-right-radius: 6px;
}

.message-time {
    margin-top: 4px;
    padding: 0 4px;
    color: var(--text-tertiary);
    font-size: 12px;
}

.status-icon {
    margin-left: 4px;
    font-size: 12px;
}

.status-icon.pending {
    color: var(--warning-color);
}

.status-icon.sent {
    color: var(--accent-color);
}

.status-icon.failed {
    color: var(--danger-color);
}

/* ==================== Input ==================== */
.input-area {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 14px;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
    background: rgba(255, 255, 255, 0.94);
    border-top: 1px solid var(--border-color);
    backdrop-filter: blur(18px);
    -webkit-backdrop-filter: blur(18px);
}

#message-input {
    flex: 1;
    min-height: 40px;
    padding: 0 15px;
    border: 1px solid var(--border-color);
    border-radius: 20px;
    outline: none;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    font: inherit;
    font-size: 16px;
    transition: border-color 0.18s ease, background 0.18s ease, box-shadow 0.18s ease;
}

#message-input:focus {
    border-color: var(--primary-color);
    background: var(--bg-secondary);
    box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.1);
}

.send-btn {
    display: flex;
    flex: 0 0 auto;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
    border: none;
    border-radius: 50%;
    background: var(--primary-color);
    color: #FFFFFF;
    cursor: pointer;
    transition: transform 0.16s ease, opacity 0.16s ease;
}

/* ==================== Modal ==================== */
.modal {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 20px;
    background: rgba(0, 0, 0, 0.22);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}

.modal.active {
    display: flex;
}

.modal-content {
    width: min(390px, 100%);
    padding: 22px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--bg-secondary);
    box-shadow: var(--shadow-soft);
    animation: modalIn 0.18s ease;
}

@keyframes modalIn {
    from {
        opacity: 0;
        transform: translateY(10px) scale(0.98);
    }

    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

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

.modal-header h3 {
    font-size: 19px;
    font-weight: 700;
    letter-spacing: 0;
}

.modal form {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.modal h4 {
    margin-bottom: 2px;
    color: var(--text-tertiary);
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 0;
}

.modal hr {
    margin: 20px 0;
    border: none;
    border-top: 1px solid var(--border-color);
}

.confirm-message {
    margin-bottom: 20px;
    color: var(--text-secondary);
    font-size: 15px;
    line-height: 1.45;
}

.confirm-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

/* ==================== Toast and loading ==================== */
.loading {
    display: flex;
    flex: 1;
    align-items: center;
    justify-content: center;
    padding: 40px;
}

.loading::after {
    width: 28px;
    height: 28px;
    border: 3px solid var(--border-color);
    border-top-color: var(--primary-color);
    border-radius: 50%;
    content: "";
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

.toast {
    position: fixed;
    bottom: max(92px, calc(72px + env(safe-area-inset-bottom)));
    left: 50%;
    z-index: 2000;
    max-width: calc(100vw - 36px);
    padding: 12px 18px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.96);
    color: var(--text-primary);
    box-shadow: var(--shadow-soft);
    font-size: 14px;
    font-weight: 600;
    transform: translateX(-50%);
    animation: toastIn 0.18s ease;
}

@keyframes toastIn {
    from {
        opacity: 0;
        transform: translate(-50%, 8px);
    }

    to {
        opacity: 1;
        transform: translate(-50%, 0);
    }
}

/* ==================== Responsive ==================== */
.mobile-only {
    display: none !important;
}

@media (max-width: 768px) {
    #main-page {
        flex-direction: column;
    }

    #conversations-panel {
        width: 100%;
        border-right: none;
    }

    #messages-panel {
        position: fixed;
        inset: 0;
        z-index: 100;
        padding-top: env(safe-area-inset-top);
        transform: translateX(100%);
        transition: transform 0.28s ease;
    }

    #messages-panel.visible {
        transform: translateX(0);
    }

    .mobile-only {
        display: inline-flex !important;
    }

    .panel-header {
        min-height: 58px;
        padding: 9px 12px;
    }

    .message-header-left,
    .message-header-actions {
        min-width: 66px;
    }

    .messages {
        padding: 14px 12px 18px;
    }

    .message-content {
        max-width: 82%;
    }

    .message-bubble {
        font-size: 16px;
    }
}

@media (min-width: 769px) {
    #main-page.active {
        display: flex;
    }
}

/* ==================== Scrollbars ==================== */
::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: #D1D1D6;
    border: 2px solid transparent;
    border-radius: 999px;
    background-clip: content-box;
}
