* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background-color: #fff;
  background-image:
    linear-gradient(#ececec 1px, transparent 1px),
    linear-gradient(90deg, #ececec 1px, transparent 1px);
  background-size: 52px 52px;
  color: #1a1a1a;
  font: 13px/1.35 ui-sans-serif, system-ui, "Segoe UI", sans-serif;
}

button, .name { font: inherit; color: inherit; }

button {
  cursor: pointer;
}

button:focus-visible,
.name:focus-visible {
  outline: 2px solid #2f6fed;
  outline-offset: 2px;
}

body.busy { user-select: none; }
body.moving { cursor: grabbing; }
body.linking .col.pk .pk-hit { cursor: grabbing; }
body.linking #wires { z-index: 8; }

#wires {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 5;
}

#tables {
  position: fixed;
  inset: 0;
  z-index: 7;
  transform-origin: 0 0;
}

.chrome {
  position: fixed;
  z-index: 20;
  display: flex;
  gap: 8px;
}

.top-right {
  top: 16px;
  right: 18px;
}

.ic {
  display: block;
  width: 16px;
  height: 16px;
  flex: none;
  background-color: currentColor;
  -webkit-mask: var(--ic) center / contain no-repeat;
  mask: var(--ic) center / contain no-repeat;
  pointer-events: none;
}

.ic[data-ic="plus"] { --ic: url("icons/plus.svg"); }
.ic[data-ic="cross"] { --ic: url("icons/cross.svg"); }
.ic[data-ic="save"] { --ic: url("icons/save.svg"); }
.ic[data-ic="arrow-top"] { --ic: url("icons/arrow-top.svg"); }
.ic[data-ic="zoom"] { --ic: url("icons/zoom.svg"); }
.ic[data-ic="zoom-in"] { --ic: url("icons/zoom-in.svg"); }
.ic[data-ic="zoom-out"] { --ic: url("icons/zoom-out.svg"); }
.ic[data-ic="crop"] { --ic: url("icons/crop.svg"); }
.ic[data-ic="key"] { --ic: url("icons/key.svg"); }

.chrome > button,
.export-wrap > button,
.import-wrap > button {
  background: #fff;
  border: 1px solid #d8d8d8;
  border-radius: 6px;
  width: 34px;
  height: 34px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.chrome .ic {
  width: 17px;
  height: 17px;
}

.bottom-left {
  left: 18px;
  bottom: 22px;
}

.chrome > button,
.export-wrap > button,
.import-wrap > button,
#add-table,
.circle,
.move {
  transition: background-color 140ms ease, transform 100ms ease, color 140ms ease;
}

@media (hover: hover) and (pointer: fine) {
  .chrome > button:hover,
  .export-wrap > button:hover,
  .import-wrap > button:hover,
  .menu button:hover {
    background: #f5f5f5;
  }
}

.chrome > button:active,
.export-wrap > button:active,
.import-wrap > button:active {
  transform: scale(0.97);
}

.export-wrap,
.import-wrap { position: relative; }

.menu {
  position: absolute;
  right: 0;
  top: calc(100% + 4px);
  min-width: 112px;
  padding: 4px;
  background: #fff;
  border: 1px solid #d8d8d8;
  border-radius: 6px;
  transform-origin: 100% 0;
  animation: enter 160ms cubic-bezier(0.215, 0.61, 0.355, 1);
}

.import-wrap .menu {
  min-width: 176px;
}

.import-wrap .menu [data-example="1nf"] {
  margin-top: 4px;
  border-top: 1px solid #eee;
  border-radius: 0;
  padding-top: 10px;
}

.import-wrap .menu [data-example="1nf"]:hover {
  border-radius: 4px;
}

.menu button {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  padding: 7px 10px;
  border-radius: 4px;
}

#add-table {
  position: fixed;
  right: 22px;
  bottom: 22px;
  z-index: 20;
  width: 48px;
  height: 48px;
  border: 0;
  border-radius: 50%;
  background: #1a1a1a;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

#add-table .ic {
  width: 22px;
  height: 22px;
}

#add-table:hover { background: #333; }
#add-table:active { transform: scale(0.96); }

#note {
  position: fixed;
  top: 18px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 30;
  background: #1a1a1a;
  color: #fff;
  padding: 8px 12px;
  border-radius: 6px;
}

#note.show {
  animation: noteIn 180ms cubic-bezier(0.215, 0.61, 0.355, 1);
}

.tbl {
  position: absolute;
  min-width: 208px;
  max-width: 260px;
  background: #fff;
  border: 1px solid #d4d4d4;
  border-radius: 4px;
  box-shadow: 0 1px 2px rgba(26, 26, 26, 0.06);
  user-select: none;
}

.tbl.enter {
  animation: enter 200ms cubic-bezier(0.215, 0.61, 0.355, 1);
}

.tbl .name { user-select: text; }

.tbl-head {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 36px;
  padding: 7px 10px;
  background: #e9e9e9;
  border-radius: 3px 3px 0 0;
  font-weight: 650;
  cursor: grab;
}

.tbl-head .name {
  flex: 0 1 auto;
  min-width: 0;
  max-width: calc(100% - 48px);
  cursor: text;
}

.head-grip {
  flex: 1;
  align-self: stretch;
  min-width: 18px;
  cursor: grab;
}

