@import "../shared.css";

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr;
  background: var(--page-bg);
  color: var(--foreground);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overflow: hidden;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  min-height: 56px;
  padding: 10px 12px;
  background: var(--background);
  border-bottom: 1px solid var(--border);
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  padding-right: 8px;
  border-right: 1px solid #e5e7eb;
}

.toolbar-group:last-of-type {
  border-right: 0;
}

select,
input,
button,
a {
  height: 36px;
  border: 1px solid #9ca3af;
  border-radius: 6px;
  background-color: #fff;
  color: #111827;
  font-size: 14px;
}

select {
  min-width: 150px;
  background-color: var(--surface);
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%236b7280' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  background-size: 14px;
  padding-right: 28px;
  cursor: pointer;
  transition: border-color 0.15s;
}

select:hover {
  border-color: #6b7280;
}

button {
  background: #e5e7eb;
}

.danger {
  color: #dc2626;
  background: #fef2f2;
  border-color: #fecaca;
}

input {
  min-width: 0;
  padding: 0 8px;
}

.course-controls {
  flex: 1 1 520px;
}

.course-controls #course {
  flex: 1 1 240px;
}

.version-controls {
  flex: 0 1 620px;
}

.version-controls input {
  flex: 1 1 160px;
}

.version-controls select {
  flex: 1 1 200px;
}

button,
a {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 12px;
  text-decoration: none;
  cursor: pointer;
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.primary {
  background: #00377b;
  color: #fff;
  border-color: #00377b;
}

#status {
  margin-left: auto;
  max-width: 360px;
  min-height: 28px;
  display: inline-flex;
  align-items: center;
  padding: 4px 8px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  background: #f8fafc;
  color: #374151;
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#status[hidden] {
  display: none;
}

#status.error {
  border-color: #fecaca;
  background: #fef2f2;
  color: #991b1b;
}

#status.ready {
  border-color: #bbf7d0;
  background: #f0fdf4;
  color: #166534;
}

.message.tool {
  background: #f8fafc;
  border-left: 3px solid #64748b;
  color: #475569;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 12px;
  padding: 4px 8px;
}

.message.tool .message-body {
  white-space: pre-wrap;
  word-break: break-word;
}

.message.tool.tool-call {
  border-left-color: #3b82f6;
}

.message.tool.tool-result {
  border-left-color: #22c55e;
}

