:root {
    --accent: #14532d;
    --accent-light: #dcfce7;
    --bg-page: #f3f6f4;
    --bg-panel: #ffffff;
    --border-color: #dbe3de;
    --text-main: #1f2a24;
    --text-muted: #6b7a72;
}

* {
    box-sizing: border-box;
}

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg-page);
    color: var(--text-main);
}

button {
    font-family: inherit;
    cursor: pointer;
}

/* ---------- Login screen ---------- */
.login-screen {
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
}

.login-card {
    background: var(--bg-panel);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 32px;
    width: 320px;
    text-align: center;
}

.login-card h1 {
    font-size: 20px;
    color: var(--accent);
    margin-bottom: 8px;
}

.login-hint {
    color: var(--text-muted);
    font-size: 14px;
    margin-bottom: 16px;
}

.login-card select {
    width: 100%;
    padding: 8px;
    margin-bottom: 12px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
}

.btn-primary {
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 6px;
    padding: 10px 16px;
    font-size: 14px;
    width: 100%;
}

.btn-link {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 13px;
    text-decoration: underline;
}

.error-text {
    color: #b91c1c;
    font-size: 13px;
    margin-top: 8px;
}

/* ---------- App shell ---------- */
.app-shell {
    display: flex;
    height: 100vh;
    width: 100vw;
    overflow: hidden;
}

.client-sidebar {
    width: 320px;
    flex-shrink: 0;
    background: var(--bg-panel);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
}

.sidebar-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px;
    border-bottom: 1px solid var(--border-color);
}

.sidebar-header input {
    flex: 1;
    padding: 8px 10px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-size: 14px;
    min-width: 0;
}

.current-user-name {
    font-size: 12px;
    color: var(--text-muted);
    white-space: nowrap;
}

.client-list {
    flex: 1;
    overflow-y: auto;
}

.client-row {
    padding: 10px 14px;
    border-bottom: 1px solid var(--border-color);
    cursor: pointer;
}

.client-row:hover {
    background: var(--bg-page);
}

.client-row.selected {
    background: var(--accent-light);
}

.client-row-line1, .client-row-line2 {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
}

.client-name {
    font-weight: 600;
    font-size: 14px;
}

.client-row.has-unread {
    background: #fef2f2;
}

.client-row.has-unread:hover {
    background: #fde8e8;
}

.client-row.has-unread.selected {
    background: var(--accent-light);
}

.unread-dot {
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: #dc2626;
    box-shadow: 0 0 0 3px #fecaca;
    margin-right: 8px;
    flex-shrink: 0;
}

.client-row.has-unread .client-name {
    color: #b91c1c;
}

.client-row.has-unread .client-last-message {
    color: #1f2a24;
    font-weight: 600;
}

.client-phone {
    font-size: 12px;
    color: var(--text-muted);
}

