/* ==========================================================================
   "Yangi FreePBX Bog'lash" (tenant) modal - compact, sectioned, responsive
   Structure: sticky header / scrollable body (only if it does not fit) / sticky footer
   ========================================================================== */

/* Above the floating chat widget (also z-index 1000, later in the DOM) so it never covers the dialog */
#tenant-modal { z-index: 1100; }

#tenant-modal .tm-dialog {
    --tm-radius: 14px;
    --tm-field-h: 40px;
    display: flex;
    flex-direction: column;
    /* % (of the full-screen .modal) instead of vw/vh: the app applies html{zoom} on laptops, which vh/vw ignore */
    width: min(850px, 90%);
    max-width: 850px;
    max-height: 90%;
    padding: 0;
    overflow: hidden;
    border: 1px solid var(--border-color, #2d3348);
    border-radius: var(--tm-radius);
    background: var(--bg-color, #0f1219);
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.6);
    text-align: left;
}

/* Header -------------------------------------------------------------------- */
#tenant-modal .tm-header {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 14px 20px;
    border-bottom: 1px solid var(--border-color, #2d3348);
    background: var(--bg-color, #0f1219);
}

#tenant-modal .tm-title {
    margin: 0;
    min-width: 0;
    font-size: 1.1rem;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
#tenant-modal .tm-title i { margin-right: 10px; color: var(--accent-color, #3b82f6); }

#tenant-modal .tm-close {
    flex: none;
    width: 32px;
    height: 32px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: var(--text-secondary, #94a3b8);
    font-size: 1.5rem;
    line-height: 1;
    cursor: pointer;
    transition: background 0.15s ease, color 0.15s ease;
}
#tenant-modal .tm-close:hover { background: rgba(255, 255, 255, 0.07); color: var(--danger-color, #ef4444); }

/* Form: body scrolls, footer stays put ------------------------------------- */
#tenant-modal .tm-form {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

#tenant-modal .tm-body {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 14px 20px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scrollbar-width: thin;
    scrollbar-color: #3a4160 transparent;
}
#tenant-modal .tm-body::-webkit-scrollbar { width: 8px; }
#tenant-modal .tm-body::-webkit-scrollbar-thumb { background: #3a4160; border-radius: 8px; }

#tenant-modal .tm-footer {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px 20px;
    border-top: 1px solid var(--border-color, #2d3348);
    background: var(--bg-color, #0f1219);
}

/* Sections ------------------------------------------------------------------ */
#tenant-modal .tm-section {
    --tm-tint: var(--accent-color, #3b82f6);
    flex: none;
    padding: 12px 14px 14px;
    border: 1px solid var(--border-color, #2d3348);
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.025);
}
#tenant-modal .tm-section--db    { --tm-tint: #60a5fa; }
#tenant-modal .tm-section--admin { --tm-tint: #4ade80; border-color: rgba(34, 197, 94, 0.28); background: rgba(34, 197, 94, 0.04); }
#tenant-modal .tm-section--ami   { --tm-tint: #fbbf24; }
#tenant-modal .tm-section--extra { --tm-tint: #a78bfa; padding: 0; }

#tenant-modal .tm-section__head {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 0 0 10px;
    font-size: 0.86rem;
    font-weight: 600;
    color: var(--text-primary, #fff);
}
#tenant-modal .tm-section__head > i {
    width: 24px;
    height: 24px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 7px;
    font-size: 0.78rem;
    color: var(--tm-tint);
    background: color-mix(in srgb, var(--tm-tint) 16%, transparent);
}
#tenant-modal .tm-section__hint {
    margin-left: auto;
    font-size: 0.72rem;
    font-weight: 400;
    color: var(--text-secondary, #94a3b8);
}

/* Grid ---------------------------------------------------------------------- */
#tenant-modal .tm-grid { display: grid; gap: 10px 12px; }
#tenant-modal .tm-grid--2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
#tenant-modal .tm-grid--3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
#tenant-modal .tm-span-2 { grid-column: span 2; }
#tenant-modal .tm-span-all { grid-column: 1 / -1; }

/* Fields -------------------------------------------------------------------- */
#tenant-modal .tm-field { min-width: 0; }
#tenant-modal .tm-label {
    display: block;
    margin: 0 0 4px;
    font-size: 0.76rem;
    font-weight: 500;
    text-align: left;
    color: var(--text-secondary, #94a3b8);
}
#tenant-modal .tm-label .req { color: #ef4444; }
#tenant-modal .tm-label i { margin-right: 4px; }

