/* ==========================================================================
   Xedeshin Viewer — Document Tabs, Viewers, Partial Editors & Compare Diff
   ========================================================================== */

.doc-workspace {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  gap: var(--space-200);
  min-height: 0;
}

/* Document Action Toolbar */
.doc-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: var(--space-100);
  padding: var(--space-100) var(--space-200);
  border-radius: var(--radius-2xl);
}

.doc-toolbar-left {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-75) var(--space-100);
  min-width: 0;
  flex: 1 1 260px;
}

.doc-title-group {
  display: flex;
  align-items: center;
  gap: var(--space-75);
  min-width: 0;
  flex: 0 1 auto;
  max-width: 100%;
}

.doc-title-group svg {
  flex-shrink: 0;
  color: var(--text-secondary);
}

.doc-title-input {
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  padding: var(--space-25) var(--space-75);
  color: var(--text-primary);
  min-width: 0;
  max-width: 280px;
  flex: 1 1 auto;
  transition:
    border-color var(--duration-hover) var(--ease-liquid),
    background-color var(--duration-hover) var(--ease-liquid);
}

.doc-title-input:not([readonly]):hover,
.doc-title-input:not([readonly]):focus {
  border-color: var(--border-medium);
  background: var(--bg-surface-2);
  outline: none;
}

.doc-meta-text {
  color: var(--text-muted);
  white-space: nowrap;
}

.doc-toolbar-right {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-75);
}

/* Document Viewport Container */
.doc-stage {
  flex: 1 1 auto;
  min-height: 62dvh;
  border-radius: var(--radius-3xl);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* Markdown & Split Editor */
.md-layout {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: 1fr;
  min-height: 62dvh;
}

.md-layout.is-split {
  grid-template-columns: 1fr 1fr;
}

.editor-pane {
  display: flex;
  flex-direction: column;
  min-height: 62dvh;
  background: var(--bg-surface-1);
}

.editor-subbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: var(--space-75);
  padding: var(--space-75) var(--space-100);
  background: var(--bg-surface-2);
  border: 1px solid var(--border-subtle);
  margin: var(--space-100);
  border-radius: var(--radius-xl);
}

.subbar-group {
  display: inline-flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-50);
}

.subbar-btn {
  padding: var(--space-50) var(--space-75);
  border-radius: var(--radius-md);
  border: 1px solid var(--border-subtle);
  background: var(--bg-surface-1);
  color: var(--text-secondary);
  transition:
    background-color var(--duration-hover) var(--ease-liquid),
    border-color var(--duration-hover) var(--ease-liquid),
    color var(--duration-hover) var(--ease-liquid);
}

.subbar-btn:hover {
  background: var(--bg-surface-3);
  border-color: var(--border-medium);
  color: var(--text-primary);
}

.subbar-btn.is-active {
  background: var(--bg-surface-4);
  border-color: var(--border-strong);
  color: var(--text-primary);
}

.subbar-seg {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 2px;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-subtle);
  background: var(--bg-surface-1);
}

.subbar-seg-btn {
  padding: var(--space-50) var(--space-75);
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  white-space: nowrap;
  transition:
    background-color var(--duration-hover) var(--ease-liquid),
    color var(--duration-hover) var(--ease-liquid);
}

.subbar-seg-btn:hover {
  color: var(--text-primary);
}

.subbar-seg-btn.is-active {
  background: var(--bg-surface-4);
  color: var(--text-primary);
}

@media (max-width: 1140px) {
  .editor-subbar {
    padding: var(--space-75);
    gap: var(--space-50);
  }

  .subbar-btn,
  .subbar-seg-btn {
    padding: var(--space-50) 6px;
  }
}

.code-editor-wrap {
  flex: 1 1 auto;
  display: flex;
  min-width: 0;
  min-height: 460px;
  overflow: auto;
}

.line-numbers {
  padding: var(--space-200) var(--space-100);
  text-align: right;
  color: var(--text-muted);
  user-select: none;
  font-family: var(--font-mono);
  font-size: 0.875rem;
  line-height: 1.65;
  min-width: 48px;
}