.chat-status {
  padding: 4px 12px;
  font-size: 12px;
  color: #6b7280;
  border-top: 1px solid #e5e7eb;
  background: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-height: 0;
  line-height: 20px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.spinner {
  display: none;
  width: 16px;
  height: 16px;
  border: 2px solid #e5e7eb;
  border-top-color: #00377b;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}

.spinner.active {
  display: block;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 460px;
  gap: 12px;
  min-height: 0;
  padding: 12px;
  transition: grid-template-columns 0.18s ease;
}

.workspace.chat-focus {
  grid-template-columns: minmax(0, 1fr) minmax(360px, 1.5fr);
}

.preview-pane,
.side-pane {
  min-height: 0;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  background: #fff;
  overflow: hidden;
}

.preview-pane {
  position: relative;
}

iframe {
  display: block;
  width: 100%;
  height: 100%;
  border: 0;
}

.loading {
  position: absolute;
  inset: 0;
  display: none;
  place-items: center;
  background: rgba(255, 255, 255, 0.82);
  color: #00377b;
  font-weight: 700;
}

.loading.active {
  display: grid;
}

.side-pane {
  display: grid;
  grid-template-rows: auto 1fr;
  min-width: 0;
}

.tabs {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  border-bottom: 1px solid #d1d5db;
}

.tab {
  width: 100%;
  border: 0;
  border-radius: 0;
  margin: 0;
  color: #00377b;
  font-weight: 700;
}

.tab.active {
  background: #00377b;
  color: #fff;
}

.panel {
  display: none;
  min-height: 0;
}

.panel.active {
  display: grid;
  min-height: 0;
}

.chat-panel {
  grid-template-rows: auto 1fr auto auto;
}

.thread-bar {
  display: grid;
  grid-template-columns: 1fr auto auto auto;
  gap: 8px;
  padding: 10px;
  border-bottom: 1px solid #d1d5db;
  background: #fff;
  align-items: center;
}

.thread-selector {
  display: flex;
  align-items: center;
  gap: 4px;
  min-width: 0;
}

.thread-selector select {
  flex: 1;
  min-width: 0;
  max-width: none;
  width: 100%;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  padding: 0;
  border: 1px solid #9ca3af;
  border-radius: 4px;
  background: #fff;
  cursor: pointer;
  color: #6b7280;
  flex-shrink: 0;
}

.icon-btn:hover {
  background: #f3f4f6;
  color: #00377b;
}

.icon-btn.active {
  background: #00377b;
  border-color: #00377b;
  color: #fff;
}

.context-badge {
  font-size: 11px;
  font-weight: 500;
  color: #6b7280;
  background: #f3f4f6;
  border: 1px solid #d1d5db;
  border-radius: 10px;
  padding: 2px 8px;
  white-space: nowrap;
  flex-shrink: 0;
}

.context-info {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 1px;
  flex-shrink: 0;
}

.context-usage {
  font-size: 10px;
  color: #9ca3af;
  white-space: nowrap;
}

.chat-log {
  min-height: 0;
  overflow: auto;
  padding: 12px;
  background: #f8fafc;
}

.message {
  max-width: 92%;
  margin-bottom: 10px;
  padding: 9px 10px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  background: #fff;
  color: #111827;
  font-size: 14px;
  line-height: 1.4;
  white-space: pre-wrap;
  overflow: hidden;
  overflow-wrap: break-word;
}

.message.error {
  border-color: #fecaca;
  background: #fef2f2;
}

.message.user {
  margin-left: auto;
  background: #eaf2ff;
  border-color: #b9d1f5;
}

.message.tool {
  margin-left: auto;
  margin-right: auto;
  background: #f3f4f6;
  border-color: #d1d5db;
  color: #374151;
  font-size: 12px;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  max-width: 92%;
  padding: 6px 10px;
}

.message.tool.tool-call {
  background: #eef2ff;
  border-color: #c7d2fe;
  color: #312e81;
}

.message.tool.tool-result {
  background: #f0fdf4;
  border-color: #bbf7d0;
  color: #166534;
}

.message-time {
  margin-top: 4px;
  color: #6b7280;
  font-size: 11px;
  white-space: normal;
}

.message-body {
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: break-word;
}

.message-body h1, .message-body h2, .message-body h3, .message-body h4 {
  margin: 8px 0 4px;
  font-weight: 600;
}
.message-body h1 { font-size: 16px; }
.message-body h2 { font-size: 14px; }
.message-body h3 { font-size: 13px; }
.message-body h4 { font-size: 12px; }

.message-body hr {
  border: 0;
  border-top: 1px solid #d1d5db;
  margin: 8px 0;
}

.message-body ul, .message-body ol {
  margin: 4px 0;
  padding-left: 20px;
}

.message-body li {
  list-style: disc;
}

.message-body ol li {
  list-style: decimal;
}

.message-body table {
  border-collapse: collapse;
  width: 100%;
  margin: 8px 0;
  font-size: 12px;
}
.message-body th, .message-body td {
  border: 1px solid #d1d5db;
  padding: 4px 8px;
  text-align: left;
}
.message-body th {
  background: #f3f4f6;
  font-weight: 600;
}

.message-body pre {
  background: #1e293b;
  color: #e2e8f0;
  padding: 10px 12px;
  border-radius: 6px;
  overflow-x: auto;
  margin: 8px 0;
  font-size: 12px;
  line-height: 1.5;
}
.message-body pre code {
  background: none;
  border: 0;
  padding: 0;
  color: inherit;
  font-size: inherit;
}

.message-body code {
  padding: 1px 4px;
  border: 1px solid #d1d5db;
  border-radius: 3px;
  background: #f3f4f6;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 12px;
}

.message-body code a {
  padding: 0;
  border: none;
  border-radius: 0;
  background: none;
  font-family: inherit;
  font-size: inherit;
  color: #00377b;
  text-decoration: underline;
  word-break: break-all;
}

.message-body blockquote {
  border-left: 3px solid #d1d5db;
  margin: 8px 0;
  padding: 4px 12px;
  color: #4b5563;
}

.message-body a {
  color: #00377b;
  text-decoration: underline;
  display: inline;
  word-break: break-all;
  background: none;
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
}

.message-body strong {
  font-weight: 600;
}

.message-body p {
  margin: 4px 0;
}

.message-body p:first-child {
  margin-top: 0;
}
.message-body p:last-child {
  margin-bottom: 0;
}

.attachments {
  display: grid;
  gap: 6px;
  margin-top: 8px;
}

.attachment {
  display: grid;
  grid-template-columns: 42px 1fr auto;
  align-items: center;
  gap: 8px;
  padding: 6px;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  background: #fff;
  font-size: 12px;
}

.attachment img {
  width: 42px;
  height: 42px;
  object-fit: cover;
  border: 1px solid #d1d5db;
}

.attachment-icon {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  width: 48px;
  height: 48px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  color: #00377b;
  background: #f8fafc;
  flex-shrink: 0;
}

.attachment-icon-label {
  font-size: 8px;
  font-weight: 700;
  letter-spacing: 0.5px;
  color: #6b7280;
  line-height: 1;
}

.attachment-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.attachment-meta {
  color: #6b7280;
  font-size: 11px;
}

.attachment-meta.ready {
  color: #166534;
}

.attachment-meta.failed,
.attachment-meta.unsupported {
  color: #991b1b;
}

.attachment-remove {
  height: 26px;
  padding: 0 8px;
}

.attachment-download {
  width: 26px;
  height: 26px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: #00377b;
  background: #eaf2ff;
  border: 1px solid #b3cde8;
  border-radius: 4px;
  text-decoration: none;
  cursor: pointer;
  flex-shrink: 0;
}

.attachment-download:hover {
  background: #dbeafe;
}

.attachment-preview {
  height: 26px;
  padding: 0 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 500;
  color: #166534;
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
  border-radius: 4px;
  cursor: pointer;
}

.attachment-preview:hover {
  background: #dcfce7;
}

.attachment-actions {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-shrink: 0;
}

.attachment-download:hover {
  background: #d4e4f7;
}

.composer {
  display: grid;
  gap: 8px;
  padding: 10px;
  border-top: 1px solid #d1d5db;
  background: #fff;
}

.composer textarea,
.fields-panel textarea {
  width: 100%;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  color: #111827;
  font: 13px/1.45 Arial, sans-serif;
}

.composer textarea {
  min-height: 84px;
  resize: vertical;
  padding: 9px;
}

.composer-actions {
  display: grid;
  grid-template-columns: auto 1fr auto auto;
  gap: 8px;
  align-items: center;
}

.stop-btn {
  background: #dc2626;
  color: #fff;
  border: none;
  border-radius: 4px;
  padding: 0 12px;
  height: 28px;
  font: 13px/1 Arial, sans-serif;
  cursor: pointer;
}

.stop-btn:hover {
  background: #b91c1c;
}

.version-display {
  display: inline-flex;
  align-items: center;
  padding: 0 8px;
  font: 12px/1 Arial, sans-serif;
  color: #6b7280;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 200px;
}

.fields-panel {
  grid-template-rows: 1fr auto;
}

.review-panel {
  grid-template-rows: auto 1fr auto;
  min-height: 0;
}

.review-summary {
  display: grid;
  gap: 6px;
  padding: 12px;
  border-bottom: 1px solid #d1d5db;
  font-size: 13px;
}

.review-summary strong {
  color: #00377b;
}

.document-draft {
  display: grid;
  gap: 8px;
  padding: 10px;
  border-bottom: 1px solid #d1d5db;
}

.draft-loader {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 118px;
  gap: 8px;
}

.draft-loader select {
  max-width: none;
  min-width: 0;
  overflow: hidden;
}

.diff-view {
  min-height: 0;
  overflow: auto;
  margin: 0;
  background: #fff;
  color: #24292f;
  font: 12px/1.45 ui-monospace, SFMono-Regular, Consolas, monospace;
  border-top: 1px solid #d1d5db;
  border-bottom: 1px solid #d1d5db;
}

.diff-empty {
  padding: 12px;
  color: #6b7280;
}

.diff-row {
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr);
  min-height: 25px;
  border-bottom: 1px solid rgba(31, 35, 40, 0.06);
}

.diff-row.added {
  background: #dafbe1;
}

.diff-row.removed {
  background: #ffebe9;
}

.diff-row.meta,
.diff-row.header,
.diff-row.title {
  background: #f6f8fa;
  color: #57606a;
  font-weight: 700;
}

.diff-row.header {
  color: #0969da;
}

.diff-sign {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 3px;
  color: #57606a;
  user-select: none;
  border-right: 1px solid rgba(31, 35, 40, 0.08);
}

.diff-code {
  padding: 3px 8px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.fields-panel textarea {
  min-height: 0;
  resize: none;
  border: 0;
  border-radius: 0;
  padding: 12px;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
}

.field-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  padding: 10px;
  border-top: 1px solid #d1d5db;
}

@media (max-width: 980px) {
  .toolbar {
    flex-wrap: wrap;
  }

  .toolbar-group {
    width: 100%;
    border-right: 0;
    padding-right: 0;
  }

  .course-controls,
  .version-controls {
    flex-wrap: wrap;
  }

  #status {
    width: 100%;
    max-width: none;
    margin-left: 0;
  }

  .workspace {
    grid-template-columns: 1fr;
    height: auto;
  }

  .preview-pane,
  .side-pane {
    height: 72dvh;
  }

  .thread-bar {
    grid-template-columns: 1fr;
  }

  .thread-selector {
    width: 100%;
  }

  #chat-title {
    width: 100%;
  }
}

