/* ==========================================================================
   کامپوننت‌ها: کارت، دکمه، چیپ، نشان، فرم، مودال، سگمنت، خالی‌حالت، توست
   ========================================================================== */

/* ---------------------------------------------------------------- کارت */
.card {
    min-width: 0;          /* جلوگیری از سرریز در گرید/فلکس (RTL) */
    box-sizing: border-box;
    /* عرض کارت همیشه از ظرف تبعیت می‌کند، نه از محتوای فرزندان. */
    contain: inline-size;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    overflow: hidden;
}

.card-pad {
    padding: 16px;
}

.card-hover {
    transition: transform var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}

.card-hover:active {
    transform: scale(.985);
}

.card-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 14px 16px 10px;
}

.card-title {
    font-size: .98rem;
    font-weight: 700;
}

.section {
    margin-top: 18px;
}

.section-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 0 4px 10px;
}

.section-title {
    font-size: 1rem;
    font-weight: 700;
    display: flex;
    align-items: center;
    gap: 8px;
}

.section-title .count {
    font-size: .72rem;
    font-weight: 700;
    color: var(--brand-600);
    background: var(--brand-50);
    border: 1px solid var(--brand-100);
    border-radius: var(--radius-pill);
    padding: 1px 8px;
}

/* ---------------------------------------------------------------- دکمه‌ها */
.btn {
    --btn-bg: var(--surface);
    --btn-fg: var(--text);
    --btn-bd: var(--border);

    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    /* min-width:0 و max-width:100% ضروری‌اند: دکمه inline-flex است و بدون آن
       عرض max-content متن، کارت/صفحه را از عرض موبایل بیرون می‌زند. */
    min-width: 0;
    max-width: 100%;
    box-sizing: border-box;
    min-height: 46px;
    padding: 10px 18px;
    border-radius: var(--radius);
    border: 1px solid var(--btn-bd);
    background: var(--btn-bg);
    color: var(--btn-fg);
    font-weight: 600;
    font-size: .95rem;
    line-height: 1.2;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    transition: background var(--dur) var(--ease), border-color var(--dur) var(--ease),
        transform .12s var(--ease), opacity var(--dur) var(--ease);
    user-select: none;
    -webkit-user-select: none;
}

.btn:active {
    transform: scale(.97);
}

.btn[disabled],
.btn.is-busy {
    opacity: .6;
    pointer-events: none;
}

.btn .icon {
    width: 20px;
    height: 20px;
}

.btn-primary {
    --btn-bg: var(--brand-600);
    --btn-fg: var(--on-brand);
    --btn-bd: var(--brand-600);
}

.btn-primary:hover {
    background: var(--brand-700);
    border-color: var(--brand-700);
}

.btn-soft {
    --btn-bg: var(--brand-50);
    --btn-fg: var(--brand-600);
    --btn-bd: var(--brand-100);
}

.btn-ghost {
    --btn-bg: transparent;
    --btn-bd: transparent;
    --btn-fg: var(--text-muted);
}

.btn-ghost:hover {
    background: var(--surface-2);
}

.btn-danger {
    --btn-bg: var(--danger-500);
    --btn-fg: #fff;
    --btn-bd: var(--danger-500);
}

.btn-danger-soft {
    --btn-bg: var(--danger-50);
    --btn-fg: var(--danger-500);
    --btn-bd: transparent;
}

.btn-block {
    width: 100%;
}

.btn-sm {
    min-height: 38px;
    padding: 6px 12px;
    font-size: .86rem;
    border-radius: var(--radius-sm);
}

.btn-lg {
    min-height: 54px;
    font-size: 1rem;
    border-radius: var(--radius-lg);
}

.icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 42px;
    height: 42px;
    border-radius: var(--radius);
    color: var(--text-muted);
    background: transparent;
    transition: background var(--dur) var(--ease), color var(--dur) var(--ease);
    position: relative;
}

.icon-btn:hover,
.icon-btn:active {
    background: var(--surface-2);
    color: var(--text);
}

.icon-btn.solid {
    background: var(--surface);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
}

.btn.is-busy::before {
    content: "";
    width: 16px;
    height: 16px;
    border: 2px solid currentColor;
    border-right-color: transparent;
    border-radius: 50%;
    animation: spin .7s linear infinite;
}

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

/* ---------------------------------------------------------------- چیپ / فیلتر */
.chips {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    padding: 4px 2px 8px;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
}

.chips::-webkit-scrollbar {
    display: none;
}

.chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 7px 14px;
    border-radius: var(--radius-pill);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text-muted);
    font-size: .85rem;
    font-weight: 500;
    white-space: nowrap;
    transition: all var(--dur) var(--ease);
    flex: 0 0 auto;
}

.chip:active {
    transform: scale(.96);
}

.chip .count {
    font-size: .72rem;
    opacity: .8;
}

.chip.is-active {
    background: var(--brand-600);
    border-color: var(--brand-600);
    color: var(--on-brand);
    font-weight: 700;
    box-shadow: 0 6px 14px -8px var(--brand-600);
}

/* ---------------------------------------------------------------- نشان (Badge) */
.badge {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 3px 9px;
    border-radius: var(--radius-pill);
    font-size: .72rem;
    font-weight: 700;
    line-height: 1.6;
    white-space: nowrap;
    background: var(--surface-3);
    color: var(--text-muted);
}

.badge .dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: currentColor;
}

.badge-green {
    background: var(--brand-50);
    color: var(--brand-600);
}

.badge-blue {
    background: var(--info-50);
    color: var(--info-500);
}

.badge-amber {
    background: var(--warning-50);
    color: var(--warning-500);
}

.badge-red {
    background: var(--danger-50);
    color: var(--danger-500);
}

.badge-gray {
    background: var(--surface-3);
    color: var(--text-muted);
}

:root[data-theme="dark"] .badge-blue {
    color: #7fb0ff;
}

:root[data-theme="dark"] .badge-amber {
    color: #f3b45e;
}

:root[data-theme="dark"] .badge-red {
    color: #ff8c8c;
}

.priority {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    font-size: .72rem;
    font-weight: 700;
    padding: 2px 8px;
    border-radius: var(--radius-pill);
}

.priority-critical {
    background: var(--danger-50);
    color: var(--danger-500);
}

.priority-high {
    background: var(--warning-50);
    color: var(--warning-500);
}

.priority-normal,
.priority-low {
    background: var(--surface-3);
    color: var(--text-muted);
}

/* ---------------------------------------------------------------- فرم */
/* فرم و کنترل‌ها نباید از کارت/صفحه بیرون بزنند (مشکل کلاسیک flex در RTL
   به‌خاطر min-width:auto). */
form, .field, .input-group, .check, .field-row {
    min-width: 0;
    max-width: 100%;
}

/* .input-group یک فلکس است؛ محتوایش (input + آیکون) نباید عرض کارت را بکشد. */
.input-group > * {
    min-width: 0;
}

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

.field-label {
    font-size: .86rem;
    font-weight: 600;
    color: var(--text-muted);
    padding-inline: 2px;
}

/* کنترل‌های متنی در RTL با جهت صفحه هم‌راستا هستند؛ مقدار ورودی (کد پرسنلی و
   رمز) با dir="ltr" در قالب چپ‌چین می‌شود بدون آنکه جای خود کنترل تغییر کند. */
.input,
.select,
.textarea {
    width: 100%;
    max-width: 100%;
    min-width: 0;
    min-height: 50px;
    text-align: start;
    padding: 12px 14px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-size: 1rem;
    /* ≥۱۶px تا iOS زوم نکند */
    transition: border-color var(--dur) var(--ease), box-shadow var(--dur) var(--ease), background var(--dur) var(--ease);
}

.textarea {
    min-height: 110px;
    resize: vertical;
    line-height: 1.8;
}

.input::placeholder,
.textarea::placeholder {
    color: var(--text-subtle);
}

.input:focus,
.select:focus,
.textarea:focus {
    outline: none;
    border-color: var(--brand-400);
    box-shadow: var(--ring);
    background: var(--bg-elevated);
}

/*
   فهرست بازشوی select در ویندوز/کروم با رنگ خودِ عنصر رنگ می‌شود؛ بدون این،
   در دارک‌مود گزینه‌ها متن تیره روی پس‌زمینهٔ تیره می‌شوند و خوانا نیستند.
*/
.select option,
.select optgroup {
    background-color: var(--surface);
    color: var(--text);
}

.select {
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 24 24' fill='none' stroke='%235c6b64' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: left 12px center;
    padding-left: 40px;
}

.input-group {
    position: relative;
    display: flex;
    align-items: center;
    width: 100%;
    box-sizing: border-box;
}

/* آیکون‌ها با inset-inline ثابت می‌شوند تا در RTL و LTR جای درست بنشینند. */
.input-group .icon {
    position: absolute;
    inset-inline-start: 14px;
    color: var(--text-subtle);
    pointer-events: none;
}

