:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dde1e7;
  --text: #1c2129;
  --muted: #667085;
  --accent: #2f6fed;
  --accent-text: #ffffff;
  --focus: rgba(47, 111, 237, 0.35);

  --del-bg: #ffebe9;
  --del-ln: #ffd7d5;
  --del-mark: #ffaba8;
  --del-text: #b42318;
  --ins-bg: #e6ffec;
  --ins-ln: #ccffd8;
  --ins-mark: #8ee0a4;
  --ins-text: #067647;
  --conf-bg: #fff4d6;
  --conf-mark: #ffd36b;
  --conf-text: #b54708;
  --empty-bg: #f6f7f9;

  --shadow: 0 10px 30px rgba(16, 24, 40, 0.14);
  --mono: ui-monospace, SFMono-Regular, "Cascadia Code", Consolas, "Liberation Mono", Menlo, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1216;
    --surface: #171b21;
    --surface-2: #1f242c;
    --border: #2d333d;
    --text: #e6e9ee;
    --muted: #9aa3b2;
    --accent: #5b8cff;
    --accent-text: #0b1020;
    --focus: rgba(91, 140, 255, 0.4);
    --del-bg: #3a1d1f;
    --del-ln: #4a2326;
    --del-mark: #7d2f33;
    --del-text: #ff9b94;
    --ins-bg: #15301f;
    --ins-ln: #1b3d27;
    --ins-mark: #2b6a3f;
    --ins-text: #7ee2a0;
    --conf-bg: #3a2e12;
    --conf-mark: #7a5a12;
    --conf-text: #ffc554;
    --empty-bg: #13161b;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0f1216;
  --surface: #171b21;
  --surface-2: #1f242c;
  --border: #2d333d;
  --text: #e6e9ee;
  --muted: #9aa3b2;
  --accent: #5b8cff;
  --accent-text: #0b1020;
  --focus: rgba(91, 140, 255, 0.4);
  --del-bg: #3a1d1f;
  --del-ln: #4a2326;
  --del-mark: #7d2f33;
  --del-text: #ff9b94;
  --ins-bg: #15301f;
  --ins-ln: #1b3d27;
  --ins-mark: #2b6a3f;
  --ins-text: #7ee2a0;
  --conf-bg: #3a2e12;
  --conf-mark: #7a5a12;
  --conf-text: #ffc554;
  --empty-bg: #13161b;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--sans);
  min-height: 100vh;
}

button, input, textarea { font: inherit; color: inherit; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.icon { width: 16px; height: 16px; fill: currentColor; flex: none; }

/* Header */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { width: 28px; height: 28px; }
.brand-name { font-weight: 700; font-size: 18px; letter-spacing: -0.01em; }
.header-actions { display: flex; gap: 6px; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 32px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 0.12s, border-color 0.12s;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.icon-only { width: 32px; padding: 0; justify-content: center; }
.btn.danger { color: var(--del-text); }
.btn.small { height: 28px; padding: 0 10px; font-size: 13px; }

.count {
  min-width: 20px;
  padding: 0 6px;
  border-radius: 10px;
  background: var(--surface-2);
  font-size: 12px;
  text-align: center;
  color: var(--muted);
}

/* Tabs */
.tabbar {
  display: flex;
  align-items: flex-end;
  gap: 4px;
  padding: 8px 20px 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.tabs {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  min-width: 0;
}

.tab {
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
  max-width: 220px;
  height: 34px;
  padding: 0 6px 0 12px;
  border: 1px solid transparent;
  border-bottom: none;
  border-radius: 8px 8px 0 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  flex: none;
  margin-bottom: -1px;
}
.tab:hover { background: var(--surface-2); color: var(--text); }
.tab.active {
  background: var(--bg);
  border-color: var(--border);
  color: var(--text);
  font-weight: 600;
}
.tab-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tab-badge { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); flex: none; }
.tab-close {
  display: grid;
  place-items: center;
  width: 20px;
  height: 20px;
  border: none;
  border-radius: 5px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  flex: none;
}
.tab-close:hover { background: var(--border); color: var(--text); }
.tab-rename {
  width: 150px;
  height: 24px;
  padding: 0 6px;
  border: 1px solid var(--accent);
  border-radius: 5px;
  background: var(--surface);
}

.tab-add {
  flex: none;
  width: 32px;
  height: 30px;
  margin-bottom: 3px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}
.tab-add:hover { background: var(--surface-2); color: var(--text); }

/* Workspace */
.workspace { padding: 16px 20px 40px; max-width: 1800px; margin: 0 auto; }

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px 16px;
  margin-bottom: 12px;
}
.toolbar-group { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px; }
.toolbar-group:last-child { gap: 8px; }

.check { display: inline-flex; align-items: center; gap: 7px; cursor: pointer; user-select: none; }
.check input { width: 16px; height: 16px; accent-color: var(--accent); margin: 0; }

.segmented {
  display: inline-flex;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
}
.segmented button {
  height: 26px;
  padding: 0 12px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}
.segmented button.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12); font-weight: 600; }

/* Inputs */
.inputs { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 16px; }
.inputs.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.pane[hidden] { display: none; }

.pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  overflow: hidden;
}
.pane.dragover { border-color: var(--accent); box-shadow: 0 0 0 3px var(--focus); }
.pane-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
}
.pane-title { font-weight: 600; }
.pane-meta { color: var(--muted); font-size: 12px; margin-right: auto; }
.file-link { color: var(--accent); font-size: 13px; cursor: pointer; }
.file-link:hover { text-decoration: underline; }

/* Editor: a transparent textarea over a backdrop that paints the diff. Both must share font metrics. */
.editor { position: relative; flex: 1; overflow: hidden; }
.editor textarea,
.highlights {
  font: 13px/1.5 var(--mono);
  letter-spacing: normal;
  tab-size: 4;
  white-space: pre;
}
.editor textarea {
  position: relative;
  z-index: 1;
  display: block;
  width: 100%;
  height: max(320px, calc(100vh - 250px));
  min-height: 160px;
  margin: 0;
  padding: 10px 12px;
  border: none;
  outline: none;
  resize: vertical;
  background: transparent;
  overflow-wrap: normal;
}
.backdrop { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }
.highlights {
  width: max-content;
  min-width: 100%;
  padding: 10px 0;
  color: transparent;
  will-change: transform;
}
.hl-line { padding: 0 12px; }
.hl-line.del { background: var(--del-bg); }
.hl-line.ins { background: var(--ins-bg); }
.hl-line mark { color: transparent; border-radius: 2px; }
.hl-line.del mark { background: var(--del-mark); }
.hl-line.ins mark { background: var(--ins-mark); }
.hl-line.conflict { background: var(--conf-bg); }
.hl-line.conflict mark { background: var(--conf-mark); }
/* Marker where the compared side added/removed lines that have no counterpart here. */
.hl-line.gap-before.gap-ins { box-shadow: inset 0 2px 0 var(--ins-text); }
.hl-line.gap-before.gap-del { box-shadow: inset 0 2px 0 var(--del-text); }
.hl-line.gap-before.gap-conflict { box-shadow: inset 0 2px 0 var(--conf-text); }
.hl-line.gap-after.gap-ins { box-shadow: inset 0 -2px 0 var(--ins-text); }
.hl-line.gap-after.gap-del { box-shadow: inset 0 -2px 0 var(--del-text); }
.hl-line.gap-after.gap-conflict { box-shadow: inset 0 -2px 0 var(--conf-text); }
.hl-line.current { outline: 1px solid var(--accent); outline-offset: -1px; }

/* Result */
.summary {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  min-height: 32px;
  margin-bottom: 12px;
}
.pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 26px;
  padding: 0 10px;
  border-radius: 13px;
  font-weight: 600;
  font-size: 13px;
}
.pill.del { background: var(--del-bg); color: var(--del-text); }
.pill.ins { background: var(--ins-bg); color: var(--ins-text); }
.pill.same { background: var(--surface-2); color: var(--muted); }
.pill.conflict { background: var(--conf-bg); color: var(--conf-text); }
.summary .spacer { flex: 1; }
.summary .muted { color: var(--muted); font-size: 13px; }

/* Drawer */
.scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.3); z-index: 20; }
.drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 21;
  display: flex;
  flex-direction: column;
  width: min(400px, 100vw);
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: var(--shadow);
}
.drawer[hidden], .scrim[hidden] { display: none; }
.drawer-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 8px; }
.drawer-head h2 { margin: 0; font-size: 16px; }
.search {
  margin: 4px 16px 10px;
  height: 34px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
}
.saved-list { list-style: none; margin: 0; padding: 0 8px 16px; overflow-y: auto; flex: 1; }
.saved-item {
  padding: 10px 10px;
  border-radius: 8px;
  border: 1px solid transparent;
}
.saved-item:hover { background: var(--surface-2); }
.saved-item.current { border-color: var(--accent); }
.saved-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.saved-meta { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 2px 0 8px; color: var(--muted); font-size: 12px; }
.saved-meta .del { color: var(--del-text); font-weight: 600; }
.saved-meta .ins { color: var(--ins-text); font-weight: 600; }
.saved-meta .conflict { color: var(--conf-text); font-weight: 600; }
.saved-actions { display: flex; gap: 6px; }
.saved-empty { padding: 32px 12px; color: var(--muted); text-align: center; }

/* Dialog */
dialog {
  width: min(420px, calc(100vw - 32px));
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.35); }
dialog form { padding: 20px; }
dialog h2 { margin: 0 0 14px; font-size: 17px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field span { font-weight: 600; font-size: 13px; }
.field input {
  height: 36px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
}
.field input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--focus); }
.hint { margin: 8px 0 18px; color: var(--muted); font-size: 13px; }
.dialog-actions { display: flex; flex-direction: row-reverse; justify-content: flex-start; gap: 8px; }
.dialog-actions [hidden] { display: none; }

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  z-index: 30;
  transform: translateX(-50%);
  padding: 10px 16px;
  border-radius: 8px;
  background: var(--text);
  color: var(--bg);
  box-shadow: var(--shadow);
  font-weight: 500;
}
.toast[hidden] { display: none; }

@media (max-width: 760px) {
  .app-header, .tabbar { padding-left: 16px; padding-right: 16px; }
  .workspace { padding: 12px 16px 32px; }
  .inputs, .inputs.three { grid-template-columns: 1fr; }
  .editor textarea { height: 260px; }
}
