:root {
    --bg: #f4f6f9;
    --surface: #ffffff;
    --text: #1f2937;
    --muted: #6b7280;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --border: #e5e7eb;
    --success: #059669;
    --error: #dc2626;
    --tab-active: #2563eb;
    --page-gutter: clamp(0.75rem, 2.5vw, 1.5rem);
    --bot-phone-width: 300px;
    --bot-phone-gap: 2rem;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: "Segoe UI", system-ui, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* Fixed floating header + tabs — full width minus gutters */
.app-top-bar {
    position: sticky;
    top: 0.625rem;
    z-index: 200;
    width: 100%;
    box-sizing: border-box;
    padding: 0.625rem var(--page-gutter) 0;
    flex-shrink: 0;
}

.app-top-panel {
    width: 100%;
    box-sizing: border-box;
    background: var(--surface);
    border: 1px solid rgba(15, 23, 42, 0.08);
    border-radius: 14px;
    box-shadow:
        0 4px 6px rgba(15, 23, 42, 0.04),
        0 12px 32px rgba(15, 23, 42, 0.1);
    overflow: hidden;
}

.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.45rem 1rem;
    background: transparent;
    border-bottom: none;
    min-height: 2.5rem;
}

.brand {
    display: flex;
    align-items: baseline;
    gap: 0.5rem;
    min-width: 0;
}

