:root {
  --bg: #16171d;
  --bg-panel: #1e2028;
  --fg: #d5d8e2;
  --fg-dim: #676c80;
  --accent: #7aa2f7;
  --border: #2a2d3a;
  --font-mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --line-height: 21px; /* overwritten from Rust at startup (view::LINE_HEIGHT) */
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/* Keep the hidden attribute authoritative even for elements that set their
   own display (e.g. #placeholder's grid). */
[hidden] {
  display: none !important;
}

/* --- scrollbars: match the theme ----------------------------------------- */

/* Standard properties (Chrome 121+, Firefox). When these are set, Chrome
   ignores the -webkit rules below — which then only serve Safari. */
* {
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

::-webkit-scrollbar {
  width: 12px;
  height: 12px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--border);
  border: 3px solid transparent;
  border-radius: 8px;
  background-clip: content-box;
}

::-webkit-scrollbar-thumb:hover {
  background-color: var(--fg-dim); /* -color keeps the content-box clip */
}

::-webkit-scrollbar-corner {
  background: transparent;
}

html,
body {
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--fg);
  font: 13px/1.4 system-ui, -apple-system, sans-serif;
  overflow: hidden;
}

#app {
  height: 100%;
  display: flex;
  flex-direction: column;
}

/* --- toolbar ----------------------------------------------------------- */

/* Single top row: tabs on the left, action buttons on the right. */
#toolbar {
  display: flex;
  align-items: stretch;
  gap: 6px;
  padding: 0 8px 0 0;
  min-height: 34px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
}

.toolbar-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 6px;
  padding-right: 2px;
}

/* --- dropdown menu -------------------------------------------------------- */

#menu-panel {
  position: fixed;
  top: 38px;
  right: 8px;
  min-width: 180px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.4);
  z-index: 11;
  overflow: hidden;
  padding: 4px 0;
}

.menu-item {
  padding: 7px 14px;
  font-size: 12px;
  color: var(--fg);
  cursor: pointer;
  user-select: none;
}

.menu-item:hover {
  background: var(--bg);
  color: var(--accent);
}

button {
  background: transparent;
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 3px 10px;
  font: inherit;
  cursor: pointer;
}

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

button.active {
  border-color: var(--accent);
  color: var(--accent);
}

/* --- tabs (the persistent file pool), inside the toolbar ----------------- */

#tab-list {
  display: flex;
  align-items: stretch;
  overflow-x: auto;
  scrollbar-width: none;
  min-width: 0;
}

.tab {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 8px 5px 12px;
  border-right: 1px solid var(--border);
  color: var(--fg-dim);
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
  user-select: none;
}

.tab:hover {
  color: var(--fg);
}

.tab.active {
  color: var(--fg);
  background: var(--bg);
  box-shadow: inset 0 2px 0 var(--accent);
}

.tab.preview .tab-name {
  font-style: italic;
  color: var(--accent);
}

.tab-dirty {
  color: var(--accent);
  font-size: 9px;
}

.tab-rename {
  font-family: inherit;
  font-size: 12px;
  background: var(--bg);
  color: var(--fg);
  border: 1px solid var(--accent);
  border-radius: 4px;
  padding: 1px 4px;
  margin: 4px 0;
  min-width: 6ch;
  outline: none;
}

.tab-close {
  padding: 0 4px;
  border-radius: 4px;
  color: var(--fg-dim);
}

.tab-close:hover {
  color: var(--fg);
  background: var(--border);
}

#tab-new {
  border: none;
  border-radius: 0;
  padding: 0 10px;
  font-size: 14px;
  color: var(--fg-dim);
}

#tab-new:hover {
  color: var(--accent);
}

/* --- closed-files panel --------------------------------------------------- */

#files-panel {
  position: fixed;
  top: 38px;
  right: 8px;
  min-width: 240px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.4);
  z-index: 10;
  overflow: hidden;
}

#files-panel header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  color: var(--fg-dim);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

#files-close {
  cursor: pointer;
  font-size: 13px;
}

#files-list {
  list-style: none;
  max-height: 40vh;
  overflow: auto;
}

.file-row {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 6px 10px;
  font-size: 12px;
}

.file-row:hover {
  background: var(--bg);
}

.file-reopen {
  cursor: pointer;
  color: var(--fg);
  flex: 1;
}

.file-reopen:hover {
  color: var(--accent);
}

.file-delete {
  cursor: pointer;
  color: var(--fg-dim);
  font-size: 11px;
}

.file-delete:hover {
  color: #f7768e;
}

/* --- editor ------------------------------------------------------------ */

#editor {
  flex: 1;
  position: relative;
  min-height: 0;
}

#scroller {
  position: absolute;
  inset: 0;
  overflow: auto;
}