#tenant-modal .tm-input {
    display: block;
    width: 100%;
    height: var(--tm-field-h);
    padding: 0 12px;
    border-radius: 8px;
    font-size: 0.88rem;
    color-scheme: dark;
    box-sizing: border-box;
}
#tenant-modal .tm-input:focus {
    border-color: var(--accent-color, #3b82f6);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.18);
}
#tenant-modal .tm-input:user-invalid { border-color: #ef4444; box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.15); }
#tenant-modal .tm-input[readonly] { opacity: 0.65; cursor: not-allowed; }

#tenant-modal select.tm-input {
    appearance: none;
    -webkit-appearance: none;
    padding-right: 32px;
    cursor: pointer;
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'><polyline points='6 9 12 15 18 9'/></svg>");
    background-repeat: no-repeat;
    background-position: right 11px center;
}

#tenant-modal textarea.tm-input {
    height: auto;
    min-height: 60px;
    padding: 9px 12px;
    resize: vertical;
    line-height: 1.4;
    font-family: inherit;
}

/* Password field with show/hide icon (JS toggles fa-eye / fa-eye-slash) ------- */
#tenant-modal .tm-pass { position: relative; }
#tenant-modal .tm-pass .tm-input { padding-right: 40px; }
#tenant-modal .tm-pass .toggle-pass-visibility {
    position: absolute;
    top: 50%;
    right: 4px;
    transform: translateY(-50%);
    width: 32px;
    height: 32px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    font-size: 0.85rem;
    color: var(--text-secondary, #94a3b8);
    opacity: 0.75;
    cursor: pointer;
    transition: opacity 0.15s ease, background 0.15s ease;
}
#tenant-modal .tm-pass .toggle-pass-visibility:hover { opacity: 1; background: rgba(255, 255, 255, 0.07); }

/* Connection test: secondary action + inline result ---------------------------- */
#tenant-modal .tm-testrow {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-top: 10px;
}
#tenant-modal .tm-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    height: 40px;
    padding: 0 18px;
    border-radius: 10px;
    font-family: inherit;
    font-size: 0.88rem;
    font-weight: 600;
    cursor: pointer;
    border: 1px solid transparent;
    transition: background 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease, transform 0.1s ease;
}
#tenant-modal .tm-btn:focus-visible { outline: 2px solid #93c5fd; outline-offset: 2px; }
#tenant-modal .tm-btn--secondary {
    height: 36px;
    flex: none;
    padding: 0 14px;
    font-weight: 500;
    font-size: 0.84rem;
    color: #60a5fa;
    background: rgba(59, 130, 246, 0.1);
    border-color: rgba(59, 130, 246, 0.3);
}
#tenant-modal .tm-btn--secondary:hover { background: rgba(59, 130, 246, 0.18); border-color: rgba(59, 130, 246, 0.5); }
#tenant-modal .tm-btn--ghost {
    color: var(--text-secondary, #94a3b8);
    background: transparent;
    border-color: var(--border-color, #2d3348);
}
#tenant-modal .tm-btn--ghost:hover { color: var(--text-primary, #fff); background: rgba(255, 255, 255, 0.06); border-color: #3a4160; }
#tenant-modal .tm-btn--primary {
    min-width: 160px;
    color: #fff;
    background: var(--accent-color, #3b82f6);
    box-shadow: 0 8px 16px -6px rgba(59, 130, 246, 0.55);
}
#tenant-modal .tm-btn--primary:hover { background: var(--accent-hover, #2563eb); transform: translateY(-1px); }

/* JS writes plain text + colour here (success / warning / error) */
#tenant-modal .tm-testresult {
    flex: 1 1 auto;
    min-width: 0;
    min-height: 18px;
    font-size: 0.8rem;
    font-weight: 500;
    line-height: 1.3;
}

