:root {
    --wj-bg: #ffffff;
    --wj-fg: #111111;
    --wj-card: rgba(0, 0, 0, 0.04);
    --wj-muted: rgba(0, 0, 0, 0.55);

    color-scheme: light dark;
}

html, body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    background: var(--wj-bg);
    color: var(--wj-fg);
    -webkit-text-size-adjust: 100%;
}

#app {
    position: fixed;
    inset: 0;
    background: var(--wj-bg);
    color: var(--wj-fg);
    box-sizing: border-box;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;

    padding:
            calc(12px + env(safe-area-inset-top))
            12px
            calc(12px + env(safe-area-inset-bottom))
            12px;

    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
}

.card {
    background: var(--wj-card);
    border-radius: 16px;
    padding: 14px 14px;
}

.title {
    font-size: 26px;
    font-weight: 800;
    margin-bottom: 6px;
}

.subtitle {
    font-size: 14px;
    color: var(--wj-muted);
    margin-bottom: 10px;
}

.roles {
    display: inline-flex;
    gap: 8px;
    flex-wrap: wrap;
}

.role {
    padding: 6px 10px;
    border-radius: 999px;
    background: rgba(127, 127, 127, 0.18);
    font-size: 13px;
}

.logs {
    margin-top: 14px;
}

.logs summary {
    cursor: pointer;
    color: var(--wj-muted);
    font-size: 14px;
    user-select: none;
}

pre {
    margin: 10px 0 0 0;
    padding: 12px;
    border-radius: 12px;
    background: rgba(127, 127, 127, 0.10);
    white-space: pre-wrap;
    word-break: break-word;
    font-size: 12px;
    line-height: 1.35;
}

.hidden { display: none; }

/* --- Programs (mobile-first) --- */
.programs {
    margin-top: 14px;
}

.programs__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 10px;
}

.programs__title {
    margin: 0;
    font-size: 18px;
    font-weight: 900;
    letter-spacing: -0.01em;
}

.programs__empty {
    font-size: 14px;
    color: var(--wj-muted);
    margin: 0 0 12px 0;
}

