/* ==========================================================================
   Vyapar-Doot — Chat workspace (sidebar + messages + composer)
   Mobile-first: sidebar is an off-canvas drawer; expands to a fixed rail on
   large screens.
   ========================================================================== */

.app-chat { background: var(--bg-page); }

.chat-shell { display: grid; grid-template-columns: 1fr; height: 100dvh; overflow: hidden; }

/* ---- Sidebar ---- */
.chat-sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: var(--z-sidebar);
    width: min(86vw, var(--sidebar-width)); background: var(--bg-surface);
    border-right: 1px solid var(--border); display: flex; flex-direction: column;
    transform: translateX(-100%); transition: transform var(--transition-slow);
}
.chat-sidebar[data-open="true"] { transform: none; box-shadow: var(--shadow-lg); }
.chat-sidebar__head { padding: var(--sp-4); border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; gap: var(--sp-2); }
.chat-sidebar__body { flex: 1; overflow-y: auto; padding: var(--sp-3); }
.chat-sidebar__foot { border-top: 1px solid var(--border); padding: var(--sp-3); }

.sidebar-search { position: relative; margin-bottom: var(--sp-3); }
.sidebar-search .input { padding-left: 2.25rem; }
.sidebar-search svg { position: absolute; left: 0.7rem; top: 50%; transform: translateY(-50%); color: var(--text-muted); width: 18px; height: 18px; }

.chat-group__label { font-size: var(--fs-xs); text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-muted); font-weight: 700; padding: var(--sp-3) var(--sp-2) var(--sp-2); }
.chat-item {
    display: flex; align-items: center; gap: var(--sp-2); padding: var(--sp-2) var(--sp-3);
    border-radius: var(--radius); color: var(--text-main); font-size: var(--fs-sm); cursor: pointer;
    text-decoration: none; min-height: 42px;
}
.chat-item:hover { background: var(--bg-muted); text-decoration: none; }
.chat-item[aria-current="true"] { background: var(--bg-muted); font-weight: 600; }
.chat-item__title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item__pin { color: var(--color-accent); }

.overlay-backdrop { position: fixed; inset: 0; background: var(--overlay); z-index: calc(var(--z-sidebar) - 1); display: none; }
.overlay-backdrop[data-show="true"] { display: block; }