/* Active checkbox ----------------------------------------------------------------- */
#tenant-modal .tm-check {
    display: flex;
    align-items: center;
    gap: 10px;
    height: var(--tm-field-h);
    padding: 0 14px;
    border: 1px solid var(--border-color, #2d3348);
    border-radius: 8px;
    background: #151926;
    color: var(--text-primary, #fff);
    font-size: 0.88rem;
    font-weight: 500;
    cursor: pointer;
    box-sizing: border-box;
}
#tenant-modal .tm-check input { width: 16px; height: 16px; margin: 0; cursor: pointer; accent-color: var(--accent-color, #3b82f6); }

/* Collapsible "extra settings" ------------------------------------------------------ */
#tenant-modal .tm-collapse > summary {
    list-style: none;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 11px 14px;
    cursor: pointer;
    user-select: none;
    border-radius: 12px;
}
#tenant-modal .tm-collapse > summary::-webkit-details-marker { display: none; }
#tenant-modal .tm-collapse > summary:hover { background: rgba(255, 255, 255, 0.03); }
#tenant-modal .tm-collapse > summary:focus-visible { outline: 2px solid #93c5fd; outline-offset: -2px; }
#tenant-modal .tm-collapse > summary .tm-section__head { margin: 0; flex: 1 1 auto; min-width: 0; }
#tenant-modal .tm-collapse .tm-chevron {
    flex: none;
    font-size: 0.75rem;
    color: var(--text-secondary, #94a3b8);
    transition: transform 0.2s ease;
}
#tenant-modal .tm-collapse[open] .tm-chevron { transform: rotate(180deg); }
#tenant-modal .tm-collapse[open] > summary { border-bottom-left-radius: 0; border-bottom-right-radius: 0; }
#tenant-modal .tm-collapse__body { padding: 4px 14px 14px; }

/* Responsive ----------------------------------------------------------------------------- */
/* Tablet: 2 columns */
@media (max-width: 820px) {
    #tenant-modal .tm-grid--3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    #tenant-modal .tm-span-2 { grid-column: 1 / -1; }
}

/* Mobile: 1 column, wider dialog, body scrolls, footer stays visible */
@media (max-width: 560px) {
    #tenant-modal .tm-dialog { width: 95%; max-height: 94%; }
    #tenant-modal .tm-header, #tenant-modal .tm-footer { padding-left: 14px; padding-right: 14px; }
    #tenant-modal .tm-body { padding: 12px 14px; }
    #tenant-modal .tm-grid--2, #tenant-modal .tm-grid--3 { grid-template-columns: minmax(0, 1fr); }
    #tenant-modal .tm-testrow { flex-direction: column; align-items: stretch; gap: 8px; }
    #tenant-modal .tm-btn--secondary { width: 100%; }
    #tenant-modal .tm-testresult { text-align: center; }
    #tenant-modal .tm-footer .tm-btn { flex: 1 1 0; min-width: 0; }
}

/* Short laptop screens: tighten vertical rhythm so more fits without scrolling */
@media (max-height: 800px) and (min-width: 561px) {
    #tenant-modal .tm-body { padding-top: 10px; padding-bottom: 10px; gap: 10px; }
    #tenant-modal .tm-section { padding-top: 10px; padding-bottom: 12px; }
    #tenant-modal .tm-section__head { margin-bottom: 8px; }
    #tenant-modal .tm-header { padding-top: 10px; padding-bottom: 10px; }
    #tenant-modal .tm-footer { padding-top: 10px; padding-bottom: 10px; }
}

@media (prefers-reduced-motion: reduce) {
    #tenant-modal .tm-btn, #tenant-modal .tm-chevron, #tenant-modal .tm-close { transition: none; }
}

/* Stored-secret state: label row with "saqlangan" badge and the explicit clear action ---------- */
#tenant-modal [hidden] { display: none !important; }
#tenant-modal .tm-labelrow {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 2px 8px;
    margin-bottom: 4px;
}
#tenant-modal .tm-labelrow .tm-label { margin: 0; }
#tenant-modal .tm-stored { font-size: 0.7rem; font-weight: 500; color: #4ade80; white-space: nowrap; }
#tenant-modal .tm-stored i { margin-right: 3px; font-size: 0.65rem; }
#tenant-modal .tm-clear {
    margin-left: auto;
    padding: 2px 6px;
    border: 0;
    border-radius: 6px;
    background: transparent;
    color: #f87171;
    font-family: inherit;
    font-size: 0.7rem;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.15s ease;
}
#tenant-modal .tm-clear:hover { background: rgba(239, 68, 68, 0.14); }
#tenant-modal .tm-clear:focus-visible { outline: 2px solid #fca5a5; outline-offset: 1px; }

/* Narrow (3-column) fields: keep the label row on ONE line so inputs stay aligned - clear action becomes icon-only (title tooltip) */
#tenant-modal .tm-labelrow { flex-wrap: nowrap; min-height: 20px; }
#tenant-modal .tm-grid--3 .tm-clear__text { display: none; }
@media (max-width: 560px) {
    #tenant-modal .tm-grid--3 .tm-clear__text { display: inline; }
}
