/* =========================================================================
   App shell: sidebar (always on the LEFT, drawer on phones, collapsible rail on desktop), main card, top bar, search
   The frame is laid out left-to-right in every language; text inside sidebar and main keeps the language's direction.
   ========================================================================= */
.app { display: grid; grid-template-columns: var(--sidebar-w) minmax(0, 1fr); height: var(--app-h, 100dvh); width: 100%; background: var(--bg);
  direction: ltr; transition: grid-template-columns var(--mid) var(--ease); }
.app.collapsed { grid-template-columns: var(--rail-w) minmax(0, 1fr); }
[dir="rtl"] .app > .sidebar, [dir="rtl"] .app > .main { direction: rtl; }
[dir="ltr"] .app > .sidebar, [dir="ltr"] .app > .main { direction: ltr; }

/* ---------- sidebar ---------- */
.sidebar { display: flex; flex-direction: column; min-width: 0; height: var(--app-h, 100dvh); background: var(--sidebar);
  padding: var(--s3) var(--s2) 0; gap: var(--s2); position: relative; z-index: 40; }
.side-head { display: flex; align-items: center; justify-content: space-between; gap: var(--s2); min-height: 44px; padding-inline: var(--s2) 0; }
.brand-home { display: flex; align-items: center; gap: 10px; color: var(--ink); text-decoration: none; font-weight: 700; font-size: 17px;
  white-space: nowrap; letter-spacing: -.01em; min-width: 0; }
.brand-home strong { font-weight: 700; overflow: hidden; text-overflow: ellipsis; }
.side-actions { display: grid; gap: 4px; padding-inline: 2px; margin-bottom: var(--s2); }
.side-btn { display: flex; align-items: center; gap: var(--s3); width: 100%; min-height: 40px; padding: 0 12px; border: 0; border-radius: var(--r-md);
  background: transparent; color: var(--ink); font-weight: 500; font-size: 14.5px; text-align: start; text-decoration: none;
  transition: background var(--fast), color var(--fast); white-space: nowrap; }
.side-btn:hover { background: var(--hover); }
.side-btn.active, .side-btn[aria-expanded="true"] { background: var(--active); font-weight: 600; }
.side-btn .ic { color: var(--muted); width: 18px; height: 18px; }
.side-btn.quiet { color: var(--muted); font-weight: 400; }
.side-btn.quiet:hover { color: var(--ink); }
.side-btn:hover .ic, .side-btn.active .ic { color: var(--ink); }
.side-btn .chev-side { margin-inline-start: auto; color: var(--faint); }
[dir="rtl"] .side-btn .chev-side { transform: scaleX(-1); }
.side-btn.new { background: var(--surface-2); border: 1px solid var(--line-strong); font-weight: 600; margin-bottom: 4px; min-height: 42px; }
.side-btn.new .ic { color: var(--accent); }
.side-btn.new:hover { background: var(--surface-3); }

.side-scroll { flex: 1; min-height: 0; overflow-y: auto; overflow-x: hidden; padding: 0 2px 12px; overscroll-behavior: contain; }
.side-label { display: flex; align-items: center; justify-content: space-between; gap: 6px; padding: 14px 10px 6px; font-size: 12px; font-weight: 600;
  color: var(--faint); letter-spacing: .03em; text-transform: uppercase; }
:lang(ar) .side-label { text-transform: none; letter-spacing: 0; font-size: 12.5px; }
.side-label .icon-btn { width: 26px; height: 26px; }
.empty-note { padding: 12px 10px; color: var(--muted); font-size: 13.5px; }

/* chat rows */
.chat-item { position: relative; display: flex; align-items: center; border-radius: var(--r-md); transition: background var(--fast); }
.chat-item > a { flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; min-height: 38px; padding: 0 10px; color: var(--ink);
  text-decoration: none; border-radius: var(--r-md); font-size: 14.5px; }
.chat-item .c-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .c-pin { color: var(--faint); flex: none; }
.chat-item:hover { background: var(--hover); }
.chat-item.active { background: var(--active); }
.chat-item.active > a { font-weight: 600; }
.chat-item .chat-menu { position: absolute; inset-inline-end: 4px; top: 50%; transform: translateY(-50%); opacity: 0; transition: opacity var(--fast); }
.chat-item:hover .chat-menu, .chat-item:focus-within .chat-menu, .chat-item .chat-menu:has(.open) { opacity: 1; }
.chat-item:hover > a, .chat-item:focus-within > a, .chat-item:has(.chat-menu .open) > a { padding-inline-end: 38px; }
.chat-item:has(.chat-menu .open) { background: var(--hover); }
@media (hover: none) { .chat-item .chat-menu { opacity: 1; } .chat-item > a { padding-inline-end: 38px; } }

/* projects */
.project { margin-bottom: 1px; }
.project-head { display: flex; align-items: center; border-radius: var(--r-md); }
.project-head > button.pname { flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; min-height: 38px; padding: 0 10px; border: 0; background: none;
  color: var(--ink); font-size: 14.5px; font-weight: 500; text-align: start; border-radius: var(--r-md); }