#content {
  position: relative; /* height set from Rust: total lines × line height */
}

#scroller {
  cursor: text;
}

/* In normal flow (not absolute) so its max-content width reliably drives
   the scroller's scrollWidth — scrollLeft would otherwise clamp against a
   stale width right after long-line renders. translateY still anchors the
   virtualized window. */
#lines {
  min-width: 100%;
  width: max-content;
  font-family: var(--font-mono);
  font-size: 13px;
  tab-size: 4;
  user-select: none;
  /* Scroll room past the longest line so its tail (and the caret) isn't
     pressed against the right edge / scrollbar. */
  padding-right: 4ch;
  box-sizing: border-box;
}

.line {
  position: relative;
  display: flex;
  height: var(--line-height);
  line-height: var(--line-height);
}

.ln {
  position: sticky;
  left: 0;
  z-index: 2;
  flex: none;
  width: calc(var(--gutter-ch, 2) * 1ch + 28px);
  padding-right: 16px;
  text-align: right;
  color: var(--fg-dim);
  background: var(--bg);
  user-select: none;
}

.txt {
  position: relative;
  z-index: 1;
  white-space: pre;
}

.sel {
  position: absolute;
  top: 0;
  height: 100%;
  background: rgb(122 162 247 / 0.25);
  z-index: 0;
}

.caret {
  position: absolute;
  top: 2px;
  bottom: 2px;
  width: 2px;
  background: var(--accent);
  z-index: 1;
  animation: caret-blink 1.1s steps(2, start) infinite;
}

/* Normal/visual mode: steady block over the char, beneath the text. */
.caret.block {
  top: 0;
  bottom: 0;
  background: rgb(122 162 247 / 0.4);
  outline: 1px solid rgb(122 162 247 / 0.7);
  outline-offset: -1px;
  z-index: 0;
  animation: none;
}

@keyframes caret-blink {
  50% {
    opacity: 0;
  }
}

/* --- soft wrap ---------------------------------------------------------
   The scroller gets .wrap; lines take natural height and the caret /
   selection are inline marks that ride the wrapped rows. */
#scroller.wrap #lines {
  width: 100%;
  padding-right: 0;
}
#scroller.wrap .line {
  height: auto;
  min-height: var(--line-height);
}
#scroller.wrap .txt {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  min-width: 0; /* let the flex item shrink so text wraps instead of overflowing */
  flex: 1;
}
.icaret {
  display: inline-block;
  width: 0;
  height: 1.2em;
  vertical-align: text-bottom;
  border-left: 2px solid var(--accent);
  margin-right: -2px;
  animation: caret-blink 1.1s steps(2, start) infinite;
}
.cblk {
  background: rgb(122 162 247 / 0.4);
  outline: 1px solid rgb(122 162 247 / 0.7);
  outline-offset: -1px;
  border-radius: 1px;
}
.cblk-end {
  display: inline-block;
  width: 1ch;
  height: 1.2em;
  vertical-align: text-bottom;
  background: rgb(122 162 247 / 0.4);
  outline: 1px solid rgb(122 162 247 / 0.7);
  outline-offset: -1px;
}
.selc {
  background: rgb(122 162 247 / 0.25);
}

/* Live :s substitution preview — the inserted replacement text. */
.spreview {
  background: rgb(158 206 106 / 0.28);
  outline: 1px solid rgb(158 206 106 / 0.55);
  outline-offset: -1px;
  border-radius: 2px;
}

.spreview:empty::after {
  /* deletion preview: a visible stub where text would be removed */
  content: "​";
  display: inline-block;
  width: 3px;
  height: 1em;
  vertical-align: text-bottom;
  background: rgb(158 206 106 / 0.55);
}

/* Search matches (vim `/` and the find bar). */
.smatch {
  background: rgb(224 175 104 / 0.28);
  border-radius: 2px;
}

.smatch-cur {
  background: rgb(224 175 104 / 0.55);
  outline: 1px solid rgb(224 175 104 / 0.9);
  outline-offset: -1px;
  border-radius: 2px;
}

#find-panel {
  position: absolute;
  top: 8px;
  right: 20px;
  z-index: 6;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px;
  background: var(--bg-panel);
  border: 1px solid rgb(122 162 247 / 0.4);
  border-radius: 8px;
}

#replace-row {
  display: flex;
  gap: 6px;
}

#find-bar,
#replace-bar {
  width: 220px;
  padding: 4px 8px;
  font: inherit;
  font-size: 12px;
  color: var(--fg);
  background: var(--bg);
  border: 1px solid rgb(122 162 247 / 0.3);
  border-radius: 5px;
  outline: none;
}

#replace-bar {
  flex: 1;
  width: auto;
  min-width: 0;
}

#find-bar:focus,
#replace-bar:focus {
  border-color: var(--accent);
}