.brand-title { font-weight: 700; font-size: 1.05rem; line-height: 1.2; }
.brand-sub {
    font-size: 0.75rem;
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.user-bar {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.85rem;
    flex-shrink: 0;
}

.user-bar .btn-link {
    padding: 0.2rem 0.35rem;
    font-size: 0.85rem;
}

.main-tabs {
    display: flex;
    width: 100%;
    gap: 0;
    background: transparent;
    border-bottom: none;
    border-top: 1px solid var(--border);
    padding: 0 0.35rem;
    overflow-x: auto;
    scrollbar-width: thin;
}

.main-tabs .tab {
    padding: 0.45rem 0.9rem;
    text-decoration: none;
    color: var(--muted);
    border-bottom: 2px solid transparent;
    white-space: nowrap;
    font-size: 0.875rem;
    line-height: 1.25;
    transition: color 0.15s, border-color 0.15s;
}

.main-tabs .tab:hover { color: var(--text); }
.main-tabs .tab.active {
    color: var(--tab-active);
    border-bottom-color: var(--tab-active);
    font-weight: 600;
}

.app-main {
    flex: 1;
    width: 100%;
    box-sizing: border-box;
    padding: 1rem var(--page-gutter) 1.5rem;
    margin: 0;
}

.app-footer {
    padding: 1rem var(--page-gutter);
    text-align: center;
    color: var(--muted);
    font-size: 0.85rem;
    border-top: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.app-footer-dev {
    font-size: 0.8rem;
    color: var(--muted);
}

.page-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 1.5rem;
    box-shadow: 0 1px 3px rgba(0,0,0,0.04);
}

.page-card h1 { margin-top: 0; font-size: 1.5rem; }

.placeholder-text, .section-desc { color: var(--muted); }

.settings-shell {
    display: grid;
    grid-template-columns: 220px 1fr;
    gap: 1.5rem;
    align-items: start;
}

.settings-menu {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 1rem;
}

.settings-menu-title {
    margin: 0 0 0.75rem;
    font-size: 1rem;
    color: var(--muted);
}

.settings-link {
    display: block;
    padding: 0.6rem 0.75rem;
    border-radius: 6px;
    text-decoration: none;
    color: var(--text);
    margin-bottom: 0.25rem;
}

.settings-link:hover { background: var(--bg); }
.settings-link.active {
    background: #eff6ff;
    color: var(--primary);
    font-weight: 600;
}

.form-group { margin-bottom: 1rem; }
.form-group label { display: block; margin-bottom: 0.35rem; font-weight: 500; }

.form-control {
    width: 100%;
    max-width: 480px;
    padding: 0.5rem 0.65rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 1rem;
}

textarea.form-control { max-width: 100%; resize: vertical; }

.form-check {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
}

.form-stack { max-width: 640px; }
.form-actions { display: flex; gap: 0.75rem; margin-top: 1rem; flex-wrap: wrap; }

.btn {
    display: inline-block;
    padding: 0.5rem 1rem;
    border-radius: 6px;
    border: 1px solid transparent;
    font-size: 0.95rem;
    cursor: pointer;
    text-decoration: none;
    background: transparent;
}

.btn-primary {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
}

.btn-primary:hover { background: var(--primary-hover); }

.btn-secondary {
    background: var(--surface);
    border-color: var(--border);
    color: var(--text);
}

.btn-link {
    border: none;
    color: var(--primary);
    padding: 0.25rem 0.5rem;
}

.btn-danger-link { color: var(--error); }
.btn-block { width: 100%; max-width: 320px; }

.inline-form { display: inline; }

.alert {
    padding: 0.75rem 1rem;
    border-radius: 6px;
    margin-bottom: 1rem;
}

.alert-success { background: #ecfdf5; color: var(--success); border: 1px solid #a7f3d0; }
.alert-error { background: #fef2f2; color: var(--error); border: 1px solid #fecaca; }

.field-error { color: var(--error); font-size: 0.85rem; }
.hint { color: var(--muted); font-size: 0.85rem; }

.data-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 1rem;
    font-size: 0.95rem;
}

.data-table th,
.data-table td {
    padding: 0.65rem 0.75rem;
    border-bottom: 1px solid var(--border);
    text-align: left;
}

.data-table th { background: var(--bg); font-weight: 600; }

.actions-cell { white-space: nowrap; }
.page-header-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.code-block {
    background: var(--bg);
    padding: 0.75rem;
    border-radius: 6px;
    overflow-x: auto;
    font-size: 0.85rem;
}

.form-row-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

@media (max-width: 640px) {
    .form-row-2 { grid-template-columns: 1fr; }
}

.btn-danger {
    background: #fff;
    color: var(--error);
    border-color: var(--error);
}

.btn-danger:hover { background: #fef2f2; }

.saved-db-section {
    margin-top: 2rem;
    padding-top: 1.5rem;
    border-top: 1px solid var(--border);
}

.saved-db-section h2 {
    margin: 0 0 1rem;
    font-size: 1.1rem;
}

.db-status-badge {
    display: inline-block;
    padding: 0.25rem 0.65rem;
    border-radius: 999px;
    font-size: 0.85rem;
    font-weight: 600;
}

.status-ok { background: #ecfdf5; color: var(--success); }
.status-fail { background: #fef2f2; color: var(--error); }

.log-settings-form { margin-bottom: 1rem; }
.log-settings-grid {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 1rem 1.5rem;
    align-items: end;
    margin-bottom: 1rem;
}

.log-filter-form {
    margin-bottom: 1rem;
    max-width: 280px;
}

.log-toggle-form {
    display: flex;
    align-items: center;
    gap: 1rem;
    margin-bottom: 1rem;
    flex-wrap: wrap;
}

.log-table details { margin-top: 0.35rem; font-size: 0.85rem; }
.log-level-error td:nth-child(2) { color: var(--error); font-weight: 600; }
.empty-row { color: var(--muted); text-align: center; }

.bot-status {
    padding: 0.75rem 1rem;
    border-radius: 8px;
    margin-bottom: 1rem;
    font-size: 0.95rem;
}

.bot-status-running { background: #ecfdf5; border: 1px solid #a7f3d0; }
.bot-status-error { background: #fef2f2; border: 1px solid #fecaca; }
.bot-status-stopped,
.bot-status-disabled { background: #f3f4f6; border: 1px solid var(--border); }

/* Bot page: card scrolls, phone fixed outside the card on the right */
.bot-page-layout {
    width: 100%;
    position: relative;
}

.bot-page-card {
    width: 100%;
}

.bot-editor-form {
    margin-top: 1.5rem;
}

.bot-section-divider {
    margin: 2rem 0 1.25rem;
    border: none;
    border-top: 1px solid var(--border);
}

.bot-settings-heading {
    margin: 0 0 1rem;
    font-size: 1.1rem;
    font-weight: 700;
}

.form-row-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

@media (max-width: 720px) {
    .form-row-2 { grid-template-columns: 1fr; }
}

.theme-presets {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

.color-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 0.75rem 1rem;
    margin-bottom: 1rem;
}

@media (max-width: 600px) {
    .color-grid { grid-template-columns: 1fr; }
}

.color-field label {
    display: block;
    font-size: 0.8rem;
    font-weight: 600;
    margin-bottom: 0.25rem;
}

.color-input-row {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}

.color-input-row input[type="color"] {
    width: 42px;
    height: 36px;
    padding: 2px;
    border: 1px solid var(--border);
    border-radius: 6px;
    cursor: pointer;
    flex-shrink: 0;
}

.color-input-row .color-text {
    flex: 1;
    font-family: ui-monospace, monospace;
    font-size: 0.85rem;
}

.bot-details {
    margin: 0.75rem 0 1rem;
    padding: 0.75rem 1rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: #fafbfc;
}

.bot-details summary {
    cursor: pointer;
    font-weight: 600;
}

.form-check-inline {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    margin-top: 0.35rem;
    font-size: 0.85rem;
    font-weight: normal;
}

.bot-phone-dock {
    display: none;
}

@media (min-width: 961px) {
    .bot-page-layout {
        padding-right: calc(var(--bot-phone-width) + var(--bot-phone-gap));
    }

    .bot-phone-dock {
        display: block;
        position: fixed;
        top: 50%;
        right: var(--page-gutter);
        width: var(--bot-phone-width);
        z-index: 90;
        transform: translateY(-50%);
    }
}

@media (max-width: 960px) {
    .bot-phone-dock {
        display: block;
        position: static;
        margin-bottom: 1.25rem;
    }

    .bot-page-layout {
        display: flex;
        flex-direction: column-reverse;
    }
}

.phone-notch .preview-label {
    margin: 0;
    font-size: 0.72rem;
    font-weight: 600;
    color: #fff;
    text-align: center;
    letter-spacing: 0.02em;
    line-height: 1.2;
}

.phone-mockup {
    display: flex;
    justify-content: center;
}

.phone-frame {
    width: 280px;
    padding: 12px 10px 14px;
    background: #1a1a1e;
    border-radius: 36px;
    box-shadow:
        0 0 0 2px #2d2d33,
        0 20px 50px rgba(0, 0, 0, 0.25),
        inset 0 0 0 1px rgba(255, 255, 255, 0.06);
}

.phone-notch {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    min-height: 32px;
    margin: 0 0 8px;
    padding: 8px 10px 6px;
    background: #0d0d10;
    border-radius: 0 0 12px 12px;
}

.phone-screen {
    position: relative;
    background: #8ebad8;
    background-image:
        radial-gradient(circle at 20% 30%, rgba(255, 255, 255, 0.15) 0%, transparent 50%),
        radial-gradient(circle at 80% 70%, rgba(255, 255, 255, 0.1) 0%, transparent 40%);
    border-radius: 24px;
    overflow: hidden;
    min-height: 420px;
    display: flex;
    flex-direction: column;
    font-size: 13px;
}

.preview-chat-layer {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.preview-app-layer {
    position: absolute;
    inset: 0;
    z-index: 10;
    display: flex;
    flex-direction: column;
    background: #f0f4f8;
    border-radius: 24px;
    overflow: hidden;
}

.preview-app-layer[hidden] {
    display: none !important;
}

.preview-app-close {
    flex-shrink: 0;
    padding: 8px 12px;
    border: none;
    background: rgba(255, 255, 255, 0.95);
    color: #3390ec;
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
    text-align: left;
    font-family: inherit;
    border-bottom: 1px solid rgba(0, 0, 0, 0.06);
}

.preview-app-frame {
    flex: 1;
    width: 100%;
    border: none;
    background: #fff;
}

.tg-inline-webapp {
    align-self: flex-start;
    max-width: 92%;
}

.tg-webapp-open-btn {
    display: block;
    width: 100%;
    padding: 10px 12px;
    border: none;
    border-radius: 10px;
    background: #3390ec;
    color: #fff;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
    box-shadow: 0 2px 8px rgba(51, 144, 236, 0.35);
}

.tg-webapp-open-btn:active {
    transform: scale(0.98);
}

.tg-app-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    background: rgba(255, 255, 255, 0.92);
    border-bottom: 1px solid rgba(0, 0, 0, 0.06);
}

.tg-back {
    color: #3390ec;
    font-size: 22px;
    line-height: 1;
    font-weight: 300;
}

.tg-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: linear-gradient(135deg, #6ab2f2, #3390ec);
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.tg-header-text {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.tg-header-text strong {
    font-size: 14px;
    font-weight: 600;
    color: #000;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.tg-header-text span {
    font-size: 12px;
    color: #8e8e93;
}

.tg-profile-hint {
    padding: 6px 14px 4px;
    font-size: 11px;
    color: rgba(0, 0, 0, 0.45);
    text-align: center;
    line-height: 1.35;
    max-height: 2.7em;
    overflow: hidden;
}

.tg-chat {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    background: transparent;
}

.tg-messages {
    flex: 1;
    min-height: 120px;
    max-height: 220px;
    overflow-y: auto;
    padding: 8px 10px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.tg-bubble {
    max-width: 88%;
    padding: 8px 10px;
    line-height: 1.4;
    font-size: 12px;
    white-space: pre-wrap;
    word-break: break-word;
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.08);
}

.tg-bubble-in {
    align-self: flex-start;
    background: #fff;
    color: #000;
    border-radius: 4px 12px 12px 12px;
}

.tg-bubble-out {
    align-self: flex-end;
    background: #eeffde;
    color: #000;
    border-radius: 12px 4px 12px 12px;
}

.tg-compose {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 8px;
    background: rgba(255, 255, 255, 0.75);
    border-top: 1px solid rgba(0, 0, 0, 0.06);
}

.tg-compose-input {
    flex: 1;
    min-width: 0;
    border: 1px solid #c5d0db;
    border-radius: 16px;
    padding: 6px 10px;
    font-size: 12px;
    font-family: inherit;
    background: #fff;
}

.tg-compose-input:focus {
    outline: none;
    border-color: #3390ec;
}

.tg-compose-send {
    flex-shrink: 0;
    width: 32px;
    height: 32px;
    border: none;
    border-radius: 50%;
    background: #3390ec;
    color: #fff;
    font-size: 14px;
    cursor: pointer;
    line-height: 1;
}

.tg-compose-send:hover {
    background: #2b7fd4;
}

.tg-reply-keyboard {
    padding: 6px 6px 8px;
    background: rgba(255, 255, 255, 0.92);
    border-top: 1px solid rgba(0, 0, 0, 0.06);
}

.tg-kb-row {
    display: flex;
    gap: 6px;
    margin-bottom: 6px;
}

.tg-kb-row:last-child {
    margin-bottom: 0;
}

.tg-kb-btn {
    flex: 1;
    min-width: 0;
    padding: 8px 4px;
    border: none;
    border-radius: 8px;
    background: #fff;
    color: #3390ec;
    font-size: 11px;
    font-weight: 500;
    cursor: pointer;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
    font-family: inherit;
}

.tg-kb-btn-wide {
    flex: 1 1 100%;
}

.tg-kb-btn:active {
    background: #e8eef3;
}

.tg-phone-footer {
    position: relative;
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 10px 12px;
    background: rgba(255, 255, 255, 0.85);
    border-top: 1px solid rgba(0, 0, 0, 0.06);
}

.tg-menu-pill {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    flex: 1;
    padding: 8px 12px;
    border: none;
    border-radius: 8px;
    background: rgba(0, 0, 0, 0.04);
    color: #3390ec;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    font-family: inherit;
}

.tg-menu-pill:hover {
    background: rgba(0, 0, 0, 0.07);
}

.tg-commands-popup {
    position: absolute;
    bottom: calc(100% + 6px);
    left: 10px;
    right: 10px;
    background: #fff;
    border-radius: 10px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
    padding: 6px;
    z-index: 5;
}

.tg-cmd-item {
    display: block;
    width: 100%;
    text-align: left;
    padding: 10px 12px;
    border: none;
    border-radius: 8px;
    background: transparent;
    color: #3390ec;
    font-size: 13px;
    cursor: pointer;
    font-family: inherit;
}

.tg-cmd-item:hover {
    background: #f0f4f8;
}

.tg-restart-btn {
    flex-shrink: 0;
    width: 36px;
    height: 36px;
    border: none;
    border-radius: 8px;
    background: rgba(0, 0, 0, 0.05);
    color: #6b7280;
    font-size: 18px;
    cursor: pointer;
    line-height: 1;
}

.tg-restart-btn:hover {
    background: rgba(0, 0, 0, 0.09);
    color: #374151;
}

.tg-menu-icon {
    font-size: 16px;
    opacity: 0.85;
}

@media (max-width: 960px) {
    .phone-mockup {
        margin-bottom: 0.5rem;
    }
}

.login-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background: linear-gradient(135deg, #1e3a8a 0%, #3b82f6 100%);
}

.login-card {
    background: var(--surface);
    padding: 2rem;
    border-radius: 12px;
    width: 100%;
    max-width: 400px;
    box-shadow: 0 10px 40px rgba(0,0,0,0.15);
}

.login-card h1 { margin-top: 0; font-size: 1.35rem; }
.login-hint { color: var(--muted); margin-bottom: 1.5rem; }
.login-default { font-size: 0.85rem; color: var(--muted); margin-top: 1rem; }

@media (max-width: 768px) {
    .settings-shell { grid-template-columns: 1fr; }
}