.code-textarea {
  flex: 1 1 auto;
  width: 100%;
  min-width: 0;
  min-height: 460px;
  padding: var(--space-200);
  border: none;
  background: transparent;
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: calc(var(--user-font-size, 16px) - 2px);
  font-weight: var(--user-font-weight, 400);
  line-height: 1.65;
  resize: none;
  outline: none;
  white-space: pre;
  overflow-x: auto;
}

.code-textarea.is-soft-wrap {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  overflow-x: hidden;
}

.code-pre-view {
  flex: 1 1 auto;
  margin: 0;
  padding: var(--space-200);
  font-family: var(--font-mono);
  font-size: calc(var(--user-font-size, 16px) - 2px);
  font-weight: var(--user-font-weight, 400);
  line-height: 1.65;
  color: var(--text-primary);
  white-space: pre-wrap;
  word-break: break-word;
}

/* Rendered Markdown / Rich Document Canvas */
.prose-canvas {
  flex: 1 1 auto;
  padding: var(--space-400);
  overflow-y: auto;
  background: var(--paper-bg);
}

.prose-inner {
  max-width: 760px;
  margin: 0 auto;
  color: var(--text-primary);
  font-size: var(--user-font-size, 16px);
  font-weight: var(--user-font-weight, 400);
  line-height: 1.7;
}

.prose-inner h1,
.prose-inner h2,
.prose-inner h3,
.prose-inner h4 {
  margin: 1.5em 0 0.6em;
  font-weight: 600;
  line-height: 1.25;
  text-wrap: balance;
}

.prose-inner h1:first-child,
.prose-inner h2:first-child {
  margin-top: 0;
}

.prose-inner p {
  margin: 0 0 1em;
  color: var(--text-secondary);
}

.prose-inner strong {
  color: var(--text-primary);
  font-weight: 600;
}

.prose-inner ul,
.prose-inner ol {
  margin: 0 0 1.2em;
  padding-left: var(--space-300);
  color: var(--text-secondary);
}

.prose-inner li {
  margin-bottom: var(--space-50);
}

.prose-inner blockquote {
  margin: var(--space-200) 0;
  padding: var(--space-100) var(--space-200);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-medium);
  background: var(--bg-surface-2);
  color: var(--text-secondary);
}

.prose-inner pre {
  padding: var(--space-200);
  border-radius: var(--radius-xl);
  border: 1px solid var(--border-subtle);
  background: var(--bg-surface-2);
  overflow-x: auto;
  font-size: 0.875rem;
}

.prose-inner code {
  padding: 2px 6px;
  border-radius: var(--radius-sm);
  background: var(--bg-surface-3);
  font-size: 0.875em;
}

.prose-inner pre code {
  padding: 0;
  background: transparent;
}

.prose-inner table {
  width: 100%;
  border-collapse: collapse;
  margin: var(--space-200) 0;
  border: 1px solid var(--border-medium);
  border-radius: var(--radius-lg);
  overflow: hidden;
}

.prose-inner th,
.prose-inner td {
  padding: var(--space-75) var(--space-100);
  border: 1px solid var(--border-subtle);
  text-align: left;
}

.prose-inner th {
  background: var(--bg-surface-2);
  font-weight: 600;
}

.prose-editable[contenteditable="true"] {
  outline: none;
  padding: var(--space-200);
  border-radius: var(--radius-xl);
  border: 1px solid var(--accent-edit-border);
  background: var(--bg-surface-1);
}

/* Spreadsheet Viewer / Editor (XLSX, CSV) */
.sheet-shell {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 62dvh;
}

.sheet-formula-bar {
  display: flex;
  align-items: center;
  gap: var(--space-100);
  padding: var(--space-75) var(--space-200);
  margin: var(--space-100) var(--space-200) 0;
  border-radius: var(--radius-xl);
  background: var(--bg-surface-2);
  border: 1px solid var(--border-subtle);
}

.sheet-cell-badge {
  min-width: 56px;
  text-align: center;
  padding: var(--space-25) var(--space-75);
  border-radius: var(--radius-md);
  background: var(--bg-surface-3);
  font-family: var(--font-mono);
}

.sheet-grid-scroll {
  flex: 1 1 auto;
  overflow: auto;
  padding: var(--space-200);
}