.project-head:hover { background: var(--hover); }
.project-head .ic.folder { color: var(--muted); }
.project-head .pname span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.project-head .count { font-size: 12px; color: var(--faint); font-weight: 500; }
.project-head .chev { transition: transform var(--fast) var(--ease); color: var(--faint); }
.project-head [aria-expanded="true"] .chev { transform: rotate(calc(var(--slide) * -1 * -90deg)); }
.project-body { margin-inline-start: 14px; padding-inline-start: 6px; border-inline-start: 1px solid var(--line); }
.project-head .p-actions { display: flex; opacity: 0; transition: opacity var(--fast); padding-inline-end: 2px; }
.project-head:hover .p-actions, .project-head:focus-within .p-actions { opacity: 1; }
@media (hover: none) { .project-head .p-actions { opacity: 1; } }

/* sidebar footer */
.side-foot { padding: var(--s2) 2px calc(var(--s3) + var(--safe-bottom)); border-top: 1px solid var(--line); display: grid; gap: var(--s2); }
.channels-stack { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--s2); }
.channels-stack .channel { min-height: 36px; padding: 0 8px; font-size: 13px; justify-content: center; gap: 8px; min-width: 0; }
.channels-stack .channel .ch-icon { width: 22px; height: 22px; border-radius: 7px; }
.channels-stack .channel .ch-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.me-wrap { position: relative; }
.me { display: flex; align-items: center; gap: 10px; width: 100%; padding: 8px; border: 0; border-radius: var(--r-md); background: transparent; text-align: start;
  transition: background var(--fast); }
