.document-editor-workspace.sheet {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  min-height: 0;
  padding: 0;
  overflow: hidden;
  background: #fffdfb;
}

main:has(.document-editor-workspace.sheet) {
  width: auto;
  max-width: none;
  margin-inline: 0;
  height: calc(100vh - 36px);
  min-height: 0;
  padding-bottom: 16px;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  overflow: hidden;
}
main:has(.document-editor-workspace.sheet) > header { margin-bottom: 12px; }
main:has(.document-editor-workspace.sheet) > #content { width: 100%; height: 100%; min-height: 0; overflow: hidden; }
main:has(.document-editor-workspace.sheet) .document-editor-shell { width: 100%; height: 100%; min-height: 0; }

.spreadsheet-shell {
  --sheet-border: #eadfd6;
  --sheet-muted: #897a6f;
  --sheet-accent: #ee8846;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  flex: 1 1 auto;
  width: 100%;
  height: 100%;
  min-height: 0;
  overflow: hidden;
  color: #2d2621;
  background: #fff;
}

.spreadsheet-toolbar {
  z-index: 4;
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  min-height: 48px;
  flex: 0 0 auto;
  padding: 7px 10px;
  border-bottom: 1px solid var(--sheet-border);
  background: #fffaf6;
}

.spreadsheet-action-strip {
  display: flex;
  flex: 1 1 auto;
  align-items: center;
  gap: 6px;
  min-width: 0;
  overflow: hidden;
}

.spreadsheet-toolbar button,
.spreadsheet-status button {
  min-width: 0;
  min-height: 34px;
  padding: 7px 11px;
  border: 1px solid #ead8ca;
  border-radius: 9px;
  color: #6f4c35;
  background: #fff;
  cursor: pointer;
}