#replace-row button {
  font: inherit;
  font-size: 11px;
  padding: 2px 8px;
  color: var(--fg);
  background: var(--bg);
  border: 1px solid rgb(122 162 247 / 0.3);
  border-radius: 5px;
  cursor: pointer;
}

#replace-row button:hover {
  border-color: var(--accent);
  color: var(--accent);
}
.sel-nub {
  display: inline-block;
  width: 0.5ch;
  height: 1.2em;
  vertical-align: text-bottom;
  background: rgb(122 162 247 / 0.25);
}

/* In-flight IME composition preview (not yet committed to the buffer). */
.ime {
  text-decoration: underline dotted;
  color: var(--accent);
}

/* --- syntax highlighting (tree-sitter captures → classes) ---------------- */

.ts-punct {
  color: #565f89;
}

.ts-string {
  color: #9ece6a;
}

.ts-number {
  color: #ff9e64;
}

.ts-const {
  color: #bb9af7;
}

.ts-key {
  color: #7aa2f7;
}

.ts-escape {
  color: #7dcfff;
}

.ts-keyword {
  color: #bb9af7;
}

.ts-type {
  color: #2ac3de;
}

.ts-fn {
  color: #7aa2f7;
}

.ts-comment {
  color: #565f89;
  font-style: italic;
}

/* ERROR / MISSING node squiggle — hover for the message */
.err {
  text-decoration-line: underline;
  text-decoration-style: wavy;
  text-decoration-color: #f7768e;
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
  cursor: help;
}

#measure {
  position: absolute;
  top: 0;
  left: -9999px;
  visibility: hidden;
  white-space: pre;
  font-family: var(--font-mono);
  font-size: 13px;
  tab-size: 4;
}

#placeholder {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--fg-dim);
  pointer-events: none;
}

/* Invisible but focusable: anchors keyboard input, and gets repositioned to
   the caret so the IME candidate window appears at the right spot. */
#hidden-input {
  position: absolute;
  top: 0;
  left: 0;
  width: 2px;
  height: var(--line-height);
  opacity: 0;
  border: none;
  outline: none;
  resize: none;
  pointer-events: none;
}

/* --- tree viewer ---------------------------------------------------------- */

#treeview {
  position: absolute;
  inset: 0;
  overflow: auto;
  padding: 8px 16px 24px;
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.6;
}

#tree-notice {
  color: #f7768e;
  padding: 2px 0 8px;
}

.tree-branch > summary {
  cursor: pointer;
  user-select: none;
}

.tree-branch > summary:hover .tree-label {
  text-decoration: underline;
}

.tree-children {
  margin-left: 7px;
  padding-left: 14px;
  border-left: 1px solid var(--border);
}

.tree-leaf {
  display: flex;
  gap: 10px;
  align-items: baseline;
}

.tree-label {
  color: var(--accent);
}

.tree-meta {
  color: var(--fg-dim);
  font-size: 11px;
  margin-left: 8px;
}

.tree-tools {
  margin-left: 10px;
  visibility: hidden;
  color: var(--fg-dim);
}

.tree-branch > summary:hover > .tree-tools {
  visibility: visible;
}

.tree-tools span {
  cursor: pointer;
  padding: 0 3px;
}

.tree-tools span:hover {
  color: var(--accent);
}

/* Copy control on leaf rows (branches get it inside .tree-tools). */
.tree-leaf > .tree-copy {
  margin-left: 8px;
  cursor: pointer;
  visibility: hidden;
  color: var(--fg-dim);
}

.tree-leaf:hover > .tree-copy {
  visibility: visible;
}

.tree-leaf > .tree-copy:hover {
  color: var(--accent);
}

.tree-value {
  cursor: pointer;
  border-radius: 4px;
  padding: 0 4px;
}

.tree-value:hover {
  background: var(--bg-panel);
  outline: 1px solid var(--border);
}

.tree-str { color: #9ece6a; }
.tree-num { color: #ff9e64; }
.tree-const { color: #bb9af7; }
.tree-plain { color: var(--fg); }

.tree-edit {
  font: inherit;
  background: var(--bg);
  color: var(--fg);
  border: 1px solid var(--accent);
  border-radius: 4px;
  padding: 0 4px;
  outline: none;
}

.tree-truncated {
  color: var(--fg-dim);
  padding: 8px 0;
}

/* --- markdown preview ------------------------------------------------------ */

#md-root {
  max-width: 760px;
  margin: 0 auto;
  padding: 16px 8px 48px;
  font: 15px/1.65 system-ui, -apple-system, sans-serif;
  color: var(--fg);
}

#md-root h1,
#md-root h2,
#md-root h3,
#md-root h4 {
  margin: 1.2em 0 0.5em;
  line-height: 1.3;
  color: var(--fg);
}

#md-root h1 {
  font-size: 1.7em;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.3em;
}