.tbl-del {
  margin-left: auto;
  cursor: pointer;
  opacity: 0;
  pointer-events: none;
  transition: opacity 140ms ease, background-color 140ms ease;
}

.tbl:hover .tbl-del {
  opacity: 1;
  pointer-events: auto;
}

.name {
  outline: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: text;
}

.name:focus {
  overflow: visible;
  text-overflow: clip;
  background: #fff;
  box-shadow: inset 0 0 0 1px #c8c8c8;
  border-radius: 2px;
  padding: 0 2px;
}

.sep {
  position: relative;
  height: 1px;
  background: #ebebeb;
  z-index: 2;
  transition: background-color 140ms ease, box-shadow 140ms ease;
}

.sep::after {
  content: "";
  position: absolute;
  inset: -7px 0;
}

body:not(.linking) .sep:hover {
  background: #2f6fed;
  box-shadow: 0 -1px 0 #2f6fed, 0 1px 0 #2f6fed;
}

.sep.hot {
  background: var(--hot, #2f6fed);
  box-shadow: 0 -1px 0 var(--hot, #2f6fed), 0 1px 0 var(--hot, #2f6fed);
}

.sep-plus {
  display: flex;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%) scale(0.86);
  width: 18px;
  height: 18px;
  border: 0;
  border-radius: 50%;
  background: #2f6fed;
  color: #fff;
  align-items: center;
  justify-content: center;
  padding: 0;
  z-index: 3;
  opacity: 0;
  pointer-events: none;
  transition: opacity 140ms ease, transform 140ms ease;
}

.sep-plus .ic {
  width: 10px;
  height: 10px;
}

body:not(.linking) .sep:hover .sep-plus {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
  pointer-events: auto;
}

body.linking .sep-plus { opacity: 0 !important; pointer-events: none !important; }
body.linking .sep:hover { background: #ebebeb; box-shadow: none; }
body.linking .sep.hot {
  background: var(--hot, #2f6fed);
  box-shadow: 0 -1px 0 var(--hot, #2f6fed), 0 1px 0 var(--hot, #2f6fed);
}

.tbl-cols { padding-bottom: 26px; }

.col {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 32px;
  padding: 5px 10px;
  position: relative;
  transition: background-color 160ms ease, color 160ms ease;
}

.col.enter {
  animation: fade 160ms ease-out;
}

.col.pk,
.col.fk { font-weight: 700; }

.col.fk {
  color: var(--fk);
  background: color-mix(in srgb, var(--fk) 14%, #fff);
}

.pk-hit {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  margin: 0 -4px 0 -2px;
  flex: none;
  cursor: grab;
  touch-action: none;
  user-select: none;
}

.pk-ic {
  width: 14px;
  height: 14px;
  color: #1a1a1a;
  pointer-events: none;
}

.col:not(.pk) .pk-hit { visibility: hidden; pointer-events: none; }

.move,
.key-btn,
.pk-hit { touch-action: none; }

.col .name { flex: 1; min-width: 0; }

.col.fk .pk-ic { color: var(--fk); }

.col-actions {
  display: flex;
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  gap: 4px;
  z-index: 3;
  opacity: 0;
  pointer-events: none;
  transition: opacity 140ms ease;
}

.col:hover .col-actions {
  opacity: 1;
  pointer-events: auto;
}
.col:hover .name { padding-right: 56px; }

body.linking .col-actions,
body.busy .col-actions {
  opacity: 0 !important;
  pointer-events: none !important;
}

.circle {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 1px solid #d4d4d4;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  color: #444;
}

.circle:hover { background: #f4f4f4; }
.circle:active { transform: scale(0.96); }

.circle.key-off {
  position: relative;
}

.circle.key-off::after {
  content: "";
  position: absolute;
  width: 12px;
  height: 1.6px;
  background: currentColor;
  transform: rotate(-45deg);
  pointer-events: none;
}

.circle svg,
.circle .ic {
  width: 12px;
  height: 12px;
  display: block;
}

.move {
  position: absolute;
  right: -1px;
  bottom: -1px;
  width: 22px;
  height: 22px;
  padding: 0;
  border: 1px solid #d4d4d4;
  border-radius: 4px 0 4px 0;
  background: #e9e9e9;
  color: #6a6a6a;
  cursor: grab;
}

.move:hover { color: #1a1a1a; }
.move:active,
body.moving .move,
body.moving .tbl-head,
body.moving .head-grip { cursor: grabbing; }

.move svg {
  width: 14px;
  height: 14px;
  display: block;
  margin: auto;
}

@keyframes enter {
  from { opacity: 0; transform: scale(0.96); }
  to { opacity: 1; transform: none; }
}

@keyframes fade {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes noteIn {
  from { opacity: 0; transform: translateX(-50%) translateY(-6px); }
  to { opacity: 1; transform: translateX(-50%); }
}

@media (prefers-reduced-motion: reduce) {
  .tbl.enter,
  .col.enter,
  .menu,
  #note.show {
    animation: none;
  }

  .sep,
  .sep-plus,
  .col-actions,
  .tbl-del,
  .chrome > button,
  .export-wrap > button,
  #add-table,
  .circle,
  .move,
  .col {
    transition: none;
  }
}
