/*
 * Announcement Widget — Стили страницы настроек
 * Страница открывается в iframe внутри Bitrix24 (REST_APP / LEFT_MENU).
 *
 * Дизайн: чистый, системный, следует стилю Bitrix24.
 * UI-компоненты: ручная стилизация, совместимая с b24ui-палитрой.
 */

/* ─── Reset / Base ──────────────────────────────────────────────────────────── */

*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --color-bg: #f5f6f8;
    --color-surface: #ffffff;
    --color-border: #d8dde6;
    --color-border-focus: #2d81e0;
    --color-text: #1f2329;
    --color-text-muted: #828d9e;
    --color-accent: #2d81e0;
    --color-accent-hover: #1969c6;
    --color-danger: #e53935;
    --color-success: #2e7d32;
    --color-success-bg: #e8f5e9;
    --color-error-bg: #ffebee;
    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 14px;
    --shadow-card: 0 2px 8px rgba(0, 0, 0, 0.07);
    --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Arial, sans-serif;
    --transition: 0.18s ease;
}

html, body {
    height: 100%;
    background: var(--color-bg);
    font-family: var(--font);
    font-size: 14px;
    line-height: 1.5;
    color: var(--color-text);
    -webkit-font-smoothing: antialiased;
}

/* ─── Layout ────────────────────────────────────────────────────────────────── */

.settings-wrapper {
    min-height: 100vh;
    padding: 20px 16px 40px;
}

.settings-container {
    width: 100%;
    max-width: 1180px;
    margin: 0 auto;
}

/* ─── Header ────────────────────────────────────────────────────────────────── */

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

.settings-header-icon {
    font-size: 28px;
    line-height: 1;
    flex-shrink: 0;
}

.settings-header h1 {
    font-size: 20px;
    font-weight: 700;
    color: var(--color-text);
    line-height: 1.2;
}

.settings-header p {
    font-size: 13px;
    color: var(--color-text-muted);
    margin-top: 2px;
}

/* ─── Card ──────────────────────────────────────────────────────────────────── */

.card {
    background: var(--color-surface);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-card);
    border: 1px solid var(--color-border);
    padding: 20px;
    margin-bottom: 16px;
}

.card-title {
    font-size: 13px;
    font-weight: 600;
    color: var(--color-text-muted);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    margin-bottom: 16px;
}

/* ─── Form fields ───────────────────────────────────────────────────────────── */

.form-group {
    margin-bottom: 18px;
}

.form-group:last-child {
    margin-bottom: 0;
}

.form-label {
    display: block;
    font-size: 13px;
    font-weight: 600;
    color: var(--color-text);
    margin-bottom: 6px;
}

.form-label .required {
    color: var(--color-danger);
    margin-left: 2px;
}

.form-hint {
    font-size: 12px;
    color: var(--color-text-muted);
    margin-top: 5px;
    line-height: 1.4;
}

.form-error {
    font-size: 12px;
    color: var(--color-danger);
    margin-top: 5px;
    display: none;
}

.form-error.visible {
    display: block;
}

/* ─── Inputs ────────────────────────────────────────────────────────────────── */

.form-input,
.form-textarea {
    width: 100%;
    padding: 9px 12px;
    border: 1.5px solid var(--color-border);
    border-radius: var(--radius-sm);
    font-family: var(--font);
    font-size: 14px;
    color: var(--color-text);
    background: #fff;
    transition: border-color var(--transition), box-shadow var(--transition);
    outline: none;
    appearance: none;
}

.form-input:focus,
.form-textarea:focus {
    border-color: var(--color-border-focus);
    box-shadow: 0 0 0 3px rgba(45, 129, 224, 0.12);
}

.form-input.error,
.form-textarea.error {
    border-color: var(--color-danger);
}

.form-input.error:focus,
.form-textarea.error:focus {
    box-shadow: 0 0 0 3px rgba(229, 57, 53, 0.12);
}

.form-input::placeholder,
.form-textarea::placeholder {
    color: #b0b9c7;
}

.form-textarea {
    resize: vertical;
    min-height: 100px;
    max-height: 240px;
    line-height: 1.55;
}

/* Char counter */
.char-counter {
    font-size: 11px;
    color: var(--color-text-muted);
    text-align: right;
    margin-top: 4px;
}

.char-counter.warn {
    color: #e65100;
}

/* ─── Switch (Status toggle) ────────────────────────────────────────────────── */

.switch-row {
    display: flex;
    align-items: center;
    gap: 12px;
}

.switch-label-text {
    font-size: 14px;
    font-weight: 500;
    color: var(--color-text);
    flex: 1;
}

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

.switch {
    position: relative;
    display: inline-block;
    width: 44px;
    height: 24px;
    flex-shrink: 0;
    cursor: pointer;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
    position: absolute;
}

