:root {
  --bg: #f0f0f0;
  --panel: #f7f7f7;
  --border: #c8c8c8;
  --text: #1e1e1e;
  --muted: #6b6b6b;
  --editor-bg: #ffffff;
  --gutter-bg: #e4e4e4;
  --gutter-text: #808080;
  --cur-line: #e8e8ff;
  --tab-bg: #dcdcdc;
  --tab-active: #ffffff;
  --tab-accent: #f59b00;
  --accent: #2f6fd1;
  --hover: #dde8f7;
  --saved: #2f6fd1;
  --unsaved: #d62b2b;
  --font-mono: Consolas, "Courier New", ui-monospace, monospace;
  --font-size: 14px;
  --line-h: 1.45;
}
body.dark {
  --bg: #2b2b2b;
  --panel: #313131;
  --border: #474747;
  --text: #dcdcdc;
  --muted: #9a9a9a;
  --editor-bg: #1e1e1e;
  --gutter-bg: #262626;
  --gutter-text: #6e6e6e;
  --cur-line: #2a2d3a;
  --tab-bg: #3a3a3a;
  --tab-active: #1e1e1e;
  --hover: #3b4252;
  --saved: #6aa3ff;
  --unsaved: #ff6b6b;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 13px/1.4 "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}
.hidden { display: none !important; }
button { font: inherit; color: inherit; }
kbd { font: 11px var(--font-mono); color: var(--muted); margin-left: auto; }

/* ---------- Login ---------- */
.login {
  height: 100%;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, #3c6fb4, #5aa0d8);
}
.login-box {
  width: min(360px, 92vw);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 26px;
  box-shadow: 0 10px 40px rgba(0,0,0,.25);
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.login-title { font-size: 20px; font-weight: 600; }
.logo { color: var(--tab-accent); }
.login-box label { display: flex; flex-direction: column; gap: 4px; font-weight: 600; }
.login-box input {
  font: 14px inherit;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--editor-bg);
  color: var(--text);
}
.login-box input:focus { outline: 2px solid var(--accent); border-color: transparent; }
.login-actions { display: flex; gap: 8px; }
.login-actions button {
  flex: 1;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--tab-bg);
  cursor: pointer;
}
.login-actions button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.login-actions button:disabled { opacity: .6; cursor: wait; }
.login-msg { min-height: 1.2em; color: var(--unsaved); }
.login-msg.info { color: var(--accent); }
.hint { margin: 0; color: var(--muted); font-size: 12px; }

/* ---------- App layout ---------- */
.app { display: flex; flex-direction: column; height: 100%; }

.menubar {
  display: flex;
  align-items: stretch;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  user-select: none;
}
.menu { position: relative; }
.menu > span { display: block; padding: 4px 10px; cursor: default; }
.menu:hover > span, .menu.open > span { background: var(--hover); }
.dropdown {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  min-width: 250px;
  background: var(--panel);
  border: 1px solid var(--border);
  box-shadow: 2px 4px 12px rgba(0,0,0,.2);
  padding: 3px 0;
  z-index: 50;
}
.menu.open .dropdown { display: block; }
.dropdown button {
  display: flex;
  width: 100%;
  gap: 16px;
  padding: 5px 16px 5px 26px;
  border: 0;
  background: none;
  text-align: left;
  cursor: default;
}
.dropdown button:hover { background: var(--hover); }
.dropdown button.danger { color: var(--unsaved); }
.dropdown hr { border: 0; border-top: 1px solid var(--border); margin: 3px 0; }
.check { margin-left: auto; width: 1em; }
.check.on::before { content: "✓"; }
.menu-spacer { flex: 1; }
.user { padding: 4px 12px; color: var(--muted); }

.toolbar {
  display: flex;
  align-items: center;
  gap: 1px;
  padding: 2px 4px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.toolbar button {
  width: 28px; height: 26px;
  border: 1px solid transparent;
  border-radius: 3px;
  background: none;
  font-size: 14px;
  cursor: pointer;
  filter: saturate(.85);
}
.toolbar button:hover { background: var(--hover); border-color: var(--border); }
.toolbar .sep { width: 1px; height: 20px; background: var(--border); margin: 0 5px; }
.save-state { color: var(--muted); font-size: 12px; padding-left: 4px; }

.main { flex: 1; display: flex; min-height: 0; }

/* ---------- Document list ---------- */
.doclist {
  width: 230px;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  background: var(--panel);
  min-height: 0;
}
.doclist-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 5px 8px;
  font-weight: 600;
  border-bottom: 1px solid var(--border);
}
.doclist-head button { border: 0; background: none; cursor: pointer; font-size: 16px; }
.doclist-filter {
  margin: 6px;
  padding: 4px 6px;
  border: 1px solid var(--border);
  background: var(--editor-bg);
  color: var(--text);
  border-radius: 3px;
}
#docItems { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
#docItems li {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px;
  cursor: pointer;
  border-left: 3px solid transparent;
}
#docItems li:hover { background: var(--hover); }
#docItems li.active { background: var(--hover); border-left-color: var(--tab-accent); }
#docItems li.open .doc-name { font-weight: 600; }
.doc-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.doc-date { color: var(--muted); font-size: 11px; white-space: nowrap; }

