:root {
  color-scheme: light;
  --bg: #e9e9e9;
  --panel: #f5f5f5;
  --paper: #fff;
  --line: #d5d5d5;
  --line-strong: #c4c4c4;
  --text: #222;
  --muted: #667;
  --blue: #2f6fad;
  --blue-2: #3d8ee6;
  --select: #cfe6fb;
  --select-2: #b9daf6;
  --header: #f3f3f3;
  --green: #e5f6d8;
  --green-2: #2f8f4e;
  --red: #c64545;
  --danger-bg: #fdecec;
  --null: #8d949c;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.16);
  --sans: "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --mono: "Cascadia Mono", "SF Mono", ui-monospace, Menlo, Consolas, monospace;
  --side: 272px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 12px/1.4 var(--sans);
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: default; opacity: 0.45; }
::selection { background: #b9d7f5; }
* { scrollbar-width: thin; scrollbar-color: #c5c5c5 transparent; }

.app {
  height: 100vh;
  display: grid;
  grid-template-rows: 40px minmax(0, 1fr) 24px;
}

.top {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 8px;
  background: linear-gradient(#fbfbfb, #ececec);
  border-bottom: 1px solid var(--line-strong);
}
.brand {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--blue);
  font-weight: 650;
  padding-right: 8px;
  border-right: 1px solid var(--line);
  margin-right: 4px;
}
.brand svg { width: 16px; height: 16px; }
.tools { display: flex; align-items: center; gap: 4px; min-width: 0; }
.tool, .objbar button, .recbar button, .side-search button {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 26px;
  padding: 0 8px;
  border: 1px solid transparent;
  border-radius: 3px;
  background: transparent;
}
.tool:hover, .objbar button:hover, .recbar button:hover { background: #fff; border-color: var(--line); }
.tool svg, .objbar svg { width: 14px; height: 14px; }
.conn { margin-left: auto; display: flex; align-items: center; gap: 6px; color: var(--muted); white-space: nowrap; }
.conn::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: #b5b5b5; }
.conn.ok { color: var(--text); }
.conn.ok::before { background: #3caa62; }
.conn.bad { color: var(--red); }
.conn.bad::before { background: var(--red); }

.workspace { display: flex; min-height: 0; }
#side {
  width: var(--side);
  flex: none;
  display: flex;
  flex-direction: column;
  min-width: 180px;
  background: var(--panel);
  border-right: 1px solid var(--line);
}
.vsplit { width: 4px; cursor: col-resize; background: #e3e3e3; flex: none; }
.vsplit:hover { background: #c5dff6; }
.side-search, .side-foot { padding: 6px 8px; border-bottom: 1px solid var(--line); }
.side-foot { border-bottom: 0; border-top: 1px solid var(--line); color: var(--muted); display: flex; gap: 6px; align-items: center; }
.side-search input, .query-db, .design-head input, .design-head select, .field-grid input, .field-grid select, .idx-grid input, .cell-input, .filter-input {
  width: 100%;
  height: 24px;
  border: 1px solid var(--line-strong);
  border-radius: 2px;
  background: #fff;
  padding: 0 6px;
}
.tree { flex: 1; overflow: auto; padding: 4px 0 8px; }
.muted { color: var(--muted); }
.pad { padding: 8px 10px; }
.node { width: 100%; display: flex; align-items: center; gap: 4px; text-align: left; border: 0; background: transparent; height: 22px; padding: 0 8px 0 4px; }
.node:hover { background: #eaf3fb; }
.node.selected { background: var(--select); }
.node .label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.node.open .label { color: var(--blue); font-weight: 600; }
.lamp { width: 8px; height: 8px; border-radius: 50%; background: #b5b5b5; flex: none; }
.lamp.open { background: #2f9e4f; }
.lamp.opening { background: #e0b15a; }
.lamp.error { background: #d45454; }
.conn-node .label { font-weight: 600; }
.chev { width: 12px; color: #777; flex: none; }
.group.open > .node .chev { transform: rotate(90deg); display: inline-block; }
.children { padding-left: 14px; }
.ico { width: 14px; height: 14px; flex: none; }
.folder {
  width: 14px; height: 10px; border: 1px solid #d2a12e; background: #f6d56b; position: relative; flex: none;
}
.folder::before {
  content: ""; position: absolute; left: -1px; top: -4px; width: 7px; height: 4px;
  background: #f6d56b; border: 1px solid #d2a12e; border-bottom: 0;
}

main { flex: 1; min-width: 0; display: flex; flex-direction: column; background: #f0f0f0; }
.tabbar {
  display: flex;
  align-items: flex-end;
  gap: 2px;
  height: 32px;
  padding: 0 6px;
  background: #e4e4e4;
  border-bottom: 1px solid var(--line-strong);
  overflow: auto hidden;
}
.tab {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 220px;
  height: 26px;
  padding: 0 8px;
  border: 1px solid var(--line-strong);
  border-bottom: 0;
  border-radius: 4px 4px 0 0;
  background: #dedede;
  color: #444;
}
.tab.active { background: var(--paper); height: 28px; color: var(--text); font-weight: 600; }
.tab .label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tab .x { border: 0; background: transparent; color: #777; width: 16px; height: 16px; line-height: 14px; padding: 0; }
.tab .x:hover { color: var(--red); }
.doc { flex: 1; min-height: 0; display: flex; flex-direction: column; background: var(--paper); }
.welcome { margin: auto; text-align: center; color: var(--muted); }
.welcome h2 { margin: 0 0 6px; color: var(--text); font-size: 16px; font-weight: 600; }

.obj { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.objbar, .recbar, .design-head, .query-bar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  background: linear-gradient(#fafafa, #f0f0f0);
  border-bottom: 1px solid var(--line);
}
.objbar .sep, .query-bar .sep { width: 1px; height: 16px; background: var(--line); margin: 0 4px; }
.spacer { flex: 1; }
.hint { color: var(--muted); }
.apply { color: var(--green-2); font-weight: 650; }
.danger { color: var(--red); }

.sheet-scroll { flex: 1; min-height: 0; overflow: auto; background: #fff; }
.sheet { border-collapse: separate; border-spacing: 0; min-width: 100%; }
.sheet th, .sheet td { border-right: 1px solid #ececec; border-bottom: 1px solid #ececec; padding: 0; height: 24px; white-space: nowrap; }
.sheet th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: linear-gradient(#fafafa, #efefef);
  font-weight: 600;
  text-align: left;
}
.sheet tr.filter th { top: 24px; background: #f7f7f7; font-weight: 400; }
.sheet th .h, .sheet td .c { display: block; padding: 2px 8px; min-width: 72px; max-width: 360px; overflow: hidden; text-overflow: ellipsis; }
.sheet th .h { cursor: pointer; }
.sheet th .mark { color: var(--blue); margin-left: 4px; }
.sheet td .c { font-family: var(--mono); font-size: 12px; }
.sheet td.num .c { text-align: right; }
.sheet td.null .c { color: var(--null); font-style: italic; font-family: var(--sans); }
.sheet td.current { box-shadow: inset 0 0 0 2px var(--blue-2); }
.sheet tr.selected td { background: var(--select); }
.sheet td.dirty { background: var(--green) !important; }
.sheet tr.deleted td { background: var(--danger-bg); color: #999; }
.sheet tr.deleted .c { text-decoration: line-through; }
.sheet tr.insert td { background: #f8fcf3; }
.gutter {
  position: sticky;
  left: 0;
  z-index: 1;
  width: 42px;
  min-width: 42px;
  background: #f3f3f3;
  text-align: right;
  color: #777;
}
.sheet th.gutter { z-index: 3; }
.gutter .c, .gutter .h { min-width: 0; text-align: right; font-family: var(--sans); }
.cell-input, .filter-input { height: 22px; border: 0; border-radius: 0; padding: 0 6px; font-family: var(--mono); }
.recbar { border-top: 1px solid var(--line); border-bottom: 0; background: #f7f7f7; }
.banner { margin: 6px 8px 0; padding: 6px 8px; background: #fff4f2; color: var(--red); border: 1px solid #f3c7c2; }
.banner.warn { background: #fff8e8; color: #8a5a00; border-color: #f0ddaa; }

.design { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.design-head input { width: 180px; }
.subtabs { display: flex; gap: 0; padding: 0 8px; background: #f7f7f7; border-bottom: 1px solid var(--line); }
.subtabs button { border: 0; border-bottom: 2px solid transparent; background: transparent; height: 28px; padding: 0 12px; color: var(--muted); }
.subtabs button.on { color: var(--blue); border-bottom-color: var(--blue); font-weight: 650; }
.design-body { flex: 1; min-height: 0; overflow: auto; }
.field-grid, .idx-grid { width: 100%; border-collapse: collapse; }
.field-grid th, .idx-grid th { position: sticky; top: 0; background: var(--header); text-align: left; padding: 4px 6px; border-bottom: 1px solid var(--line); }
.field-grid td, .idx-grid td { border-bottom: 1px solid #eee; padding: 2px; }
.field-grid input, .field-grid select, .idx-grid input { border: 1px solid transparent; background: transparent; }
.field-grid input:focus, .field-grid select:focus, .idx-grid input:focus { border-color: var(--blue-2); background: #fff; outline: none; }
.sql-preview { margin: 0; padding: 12px; white-space: pre-wrap; font: 12px/1.5 var(--mono); }
.fk { padding: 8px 12px; color: var(--muted); }

.query { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.query-db { width: 140px; }
.editor { height: 220px; min-height: 100px; display: flex; flex-direction: column; background: #fff; }
.editor-box { position: relative; flex: 1; min-height: 0; }
.gutter-nums, #sql-editor { font: 12px/20px var(--mono); padding-top: 8px; }
.gutter-nums {
  position: absolute; left: 0; top: 0; bottom: 0; width: 42px; overflow: hidden;
  text-align: right; padding-right: 8px; color: #9aa; user-select: none; white-space: pre; background: #fafafa; border-right: 1px solid var(--line);
}
#sql-editor {
  width: 100%; height: 100%; border: 0; resize: none; outline: none; padding: 8px 8px 8px 50px; white-space: pre; overflow: auto;
}
.hsplit { height: 4px; cursor: row-resize; background: #e3e3e3; }
.hsplit:hover { background: #c5dff6; }
.query-results { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.msg { padding: 8px 12px; font-family: var(--mono); white-space: pre-wrap; }
.msg.bad { color: var(--red); }

.menu {
  position: fixed;
  z-index: 30;
  min-width: 168px;
  padding: 4px 0;
  background: #fff;
  border: 1px solid #cfcfcf;
  box-shadow: var(--shadow);
}
.menu button { display: block; width: 100%; text-align: left; border: 0; background: transparent; padding: 5px 16px; }
.menu button:hover:not(:disabled) { background: var(--select); }
.menu hr { border: 0; border-top: 1px solid #eee; margin: 4px 0; }

.mask { position: fixed; inset: 0; z-index: 40; background: rgba(0,0,0,.25); display: grid; place-items: center; }
.dialog { width: min(460px, calc(100vw - 32px)); background: #fff; border: 1px solid #cfcfcf; box-shadow: var(--shadow); }
.dialog header, .dialog footer { padding: 10px 12px; background: #f7f7f7; }
.dialog header { font-weight: 650; border-bottom: 1px solid var(--line); }
.dialog footer { display: flex; align-items: center; justify-content: flex-end; gap: 8px; border-top: 1px solid var(--line); }
.dialog footer .spacer { flex: 1; }
.form { display: grid; grid-template-columns: 72px minmax(0, 1fr); gap: 8px 10px; padding: 14px 12px 4px; align-items: center; }
.form input { height: 26px; border: 1px solid var(--line-strong); padding: 0 6px; width: 100%; }
.form-msg { color: var(--muted); max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conn-dialog { width: min(440px, calc(100vw - 32px)); }
.conn-dialog .hint { margin: 0; padding: 4px 12px 12px; color: var(--muted); }
.dialog p { margin: 0; padding: 16px 12px; }
.dialog .primary { background: var(--blue); color: #fff; border: 1px solid var(--blue); height: 26px; padding: 0 12px; }
.dialog .plain { background: #fff; border: 1px solid var(--line-strong); height: 26px; padding: 0 12px; }
.dialog textarea { width: calc(100% - 24px); margin: 12px; min-height: 180px; font-family: var(--mono); }

.status {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  padding: 0 8px;
  background: #f7f7f7;
  border-top: 1px solid var(--line-strong);
  color: #555;
}

@media (max-width: 860px) {
  body { overflow: auto; }
  .app { height: auto; min-height: 100vh; }
  .workspace { flex-direction: column; }
  #side { width: 100% !important; max-height: 240px; }
  .vsplit { display: none; }
  main { min-height: 520px; }
}