.spreadsheet-toolbar button:hover,
.spreadsheet-status button:hover { border-color: #ef9a62; color: #c86128; }
.spreadsheet-toolbar button:disabled { opacity: .45; cursor: not-allowed; }
.spreadsheet-action-strip > button,
.spreadsheet-more-wrap > button { flex: 0 0 auto; white-space: nowrap; }
.spreadsheet-toolbar-state { display: flex; flex: 0 0 auto; align-items: center; gap: 9px; min-width: 0; color: var(--sheet-muted); font-size: 12px; white-space: nowrap; }
.spreadsheet-toolbar-state #sheetPresence { max-width: 96px; overflow: hidden; text-overflow: ellipsis; }
.spreadsheet-toolbar-state #sheetFullscreen { display: inline-flex; align-items: center; gap: 5px; color: #b75f31; }
.spreadsheet-toolbar-state #sheetFullscreen i { font-size: 16px; font-style: normal; line-height: 1; }

.spreadsheet-more-wrap { position: relative; flex: 0 0 auto; }
.spreadsheet-more-wrap.hidden { display: none; }
.spreadsheet-overflow-menu {
  position: absolute;
  z-index: 10070;
  top: 100%;
  right: 0;
  display: grid;
  grid-template-columns: repeat(2, minmax(118px, 1fr));
  gap: 5px;
  width: min(310px, calc(100vw - 24px));
  margin-top: 7px;
  padding: 8px;
  border: 1px solid #ead8ca;
  border-radius: 12px;
  box-shadow: 0 12px 32px rgba(74,48,31,.18);
  background: #fffdfb;
}
.spreadsheet-overflow-menu.hidden { display: none; }
.spreadsheet-overflow-menu button { width: 100%; justify-self: stretch; text-align: left; }

.spreadsheet-status {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  color: var(--sheet-muted);
  font-size: 12px;
  white-space: nowrap;
}

.spreadsheet-status::before {
  width: 7px;
  height: 7px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: #8dbe9c;
  content: '';
}

.spreadsheet-status[data-state="loading"]::before { background: #e8a35d; }
.spreadsheet-status[data-state="saving"]::before { background: #ee8846; animation: sheetPulse 1s infinite alternate; }
.spreadsheet-status[data-state="error"] { color: #b54d47; }
.spreadsheet-status[data-state="error"]::before { background: #d86d65; }

body.spreadsheet-immersive { overflow: hidden !important; }
.spreadsheet-shell.is-fullscreen {
  position: fixed;
  /* Keep focus mode above the workbench, but below Univer body portals and
     the shared modal (10040+).  The previous 10050 trapped every dropdown,
     color picker and permission dialog behind the fullscreen sheet. */
  z-index: 900;
  inset: 0;
  width: 100vw;
  height: 100vh;
  min-width: 0;
  min-height: 0;
  box-shadow: none;
}

@keyframes sheetPulse { to { opacity: .3; } }

.spreadsheet-stage {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  min-width: 0;
  min-height: 0;
  height: 100%;
  overflow: hidden;
}

.spreadsheet-drop-hint {
  position: absolute;
  z-index: 12;
  inset: 16px;
  display: none;
  place-items: center;
  border: 2px dashed #ee8846;
  border-radius: 16px;
  color: #b85b28;
  font-weight: 700;
  background: rgba(255, 246, 238, .94);
  pointer-events: none;
}

.spreadsheet-stage.is-dragging .spreadsheet-drop-hint { display: grid; }

#spreadsheetCanvas {
  position: relative;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background: #fff;
}

#spreadsheetCanvas > div:not([id^="univer-doc-selection-container-"]) { width: 100%; height: 100%; min-width: 0; min-height: 0; }

/* Univer creates this fixed, hidden contenteditable host only while a cell is
   being edited. It must stay 1px; stretching it to the root size turns it into
   a transparent z-index:1000 click shield after a double click. */
#spreadsheetCanvas > [id^="univer-doc-selection-container-"] {
  width: 1px !important;
  height: 1px !important;
  overflow: hidden !important;
  pointer-events: none !important;
}

/* The workbench's legacy global `header` rule forces every header into a
   `1fr 480px 1fr` grid. Univer's ribbon header is also a <header>, so that
   rule squeezed the real toolbar to 480px while leaving both sides empty.
   Keep the ribbon tabs at their natural width and let the toolbar consume all
   remaining room; Univer's own overflow controller can now respond correctly. */
#spreadsheetCanvas header[data-u-comp="headerbar"] {
  grid-template-columns: max-content minmax(0, 1fr) 0 !important;
  gap: 0 !important;
}
#spreadsheetCanvas header[data-u-comp="headerbar"] > :nth-child(2),
#spreadsheetCanvas header[data-u-comp="headerbar"] [role="toolbar"] {
  width: 100% !important;
  min-width: 0 !important;
  max-width: none !important;
}
#spreadsheetCanvas header[data-u-comp="headerbar"] [role="toolbar"] {
  justify-content: flex-start !important;
}

/* The formula bar is wrapped by a plain <header>, which was also inheriting
   the workbench's page-header grid and therefore stopped at 480px. Keep this
   override narrowly scoped to Univer's formula-bar host. */
#spreadsheetCanvas header:not([class]):not([data-u-comp]) {
  display: block !important;
  grid-template-columns: none !important;
  width: 100% !important;
  min-width: 0 !important;
}
#spreadsheetCanvas header:not([class]):not([data-u-comp]) > [data-u-comp="formula-bar"] {
  width: 100% !important;
  min-width: 0 !important;
}

/* Neutralize the workbench's global `aside` rule inside Univer. Without this,
   Univer's empty left/right slot containers each inherit a fixed 237px width. */
#spreadsheetCanvas [class*="univer-grid-cols-[auto_1fr_auto]"] > aside {
  width: auto;
  height: 100%;
  min-height: 0;
  flex: initial;
  padding: 0;
  border-radius: 0;
  overflow: visible;
}

.spreadsheet-asset-panel {
  position: absolute;
  z-index: 6;
  top: 10px;
  right: 10px;
  bottom: 10px;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  width: min(310px, calc(100% - 20px));
  min-height: 0;
  overflow: hidden;
  border: 1px solid var(--sheet-border);
  border-radius: 14px;
  box-shadow: 0 10px 30px rgba(74, 48, 31, .12);
  background: rgba(255, 253, 251, .98);
}

