/* ============================================================
   YaraPlus Support Mini App - style.css
   Persian RTL styling with Telegram theme variables
   and dedicated agent dashboard layout.
   ============================================================ */

:root {
    --tg-bg: #ffffff;
    --tg-text: #111418;
    --tg-hint: #707579;
    --tg-link: #2481cc;
    --tg-border: #e7e8ec;
    --tg-section-bg: #ffffff;
    --tg-accent: #2481cc;
    --tg-destructive: #e53935;
    --shadow: 0 2px 8px rgba(0, 0, 0, 0.06);

    --stat-blue: #2481cc;
    --stat-green: #4caf50;
    --stat-amber: #f5a623;
    --stat-purple: #9c27b0;
    --stat-cyan: #00bcd4;
    --stat-red: #e53935;
}

@media (prefers-color-scheme: dark) {
    :root {
        --tg-bg: #18222d;
        --tg-text: #ffffff;
        --tg-hint: #8d979d;
        --tg-border: #2c3a44;
        --tg-section-bg: #1d2733;
        --tg-accent: #5ab3f3;
        --shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
    }
}

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

html, body {
    /* Vazirmatn-style fallback for clean Persian rendering */
    font-family: "Vazirmatn", "IRANSans", -apple-system, BlinkMacSystemFont,
                 "Segoe UI", Tahoma, "Helvetica Neue", Arial, sans-serif;
    background: var(--tg-bg);
    color: var(--tg-text);
    line-height: 1.7; /* taller for better Persian readability */
    direction: rtl;
    overflow-x: hidden;
    padding-bottom: 50px;
    font-size: 15px;
    -webkit-font-smoothing: antialiased;
}

/* HEADER */
.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px;
    background: var(--tg-section-bg);
    box-shadow: var(--shadow);
    position: sticky;
    top: 0;
    z-index: 100;
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-direction: row-reverse; /* logo on right in RTL */
}

