:root {
  color-scheme: light;
  --bg: #f4f6f4;
  --panel: #ffffff;
  --ink: #1d2528;
  --muted: #657176;
  --line: #d9dfdc;
  --accent: #2d6f73;
  --accent-dark: #1f5659;
  --danger: #b0413e;
  --grid: #da6f63;
  --grid-soft: #f8e1dd;
  --cell-size: 34px;
  --font-size: 20px;
  --font-family: KaiTi, STKaiti, serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family: "Microsoft YaHei", "PingFang SC", sans-serif;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  min-height: 36px;
  padding: 0 12px;
  border-radius: 6px;
  cursor: pointer;
}

button:hover {
  border-color: var(--accent);
  color: var(--accent-dark);
}

button:disabled {
  color: #a3abad;
  cursor: not-allowed;
}

.topbar {
  min-height: 72px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 14px 22px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}

.topbar h1 {
  margin: 0;
  font-size: 20px;
  line-height: 1.2;
}

.stat-badge {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  min-height: 40px;
  padding: 0 18px;
  background: var(--accent);
  border-radius: 8px;
  color: #fff;
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
}

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

.topbar-actions button:nth-last-child(-n + 3) {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.topbar-actions button:nth-last-child(-n + 3):hover {
  background: var(--accent-dark);
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(320px, 390px) 1fr;
  min-height: calc(100vh - 72px);
}

.panel {
  border-right: 1px solid var(--line);
  background: var(--panel);
  overflow: auto;
}

.section {
  padding: 18px;
  border-bottom: 1px solid var(--line);
}

.section h2,
.section-title h2 {
  margin: 0 0 12px;
  font-size: 15px;
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.section-title h2 {
  margin: 0;
}

.file-button {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 32px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
}

.file-button:hover {
  border-color: var(--accent);
  color: var(--accent-dark);
}

.file-button input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

textarea {
  width: 100%;
  min-height: 310px;
  resize: vertical;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 12px;
  color: var(--ink);
  line-height: 1.65;
}

textarea:focus,
input:focus,
select:focus {
  outline: 2px solid rgba(45, 111, 115, 0.2);
  border-color: var(--accent);
}

.control-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.control-grid label,
.toggle-list label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
}

.control-grid input,
.control-grid select,
.section-title input {
  width: 100%;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 10px;
  background: #fff;
  color: var(--ink);
}

.toggle-list {
  display: grid;
  gap: 10px;
  margin-top: 14px;
}

.toggle-list label {
  grid-template-columns: 18px 1fr;
  align-items: center;
  gap: 8px;
}

.punctuation-settings {
  display: grid;
  gap: 10px;
  margin-top: 14px;
}

.punctuation-rule {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 112px;
  align-items: center;
  gap: 12px;
  min-height: 58px;
}

.punctuation-rule + .punctuation-rule {
  padding-top: 10px;
  border-top: 1px solid var(--line);
}

.punctuation-toggle {
  display: grid;
  grid-template-columns: 18px minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 13px;
}

.punctuation-example {
  margin: 0;
  width: 112px;
}

.punctuation-example figcaption {
  margin: 0 0 4px;
  color: var(--muted);
  font-size: 12px;
  text-align: left;
}

.mini-grid {
  display: grid;
  border-top: 1px solid var(--grid);
  border-left: 1px solid var(--grid);
  background: #fff;
}

.mini-grid-start {
  grid-template-columns: repeat(3, 24px);
}

.mini-grid-end {
  grid-template-columns: repeat(4, 24px);
}

.mini-grid span {
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-right: 1px solid var(--grid);
  border-bottom: 1px solid var(--grid);
  color: var(--ink);
  font-family: var(--font-family);
  font-size: 13px;
  line-height: 1;
}

.mini-grid span:nth-child(3) {
  background: var(--grid-soft);
}

.version-list {
  display: grid;
  gap: 8px;
  max-height: 210px;
  overflow: auto;
}

.version-item {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 8px;
  padding: 9px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fbfcfb;
}

.version-item strong {
  display: block;
  font-size: 13px;
}

.version-item span {
  display: block;
  color: var(--muted);
  font-size: 12px;
}

.version-item button {
  min-height: 30px;
  padding: 0 8px;
  font-size: 12px;
}

.version-item .danger {
  color: var(--danger);
}

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

.preview-toolbar {
  position: sticky;
  top: 0;
  z-index: 3;
  min-height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 18px;
  background: rgba(244, 246, 244, 0.96);
  border-bottom: 1px solid var(--line);
}

.preview-status {
  display: grid;
  gap: 3px;
  min-width: 220px;
}

.position-info {
  color: var(--muted);
  font-size: 13px;
}

.selected-info {
  color: var(--accent-dark);
  font-size: 13px;
}

.page-controls {
  display: flex;
  align-items: center;
  gap: 10px;
}

.page-controls button {
  width: 36px;
  padding: 0;
  font-size: 22px;
}

.page-controls span {
  min-width: 92px;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
}

.pages {
  overflow: auto;
  padding: 24px;
}

.paper {
  width: fit-content;
  margin: 0 auto 28px;
  padding: 28px;
  background: #fffefd;
  border: 1px solid #e7d6d1;
  box-shadow: 0 10px 28px rgba(35, 48, 50, 0.12);
}

.paper.is-hidden {
  display: none;
}

.paper-title {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 10px;
  color: var(--muted);
  font-size: 12px;
}

.grid {
  display: grid;
  grid-template-columns: repeat(var(--columns), var(--cell-size));
  grid-auto-rows: var(--cell-size);
  border-top: 1px solid var(--grid);
  border-left: 1px solid var(--grid);
  background: #fff;
}

.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  border-right: 1px solid var(--grid);
  border-bottom: 1px solid var(--grid);
  color: #1c1f20;
  font-family: var(--font-family);
  font-size: var(--font-size);
  line-height: 1;
  overflow: hidden;
}