.preview-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.5);
}

.preview-overlay[hidden] {
  display: none;
}

.preview-modal {
  background: #fff;
  border-radius: 8px;
  width: 90vw;
  max-width: 900px;
  height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}

.preview-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid #e5e7eb;
  background: #f9fafb;
  flex-shrink: 0;
}

.preview-filename {
  font-size: 14px;
  font-weight: 600;
  color: #111827;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.preview-close {
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: #6b7280;
  background: none;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  flex-shrink: 0;
}

.preview-close:hover {
  background: #e5e7eb;
  color: #111827;
}

.preview-body {
  flex: 1;
  overflow: auto;
  padding: 0;
}

.preview-loading,
.preview-error {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #6b7280;
  font-size: 14px;
  padding: 16px;
}

.preview-error {
  color: #991b1b;
}

.preview-iframe {
  width: 100%;
  height: 100%;
  border: none;
}

.preview-rendered {
  line-height: 1.6;
  color: #1f2937;
  padding: 16px;
}

.preview-rendered h1,
.preview-rendered h2,
.preview-rendered h3 {
  color: #00377b;
  margin: 16px 0 8px;
}

.preview-rendered table {
  border-collapse: collapse;
  width: 100%;
  margin: 8px 0;
}

.preview-rendered th,
.preview-rendered td {
  border: 1px solid #d1d5db;
  padding: 6px 10px;
  text-align: left;
  font-size: 13px;
}