.me:hover, .me[aria-expanded="true"] { background: var(--hover); }
.me-text { display: grid; line-height: 1.3; min-width: 0; flex: 1; }
.me-name { font-weight: 600; font-size: 14.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.me-plan { font-size: 12.5px; color: var(--muted); }
.me .ic { color: var(--faint); }

/* account menu (opens upward from the user card) */
.account-menu { min-width: 0; max-width: none; padding: 6px; }
.account-menu .who-card { display: flex; align-items: center; gap: 10px; padding: 8px 8px 10px; }
.account-menu .who-card .who-text { display: grid; line-height: 1.35; min-width: 0; }
.account-menu .who-card strong { font-size: 14.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.account-menu .who-card small { color: var(--muted); font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; direction: ltr; text-align: start; }
.account-menu .theme-row { display: flex; align-items: center; justify-content: space-between; gap: var(--s2); padding: 6px 10px; }
.account-menu .theme-row .segmented > * { min-height: 32px; padding: 0 10px; }
.menu .item .chev-r { margin-inline-start: auto; color: var(--faint); }
[dir="rtl"] .menu .item .chev-r { transform: scaleX(-1); }
.menu .item[aria-expanded="true"] { background: var(--hover); }
.menu .item[disabled] { opacity: .55; cursor: default; }
.menu .item[disabled]:hover { background: none; }
.menu .soon { margin-inline-start: auto; font-size: 11.5px; font-weight: 600; color: var(--muted); padding: 1px 8px; border-radius: var(--r-full); background: var(--surface-3); }
.menu.tools { min-width: 250px; }
.parent-link { display: flex; align-items: center; gap: 8px; padding: 6px 10px; font-size: 12.5px; color: var(--faint); text-decoration: none; }
.parent-link:hover { color: var(--accent); }

/* ---------- collapsed rail (desktop) ---------- */
.app.collapsed .sidebar { padding-inline: 10px; align-items: center; }
.app.collapsed .brand-home strong, .app.collapsed .side-btn span, .app.collapsed .side-btn .chev-side, .app.collapsed .side-scroll,
.app.collapsed .channels-stack, .app.collapsed .me-text, .app.collapsed .me > .ic, .app.collapsed .parent-link, .app.collapsed .side-label { display: none !important; }
.app.collapsed .side-head { flex-direction: column; gap: var(--s2); padding: 0; }
.app.collapsed .side-btn { justify-content: center; padding: 0; width: 44px; }
.app.collapsed .side-foot { border: 0; margin: auto 0 0; padding-inline: 0; }
.app.collapsed .me { padding: 4px; width: auto; justify-content: center; }
.app.collapsed .side-actions { justify-items: center; padding: 0; }

/* ---------- main column: a rounded card on the frame ---------- */
.main { display: flex; flex-direction: column; min-width: 0; height: calc(var(--app-h, 100dvh) - 16px); margin: 8px 8px 8px 0; position: relative;
  background: var(--main); border: 1px solid var(--line); border-radius: var(--r-xl); overflow: hidden; }
.topbar { display: flex; align-items: center; gap: var(--s2); min-height: var(--topbar-h); padding: var(--s2) var(--s4); z-index: 20; }
.top-title { flex: 1; min-width: 0; font-size: 15.5px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.top-title .title-btn { display: inline-flex; align-items: center; max-width: 100%; gap: 6px; border: 0; background: none; padding: 4px 8px; margin-inline-start: -8px;
  border-radius: var(--r-sm); font: inherit; color: inherit; }
.top-title .title-btn:hover { background: var(--hover); }
.top-tools { display: flex; align-items: center; gap: 2px; }
.top-chip { display: inline-flex; align-items: center; gap: 6px; min-height: 30px; padding: 0 10px; border: 1px solid var(--line-strong); border-radius: var(--r-full);
  background: var(--surface-2); color: var(--muted); font-size: 12.5px; font-weight: 600; white-space: nowrap; }
.top-chip button { display: inline-grid; place-items: center; border: 0; background: none; color: inherit; padding: 0; width: 18px; height: 18px; border-radius: 50%; }
.top-chip button:hover { background: var(--active); color: var(--ink); }
.lang-menu { position: relative; }
.lang-btn { display: inline-flex; align-items: center; gap: 6px; height: 38px; padding: 0 10px; border: 1px solid transparent; border-radius: var(--r-sm);
  background: transparent; color: var(--muted); font-size: 14px; font-weight: 600; transition: background var(--fast), color var(--fast); }
.lang-btn:hover { background: var(--hover); color: var(--ink); }
.view { flex: 1; min-height: 0; display: flex; flex-direction: column; position: relative; }

/* scrim for the phone drawer */
.scrim { position: fixed; inset: 0; z-index: 35; background: var(--scrim); animation: fade var(--fast) var(--ease); }

/* ---------- search palette ---------- */
.palette { width: min(640px, 100%); max-height: min(560px, 80dvh); display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--line-strong);
  border-radius: var(--r-xl); box-shadow: var(--shadow-lg); overflow: hidden; animation: pop var(--mid) var(--ease); align-self: start; margin-top: 10vh; }
.palette-top { display: flex; align-items: center; gap: var(--s3); padding: 0 var(--s4); border-bottom: 1px solid var(--line); }
.palette-top .ic { color: var(--muted); }
.palette-top input { flex: 1; min-height: 58px; border: 0; background: transparent; font-size: 16.5px; outline: none; min-width: 0; }
.palette-list { overflow-y: auto; padding: var(--s2); display: grid; gap: 2px; align-content: start; }
.palette-item { display: flex; align-items: center; gap: var(--s3); min-height: 48px; padding: 6px 12px; border: 0; border-radius: var(--r-md); background: none;
  text-align: start; color: var(--ink); text-decoration: none; }
.palette-item:hover, .palette-item[aria-selected="true"] { background: var(--active); }
.palette-item .m-text { flex: 1; }
.palette-item .m-text strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.palette-item .m-text small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.palette-item .when { font-size: 12px; color: var(--faint); flex: none; }
.palette-empty { padding: var(--s6) var(--s4); text-align: center; color: var(--muted); }
.palette-foot { display: flex; gap: var(--s4); padding: 8px var(--s4); border-top: 1px solid var(--line); font-size: 12.5px; color: var(--faint); }
mark { background: color-mix(in srgb, var(--cyan) 28%, transparent); color: inherit; border-radius: 3px; padding: 0 1px; }

/* ---------- phone / tablet: the sidebar becomes a drawer that slides in from the left ---------- */
@media (max-width: 900px) {
  .app, .app.collapsed { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: fixed; inset-block: 0; left: 0; width: min(86vw, 320px); transform: translateX(-100%);
    visibility: hidden; transition: transform var(--mid) var(--ease), visibility 0s linear var(--mid); box-shadow: var(--shadow-lg);
    border-inline-end: 1px solid var(--line); padding-top: calc(var(--s3) + var(--safe-top)); }
  .sidebar.open { transform: none; visibility: visible; transition: transform var(--mid) var(--ease), visibility 0s; }
  .app.collapsed .sidebar { padding-inline: var(--s2); align-items: stretch; }
  .app.collapsed .brand-home strong, .app.collapsed .side-btn span, .app.collapsed .side-btn .chev-side, .app.collapsed .side-scroll,
  .app.collapsed .channels-stack, .app.collapsed .me-text, .app.collapsed .me > .ic, .app.collapsed .parent-link, .app.collapsed .side-label { display: revert !important; }
  .app.collapsed .side-btn { justify-content: flex-start; padding: 0 10px; width: 100%; }
  .app.collapsed .side-head { flex-direction: row; }
  .app.collapsed .side-actions { justify-items: stretch; }
  /* full-bleed on phones: the card's rounded frame would only waste space */
  .main { margin: 0; height: var(--app-h, 100dvh); border: 0; border-radius: 0; }
  .topbar { padding: calc(var(--s2) + var(--safe-top)) var(--s2) var(--s2); }
  .only-mobile { display: inline-grid !important; }
  .only-desktop { display: none !important; }
  .palette { margin-top: 4vh; }
  .overlay { padding: var(--s3); }
}
@media (min-width: 901px) { .only-desktop { display: inline-grid; } }
