:root {
  --ink: #0e0f12;
  --bg: #f6f7f8;
  --surface: #ffffff;
  --surface2: #f2f4f7;
  --line: #e5e7eb;
  --muted: #6b7280;
  --danger: #dc2626;
  --blue: #1a73e8;
  --shadow: 0 12px 30px rgba(15, 23, 42, 0.08);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body { font-family: Arial, sans-serif; background: var(--bg); color: var(--ink); }
button, input, textarea, select { font: inherit; }
button { -webkit-tap-highlight-color: transparent; cursor: pointer; }
button {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  border-radius: 10px;
  padding: 10px 12px;
  font-weight: 850;
}
button:disabled { opacity: .45; cursor: default; }

.app { min-height: 100dvh; background: var(--bg); }

.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  z-index: 40;
  width: min(320px, 88vw);
  transform: translateX(-105%);
  transition: transform .16s ease;
  background: var(--ink);
  color: #fff;
  padding: calc(16px + env(safe-area-inset-top, 0px)) 16px 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: 0 18px 40px rgba(0,0,0,.22);
}
.app.history-open .sidebar { transform: translateX(0); }

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding-bottom: 14px;
  border-bottom: 1px solid rgba(255,255,255,.12);
}
.brand__mark {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  background: #fff;
  color: var(--ink);
  font-weight: 900;
}
.brand strong { display: block; font-size: 15px; }
.brand span { display: block; margin-top: 2px; color: #c6cad1; font-size: 13px; }
.sidebar__new { background: #fff; color: var(--ink); border-color: transparent; }

.history { min-height: 0; }
.history__head { color: #c6cad1; font-size: 12px; text-transform: uppercase; font-weight: 800; margin-bottom: 8px; }
.history__list { display: grid; gap: 8px; max-height: calc(100dvh - 180px); overflow: auto; padding-right: 2px; scrollbar-width: none; }
.history__list::-webkit-scrollbar { display: none; }
.historyItem {
  width: 100%;
  text-align: left;
  background: rgba(255,255,255,.06);
  border-color: rgba(255,255,255,.08);
  color: #fff;
  padding: 10px;
}
.historyItem strong { display: block; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-size: 13px; }
.historyItem span { color: #c6cad1; font-size: 12px; }

.workspace { min-width: 0; padding-bottom: calc(150px + env(safe-area-inset-bottom, 0px)); }
.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  background: #fff;
  border-bottom: 1px solid var(--line);
  padding: calc(12px + env(safe-area-inset-top, 0px)) 14px 12px;
}
.topbar > div:not(.topbar__status) { min-width: 0; flex: 1; }
h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 2px; font-size: 18px; line-height: 1.15; letter-spacing: 0; }
h2 { margin-bottom: 0; font-size: 16px; }
.topbar p { display: none; margin-bottom: 0; color: var(--muted); max-width: 680px; line-height: 1.45; }
.mobileHistoryBtn {
  width: 38px;
  height: 38px;
  padding: 0;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  font-size: 18px;
}
.topbar__status {
  color: var(--muted);
  background: var(--surface2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 7px 9px;
  font-size: 11px;
  font-weight: 800;
  white-space: nowrap;
  max-width: 116px;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Mobile tab switch: Таблица / Чат */
.mobileTabs {
  display: flex;
  gap: 6px;
  padding: 8px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 12;
}
.mobileTabs button {
  flex: 1;
  border-radius: 12px;
  padding: 10px;
  background: var(--surface2);
  border-color: transparent;
  color: var(--muted);
}
.mobileTabs button.is-active { background: var(--ink); color: #fff; }

.workgrid { display: grid; grid-template-columns: 1fr; gap: 12px; align-items: start; padding: 14px; }
.panel, .resultPane { background: var(--surface); border: 1px solid var(--line); border-radius: 16px; box-shadow: none; }
.panel { padding: 16px; }
.panel__head, .resultToolbar { display: flex; justify-content: space-between; align-items: center; gap: 14px; margin-bottom: 16px; }
.panel__head span, #resultMeta { color: var(--muted); font-size: 13px; font-weight: 700; }

/* Files + header — bottom sheet on mobile */
.inputPane {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  max-height: 88dvh;
  overflow-y: auto;
  background: var(--surface);
  border: 0;
  border-radius: 20px 20px 0 0;
  box-shadow: 0 -18px 40px rgba(15,23,42,.18);
  transform: translateY(102%);
  transition: transform .2s ease;
  padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
}
.app.files-open .inputPane { transform: translateY(0); }
.inputPane .panel { border: 0; border-radius: 0; padding: 0 16px 4px; }
.sheet__handle {
  width: 44px;
  height: 5px;
  border-radius: 999px;
  background: var(--line);
  margin: 10px auto 2px;
}
.sheet__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 4px 16px 12px;
}
.sheet__head h2 { font-size: 16px; }
.sheet__close {
  width: 36px;
  height: 36px;
  padding: 0;
  display: grid;
  place-items: center;
  border-radius: 999px;
  font-size: 16px;
  background: var(--surface2);
  border-color: transparent;
}
.sheetBackdrop {
  position: fixed;
  inset: 0;
  z-index: 25;
  background: rgba(15,23,42,.4);
  display: none;
}
.app.files-open .sheetBackdrop { display: block; }

.dropzone {
  min-height: 132px;
  border: 1.5px dashed #c8cdd5;
  border-radius: 14px;
  display: grid;
  place-items: center;
  gap: 5px;
  text-align: center;
  background: #fff;
  padding: 18px;
  cursor: pointer;
}
.dropzone.is-drag { border-color: var(--ink); background: var(--surface2); }
.dropzone input { display: none; }
.dropzone__icon {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  background: var(--surface2);
  color: var(--ink);
  font-size: 24px;
  font-weight: 900;
}
.dropzone small { color: var(--muted); }

.files { display: grid; gap: 8px; margin: 14px 0; }
.fileRow {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 10px;
  background: var(--surface2);
  border: 1px solid var(--line);
  border-radius: 12px;
}
.fileRow strong { display: block; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fileRow span { color: var(--muted); font-size: 12px; }
.fileRow a { color: var(--blue); font-size: 12px; font-weight: 800; }

.library {
  margin: 14px 0;
  border-top: 1px solid var(--line);
  padding-top: 14px;
}
.library__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}
.library__head h2 {
  font-size: 15px;
}
.library__head span {
  min-width: 24px;
  height: 24px;
  display: grid;
  place-items: center;
  border-radius: 999px;
  background: var(--surface2);
  color: var(--ink);
  font-size: 12px;
  font-weight: 900;
}
.library__list { display: grid; gap: 8px; }
.libraryEmpty {
  color: var(--muted);
  background: var(--surface2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px;
  font-size: 13px;
  line-height: 1.35;
}
.libraryRow {
  display: grid;
  gap: 9px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fff;
}
.libraryRow strong {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
}
.libraryRow span {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
}
.libraryRow__actions {
  display: flex;
  gap: 8px;
  align-items: center;
}
.libraryRow__actions button {
  padding: 7px 10px;
  border-radius: 8px;
  font-size: 12px;
}
.libraryRow__actions a {
  color: var(--blue);
  font-size: 12px;
  font-weight: 800;
}

.field { display: grid; gap: 7px; margin-top: 14px; }
.field span { color: var(--muted); font-size: 13px; font-weight: 800; }
textarea, select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--ink);
  padding: 10px 12px;
  outline: none;
}
textarea:focus, select:focus { border-color: #9cc4ff; box-shadow: 0 0 0 3px rgba(26, 115, 232, .14); }
.primary { background: var(--ink); border-color: var(--ink); color: #fff; }
.primary:hover { background: #252932; }

.notice {
  margin-top: 12px;
  border: 1px solid #fecaca;
  background: #fff1f2;
  color: var(--danger);
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 13px;
  font-weight: 800;
}

.resultPane { overflow: hidden; display: flex; flex-direction: column; }
.resultToolbar { padding: 16px; border-bottom: 1px solid var(--line); margin-bottom: 0; }
.resultToolbar__actions { display: flex; gap: 8px; }
.resultToolbar__actions button { white-space: nowrap; }
.estimateView {
  border-bottom: 1px solid var(--line);
  background: #fff;
}
.proposalDocument {
  padding: 16px;
  line-height: 1.5;
  overflow-x: auto;
}
.proposalDocument.empty {
  color: var(--muted);
}
.proposalDocument.empty h3 {
  color: var(--ink);
  margin-bottom: 8px;
}
.proposalMeta {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  margin-bottom: 12px;
}
.proposalDocument h1,
.proposalDocument h2,
.proposalDocument h3 {
  color: var(--ink);
  margin: 16px 0 8px;
  letter-spacing: 0;
}
.proposalDocument h1:first-child,
.proposalDocument h2:first-child,
.proposalDocument h3:first-child {
  margin-top: 0;
}
.proposalDocument p { margin-bottom: 9px; }
.proposalDocument table {
  width: 100%;
  min-width: 720px;
  border-collapse: collapse;
  margin: 12px 0;
  font-size: 13px;
}
.proposalDocument td,
.proposalDocument th {
  border: 1px solid var(--line);
  padding: 8px;
  text-align: left;
  vertical-align: top;
}
.proposalDocument tr:first-child td {
  background: var(--surface2);
  font-weight: 850;
}
.proposalQuestions {
  border-top: 1px solid var(--line);
  background: #fbfbfc;
  padding: 14px 16px;
  line-height: 1.45;
}
.proposalQuestions__head {
  margin-bottom: 8px;
  font-weight: 900;
}
.result { padding: 18px; min-height: 380px; line-height: 1.55; }
.result.empty { color: var(--muted); }
.result h1, .result h2, .result h3,
.questions h1, .questions h2, .questions h3 { color: var(--ink); margin: 18px 0 8px; letter-spacing: 0; }
.result h1:first-child, .result h2:first-child, .result h3:first-child,
.questions h1:first-child, .questions h2:first-child, .questions h3:first-child { margin-top: 0; }
.result p, .questions p { margin-bottom: 10px; }
.result table, .questions table { width: 100%; border-collapse: collapse; margin: 14px 0; font-size: 14px; }
.result th, .result td, .questions th, .questions td { border: 1px solid var(--line); padding: 8px; text-align: left; vertical-align: top; }
.result th, .questions th { background: var(--surface2); }
.result code, .questions code { background: var(--surface2); padding: 2px 4px; border-radius: 4px; }

.questionsPane {
  border-top: 1px solid var(--line);
  background: #fbfbfc;
  padding: 16px;
}
.questionsPane__head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}
.questionsPane__head span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}
.questions {
  color: var(--ink);
  line-height: 1.5;
}

.paneChat {
  display: flex;
  flex-direction: column;
}
.app[data-mobile-tab="table"] .paneChat { display: none; }
.app[data-mobile-tab="chat"] .paneTable { display: none; }

.chatMessages {
  min-height: 52dvh;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scrollbar-width: none;
}
.chatMessages::-webkit-scrollbar { display: none; }
.chatEmpty {
  margin: auto;
  color: var(--muted);
  text-align: center;
  max-width: 360px;
  padding: 24px 10px;
}
.chatEmpty h3 {
  color: var(--ink);
  margin-bottom: 8px;
}
.chatMessage {
  max-width: 92%;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 10px 12px;
  background: #fff;
  line-height: 1.5;
}
.chatMessage--user {
  align-self: flex-end;
  background: var(--ink);
  color: #fff;
  border-color: var(--ink);
}
.chatMessage--assistant {
  align-self: flex-start;
  background: #fff;
}
.chatMessage__meta {
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
}
.chatMessage--user .chatMessage__meta {
  color: #c6cad1;
}
.chatMessage__body h1,
.chatMessage__body h2,
.chatMessage__body h3 {
  margin: 14px 0 8px;
  letter-spacing: 0;
}
.chatMessage__body h1:first-child,
.chatMessage__body h2:first-child,
.chatMessage__body h3:first-child {
  margin-top: 0;
}
.chatMessage__body p {
  margin: 0 0 8px;
}
.chatMessage__body table {
  width: 100%;
  border-collapse: collapse;
  margin: 10px 0;
  font-size: 13px;
}
.chatMessage__body td {
  border: 1px solid var(--line);
  padding: 7px;
  vertical-align: top;
}
.messageFiles {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}
.messageFiles a {
  border-radius: 999px;
  background: var(--surface2);
  color: var(--blue);
  padding: 5px 8px;
  font-size: 12px;
  font-weight: 800;
}
.messageQuestions {
  margin-top: 10px;
  border-top: 1px solid var(--line);
  padding-top: 10px;
}
.messageQuestions strong {
  display: block;
  margin-bottom: 6px;
}

/* Command bar: quick actions + composer (fixed bottom on mobile) */
.commandBar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 16;
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.quickActions {
  display: flex;
  gap: 7px;
  overflow-x: auto;
  scrollbar-width: none;
}
.quickActions::-webkit-scrollbar { display: none; }
.quickActions button {
  flex: 0 0 auto;
  padding: 8px 10px;
  border-radius: 999px;
  font-size: 12px;
  background: var(--surface2);
}
.chatComposer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}
.composer__files {
  position: relative;
  flex: 0 0 auto;
  width: 46px;
  height: 46px;
  padding: 0;
  display: grid;
  place-items: center;
  font-size: 18px;
  border-radius: 12px;
  background: var(--surface2);
  border-color: var(--line);
}
.composer__files[data-count]:not([data-count="0"])::after {
  content: attr(data-count);
  position: absolute;
  top: -5px;
  right: -5px;
  min-width: 18px;
  height: 18px;
  padding: 0 4px;
  display: grid;
  place-items: center;
  border-radius: 999px;
  background: var(--blue);
  color: #fff;
  font-size: 11px;
  font-weight: 900;
}
.chatComposer textarea {
  flex: 1 1 auto;
  min-height: 46px;
  max-height: 140px;
  resize: none;
  line-height: 1.35;
}
.composer__send {
  flex: 0 0 auto;
  width: 46px;
  height: 46px;
  min-height: 0;
  padding: 0;
  display: grid;
  place-items: center;
}
.composer__sendText { display: none; }
.composer__icon { display: none; }
.composer__send .composer__icon--mic { display: block; }
.composer__send.is-send .composer__icon--mic { display: none; }
.composer__send.is-send .composer__icon--send { display: block; }
.composer__send.is-listening {
  background: var(--danger);
  border-color: var(--danger);
  animation: micPulse 1.2s ease-in-out infinite;
}
.composer__send.is-listening .composer__icon--send { display: none; }
.composer__send.is-listening .composer__icon--mic { display: block; }
@keyframes micPulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(220, 38, 38, .45); }
  50% { box-shadow: 0 0 0 7px rgba(220, 38, 38, 0); }
}

@media (min-width: 821px) {
  .app { display: grid; grid-template-columns: 300px minmax(0, 1fr); }
  .sidebar { position: static; width: auto; transform: none; transition: none; box-shadow: none; padding: 20px; z-index: auto; }
  .workspace { padding: 0; }
  .topbar { padding: 12px 18px; align-items: flex-start; gap: 16px; position: sticky; top: 0; z-index: 10; }
  .topbar p { display: block; font-size: 13px; }
  h1 { font-size: 22px; }
  h2 { font-size: 17px; }
  .mobileHistoryBtn, .mobileTabs { display: none; }
  .topbar__status { max-width: none; font-size: 13px; padding: 8px 12px; border-radius: 999px; }
  .workgrid { grid-template-columns: minmax(360px, 520px) minmax(0, 1fr); gap: 18px; padding: 18px; }
  .panel, .resultPane { box-shadow: var(--shadow); }

  /* Files: back to a static left column */
  .inputPane {
    position: static;
    transform: none;
    transition: none;
    max-height: none;
    overflow: visible;
    border-radius: 0;
    box-shadow: none;
    padding-bottom: 0;
    z-index: auto;
  }
  .inputPane .panel { border: 1px solid var(--line); border-radius: 16px; padding: 16px; box-shadow: var(--shadow); }
  .sheet__handle, .sheet__head { display: none; }
  .sheetBackdrop { display: none !important; }

  /* Both panes visible, table grows, chat keeps a fixed slice */
  .resultPane { min-height: calc(100dvh - 86px); }
  .app[data-mobile-tab] .paneTable,
  .app[data-mobile-tab] .paneChat { display: flex; }
  .paneTable { flex: 1 1 auto; min-height: 0; flex-direction: column; }
  .estimateView { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; }
  .proposalDocument { padding: 18px 22px; flex: 1 1 auto; min-height: 0; overflow: auto; }
  .proposalQuestions { padding: 16px 22px; flex: 0 0 auto; }
  .paneChat { flex: 0 0 auto; }
  .chatMessages { min-height: 0; max-height: 250px; padding: 18px; }

  /* Command bar flows under the chat */
  .commandBar { position: static; z-index: auto; border-top: 1px solid var(--line); padding: 16px 18px; }
  .composer__files { display: none; }
  .composer__send { width: auto; height: auto; min-height: 45px; padding: 10px 18px; }
  .composer__send.is-listening { animation: none; }
  .composer__sendText { display: inline; }
  .composer__send .composer__icon { display: none !important; }
  .chatMessage { max-width: 82%; }
}

@media (max-width: 560px) {
  .panel__head, .resultToolbar { align-items: flex-start; }
  .resultToolbar { display: grid; }
  .resultToolbar__actions { width: 100%; }
  .resultToolbar__actions button { flex: 1; }
}

@media print {
  body { background: #fff; }
  .sidebar, .topbar, .mobileTabs, .inputPane, .resultToolbar, .paneChat, .commandBar, .sheetBackdrop, .proposalQuestions { display: none !important; }
  .app, .workgrid { display: block; }
  .workspace { padding: 0; }
  .resultPane { border: 0; box-shadow: none; min-height: 0; }
  .paneTable { display: block !important; }
  .proposalDocument { padding: 0; overflow: visible; max-height: none; }
  .proposalDocument table { min-width: 0; font-size: 11px; }
}