.switch-track {
    position: absolute;
    inset: 0;
    background: #d0d5dd;
    border-radius: 24px;
    transition: background var(--transition);
}

.switch input:checked + .switch-track {
    background: var(--color-accent);
}

.switch-thumb {
    position: absolute;
    width: 18px;
    height: 18px;
    background: #fff;
    border-radius: 50%;
    top: 3px;
    left: 3px;
    transition: transform var(--transition);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

.switch input:checked ~ .switch-thumb {
    transform: translateX(20px);
}

/* ─── Color pickers ─────────────────────────────────────────────────────────── */

.color-row {
    display: flex;
    gap: 16px;
}

.color-group {
    flex: 1 1 0;
    min-width: 0;
}

.color-input-wrap {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    min-width: 0;
    border: 1.5px solid var(--color-border);
    border-radius: var(--radius-sm);
    overflow: hidden;
    background: #fff;
    transition: border-color var(--transition);
}

.color-input-wrap:focus-within {
    border-color: var(--color-border-focus);
    box-shadow: 0 0 0 3px rgba(45, 129, 224, 0.12);
}

.color-swatch {
    width: 36px;
    height: 36px;
    border: none;
    cursor: pointer;
    padding: 4px;
    background: none;
    flex-shrink: 0;
}

.color-swatch::-webkit-color-swatch-wrapper {
    padding: 0;
    border-radius: 2px;
}

.color-swatch::-webkit-color-swatch {
    border: none;
    border-radius: 3px;
}

.color-text-input {
    flex: 1 1 auto;
    width: 100%;
    border: none;
    font-family: 'Courier New', monospace;
    font-size: 13px;
    color: var(--color-text);
    outline: none;
    background: transparent;
    padding: 0 10px 0 0;
    min-width: 0;
}

/* ─── Disk Upload component ─────────────────────────────────────────────────── */

.disk-upload-row {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.disk-file-label {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 14px;
    border: 1.5px dashed var(--color-border);
    border-radius: var(--radius-sm);
    background: #fafbfd;
    cursor: pointer;
    font-size: 13px;
    color: var(--color-text-muted);
    transition: border-color var(--transition), background var(--transition);
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    user-select: none;
}

.disk-file-label:hover {
    border-color: var(--color-border-focus);
    background: #f0f6ff;
    color: var(--color-accent);
}

.disk-file-label.has-file {
    border-style: solid;
    border-color: var(--color-accent);
    background: #f0f6ff;
    color: var(--color-accent);
    font-weight: 600;
}

.disk-file-icon {
    font-size: 16px;
    flex-shrink: 0;
}

.btn-upload {
    flex-shrink: 0;
    white-space: nowrap;
}

/* Progress bar */
.disk-upload-status {
    margin-top: 8px;
}

.disk-upload-progress {
    height: 4px;
    background: var(--color-border);
    border-radius: 2px;
    overflow: hidden;
    margin-bottom: 5px;
}

.disk-upload-bar {
    height: 100%;
    width: 0;
    background: var(--color-accent);
    border-radius: 2px;
    transition: width 0.3s ease;
}

.disk-upload-bar.done {
    background: var(--color-success);
}

.disk-upload-bar.error {
    background: var(--color-danger);
}

.disk-upload-msg {
    font-size: 12px;
    color: var(--color-text-muted);
}

.disk-upload-msg.success {
    color: var(--color-success);
    font-weight: 600;
}

.disk-upload-msg.error {
    color: var(--color-danger);
}

/* Image preview thumbnail */
.disk-image-preview {
    position: relative;
    display: inline-block;
    margin-top: 10px;
    border-radius: var(--radius-sm);
    overflow: hidden;
    border: 1.5px solid var(--color-border);
    max-width: 200px;
}

.disk-image-preview img {
    display: block;
    width: 200px;
    height: auto;
    max-height: 100px;
    object-fit: cover;
}

.disk-clear-btn {
    position: absolute;
    top: 4px;
    right: 4px;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: rgba(0,0,0,0.55);
    border: none;
    color: #fff;
    font-size: 12px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    padding: 0;
    transition: background var(--transition);
}

.disk-clear-btn:hover {
    background: rgba(0,0,0,0.8);
}

/* URL fallback section */
.disk-url-fallback {
    margin-top: 12px;
    display: flex;
    flex-direction: column;
    gap: 5px;
}

.disk-url-label {
    font-size: 12px;
    color: var(--color-text-muted);
    font-style: italic;
}

.disk-url-input {
    font-size: 13px;
}

/* Upload spinner — dark on light button */
.upload-spinner {
    border-color: rgba(31, 35, 41, 0.25);
    border-top-color: var(--color-text);
}

/* ─── Preview section ───────────────────────────────────────────────────────── */

.preview-card {
    background: var(--color-surface);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-card);
    border: 1px solid var(--color-border);
    padding: 20px;
    margin-bottom: 16px;
}

.preview-card .card-title {
    margin-bottom: 12px;
}

.preview-inner {
    background: var(--color-bg);
    border-radius: var(--radius-md);
    padding: 16px;
    min-height: 80px;
}

/* Preview widget styles — mirrors widget.css */
.preview-inner .announce-widget {
    width: 100%;
    font-family: var(--font);
    font-size: 14px;
    line-height: 1.5;
    color: #1f2329;
}

.preview-inner .announce-card {
    border-radius: 10px;
    overflow: hidden;
    background: #ffffff;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    padding: 14px;
    box-sizing: border-box;
}

.preview-inner .announce-content {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.preview-inner .announce-image {
    width: 100%;
    overflow: hidden;
    border-radius: 6px;
    line-height: 0;
}

.preview-inner .announce-image img {
    width: 100%;
    height: auto;
    max-height: 160px;
    object-fit: cover;
    display: block;
    border-radius: 6px;
}

.preview-inner .announce-text-block {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.preview-inner .announce-title {
    font-size: 16px;
    font-weight: 700;
    line-height: 1.3;
    margin: 0;
    color: inherit;
    word-break: break-word;
}

.preview-inner .announce-text {
    font-size: 13px;
    line-height: 1.5;
    margin: 0;
    color: inherit;
    opacity: 0.85;
    word-break: break-word;
}

.preview-inner .announce-text--preview {
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
    white-space: normal;
}

.preview-inner .announce-link {
    display: inline-block;
    font-size: 13px;
    font-weight: 600;
    color: #1a73e8;
    text-decoration: none;
    padding: 4px 0;
    transition: opacity 0.15s ease;
}

.preview-inner .announce-link:hover {
    opacity: 0.7;
}

.preview-inner .announce-card[style*="background"] .announce-link {
    color: inherit;
    opacity: 0.9;
    text-decoration: underline;
}

.preview-inner .announce-meta {
    font-size: 11px;
    opacity: 0.45;
    padding: 8px 0 0;
    color: inherit;
}

.preview-inner .announce-empty {
    text-align: center;
    padding: 20px 16px;
    font-size: 13px;
    color: #adb5bd;
    background: #f8f9fa;
    border-radius: 10px;
    border: 1px dashed #dee2e6;
}

/* ─── Action buttons ────────────────────────────────────────────────────────── */

.form-actions {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 10px;
    padding-top: 4px;
}

.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 9px 20px;
    border-radius: var(--radius-sm);
    font-family: var(--font);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    border: 1.5px solid transparent;
    transition: background var(--transition), border-color var(--transition), opacity var(--transition);
    line-height: 1;
    white-space: nowrap;
    outline: none;
}

.btn:focus-visible {
    box-shadow: 0 0 0 3px rgba(45, 129, 224, 0.25);
}

.btn-primary {
    background: var(--color-accent);
    color: #fff;
    border-color: var(--color-accent);
}

.btn-primary:hover:not(:disabled) {
    background: var(--color-accent-hover);
    border-color: var(--color-accent-hover);
}

.btn-secondary {
    background: transparent;
    color: var(--color-text);
    border-color: var(--color-border);
}

.btn-secondary:hover:not(:disabled) {
    background: var(--color-bg);
    border-color: #b0bac5;
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-spinner {
    display: inline-block;
    width: 14px;
    height: 14px;
    border: 2px solid rgba(255,255,255,0.4);
    border-top-color: #fff;
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
    flex-shrink: 0;
}

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

/* ─── Toast notifications ───────────────────────────────────────────────────── */

.toast-container {
    position: fixed;
    top: 16px;
    right: 16px;
    z-index: 9999;
    display: flex;
    flex-direction: column;
    gap: 8px;
    pointer-events: none;
}

.toast {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    background: #fff;
    border-radius: var(--radius-md);
    padding: 12px 16px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.14);
    border: 1px solid var(--color-border);
    max-width: 360px;
    pointer-events: all;
    animation: toastIn 0.25s ease;
}

.toast.success {
    border-left: 4px solid #43a047;
}

.toast.error {
    border-left: 4px solid var(--color-danger);
}

.toast-icon {
    font-size: 18px;
    flex-shrink: 0;
    line-height: 1;
    margin-top: 1px;
}

.toast-body {
    flex: 1;
    min-width: 0;
}

.toast-title {
    font-size: 13px;
    font-weight: 700;
    color: var(--color-text);
    margin-bottom: 2px;
}

.toast-message {
    font-size: 12px;
    color: var(--color-text-muted);
    line-height: 1.4;
}

@keyframes toastIn {
    from { opacity: 0; transform: translateX(24px); }
    to   { opacity: 1; transform: translateX(0); }
}

/* ─── Access denied screen ──────────────────────────────────────────────────── */

#screen-access-denied {
    display: none;
    text-align: center;
    padding: 60px 24px 40px;
}

#screen-access-denied .denied-icon {
    font-size: 56px;
    margin-bottom: 20px;
}