/* ---- Main ---- */
.chat-main { display: flex; flex-direction: column; height: 100dvh; min-width: 0; }
.chat-header {
    display: flex; align-items: center; gap: var(--sp-3); padding: var(--sp-3) var(--sp-4);
    border-bottom: 1px solid var(--border); background: var(--bg-surface); min-height: var(--header-height-mobile);
}
.chat-header__title { font-weight: 700; font-size: var(--fs-base); flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn {
    width: 42px; height: 42px; display: inline-grid; place-items: center; border-radius: var(--radius);
    border: 1px solid transparent; background: transparent; color: var(--text-main); cursor: pointer;
}
.icon-btn:hover { background: var(--bg-muted); }
.icon-btn svg { width: 20px; height: 20px; }

.mode-select { display: inline-flex; align-items: center; gap: var(--sp-2); }
.mode-select .select { min-height: 40px; padding-block: 0.3rem; font-size: var(--fs-sm); max-width: 200px; }

/* ---- Messages ---- */
.chat-scroll { flex: 1; overflow-y: auto; padding: var(--sp-5) var(--sp-4); }
.chat-thread { max-width: 820px; margin-inline: auto; display: flex; flex-direction: column; gap: var(--sp-5); }

.msg { display: flex; gap: var(--sp-3); }
.msg__avatar { flex: 0 0 auto; width: 36px; height: 36px; border-radius: 10px; display: grid; place-items: center; font-weight: 800; font-size: var(--fs-sm); }
.msg--user .msg__avatar { background: var(--bg-muted); color: var(--text-main); }
.msg--ai .msg__avatar { background: linear-gradient(135deg, var(--color-primary), var(--color-primary-light)); color: #fff; }
.msg__body { min-width: 0; flex: 1; }
.msg__name { font-size: var(--fs-xs); font-weight: 700; color: var(--text-muted); margin-bottom: var(--sp-1); }
.msg__content { font-size: var(--fs-base); line-height: var(--lh-base); }
.msg__content p:last-child { margin-bottom: 0; }
.msg__content pre { background: var(--bg-muted); padding: var(--sp-3); border-radius: var(--radius); overflow-x: auto; font-size: var(--fs-sm); }
.msg__content code { background: var(--bg-muted); padding: 0.15em 0.4em; border-radius: 6px; font-size: 0.9em; }
.msg__content pre code { background: none; padding: 0; }
.msg__content table { width: 100%; border-collapse: collapse; margin-bottom: var(--sp-4); }
.msg__content th, .msg__content td { border: 1px solid var(--border); padding: var(--sp-2) var(--sp-3); text-align: left; font-size: var(--fs-sm); }
.msg__actions { display: flex; gap: var(--sp-1); margin-top: var(--sp-3); flex-wrap: wrap; }
.msg-action { display: inline-flex; align-items: center; gap: 0.35em; font-size: var(--fs-xs); font-weight: 600; color: var(--text-muted); background: transparent; border: 1px solid var(--border); border-radius: var(--radius-pill); padding: 0.3rem 0.7rem; cursor: pointer; min-height: 32px; }
.msg-action:hover { background: var(--bg-muted); color: var(--text-main); }

/* Welcome / empty */
.chat-welcome { max-width: 760px; margin: auto; text-align: center; padding: var(--sp-6) var(--sp-2); }
.chat-welcome__mark { width: 64px; height: 64px; margin: 0 auto var(--sp-4); }
.chat-welcome h1 { font-size: clamp(1.6rem, 5vw, 2.2rem); }
.starter-grid { display: grid; gap: var(--sp-3); margin-top: var(--sp-6); text-align: left; }
.starter {
    background: var(--bg-surface); border: 1px solid var(--border); border-radius: var(--radius-card);
    padding: var(--sp-4); cursor: pointer; transition: border-color var(--transition), box-shadow var(--transition), transform var(--transition);
    display: flex; gap: var(--sp-3); align-items: flex-start; text-align: left; font: inherit; color: inherit; width: 100%;
}
.starter:hover { border-color: var(--color-primary-light); box-shadow: var(--shadow-md); transform: translateY(-2px); }
.starter__icon { flex: 0 0 auto; width: 38px; height: 38px; border-radius: 10px; background: var(--bg-muted); display: grid; place-items: center; color: var(--color-primary-light); }
.starter__title { font-weight: 700; font-size: var(--fs-sm); margin-bottom: 2px; }
.starter__desc { font-size: var(--fs-xs); color: var(--text-muted); }
@media (min-width: 640px) { .starter-grid { grid-template-columns: repeat(2, 1fr); } }

/* Status line under typing */
.msg__status { font-size: var(--fs-sm); color: var(--text-muted); margin-top: var(--sp-2); font-style: italic; }

/* Sources */
.sources-drawer { margin-top: var(--sp-3); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.sources-drawer summary { cursor: pointer; padding: var(--sp-2) var(--sp-3); font-size: var(--fs-sm); font-weight: 600; background: var(--bg-muted); list-style: none; }
.sources-drawer summary::-webkit-details-marker { display: none; }
.sources-drawer summary::before { content: "🔗 "; }
.sources-list { padding: var(--sp-2) var(--sp-3); display: grid; gap: var(--sp-2); }
.source-card { padding: var(--sp-2) 0; border-top: 1px dashed var(--border); }
.source-card:first-child { border-top: none; }
.source-card a { font-weight: 600; font-size: var(--fs-sm); word-break: break-word; }
.source-card__meta { display: flex; align-items: center; gap: var(--sp-2); margin-top: 2px; flex-wrap: wrap; }
.source-card__domain { font-size: var(--fs-xs); color: var(--text-muted); }

/* Sidebar chat item menu + active state */
.chat-item { position: relative; }
.chat-item.is-active, .chat-item[aria-current="true"] { background: var(--bg-muted); font-weight: 600; }
.chat-item__menu { width: 28px; height: 28px; opacity: 0; flex: 0 0 auto; }
.chat-item:hover .chat-item__menu, .chat-item:focus-within .chat-item__menu { opacity: 1; }
@media (hover: none) { .chat-item__menu { opacity: 1; } }

/* Typing indicator */
.typing { display: inline-flex; gap: 4px; align-items: center; padding: var(--sp-2) 0; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--text-muted); animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 60%, 100% { opacity: 0.25; } 30% { opacity: 1; } }

/* ---- Composer ---- */
.composer { border-top: 1px solid var(--border); background: var(--bg-surface); padding: var(--sp-3) var(--sp-4) calc(var(--sp-3) + env(safe-area-inset-bottom)); }
.composer__inner { max-width: 820px; margin-inline: auto; }
.composer__toolbar { display: flex; align-items: center; gap: var(--sp-2); margin-bottom: var(--sp-2); flex-wrap: wrap; }
.composer__box {
    display: flex; align-items: flex-end; gap: var(--sp-2); background: var(--bg-surface);
    border: 1px solid var(--border-strong); border-radius: var(--radius-card); padding: var(--sp-2);
    transition: border-color var(--transition), box-shadow var(--transition);
}
.composer__box:focus-within { border-color: var(--color-primary-light); box-shadow: 0 0 0 3px rgba(58,73,168,0.15); }
.composer__textarea { flex: 1; border: none; background: transparent; resize: none; font: inherit; color: var(--text-main); padding: 0.55rem 0.5rem; max-height: 200px; min-height: 24px; line-height: 1.5; }
.composer__textarea:focus { outline: none; }
.composer__send { flex: 0 0 auto; width: 44px; height: 44px; border-radius: 12px; border: none; background: var(--color-primary); color: #fff; cursor: pointer; display: grid; place-items: center; }
.composer__send:disabled { opacity: 0.45; cursor: not-allowed; }
.composer__meta { display: flex; justify-content: space-between; align-items: center; margin-top: var(--sp-2); font-size: var(--fs-xs); color: var(--text-muted); gap: var(--sp-3); flex-wrap: wrap; }
.toggle { display: inline-flex; align-items: center; gap: var(--sp-2); cursor: pointer; font-size: var(--fs-sm); }
.toggle input { position: absolute; opacity: 0; }
.toggle__track { width: 40px; height: 22px; border-radius: 999px; background: var(--border-strong); position: relative; transition: background var(--transition); flex: 0 0 auto; }
.toggle__track::after { content: ""; position: absolute; top: 2px; left: 2px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: transform var(--transition); }
.toggle input:checked + .toggle__track { background: var(--color-secondary); }
.toggle input:checked + .toggle__track::after { transform: translateX(18px); }
.toggle input:focus-visible + .toggle__track { outline: 3px solid var(--color-accent); outline-offset: 2px; }

/* Usage pill */
.usage-pill { display: inline-flex; align-items: center; gap: var(--sp-2); font-size: var(--fs-xs); background: var(--bg-muted); border-radius: var(--radius-pill); padding: 0.3rem 0.7rem; color: var(--text-muted); }

/* Desktop layout: fixed sidebar rail */
@media (min-width: 1024px) {
    .chat-shell { grid-template-columns: var(--sidebar-width) 1fr; }
    .chat-sidebar { position: relative; transform: none !important; width: auto; box-shadow: none; }
    .chat-sidebar__collapse-only { display: none; }
    .overlay-backdrop { display: none !important; }
    .sidebar-toggle-mobile { display: none; }
    .starter-grid { grid-template-columns: repeat(2, 1fr); }
}

/* Modal / off-canvas (quota modal). The chat page loads chat.css (not public.css),
   so it needs its own copy of these rules — otherwise the modal shows by default. */
.mobile-nav { position: fixed; inset: 0; z-index: var(--z-overlay); display: none; }
.mobile-nav[data-open="true"] { display: block; }
.mobile-nav__backdrop { position: absolute; inset: 0; background: var(--overlay); }
.mobile-nav__panel {
    position: absolute; top: 0; right: 0; height: 100%; width: min(84vw, 340px);
    background: var(--bg-surface); box-shadow: var(--shadow-lg); padding: var(--sp-5);
    display: flex; flex-direction: column; gap: var(--sp-2); overflow-y: auto;
}
.mobile-nav__close { align-self: flex-end; }