.programs__list {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

/* --- Buttons --- */
.btn {
    box-sizing: border-box;
    padding: 10px 12px;
    border-radius: 12px;
    border: 1px solid rgba(0, 0, 0, 0.12);
    background: rgba(255, 255, 255, 0.85);
    color: inherit;
    font-size: 14px;
    font-weight: 900;
    cursor: pointer;
    white-space: nowrap;
}

.btn:active {
    transform: translateY(0.5px);
}

.btn-primary {
    border: 0;
    background: #2563eb;
    color: #ffffff;
}

/* --- Program card --- */
.program-card {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    padding-right: 52px; /* room for ⋮ button */
    border-radius: 16px;
    border: 1px solid rgba(0, 0, 0, 0.08);
    background: rgba(255, 255, 255, 0.92);
    box-shadow: 0 1px 0 rgba(0, 0, 0, 0.03);
    position: relative;
    -webkit-touch-callout: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    /* Smooth "push away" animation while dragging */
    transition: transform 3000ms ease;
}

.program-card__img {
    width: 64px;
    height: 64px;
    flex: 0 0 64px;
    border-radius: 14px;
    background: linear-gradient(135deg, rgba(37, 99, 235, 0.35), rgba(16, 185, 129, 0.20));
}

.program-card__body {
    min-width: 0;
    flex: 1 1 auto;
}

.program-card__title {
    font-size: 16px;
    font-weight: 900;
    margin-bottom: 4px;
}

.program-card__desc {
    font-size: 13px;
    color: var(--wj-muted);
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.prog-menu-btn {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 36px;
    height: 36px;
    flex: 0 0 36px;
    border-radius: 12px;
    border: 1px solid rgba(0, 0, 0, 0.10);
    background: rgba(255, 255, 255, 0.7);
    color: rgba(0, 0, 0, 0.70);
    font-size: 18px;
    line-height: 1;
    cursor: pointer;
}

.prog-menu-btn:active {
    transform: translateY(0.5px);
}

.prog-ctx-menu {
    position: fixed;
    z-index: 1200;
    min-width: 180px;
    border-radius: 14px;
    border: 1px solid #e5e5e5;
    background: #ffffff;
    /* Force readable text in Telegram dark theme */
    color: #222222;
    box-shadow: 0 14px 40px rgba(0, 0, 0, 0.18);
    padding: 6px;
}

.prog-ctx-menu__item {
    width: 100%;
    text-align: left;
    padding: 10px 10px;
    border: 0;
    background: transparent;
    border-radius: 10px;
    font-size: 14px;
    font-weight: 800;
    cursor: pointer;
    /* Some Telegram clients may override button text color; keep it readable */
    color: #222222 !important;
    -webkit-text-fill-color: #222222;
}

/* Hover on desktop; harmless on mobile */
.prog-ctx-menu__item:hover {
    background: #f2f2f2;
    cursor: pointer;
}

.prog-ctx-menu__item:active {
    background: #eaeaea;
}

.prog-ctx-menu__item--danger {
    color: #b00020 !important;
    -webkit-text-fill-color: #b00020;
}

.prog-ctx-menu__item--danger:hover {
    background: #ffeaea;
}

/* --- Program reorder (frontend only) --- */
body.wj-dragging {
    user-select: none;
}

/* Prevent browser from converting long-press/move into scroll/gesture while arming/dragging */
body.wj-drag-arming .program-card,
body.wj-dragging .program-card {
    touch-action: none;
}

/* Prevent scroll/gesture conflicts while dragging on touch */
body.wj-dragging .programs__list {
    touch-action: none;
    /* Keep cards inside the list area while dragging (no overlap with header/button) */
    overflow: hidden;
}

.program-card--dragging {
    opacity: 0.7;
    transform: scale(1.02);
    will-change: left, top, transform;
}

.program-card--ghost {
    /* Ghost must follow finger instantly (no 3s transform transition). */
    transition: none !important;
}

.program-card--source-hidden {
    opacity: 0 !important;
}

#wj-drag-layer {
    touch-action: none;
}

.program-card--placeholder {
    /* Occupies space, but visually "empty slot" (desktop stays as-is) */
    visibility: hidden;
}

/* Mobile-only placeholder visuals */
@media (hover: none) and (pointer: coarse) {
    .program-card--placeholder {
        visibility: visible;
        pointer-events: none;
        background: rgba(37, 99, 235, 0.06);
        border: 2px dashed rgba(37, 99, 235, 0.45);
        box-shadow: none;
    }
}

@media (max-width: 420px) {
    .programs__header {
        align-items: stretch;
        flex-direction: column;
    }
    .btn {
        width: 100%;
    }
}

/* --- Modal --- */
.modal {
    position: fixed;
    inset: 0;
    z-index: 999;
}

.modal__overlay {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
}

.modal__dialog {
    position: relative;
    margin: 0 auto;
    top: 50%;
    transform: translateY(-50%);
    width: min(560px, 95vw);
    max-height: 85vh;
    overflow: auto;
    box-sizing: border-box;
    background: #ffffff;
    color: #111111;
    border-radius: 16px;
    box-shadow: 0 18px 60px rgba(0, 0, 0, 0.28);
    padding: 16px;
}

.modal__title {
    font-size: 18px;
    font-weight: 900;
    margin-bottom: 12px;
}

.modal__close {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 36px;
    height: 36px;
    border-radius: 10px;
    border: 0;
    background: rgba(0, 0, 0, 0.06);
    color: rgba(0, 0, 0, 0.75);
    font-size: 22px;
    line-height: 1;
    cursor: pointer;
}

.modal__actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    margin-top: 14px;
}

.modal__error {
    margin-top: 10px;
    color: #b91c1c;
    font-size: 13px;
    font-weight: 700;
}

.field {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-bottom: 10px;
}

.field__label {
    font-size: 13px;
    font-weight: 800;
    color: rgba(0, 0, 0, 0.75);
}

.field__input {
    width: 100%;
    box-sizing: border-box;
    padding: 10px 12px;
    border-radius: 12px;
    border: 1px solid rgba(0, 0, 0, 0.14);
    background: #ffffff;
    color: #111111;
    font-size: 14px;
    outline: none;
}

.field__input:focus {
    border-color: rgba(37, 99, 235, 0.55);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.18);
}

.field__textarea {
    resize: vertical;
    min-height: 96px;
}

@media (max-width: 420px) {
    .modal__actions {
        flex-direction: column;
    }
    .modal__actions .btn {
        width: 100%;
    }
}