.input-group .input {
    flex: 1 1 auto;
    padding-inline-start: 44px;
}

.input-group .input-action {
    position: absolute;
    inset-inline-end: 6px;
}

.input-hint {
    font-size: .78rem;
    color: var(--text-subtle);
    padding-inline: 2px;
}

.input-error {
    color: var(--danger-500);
    font-size: .8rem;
    padding-inline: 2px;
}

.field-row {
    display: grid;
    grid-template-columns: 1fr auto;
    align-items: end;
    gap: 10px;
}

.check {
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 44px;
    max-width: 100%;
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
}

.check input {
    appearance: none;
    width: 22px;
    height: 22px;
    border-radius: 7px;
    border: 1.5px solid var(--border-strong);
    background: var(--surface);
    display: grid;
    place-items: center;
    transition: all var(--dur) var(--ease);
    flex: 0 0 auto;
}

.check input:checked {
    background: var(--brand-600);
    border-color: var(--brand-600);
}

.check input:checked::after {
    content: "";
    width: 11px;
    height: 6px;
    border-left: 2px solid var(--on-brand);
    border-bottom: 2px solid var(--on-brand);
    transform: rotate(-45deg) translateY(-1px);
}

/* ---------------------------------------------------------------- سگمنت (تب) */
.segment {
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: 1fr;
    gap: 4px;
    padding: 4px;
    background: var(--surface-3);
    border-radius: var(--radius);
    margin-bottom: 12px;
}

.segment a {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-height: 40px;
    border-radius: 12px;
    font-size: .88rem;
    font-weight: 600;
    color: var(--text-muted);
    transition: all var(--dur) var(--ease);
}

.segment a.is-active {
    background: var(--surface);
    color: var(--text);
    box-shadow: var(--shadow-sm);
}

/* ---------------------------------------------------------------- خالی‌حالت */
.empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    padding: 36px 20px;
    text-align: center;
    color: var(--text-muted);
}

.empty-icon {
    width: 64px;
    height: 64px;
    display: grid;
    place-items: center;
    border-radius: 20px;
    background: var(--surface-2);
    color: var(--brand-500);
    border: 1px solid var(--border);
}

.empty-icon .icon {
    width: 30px;
    height: 30px;
}

.empty strong {
    color: var(--text);
    font-size: .98rem;
}

.empty p {
    font-size: .85rem;
    max-width: 30ch;
}

/* ---------------------------------------------------------------- اسکلت بارگذاری */
.skeleton {
    background: linear-gradient(90deg, var(--surface-2) 25%, var(--surface-3) 37%, var(--surface-2) 63%);
    background-size: 400% 100%;
    animation: shimmer 1.4s ease infinite;
    border-radius: var(--radius-sm);
}

@keyframes shimmer {
    0% {
        background-position: 100% 50%;
    }

    100% {
        background-position: 0 50%;
    }
}

/* ---------------------------------------------------------------- مودال (Bottom Sheet در موبایل) */
.sheet {
    position: fixed;
    inset: 0;
    z-index: 90;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    visibility: hidden;
    pointer-events: none;
}

.sheet.is-open {
    visibility: visible;
    pointer-events: auto;
}

.sheet-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(8, 18, 14, .5);
    opacity: 0;
    transition: opacity var(--dur) var(--ease);
    backdrop-filter: blur(2px);
}

.sheet.is-open .sheet-backdrop {
    opacity: 1;
}

.sheet-panel {
    position: relative;
    width: 100%;
    max-width: var(--maxw);
    max-height: 88dvh;
    display: flex;
    flex-direction: column;
    background: var(--bg-elevated);
    border-radius: 24px 24px 0 0;
    box-shadow: var(--shadow-lg);
    transform: translateY(100%);
    transition: transform .28s var(--ease);
    padding-bottom: env(safe-area-inset-bottom);
}

.sheet.is-open .sheet-panel {
    transform: translateY(0);
}

.sheet-handle {
    width: 44px;
    height: 5px;
    border-radius: 99px;
    background: var(--border-strong);
    margin: 10px auto 2px;
}

.sheet-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 18px 4px;
}

.sheet-title {
    font-weight: 700;
    font-size: 1.02rem;
}

.sheet-body {
    padding: 8px 18px 18px;
    overflow-y: auto;
    overscroll-behavior: contain;
}

.sheet-foot {
    padding: 10px 18px calc(16px + env(safe-area-inset-bottom));
    border-top: 1px solid var(--border);
    display: flex;
    gap: 10px;
}