#screen-access-denied h2 {
    font-size: 18px;
    font-weight: 700;
    color: var(--color-text);
    margin-bottom: 10px;
}

#screen-access-denied p {
    font-size: 14px;
    color: var(--color-text-muted);
    max-width: 340px;
    margin: 0 auto 24px;
    line-height: 1.55;
}

/* ─── Loading screen ────────────────────────────────────────────────────────── */

#screen-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 200px;
    gap: 12px;
    font-size: 14px;
    color: var(--color-text-muted);
}

.loading-spinner {
    width: 24px;
    height: 24px;
    border: 3px solid var(--color-border);
    border-top-color: var(--color-accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    flex-shrink: 0;
}

/* ─── Main screen (hidden until loaded) ─────────────────────────────────────── */

#screen-main {
    display: none;
}

/* ─── Responsive ────────────────────────────────────────────────────────────── */

@media (max-width: 767px) {
    .settings-wrapper {
        padding: 12px 10px 32px;
    }

    .settings-container {
        width: 100%;
        max-width: 100%;
    }

    .settings-header {
        align-items: flex-start;
        flex-wrap: wrap;
        gap: 10px;
    }

    .settings-header h1 {
        font-size: 17px;
    }

    .card,
    .preview-card,
    .ann-list-panel {
        padding: 14px;
    }

    .admin-layout {
        grid-template-columns: 1fr;
        gap: 14px;
    }

    .ann-list-panel {
        position: static;
        top: auto;
    }

    .ann-list-header {
        gap: 10px;
        align-items: stretch;
        flex-direction: column;
    }

    .ann-list-header .btn {
        width: 100%;
        justify-content: center;
    }

    .color-row {
        flex-direction: column;
        gap: 12px;
    }

    .color-group,
    .color-input-wrap,
    .color-text-input {
        width: 100%;
    }

    .switch-row {
        align-items: flex-start;
        flex-wrap: wrap;
    }

    .disk-upload-row {
        flex-direction: column;
        align-items: stretch;
    }

    .disk-file-label,
    .btn-upload,
    .disk-url-input {
        width: 100%;
    }

    .disk-image-preview,
    .disk-image-preview img {
        max-width: 100%;
        width: 100%;
    }

    .disk-url-fallback {
        flex-direction: column;
        align-items: stretch;
        gap: 6px;
    }

    .dept-tree {
        max-height: 260px;
    }

    .dept-tree-item {
        align-items: flex-start;
    }

    .form-actions {
        flex-direction: column-reverse;
        gap: 8px;
    }

    .btn {
        width: 100%;
        justify-content: center;
    }
}