.cell.editable {
  cursor: text;
}

.cell.editable:hover,
.cell.editable:focus {
  background: #fff6d9;
  outline: 2px solid rgba(45, 111, 115, 0.28);
  outline-offset: -2px;
}

.cell.selected-range {
  background: #e7f1ee;
}

.cell.selected-range.squeezed {
  background: #f3dfc5;
}

.cell.squeezed {
  background: var(--grid-soft);
}

.cell.word {
  background-image: repeating-linear-gradient(
    to right,
    transparent 0,
    transparent calc(var(--cell-size) - 1px),
    var(--grid) calc(var(--cell-size) - 1px),
    var(--grid) var(--cell-size)
  );
}

.cell-text {
  display: inline-block;
  max-width: 100%;
  white-space: nowrap;
}

.cell.squeezed .cell-text,
.cell.word .cell-text,
.cell.compact .cell-text {
  transform: scale(var(--fit-scale, 1));
  transform-origin: center;
  white-space: nowrap;
}

.cell.space,
.cell.indent {
  color: transparent;
}

.cell.editing {
  background: #fff;
  padding: 1px;
}

.cell-editor {
  width: 100%;
  height: 100%;
  border: 0;
  border-radius: 0;
  padding: 0 2px;
  background: #fff;
  color: var(--ink);
  text-align: center;
  font-family: var(--font-family);
  font-size: min(var(--font-size), 16px);
  line-height: 1;
}

.cell-editor:focus {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.contextual-edit-btn {
  position: fixed;
  z-index: 18;
  min-height: 34px;
  padding: 0 12px;
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  box-shadow: 0 10px 24px rgba(35, 48, 50, 0.2);
}

.contextual-edit-btn:hover {
  background: var(--accent-dark);
  border-color: var(--accent-dark);
  color: #fff;
}

.contextual-edit-btn[hidden] {
  display: none;
}

.selection-dialog {
  position: fixed;
  top: auto;
  right: 24px;
  bottom: 24px;
  left: auto;
  width: min(560px, calc(100vw - 48px));
  max-height: calc(100vh - 96px);
  margin: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0;
  color: var(--ink);
  box-shadow: 0 18px 48px rgba(35, 48, 50, 0.22);
}

.selection-dialog::backdrop {
  background: transparent;
}

.selection-form {
  display: grid;
  gap: 14px;
  padding: 16px;
  background: #fff;
  max-height: inherit;
  overflow: auto;
}

.dialog-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.dialog-title h2 {
  margin: 0;
  font-size: 16px;
}

.dialog-title button {
  width: 34px;
  padding: 0;
  font-size: 20px;
}

.selection-field {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
}

.field-label {
  color: var(--muted);
}

.selection-original {
  min-height: 72px;
  max-height: 160px;
  overflow: auto;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #f7f9f8;
  color: var(--ink);
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-word;
}

#selectionEditor {
  min-height: 160px;
  max-height: min(40vh, 280px);
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
}

.dialog-actions button {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.dialog-actions button:hover {
  background: var(--accent-dark);
}

@media (max-width: 900px) {
  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

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

  .preview-toolbar {
    align-items: flex-start;
    flex-wrap: wrap;
  }

  .panel {
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .selection-dialog {
    right: 16px;
    bottom: 16px;
    width: calc(100vw - 32px);
    max-height: calc(100vh - 48px);
  }
}

@media print {
  body {
    background: #fff;
  }

  .topbar,
  .panel,
  .contextual-edit-btn,
  .selection-dialog,
  .preview-toolbar {
    display: none !important;
  }

  .app-shell,
  .preview-area {
    display: block;
    min-height: auto;
  }

  .pages {
    overflow: visible;
    padding: 0;
  }

  .paper {
    box-shadow: none;
    border: 0;
    margin: 0 auto;
    page-break-after: always;
  }

  .paper.is-hidden {
    display: block;
  }
}
