:root {
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: #26332d;
  background: #f4f6f3;
  font-synthesis: none;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  background: #f4f6f3;
}

button, .button, textarea { font: inherit; }

button, .button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 38px;
  padding: 0 13px;
  border: 1px solid #d5dcd7;
  border-radius: 9px;
  color: #34443c;
  background: #fff;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
  transition: border-color .15s, background .15s, transform .05s;
}

button:hover, .button:hover { background: #f8faf8; border-color: #bfc9c2; }
button:active, .button:active { transform: translateY(1px); }
button:focus-visible, .button:focus-visible, textarea:focus-visible { outline: 3px solid #b9d8c7; outline-offset: 2px; }
button:disabled { opacity: .45; cursor: default; transform: none; }

.app {
  width: min(1500px, 100%);
  min-height: 100vh;
  margin: 0 auto;
  padding: 14px clamp(18px, 3vw, 46px) 10px;
  display: grid;
  grid-template-rows: auto auto auto;
  gap: 5px;
}

.toolbar { width: 100%; min-width: 0; display: flex; align-items: center; flex-wrap: nowrap; gap: 5px; }
.brand { margin-right: 5px; font-size: 19px; font-weight: 720; letter-spacing: -.02em; }
.toolbar > button,
.toolbar > .button {
  min-height: 32px;
  padding: 0 10px;
  font-size: 14px;
}
.toolbar .icon-action {
  flex: 0 0 30px;
  width: 30px;
  min-width: 30px;
  padding: 0;
}
.icon-action svg {
  width: 17px;
  height: 17px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.symbol-action { font: 18px/1 Georgia, "Times New Roman", serif !important; }

.mode { gap: 8px; background: #fff; }
.mode.is-on { border-color: #b9d9c6; background: #edf8f1; color: #23623f; }
.mode-dot { width: 8px; height: 8px; border-radius: 50%; background: #98a39d; }
.mode.is-on .mode-dot { background: #37a463; box-shadow: 0 0 0 3px #d3eedc; }
kbd { padding: 2px 5px; border: 1px solid #cfd7d1; border-radius: 4px; background: rgba(255,255,255,.7); color: #68756e; font: 11px/1.2 inherit; }
.danger { color: #9a4b4b; }
.font-controls {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  align-self: flex-end;
  gap: 2px;
}
.font-controls .compact-control {
  width: 22px;
  min-width: 22px;
  min-height: 22px;
  padding: 0;
  border-radius: 6px;
  font-size: 14px;
  line-height: 1;
}

.document-tabs {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  align-items: flex-end;
  gap: 4px;
  overflow: hidden;
}
.tabs-list {
  flex: 0 1 auto;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 4px;
  overflow-x: auto;
  scrollbar-width: thin;
}
.document-tab {
  flex: 0 1 220px;
  min-width: 110px;
  max-width: 240px;
  min-height: 30px;
  padding: 0 7px 0 12px;
  display: flex;
  align-items: center;
  gap: 7px;
  border: 1px solid #dbe1dc;
  border-radius: 9px;
  border-color: #dbe1dc;
  color: #66736b;
  background: #eef1ef;
}
.document-tab[aria-selected="true"] {
  border-color: #b9d9c6;
  color: #285e3f;
  background: #fff;
  box-shadow: 0 2px 8px rgba(41, 61, 49, .06);
}
.tab-title {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tab-rename-input {
  width: 100%;
  min-width: 0;
  padding: 2px 4px;
  border: 1px solid #9fc8ae;
  border-radius: 4px;
  color: #285e3f;
  background: #fff;
  font: inherit;
  outline: 0;
}
.tab-select {
  min-width: 0;
  min-height: 28px;
  flex: 1;
  justify-content: flex-start;
  padding: 0;
  border: 0;
  color: inherit;
  background: transparent;
}
.tab-select:hover { border: 0; background: transparent; }
.tab-close {
  flex: 0 0 20px;
  width: 20px;
  height: 20px;
  min-height: 20px;
  padding: 0;
  border: 0;
  border-radius: 5px;
  color: #7b867f;
  background: transparent;
  font-size: 17px;
}
.tab-close:hover { color: #9a4b4b; background: #f4e9e9; }
.add-tab {
  flex: 0 0 28px;
  width: 28px;
  min-height: 28px;
  padding: 0;
  font-size: 21px;
}

.editor-label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0,0,0,0); }
.editor-shell {
  position: relative;
  width: 100%;
  border: 1px solid #dce2dd;
  border-radius: 14px;
  background: #fff;
  box-shadow: 0 8px 28px rgba(41, 61, 49, .045);
  overflow: hidden;
}
#editor, #invisiblesLayer {
  padding: 14px 24px;
  font-family: Verdana, Tahoma, sans-serif;
  font-size: var(--editor-font-size, 16px);
  line-height: 1.38;
  tab-size: 4;
}
#editor {
  position: relative;
  z-index: 1;
  display: block;
  width: 100%;
  height: clamp(420px, calc(100vh - 78px), 900px);
  min-height: 240px;
  resize: vertical;
  border: 0;
  border-radius: 13px;
  color: #202b26;
  background: transparent;
  caret-color: #27754a;
}
#editor::placeholder { color: #a4afa8; }
#invisiblesLayer {
  position: absolute;
  z-index: 0;
  inset: 0;
  display: none;
  margin: 0;
  overflow: auto;
  overflow-anchor: none;
  border-radius: 13px;
  color: transparent;
  background: #fff;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  pointer-events: none;
}
.invisible-space,
.invisible-newline,
.invisible-tab { position: relative; }
.invisible-space::after {
  content: "·";
  position: absolute;
  inset: 0;
  color: #637c6d;
  text-align: center;
  pointer-events: none;
}
.invisible-newline::after {
  content: "¶";
  position: absolute;
  left: 0;
  color: #637c6d;
  pointer-events: none;
}
.invisible-tab::after {
  content: "→";
  position: absolute;
  left: 0;
  color: #637c6d;
  pointer-events: none;
}
.editor-shell.show-invisibles #invisiblesLayer { display: block; }
.editor-shell.show-invisibles #editor::selection { color: #202b26; background: rgba(91, 145, 112, .28); }
#invisiblesButton[aria-pressed="true"] { border-color: #b9d9c6; background: #edf8f1; color: #23623f; }

.statusbar { display: flex; justify-content: space-between; gap: 20px; padding: 0 3px; color: #758078; font-size: 13px; }
.editor-meta { display: flex; justify-content: flex-end; gap: 7px 14px; text-align: right; }
#textStats { white-space: nowrap; }
.storage-warning { color: #9a642f; font-weight: 600; }
#saveStatus.is-saving { color: #49705a; }
#saveStatus.is-error { color: #a24949; }

.dialog {
  width: min(1000px, calc(100vw - 32px));
  max-height: min(760px, calc(100vh - 32px));
  padding: 0;
  border: 1px solid #d7ded9;
  border-radius: 16px;
  color: #26332d;
  background: #fff;
  box-shadow: 0 24px 80px rgba(25, 39, 31, .2);
}
.dialog::backdrop { background: rgba(29, 40, 34, .28); backdrop-filter: blur(2px); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; padding: 20px 22px 16px; border-bottom: 1px solid #e3e7e4; }
.dialog-head h2 { margin: 0 0 3px; font-size: 20px; }
.dialog-head p { margin: 0; color: #7b867f; font-size: 13px; }
.icon-button { min-width: 38px; padding: 0; border: 0; font-size: 25px; color: #66736b; }

.history-layout { display: grid; grid-template-columns: 280px minmax(0, 1fr); min-height: 500px; }
.history-list { overflow: auto; border-right: 1px solid #e3e7e4; background: #f8faf8; }
.history-item { display: block; width: 100%; min-height: 0; padding: 13px 16px; border: 0; border-bottom: 1px solid #e7ebe8; border-radius: 0; text-align: left; white-space: normal; }
.history-item strong, .history-item span { display: block; }
.history-item strong { margin-bottom: 4px; font-size: 13px; font-weight: 650; }
.history-item span { overflow: hidden; color: #7b867f; font-size: 12px; text-overflow: ellipsis; white-space: nowrap; }
.history-item.is-selected { background: #eaf5ee; color: #245e3e; }
.empty-history { padding: 28px 18px; color: #7c8780; text-align: center; }
.preview-panel { min-width: 0; padding: 16px; display: grid; grid-template-rows: auto 1fr; gap: 12px; }
.preview-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; color: #637168; font-size: 13px; }
#historyPreview { width: 100%; min-height: 380px; padding: 18px; resize: none; border: 1px solid #dce2dd; border-radius: 10px; color: #34413a; background: #fafbfa; font: 16px/1.55 Georgia, "Times New Roman", serif; }

.help-dialog { width: min(860px, calc(100vw - 32px)); }
.help-content { padding: 22px; color: #526158; line-height: 1.55; }
.rules { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 7px; }
.rules code { min-width: 0; padding: 8px 10px; display: flex; justify-content: space-between; gap: 12px; border-radius: 7px; color: #285e3f; background: #edf6f0; font-size: 13px; }
.rules code b { flex: 0 0 auto; }
.rules code span { min-width: 0; color: #526a5b; text-align: right; overflow-wrap: anywhere; }
.help-content p { margin: 18px 0 0; }

@media (max-width: 820px) {
  .app { padding: 12px; }
  .brand { width: 100%; }
  .toolbar { flex-wrap: wrap; }
  .document-tabs { order: 3; flex-basis: 100%; }
  .history-layout { grid-template-columns: 1fr; grid-template-rows: 180px 1fr; }
  .history-list { border-right: 0; border-bottom: 1px solid #e3e7e4; }
  .statusbar { flex-direction: column; gap: 3px; }
  .editor-meta { justify-content: space-between; flex-wrap: wrap; text-align: left; }
}

@media (max-width: 520px) {
  .toolbar > button:not(.icon-action), .toolbar > .button:not(.icon-action) { flex: 1; }
  .mode { flex-basis: auto; }
  .document-tab { flex-basis: 170px; }
  #editor, #invisiblesLayer { padding: 12px 16px; font-size: 16px; }
  .rules { grid-template-columns: 1fr; }
}
