/* === box-sizing شامل === */
.tk-app, .tk-app *, .tk-app *::before, .tk-app *::after,
.tk-login-wrap, .tk-login-wrap *, .tk-login-wrap *::before, .tk-login-wrap *::after {
    box-sizing: border-box;
}

.tk-app {
    --tk-bg: #ffffff;
    --tk-sidebar-bg: #f7f7f8;
    --tk-border: #e5e7eb;
    --tk-text: #111111;
    --tk-text-light: #6b7280;
    --tk-primary: #0f172a;
    --tk-bubble-ai: #f1f1f3;
    --tk-bubble-user-text: #ffffff;
    --tk-hover: rgba(0,0,0,.05);

    display: flex; width: 100%;
    height: 82vh; height: min(82vh, calc(100vh - 40px)); height: min(82dvh, calc(100dvh - 40px));
    min-height: 480px; max-height: 900px;
    border: 1px solid var(--tk-border); border-radius: 12px; overflow: hidden;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Tahoma, sans-serif;
    background: var(--tk-bg); color: var(--tk-text); position: relative;
}

.tk-app[data-theme="dark"] {
    --tk-bg: #1a1a1e; --tk-sidebar-bg: #141417; --tk-border: #2c2c31;
    --tk-text: #f0f0f0; --tk-text-light: #9ca3af; --tk-primary: #3b82f6; --tk-bubble-ai: #2a2a2f;
    --tk-hover: rgba(255,255,255,.06);
}

/* ================= الشريط الجانبي (تصميم مسطّح بدون صناديق) ================= */
.tk-sidebar {
    width: 260px; max-width: 70vw; background: var(--tk-sidebar-bg);
    border-left: 1px solid var(--tk-border); display: flex; flex-direction: column;
    padding: 14px 10px; flex-shrink: 0; overflow-y: auto; overflow-x: hidden; min-height: 0;
    transition: width .2s ease, margin-right .25s ease, padding .2s ease;
}
.tk-sidebar.collapsed { width: 0; padding-left: 0; padding-right: 0; border: none; }

.tk-sidebar-logo { text-align: center; margin-bottom: 14px; }
.tk-sidebar-logo img { max-height: var(--tk-logo-size, 56px); max-width: 100%; border-radius: 6px; }