.client-last-message {
    font-size: 13px;
    color: var(--text-muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.client-last-when {
    font-size: 11px;
    color: var(--text-muted);
    flex-shrink: 0;
}

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

/* ---------- Chat panel ---------- */
.chat-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.chat-empty-state {
    margin: auto;
    color: var(--text-muted);
    font-size: 15px;
}

.chat-header {
    padding: 14px 20px;
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-panel);
    display: flex;
    align-items: baseline;
    gap: 10px;
}

.chat-header-name {
    font-weight: 600;
    font-size: 16px;
}

.chat-header-phone {
    color: var(--text-muted);
    font-size: 13px;
}

.chat-back-btn {
    display: none;
    background: none;
    border: none;
    font-size: 18px;
    color: var(--accent);
    padding: 0 4px 0 0;
}

.chat-header-disconnected {
    font-size: 12px;
    color: #b91c1c;
    background: #fee2e2;
    padding: 2px 8px;
    border-radius: 10px;
}

.chat-disconnect-btn {
    margin-left: auto;
    background: none;
    border: none;
    font-size: 16px;
    opacity: 0.55;
    padding: 4px;
    border-radius: 6px;
}

.chat-header-balance {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    color: var(--text-muted);
}

.chat-header-balance input {
    width: 80px;
    padding: 3px 6px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-size: 13px;
}

.chat-header-balance button {
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 6px;
    width: 24px;
    height: 24px;
    font-size: 12px;
}

.invoice-toggle {
    display: block;
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 6px;
    cursor: pointer;
}

.chat-disconnect-btn:hover {
    opacity: 1;
    background: #fee2e2;
}

.messages-list {
    flex: 1;
    overflow-y: auto;
    padding: 16px 20px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.messages-empty {
    margin: auto;
    color: var(--text-muted);
}

.date-divider {
    text-align: center;
    margin: 12px 0;
}

.date-divider span {
    background: var(--bg-page);
    color: var(--text-muted);
    font-size: 12px;
    padding: 3px 10px;
    border-radius: 10px;
}

.message-row {
    display: flex;
    margin: 4px 0;
}

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

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

.message-bubble {
    max-width: 60%;
    border-radius: 10px;
    padding: 8px 12px;
    font-size: 14px;
    line-height: 1.4;
    overflow: hidden;
}

.align-left .message-bubble {
    background: #ffffff;
    border: 1px solid var(--border-color);
}

.align-right .message-bubble {
    background: var(--accent);
    color: #fff;
}

.message-sender {
    font-size: 12px;
    font-weight: 600;
    margin-bottom: 2px;
    opacity: 0.85;
}

.message-confidential-badge {
    display: inline-block;
    font-size: 11px;
    font-weight: 600;
    line-height: 1.3;
    padding: 3px 7px;
    border-radius: 5px;
    margin-bottom: 5px;
    background: rgba(0, 0, 0, 0.08);
    color: var(--text-muted);
}

.align-right .message-confidential-badge {
    background: rgba(255, 255, 255, 0.2);
    color: #ffffff;
}

.message-image {
    max-width: 100%;
    border-radius: 6px;
    display: block;
    margin-bottom: 4px;
}

.voice-player {
    display: flex;
    align-items: center;
    gap: 6px;
    width: 100%;
    max-width: 210px;
    margin-bottom: 4px;
    box-sizing: border-box;
}

.voice-play-btn {
    flex-shrink: 0;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: none;
    background: rgba(20, 83, 45, 0.12);
    color: inherit;
    font-size: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.align-right .voice-play-btn {
    background: rgba(255, 255, 255, 0.25);
}

.voice-waveform {
    flex: 1 1 auto;
    min-width: 0;
    width: 100%;
    height: 32px;
    cursor: pointer;
}

.voice-time {
    flex-shrink: 0;
    font-size: 11px;
    opacity: 0.8;
    width: 32px;
    text-align: right;
    white-space: nowrap;
    overflow: hidden;
}

.message-text {
    white-space: pre-wrap;
    word-break: break-word;
}

.message-footer {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 6px;
    margin-top: 2px;
}

.message-time {
    font-size: 11px;
    opacity: 0.7;
}

.message-delete-btn {
    display: inline-flex;
    align-items: center;
    gap: 2px;
    background: rgba(0, 0, 0, 0.08);
    border: 1px solid rgba(0, 0, 0, 0.15);
    border-radius: 6px;
    color: var(--text-muted);
    line-height: 1;
    opacity: 0.85;
    padding: 4px 6px;
}

.align-right .message-delete-btn {
    background: rgba(255, 255, 255, 0.18);
    border-color: rgba(255, 255, 255, 0.4);
    color: #ffffff;
}

.message-delete-btn:hover {
    opacity: 1;
    background: rgba(0, 0, 0, 0.16);
}

.align-right .message-delete-btn:hover {
    background: rgba(255, 255, 255, 0.32);
}

/* "Delete everywhere" is destructive — always red, on any bubble color. */
.message-delete-everyone-btn {
    color: #dc2626;
    background: rgba(220, 38, 38, 0.12);
    border-color: rgba(220, 38, 38, 0.4);
}

.align-right .message-delete-everyone-btn {
    color: #fecaca;
    background: rgba(220, 38, 38, 0.28);
    border-color: rgba(254, 202, 202, 0.5);
}

.message-delete-everyone-btn:hover {
    background: rgba(220, 38, 38, 0.22);
}

.align-right .message-delete-everyone-btn:hover {
    background: rgba(220, 38, 38, 0.4);
}

/* ---------- Composer ---------- */
.composer {
    border-top: 2px solid var(--accent);
    padding: 10px 16px;
    background: var(--bg-panel);
}

.composer-readonly {
    color: var(--text-muted);
    font-size: 13px;
    text-align: center;
    padding: 14px;
}

.composer-row {
    display: flex;
    align-items: flex-end;
    gap: 8px;
}

#composer-text {
    flex: 1;
    resize: none;
    border: 1px solid var(--border-color);
    outline: none;
    font-size: 14px;
    font-family: inherit;
    padding: 8px 12px;
    max-height: 120px;
    border-radius: 18px;
    background: var(--bg-page);
}

#composer-text:focus {
    border-color: var(--accent);
    background: var(--bg-panel);
}

.btn-send {
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 50%;
    width: 38px;
    height: 38px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s ease, transform 0.1s ease;
}

.btn-send svg {
    margin-left: -2px;
}

.btn-send:hover {
    background: #1a6b39;
}

.btn-send:active {
    transform: scale(0.94);
}

.btn-icon {
    background: var(--bg-page);
    border: 1px solid var(--border-color);
    font-size: 18px;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    flex-shrink: 0;
}

.btn-icon:hover {
    background: var(--accent-light);
    border-color: var(--accent);
}

.btn-icon.recording {
    background: #dc2626;
    color: #fff;
    animation: pulse-recording 1s infinite;
}

@keyframes pulse-recording {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.6; }
}