.sheet-table {
  border-collapse: collapse;
  min-width: 100%;
  font-size: 0.875rem;
}

.sheet-table th,
.sheet-table td {
  border: 1px solid var(--border-subtle);
  padding: var(--space-75) var(--space-100);
  min-width: 112px;
  max-width: 320px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sheet-table th {
  background: var(--bg-surface-2);
  color: var(--text-secondary);
  font-weight: 600;
  text-align: center;
  position: sticky;
  top: 0;
  z-index: 2;
}

.sheet-table .row-num-cell {
  min-width: 48px;
  width: 48px;
  text-align: center;
  color: var(--text-muted);
  background: var(--bg-surface-2);
  position: sticky;
  left: 0;
  z-index: 1;
}

.sheet-table td[contenteditable="true"] {
  background: var(--bg-surface-1);
  cursor: text;
}

.sheet-table td.is-selected-cell {
  outline: 2px solid var(--border-focus);
  outline-offset: -2px;
}

.sheet-tabs-footer {
  display: flex;
  align-items: center;
  gap: var(--space-75);
  padding: var(--space-100) var(--space-200);
  overflow-x: auto;
}

/* Image Viewer (Read-Only) */
.image-stage {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 62dvh;
}

.image-canvas-wrap {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-300);
  overflow: auto;
  background-image:
    linear-gradient(45deg, var(--checker-a) 25%, transparent 25%),
    linear-gradient(-45deg, var(--checker-a) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, var(--checker-b) 75%),
    linear-gradient(-45deg, transparent 75%, var(--checker-b) 75%);
  background-size: 24px 24px;
  background-position: 0 0, 0 12px, 12px -12px, -12px 0;
}

.image-preview-el {
  max-width: 100%;
  max-height: 60dvh;
  object-fit: contain;
  border-radius: var(--radius-lg);
  box-shadow: var(--glass-shadow);
  transition: transform var(--duration-hover) var(--ease-liquid);
}

/* Presentation Viewer (PPTX, Read-Only) */
.pptx-shell {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: 220px 1fr;
  gap: var(--space-200);
  padding: var(--space-200);
  min-height: 62dvh;
}

.pptx-sidebar {
  display: flex;
  flex-direction: column;
  gap: var(--space-75);
  overflow-y: auto;
  max-height: 66dvh;
  padding-right: var(--space-50);
}

.pptx-thumb {
  padding: var(--space-100);
  border-radius: var(--radius-xl);
  border: 1px solid var(--border-subtle);
  background: var(--bg-surface-2);
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: var(--space-50);
  transition:
    border-color var(--duration-hover) var(--ease-liquid),
    background-color var(--duration-hover) var(--ease-liquid);
}

.pptx-thumb:hover {
  border-color: var(--border-medium);
}

.pptx-thumb.is-active {
  border-color: var(--border-strong);
  background: var(--bg-surface-3);
}

.pptx-stage-wrap {
  display: flex;
  flex-direction: column;
  gap: var(--space-200);
  align-items: center;
  justify-content: center;
}

.pptx-slide-card {
  width: 100%;
  max-width: 860px;
  aspect-ratio: 16 / 9;
  padding: var(--space-500);
  border-radius: var(--radius-2xl);
  border: 1px solid var(--border-medium);
  background: var(--bg-surface-2);
  box-shadow: var(--specular-top), var(--glass-shadow);
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: var(--space-200);
  overflow-y: auto;
  animation: tabEnter 360ms var(--ease-liquid);
}

.pptx-slide-title {
  margin: 0;
  color: var(--text-primary);
}

.pptx-slide-list {
  margin: 0;
  padding-left: var(--space-300);
  display: flex;
  flex-direction: column;
  gap: var(--space-100);
  color: var(--text-secondary);
}

.pptx-slide-media {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-100);
}

.pptx-slide-media img {
  max-height: 220px;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-subtle);
}

.pptx-notes-box {
  width: 100%;
  max-width: 860px;
  padding: var(--space-100) var(--space-200);
  border-radius: var(--radius-xl);
  border: 1px solid var(--border-subtle);
  background: var(--bg-surface-2);
  color: var(--text-secondary);
}

