:root {
  --bg: #eef1f4;
  --panel: #ffffff;
  --panel-soft: #f7f8fa;
  --grid: #d8dde3;
  --grid-dark: #b8c0c9;
  --text: #1d252f;
  --muted: #66717e;
  --green: #1f9d55;
  --green-soft: #e5f5eb;
  --blue: #2563eb;
  --danger: #d93025;
  --shadow: 0 10px 26px rgba(28, 35, 45, 0.12);
  --row-head: 42px;
  --col-head: 32px;
  --sidebar-width: 188px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 980px;
  height: 100vh;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: "Microsoft YaHei UI", "Microsoft YaHei", sans-serif;
  font-size: 13px;
}

button,
input {
  font: inherit;
}

.workspace {
  display: grid;
  grid-template-rows: 38px minmax(44px, auto) 1fr 34px;
  height: 100vh;
}

.appbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  border-bottom: 1px solid #dde1e6;
  background: #fbfcfd;
}

.sheet-title {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.back-button {
  width: 28px;
  height: 28px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--text);
  font-size: 28px;
  line-height: 1;
  cursor: pointer;
}

.sheet-title p,
.sheet-title h1 {
  margin: 0;
  line-height: 1.15;
}

.sheet-title p {
  color: var(--muted);
  font-size: 11px;
}

.sheet-title h1 {
  min-width: 220px;
  max-width: 680px;
  overflow: hidden;
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
  text-overflow: ellipsis;
  outline: none;
}

.presence {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
}

.dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--danger);
}

.dot.online {
  background: var(--green);
  box-shadow: 0 0 0 4px rgba(31, 157, 85, 0.14);
}

.ribbon {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 44px;
  padding: 6px 16px;
  border-bottom: 1px solid #d7dce2;
  background: var(--panel);
  box-shadow: 0 1px 5px rgba(24, 32, 42, 0.05);
}

.tab {
  display: inline-grid;
  height: 44px;
  place-items: center;
  padding: 0 10px;
  color: #4e5966;
  font-weight: 700;
}

.tab.active {
  position: relative;
  color: var(--green);
}

.tab.active::after {
  position: absolute;
  right: 8px;
  bottom: 0;
  left: 8px;
  height: 3px;
  border-radius: 3px 3px 0 0;
  background: var(--green);
  content: "";
}

.ribbon .tab:not(.active),
#deleteRowBtn,
#deleteColBtn {
  display: none;
}

.tools {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.tools button {
  height: 28px;
  border: 1px solid transparent;
  border-radius: 4px;
  background: transparent;
  color: #44505c;
  cursor: pointer;
}

.tools button:hover {
  border-color: var(--grid-dark);
  background: var(--panel-soft);
}

.tool-separator {
  width: 1px;
  height: 22px;
  background: var(--grid);
}

.tool-field {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 28px;
  color: #44505c;
  font-size: 12px;
  font-weight: 700;
}

.tool-field input[type="number"] {
  width: 54px;
  height: 26px;
  border: 1px solid var(--grid-dark);
  border-radius: 4px;
  padding: 0 5px;
}

.bulk-count {
  width: 48px;
  height: 28px;
  border: 1px solid var(--grid-dark);
  border-radius: 4px;
  padding: 0 5px;
}

.tool-field input[type="color"] {
  width: 30px;
  height: 26px;
  border: 1px solid var(--grid-dark);
  border-radius: 4px;
  background: #fff;
  padding: 2px;
}

.icon-tool {
  min-width: 28px;
  padding: 0 7px;
}

.workbench {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  min-height: 0;
}

body.categories-bottom .workbench {
  grid-template-columns: 1fr;
}

body.categories-bottom .category-sidebar {
  display: none;
}

.category-sidebar {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  border-right: 1px solid var(--grid);
  background: #fbfcfd;
}

.sidebar-resize {
  position: absolute;
  top: 0;
  right: -4px;
  bottom: 0;
  z-index: 10;
  width: 8px;
  cursor: col-resize;
}

.category-sidebar header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 54px;
  padding: 8px 10px 8px 14px;
  border-bottom: 1px solid var(--grid);
  color: #44505c;
  font-weight: 700;
}