/* === عنصر مسطّح عام: تُستخدم له "محادثة جديدة" وعناصر الأدوات، بلا خلفية أو حدود ثابتة === */
.tk-flat-item {
    display: flex; align-items: center; gap: 10px; width: 100%;
    background: none; border: none; padding: 9px 8px; border-radius: 8px;
    cursor: pointer; color: var(--tk-text); font-size: 13px; text-align: right; font-family: inherit;
}
.tk-flat-item:hover { background: var(--tk-hover); }
.tk-flat-item.active { color: #2271b1; font-weight: 700; }
.tk-flat-item .icon { font-size: 15px; flex-shrink: 0; width: 18px; text-align: center; }
.tk-flat-item.strong { font-weight: 700; }

.tk-sidebar-section { margin-bottom: 6px; }
.tk-sidebar-divider { height: 1px; background: var(--tk-border); margin: 10px 4px; }

/* === مربع بحث المحادثات: مسطّح، بلا إطار مربّع === */
.tk-sidebar-search { display: flex; align-items: center; gap: 6px; padding: 6px 8px; margin-bottom: 6px; border-bottom: 1px solid var(--tk-border); }
.tk-sidebar-search input { border: none; background: none; outline: none; font-size: 13px; color: var(--tk-text); width: 100%; font-family: inherit; }
.tk-sidebar-search .icon { color: var(--tk-text-light); font-size: 13px; }

.tk-threads-list { overflow-y: auto; flex: 1; min-height: 80px; }
.tk-thread-item { display: flex; align-items: center; gap: 6px; padding: 9px 8px; border-radius: 8px; cursor: pointer; font-size: 13px; color: var(--tk-text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tk-thread-item:hover { background: var(--tk-hover); }
.tk-thread-item.active { background: var(--tk-hover); color: #2271b1; font-weight: 600; }
.tk-thread-tool-icon { flex-shrink: 0; font-size: 12px; }

/* === صندوق الحساب: رأس مضغوط (اسم المستخدم + الباقة + سهم) + قائمة منبثقة تفتح للأعلى === */
.tk-account-wrap { margin-top: 8px; position: relative; }
.tk-account-header {
    display: flex; align-items: center; justify-content: space-between; width: 100%;
    background: none; border: none; padding: 9px 8px; border-radius: 8px; cursor: pointer;
    color: var(--tk-text); font-family: inherit; text-align: right;
}
.tk-account-header:hover { background: var(--tk-hover); }
.tk-account-header-main { display: flex; align-items: center; gap: 6px; overflow: hidden; }
.tk-account-username { font-size: 13px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tk-account-tier-pill { font-size: 10px; padding: 2px 8px; border-radius: 10px; background: #2271b1; color: #fff; white-space: nowrap; flex-shrink: 0; }
.tk-account-chevron { font-size: 10px; color: var(--tk-text-light); flex-shrink: 0; }

.tk-account-popover {
    display: none; position: absolute; bottom: calc(100% + 6px); right: 0; left: 0;
    background: var(--tk-bg); border: 1px solid var(--tk-border); border-radius: 12px;
    box-shadow: 0 8px 24px rgba(0,0,0,.15); padding: 10px; z-index: 6;
}
.tk-account-popover.open { display: block; }
/* صفوف مسطحة بلا إطارات فردية على النصوص */
.tk-account-row { display: flex; align-items: center; justify-content: space-between; padding: 7px 4px; font-size: 12px; color: var(--tk-text); }
.tk-account-row .label { color: var(--tk-text-light); }
.tk-account-row .value { font-weight: 700; }
.tk-account-divider { height: 1px; background: var(--tk-border); margin: 6px 0; }
/* === تسطيح كامل: بلا خلفية مصمتة أو إطار، فقط نص بلون مميّز وتظليل عند المرور === */
.tk-account-upgrade {
    display: flex; align-items: center; gap: 6px; justify-content: center;
    background: none; border: none; color: #2271b1; text-decoration: none;
    padding: 8px; border-radius: 8px; font-size: 12px; font-weight: 700; margin-top: 2px;
}
.tk-account-upgrade:hover { background: var(--tk-hover); }

.tk-lang-toggle { display: flex; align-items: center; justify-content: center; gap: 10px; padding: 6px 4px; font-size: 12px; }
.tk-lang-btn { background: none; border: none; padding: 4px 2px; font-size: 12px; cursor: pointer; color: var(--tk-text-light); font-weight: 600; }
.tk-lang-btn.active { color: #2271b1; }
.tk-lang-sep { color: var(--tk-border); }

/* === مفتاح داكن/فاتح: صف مسطّح + مفتاح انزلاقي فوق صندوق الحساب مباشرة === */
.tk-theme-row { display: flex; align-items: center; justify-content: space-between; padding: 9px 8px; font-size: 13px; color: var(--tk-text); }
.tk-theme-row .icon-label { display: flex; align-items: center; gap: 8px; }
.tk-theme-switch { position: relative; width: 38px; height: 21px; flex-shrink: 0; }
.tk-theme-switch input { opacity: 0; width: 0; height: 0; }
.tk-theme-switch .track { position: absolute; inset: 0; background: var(--tk-border); border-radius: 20px; transition: .2s; cursor: pointer; }
.tk-theme-switch .track::before { content: ""; position: absolute; height: 15px; width: 15px; right: 3px; bottom: 3px; background: #fff; border-radius: 50%; transition: .2s; box-shadow: 0 1px 3px rgba(0,0,0,.3); }
.tk-theme-switch input:checked + .track { background: var(--tk-primary); }
.tk-theme-switch input:checked + .track::before { transform: translateX(-17px); }

/* ================= المنطقة الرئيسية ================= */
.tk-main { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.tk-topbar { display: flex; align-items: center; gap: 10px; padding: 10px 15px; border-bottom: 1px solid var(--tk-border); }
.tk-topbar-title { flex: 1; font-size: 13px; font-weight: 600; color: var(--tk-text-light); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* === إصلاح حرج: z-index أعلى من الشريط الجانبي دائماً حتى لا يُغطّى الزر عند الفتح ويصبح غير قابل للنقر === */
.tk-sidebar-toggle {
    position: relative; z-index: 11;
    display: inline-flex; align-items: center; justify-content: center; background: none; border: none;
    font-size: 20px; cursor: pointer; color: var(--tk-text); flex-shrink: 0; padding: 4px; border-radius: 6px;
}
.tk-sidebar-toggle:hover { background: var(--tk-hover); }

/* شارة الاستهلاك القديمة أُزيلت نهائياً - الرصيد يظهر الآن فقط داخل قائمة الحساب المنبثقة (كان مكرراً) */

.tk-chat-area { flex: 1; min-height: 0; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 16px; }
/* === إصلاح الاتجاه: كلا الطرفين على نفس الجهة (بداية سطر الاتجاه النشط) ===
 * flex-start يتبع اتجاه الحاوية تلقائياً: يمين مع rtl (عربي)، يسار مع ltr
 * (إنجليزي) بمجرد تبديل dir على العنصر الجذر — دون أي شرط إضافي بالكود. */
.tk-msg { display: flex; gap: 10px; max-width: 85%; align-self: flex-start; }
.tk-msg.error { align-self: flex-start; }

.tk-msg-avatar { width: 30px; height: 30px; border-radius: 50%; flex-shrink: 0; display: flex; align-items: center; justify-content: center; font-size: 14px; font-weight: bold; overflow: hidden; background: var(--tk-border); color: var(--tk-text-light); }
.tk-msg-avatar img { width: 100%; height: 100%; object-fit: cover; }
.tk-msg.user .tk-msg-avatar, .tk-msg.ai .tk-msg-avatar { background: var(--tk-primary); color: #fff; }

.tk-msg-content { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.tk-msg-bubble { padding: 10px 14px; border-radius: 12px; line-height: 1.8; font-size: 14px; word-wrap: break-word; overflow-wrap: break-word; }
.tk-msg.user .tk-msg-bubble { background: var(--tk-primary); color: var(--tk-bubble-user-text); border-bottom-left-radius: 2px; }
.tk-msg.ai .tk-msg-bubble { background: var(--tk-bubble-ai); color: var(--tk-text); border-bottom-right-radius: 2px; }
.tk-msg.error .tk-msg-bubble { background: #fee2e2; color: #b91c1c; }
.tk-msg-bubble pre { background: #1e1e1e; color: #eee; padding: 10px; border-radius: 6px; overflow-x: auto; direction: ltr; text-align: left; max-width: 100%; }
.tk-msg-bubble code { font-family: monospace; font-size: 13px; }
.tk-generated-image { max-width: 100%; border-radius: 10px; display: block; margin-top: 6px; }
.tk-file-chip { display: inline-flex; align-items: center; gap: 6px; background: var(--tk-border); padding: 4px 10px; border-radius: 14px; font-size: 12px; margin-bottom: 6px; }

.tk-action-bar { display: flex; gap: 4px; opacity: .7; }
.tk-action-icon { background: none; border: none; cursor: pointer; font-size: 13px; padding: 3px 6px; border-radius: 5px; color: var(--tk-text-light); }
.tk-action-icon:hover { background: var(--tk-hover); }
.tk-action-icon.active { color: #2271b1; }

/* ================= شريط الإدخال: كبسولة واحدة على غرار Claude ================= */
.tk-composer { padding: 10px 15px 6px; border-top: 1px solid var(--tk-border); position: relative; }
.tk-composer-pill {
    display: flex !important; align-items: center; gap: 4px;
    border: 1.5px solid var(--tk-border) !important; border-radius: 26px !important;
    padding: 6px 8px; background: var(--tk-bg);
}
.tk-composer-pill:focus-within { border-color: var(--tk-primary) !important; }

/* === أيقونات الكبسولة (إرفاق/ميكروفون/إرسال): دوائر مسطّحة بلا إطار، صريحة القيم لتفادي أي تعارض مع أنماط القالب === */
.tk-attach-btn, .tk-mic-btn, .tk-send-btn {
    background: transparent !important; border: none !important; box-shadow: none !important;
    width: 34px !important; height: 34px !important; min-width: 34px; flex-shrink: 0;
    cursor: pointer; border-radius: 50% !important; padding: 0 !important;
    display: flex !important; align-items: center !important; justify-content: center !important;
    color: var(--tk-text-light);
}
.tk-attach-btn svg, .tk-mic-btn svg, .tk-send-btn svg {
    width: 19px !important; height: 19px !important; display: block !important;
    fill: none !important; stroke: currentColor !important; stroke-width: 2px;
}
.tk-attach-btn:hover, .tk-mic-btn:hover { background: var(--tk-hover) !important; color: var(--tk-text); }
.tk-mic-btn.recording { color: #ef4444; animation: tk-pulse 1.2s infinite; }
@keyframes tk-pulse { 0%,100% { opacity: 1; } 50% { opacity: .6; } }

.tk-input { flex: 1; min-width: 0; resize: none; padding: 8px 4px; border: none !important; background: none !important; color: var(--tk-text); font-family: inherit; font-size: 14px; max-height: 120px; outline: none !important; box-shadow: none !important; }

.tk-send-btn { background: var(--tk-primary) !important; color: #fff !important; }
.tk-send-btn:hover { opacity: .85; background: var(--tk-primary) !important; }
.tk-send-btn:disabled { opacity: .4; cursor: not-allowed; }

.tk-composer-disclaimer { text-align: center; font-size: 11px; color: var(--tk-text-light); margin-top: 6px; }

/* مبدّل النموذج: كبسولة نصية صغيرة داخل شريط الإدخال، تفتح قائمة للأعلى */
.tk-model-switcher { position: relative; flex-shrink: 0; }
.tk-model-pill {
    display: inline-flex; align-items: center; gap: 4px; height: 30px; padding: 0 10px;
    border-radius: 16px; border: none; background: transparent;
    color: var(--tk-text-light); font-size: 12px; font-weight: 600; cursor: pointer;
}
.tk-model-pill:hover { background: var(--tk-hover); color: var(--tk-text); }
.tk-model-pill .chevron { font-size: 9px; }

.tk-model-menu {
    display: none; position: absolute; bottom: calc(100% + 8px); left: 0; min-width: 220px;
    background: var(--tk-bg); border: 1px solid var(--tk-border); border-radius: 12px;
    box-shadow: 0 8px 24px rgba(0,0,0,.15); padding: 6px; z-index: 5;
}
.tk-model-menu.open { display: block; }
.tk-model-menu-item { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 9px 10px; border-radius: 8px; cursor: pointer; }
.tk-model-menu-item:hover { background: var(--tk-hover); }
.tk-model-menu-item .name { font-size: 13px; font-weight: 700; color: var(--tk-text); }
.tk-model-menu-item .check { color: #2271b1; font-size: 13px; }

.tk-login-wrap { display: flex; justify-content: center; align-items: center; min-height: 60vh; padding: 15px; width: 100%; }
.tk-login-box { background: #fff; border: 1px solid #eee; border-radius: 12px; padding: 30px; max-width: 380px; width: 100%; text-align: center; }
.tk-login-logo { max-height: 50px; margin-bottom: 15px; border-radius: 8px; }

.tk-sidebar-backdrop { display: none; position: absolute; inset: 0; background: rgba(0,0,0,.35); z-index: 9; }
.tk-sidebar-backdrop.visible { display: block; }

@media (max-width: 782px) {
    .tk-app { height: 85vh; border-radius: 0; }
    .tk-sidebar { position: absolute; top: 0; right: 0; z-index: 10; height: 100%; margin-right: -280px; width: 260px; box-shadow: 2px 0 10px rgba(0,0,0,.15); }
    .tk-sidebar.open { margin-right: 0; }
    .tk-sidebar.collapsed { width: 260px; padding: 14px 10px; border: none; }
    .tk-msg { max-width: 94%; }
    .tk-topbar { padding: 8px 10px; }
    .tk-chat-area { padding: 14px; }
    .tk-composer { padding: 10px; }
    .tk-model-menu { min-width: 180px; }
}

@media (max-width: 400px) {
    /* .tk-usage-badge removed */
}