#md-root h2 {
  font-size: 1.4em;
}

#md-root p,
#md-root ul,
#md-root ol {
  margin: 0.6em 0;
}

#md-root ul,
#md-root ol {
  padding-left: 1.6em;
}

#md-root li {
  margin: 0.25em 0;
}

#md-root code {
  font-family: var(--font-mono);
  font-size: 0.9em;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
}

#md-root pre {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
  overflow-x: auto;
  margin: 0.8em 0;
}

#md-root pre code {
  background: none;
  border: none;
  padding: 0;
}

#md-root blockquote {
  border-left: 3px solid var(--accent);
  margin: 0.8em 0;
  padding: 2px 0 2px 14px;
  color: var(--fg-dim);
}

#md-root a {
  color: var(--accent);
}

#md-root hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 1.5em 0;
}

#md-root table {
  border-collapse: collapse;
  margin: 0.8em 0;
}

#md-root th,
#md-root td {
  border: 1px solid var(--border);
  padding: 5px 12px;
}

#md-root th {
  background: var(--bg-panel);
}

#md-root img {
  max-width: 100%;
}

/* --- status bar ---------------------------------------------------------- */

#status {
  display: flex;
  gap: 16px;
  padding: 4px 12px;
  background: var(--bg-panel);
  border-top: 1px solid var(--border);
  color: var(--fg-dim);
  font-size: 12px;
}

#status-vim {
  color: var(--accent);
  font-weight: 600;
  font-family: var(--font-mono);
}

#status-name {
  color: var(--fg);
}

#status-msg {
  color: var(--accent);
}

#status-err {
  color: #f7768e;
  cursor: pointer;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

#status-err:hover {
  text-decoration: underline;
}

#status-hint {
  margin-left: auto;
}

#lang-select {
  background: transparent;
  color: var(--fg-dim);
  border: 1px solid transparent;
  border-radius: 4px;
  font: inherit;
  padding: 0 2px;
  cursor: pointer;
}

#lang-select:hover {
  color: var(--accent);
  border-color: var(--border);
}

#lang-select option {
  background: var(--bg-panel);
  color: var(--fg);
}

/* --- input event log (IME pipeline debugging) --------------------------- */

#event-log {
  position: fixed;
  right: 12px;
  bottom: 36px;
  width: 360px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.4);
  overflow: hidden;
}

#event-log header {
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  color: var(--fg-dim);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

#event-list {
  list-style: none;
  max-height: 40vh;
  overflow: auto;
  padding: 6px 10px;
  font-family: var(--font-mono);
  font-size: 11px;
  white-space: pre;
}

#event-list li {
  padding: 1px 0;
}

/* --- CSV table preview --------------------------------------------------- */
#table-root {
  padding: 12px 16px;
  overflow: auto;
}

.csv-table {
  border-collapse: collapse;
  font-size: 12.5px;
}

.csv-table th,
.csv-table td {
  padding: 4px 10px;
  border: 1px solid rgb(122 162 247 / 0.15);
  text-align: left;
  white-space: pre;
  max-width: 40ch;
  overflow: hidden;
  text-overflow: ellipsis;
}

.csv-table th {
  position: sticky;
  top: 0;
  background: var(--bg-panel);
  color: var(--accent);
  cursor: pointer;
  user-select: none;
}

.csv-table tbody tr:nth-child(even) {
  background: rgb(122 162 247 / 0.05);
}

.table-note {
  margin-top: 8px;
  color: var(--fg-dim);
  font-size: 11.5px;
}

/* --- diff preview --------------------------------------------------------- */
#diff-root {
  padding: 10px 0 24px;
  font-family: var(--font-mono);
  font-size: 12.5px;
  overflow: auto;
}

.diff-header {
  padding: 4px 16px 10px;
  color: var(--fg-dim);
  font-family: system-ui, sans-serif;
  font-size: 12px;
}

.diff-line {
  display: flex;
  white-space: pre;
  line-height: 1.5;
}

.diff-ln {
  flex: none;
  padding: 0 10px 0 8px;
  color: var(--fg-dim);
  user-select: none;
}

.diff-text {
  flex: 1;
  padding-right: 16px;
}

.diff-add {
  background: rgb(158 206 106 / 0.14);
}

.diff-add .diff-text {
  color: #9ece6a;
}

.diff-del {
  background: rgb(247 118 142 / 0.12);
}

.diff-del .diff-text {
  color: #f7768e;
}

.diff-sep {
  padding: 3px 16px;
  margin: 2px 0;
  color: var(--fg-dim);
  background: rgb(122 162 247 / 0.06);
  font-size: 11.5px;
  user-select: none;
}