.category-heading {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.category-heading span {
  color: #2d3845;
  font-size: 14px;
  line-height: 1.15;
}

.category-heading small {
  color: var(--muted);
  font-size: 11px;
  font-weight: 400;
}

.category-header-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 0 0 auto;
}

.dock-toggle {
  height: 26px;
  border: 1px solid #cfd7df;
  border-radius: 999px;
  background: #fff;
  color: #51606f;
  cursor: pointer;
  padding: 0 10px;
  font-size: 12px;
  white-space: nowrap;
}

.dock-toggle:hover {
  border-color: #9acdac;
  background: var(--green-soft);
  color: var(--green);
}

.add-category {
  display: grid;
  width: 26px;
  height: 26px;
  place-items: center;
  border: 1px solid transparent;
  border-radius: 50%;
  background: var(--green);
  color: #fff;
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
}

.add-category:hover {
  border-color: var(--green);
  background: #168446;
}

.category-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 8px;
  overflow: auto;
}

.category-wrap,
.bottom-category-wrap {
  display: flex;
  align-items: center;
  gap: 4px;
  min-width: 0;
}

.category-wrap {
  width: 100%;
}

.category-item {
  display: flex;
  align-items: center;
  flex: 1;
  min-width: 0;
  min-height: 34px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: #4f5b67;
  cursor: pointer;
  padding: 0 10px;
  text-align: left;
}

.category-item:hover {
  background: var(--panel-soft);
}

.category-item.active {
  border-color: #bbe0c7;
  background: var(--green-soft);
  color: var(--green);
  font-weight: 700;
}

.category-delete {
  display: grid;
  width: 24px;
  height: 24px;
  flex: 0 0 auto;
  place-items: center;
  border: 1px solid transparent;
  border-radius: 50%;
  background: transparent;
  color: #8a96a3;
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
}

.category-delete:hover {
  border-color: #f1c3bd;
  background: #fff1ef;
  color: var(--danger);
}

.sheet-shell {
  position: relative;
  overflow: auto;
  background:
    linear-gradient(45deg, rgba(0, 0, 0, 0.018) 25%, transparent 25%),
    linear-gradient(-45deg, rgba(0, 0, 0, 0.018) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, rgba(0, 0, 0, 0.018) 75%),
    linear-gradient(-45deg, transparent 75%, rgba(0, 0, 0, 0.018) 75%),
    #f8f9fb;
  background-position: 0 0, 0 8px, 8px -8px, -8px 0;
  background-size: 16px 16px;
}

.sheet-grid {
  position: relative;
  display: grid;
  min-width: max-content;
  min-height: max-content;
}

.corner,
.col-header,
.row-header,
.cell {
  border-right: 1px solid var(--grid);
  border-bottom: 1px solid var(--grid);
}

.corner {
  position: sticky;
  top: 0;
  left: 0;
  z-index: 6;
  background: #edf1f4;
}

.col-header {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #edf1f4;
  color: #5a6674;
  font-size: 12px;
  user-select: none;
}

.row-header {
  position: sticky;
  left: 0;
  z-index: 4;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #edf1f4;
  color: #5a6674;
  user-select: none;
}

.cell {
  position: relative;
  display: flex;
  align-items: center;
  min-width: 0;
  min-height: 0;
  padding: 4px 7px;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.82);
  outline: none;
  white-space: pre-wrap;
  word-break: break-word;
}

.cell:hover {
  background: #fff;
}

.cell.selected {
  z-index: 3;
  box-shadow: inset 0 0 0 2px var(--green);
}

.cell.range {
  background: rgba(31, 157, 85, 0.1);
}

.cell.remote {
  box-shadow: inset 0 0 0 2px var(--blue);
}