.spreadsheet-asset-panel.hidden { display: none; }
.spreadsheet-asset-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 13px 14px; border-bottom: 1px solid var(--sheet-border); }
.spreadsheet-asset-head b { font-size: 14px; }
.spreadsheet-asset-head button { border: 0; font-size: 18px; background: transparent; cursor: pointer; }
.spreadsheet-asset-list { min-height: 0; padding: 10px; overflow: auto; overscroll-behavior: contain; }
.spreadsheet-asset-empty { padding: 34px 12px; color: var(--sheet-muted); line-height: 1.7; text-align: center; }
.spreadsheet-asset-item { display: grid; grid-template-columns: 64px minmax(0, 1fr); gap: 10px; padding: 9px; border-radius: 10px; }
.spreadsheet-asset-item:hover { background: #fff4eb; }
.spreadsheet-asset-thumb { width: 64px; height: 50px; padding: 0; overflow: hidden; border: 0; border-radius: 8px; background: #fff; cursor: zoom-in; }
.spreadsheet-asset-item img { width: 64px; height: 50px; object-fit: contain; border: 1px solid #eee4dc; border-radius: 8px; background: #fff; }
.spreadsheet-asset-info { min-width: 0; }
.spreadsheet-asset-item b,
.spreadsheet-asset-item small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.spreadsheet-asset-item small { margin-top: 4px; color: var(--sheet-muted); }
.spreadsheet-asset-actions { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 10px; padding-left: 2px; }
.spreadsheet-asset-actions button,
.spreadsheet-asset-actions a { display: inline-block; padding: 0; border: 0; color: #c9632c; font: inherit; font-size: 12px; text-decoration: none; background: transparent; cursor: pointer; }
.spreadsheet-asset-actions .danger { color: #b44845; }

.spreadsheet-image-preview {
  position: fixed;
  z-index: 10080;
  inset: 0;
  display: grid;
  grid-template-columns: 56px minmax(0, 1fr) 56px;
  place-items: center;
  padding: 4vh 3vw;
  background: rgba(31, 24, 20, .72);
  backdrop-filter: blur(5px);
}
.spreadsheet-image-preview.hidden { display: none; }

.spreadsheet-preview-close {
  position: fixed;
  top: 24px;
  right: 28px;
  width: 42px;
  height: 42px;
  border: 1px solid rgba(255,255,255,.5);
  border-radius: 50%;
  color: #fff;
  font-size: 28px;
  line-height: 1;
  background: rgba(0,0,0,.24);
  cursor: pointer;
}

.spreadsheet-preview-nav {
  width: 44px;
  height: 66px;
  border: 1px solid rgba(255,255,255,.32);
  border-radius: 13px;
  color: #fff;
  font-size: 38px;
  line-height: 1;
  background: rgba(0,0,0,.2);
  cursor: pointer;
  transition: background .18s ease, transform .18s ease;
}
.spreadsheet-preview-nav:hover { background: rgba(238,136,70,.78); transform: translateY(-1px); }
.spreadsheet-preview-nav:disabled { opacity: .25; cursor: default; transform: none; }
.spreadsheet-image-preview figure { display: grid; gap: 10px; min-width: 0; max-width: calc(100vw - 132px); max-height: 96vh; margin: 0; color: #fff; text-align: center; }
.spreadsheet-preview-stage { display: grid; width: min(1380px, calc(100vw - 150px)); height: min(84vh, 1040px); place-items: center; overflow: hidden; border-radius: 14px; background: rgba(18,14,12,.28); touch-action: none; }
.spreadsheet-preview-stage.can-pan { cursor: grab; }
.spreadsheet-preview-stage.is-panning { cursor: grabbing; }
.spreadsheet-image-preview img { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 10px; box-shadow: 0 18px 60px rgba(0,0,0,.35); transform-origin: center; transition: transform .18s ease; user-select: none; -webkit-user-drag: none; }
.spreadsheet-preview-stage.is-panning img { transition: none; }
.spreadsheet-image-preview figcaption { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.spreadsheet-preview-controls { display: flex; align-items: center; justify-content: center; gap: 7px; }
.spreadsheet-preview-controls button,
.spreadsheet-preview-controls a { min-width: 36px; min-height: 32px; padding: 6px 10px; border: 1px solid rgba(255,255,255,.35); border-radius: 9px; color: #fff; font: inherit; text-decoration: none; background: rgba(0,0,0,.24); cursor: pointer; }
.spreadsheet-preview-controls span { min-width: 56px; color: rgba(255,255,255,.74); font-size: 12px; }

.spreadsheet-failure {
  position: absolute;
  z-index: 8;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 9px;
  padding: 30px;
  color: #6e5b4e;
  text-align: center;
  background: #fffaf6;
}
.spreadsheet-failure.hidden { display: none; }
.spreadsheet-failure b { color: #392d26; font-size: 18px; }
.spreadsheet-failure p { max-width: 560px; margin: 0; line-height: 1.7; }

.spreadsheet-permission-note { margin: 0 0 14px; color: #796a60; line-height: 1.65; }
.spreadsheet-range-summary { padding: 12px 14px; border: 1px solid #f0ddce; border-radius: 10px; background: #fff8f2; }
.spreadsheet-permission-form { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 13px; margin-top: 14px; }
.spreadsheet-permission-form label { display: grid; gap: 6px; color: #6f6258; font-size: 13px; }
.spreadsheet-permission-form select,
.spreadsheet-permission-form input { width: 100%; min-width: 0; min-height: 42px; padding: 8px 10px; border: 1px solid #e8d9cf; border-radius: 9px; background: #fff; box-sizing: border-box; }
.spreadsheet-permission-rules { display: grid; gap: 8px; max-height: 260px; margin-top: 14px; overflow: auto; }
.spreadsheet-permission-rule { display: grid; grid-template-columns: minmax(0, 1fr) auto auto; gap: 8px; align-items: center; padding: 10px 12px; border: 1px solid #eee2d9; border-radius: 10px; }
.spreadsheet-permission-rule small { color: #8a7b70; }
.spreadsheet-permission-rule button { border: 0; color: #b14f49; background: transparent; cursor: pointer; }

.spreadsheet-cell-type-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
.spreadsheet-cell-type-grid label { cursor: pointer; }
.spreadsheet-cell-type-grid input { position: absolute; opacity: 0; pointer-events: none; }
.spreadsheet-cell-type-grid span { display: grid; min-height: 46px; place-items: center; border: 1px solid #eadfd6; border-radius: 10px; background: #fff; }
.spreadsheet-cell-type-grid input:checked + span { border-color: #ef8b4c; color: #b95e2b; box-shadow: 0 0 0 2px rgba(239,139,76,.12); background: #fff6ef; }
.spreadsheet-option-label { display: none; gap: 7px; margin-top: 16px; color: #6f6258; font-size: 13px; }
.spreadsheet-option-label.active { display: grid; }
.spreadsheet-option-label textarea { width: 100%; resize: vertical; padding: 10px 12px; border: 1px solid #e8d9cf; border-radius: 10px; box-sizing: border-box; }

@media (max-width: 900px) {
  main:has(.document-editor-workspace.sheet) { height: calc(100vh - 20px); padding-bottom: 10px; }
  .spreadsheet-shell { height: 100%; min-height: 0; }
  .spreadsheet-toolbar { gap: 6px; }
  .spreadsheet-toolbar-state #sheetPresence { display: none; }
  .spreadsheet-toolbar-state #sheetFullscreen span { display: none; }
  .spreadsheet-overflow-menu { right: -70px; }
  .spreadsheet-permission-form { grid-template-columns: 1fr; }
  .spreadsheet-cell-type-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 640px) {
  .spreadsheet-toolbar button { padding-inline: 9px; }
  .spreadsheet-toolbar-state #sheetSaveStatus { display: none; }
  .spreadsheet-image-preview { grid-template-columns: 42px minmax(0, 1fr) 42px; padding-inline: 4px; }
  .spreadsheet-preview-stage { width: calc(100vw - 94px); }
}