/* ---------- Tabs ---------- */
.editor-area { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }
.tabs {
  display: flex;
  overflow-x: auto;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  padding-top: 3px;
  scrollbar-width: thin;
  min-height: 30px;
}
.tab {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 6px 4px 8px;
  margin-right: 1px;
  background: var(--tab-bg);
  border: 1px solid var(--border);
  border-bottom: 0;
  border-top: 3px solid transparent;
  border-radius: 3px 3px 0 0;
  white-space: nowrap;
  cursor: default;
  user-select: none;
  max-width: 220px;
}
.tab.active { background: var(--tab-active); border-top-color: var(--tab-accent); position: relative; top: 1px; }
.tab.drag-over { box-shadow: inset 3px 0 0 var(--accent); }
.tab .disk { font-size: 12px; color: var(--saved); }
.tab.dirty .disk { color: var(--unsaved); }
.tab .name { overflow: hidden; text-overflow: ellipsis; }
.tab .x {
  border: 0;
  background: none;
  width: 18px; height: 18px;
  border-radius: 3px;
  line-height: 1;
  cursor: pointer;
  color: var(--muted);
}
.tab .x:hover { background: #e05555; color: #fff; }
.tab input.rename {
  font: inherit;
  width: 140px;
  padding: 0 3px;
  border: 1px solid var(--accent);
  background: var(--editor-bg);
  color: var(--text);
}

/* ---------- Find bar ---------- */
.findbar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.findbar input[type="text"], .findbar input:not([type]) {
  padding: 3px 6px;
  border: 1px solid var(--border);
  background: var(--editor-bg);
  color: var(--text);
  width: 200px;
}
.findbar button {
  padding: 2px 8px;
  border: 1px solid var(--border);
  background: var(--tab-bg);
  border-radius: 3px;
  cursor: pointer;
}
.findbar:not(.replace-mode) .replace-only { display: none; }
.find-info { color: var(--muted); min-width: 80px; }

/* ---------- Editor ---------- */
.editor-wrap {
  flex: 1;
  display: flex;
  min-height: 0;
  background: var(--editor-bg);
  font-family: var(--font-mono);
  font-size: var(--font-size);
  line-height: var(--line-h);
}
.gutter {
  background: var(--gutter-bg);
  color: var(--gutter-text);
  text-align: right;
  overflow: hidden;
  user-select: none;
  border-right: 1px solid var(--border);
  min-width: 44px;
}
#gutterInner { padding: 4px 8px 50vh 10px; }
#gutterInner div.cur { color: var(--text); font-weight: 600; }
.text-holder { flex: 1; position: relative; min-width: 0; }
#editor, .mirror {
  font: inherit;
  line-height: inherit;
  padding: 4px 8px;
  tab-size: 4;
  letter-spacing: normal;
}
#editor {
  position: absolute;
  inset: 0;
  width: 100%; height: 100%;
  border: 0;
  outline: 0;
  resize: none;
  background: transparent;
  color: var(--text);
  white-space: pre;
  overflow: auto;
  z-index: 1;
}
#editor.wrap { white-space: pre-wrap; overflow-wrap: anywhere; }
.mirror {
  position: absolute;
  top: 0; left: 0;
  visibility: hidden;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  pointer-events: none;
}
.cur-line {
  position: absolute;
  left: 0; right: 0;
  background: var(--cur-line);
  pointer-events: none;
  z-index: 0;
}

.empty {
  position: absolute;
  inset: 31px 0 0 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  background: var(--editor-bg);
  z-index: 5;
}
.empty a { color: var(--accent); }

/* ---------- Status bar ---------- */
.statusbar {
  display: flex;
  gap: 0;
  background: var(--panel);
  border-top: 1px solid var(--border);
  font-size: 12px;
  user-select: none;
}
.statusbar span { padding: 2px 12px; border-left: 1px solid var(--border); white-space: nowrap; }
.statusbar span:first-child { border-left: 0; }
.statusbar .grow { flex: 1; border-left: 0; }

.toast {
  position: fixed;
  bottom: 36px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: #333;
  color: #fff;
  padding: 8px 16px;
  border-radius: 4px;
  opacity: 0;
  pointer-events: none;
  transition: .2s;
  z-index: 100;
}
.toast.show { opacity: 1; transform: translateX(-50%); }
.toast.error { background: #c62828; }

@media (max-width: 700px) {
  .doclist { position: absolute; z-index: 20; top: 58px; bottom: 22px; box-shadow: 4px 0 16px rgba(0,0,0,.2); }
  kbd { display: none; }
}