.preview-rendered th {
  background: #f3f4f6;
  font-weight: 600;
}

.preview-table {
  border-collapse: collapse;
  width: 100%;
  margin: 0;
  font-size: 13px;
}

.preview-table th,
.preview-table td {
  border: 1px solid #d1d5db;
  padding: 6px 10px;
  text-align: left;
}

.preview-table th {
  background: #00377b;
  color: #fff;
  font-weight: 600;
  position: sticky;
  top: 0;
}

.preview-table tbody tr:nth-child(even) {
  background: #f8fafc;
}

.preview-table tbody tr:hover {
  background: #e0e7ff;
}

.preview-text {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 13px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
  margin: 0;
  color: #1f2937;
  padding: 16px;
}

@media (max-width: 980px) {
  .toolbar { min-height: auto; padding: 8px 10px; gap: 6px; }
  .toolbar-group { padding-right: 6px; gap: 6px; }
  .toolbar-group.version-controls { display: none; }
  .side-pane { min-width: 0; }
}

@media (max-width: 768px) {
  .workspace { grid-template-columns: 1fr !important; }
  .preview-pane { display: none; }
  .side-pane { border-left: 0; }
  .toolbar-group.version-controls { display: none; }
  .toolbar-group.layout-controls { display: none; }
  .toolbar-group.auth-controls { margin-left: auto; }
}