.brand .logo {
    width: 40px;
    height: 40px;
    background: linear-gradient(135deg, #2481cc, #5ab3f3);
    color: white;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    font-size: 22px;
}

.brand h1 {
    font-size: 18px;
    font-weight: 700;
    margin: 0;
    text-align: right;
}

.brand .tag {
    color: var(--tg-hint);
    font-size: 12px;
    text-align: right;
    display: block;
}

.user-chip {
    background: var(--tg-border);
    padding: 6px 12px;
    border-radius: 16px;
    font-size: 13px;
    color: var(--tg-text);
    max-width: 160px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* TABS */
.tab-nav {
    display: flex;
    overflow-x: auto;
    background: var(--tg-section-bg);
    border-bottom: 1px solid var(--tg-border);
    padding: 0 8px;
    position: sticky;
    top: 72px;
    z-index: 99;
    scrollbar-width: none;
}
.tab-nav::-webkit-scrollbar { display: none; }

.tab {
    flex: 1;
    min-width: 100px;
    padding: 12px 8px;
    border: none;
    background: none;
    color: var(--tg-hint);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    white-space: nowrap;
    border-bottom: 2px solid transparent;
    transition: color 0.2s, border-color 0.2s;
}

.tab.active {
    color: var(--tg-accent);
    border-bottom: 2px solid var(--tg-accent);
}

.tab-panel {
    display: none;
    padding: 16px;
}
.tab-panel.active { display: block; }

/* PERIOD PICKER */
.period-picker {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
    flex-direction: row-reverse;
}
.period-btn {
    flex: 1;
    padding: 10px;
    background: var(--tg-section-bg);
    border: 1px solid var(--tg-border);
    border-radius: 10px;
    color: var(--tg-text);
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
}
.period-btn.active {
    background: var(--tg-accent);
    color: white;
    border-color: var(--tg-accent);
}

/* STAT GRID */
.stat-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
    margin-bottom: 24px;
}
@media (min-width: 720px) {
    .stat-grid { grid-template-columns: repeat(4, 1fr); }
}

.stat-card {
    background: var(--tg-section-bg);
    padding: 16px;
    border-radius: 14px;
    box-shadow: var(--shadow);
    position: relative;
    overflow: hidden;
}
.stat-card::before {
    content: "";
    position: absolute;
    right: 0; top: 0; /* RTL: accent bar on right */
    width: 4px; height: 100%;
}
.stat-blue::before { background: var(--stat-blue); }
.stat-green::before { background: var(--stat-green); }
.stat-amber::before { background: var(--stat-amber); }
.stat-purple::before { background: var(--stat-purple); }
.stat-cyan::before { background: var(--stat-cyan); }
.stat-red::before { background: var(--stat-red); }

.stat-label {
    color: var(--tg-hint);
    font-size: 13px;
    margin-bottom: 6px;
}
.stat-value {
    font-size: 24px;
    font-weight: 700;
}

/* CHARTS */
.chart-wrap {
    background: var(--tg-section-bg);
    padding: 16px;
    border-radius: 14px;
    margin-bottom: 16px;
    box-shadow: var(--shadow);
}
.chart-wrap h3 {
    margin-bottom: 12px;
    font-size: 15px;
    font-weight: 600;
    color: var(--tg-text);
    text-align: right;
}

/* AGENT DASHBOARD */
.agent-welcome {
    background: linear-gradient(135deg, rgba(36, 129, 204, 0.1), rgba(90, 179, 243, 0.05));
    padding: 16px;
    border-radius: 14px;
    margin-bottom: 16px;
    text-align: right;
}
.agent-welcome h2 {
    font-size: 17px;
    margin-bottom: 4px;
}
.agent-welcome p {
    color: var(--tg-hint);
    font-size: 13px;
}

.quick-actions {
    background: var(--tg-section-bg);
    padding: 16px;
    border-radius: 14px;
    margin-bottom: 16px;
    box-shadow: var(--shadow);
}
.quick-actions h3 {
    margin-bottom: 12px;
    text-align: right;
    font-size: 15px;
}
.action-row {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    flex-direction: row-reverse;
}
.action-row .btn, .action-row a.btn {
    flex: 1;
    min-width: 140px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
}

.agent-section {
    background: var(--tg-section-bg);
    padding: 16px;
    border-radius: 14px;
    margin-bottom: 16px;
    box-shadow: var(--shadow);
}
.agent-section h3 {
    margin-bottom: 12px;
    text-align: right;
    font-size: 15px;
}
.agent-section .hint {
    color: var(--tg-hint);
    font-size: 12px;
    margin-top: 8px;
    text-align: right;
}

.mini-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.mini-list .mini-item {
    padding: 12px;
    border-radius: 10px;
    background: var(--tg-bg);
    border: 1px solid var(--tg-border);
    cursor: pointer;
    transition: background 0.15s;
}
.mini-list .mini-item:hover { background: rgba(36, 129, 204, 0.05); }
.mini-list .mini-item .top {
    display: flex;
    justify-content: space-between;
    margin-bottom: 4px;
    font-size: 14px;
    font-weight: 600;
}
.mini-list .mini-item .sub {
    color: var(--tg-hint);
    font-size: 12px;
}

/* CHATS LAYOUT */
.layout-split {
    display: flex;
    flex-direction: column;
    gap: 12px;
}
@media (min-width: 900px) {
    .layout-split {
        flex-direction: row;
        height: 70vh;
    }
    .chat-list { width: 320px; }
    .chat-window { flex: 1; }
}

.chat-list {
    background: var(--tg-section-bg);
    border-radius: 12px;
    box-shadow: var(--shadow);
    padding: 8px;
    max-height: 60vh;
    overflow-y: auto;
}

.chat-item {
    padding: 12px;
    border-radius: 10px;
    cursor: pointer;
    border: 1px solid transparent;
    transition: background 0.15s;
}
.chat-item:hover { background: rgba(36, 129, 204, 0.05); }
.chat-item.active { background: rgba(36, 129, 204, 0.12); border-color: var(--tg-accent); }

.chat-item .top-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 4px;
}
.chat-item .name { font-weight: 600; font-size: 14px; }
.chat-item .time { color: var(--tg-hint); font-size: 11px; }
.chat-item .preview { color: var(--tg-hint); font-size: 12px; }
.chat-item .status-badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 10px;
    font-size: 10px;
    margin-right: 4px; /* RTL */
}
.badge-active { background: #4caf50; color: white; }
.badge-queued { background: #f5a623; color: white; }
.badge-closed { background: #9e9e9e; color: white; }

.chat-window {
    background: var(--tg-section-bg);
    border-radius: 12px;
    box-shadow: var(--shadow);
    padding: 16px;
    min-height: 60vh;
    display: flex;
    flex-direction: column;
}

.empty-state, .loading {
    color: var(--tg-hint);
    text-align: center;
    padding: 32px 16px;
}

.messages-stream {
    flex: 1;
    overflow-y: auto;
    margin-bottom: 12px;
    padding: 4px;
}

.message-bubble {
    max-width: 80%;
    padding: 10px 14px;
    border-radius: 14px;
    margin-bottom: 8px;
    word-wrap: break-word;
    font-size: 14px;
    clear: both;
}
.message-bubble.user {
    background: var(--tg-accent);
    color: white;
    float: left; /* user messages on left in RTL = right side reading */
    border-bottom-left-radius: 4px;
}
.message-bubble.agent {
    background: var(--tg-border);
    color: var(--tg-text);
    float: right;
    border-bottom-right-radius: 4px;
}
.message-bubble .meta {
    font-size: 10px;
    color: rgba(255, 255, 255, 0.7);
    margin-top: 4px;
}
.message-bubble.agent .meta {
    color: var(--tg-hint);
}

.chat-input-row {
    display: flex;
    gap: 8px;
    flex-direction: row-reverse;
}
.chat-input-row input {
    flex: 1;
    padding: 10px 14px;
    border-radius: 18px;
    border: 1px solid var(--tg-border);
    background: var(--tg-bg);
    color: var(--tg-text);
    font-size: 14px;
    text-align: right;
}
.chat-input-row button {
    padding: 10px 16px;
    border: none;
    background: var(--tg-accent);
    color: white;
    border-radius: 18px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
}

/* TICKETS */
.filter-bar {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
    flex-wrap: wrap;
    flex-direction: row-reverse;
}
.filter-bar select,
.filter-bar input {
    padding: 8px 12px;
    border-radius: 10px;
    border: 1px solid var(--tg-border);
    background: var(--tg-section-bg);
    color: var(--tg-text);
    font-size: 14px;
}

.btn {
    padding: 8px 14px;
    border-radius: 10px;
    border: 1px solid var(--tg-border);
    background: var(--tg-section-bg);
    color: var(--tg-text);
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    transition: background 0.15s;
    text-align: center;
}
.btn:hover { background: rgba(0,0,0,0.04); }
.btn-primary { background: var(--tg-accent); color: white; border-color: var(--tg-accent); }
.btn-secondary { background: var(--tg-section-bg); color: var(--tg-accent); border-color: var(--tg-accent); }
.btn-back { margin-bottom: 12px; }

.ticket-list, .agent-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.ticket-card, .agent-card {
    background: var(--tg-section-bg);
    padding: 14px;
    border-radius: 12px;
    box-shadow: var(--shadow);
    cursor: pointer;
    transition: transform 0.1s;
    text-align: right;
}
.ticket-card:hover { transform: translateY(-1px); }
.ticket-card .tnum { font-weight: 700; color: var(--tg-accent); font-size: 13px; }
.ticket-card .subj { font-weight: 600; margin: 4px 0; font-size: 15px; }
.ticket-card .row {
    display: flex;
    gap: 8px;
    align-items: center;
    font-size: 12px;
    color: var(--tg-hint);
    flex-wrap: wrap;
    flex-direction: row-reverse;
}

.pill {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 10px;
    font-size: 10px;
    font-weight: 600;
}
.pill.high { background: #ffeaea; color: #e53935; }
.pill.medium { background: #fff4e0; color: #f5a623; }
.pill.low { background: #e0f7fa; color: #0097a7; }
.pill.open { background: #e3f2fd; color: #1976d2; }
.pill.in_progress { background: #fff8e1; color: #f57c00; }
.pill.closed { background: #f1f1f1; color: #757575; }

/* TICKET DETAIL */
.ticket-detail {
    background: var(--tg-section-bg);
    padding: 16px;
    border-radius: 12px;
    box-shadow: var(--shadow);
    text-align: right;
}
.ticket-detail h2 {
    margin-bottom: 8px;
}
.ticket-detail dl {
    display: grid;
    grid-template-columns: 120px 1fr;
    gap: 8px 12px;
    margin: 12px 0;
}
.ticket-detail dt { color: var(--tg-hint); font-size: 13px; }
.ticket-detail dd { font-size: 14px; }

.status-update-bar {
    margin-top: 16px;
    padding-top: 12px;
    border-top: 1px solid var(--tg-border);
    display: flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
    flex-direction: row-reverse;
}
.status-update-bar select,
.status-update-bar button {
    padding: 6px 12px;
    border-radius: 8px;
    border: 1px solid var(--tg-border);
    background: var(--tg-bg);
    color: var(--tg-text);
    font-size: 13px;
}

/* REPORTS */
.report-content {
    background: var(--tg-section-bg);
    padding: 16px;
    border-radius: 12px;
    box-shadow: var(--shadow);
    text-align: right;
}
.report-content h3 { margin-bottom: 8px; }
.report-content section { margin-bottom: 20px; }
.report-content ul {
    padding-right: 20px; /* RTL: bullets on right */
    padding-left: 0;
    list-style-position: outside;
}
.report-content table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}
.report-content table th, .report-content table td {
    border: 1px solid var(--tg-border);
    padding: 6px 10px;
    text-align: right;
}

/* AGENTS (Admin only) */
.agent-card .name { font-weight: 700; font-size: 15px; }
.agent-card .info { color: var(--tg-hint); font-size: 12px; margin: 4px 0; text-align: right; }
.agent-card .actions {
    display: flex;
    gap: 8px;
    margin-top: 8px;
    flex-wrap: wrap;
    flex-direction: row-reverse;
}
.agent-card .actions button {
    padding: 4px 10px;
    border-radius: 8px;
    border: 1px solid var(--tg-border);
    background: var(--tg-bg);
    color: var(--tg-text);
    cursor: pointer;
    font-size: 12px;
}
.agent-card .actions .danger {
    border-color: var(--tg-destructive);
    color: var(--tg-destructive);
}

.modal-form {
    position: fixed;
    bottom: 0;
    left: 0; right: 0;
    background: var(--tg-section-bg);
    padding: 16px;
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -2px 12px rgba(0,0,0,0.15);
    z-index: 200;
    text-align: right;
}
.modal-form h3 { margin-bottom: 8px; }
.modal-form input {
    width: 100%;
    padding: 10px;
    border-radius: 10px;
    border: 1px solid var(--tg-border);
    background: var(--tg-bg);
    color: var(--tg-text);
    margin-bottom: 8px;
    font-size: 15px;
    text-align: right;
}
.modal-form .btn { width: 100%; margin-bottom: 4px; }

/* USERS LIST (for the Add Agent modal) */
.user-list {
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.user-list .user-item {
    padding: 10px 12px;
    border-radius: 10px;
    background: var(--tg-bg);
    border: 1px solid var(--tg-border);
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    transition: background 0.15s;
}
.user-list .user-item:hover {
    background: rgba(36, 129, 204, 0.08);
    border-color: var(--tg-accent);
}
.user-list .user-item .user-info {
    text-align: right;
}
.user-list .user-item .user-info .name {
    font-weight: 600;
    font-size: 14px;
}
.user-list .user-item .user-info .sub {
    color: var(--tg-hint);
    font-size: 12px;
}
.user-list .user-item .add-btn {
    background: var(--tg-accent);
    color: white;
    border: none;
    border-radius: 8px;
    padding: 6px 12px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
}
.user-list .user-item .add-btn:hover {
    opacity: 0.85;
}
.user-list .user-item .add-btn.already {
    background: var(--tg-border);
    color: var(--tg-hint);
    cursor: not-allowed;
}

/* Search box */
input[type="search"] {
    width: 100%;
    padding: 10px 14px;
    border-radius: 10px;
    border: 1px solid var(--tg-border);
    background: var(--tg-section-bg);
    color: var(--tg-text);
    font-size: 14px;
    text-align: right;
}
input[type="search"]::placeholder {
    color: var(--tg-hint);
}
input[type="search"]:focus {
    outline: none;
    border-color: var(--tg-accent);
    box-shadow: 0 0 0 2px rgba(36, 129, 204, 0.15);
}

/* Modal-form close button */
.modal-form .btn {
    width: 100%;
    margin-bottom: 4px;
}

/* FOOTER */
.app-footer {
    position: fixed;
    bottom: 0;
    left: 0; right: 0;
    background: var(--tg-section-bg);
    border-top: 1px solid var(--tg-border);
    padding: 8px 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 12px;
    color: var(--tg-hint);
    z-index: 100;
    flex-direction: row-reverse;
}

#footer-status.online { color: #4caf50; }
#footer-status.offline { color: #e53935; }