/* PDF Viewer */
.pdf-stage {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-200);
  padding: var(--space-300);
  overflow: auto;
  max-height: 74dvh;
}

.pdf-page-canvas {
  border-radius: var(--radius-lg);
  box-shadow: var(--glass-shadow);
  max-width: 100%;
  height: auto;
}

/* Compare Multi-Pane & Diff View */
.compare-workspace {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  gap: var(--space-200);
}

.compare-split-grid {
  flex: 1 1 auto;
  display: grid;
  gap: var(--space-200);
  min-height: 60dvh;
}

.compare-split-grid.cols-2 {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.compare-split-grid.cols-3 {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.compare-split-grid.cols-4 {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.compare-pane {
  display: flex;
  flex-direction: column;
  border-radius: var(--radius-2xl);
  border: 1px solid var(--border-medium);
  background: var(--glass-bg);
  overflow: hidden;
  min-height: 360px;
}

.compare-pane-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-100);
  padding: var(--space-100) var(--space-200);
  background: var(--bg-surface-2);
  border: 1px solid var(--border-subtle);
  margin: var(--space-75);
  border-radius: var(--radius-xl);
}

.compare-pane-body {
  flex: 1 1 auto;
  overflow: auto;
  padding: var(--space-200);
}

/* Diff Table */
.diff-table-wrap {
  flex: 1 1 auto;
  overflow: auto;
  border-radius: var(--radius-2xl);
  border: 1px solid var(--border-medium);
  background: var(--bg-surface-1);
}

.diff-table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--font-mono);
  font-size: 0.875rem;
  line-height: 1.55;
}

.diff-row td {
  padding: 3px var(--space-100);
  vertical-align: top;
  white-space: pre-wrap;
  word-break: break-word;
}

.diff-ln {
  width: 44px;
  min-width: 44px;
  text-align: right;
  color: var(--text-muted);
  user-select: none;
  border-right: 1px solid var(--border-subtle);
}

.diff-row.is-added td {
  background: var(--accent-edit-bg);
}

.diff-row.is-removed td {
  background: var(--accent-danger-bg);
}

.diff-row.is-added .diff-sign {
  color: var(--accent-edit);
}

.diff-row.is-removed .diff-sign {
  color: var(--accent-danger);
}

/* Image Slider Overlay Compare */
.img-slider-compare {
  position: relative;
  max-width: 820px;
  width: 100%;
  margin: 0 auto;
  aspect-ratio: 16 / 10;
  border-radius: var(--radius-2xl);
  overflow: hidden;
  border: 1px solid var(--border-medium);
  background: var(--bg-surface-2);
  user-select: none;
}

.img-slider-layer {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.img-slider-layer img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.img-slider-handle {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 2px;
  background: var(--text-primary);
  z-index: 4;
  pointer-events: none;
}

.img-slider-range {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: ew-resize;
  z-index: 5;
  margin: 0;
}

@media (max-width: 900px) {
  .md-layout.is-split {
    grid-template-columns: 1fr;
  }

  .pptx-shell {
    grid-template-columns: 1fr;
  }

  .pptx-sidebar {
    flex-direction: row;
    max-height: none;
  }

  .pptx-thumb {
    min-width: 160px;
  }

  .compare-split-grid.cols-2,
  .compare-split-grid.cols-3,
  .compare-split-grid.cols-4 {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 640px) {
  .prose-canvas {
    padding: var(--space-200) var(--space-100);
  }

  .pptx-slide-card {
    aspect-ratio: auto;
    min-height: 220px;
    padding: var(--space-200);
  }
}

@media (max-width: 480px) {
  .doc-toolbar {
    padding: var(--space-100);
  }

  .doc-title-group {
    flex: 1 1 100%;
  }

  .doc-toolbar-right {
    width: 100%;
  }

  .doc-toolbar-right .btn {
    flex: 1 1 auto;
  }

  .sheet-formula-bar {
    flex-wrap: wrap;
    margin: var(--space-100) var(--space-100) 0;
    padding: var(--space-75) var(--space-100);
  }

  .sheet-formula-bar [data-sheet-search] {
    max-width: none !important;
    width: 100%;
    order: 10;
  }
}