.sheet-foot .btn {
    flex: 1;
}

@media (min-width: 640px) {
    .sheet {
        align-items: center;
    }

    .sheet-panel {
        border-radius: var(--radius-lg);
        max-height: 84dvh;
        transform: translateY(16px) scale(.98);
        opacity: 0;
    }

    .sheet.is-open .sheet-panel {
        transform: none;
        opacity: 1;
    }

    .sheet-handle {
        display: none;
    }
}

/* ---------------------------------------------------------------- توست */
.toast-host {
    position: fixed;
    inset-inline: 0;
    bottom: calc(var(--nav-h) + 14px + env(safe-area-inset-bottom));
    z-index: 120;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    padding: 0 16px;
    pointer-events: none;
}

.toast {
    display: flex;
    align-items: center;
    gap: 10px;
    max-width: var(--maxw);
    width: fit-content;
    padding: 12px 16px;
    border-radius: var(--radius);
    background: #12211b;
    color: #f3faf6;
    font-size: .88rem;
    font-weight: 500;
    box-shadow: var(--shadow-lg);
    transform: translateY(14px);
    opacity: 0;
    transition: all .26s var(--ease);
    pointer-events: auto;
}

:root[data-theme="dark"] .toast {
    background: #e9f1ed;
    color: #0b1210;
}

.toast.is-visible {
    transform: none;
    opacity: 1;
}

.toast .icon {
    width: 20px;
    height: 20px;
}

.toast-success .icon {
    color: #4ade80;
}

.toast-danger .icon {
    color: #f87171;
}

/* ---------------------------------------------------------------- آواتار */
.avatar {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    display: grid;
    place-items: center;
    font-weight: 700;
    font-size: .95rem;
    background: var(--brand-100);
    color: var(--brand-700);
    border: 1px solid var(--brand-200);
    flex: 0 0 auto;
    overflow: hidden;
}

.avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.avatar-lg {
    width: 72px;
    height: 72px;
    font-size: 1.4rem;
}

/* ---------------------------------------------------------------- نوار پیشرفت */
.progress {
    height: 8px;
    border-radius: 99px;
    background: var(--surface-3);
    overflow: hidden;
}

.progress>span {
    display: block;
    height: 100%;
    border-radius: inherit;
    background: linear-gradient(90deg, var(--brand-500), var(--brand-400));
    transition: width .6s var(--ease);
}

/* ---------------------------------------------------------------- کلید/مقدار */
.kv {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 11px 0;
}

.kv+.kv {
    border-top: 1px dashed var(--border);
}

.kv-icon {
    color: var(--text-subtle);
    margin-top: 2px;
}

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

.kv-label {
    font-size: .76rem;
    color: var(--text-subtle);
}

.kv-value {
    font-weight: 500;
    font-size: .92rem;
    word-break: break-word;
    /* متن ممکن است با کلمهٔ لاتین («APN»، سریال) شروع شود؛ باید از راست بخواند. */
    text-align: start;
    unicode-bidi: plaintext;
}

/* ---------------------------------------------------------------- نوار بالای صفحه داخلی */
/*
   چیدمان ساده و قابل پیش‌بینی:
     • direction: rtl  →  محور اصلی از راست به چپ است
     • اولین فرزند DOM  →  سمت راست (دکمهٔ بازگشت)
     • عنوان با flex:1  →  وسط
     • آخرین فرزند  →  سمت چپ (اکشن، مثل «خواندن همه»)
   هیچ grid/order/row-reverse ای لازم نیست؛ هر کدام در RTL نتیجه را برعکس
   می‌کنند و همان چیزی بود که دکمه را به چپ می‌برد.
*/
.subheader {
    display: flex;
    flex-direction: row;
    align-items: center;
    direction: rtl;
    gap: 8px;
    padding: 10px 12px;
    position: sticky;
    top: 0;
    z-index: 30;
    background: color-mix(in srgb, var(--bg) 88%, transparent);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    border-bottom: 1px solid transparent;
    transition: border-color var(--dur) var(--ease);
}

.subheader.is-scrolled {
    border-bottom-color: var(--border);
}

.subheader-title {
    flex: 1 1 0;
    min-width: 0;
    font-weight: 700;
    font-size: 1rem;
    text-align: center;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.subheader .icon-btn,
.subheader > form {
    flex: 0 0 auto;
}

.subheader-back {
    margin: 0;
}

.subheader-back .icon {
    /* آیکون «back» خودش به راست اشاره می‌کند؛ آینه نمی‌شود. */
    transform: none !important;
}