.recording-indicator {
    color: #dc2626;
    font-size: 13px;
    font-weight: 600;
    white-space: nowrap;
}

.image-preview {
    position: relative;
    display: inline-block;
    margin-bottom: 8px;
}

.image-preview img {
    max-height: 100px;
    border-radius: 6px;
    display: block;
}

#image-preview-remove {
    position: absolute;
    top: -6px;
    right: -6px;
    background: #b91c1c;
    color: #fff;
    border: none;
    border-radius: 50%;
    width: 20px;
    height: 20px;
    font-size: 11px;
    line-height: 1;
}

/* ---------- Admin ---------- */
.admin-page {
    max-width: 900px;
    margin: 0 auto;
    padding: 24px 20px 60px;
}

.admin-header {
    display: flex;
    align-items: baseline;
    gap: 16px;
    margin-bottom: 20px;
}

.admin-header h1 {
    font-size: 20px;
    color: var(--accent);
    margin: 0;
}

.admin-card {
    background: var(--bg-panel);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    padding: 18px 20px;
    margin-bottom: 20px;
}

.admin-card h2 {
    font-size: 15px;
    margin: 0 0 12px;
}

.create-user-form {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.create-user-form input,
.create-user-form select {
    padding: 8px 10px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-size: 14px;
}

.create-user-form input[type="number"] {
    width: 140px;
}

.create-user-form button {
    padding: 8px 16px;
}

.users-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
}

.users-table th, .users-table td {
    text-align: left;
    padding: 8px 6px;
    border-bottom: 1px solid var(--border-color);
}

.users-table input[type="text"] {
    width: 100%;
    border: 1px solid transparent;
    background: transparent;
    padding: 4px;
    border-radius: 4px;
    font-size: 14px;
}

.users-table input[type="text"]:hover,
.users-table input[type="text"]:focus {
    border-color: var(--border-color);
    background: var(--bg-page);
}

.users-table select {
    padding: 4px 6px;
    border-radius: 4px;
    border: 1px solid var(--border-color);
}

.tg-id-cell {
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
}

/* ---------- Mobile ---------- */
@media (max-width: 768px) {
    .app-shell {
        position: relative;
    }

    .client-sidebar {
        width: 100%;
    }

    .chat-panel {
        position: fixed;
        inset: 0;
        background: var(--bg-page);
        display: none;
        z-index: 10;
    }

    .chat-panel.mobile-active {
        display: flex;
    }

    .app-shell.chat-open .client-sidebar {
        display: none;
    }

    .chat-back-btn {
        display: inline-block;
    }
}

/* ---------- Client portal ---------- */
.portal-page {
    max-width: 480px;
    margin: 0 auto;
    padding: 20px 16px 60px;
}

.portal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
}

.portal-header h1 {
    font-size: 20px;
    margin: 0;
}

.portal-balance-card {
    background: var(--accent);
    color: #fff;
    border-radius: 12px;
    padding: 20px;
    text-align: center;
    margin-bottom: 24px;
}

.portal-balance-label {
    font-size: 13px;
    opacity: 0.85;
    margin-bottom: 4px;
}

.portal-balance-value {
    font-size: 32px;
    font-weight: 700;
}

.portal-section-title {
    font-size: 16px;
    margin: 0 0 12px;
}

.portal-invoices {
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.portal-invoice-card {
    background: var(--bg-panel);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    padding: 10px;
}

.portal-invoice-card img {
    width: 100%;
    border-radius: 6px;
    display: block;
}

.portal-invoice-caption {
    margin-top: 8px;
    font-size: 14px;
    white-space: pre-wrap;
}

.portal-invoice-date {
    margin-top: 4px;
    font-size: 12px;
    color: var(--text-muted);
}

.portal-pin-gate {
    background: var(--bg-panel);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 24px 20px;
    text-align: center;
}

.portal-pin-input {
    display: block;
    width: 140px;
    margin: 14px auto;
    padding: 10px;
    font-size: 26px;
    letter-spacing: 10px;
    text-align: center;
    border: 1px solid var(--border-color);
    border-radius: 8px;
}

.portal-pin-input:focus {
    outline: none;
    border-color: var(--accent);
}