@media (min-width: 768px) {
    .settings-container {
        padding: 0;
    }

    .admin-layout {
        grid-template-columns: minmax(240px, 280px) minmax(0, 1fr);
    }
}

/* ─── Viewer screen ─────────────────────────────────────────────────────────── */

#screen-viewer {
    display: none;
}

/* Viewer card wrapper */
.viewer-card {
    margin-bottom: 16px;
}

/* Inner card — mirrors widget look but with full text */
.viewer-inner {
    background: var(--color-surface);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-card);
    border: 1px solid var(--color-border);
    padding: 24px;
    box-sizing: border-box;
}

.viewer-image {
    width: 100%;
    overflow: hidden;
    border-radius: var(--radius-md);
    line-height: 0;
    margin-bottom: 16px;
}

.viewer-image img {
    width: 100%;
    height: auto;
    max-height: 320px;
    object-fit: cover;
    display: block;
    border-radius: var(--radius-md);
}

.viewer-text-block {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.viewer-title {
    font-size: 20px;
    font-weight: 700;
    line-height: 1.3;
    margin: 0;
    color: inherit;
    word-break: break-word;
}

.viewer-text {
    font-size: 15px;
    line-height: 1.65;
    color: inherit;
    opacity: 0.9;
    word-break: break-word;
    overflow-wrap: anywhere;
    /* Full text — no line clamp */
    white-space: pre-line;
}

.viewer-link-btn {
    align-self: flex-start;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

/* Empty state inside viewer */
.viewer-empty {
    text-align: center;
    padding: 48px 24px;
}

.viewer-empty .denied-icon {
    font-size: 48px;
    margin-bottom: 16px;
}

.viewer-empty p {
    font-size: 15px;
    color: var(--color-text-muted);
}

/* Admin preview button in main settings header */
#btn-admin-preview {
    margin-left: auto;
}

@media (max-width: 480px) {
    .viewer-inner {
        padding: 16px;
    }
    .viewer-title {
        font-size: 17px;
    }
    .viewer-text {
        font-size: 14px;
    }
}