.cell.remote::after {
  position: absolute;
  top: 2px;
  right: 3px;
  max-width: 88px;
  overflow: hidden;
  border-radius: 3px;
  background: var(--blue);
  color: #fff;
  content: attr(data-remote-user);
  font-size: 10px;
  line-height: 16px;
  padding: 0 4px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.cell[data-editor]::before {
  position: absolute;
  right: 4px;
  bottom: 2px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #d8a027;
  content: "";
}

.cell.editing {
  z-index: 7;
  overflow: visible;
  background: #fff;
  box-shadow: inset 0 0 0 2px var(--green), var(--shadow);
}

.cell.hidden-by-merge {
  display: none;
}

.cell .link {
  color: #0b55e7;
  font-weight: 700;
  text-decoration: underline;
}

.cell-image {
  display: block;
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  object-position: center;
  pointer-events: none;
}

.image-preview {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: grid;
  place-items: center;
  background: rgba(15, 23, 42, 0.78);
  padding: 28px;
}

.image-preview img {
  max-width: min(96vw, 1400px);
  max-height: 92vh;
  border-radius: 6px;
  background: #fff;
  box-shadow: 0 18px 60px rgba(0, 0, 0, 0.32);
  object-fit: contain;
}

.image-preview-close {
  position: fixed;
  top: 18px;
  right: 22px;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.92);
  color: #111827;
  cursor: pointer;
  font-size: 24px;
  line-height: 1;
}

.resize-handle {
  position: absolute;
  z-index: 8;
  background: transparent;
}

.col-header .resize-handle {
  top: 0;
  right: -6px;
  width: 12px;
  height: 100%;
  cursor: col-resize;
}

.row-header .resize-handle {
  right: 0;
  bottom: -6px;
  left: 0;
  height: 12px;
  cursor: row-resize;
}

.statusbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 10px;
  border-top: 1px solid var(--grid);
  background: #fbfcfd;
  color: var(--muted);
}

.statusbar p {
  margin: 0;
  font-size: 12px;
}

.bottom-category-list {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  overflow: auto hidden;
  flex: 1;
}

.bottom-dock-toggle {
  height: 26px;
  border: 1px solid #cfd7df;
  border-radius: 999px;
  background: #fff;
  color: #51606f;
  cursor: pointer;
  padding: 0 10px;
  font-size: 12px;
  white-space: nowrap;
}

.bottom-dock-toggle:hover {
  border-color: #9acdac;
  background: var(--green-soft);
  color: var(--green);
}

.bottom-category-item {
  height: 26px;
  max-width: 180px;
  border: 1px solid transparent;
  border-radius: 5px;
  background: transparent;
  color: #53606d;
  cursor: pointer;
  padding: 0 10px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.bottom-category-item.active {
  border-color: #bbe0c7;
  background: var(--green-soft);
  color: var(--green);
  font-weight: 700;
}

.category-dialog {
  width: 360px;
  border: 1px solid var(--grid-dark);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 0;
}

.category-dialog::backdrop {
  background: rgba(22, 29, 38, 0.22);
}

.category-dialog form {
  display: grid;
  gap: 14px;
  padding: 18px;
}

.category-dialog h2 {
  margin: 0;
  font-size: 18px;
}

.category-dialog label {
  display: grid;
  gap: 6px;
  color: #46515e;
  font-weight: 700;
}

.category-dialog fieldset {
  display: grid;
  gap: 8px;
  border: 1px solid var(--grid);
  border-radius: 6px;
  margin: 0;
  padding: 10px;
}

.category-dialog legend {
  color: #46515e;
  font-weight: 700;
  padding: 0 4px;
}

.category-dialog .radio-line {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 400;
}

.category-dialog .radio-line input {
  width: 16px;
  height: 16px;
}

.category-dialog input {
  height: 34px;
  border: 1px solid var(--grid-dark);
  border-radius: 5px;
  outline: none;
  padding: 0 10px;
}

.category-dialog input:focus {
  border-color: var(--green);
  box-shadow: 0 0 0 3px rgba(31, 157, 85, 0.12);
}

.dialog-error {
  min-height: 18px;
  margin: 0;
  color: var(--danger);
}

.category-dialog menu {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin: 0;
  padding: 0;
}

.category-dialog button {
  height: 32px;
  border: 1px solid var(--grid-dark);
  border-radius: 5px;
  background: var(--panel);
  cursor: pointer;
}

.category-dialog button.primary {
  border-color: var(--green);
  background: var(--green);
  color: #fff;
}
